367 files, from working commit b1a079a. Co-Authored-By: Claude Fable 5.2 <noreply@anthropic.com>
82 lines
5.8 KiB
Python
82 lines
5.8 KiB
Python
#!/usr/bin/env python3
|
|
"""One saved catalogue-wide source protocol inventory and native reference matrix.
|
|
References are candidates, never proof that a listener or semantic mapping works.
|
|
"""
|
|
import argparse,ast,json,re,subprocess,time
|
|
from collections import defaultdict
|
|
from pathlib import Path
|
|
ROOT=Path(__file__).resolve().parents[1]
|
|
IDENT=re.compile(r'\b[A-Za-z_]\w*\b')
|
|
ENUM=re.compile(r'\benum\s+(?:class\s+)?(?P<name>\w+)?\s*(?::[^{}]+)?\{(?P<body>[^{}]*)\}',re.S)
|
|
def save(p,v):
|
|
p.parent.mkdir(parents=True,exist_ok=True);tmp=p.with_suffix(p.suffix+'.tmp');tmp.write_text(json.dumps(v,indent=2)+'\n');tmp.replace(p)
|
|
def strip_comments(s):return re.sub(r'//[^\n]*|/\*.*?\*/',lambda m:'\n'*m.group().count('\n'),s,flags=re.S)
|
|
def number(expr,known):
|
|
expr=re.sub(r'(?<=\d)[uUlL]+\b','',expr.strip())
|
|
def visit(n):
|
|
if isinstance(n,ast.Constant) and type(n.value) is int:return n.value
|
|
if isinstance(n,ast.Name) and n.id in known:return known[n.id]
|
|
if isinstance(n,ast.UnaryOp):
|
|
v=visit(n.operand)
|
|
if isinstance(n.op,ast.Invert):return ~v
|
|
if isinstance(n.op,ast.USub):return -v
|
|
if isinstance(n.op,ast.UAdd):return v
|
|
if isinstance(n,ast.BinOp):
|
|
a,b=visit(n.left),visit(n.right)
|
|
if isinstance(n.op,ast.BitOr):return a|b
|
|
if isinstance(n.op,ast.BitAnd):return a&b
|
|
if isinstance(n.op,ast.BitXor):return a^b
|
|
if isinstance(n.op,ast.LShift):return a<<b
|
|
if isinstance(n.op,ast.RShift):return a>>b
|
|
if isinstance(n.op,ast.Add):return a+b
|
|
if isinstance(n.op,ast.Sub):return a-b
|
|
if isinstance(n.op,ast.Mult):return a*b
|
|
raise ValueError('Unresolved source expression')
|
|
try:return visit(ast.parse(expr,mode='eval').body)
|
|
except (SyntaxError,ValueError,TypeError,KeyError):return None
|
|
|
|
def main():
|
|
ap=argparse.ArgumentParser();ap.add_argument('--server-root',type=Path,required=True);ap.add_argument('--output',type=Path,required=True);args=ap.parse_args();out=args.output.resolve();out.mkdir(parents=True,exist_ok=True)
|
|
print('REA field/flag/listener correlation: inventory and saved comparison running.',flush=True)
|
|
server=args.server_root.resolve();r=subprocess.run(['rg','--files',str(server/'src')],text=True,capture_output=True,check=True)
|
|
headers=[Path(p) for p in r.stdout.splitlines() if p.endswith(('.h','.hpp'))]
|
|
records=[];known={};source_files=[]
|
|
for p in headers:
|
|
raw=p.read_text(errors='replace');s=strip_comments(raw)
|
|
if 'enum' not in s:continue
|
|
file_records=[]
|
|
for m in ENUM.finditer(s):
|
|
last=-1;enum=m.group('name') or '<anonymous>'
|
|
for part in m.group('body').split(','):
|
|
part=re.sub(r'^\s*#.*$','',part,flags=re.M).strip();a=re.fullmatch(r'(\w+)\s*(?:=\s*(.*))?',part,re.S)
|
|
if not a:continue
|
|
name,expr=a.groups();value=number(expr,known) if expr else (last+1 if last is not None else None)
|
|
if value is not None:known[name]=value
|
|
last=value
|
|
kind='opcode' if name.startswith(('SMSG_','CMSG_','MSG_','UMSG_')) else ('update-field' if 'UpdateFields' in p.name else 'enum-or-flag')
|
|
rec={'source_file':str(p),'source_enum':enum,'symbol':name,'expression':expr,'value':value,'kind':kind,'conditional_context_not_resolved':True}
|
|
records.append(rec);file_records.append(rec)
|
|
if file_records:source_files.append(str(p))
|
|
symbols={r['symbol'] for r in records};refs=defaultdict(list);numeric_dispatch=defaultdict(list);native_fields=[]
|
|
paths=[p for base in ['native','src','include/gateway'] for p in (ROOT/base).rglob('*') if p.is_file() and p.suffix in ('.c','.cpp','.h','.hpp')]
|
|
for p in paths:
|
|
s=strip_comments(p.read_text(errors='replace'))
|
|
for ln,line in enumerate(s.splitlines(),1):
|
|
tokens=set(IDENT.findall(line))
|
|
for name in tokens&symbols:refs[name].append({'file':str(p.relative_to(ROOT)),'line':ln,'dispatch_candidate':bool(re.search(r'\bcase\b|\bif\b',line))})
|
|
if re.search(r'\bcase\s+0x[0-9a-fA-F]+\s*:|opcode\s*==\s*0x',line):
|
|
for h in re.findall(r'0x[0-9a-fA-F]+',line):numeric_dispatch[int(h,16)].append({'file':str(p.relative_to(ROOT)),'line':ln})
|
|
if any(t in line for t in ('AuraState','UnitData','PlayerData','ObjectData','UpdateMask')):native_fields.append({'file':str(p.relative_to(ROOT)),'line':ln,'symbols':sorted(tokens)})
|
|
totals=defaultdict(lambda:defaultdict(int))
|
|
for rec in records:
|
|
rec['native_symbol_references']=refs.get(rec['symbol'],[])
|
|
rec['legacy_numeric_dispatch_candidates']=numeric_dispatch.get(rec['value'],[]) if rec['kind']=='opcode' else []
|
|
rec['status']='candidate-reference-requires-contract' if rec['native_symbol_references'] or rec['legacy_numeric_dispatch_candidates'] else 'no-static-reference-found'
|
|
rec['semantic_mapping_verified']=False
|
|
totals[rec['kind']][rec['status']]+=1
|
|
save(out/'server-contract-catalogue.json',{'source_root':str(server),'source_files':source_files,'records':records,'value_parse_holds':sum(r['value'] is None for r in records),'limitations':['Local source is authoritative for the local server; Octo fork differences require captures/client evidence.','Compile/preprocessor contexts and alias expressions need pinned contracts.','A symbol/numeric reference is not proof of a dispatch handler, listener or native semantic equivalence.']})
|
|
save(out/'native-field-reference-index.json',{'references':native_fields})
|
|
save(out/'coverage-summary.json',{'phase':'static-correlation-inventory-complete','finished':time.time(),'source_header_count':len(headers),'source_enum_header_count':len(source_files),'source_record_count':len(records),'counts':{k:dict(v) for k,v in totals.items()},'actual_listener_mapping_complete':False,'next':'Join pinned REA field/opcode contracts and existing handler receipts; classify payload/flag/definition/asset gaps before applying shared fixes.','no_client_input':True,'no_authentication':True,'no_client_changes':True})
|
|
print('Saved complete source enum inventory and candidate native references; unresolved semantics remain explicit.',flush=True)
|
|
if __name__=='__main__':main()
|