Files
2026-11-23 03:28:32 +02:00

83 lines
4.7 KiB
Python

"""Convert Source (Octo) WMO families for the Turtle maps; texture/doodad bindings from client + recovered bindings."""
import json, struct, sys, hashlib, pickle, collections
sys.path.insert(0, '.'); sys.path.insert(0, '/mnt/Games/@project/gateway/ops')
import octo_mpq as O, source_map_wdt as smw, map_asset_preflight as terrain
from recover_bindings import chunks
IDS = pickle.load(open('/tmp/claude-1000/-home-praxis-Projects/8c775e16-46f6-4cfc-9a63-844a8b79c754/scratchpad/cr/root-ids.pkl', 'rb'))
RB = json.load(open('recovered-bindings.json'))
RM = {p: int(max(c, key=c.get)) for p, c in RB['models'].items()}
LF = {}
for l in open('/mnt/Games/@project/listfiles/community-listfile.csv', encoding='utf-8', errors='replace'):
a, _, p = l.rstrip('\n').partition(';')
if p.endswith(('.m2', '.blp')): LF.setdefault(p.lower(), int(a))
norm = lambda p: p.replace('\\', '/').lower()
def model_fdi(p):
p = norm(p); q = p.rsplit('.', 1)[0] + '.m2'
if q in RM: return RM[q]
f = LF.get(q); return f if f in IDS else None
def wmo_names(raw, tag):
c = dict(chunks(raw)); b = c.get(tag, b''); out = []; i = 0
while i < len(b):
j = b.index(b'\0', i)
if j > i: out.append(b[i:j].decode('latin1'))
i = j + 1
return out
def source_family(path):
raw = O.read(path.replace('/', '\\'))
if raw is None: return None, None
c = dict(chunks(raw)); n = struct.unpack_from('<I', c['MOHD'], 4)[0]; base = path[:-4]
groups = {}
for i in range(n):
g = O.read(f'{base}_{i:03d}.wmo'.replace('/', '\\'))
if g is None: return raw, None
groups[i] = g
return raw, groups
def tuples(raw):
c = dict(chunks(raw)); m = c['MOMT']
return {struct.unpack_from('<3I', m, k)[:1] + (struct.unpack_from('<I', m, k + 4)[0], struct.unpack_from('<I', m, k + 8)[0]) for k in range(0, len(m), 64)}
def null_absent_secondary(raw, exists):
"""Secondary (env) texture absent from the Octo client and the native client -> the WMO's own empty string (NULL handle, as Octo draws it)."""
c = list(chunks(raw)); d = dict(c); tx = d['MOTX']; m = bytearray(d['MOMT'])
name = lambda o: tx[o:tx.index(b'\0', o)].decode('latin1')
empty = next((struct.unpack_from('<I', m, k + 24)[0] for k in range(0, len(m), 64) if not name(struct.unpack_from('<I', m, k + 24)[0])), None)
nulled = []
for k in range(0, len(m), 64):
o = struct.unpack_from('<I', m, k + 24)[0]; n = name(o)
if n and not exists(n) and empty is not None: struct.pack_into('<I', m, k + 24, empty); nulled.append(n)
if not nulled: return raw, []
out = b''.join(t[::-1].encode() + struct.pack('<I', len(m if t == 'MOMT' else b)) + (bytes(m) if t == 'MOMT' else b) for t, b in c)
return out, nulled
def SKY(raw):
n = dict(chunks(raw)).get('MOSB', b'').rstrip(b'\0').decode('latin1')
return {'sky_binding': model_fdi(n)} if n else {}
def LEG(raw):
c = dict(chunks(raw)); m = c['MOMT']
return terrain._WMO_LEGACY_MATERIAL_PROFILE if any(struct.unpack_from('<I', m, k + 36)[0] for k in range(0, len(m), 64)) else None
if __name__ == '__main__':
inv = json.load(open('inventory.json'))
owners = collections.defaultdict(set)
for mid, v in inv.items():
for p in v['missing']['wmo']: owners[p].add(int(mid))
res = {}
for p, maps in sorted(owners.items()):
raw, groups = source_family(p)
if raw is None: res[p] = 'NO SOURCE ROOT'; print(p, res[p]); continue
if groups is None: res[p] = 'MISSING GROUP'; print(p, res[p]); continue
tex = {}; newtex = []
for t in wmo_names(raw, 'MOTX'):
f = LF.get(norm(t))
if f in IDS: tex[norm(t)] = f
else: tex[norm(t)] = 900000000 + len(newtex); newtex.append(norm(t))
dood = {norm(d): model_fdi(d) for d in wmo_names(raw, 'MODN')}
dood = {k: v for k, v in dood.items() if v}
gb = [800000000 + i for i in range(len(groups))]
try:
root, conv, audit = smw.numeric_wmo_family(raw, p, groups, map_ids=sorted(maps), texture_bindings=tex, group_bindings=gb,
doodad_bindings=dood, legacy_material_profile=LEG(raw), **SKY(raw), native_observed_tuples=tuples(raw) & (smw.NATIVE_OBSERVED_MATERIAL_TUPLES | smw.NATIVE_FLAG_UNION_MATERIAL_TUPLES))
res[p] = dict(ok=True, groups=len(groups), new_textures=len(newtex), absent_doodads=len(audit['absent_doodads']))
except Exception as e:
res[p] = dict(ok=False, error=f'{type(e).__name__}: {e}'[:200], groups=len(groups), new_textures=len(newtex))
print(p, res[p], flush=True)
json.dump(res, open('wmo-dryrun.json', 'w'), indent=1)
print('ok', sum(1 for v in res.values() if isinstance(v, dict) and v['ok']), 'of', len(res))