Files

97 lines
7.2 KiB
Python

"""Batch 8 (2026-11-25, Noctis error report): 18 Octo item models whose native retail FileDataIDs are named by
ModelFileData but absent from the Classic store (displays 758343..759170, 26 Turtle items, e.g. mace_2h_blood_a_01
FDID 146756). Octo MD20 v256 -> fixups (as fixups.py) -> m2_equipment_convert (conv7 options) -> MD21 container ON THE
NATIVE FDIDs (m2 and listfile skins), so ModelFileData/ItemDisplayInfo stay untouched. Hardcoded textures resolve as
build4 (Octo bytes' client FDID, else the native path)."""
import json, struct, sys, hashlib, shutil
from pathlib import Path
sys.path.insert(0, '/mnt/Games/@project/wt-master-merge/ops'); sys.path.insert(0, '/mnt/Games/@project/gateway/ops/maps14-20261122')
sys.path.insert(0, '/mnt/Games/@project/gateway/runtime/continuation-20261113/claude-lane/content/prune-56ec-20261125')
from casc_asset_package import model_container
import m2_equipment_convert as c, octo_mpq, frozen_store as fs
W = Path(__file__).resolve().parent; PREFIX = 'add8'
md5 = lambda b: hashlib.md5(b).hexdigest(); sha = lambda b: hashlib.sha256(b).hexdigest()
MODELS = {'Weapon': ['mace_2h_blood_a_01', 'offhand_draenei_a_01', 'offhand_draenei_a_02', 'stave_2h_feral_pvp_c_01', 'offhand_blood_a_01',
'offhand_blood_a_02', 'thrown_1h_fan_a_01', 'sword_1h_draenei_c_01', 'stave_2h_feral_d_01', 'stave_2h_draenei_a_01', 'bow_1h_blood_c_01',
'polearm_2h_blood_elf_d_01', 'sword_1h_alliancecovenant_d_01', 'firearm_2h_rifle_ulduar_d_01', 'hand_1h_zulaman_d_01right',
'firearm_2h_rifle_a_05', 'hand_1h_shaman_d_01right'], 'Shield': ['shield_2h_outlandraid_d_07']}
# current store: FDIDs and content key -> FDIDs
st = fs.Store(); root = st.root; fids = set(); ck = {}
for g in st.layout['groups']:
f = 0
for i, (d,) in enumerate(struct.iter_unpack('<I', memoryview(root)[g['ids_offset']:g['ckeys_offset']])):
f += d; fids.add(f); ck.setdefault(root[g['ckeys_offset'] + 16 * i:g['ckeys_offset'] + 16 * i + 16].hex(), set()).add(f); f += 1
lf = {}; lfi = {}
for l in open('/mnt/Games/@project/listfiles/community-listfile.csv', errors='replace'):
i, _, p = l.rstrip('\n').partition(';'); lf[p.lower()] = int(i); lfi[int(i)] = p.lower()
def exists(p):
try: return bool(octo_mpq.read(p))
except Exception: return False
def tex_fdid(path):
try: h = md5(octo_mpq.read(path))
except Exception: h = None
if h and h in ck: return min(ck[h])
f = lf.get(path.lower().replace('\\', '/')); assert f in fids, path; return f
# 1. source + fixups
src = W / 'models/src8'; shutil.rmtree(src, ignore_errors=True); src.mkdir(parents=True); rows = []
for d, names in MODELS.items():
for n in names:
member = f'item/objectcomponents/{d.lower()}/{n}.m2'; raw = bytearray(octo_mpq.read(f'Item\\ObjectComponents\\{d}\\{n}.m2'))
assert raw[:4] == b'MD20'; fix = []
k_n, k_o = struct.unpack_from('<II', raw, 92)
kinds = [struct.unpack_from('<I', raw, k_o + 16 * i)[0] for i in range(k_n)]
for i in range(k_n):
k, fl, ln, at = struct.unpack_from('<4I', raw, k_o + 16 * i)
name = bytes(raw[at:at + ln]).rstrip(b'\0').decode('latin1') if ln else ''
if k == 2 and ln and (':' in name or name.startswith(('\\', '/'))): struct.pack_into('<I', raw, k_o + 16 * i + 8, 0); fix.append((i, 'type2 name cleared'))
elif k == 1 and name and exists(name): struct.pack_into('<I', raw, k_o + 16 * i, 0); fix.append((i, 'type1 -> hardcoded'))
# Item model without any type-2 slot whose type-1 (skin) slot names no real file: the author meant the item texture
# (Octo fills it from ItemDisplayInfo; e.g. firearm_2h_rifle_ulduar_d_01 'GLOBEDIFA' drew white). Make it type 2.
elif k == 1 and 2 not in kinds:
struct.pack_into('<I', raw, k_o + 16 * i, 2); struct.pack_into('<I', raw, k_o + 16 * i + 8, 0); rn, ro = struct.unpack_from('<II', raw, 124)
lut = list(struct.unpack_from('<%dh' % rn, raw, ro)) if rn else []
if len(lut) > 2 and lut[1] == i and lut[2] == -1: struct.pack_into('<hh', raw, ro + 2, -1, i)
fix.append((i, 'type1 -> item type2'))
h = sha(raw); (src / f'{h}.m2').write_bytes(raw)
rows.append(dict(member=member, archive='octo', sha256=h, file=f'{h}.m2', magic='MD20', version=struct.unpack_from('<I', raw, 4)[0], fixups=fix))
json.dump(dict(matches=rows), open(src / 'manifest.json', 'w'), indent=1)
# 2. convert (conv7 options)
kw = dict(allow_qualified_cutout_shader16=True, paired_single_texture_rule=True, convert_ribbons=True, convert_particles=True,
downgrade_hermite=True, convert_uv_animation=True, convert_lights=True, resample_step_visibility=True, drop_unreachable_events=True,
two_texture_rule=True, legacy_replaceable_types=True)
for d in ('conv8', 'conv8a', 'conv8b'): shutil.rmtree(W / 'models' / d, ignore_errors=True)
a = c.batch(src / 'manifest.json', W / 'models/conv8a', canonicalize_rigid_views=True, **kw)
b = c.batch(src / 'manifest.json', W / 'models/conv8b', canonicalize_rigid_views=False, **kw)
conv = W / 'models/conv8'; conv.mkdir(); recs = []; bb = {r['source_sha256']: r for r in b['records']}
for r in a['records']:
s = 'conv8a'
if not r['structural_conversion_complete'] and bb[r['source_sha256']]['structural_conversion_complete']: r = bb[r['source_sha256']]; s = 'conv8b'
for o in r.get('outputs', []): shutil.copy(W / 'models' / s / o['file'], conv / o['file'])
recs.append(r)
# 3. containers on native FDIDs
out = W / 'models/out8'; shutil.rmtree(out, ignore_errors=True); out.mkdir(); models = {}; art = []; holds = []
FREE = 8811800; assert not any(FREE <= f < FREE + 200 for f in fids)
for r in recs:
member = r['source_candidates'][0]['member']; base = member[:-3]
if not r['structural_conversion_complete']: holds.append(dict(member=member, reason=r.get('blockers') or r.get('reason') or 'conversion incomplete')); continue
body = (conv / (r['source_sha256'] + '.md20')).read_bytes()
skinl = [(conv / o['file']).read_bytes() for o in r['outputs'] if o['role'] == 'native-skin']
m2f = lf[base + '.m2']; assert m2f not in fids, member
skfs = []
for v in range(len(skinl)):
f = lf.get(f'{base}{v:02}.skin')
if f is None or f in fids: f = FREE; FREE += 1
skfs.append(f)
n, o = struct.unpack_from('<II', body, 80); txid = []
for i in range(n):
t, _, ln, no = struct.unpack_from('<IIII', body, o + 16 * i)
txid.append(tex_fdid(body[no:no + ln].rstrip(b'\0').decode('latin1')) if t == 0 else 0)
m2 = model_container(body, skfs, txid); (out / f'{m2f}.m2').write_bytes(m2)
art.append(dict(file_data_id=m2f, path=base + '.m2', ckey=md5(m2), kind='M2', file=str(out / f'{m2f}.m2')))
for v, (skf, skin) in enumerate(zip(skfs, skinl)):
(out / f'{skf}.skin').write_bytes(skin)
art.append(dict(file_data_id=skf, path=base + f'{v:02}.skin', ckey=md5(skin), kind='SKIN', file=str(out / f'{skf}.skin')))
models[base.split('/')[-1]] = dict(m2=m2f, skins=skfs, txid=txid, member=member, box=list(struct.unpack_from('<6f', body, 0xA0)))
json.dump(dict(models=models, art=art, holds=holds), open(W / f'{PREFIX}-models.json', 'w'), indent=1)
print('models', len(models), 'held', len(holds), 'art files', len(art)); [print('HOLD', h) for h in holds]