68 lines
4.5 KiB
Python
68 lines
4.5 KiB
Python
"""Survey two-texture batches (ops=2): source row + both stages' blend/env -> Blizzard native (flags, prio, shader).
|
|
Derived from m2_pair_rule_check.py. Check a candidate batch rule (source 256 batch row -> native 272 flags/priority/shader) against every Blizzard model pair with identical triangles.
|
|
Usage: python3 -I -B m2_pair_rule_check.py <work dir> (after m2_pair_material_survey.py list + native extraction)"""
|
|
import sys, os, json, struct, collections, ctypes as C
|
|
sys.path.insert(0, '/mnt/Games/@project/wt-master-merge/ops'); sys.path.insert(0, '/mnt/Games/@project/gateway/ops/maps14-20261122')
|
|
import octo_mpq
|
|
import m2_format_compare as fc
|
|
|
|
|
|
def s16(v): return v - 65536 if v > 32767 else v
|
|
|
|
|
|
def predict(row, blend, env):
|
|
"""row = source batch (flags, prio byte, word2, ...). Returns (flags, prio, shader) or None when the rule does not cover it."""
|
|
flags, prio, word, layer, ops = row[0], row[1], row[2], row[7], row[8]
|
|
if ops != 1: return None
|
|
shader = (0x80 if env else 0) | (0x10 if blend else 0)
|
|
if word == 16: p = prio # already carries a shader word
|
|
elif prio: p = prio
|
|
else: p = s16(word)
|
|
return flags, p, shader
|
|
|
|
|
|
def main(work):
|
|
pairs = json.load(open(os.path.join(work, 'pairs.json'))); nat = os.path.join(work, 'native'); handles = {}
|
|
def octo(arch, member): return octo_mpq.read(member)
|
|
two = collections.Counter(); ex2 = {}; st = collections.Counter(); miss = collections.Counter(); ex = {}; uncovered = collections.Counter(); models_ok = models_bad = 0
|
|
for p in pairs:
|
|
nm = os.path.join(nat, p['path']); ns = os.path.join(nat, p['path'][:-3] + '00.skin')
|
|
if not (os.path.exists(nm) and os.path.exists(ns)): continue
|
|
src = octo(p['archive'], p['member'])
|
|
if not src or src[:4] != b'MD20' or struct.unpack_from('<I', src, 4)[0] != 256: continue
|
|
try: s = fc.model(src); t = fc.model(open(nm, 'rb').read()); ss = fc.skin(s); ts = fc.skin(t, open(ns, 'rb').read())
|
|
except Exception: continue
|
|
if ss['faces'] != ts['faces']: continue
|
|
sb, tb = ss['summary']['batch_rows'], ts['summary']['batch_rows']
|
|
if len(sb) != len(tb) or any(a[3] != b[3] for a, b in zip(sb, tb)): st['rows not aligned'] += 1; continue
|
|
st['aligned models'] += 1
|
|
mats = list(struct.iter_unpack('<HH', fc._extent(src, struct.unpack_from('<I', src, 136)[0], struct.unpack_from('<I', src, 132)[0], 4, 324)))
|
|
cn, co = struct.unpack_from('<II', src, 156); coord = [s16(x[0]) for x in struct.iter_unpack('<H', src[co:co + 2 * cn])]
|
|
tbody = t['body']; mc, mo = struct.unpack_from('<II', tbody, 112); tmats = list(struct.iter_unpack('<HH', tbody[mo:mo + 4 * mc]))
|
|
bad = False
|
|
for a, b in zip(sb, tb):
|
|
if a[8] == 2:
|
|
envs = tuple(a[10] + k < len(coord) and coord[a[10] + k] == -1 for k in (0, 1))
|
|
blends = tuple(mats[a[6] + k][1] if a[6] + k < len(mats) else None for k in (0, 1))
|
|
key = 'flags=%d prio=%d word=%d blend0..1=%s env0..1=%s -> native flags=%d prio=%d shader=0x%x' % (a[0], a[1], a[2], blends, envs, b[0], b[1], b[2])
|
|
two[key] += 1; ex2.setdefault(key, p['path'])
|
|
blend = mats[a[6]][1] if a[6] < len(mats) else None; env = a[10] < len(coord) and coord[a[10]] == -1
|
|
pr = predict(a, blend, env)
|
|
if pr is None:
|
|
uncovered['ops=%d' % a[8]] += 1; st['rows outside the rule'] += 1; continue
|
|
got = (b[0], b[1], b[2])
|
|
if pr == got and (a[6] >= len(mats) or b[6] >= len(tmats) or mats[a[6]] == tmats[b[6]]): st['rows predicted exactly'] += 1
|
|
else:
|
|
bad = True; st['rows mispredicted'] += 1
|
|
k = 'src %s blend=%s env=%s -> predicted %s, Blizzard %s mat %s->%s' % (tuple(a[:3]) + (a[7], a[8]), blend, env, pr, got, mats[a[6]] if a[6] < len(mats) else None, tmats[b[6]] if b[6] < len(tmats) else None)
|
|
miss[k] += 1; ex.setdefault(k, p['path'])
|
|
if bad: models_bad += 1
|
|
else: models_ok += 1
|
|
print(dict(st), '| models fully predicted', models_ok, 'with a miss', models_bad, '| uncovered', dict(uncovered))
|
|
print('TWO-TEXTURE rows', sum(two.values()))
|
|
for k, v in two.most_common(40): print(' %3d %s e.g. %s' % (v, k, ex2[k]))
|
|
json.dump(dict(two=dict(two), two_examples=ex2, stats=dict(st), models_ok=models_ok, models_bad=models_bad, misses=dict(miss), examples=ex), open(os.path.join(work, 'two-texture-survey.json'), 'w'), indent=1)
|
|
|
|
|
|
if __name__ == '__main__': main(sys.argv[1])
|