// 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 import ghidra.app.script.GhidraScript; import ghidra.program.model.data.*; import ghidra.program.model.listing.*; import ghidra.program.model.symbol.*; import java.nio.file.*; import ghidra.program.model.listing.ParameterImpl; import java.util.*; public class ApplyTypes extends GhidraScript { Map> fields = new LinkedHashMap<>(); Map base = new HashMap<>(); DataTypeManager dtm; DataType vec; DataType prim(String t) { t = t.replaceAll("\\s+\\w+$", "").replace("const ", "").trim(); // drop field name if (t.endsWith("*")) return new PointerDataType(VoidDataType.dataType); switch (t) { case "int": case "int32_t": case "unsigned int": case "uint32_t": case "unsigned long": case "long": return IntegerDataType.dataType; case "float": return FloatDataType.dataType; case "bool": case "char": case "uint8_t": case "int8_t": case "unsigned char": return ByteDataType.dataType; case "short": case "unsigned short": case "uint16_t": case "int16_t": return ShortDataType.dataType; case "unsigned long long": case "uint64_t": case "long long": return QWordDataType.dataType; case "double": return DoubleDataType.dataType; case "Vector": return vec; } return null; } void addFields(StructureDataType s, String cls, int depth) { if (depth > 8) return; if (base.containsKey(cls)) addFields(s, base.get(cls), depth + 1); for (String[] f : fields.getOrDefault(cls, List.of())) { DataType dt = prim(f[2]); if (dt == null) continue; int off = Integer.decode(f[1]); String nm = f[2].replaceAll(".*\\s", "").replaceAll("[^\\w]", ""); try { if (off + dt.getLength() > s.getLength()) s.growStructure(off + dt.getLength() - s.getLength()); s.replaceAtOffset(off, dt, dt.getLength(), nm, null); } catch (Exception e) { /* overlapping/odd field: skip */ } } } public void run() throws Exception { dtm = currentProgram.getDataTypeManager(); StructureDataType v = new StructureDataType("Vector", 0); v.add(FloatDataType.dataType, "x", null); v.add(FloatDataType.dataType, "y", null); vec = dtm.addDataType(v, DataTypeConflictHandler.REPLACE_HANDLER); for (String l : Files.readAllLines(Paths.get(getScriptArgs()[0]))) { String[] c = l.split("\t"); if (c[1].equals("base")) base.put(c[0], c[2]); else fields.computeIfAbsent(c[0], k -> new ArrayList<>()).add(c); } SymbolTable st = currentProgram.getSymbolTable(); Namespace global = currentProgram.getGlobalNamespace(); Set classes = new TreeSet<>(fields.keySet()); classes.addAll(base.keySet()); int typed = 0; for (String cls : classes) { StructureDataType s = new StructureDataType(cls, 4); addFields(s, cls, 0); if (s.getLength() < 0x20) continue; // skip trivial structs DataType dt = dtm.addDataType(s, DataTypeConflictHandler.REPLACE_HANDLER); Namespace ns = st.getNamespace(cls, global); if (ns == null) ns = st.createClass(global, cls, SourceType.USER_DEFINED); else if (!(ns instanceof GhidraClass)) ns = st.convertNamespaceToClass(ns); Namespace alias = st.getNamespace(cls.replace("_", ""), global); if (alias != null && !alias.equals(ns)) for (Symbol sym : st.getSymbols(alias)) { if (sym.getSymbolType() != SymbolType.FUNCTION) continue; try { sym.setNamespace(ns); } catch (Exception e) { /* name clash: keep in alias ns */ } } for (Symbol sym : st.getSymbols(ns)) { Function f = getFunctionAt(sym.getAddress()); String cm = f == null ? null : f.getComment(); if (cm == null || !(cm.contains("__thiscall") || cm.contains("vtable slot"))) continue; f.setCallingConvention("__thiscall"); // Ghidra then types `this` as * typed++; } } // NpcAI routines are __fastcall(npc in ECX) DataType npcPtr = new PointerDataType(dtm.getDataType(new CategoryPath("/"), "Entity_NPC")); Namespace ai = st.getNamespace("NpcAI", global); int ain = 0; if (ai != null) for (Symbol sym : st.getSymbols(ai)) { Function f = getFunctionAt(sym.getAddress()); if (f == null) continue; f.setCallingConvention("__fastcall"); f.replaceParameters(Function.FunctionUpdateType.DYNAMIC_STORAGE_FORMAL_PARAMS, true, SourceType.USER_DEFINED, new ParameterImpl("npc", npcPtr, currentProgram)); ain++; } println("ApplyTypes: " + classes.size() + " structs, " + typed + " thiscall methods, " + ain + " NpcAI params"); } }