Files
isaac-port/scripts/ApplyLocals.java
T
shweetandClaude Opus 5.5 24098479e7 ApplyProtos: read the purge from the RETs when Ghidra has none, trust db/ prototypes past that; log what it skips
Purge unknown 139 -> 11, prototypes applied 3032 -> 3163 (round 24's 12 included), param_N lines 33145 -> 31116,
in_ECX 35059 -> 32432; Room::Update/Render get a typed this. ApplyLocals: a same-name row with a type retypes in place,
and an unparseable type fails only its row. notes/decomp_issues.md collects what the round 23-24 agents found wrong.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
2026-11-19 18:16:18 -05:00

54 lines
3.0 KiB
Java

// Applies db/locals.tsv (function address, decompiler local name, new name [, C type]) by decompiling each listed
// function and committing the renamed HighVariable to the database, so later decompiles keep the name. Arg: locals.tsv
import ghidra.app.script.GhidraScript;
import ghidra.app.decompiler.*;
import ghidra.program.model.data.*;
import ghidra.program.model.listing.*;
import ghidra.program.model.pcode.*;
import ghidra.program.model.symbol.SourceType;
import ghidra.util.data.DataTypeParser;
import java.nio.file.*;
import java.util.*;
public class ApplyLocals extends GhidraScript {
public void run() throws Exception {
Map<String, List<String[]>> byFn = new LinkedHashMap<>();
for (String line : Files.readAllLines(Paths.get(getScriptArgs()[0]))) {
if (line.isBlank() || line.startsWith("#")) continue;
String[] c = line.split("\t");
byFn.computeIfAbsent(String.format("%08x", Long.parseLong(c[0].replace("0x", ""), 16)), k -> new ArrayList<>()).add(c);
}
DecompInterface di = new DecompInterface();
DecompileOptions opts = new DecompileOptions();
opts.setMaxPayloadMBytes(1024);
di.setOptions(opts);
di.openProgram(currentProgram);
DataTypeParser parser = new DataTypeParser(currentProgram.getDataTypeManager(), null, null, DataTypeParser.AllowedDataTypes.ALL);
int n = 0;
for (var e : byFn.entrySet()) {
Function f = getFunctionAt(toAddr(e.getKey()));
DecompileResults r = f == null ? null : di.decompileFunction(f, 900, monitor);
if (r == null || !r.decompileCompleted()) { println("ApplyLocals: cannot decompile " + e.getKey() + (r == null ? "" : ": " + r.getErrorMessage())); continue; }
Map<String, HighSymbol> syms = new HashMap<>();
for (Iterator<HighSymbol> it = r.getHighFunction().getLocalSymbolMap().getSymbols(); it.hasNext(); ) {
HighSymbol s = it.next();
syms.put(s.getName(), s);
}
for (String[] c : e.getValue()) {
boolean isRetypeOnly = c[1].equals(c[2]) && c.length > 3 && !c[3].isBlank(); // same name, new type
if (syms.containsKey(c[2]) && !isRetypeOnly) continue; // already applied
HighSymbol s = syms.get(c[1]);
if (s == null) { println("ApplyLocals: " + e.getKey() + " has no local " + c[1]); continue; }
try { // an unparseable type fails only its own row
DataType t = c.length > 3 && !c[3].isBlank() ? parser.parse(c[3]) : null;
if (isRetypeOnly && t != null && t.isEquivalent(s.getDataType())) continue; // already applied
HighFunctionDBUtil.updateDBVariable(s, c[2], t, SourceType.USER_DEFINED); n++;
}
catch (Exception ex) { println("ApplyLocals: " + e.getKey() + " " + c[1] + " -> " + c[2] + ": " + ex.getMessage()); }
}
}
di.dispose();
println("ApplyLocals: " + n + " locals");
}
}