From 0d488e24e3e71667954582733fb342f55f33d896 Mon Sep 17 00:00:00 2001 From: Guus Waals <_@guusw.nl> Date: Mon, 2 Jun 2025 12:43:30 +0800 Subject: [PATCH] Fix global uniqueness --- GhidraScripts/CleanupDatabase.java | 1 + GhidraScripts/DumpCurrentFunction.java | 1 + GhidraScripts/DumpCurrentFunctionFix.java | 1 + GhidraScripts/DumpGlobals.java | 1 - GhidraScripts/ExportData.java | 1 + GhidraScripts/re3lib/GlobalDumper.java | 2 -- GhidraScripts/re3lib/TypeDumper.java | 15 ++++++++++----- game_re/gh_fix/r3_restore.cxx | 2 +- game_re/gh_global.cxx | 4 ++++ game_re/gh_global.h | 4 ++++ .../{level_displayFn.cxx => FUN_00447130.cxx} | 8 ++++---- game_re/gh_stub/FUN_00451530.cxx | 13 +++++++++++++ game_re/gh_stub/FUN_0045fc70.cxx | 13 +++++++++++++ game_re/gh_stub/FUN_0046fec0.cxx | 13 +++++++++++++ game_re/gh_stub/FUN_0048a920.cxx | 13 +++++++++++++ game_re/gh_stub/UNKr3_renderViewport.cxx | 13 +++++++++++++ game_re/gh_stub/doGraphics.cxx | 13 +++++++++++++ game_re/gh_stub/get_one.cxx | 13 +++++++++++++ game_re/gh_types.h | 1 + tooling/database.cpp | 2 +- 20 files changed, 120 insertions(+), 14 deletions(-) rename game_re/gh_stub/{level_displayFn.cxx => FUN_00447130.cxx} (64%) create mode 100644 game_re/gh_stub/FUN_00451530.cxx create mode 100644 game_re/gh_stub/FUN_0045fc70.cxx create mode 100644 game_re/gh_stub/FUN_0046fec0.cxx create mode 100644 game_re/gh_stub/FUN_0048a920.cxx create mode 100644 game_re/gh_stub/UNKr3_renderViewport.cxx create mode 100644 game_re/gh_stub/doGraphics.cxx create mode 100644 game_re/gh_stub/get_one.cxx diff --git a/GhidraScripts/CleanupDatabase.java b/GhidraScripts/CleanupDatabase.java index ab247c0d..c5163e56 100644 --- a/GhidraScripts/CleanupDatabase.java +++ b/GhidraScripts/CleanupDatabase.java @@ -1,6 +1,7 @@ // Cleanup the database of all missing files, and report duplicates // @category _Reman3 // @menupath Reman3.Cleanup Database +// @importpackage org.sqlite import ghidra.app.script.GhidraScript; import ghidra.program.model.listing.Function; diff --git a/GhidraScripts/DumpCurrentFunction.java b/GhidraScripts/DumpCurrentFunction.java index b03010db..79cd528a 100644 --- a/GhidraScripts/DumpCurrentFunction.java +++ b/GhidraScripts/DumpCurrentFunction.java @@ -1,6 +1,7 @@ // Script to export decompiled C code for selected function from Ghidra // @category _Reman3 // @menupath Reman3.Dump Current Function +// @importpackage org.sqlite import ghidra.app.script.GhidraScript; import ghidra.program.model.listing.Function; diff --git a/GhidraScripts/DumpCurrentFunctionFix.java b/GhidraScripts/DumpCurrentFunctionFix.java index d1e46939..1343d670 100644 --- a/GhidraScripts/DumpCurrentFunctionFix.java +++ b/GhidraScripts/DumpCurrentFunctionFix.java @@ -1,6 +1,7 @@ // Script to export decompiled C code for selected function from Ghidra as Fix type // @category _Reman3 // @menupath Reman3.Dump Current Function (Fix) +// @importpackage org.sqlite import ghidra.app.script.GhidraScript; import ghidra.program.model.listing.Function; diff --git a/GhidraScripts/DumpGlobals.java b/GhidraScripts/DumpGlobals.java index 4e23551a..c794bd07 100644 --- a/GhidraScripts/DumpGlobals.java +++ b/GhidraScripts/DumpGlobals.java @@ -17,7 +17,6 @@ public class DumpGlobals extends GhidraScript { try (FunctionDatabase functionDatabase = new FunctionDatabase(this)) { GlobalDumper globalDumper = new GlobalDumper(this, functionDatabase); - globalDumper.dumpGlobals(); TypeDumper dumper = new TypeDumper(this); diff --git a/GhidraScripts/ExportData.java b/GhidraScripts/ExportData.java index 1c622527..236fdbc7 100644 --- a/GhidraScripts/ExportData.java +++ b/GhidraScripts/ExportData.java @@ -1,6 +1,7 @@ // Exports binary read only and data segments to a binary + header file // @category _Reman3 // @menupath Reman3.Export Data Segment +// @importpackage org.sqlite import java.io.File; import java.io.FileOutputStream; diff --git a/GhidraScripts/re3lib/GlobalDumper.java b/GhidraScripts/re3lib/GlobalDumper.java index bc46174e..b64c648e 100644 --- a/GhidraScripts/re3lib/GlobalDumper.java +++ b/GhidraScripts/re3lib/GlobalDumper.java @@ -22,8 +22,6 @@ import ghidra.program.model.address.Address; import ghidra.program.model.data.AbstractStringDataType; import ghidra.program.model.data.Array; import ghidra.program.model.data.DataType; -import ghidra.program.model.data.DataTypeManager; -import ghidra.program.model.data.DataTypePath; import ghidra.program.model.data.PointerDataType; import ghidra.program.model.listing.Data; import ghidra.program.model.pcode.HighSymbol; diff --git a/GhidraScripts/re3lib/TypeDumper.java b/GhidraScripts/re3lib/TypeDumper.java index 1de39383..eb07eb80 100644 --- a/GhidraScripts/re3lib/TypeDumper.java +++ b/GhidraScripts/re3lib/TypeDumper.java @@ -15,6 +15,7 @@ import ghidra.program.model.data.DataType; import ghidra.program.model.data.Enum; import ghidra.program.model.data.EnumDataType; import ghidra.program.model.data.FunctionDefinition; +import ghidra.program.model.data.Pointer; import ghidra.program.model.data.ProgramBasedDataTypeManager; import ghidra.program.model.data.Structure; import ghidra.program.model.data.TypeDef; @@ -44,15 +45,19 @@ public class TypeDumper { for (GlobalEntry globalEntry : globals) { // Fetch type of global from ghidra Address addr = globalEntry.address; - if (globalEntry.name == "p_fn_vDisplayAll") - script.println("EEEEE"); + if (globalEntry.name.equals("p_fn_vDisplayAll")) + script.println("EE" + globalEntry.name + "EE"); + // script.print("Global: " + globalEntry.name); Data data = script.getDataAt(addr); if (data != null) { DataType type = data.getDataType(); - if (type instanceof FunctionDefinition) { - requiredFunctionTypes.add(type.getDisplayName()); - script.println("Adding required global function type: " + type.getDisplayName()); + if (type instanceof Pointer) { + DataType inner = ((Pointer) type).getDataType(); + if (inner instanceof FunctionDefinition) { + requiredFunctionTypes.add(inner.getDisplayName()); + script.println("Adding required global function type: " + type.getDisplayName()); + } } } } diff --git a/game_re/gh_fix/r3_restore.cxx b/game_re/gh_fix/r3_restore.cxx index 197a4dc3..00db072a 100644 --- a/game_re/gh_fix/r3_restore.cxx +++ b/game_re/gh_fix/r3_restore.cxx @@ -6,7 +6,7 @@ extern "C" { undefined FUN_00470350(void); // 00470350 // FUN_00470350 // cdecl undefined r3_load_textures_2(void); // 00471760 // r3_load_textures_2 // cdecl -void level_displayFn(byte param); // 00446fc0 // level_displayFn // cdecl +void r3_levelDisplayFn(byte param); // 00446fc0 // r3_levelDisplayFn // cdecl void r3_noop(void * p_cTxt1, void * p_cTxt2); // 00401100 // r3_noop // stdcall undefined r3_closeConcatTextureFile(void); // 004711d0 // r3_closeConcatTextureFile // cdecl undefined FUN_00470db0(void); // 00470db0 // FUN_00470db0 // cdecl diff --git a/game_re/gh_global.cxx b/game_re/gh_global.cxx index 362326f1..29bf971a 100644 --- a/game_re/gh_global.cxx +++ b/game_re/gh_global.cxx @@ -117,6 +117,9 @@ char(&s_volumeNameBuffer)[128] = reinterpret_cast(GH_MEM(0x005d27b r3_main_data& r3_main_data_005d28b6= (r3_main_data&) GH_MEM(0x005d28b6); dword& g_soundOnHD= (dword&) GH_MEM(0x005d29bc); dword& g_complete= (dword&) GH_MEM(0x005d29c0); +undefined1& DAT_005d2b18= (undefined1&) GH_MEM(0x005d2b18); +undefined4& DAT_005e693c= (undefined4&) GH_MEM(0x005e693c); +undefined4& DAT_0063be24= (undefined4&) GH_MEM(0x0063be24); int& g_windowInitialized= (int&) GH_MEM(0x0077d0a4); int& g_runMaximized= (int&) GH_MEM(0x0077d0a8); undefined4& g_engineShouldRun= (undefined4&) GH_MEM(0x0077d0b0); @@ -136,6 +139,7 @@ char *& g_crt_cmdLine= (char *&) GH_MEM(0x0077ea84); char(&s_quitting1)[64] = reinterpret_cast(GH_MEM(0x007825c0)); char(&s_wndStrQuiting)[56] = reinterpret_cast(GH_MEM(0x00782600)); GAM_EngineStructure& g_stEngineStructure= (GAM_EngineStructure&) GH_MEM(0x007d7dc0); +undefined1& DAT_007d98fc= (undefined1&) GH_MEM(0x007d98fc); undefined4& DAT_007d9cc4= (undefined4&) GH_MEM(0x007d9cc4); byte& g_config_unused0= (byte&) GH_MEM(0x007d9df0); char(&g_driveLetter)[4] = reinterpret_cast(GH_MEM(0x007d9e70)); diff --git a/game_re/gh_global.h b/game_re/gh_global.h index 38e5cebb..e1ca2fc9 100644 --- a/game_re/gh_global.h +++ b/game_re/gh_global.h @@ -122,6 +122,9 @@ extern char(&s_volumeNameBuffer)[128]; // 005d27b0 extern r3_main_data& r3_main_data_005d28b6; // 005d28b6 extern dword& g_soundOnHD; // 005d29bc extern dword& g_complete; // 005d29c0 +extern undefined1& DAT_005d2b18; // 005d2b18 +extern undefined4& DAT_005e693c; // 005e693c +extern undefined4& DAT_0063be24; // 0063be24 extern int& g_windowInitialized; // 0077d0a4 extern int& g_runMaximized; // 0077d0a8 extern undefined4& g_engineShouldRun; // 0077d0b0 @@ -141,6 +144,7 @@ extern char *& g_crt_cmdLine; // 0077ea84 extern char(&s_quitting1)[64]; // 007825c0 extern char(&s_wndStrQuiting)[56]; // 00782600 extern GAM_EngineStructure& g_stEngineStructure; // 007d7dc0 +extern undefined1& DAT_007d98fc; // 007d98fc extern undefined4& DAT_007d9cc4; // 007d9cc4 extern byte& g_config_unused0; // 007d9df0 extern char(&g_driveLetter)[4]; // 007d9e70 diff --git a/game_re/gh_stub/level_displayFn.cxx b/game_re/gh_stub/FUN_00447130.cxx similarity index 64% rename from game_re/gh_stub/level_displayFn.cxx rename to game_re/gh_stub/FUN_00447130.cxx index f15cf90f..b12ccfdc 100644 --- a/game_re/gh_stub/level_displayFn.cxx +++ b/game_re/gh_stub/FUN_00447130.cxx @@ -6,8 +6,8 @@ #include #include -// 00446fc0 -// level_displayFn -extern "C" void level_displayFn(byte param) { - gh_stub_impl_cdecl<0x00446fc0, void>(param); +// 00447130 +// FUN_00447130 +extern "C" undefined FUN_00447130(void) { + return gh_stub_impl_cdecl<0x00447130, undefined>(); } diff --git a/game_re/gh_stub/FUN_00451530.cxx b/game_re/gh_stub/FUN_00451530.cxx new file mode 100644 index 00000000..9b223b28 --- /dev/null +++ b/game_re/gh_stub/FUN_00451530.cxx @@ -0,0 +1,13 @@ +// AUTO-GENERATED FILE!!!! +// This function has yet to be decompiled using 'Dump Current Function' in ghidra +// with possible manualy fixes + +#include +#include +#include + +// 00451530 +// FUN_00451530 +extern "C" undefined FUN_00451530(undefined4 param_1) { + return gh_stub_impl_cdecl<0x00451530, undefined>(param_1); +} diff --git a/game_re/gh_stub/FUN_0045fc70.cxx b/game_re/gh_stub/FUN_0045fc70.cxx new file mode 100644 index 00000000..9fc686a2 --- /dev/null +++ b/game_re/gh_stub/FUN_0045fc70.cxx @@ -0,0 +1,13 @@ +// AUTO-GENERATED FILE!!!! +// This function has yet to be decompiled using 'Dump Current Function' in ghidra +// with possible manualy fixes + +#include +#include +#include + +// 0045fc70 +// FUN_0045fc70 +extern "C" undefined FUN_0045fc70(undefined4 param_1) { + return gh_stub_impl_cdecl<0x0045fc70, undefined>(param_1); +} diff --git a/game_re/gh_stub/FUN_0046fec0.cxx b/game_re/gh_stub/FUN_0046fec0.cxx new file mode 100644 index 00000000..286f405e --- /dev/null +++ b/game_re/gh_stub/FUN_0046fec0.cxx @@ -0,0 +1,13 @@ +// AUTO-GENERATED FILE!!!! +// This function has yet to be decompiled using 'Dump Current Function' in ghidra +// with possible manualy fixes + +#include +#include +#include + +// 0046fec0 +// FUN_0046fec0 +extern "C" undefined FUN_0046fec0(void) { + return gh_stub_impl_cdecl<0x0046fec0, undefined>(); +} diff --git a/game_re/gh_stub/FUN_0048a920.cxx b/game_re/gh_stub/FUN_0048a920.cxx new file mode 100644 index 00000000..5832651f --- /dev/null +++ b/game_re/gh_stub/FUN_0048a920.cxx @@ -0,0 +1,13 @@ +// AUTO-GENERATED FILE!!!! +// This function has yet to be decompiled using 'Dump Current Function' in ghidra +// with possible manualy fixes + +#include +#include +#include + +// 0048a920 +// FUN_0048a920 +extern "C" undefined FUN_0048a920(undefined4 param_1) { + return gh_stub_impl_cdecl<0x0048a920, undefined>(param_1); +} diff --git a/game_re/gh_stub/UNKr3_renderViewport.cxx b/game_re/gh_stub/UNKr3_renderViewport.cxx new file mode 100644 index 00000000..acab9cd6 --- /dev/null +++ b/game_re/gh_stub/UNKr3_renderViewport.cxx @@ -0,0 +1,13 @@ +// AUTO-GENERATED FILE!!!! +// This function has yet to be decompiled using 'Dump Current Function' in ghidra +// with possible manualy fixes + +#include +#include +#include + +// 00447200 +// ?r3_renderViewport +extern "C" undefined UNKr3_renderViewport(undefined4 viewportIdx) { + return gh_stub_impl_cdecl<0x00447200, undefined>(viewportIdx); +} diff --git a/game_re/gh_stub/doGraphics.cxx b/game_re/gh_stub/doGraphics.cxx new file mode 100644 index 00000000..997b9c77 --- /dev/null +++ b/game_re/gh_stub/doGraphics.cxx @@ -0,0 +1,13 @@ +// AUTO-GENERATED FILE!!!! +// This function has yet to be decompiled using 'Dump Current Function' in ghidra +// with possible manualy fixes + +#include +#include +#include + +// 00447460 +// doGraphics +extern "C" void doGraphics(byte param_1) { + gh_stub_impl_stdcall<0x00447460, void>(param_1); +} diff --git a/game_re/gh_stub/get_one.cxx b/game_re/gh_stub/get_one.cxx new file mode 100644 index 00000000..c2317c47 --- /dev/null +++ b/game_re/gh_stub/get_one.cxx @@ -0,0 +1,13 @@ +// AUTO-GENERATED FILE!!!! +// This function has yet to be decompiled using 'Dump Current Function' in ghidra +// with possible manualy fixes + +#include +#include +#include + +// 0046f2c0 +// get_one +extern "C" int get_one(void) { + return gh_stub_impl_cdecl<0x0046f2c0, int>(); +} diff --git a/game_re/gh_types.h b/game_re/gh_types.h index 0b75cf23..044525d0 100644 --- a/game_re/gh_types.h +++ b/game_re/gh_types.h @@ -5153,6 +5153,7 @@ struct tdstCsaList_ { #define C_wTimerPaused 4 struct _tdstRangeSplitListDisk { }; +typedef undefined (*level_displayFn)(void); #define __STDC__ 1 #define C_SNDREAL_0 0 #define C_SNDREAL_1 65536 diff --git a/tooling/database.cpp b/tooling/database.cpp index ac51ea4c..ef348e31 100644 --- a/tooling/database.cpp +++ b/tooling/database.cpp @@ -82,7 +82,7 @@ DatabaseManager::DatabaseManager(const std::string &db_path) : db(nullptr) { const char *create_tables = R"( CREATE TABLE IF NOT EXISTS Functions (filepath TEXT, name TEXT, address TEXT, type INTEGER DEFAULT 0, calling_convention INTEGER DEFAULT 0, parameter_names TEXT DEFAULT '', parameter_types TEXT DEFAULT '', return_type TEXT DEFAULT '', PRIMARY KEY (name, filepath)); CREATE TABLE IF NOT EXISTS Imports (filepath TEXT, name TEXT, address TEXT, type INTEGER DEFAULT 0, calling_convention INTEGER DEFAULT 0, parameter_names TEXT DEFAULT '', parameter_types TEXT DEFAULT '', return_type TEXT DEFAULT '', PRIMARY KEY (name, filepath)); - CREATE TABLE IF NOT EXISTS Globals (filepath TEXT, name TEXT, address TEXT); + CREATE TABLE IF NOT EXISTS Globals (filepath TEXT, name TEXT, address TEXT, PRIMARY KEY (name, filepath)); )"; sqlite3_exec(db, create_tables, nullptr, nullptr, nullptr);