diff --git a/game_re/CMakeLists.txt b/game_re/CMakeLists.txt index 06dc0354..8d123233 100644 --- a/game_re/CMakeLists.txt +++ b/game_re/CMakeLists.txt @@ -1,64 +1,71 @@ -add_executable(game_re - r3/main.cpp - r3/binders/static_mem.cxx - gh_global.cxx -) - -target_compile_features(game_re PUBLIC cxx_std_20) -target_include_directories(game_re PUBLIC ${CMAKE_CURRENT_SOURCE_DIR}) - -set_target_properties( - game_re PROPERTIES - RUNTIME_OUTPUT_DIRECTORY ${CMAKE_BINARY_DIR}/bin -) - -if(WIN32 AND R3_32BIT) - target_link_libraries(game_re PRIVATE - binkw32 - d3d8 - dinput8 +function(setup_target TARGET) + add_executable(${TARGET} + r3/main.cpp + r3/binders/static_mem.cxx + gh_global.cxx ) -endif() -target_compile_definitions(game_re PRIVATE - R3_GAME_DATA_DIR=\"${GAME_DATA_DIR}\" -) + target_compile_features(${TARGET} PUBLIC cxx_std_20) + target_include_directories(${TARGET} PUBLIC ${CMAKE_CURRENT_SOURCE_DIR}) -get_filename_component(R3_DATA_SEGMENT_FILE "${CMAKE_CURRENT_SOURCE_DIR}/gh_datasegment.bin" ABSOLUTE) -target_compile_definitions(game_re PRIVATE - R3_DATA_SEGMENT_FILE=\"${R3_DATA_SEGMENT_FILE}\" -) + set_target_properties( + ${TARGET} PROPERTIES + RUNTIME_OUTPUT_DIRECTORY ${CMAKE_BINARY_DIR}/bin + ) -target_compile_definitions(game_re PRIVATE - _CRT_SECURE_NO_WARNINGS=1 - _CRT_NONSTDC_NO_WARNINGS=1) + if(WIN32 AND R3_32BIT) + target_link_libraries(${TARGET} PRIVATE + binkw32 + d3d8 + dinput8 + ) + endif() -target_link_libraries(game_re PRIVATE spdlog) + target_compile_definitions(${TARGET} PRIVATE + R3_GAME_DATA_DIR=\"${GAME_DATA_DIR}\" + ) -file(GLOB GH_AUTO_SOURCES ${CMAKE_CURRENT_SOURCE_DIR}/gh_auto/*.cxx) -file(GLOB GH_FIX_SOURCES ${CMAKE_CURRENT_SOURCE_DIR}/gh_fix/*.cxx) -file(GLOB GH_STUB_SOURCES ${CMAKE_CURRENT_SOURCE_DIR}/gh_stub/*.cxx) + get_filename_component(R3_DATA_SEGMENT_FILE "${CMAKE_CURRENT_SOURCE_DIR}/gh_datasegment.bin" ABSOLUTE) + target_compile_definitions(${TARGET} PRIVATE + R3_DATA_SEGMENT_FILE=\"${R3_DATA_SEGMENT_FILE}\" + ) -target_sources(game_re PRIVATE - ${GH_AUTO_SOURCES} - ${GH_FIX_SOURCES} - ${GH_STUB_SOURCES} -) + target_compile_definitions(${TARGET} PRIVATE + _CRT_SECURE_NO_WARNINGS=1 + _CRT_NONSTDC_NO_WARNINGS=1) -# Ignore -Wformat-security -target_compile_options(game_re PRIVATE -Wno-format-security) + target_link_libraries(${TARGET} PRIVATE spdlog) -# Ignore -Wmicrosoft-cast -target_compile_options(game_re PRIVATE -Wno-microsoft-cast) + file(GLOB GH_AUTO_SOURCES ${CMAKE_CURRENT_SOURCE_DIR}/gh_auto/*.cxx) + file(GLOB GH_FIX_SOURCES ${CMAKE_CURRENT_SOURCE_DIR}/gh_fix/*.cxx) + file(GLOB GH_STUB_SOURCES ${CMAKE_CURRENT_SOURCE_DIR}/gh_stub/*.cxx) -# Automatically re-run CMake if any gh_*.cxx files change -# due to ghidra script runs -set_property( - DIRECTORY - APPEND - PROPERTY CMAKE_CONFIGURE_DEPENDS ${CMAKE_CURRENT_SOURCE_DIR}/gh_cmake_timestamp -) + target_sources(${TARGET} PRIVATE + ${GH_AUTO_SOURCES} + ${GH_FIX_SOURCES} + ${GH_STUB_SOURCES} + ) -target_precompile_headers(game_re PRIVATE - "$<$:${CMAKE_CURRENT_SOURCE_DIR}/r3/binders/auto_pch.cxx>" -) + # Ignore -Wformat-security + target_compile_options(${TARGET} PRIVATE -Wno-format-security) + + # Ignore -Wmicrosoft-cast + target_compile_options(${TARGET} PRIVATE -Wno-microsoft-cast) + + # Automatically re-run CMake if any gh_*.cxx files change + # due to ghidra script runs + set_property( + DIRECTORY + APPEND + PROPERTY CMAKE_CONFIGURE_DEPENDS ${CMAKE_CURRENT_SOURCE_DIR}/gh_cmake_timestamp + ) + + target_precompile_headers(${TARGET} PRIVATE + "$<$:${CMAKE_CURRENT_SOURCE_DIR}/r3/binders/auto_pch.cxx>" + ) +endfunction() + +setup_target(game_re) + +setup_target(game_dbg) +target_compile_definitions(game_dbg PRIVATE RE_DBG_INJECTED=1) diff --git a/game_re/scan_sources b/game_re/scan_sources index f1f96975..984a2479 100644 --- a/game_re/scan_sources +++ b/game_re/scan_sources @@ -6,14 +6,21 @@ set -e pushd $script_dir -file_list=files.txt -rm -f $file_list -for dir in gh_auto gh_stub gh_fix; do - for file in $dir/*.cxx; do - echo $file >>$file_list - done +mkdir -p logs +types=(auto ref fix stub) +for type in "${types[@]}"; do + file_list="logs/files_${type}.txt" + rm -f "$file_list" + + if [ -d "gh_${type}" ]; then + for file in "gh_${type}"/*.cxx; do + echo "$file" >>"$file_list" + done + $tool "@$file_list" -v --type=$type --log-file=logs/log-functions-${type}.txt + fi done -$tool @$file_list +$tool gh_global.h -mglobals -v --log-file=logs/log-globals.txt +$tool -mduplicates -v --log-file=logs/log-duplicates.txt popd diff --git a/java/ghidra/Test.java b/java/ghidra/Test.java index 63137447..997d36c5 100644 --- a/java/ghidra/Test.java +++ b/java/ghidra/Test.java @@ -1,5 +1,6 @@ // @category _Reman3 // @menupath Reman3.Test +// @importpackage org.sqlite import ghidra.app.script.GhidraScript; import ghidra.program.model.address.Address; @@ -26,15 +27,15 @@ import java.sql.ResultSet; import java.sql.SQLException; import java.sql.Statement; -public class Test extends GhidraScript { - // Will rebuild all functions - public boolean rebuildAllGlobals = true; - FunctionDatabase functionDB; +import org.sqlite.JDBC; +public class Test extends GhidraScript { @Override public void run() throws Exception { RecompileConfig.INSTANCE = new RecompileConfig(this); + java.sql.DriverManager.registerDriver(new JDBC()); + // Example SQLite usage testSQLite(); } @@ -80,14 +81,4 @@ public class Test extends GhidraScript { throw e; } } - - private void scanDirectory(File directory, FunctionDatabase.Type type) throws Exception { - File[] files = directory.listFiles((dir, name) -> name.endsWith(".cxx")); - if (files == null) - return; - - for (File file : files) { - scanFile(file, type); - } - } } \ No newline at end of file diff --git a/java/ghidra/re3lib/FunctionDatabase.java b/java/ghidra/re3lib/FunctionDatabase.java index e35fe480..99467ab6 100644 --- a/java/ghidra/re3lib/FunctionDatabase.java +++ b/java/ghidra/re3lib/FunctionDatabase.java @@ -16,7 +16,8 @@ public class FunctionDatabase { public enum Type { Auto, Fix, - Stub + Stub, + Ref } public class Dependency implements java.io.Serializable { diff --git a/java/ghidra/readme.md b/java/ghidra/readme.md index e2d20dae..18add8cc 100644 --- a/java/ghidra/readme.md +++ b/java/ghidra/readme.md @@ -1,3 +1,13 @@ # Ghidra Scripts -Add this to your scripts folder and run to generate c code for all functions in the project \ No newline at end of file +Add this to your ghidra scripts folder to add the reman decompile scripts to ghidra + +NOTE: Make sure to also add the include `sqlite-jdbc-3.49.1.0.jar` to the ghidra scripts folders, as this is required for the ghidra scripts to read the decompile database. + +## Decompile database + +The decompile database is a sqlite database that contains a list of all files that implement decompiled functions and their source address. It is used to regenerate files, check for duplicates, and more. + +To generate the database from the current set of files, run the scan_sources script in the /game_re folder. + +Make sure you have set up the tooling by running the /tooling/setup script.