Typed this for ~780 more methods whose ECX reads fit their class's confirmed fields; Sprite/Color = ANM2/ColorMod
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
1 parent
76ef4d7834
commit
cbb2f465ec
5 files changed
+62
-6
No files matched your search
+20
-2
@@ -1,6 +1,6 @@
|
||||
// Builds structs from zhl_fields.tsv (class, offset|base, type name), turns those namespaces into classes so
|
||||
// `this` gets typed, merges Lua-named namespaces (EntityPlayer -> Entity_Player), types NpcAI::* param 1 as Entity_NPC*.
|
||||
// Arg: zhl_fields.tsv
|
||||
// Args: merged fields.tsv [this_types.tsv: methods whose ECX use fits their class]
|
||||
import ghidra.app.script.GhidraScript;
|
||||
import ghidra.program.model.data.*;
|
||||
import ghidra.program.model.listing.*;
|
||||
@@ -100,6 +100,24 @@ public class ApplyTypes extends GhidraScript {
|
||||
typed++;
|
||||
}
|
||||
}
|
||||
// Lua aliases: the Lua Sprite and Color classes are ANM2 and ColorMod, so their methods' `this` gets that layout
|
||||
for (String[] al : new String[][] {{"Sprite", "ANM2"}, {"Color", "ColorMod"}}) {
|
||||
DataType t = dtm.getDataType(new CategoryPath("/"), al[1]);
|
||||
if (!(t instanceof Structure)) continue;
|
||||
Structure copy = (Structure) t.copy(dtm);
|
||||
copy.setName(al[0]);
|
||||
dtm.addDataType(copy, DataTypeConflictHandler.REPLACE_HANDLER);
|
||||
}
|
||||
// methods whose ECX offsets fit their class (scripts/this_types.py) become __thiscall, typing `this`
|
||||
int fitted = 0;
|
||||
if (getScriptArgs().length > 1) for (String l : Files.readAllLines(Paths.get(getScriptArgs()[1]))) {
|
||||
Function f = getFunctionAt(toAddr(l.split("\t")[0]));
|
||||
if (f == null || "__thiscall".equals(f.getCallingConventionName())) continue;
|
||||
Namespace ns = f.getParentNamespace();
|
||||
if (!(ns instanceof GhidraClass)) try { st.convertNamespaceToClass(ns); } catch (Exception e) { continue; }
|
||||
f.setCallingConvention("__thiscall");
|
||||
fitted++;
|
||||
}
|
||||
// NpcAI routines are __fastcall(npc in ECX)
|
||||
DataType npcPtr = new PointerDataType(dtm.getDataType(new CategoryPath("/"), "Entity_NPC"));
|
||||
Namespace ai = st.getNamespace("NpcAI", global);
|
||||
@@ -112,6 +130,6 @@ public class ApplyTypes extends GhidraScript {
|
||||
new ParameterImpl("npc", npcPtr, currentProgram));
|
||||
ain++;
|
||||
}
|
||||
println("ApplyTypes: " + classes.size() + " structs, " + typed + " thiscall methods, " + ain + " NpcAI params");
|
||||
println("ApplyTypes: " + classes.size() + " structs, " + typed + " thiscall methods, " + fitted + " fitted by ECX use, " + ain + " NpcAI params");
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,34 @@
|
||||
# Which named methods (Class::method, from Lua, db or ZHL without a declaration) really get a Class object in ECX:
|
||||
# at least 60% of the distinct offsets they read from in_ECX must fall inside Class's confirmed fields (5+ known).
|
||||
# ApplyTypes.java then makes them __thiscall, so `this` is typed. Lua aliases: Sprite = ANM2, Color = ColorMod.
|
||||
# Usage: this_types.py all.c merged_fields.tsv out.tsv (out: address \t class \t hits/offsets \t name)
|
||||
import collections, re, sys
|
||||
src, fields_tsv, out = sys.argv[1:4]
|
||||
ALIAS = {'Sprite': 'ANM2', 'Color': 'ColorMod'}
|
||||
SIZE = {'bool': 1, 'char': 1, 'int8_t': 1, 'uint8_t': 1, 'short': 2, 'int16_t': 2, 'uint16_t': 2, 'double': 8, 'uint64_t': 8,
|
||||
'int64_t': 8, 'Vector': 8, 'ColorMod': 0x2c, 'ANM2': 0x114, 'KColor': 0x10, 'BitSet128': 0x10, 'RNG': 0x10,
|
||||
'std_string': 0x18, 'AnimationState': 0x20}
|
||||
spans, base = collections.defaultdict(list), {}
|
||||
for l in open(fields_tsv):
|
||||
c = l.rstrip('\n').split('\t')
|
||||
if c[1] == 'base': base[c[0]] = c[2]; continue
|
||||
d = c[2].strip(); n = re.search(r'\[(\w+)\]\s*$', d)
|
||||
if n: d = d[:n.start()]
|
||||
t = d.rsplit(None, 1)[0].replace('const ', '') if ' ' in d else d
|
||||
spans[c[0]].append((int(c[1], 16), (4 if t.endswith('*') else SIZE.get(t, 4)) * (int(n.group(1), 0) if n else 1)))
|
||||
def known(cls):
|
||||
r = []
|
||||
while cls: r += spans.get(cls, []); cls = base.get(cls)
|
||||
return r
|
||||
rows, addr, name, offs = [], None, None, set()
|
||||
def flush():
|
||||
if not (name and '::' in name and offs) or re.match(r'(FUN_|NpcAI|NpcHelper|std::|Catch|Unwind)', name): return
|
||||
ns = name.rsplit('::', 1)[0]; cls = ALIAS.get(ns, ns); k = known(cls)
|
||||
hit = sum(1 for o in offs if any(a <= o < a + s for a, s in k))
|
||||
if len(k) >= 5 and hit / len(offs) >= 0.6: rows.append(f'{addr}\t{cls}\t{hit}/{len(offs)}\t{name}\n')
|
||||
for line in open(src, errors='replace'):
|
||||
if line.startswith('//@@ '): flush(); _, addr, name = line.split()[:3]; offs = set(); continue
|
||||
offs.update(int(m.group(1), 16) for m in re.finditer(r'in_ECX \+ (0x[0-9a-f]+)', line))
|
||||
flush()
|
||||
open(out, 'w').writelines(rows)
|
||||
print(f'this_types: {len(rows)} methods')
|
||||
Reference in new issue
Block a user