Ghidra 12.2 project and full dump of isaac-ng.exe v1.9.7.17, query scripts, notes indexed by entity, anm2/rooms reimplementations, and db/ (names + fields) that build.sh re-applies. Fixes awk numeric address comparison in find.sh/decompile.sh. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
73 lines
4.0 KiB
Java
73 lines
4.0 KiB
Java
// Resolves the Entity_NPC AI dispatch (byte index table + jump table, keyed by entity type) and names the AI routines.
|
|
// Args: dispatcherAddr Global.zhl out.tsv
|
|
// Writes: type \t ENTITY_NAME \t caseBlock \t calls made in that case block (addr:name ...)
|
|
// Renames each still-unnamed first callee to NpcAI::<ENTITY_NAME> (run without -readOnly to persist).
|
|
import ghidra.app.script.GhidraScript;
|
|
import ghidra.program.model.address.*;
|
|
import ghidra.program.model.listing.*;
|
|
import ghidra.program.model.scalar.Scalar;
|
|
import ghidra.program.model.symbol.*;
|
|
import java.io.*;
|
|
import java.nio.file.*;
|
|
import java.util.*;
|
|
import java.util.regex.*;
|
|
|
|
public class NpcTable extends GhidraScript {
|
|
public void run() throws Exception {
|
|
String[] a = getScriptArgs();
|
|
Map<Integer, String> names = new HashMap<>();
|
|
boolean inEnum = false;
|
|
Pattern ep = Pattern.compile("^\\s*(ENTITY_\\w+)\\s*=\\s*(\\d+)");
|
|
for (String l : Files.readAllLines(Paths.get(a[1]))) {
|
|
if (l.contains("enum eEntityType")) inEnum = true;
|
|
else if (inEnum && l.contains("}")) break;
|
|
Matcher m = ep.matcher(l);
|
|
if (inEnum && m.find()) names.putIfAbsent(Integer.parseInt(m.group(2)), m.group(1));
|
|
}
|
|
|
|
// find "CMP EAX,limit / JA / MOVZX EAX,byte ptr [EAX+idx] / JMP [EAX*4+jt]" preceded by "SUB/ADD EAX,-base" or LEA
|
|
Function disp = getFunctionAt(toAddr(a[0]));
|
|
if (disp.getSymbol().getSource() == SourceType.DEFAULT) disp.setName("Entity_NPC_UpdateAI_dispatch", SourceType.USER_DEFINED);
|
|
Instruction in = getInstructionAt(toAddr(a[0]));
|
|
long idx = 0, jt = 0, limit = 0;
|
|
for (int i = 0; i < 400 && in != null; i++, in = in.getNext()) {
|
|
String s = in.toString();
|
|
if (s.startsWith("CMP EAX,")) limit = ((Scalar) in.getOpObjects(1)[0]).getValue();
|
|
if (s.startsWith("MOVZX EAX,byte ptr [EAX +")) idx = Long.decode(s.replaceAll(".*\\+ (0x[0-9a-f]+)\\].*", "$1"));
|
|
if (s.startsWith("JMP dword ptr [EAX*0x4 +") && idx != 0) { jt = Long.decode(s.replaceAll(".*\\+ (0x[0-9a-f]+)\\].*", "$1")); break; }
|
|
}
|
|
long base = 10; // switch is on (type - 10); see "param_1[10] - 10U" in the decompile
|
|
println(String.format("idx=%x jt=%x limit=%d", idx, jt, limit));
|
|
SymbolTable st = currentProgram.getSymbolTable();
|
|
Namespace ns = st.getNamespace("NpcAI", null);
|
|
if (ns == null) ns = st.createNameSpace(null, "NpcAI", SourceType.USER_DEFINED);
|
|
Address end = in.getNext().getAddress();
|
|
try (PrintWriter o = new PrintWriter(a[2])) {
|
|
for (long t = 0; t <= limit; t++) {
|
|
int bi = getByte(toAddr(idx + t)) & 0xff;
|
|
Address blk = toAddr(getInt(toAddr(jt + 4L * bi)) & 0xffffffffL);
|
|
StringBuilder calls = new StringBuilder();
|
|
Function first = null;
|
|
Instruction x = getInstructionAt(blk);
|
|
for (int i = 0; i < 60 && x != null; i++, x = x.getNext()) {
|
|
String s = x.toString();
|
|
if (s.startsWith("CALL")) {
|
|
Address[] fl = x.getFlows();
|
|
Function f = fl.length > 0 ? getFunctionAt(fl[0]) : null;
|
|
if (f != null) { calls.append(f.getEntryPoint()).append(':').append(f.getName(true)).append(' '); if (first == null) first = f; }
|
|
}
|
|
if (s.startsWith("JMP") && !s.contains("[") || s.startsWith("RET")) break;
|
|
}
|
|
int type = (int) (t + base);
|
|
String nm = names.getOrDefault(type, "TYPE_" + type);
|
|
if (first != null && first.getSymbol().getSource() == SourceType.DEFAULT) {
|
|
first.setParentNamespace(ns);
|
|
first.setName(nm, SourceType.USER_DEFINED);
|
|
first.setComment("NPC AI for entity type " + type + " (from dispatcher " + a[0] + ")");
|
|
}
|
|
o.println(type + "\t" + nm + "\t" + blk + "\t" + calls.toString().trim());
|
|
}
|
|
}
|
|
}
|
|
}
|