diff --git a/scripts/ApplyTypes.java b/scripts/ApplyTypes.java index 017022b..a0d68bc 100644 --- a/scripts/ApplyTypes.java +++ b/scripts/ApplyTypes.java @@ -118,18 +118,21 @@ public class ApplyTypes extends GhidraScript { 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); + // per-type AI routines take the entity in ECX: NpcAI (dispatcher) and FamiliarAI (Entity_Familiar::Update cases, + // their ECX reads all land in Entity_Familiar fields); FamiliarAI ones with stack arguments are left alone int ain = 0; - if (ai != null) for (Symbol sym : st.getSymbols(ai)) { + for (String[] ai : new String[][] {{"NpcAI", "Entity_NPC", "npc"}, {"FamiliarAI", "Entity_Familiar", "familiar"}}) { + DataType npcPtr = new PointerDataType(dtm.getDataType(new CategoryPath("/"), ai[1])); + Namespace aiNs = st.getNamespace(ai[0], global); + if (aiNs != null) for (Symbol sym : st.getSymbols(aiNs)) { Function f = getFunctionAt(sym.getAddress()); - if (f == null) continue; + if (f == null || (ai[0].equals("FamiliarAI") && f.getStackPurgeSize() != 0)) continue; f.setCallingConvention("__fastcall"); f.replaceParameters(Function.FunctionUpdateType.DYNAMIC_STORAGE_FORMAL_PARAMS, true, SourceType.USER_DEFINED, - new ParameterImpl("npc", npcPtr, currentProgram)); + new ParameterImpl(ai[2], npcPtr, currentProgram)); ain++; } - println("ApplyTypes: " + classes.size() + " structs, " + typed + " thiscall methods, " + fitted + " fitted by ECX use, " + ain + " NpcAI params"); + } + println("ApplyTypes: " + classes.size() + " structs, " + typed + " thiscall methods, " + fitted + " fitted by ECX use, " + ain + " NpcAI/FamiliarAI params"); } }