tex_scan.py 5.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162
  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. 16: 'R32f', 17: 'RGBA16161616', 18: 'RGB161616', 19: 'RGBA16161616S', 20: 'RGB161616S', 21: 'RGBA8888s',
  22. }
  23. def cstring(data, pos):
  24. end = data.find(b'\0', pos)
  25. if end < 0:
  26. return None, len(data)
  27. return data[pos:end].decode('latin-1'), end + 1
  28. def u32(data, pos):
  29. if pos + 4 > len(data):
  30. return 0, pos
  31. return struct.unpack_from('<I', data, pos)[0], pos + 4
  32. def scan_tex(data, found, item):
  33. def note(key):
  34. if len(found[key]) < 5:
  35. found[key].add(item)
  36. counts[key] += 1
  37. magic, pos = cstring(data, 0)
  38. note(f'container {magic}')
  39. if magic not in ('TEXV0005', 'TEXV0004'):
  40. return
  41. # TEXV0004: version 0 TEXI fields and version 0 TEXB body, no tags
  42. if magic == 'TEXV0004':
  43. sections = [('TEXI', 0), ('TEXB', 0)]
  44. else:
  45. sections = None
  46. def read_texi(pos, version):
  47. fmt, pos = u32(data, pos)
  48. flags, pos = u32(data, pos)
  49. pos += 16
  50. if flags & 0x40:
  51. pos += 4
  52. if version >= 1:
  53. pos += 4
  54. note(f'format {fmt} {FORMATS.get(fmt, "?")}')
  55. for bit in range(32):
  56. if flags & (1 << bit):
  57. note(f'flag 0x{1 << bit:x}')
  58. return pos, flags
  59. flags = 0
  60. if sections:
  61. pos, flags = read_texi(pos, 0)
  62. note('TEXB version 0 (TEXV0004)')
  63. return
  64. while pos < len(data):
  65. tag, pos = cstring(data, pos)
  66. if not tag or len(tag) < 4:
  67. break
  68. version = int(tag[4:]) if tag[4:].isdigit() else -1
  69. if tag.startswith('TEXI'):
  70. note(f'section {tag}')
  71. pos, flags = read_texi(pos, version)
  72. elif tag.startswith('TEXB'):
  73. note(f'section {tag}')
  74. if version >= 1:
  75. images, pos = u32(data, pos)
  76. if images > 1:
  77. note('several images')
  78. if version >= 3:
  79. fif, pos = u32(data, pos)
  80. note(f'freeimage {fif:#x}' if fif != 0xFFFFFFFF else 'freeimage none')
  81. if version >= 4:
  82. conditions, pos = u32(data, pos)
  83. if conditions:
  84. note(f'TEXB0004 conditions={min(conditions, 2)}{"+" if conditions > 2 else ""}')
  85. # the mip data follows, TEXS comes after it but its offset isn't stored: look for the tag
  86. texs = data.find(b'TEXS', pos)
  87. if texs < 0:
  88. break
  89. pos = texs
  90. elif tag.startswith('TEXS'):
  91. note(f'section {tag}')
  92. break
  93. else:
  94. note(f'unknown section {tag}')
  95. break
  96. counts = defaultdict(int)
  97. def scan_pkg(path, found, item):
  98. with open(path, 'rb') as f:
  99. data = f.read()
  100. n, pos = u32(data, 0)
  101. header = data[4:4 + n].decode('latin-1', 'replace')
  102. if not header.startswith('PKGV'):
  103. return
  104. pos = 4 + n
  105. count, pos = u32(data, pos)
  106. entries = []
  107. for _ in range(count):
  108. n, pos = u32(data, pos)
  109. name = data[pos:pos + n].decode('utf-8', 'replace')
  110. pos += n
  111. offset, pos = u32(data, pos)
  112. length, pos = u32(data, pos)
  113. entries.append((name, offset, length))
  114. base = pos
  115. for name, offset, length in entries:
  116. if name.lower().endswith('.tex'):
  117. scan_tex(data[base + offset:base + offset + length], found, item)
  118. def main():
  119. dirs = sys.argv[1:] or DEFAULT_DIRS
  120. found = defaultdict(set)
  121. for root_dir in dirs:
  122. for root, _, files in os.walk(root_dir):
  123. rel = os.path.relpath(root, root_dir)
  124. item = rel.split(os.sep)[0] if rel != '.' else os.path.basename(root_dir)
  125. for name in files:
  126. path = os.path.join(root, name)
  127. if name.endswith('.pkg'):
  128. scan_pkg(path, found, item)
  129. elif name.lower().endswith('.tex'):
  130. with open(path, 'rb') as f:
  131. scan_tex(f.read(), found, item)
  132. for key in sorted(counts):
  133. print(f'{counts[key]:7d} {key:32s} {" ".join(sorted(found[key]))}')
  134. if __name__ == '__main__':
  135. main()