// Comprehensive decompile of the vehicleSubSystems Make dispatcher, the whole
// SubsystemCluster family, and the sub-gauge ctors they build.  Also dumps the
// per-group geometry table and the relevant vtables.  Writes to scratchpad.
import ghidra.app.script.GhidraScript;
import ghidra.app.decompiler.*;
import ghidra.program.model.address.Address;
import ghidra.program.model.listing.*;
import ghidra.program.model.mem.*;
import ghidra.program.model.symbol.*;
import java.io.*;

public class DecompVSS extends GhidraScript {
    public void run() throws Exception {
        String outPath = "C:\\Users\\epilectrik\\AppData\\Local\\Temp\\claude\\C--git-bt411\\09bf24bb-f92d-423a-b815-1eb662736f58\\scratchpad\\vss_kids.txt";
        PrintWriter out = new PrintWriter(new FileWriter(outPath));
        FunctionManager fm = currentProgram.getFunctionManager();
        Memory mem = currentProgram.getMemory();
        DecompInterface dec = new DecompInterface();
        dec.openProgram(currentProgram);

        long[] targets = { 0x4c4170L, 0x4c5470L, 0x4c5b7cL, 0x4c5148L };
        for (long t : targets) {
            Address a = toAddr(t);
            Function f = fm.getFunctionContaining(a);
            if (f == null) {
                try { disassemble(a); f = createFunction(a, "FUN_" + Long.toHexString(t)); }
                catch (Exception e) { out.println("// could not create fn at " + a + ": " + e); }
            }
            out.println("\n//====================================================================");
            out.println("// FUN_" + Long.toHexString(t) + (f!=null ? "  ("+f.getName()+")" : ""));
            out.println("//====================================================================");
            if (f == null) { out.println("// NO FUNCTION"); continue; }
            DecompileResults r = dec.decompileFunction(f, 60, monitor);
            if (r != null && r.decompileCompleted()) out.println(r.getDecompiledFunction().getC());
            else out.println("// decompile failed: " + (r!=null?r.getErrorMessage():"null"));
        }

        // vtables for the cluster family
        long[] vts = { 0x51a020L /*SubsystemCluster*/, 0x519fd4L /*HeatSink*/, 0x519f88L /*Myomer*/,
                       0x519f38L /*Weapon*/, 0x519ee8L /*Energy*/, 0x519e98L /*Ballistic*/ };
        out.println("\n//==== cluster vtables ====");
        for (long vt : vts) {
            StringBuilder sb = new StringBuilder(String.format("vt 0x%x: ", vt));
            for (int i=0;i<20;i++){ long v = mem.getInt(toAddr(vt+i*4))&0xffffffffL; sb.append(String.format("[%d]=0x%x ",i,v)); }
            out.println(sb.toString());
        }
        // geometry table
        out.println("\n//==== geometry table @0x51bf34 (12 groups x 7 dwords) ====");
        for (int g=0; g<12; g++) {
            StringBuilder sb = new StringBuilder("group "+(g+1)+": ");
            for (int k=0;k<7;k++){ long v = mem.getInt(toAddr(0x51bf34L+(g*7L+k)*4))&0xffffffffL; sb.append(String.format("0x%x ",v)); }
            out.println(sb.toString());
        }
        out.close();
        println("DecompVSS full done -> " + outPath);
    }
}
