diff --git a/decompile/Makefile b/decompile/Makefile index 0be07a0..8691d92 100644 --- a/decompile/Makefile +++ b/decompile/Makefile @@ -31,19 +31,17 @@ src/JSRF/Jet2.exe: $(OBJ) $(LIB) LIB.EXE /NOLOGO /MACHINE:X86 /DEF:$< /OUT:$@ # Compile object files from source -.c.obj: - CL.EXE /nologo /Wall /W4 /Ogityb0 /GfX /Fo$@ /c $< .cpp.obj: CL.EXE /nologo /Wall /W4 /Ogityb0 /GfX /Fo$@ /c $< # Header files used for each object -src/JSRF/Jet2.obj: src/JSRF/Core.hpp src/Std.hpp src/XDK/D3D.h\ - src/XDK/Win32.h +src/JSRF/Jet2.obj: src/JSRF/Core.hpp src/Std.hpp src/XDK/D3D.hpp\ + src/XDK/Win32.hpp src/JSRF/Core.obj: src/JSRF/Core.hpp src/Smilebit/MMatrix.hpp src/Std.hpp\ - src/XDK/D3D.h src/XDK/Win32.h + src/XDK/D3D.hpp src/XDK/Win32.hpp src/JSRF/GameData.obj: src/JSRF/GameData.hpp -src/XDK/CRT/CRT0.obj: src/XDK/Win32.h +src/XDK/CRT/CRT0.obj: src/XDK/Win32.hpp diff --git a/decompile/src/JSRF/Core.hpp b/decompile/src/JSRF/Core.hpp index 984d5fd..7d4f490 100644 --- a/decompile/src/JSRF/Core.hpp +++ b/decompile/src/JSRF/Core.hpp @@ -7,8 +7,8 @@ Game and GameObj classes that form the foundation of the JSRF game code. #include "../Smilebit/MMatrix.hpp" #include "../Std.hpp" -#include "../XDK/D3D.h" -#include "../XDK/Win32.h" +#include "../XDK/D3D.hpp" +#include "../XDK/Win32.hpp" // TODO; move to header for Graphics COM object in Smilebit libraries diff --git a/decompile/src/JSRF/GameData.cpp b/decompile/src/JSRF/GameData.cpp index 0efbbbc..e9bba24 100644 --- a/decompile/src/JSRF/GameData.cpp +++ b/decompile/src/JSRF/GameData.cpp @@ -4,7 +4,7 @@ Save data and closely-related runtime data. #pragma bss_seg(".data") -#include "../XDK/Win32.h" +#include "../XDK/Win32.hpp" #include "GameData.hpp" diff --git a/decompile/src/JSRF/GameData.hpp b/decompile/src/JSRF/GameData.hpp index 0169de2..75f3e65 100644 --- a/decompile/src/JSRF/GameData.hpp +++ b/decompile/src/JSRF/GameData.hpp @@ -5,7 +5,7 @@ Save data and closely-related runtime data. #ifndef GAMEDATA_HPP #define GAMEDATA_HPP -#include "../XDK/Win32.h" +#include "../XDK/Win32.hpp" // Data structure actually saved to disk diff --git a/decompile/src/Smilebit/MMatrix.cpp b/decompile/src/Smilebit/MMatrix.cpp index 1bc46d9..83b8e07 100644 --- a/decompile/src/Smilebit/MMatrix.cpp +++ b/decompile/src/Smilebit/MMatrix.cpp @@ -7,7 +7,7 @@ Smilebit's stack-based matrix math library. #include "../Std.hpp" -#include "../XDK/Win32.h" +#include "../XDK/Win32.hpp" #include "MMatrix.hpp" diff --git a/decompile/src/Smilebit/MMatrix.hpp b/decompile/src/Smilebit/MMatrix.hpp index 9d7354d..788740e 100644 --- a/decompile/src/Smilebit/MMatrix.hpp +++ b/decompile/src/Smilebit/MMatrix.hpp @@ -5,7 +5,7 @@ Smilebit's stack-based matrix math library. #ifndef MMATRIX_HPP #define MMATRIX_HPP -#include "../XDK/D3D.h" +#include "../XDK/D3D.hpp" // 4x4 matrix type diff --git a/decompile/src/XDK/CRT/CRT0.c b/decompile/src/XDK/CRT/CRT0.cpp similarity index 67% rename from decompile/src/XDK/CRT/CRT0.c rename to decompile/src/XDK/CRT/CRT0.cpp index bcaf52d..c305130 100644 --- a/decompile/src/XDK/CRT/CRT0.c +++ b/decompile/src/XDK/CRT/CRT0.cpp @@ -1,24 +1,24 @@ -/* JSRF Decompilation: XDK/CRT0.c +/* JSRF Decompilation: XDK/CRT0.cpp C runtime initialization. Like other CRT code, there's some magic here with symbols that get special treatment from the compiler and linker. */ -#include "../Win32.h" +#include "../Win32.hpp" // Every program is supposed to have a main(), so we can just assume its // existence with a declaration here -int main(void); +int main(); -void __cdecl mainCRTStartup (void); -DWORD __stdcall mainXapiStartup(LPVOID lpThreadParameter); + void __stdcall mainCRTStartup (); +static DWORD _mainXapiStartup(LPVOID lpThreadParameter); // Address: 0x00148023 // Matching: no -void __cdecl mainCRTStartup(void) { +void __stdcall mainCRTStartup() { /* The true entrypoint of the game, spawning a thread for the rest to run in The linker automatically sets this function to the entrypoint. */ @@ -26,7 +26,7 @@ The linker automatically sets this function to the entrypoint. // Address: 0x00147FB4 // Matching: no -DWORD __stdcall mainXapiStartup(LPVOID const lpThreadParameter) { +DWORD _mainXapiStartup(LPVOID const lpThreadParameter) { /* Runs some initialization and then calls main() */ main(); return 0; diff --git a/decompile/src/XDK/D3D.h b/decompile/src/XDK/D3D.hpp similarity index 55% rename from decompile/src/XDK/D3D.h rename to decompile/src/XDK/D3D.hpp index 325035f..cea0682 100644 --- a/decompile/src/XDK/D3D.h +++ b/decompile/src/XDK/D3D.hpp @@ -1,16 +1,11 @@ -/* JSRF Decompilation: XDK/D3D.h +/* JSRF Decompilation: XDK/D3D.hpp Direct3D8 declarations. */ -#ifndef D3D_H -#define D3D_H +#ifndef D3D_HPP +#define D3D_HPP -#include "Win32.h" - - -#ifdef __cplusplus -extern "C" { -#endif +#include "Win32.hpp" typedef DWORD D3DCOLOR; @@ -26,10 +21,4 @@ struct D3DRECT { LONG x1, y1, x2, y2; }; - -#ifdef __cplusplus -} -#endif - - #endif diff --git a/decompile/src/XDK/Win32.h b/decompile/src/XDK/Win32.hpp similarity index 89% rename from decompile/src/XDK/Win32.h rename to decompile/src/XDK/Win32.hpp index 8bb6546..ab72d1f 100644 --- a/decompile/src/XDK/Win32.h +++ b/decompile/src/XDK/Win32.hpp @@ -1,15 +1,9 @@ -/* JSRF Decompilation: XDK/Win32.h +/* JSRF Decompilation: XDK/Win32.hpp Definitions normally provided by Windows headers. */ -#ifndef WIN32_H -#define WIN32_H - - -#ifdef __cplusplus -extern "C" { -#endif - +#ifndef WIN32_HPP +#define WIN32_HPP // The famous Win32 typedefs typedef unsigned char UCHAR; @@ -61,10 +55,4 @@ LPVOID __stdcall VirtualAlloc( DWORD flProtect ); - -#ifdef __cplusplus -} -#endif - - #endif diff --git a/ghidra/ghidra_scripts/EnhancedImport.java b/ghidra/ghidra_scripts/EnhancedImport.java index 00fcdcb..3bdf040 100644 --- a/ghidra/ghidra_scripts/EnhancedImport.java +++ b/ghidra/ghidra_scripts/EnhancedImport.java @@ -5,20 +5,15 @@ import ghidra.app.script.GhidraScript; import ghidra.app.services.DataTypeQueryService; import ghidra.program.model.address.Address; -import ghidra.program.model.data.ArrayDataType; import ghidra.program.model.data.DataType; -import ghidra.program.model.data.PointerDataType; import ghidra.program.model.listing.Data; import ghidra.program.model.listing.Function; import ghidra.program.model.symbol.Namespace; import ghidra.program.model.symbol.SourceType; import ghidra.program.model.symbol.Symbol; -import ghidra.util.StringUtilities; import java.io.FileReader; -import java.util.ArrayList; import java.util.Arrays; -import java.util.Collections; import java.util.List; import java.util.Optional; @@ -39,7 +34,7 @@ public class EnhancedImport extends GhidraScript{ case "func": importFunc(addr, parts); break; default: throw new Exception( "Symbol type \"" + parts[1] + - "\" on line " + String.valueOf(i) + + "\" on line " + Integer.toString(i) + " is not \"data\" or \"func\"" ); } @@ -50,7 +45,7 @@ public class EnhancedImport extends GhidraScript{ final Address addr, final String[] parts ) throws Exception { - final String name = unqualified(parts[3]); + final String name = unqualified(parts[3]); print("Importing data symbol \"" + parts[3] + "\"..."); @@ -59,14 +54,21 @@ public class EnhancedImport extends GhidraScript{ final Symbol s = Optional.ofNullable(getSymbolAt(addr)) .orElse(createLabel(addr, name, ns, true, SourceType.USER_DEFINED)); - // Create data - final Optional t_maybe = makeType(parts[2]); - if (t_maybe.orElse(null) instanceof DataType t) { - clearListing(addr, addr.add(Math.max(t.getLength(), 1) - 1)); - currentProgram.getListing().createData(addr, t); + // Create data (TODO: parse type name to get base name, then pass base type to PointerDataType and ArrayDataType constructors to create final type) + final List foundTypes = state.getTool() + .getService(DataTypeQueryService.class) + .findDataTypes(parts[2], null); + if (foundTypes.size() == 0) { + println(" can't find data type \"" + parts[2] + "\", skipping."); + return; + } - println(" done."); - } else println(", skipping."); + final DataType t = foundTypes.get(0); // Boldly assume first is right + + clearListing(addr, addr.add(Math.max(t.getLength(), 1) - 1)); + currentProgram.getListing().createData(addr, t); + + println(" done."); } private static String unqualified(final String qualifiedName) { @@ -91,64 +93,6 @@ public class EnhancedImport extends GhidraScript{ return ns; } - private Optional makeType(final String type) throws Exception { - /* Attempt to create the described type from a known base type */ - final String baseName = StringUtilities.findWord(type, 0); - final List foundTypes = state.getTool() - .getService(DataTypeQueryService.class) - .findDataTypes(baseName, null); - if (foundTypes.size() == 0) { - print(" can't find data type \"" + baseName + "\""); - return Optional.empty(); - } - - return Optional.of(derivedType( - foundTypes.get(0), // Boldly assume first is right - type.substring(baseName.length()) - )); - } - - private static DataType derivedType( - final DataType t, - final String modifiers - ) throws Exception { - /* Return the given datatype with given pointer/array modifiers */ - DataType ret = t; - for (int i = 0; i < modifiers.length(); i++) switch (modifiers.charAt(i)) { - case '*': - ret = new PointerDataType(ret); - continue; - - case '[': - // Read array dimensions - final ArrayList dims = new ArrayList<>(); - do { - final String n = StringUtilities.findWord(modifiers, i+1); - i += n.length() + 1; - - dims.add(Integer.valueOf(n)); - } while (modifiers.charAt(i) == '['); - - // Apply array - Collections.reverse(dims); - for (int dim : dims) ret = new ArrayDataType(ret, dim); - - continue; - - case ' ': - continue; - - default: - throw new Exception( - "Unexpected character \"" + - String.valueOf(modifiers.charAt(i)) + - "\" in type modifiers" - ); - } - - return ret; - } - private void importFunc( final Address addr, final String[] parts