tex_scan.py 5.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161
  1. #!/usr/bin/env python3
  2. """Counts what the installed .tex files use, read like wallpaper64.exe 2.8.42's loader (sub_14015E580).
  3. Usage:
  4. tex_scan.py [workshop dir] [more dirs...]
  5. Defaults to the Steam workshop folder of Wallpaper Engine plus WE's own assets. Looks at every .tex inside
  6. scene.pkg/general.pkg files and loose .tex files. Prints the container/section versions (TEXV, TEXI, TEXB, TEXS),
  7. texture formats, flag bits and FreeImage types, each with up to five items that have it. Use it before porting
  8. a texture feature to see whether the library has it at all.
  9. """
  10. import os
  11. import struct
  12. import sys
  13. from collections import defaultdict
  14. DEFAULT_DIRS = [
  15. '/workspace/SteamLibrary/steamapps/workshop/content/431960',
  16. '/workspace/SteamLibrary/steamapps/common/wallpaper_engine/assets',
  17. ]
  18. FORMATS = {
  19. 0: 'ARGB8888', 1: 'RGB888', 2: 'RGB565', 4: 'DXT5', 6: 'DXT3', 7: 'DXT1', 8: 'RG88', 9: 'R8',
  20. 10: 'RG1616f', 11: 'R16f', 12: 'BC7', 13: 'RGBa1010102', 14: 'RGBA16161616f', 15: 'RGB161616f',
  21. }
  22. def cstring(data, pos):
  23. end = data.find(b'\0', pos)
  24. if end < 0:
  25. return None, len(data)
  26. return data[pos:end].decode('latin-1'), end + 1
  27. def u32(data, pos):
  28. if pos + 4 > len(data):
  29. return 0, pos
  30. return struct.unpack_from('<I', data, pos)[0], pos + 4
  31. def scan_tex(data, found, item):
  32. def note(key):
  33. if len(found[key]) < 5:
  34. found[key].add(item)
  35. counts[key] += 1
  36. magic, pos = cstring(data, 0)
  37. note(f'container {magic}')
  38. if magic not in ('TEXV0005', 'TEXV0004'):
  39. return
  40. # TEXV0004: version 0 TEXI fields and version 0 TEXB body, no tags
  41. if magic == 'TEXV0004':
  42. sections = [('TEXI', 0), ('TEXB', 0)]
  43. else:
  44. sections = None
  45. def read_texi(pos, version):
  46. fmt, pos = u32(data, pos)
  47. flags, pos = u32(data, pos)
  48. pos += 16
  49. if flags & 0x40:
  50. pos += 4
  51. if version >= 1:
  52. pos += 4
  53. note(f'format {fmt} {FORMATS.get(fmt, "?")}')
  54. for bit in range(32):
  55. if flags & (1 << bit):
  56. note(f'flag 0x{1 << bit:x}')
  57. return pos, flags
  58. flags = 0
  59. if sections:
  60. pos, flags = read_texi(pos, 0)
  61. note('TEXB version 0 (TEXV0004)')
  62. return
  63. while pos < len(data):
  64. tag, pos = cstring(data, pos)
  65. if not tag or len(tag) < 4:
  66. break
  67. version = int(tag[4:]) if tag[4:].isdigit() else -1
  68. if tag.startswith('TEXI'):
  69. note(f'section {tag}')
  70. pos, flags = read_texi(pos, version)
  71. elif tag.startswith('TEXB'):
  72. note(f'section {tag}')
  73. if version >= 1:
  74. images, pos = u32(data, pos)
  75. if images > 1:
  76. note('several images')
  77. if version >= 3:
  78. fif, pos = u32(data, pos)
  79. note(f'freeimage {fif:#x}' if fif != 0xFFFFFFFF else 'freeimage none')
  80. if version >= 4:
  81. conditions, pos = u32(data, pos)
  82. if conditions:
  83. note(f'TEXB0004 conditions={min(conditions, 2)}{"+" if conditions > 2 else ""}')
  84. # the mip data follows, TEXS comes after it but its offset isn't stored: look for the tag
  85. texs = data.find(b'TEXS', pos)
  86. if texs < 0:
  87. break
  88. pos = texs
  89. elif tag.startswith('TEXS'):
  90. note(f'section {tag}')
  91. break
  92. else:
  93. note(f'unknown section {tag}')
  94. break
  95. counts = defaultdict(int)
  96. def scan_pkg(path, found, item):
  97. with open(path, 'rb') as f:
  98. data = f.read()
  99. n, pos = u32(data, 0)
  100. header = data[4:4 + n].decode('latin-1', 'replace')
  101. if not header.startswith('PKGV'):
  102. return
  103. pos = 4 + n
  104. count, pos = u32(data, pos)
  105. entries = []
  106. for _ in range(count):
  107. n, pos = u32(data, pos)
  108. name = data[pos:pos + n].decode('utf-8', 'replace')
  109. pos += n
  110. offset, pos = u32(data, pos)
  111. length, pos = u32(data, pos)
  112. entries.append((name, offset, length))
  113. base = pos
  114. for name, offset, length in entries:
  115. if name.lower().endswith('.tex'):
  116. scan_tex(data[base + offset:base + offset + length], found, item)
  117. def main():
  118. dirs = sys.argv[1:] or DEFAULT_DIRS
  119. found = defaultdict(set)
  120. for root_dir in dirs:
  121. for root, _, files in os.walk(root_dir):
  122. rel = os.path.relpath(root, root_dir)
  123. item = rel.split(os.sep)[0] if rel != '.' else os.path.basename(root_dir)
  124. for name in files:
  125. path = os.path.join(root, name)
  126. if name.endswith('.pkg'):
  127. scan_pkg(path, found, item)
  128. elif name.lower().endswith('.tex'):
  129. with open(path, 'rb') as f:
  130. scan_tex(f.read(), found, item)
  131. for key in sorted(counts):
  132. print(f'{counts[key]:7d} {key:32s} {" ".join(sorted(found[key]))}')
  133. if __name__ == '__main__':
  134. main()