// Script to export decompiled C code for selected function from Ghidra
// @category _Reman3
// @menupath Reman3.Dump Current Function

import ghidra.app.script.GhidraScript;
import ghidra.program.model.listing.Function;
import re3lib.FunctionDumper;
import re3lib.GlobalDumper;
import re3lib.RecompileConfig;

public class DumpCurrentFunction extends GhidraScript {
  @Override
  public void run() throws Exception {
    RecompileConfig.INSTANCE = new RecompileConfig(this);
    RecompileConfig.INSTANCE.createDirectories();

    GlobalDumper globalDumper = new GlobalDumper(this);
    globalDumper.loadGlobalManifest();
    FunctionDumper functionDumper = new FunctionDumper(this, globalDumper);

    Function currentFunction = getFunctionContaining(currentAddress);
    if (currentFunction != null) {
      functionDumper.dump(currentFunction);
    } else {
      println("No function found at the current address.");
    }

    if (functionDumper.createdFile)
      RecompileConfig.INSTANCE.touchCMakeTimestamp();

    globalDumper.dumpGlobals();
    globalDumper.saveGlobalManifest();
  }
}
