Files
dragon-hd/tools/tex_survey.py

68 lines
2.1 KiB
Python

import struct, os, collections
res = r"F:\steam\steamapps\common\The I of the Dragon\Data\Textures.res"
data = open(res, "rb").read()
count = struct.unpack_from("<I", data, 0)[0]
p = 4
entries = []
for i in range(count):
ln = struct.unpack_from("<I", data, p)[0]; p += 4
nm = data[p:p+ln].decode("latin1"); p += ln
o, s = struct.unpack_from("<II", data, p); p += 8
entries.append((nm, o, s))
print("total entries:", len(entries))
ext = collections.Counter(os.path.splitext(n)[1].lower() for n, _, _ in entries)
print("extensions:", dict(ext))
dim_hist = collections.Counter()
fmt_hist = collections.Counter()
alpha_hist = collections.Counter()
sizes = []
problems = []
total_pixels = 0
def tga_info(b):
idlen = b[0]; cmap = b[1]; typ = b[2]
w = struct.unpack_from("<H", b, 12)[0]
h = struct.unpack_from("<H", b, 14)[0]
bpp = b[16]
return typ, w, h, bpp
def dds_info(b):
h = struct.unpack_from("<I", b, 12)[0]
w = struct.unpack_from("<I", b, 16)[0]
four = b[84:88]
return w, h, four
for nm, o, s in entries:
e = os.path.splitext(nm)[1].lower()
b = data[o:o+s]
try:
if e == ".tga":
typ, w, h, bpp = tga_info(b)
fmt_hist[f"TGA{typ}/{bpp}bpp"] += 1
dim_hist[f"{w}x{h}"] += 1
total_pixels += w*h
if bpp == 32:
alpha_hist["TGA32(has alpha channel)"] += 1
elif e == ".dds":
w, h, four = dds_info(b)
fmt_hist["DDS:"+four.decode("latin1").strip()] += 1
dim_hist[f"{w}x{h}"] += 1
total_pixels += w*h
else:
fmt_hist["other:"+e] += 1
except Exception as ex:
problems.append((nm, s, str(ex)))
print("\nformats:")
for k, v in fmt_hist.most_common():
print(" %-24s %d" % (k, v))
print("\ntop dimensions:")
for k, v in dim_hist.most_common(20):
print(" %-12s %d" % (k, v))
print("\nalpha:", dict(alpha_hist))
print("\ntotal pixels: %.1f Mpix (%.0f MB as RGBA8)" % (total_pixels/1e6, total_pixels*4/1e6))
print("problems:", problems[:5], "...total", len(problems))