### Example commands and output for XenonRecomp Source: https://github.com/hedge-dev/xenonrecomp Two example commands. The first invokes the recompiler with a TOML and header, producing the listed output files into the configured out directory. The second runs test mode, recompiling each .o and generating main.cpp plus per-test .cpp files. ```bash XenonRecomp SWA.toml XenonUtils/ppc_context.h # Outputs ppc_config.h, ppc_context.h, ppc_recomp_shared.h, # ppc_func_mapping.cpp, ppc_recomp.{0..n}.cpp into the configured out directory. XenonRecomp xenia_testing/bin XenonUtils/ppc_context.h out_tests # Test mode: recompiles each .o and generates main.cpp + per-test .cpp files. ``` -------------------------------- ### LoadFile usage example Source: https://github.com/hedge-dev/xenonrecomp Shows how to call LoadFile and handle the empty-vector failure case, which also occurs for zero-byte files. ```cpp #include const auto data = LoadFile("default.xex"); if (data.empty()) { // Unreadable file. return; } Image image = Image::ParseImage(data.data(), data.size()); ``` -------------------------------- ### MemoryMappedFile usage example Source: https://github.com/hedge-dev/xenonrecomp Shows mapping a file and passing the data to XexPatcher::apply. This is how the path-based apply overload reads its inputs. ```cpp #include #include MemoryMappedFile baseXex("default.xex"); if (!baseXex.isOpen()) { return; } std::vector outBytes; XexPatcher::Result r = XexPatcher::apply( baseXex.data(), baseXex.size(), patch.data(), patch.size(), outBytes, false); ``` -------------------------------- ### Xex2LoadImage usage example Source: https://github.com/hedge-dev/xenonrecomp Shows how to load a XEX2 file and call Xex2LoadImage. Checks if the resulting image has no sections, indicating a decryption, decompression, or parse failure. ```cpp #include #include const auto bytes = LoadFile("default.xex"); Image image = Xex2LoadImage(bytes.data(), bytes.size()); if (image.sections.empty()) { // Decryption/decompression/parse failure. } ``` -------------------------------- ### Recompiler usage example in C++ Source: https://github.com/hedge-dev/xenonrecomp Demonstrates the complete workflow: loading a TOML configuration, analysing the image, renaming the entry point symbol to '_xstart', and recompiling with a specified PPC context header. The Recompile call generates multiple output files including ppc_config.h, ppc_context.h, ppc_recomp_shared.h, ppc_func_mapping.cpp, and ppc_recomp.{0..n}.cpp. On failure to load the config, the function returns -1. ```cpp #include Recompiler recompiler; if (!recompiler.LoadConfig("SWA.toml")) { // Patch load/apply failure (see errors.md). return -1; } recompiler.Analyse(); // Rename the entry point like XenonRecomp/main.cpp does. auto entry = recompiler.image.symbols.find(recompiler.image.entry_point); if (entry != recompiler.image.symbols.end()) entry->name = "_xstart"; recompiler.Recompile("XenonUtils/ppc_context.h"); // Output: /ppc_config.h, ppc_context.h, // ppc_recomp_shared.h, ppc_func_mapping.cpp, ppc_recomp.{0..n}.cpp ``` -------------------------------- ### Load and iterate achievements from XDBF file Source: https://github.com/hedge-dev/xenonrecomp Loads an XDBF file (e.g., achievements.spa) and wraps it in XDBFWrapper. Checks pBuffer for validity, then iterates achievements in English and fetches a specific achievement by ID in Japanese. The example shows how to access achievement fields and handle missing IDs. ```cpp #include #include const auto spaData = LoadFile("achievements.spa"); XDBFWrapper wrapper(spaData.data(), spaData.size()); if (wrapper.pBuffer == nullptr) { // Not a valid XDBF file. return; } for (const Achievement& a : wrapper.GetAchievements(XDBF_LANGUAGE_ENGLISH)) { // a.ID, a.Name, a.UnlockedDesc, a.Score, a.pImageBuffer/a.ImageBufferSize } Achievement specific = wrapper.GetAchievement(XDBF_LANGUAGE_JAPANESE, 42); if (specific.ID == 0) { // 42 not present in the file. } ``` -------------------------------- ### Using xpointer with XANSI_STRING Source: https://github.com/hedge-dev/xenonrecomp Shows how to access the host pointer from an xpointer member (str.Buffer) via get() to print the string. ```cpp XANSI_STRING str{}; // str.Buffer is an xpointer: guest address on disk, host pointer in C++. printf("%s", str.Buffer.get()); ``` -------------------------------- ### Using ByteSwap and ByteSwapInplace Source: https://github.com/hedge-dev/xenonrecomp Example usage of ByteSwap and ByteSwapInplace. Includes and swaps a big-endian instruction word, then swaps it in place. ```cpp #include uint32_t hostWord = ByteSwap(0xE9C1FF68u); // swap a big-endian instruction word ByteSwapInplace(hostWord); ``` -------------------------------- ### TOML midasm_hook configuration example Source: https://github.com/hedge-dev/xenonrecomp Defines two mid-asm hooks in the top-level TOML. The first hook uses registers r3, the second uses r3 and fpscr with return_on_true. Address is the map key; the instruction at that address is not overwritten. ```toml [[midasm_hook]] name = "IndexBufferLengthMidAsmHook" address = 0x82E26244 registers = ["r3"] [[midasm_hook]] name = "CheckSomething" address = 0x82E24000 registers = ["r3", "fpscr"] return_on_true = true ``` -------------------------------- ### Example usage of Image::ParseImage and Find Source: https://github.com/hedge-dev/xenonrecomp Shows how to load a supported executable (XEX2 or big-endian ELF) using LoadFile and Image::ParseImage, then check for empty sections, resolve code bytes by virtual address with Image::Find, and locate a section by name. The comment notes that pdata->data / pdata->size give raw bytes of the section. ```cpp #include #include // Parse any supported executable (XEX2 or big-endian ELF). const auto file = LoadFile("default.xex"); // XenonUtils/file.h Image image = Image::ParseImage(file.data(), file.size()); if (image.sections.empty()) { // Unrecognized or failed image load. } // Resolve code bytes by virtual address. const void* entryCode = image.Find(image.entry_point); // Locate a section by name. const Section* pdata = image.Find(".pdata"); if (pdata != nullptr) { // pdata->data / pdata->size give raw bytes of the section. } ``` -------------------------------- ### Example usage of TestRecompiler::RecompileTests Source: https://github.com/hedge-dev/xenonrecomp Shows how to invoke RecompileTests with source and destination directories. The generated main.cpp is compiled by the XenonTests CMake target and prints mismatches. ```cpp #include // Recompile Xenia's PPC tests from their build output. TestRecompiler::RecompileTests( "xenia/src/xenia/cpu/ppc/testing/bin", "XenonTests/src"); ``` -------------------------------- ### Optional functions and invalid_instructions config .toml Source: https://github.com/hedge-dev/xenonrecomp/wiki/Overview Example from Sonic: Unleashed showing optional arrays for function boundaries (functions) and invalid instructions (invalid_instructions) in the config .toml. These are needed when functions do not exist in .pdata or have jump tables that do not analyze properly. ```toml # These functions do not exist in .pdata and do # not analyze properly due to having jump tables functions = [ { address = 0x824E7EF0, size = 0x98 }, { address = 0x824E7F28, size = 0x60 }, { address = 0x82C980E8, size = 0x110 }, { address = 0x82CF7080, size = 0x80 }, { address = 0x82D9AC08, size = 0x78 }, { address = 0x82E86770, size = 0x98 }, { address = 0x82E97E50, size = 0x84 }, { address = 0x82EE2D08, size = 0x154 }, { address = 0x82EF5C38, size = 0x64 }, { address = 0x82EF5D78, size = 0x3F8 }, { address = 0x82F08730, size = 0x2B0 }, { address = 0x82F098C0, size = 0x19C }, { address = 0x82F13980, size = 0xF4 }, { address = 0x82F1D668, size = 0x1E8 }, { address = 0x82F22908, size = 0x20C }, { address = 0x82F25FD8, size = 0x240 }, { address = 0x82F852A0, size = 0xCC }, { address = 0x830DADA0, size = 0x150 }, { address = 0x831487D0, size = 0xD4 }, { address = 0x831530C8, size = 0x258 }, { address = 0x831539E0, size = 0xD0 }, { address = 0x83168940, size = 0x100 }, { address = 0x83168A48, size = 0x11C }, { address = 0x83168B70, size = 0x128 }, { address = 0x83168F18, size = 0x254 }, { address = 0x8316C678, size = 0x78 }, { address = 0x8317CD30, size = 0x50 }, { address = 0x83180700, size = 0x74 }, { address = 0x8319ED58, size = 0x98 }, { address = 0x82455E70, size = 0x84 }, { address = 0x82456DC8, size = 0xD4 }, { address = 0x826ABB70, size = 0x70 }, { address = 0x82893088, size = 0x45C }, { address = 0x82C49540, size = 0x114 }, { address = 0x82DE35D8, size = 0x68 }, { address = 0x82DE3640, size = 0x64 }, { address = 0x82DE36A8, size = 0x5C }, { address = 0x82DE3708, size = 0x198 }, { address = 0x82DE38A0, size = 0x16C }, { address = 0x830B7DD0, size = 0x74 }, { address = 0x831B0BA0, size = 0xA0 }, { address = 0x8305D168, size = 0x278 } ] invalid_instructions = [ { data = 0x00000000, size = 4 }, # Padding { data = 0x831B1C90, size = 8 }, # C++ Frame Handler { data = 0x8324B3BC, size = 8 }, # C Specific Frame Handler { data = 0x831C8B50, size = 8 }, { data = 0x00485645, size = 44 } # End of .text ] ``` -------------------------------- ### Calling a recompiled function with PPCContext Source: https://github.com/hedge-dev/xenonrecomp Includes generated `ppc_config.h` and copied `ppc_context.h`. Shows generated function pattern: each function gets an `__imp__` implementation with prologue and a `PPC_WEAK_FUNC` wrapper. To call from runtime code, create a `PPCContext`, load host FPSCR, provide a 32-byte aligned guest base pointer, then call the wrapper (e.g., `sub_831B0B40`). ```cpp #include "ppc_config.h" // generated #include "ppc_context.h" // copied from XenonUtils // Generated output looks like: PPC_FUNC_IMPL(__imp__sub_831B0B40) { PPC_FUNC_PROLOGUE(); // ... translated instructions ... } PPC_WEAK_FUNC(sub_831B0B40) { __imp__sub_831B0B40(ctx, base); } // Calling a recompiled function from runtime code: PPCContext ctx{}; ctx.fpscr.loadFromHost(); uint8_t* base = ...; // 32-byte aligned guest memory sub_831B0B40(ctx, base); ``` -------------------------------- ### xpointer Source: https://github.com/hedge-dev/xenonrecomp A 32-bit guest pointer that converts between guest virtual addresses and host addresses via the runtime-provided MmGetHostAddress. Provides constructors, get(), and conversion operators for seamless guest-host pointer handling. ```APIDOC ## xpointer ### Description A 32-bit guest pointer that converts between guest virtual addresses and host addresses via the runtime-provided `MmGetHostAddress` (an `extern "C"` symbol the recompilation project must supply; a null `MmGetHostAddress(0)` result serves as the host address of guest address 0). ### Members - `xpointer()` - Null guest pointer. - `xpointer(T* p)` - Converts a host pointer to a guest pointer: `reinterpret_cast(p) - reinterpret_cast(MmGetHostAddress(0))`; null host pointers map to guest `0`. - `get()` - `nullptr` if the guest pointer is 0, else `reinterpret_cast(MmGetHostAddress(ptr))`. - `operator T*` / `operator->` - Implicit conversion / member access through `get()`. ### Example ```cpp XANSI_STRING str{}; // str.Buffer is an xpointer: guest address on disk, host pointer in C++. printf("%s", str.Buffer.get()); ``` ``` -------------------------------- ### xpointer template definition Source: https://github.com/hedge-dev/xenonrecomp Defines a 32-bit guest pointer that converts between guest virtual addresses and host addresses via the runtime-provided MmGetHostAddress. The constructor from a host pointer maps null host pointers to guest 0, and get() returns nullptr for guest pointer 0. ```cpp extern "C" void* MmGetHostAddress(uint32_t ptr); template struct xpointer { be ptr; xpointer(); xpointer(T* p); T* get() const; operator T* () const; T* operator->() const; }; ``` -------------------------------- ### Define explicit function boundaries in TOML Source: https://github.com/hedge-dev/xenonrecomp/blob/main/README.md Defines explicit function boundaries using the `functions` property. Use this when XenonAnalyse fails to analyze functions correctly, for example, with functions containing jump tables. ```toml functions = [ { address = 0x824E7EF0, size = 0x98 }, { address = 0x824E7F28, size = 0x60 }, ] ``` -------------------------------- ### Configure register restore and save function addresses in TOML Source: https://github.com/hedge-dev/xenonrecomp/blob/main/README.md Defines the starting addresses of the Xbox 360 register restore and save functions in the TOML configuration. The recompiler requires these addresses to correctly recompile functions that use non-volatile registers. Each function has a specific byte pattern that can be used to locate it. ```toml restgprlr_14_address = 0x831B0B40 savegprlr_14_address = 0x831B0AF0 restfpr_14_address = 0x831B144C savefpr_14_address = 0x831B1400 restvmx_14_address = 0x831B36E8 savevmx_14_address = 0x831B3450 restvmx_64_address = 0x831B377C savevmx_64_address = 0x831B34E4 ``` -------------------------------- ### XenonRecomp command-line usage Source: https://github.com/hedge-dev/xenonrecomp/blob/main/README.md Command-line usage for XenonRecomp. Provide a TOML file with recompiler configurations and the path to the ppc_context.h file in the XenonUtils directory. ```text XenonRecomp [input TOML file path] [input PPC context header file path] ``` -------------------------------- ### XenonRecomp command-line usage Source: https://github.com/hedge-dev/xenonrecomp/wiki/Overview Run XenonRecomp.exe with an input config .toml file and a PPC context header file to generate C++ code from the x360 executable. ```text XenonRecomp.exe [input config .toml file] [input ppc_context.h file] ``` -------------------------------- ### Recompile PPC tests with XenonRecomp Source: https://github.com/hedge-dev/xenonrecomp/blob/main/README.md Command-line usage for recompiling Xenia's PPC tests. Provide the testing directory, the PPC context header file, and the output directory. After generation, refresh XenonTests' CMake cache to make the generated files appear in the project. ```text XenonRecomp [input testing directory path] [input PPC context header file path] [output directory path] ``` -------------------------------- ### Minimal XenonRecomp config .toml Source: https://github.com/hedge-dev/xenonrecomp/wiki/Overview Bare minimum configuration for XenonRecomp: specifies the input xex file, output directory, jump table file, and required save/restore addresses. You must provide correct addresses for your game. ```toml [main] file_path = "Default_unpacked.xex" # Input xex file out_directory_path = "ppc" # Directory output for generated code switch_table_file_path = "jump_tables.toml" # The jump table we got from XenonAnalyze # rest/save addresses, you will have to provide the correct addresses from your game here restgprlr_14_address = 0x82bafea0 savegprlr_14_address = 0x82bafe50 restfpr_14_address = 0x82bb0b3c savefpr_14_address = 0x82bb0af0 restvmx_14_address = 0x82bb1d58 savevmx_14_address = 0x82bb1ac0 savevmx_64_address = 0x82bb1b54 restvmx_64_address = 0x82bb1dec ``` -------------------------------- ### Main configuration file (TOML) Source: https://github.com/hedge-dev/xenonrecomp Defines paths, addresses, and function/invalid instruction lists for the recompiler. The `functions` array specifies code ranges to translate, and `invalid_instructions` marks data regions to skip. All addresses are required. ```toml [main] file_path = "../private/default.xex" patch_file_path = "../private/default.xexp" patched_file_path = "../private/default_patched.xex" out_directory_path = "../ppc" switch_table_file_path = "SWA_switch_tables.toml" skip_lr = false skip_msr = false ctr_as_local = false xer_as_local = false reserved_as_local = false cr_as_local = false non_argument_as_local = false non_volatile_as_local = false restgprlr_14_address = 0x831B0B40 savegprlr_14_address = 0x831B0AF0 restfpr_14_address = 0x831B144C savefpr_14_address = 0x831B1400 restvmx_14_address = 0x831B36E8 savevmx_14_address = 0x831B3450 restvmx_64_address = 0x831B377C savevmx_64_address = 0x831B34E4 longjmp_address = 0x831B6790 setjmp_address = 0x831B6AB0 functions = [ { address = 0x824E7EF0, size = 0x98 }, { address = 0x824E7F28, size = 0x60 }, ] invalid_instructions = [ { data = 0x00000000, size = 4 }, # Padding { data = 0x831B1C90, size = 8 }, # C++ Frame Handler { data = 0x8324B3BC, size = 8 }, # C Specific Frame Handler { data = 0x831C8B50, size = 8 }, { data = 0x00485645, size = 44 } # End of .text ] ``` -------------------------------- ### template struct be Source: https://github.com/hedge-dev/xenonrecomp A big-endian value box that stores byte-swapped data for guest-endian compatibility. Provides methods to set, get, and manipulate the value in host order. ```APIDOC ## template struct be ### Description A big-endian value box: `value` stores the *byte-swapped* form, so reading the raw struct memory (e.g. from a guest buffer) gives the on-disk big-endian representation while normal C++ access yields host-endian values. ### Members - `be()` - Zero-initialized. - `be(T v)` - Stores `byteswap(v)`. - `static T byteswap(T value)` - Byte-swaps with special handling for `double`, `float`, and enum types (swaps their raw underlying representation) and a generic path otherwise. - `set(T v)` / `get()` - Write/read the boxed value in host order. - `operator T()` - Implicit read (calls `get()`), so a `be` can be used wherever `uint32_t` is expected. - `operator|=` / `operator&=` - Read-modify-write against `get()`, then re-store. - `operator=` - Assigns a host value. ### Example ```cpp #include struct Xex2Header* hdr = (Xex2Header*)fileData; uint32_t moduleFlags = hdr->moduleFlags; // implicit get() -> host order hdr->reserved = 0x1234; // operator= -> stored swapped ``` ``` -------------------------------- ### XDBFWrapper constructor declaration Source: https://github.com/hedge-dev/xenonrecomp Parses the header and locates the entry table, free-space table, and content region. If the buffer is null, too small (<= sizeof(XDBFHeader)), or the header signature is not XDBF_SIGNATURE, the wrapper nulls out its own pBuffer; subsequent lookups are unsafe, so check pBuffer after construction. ```cpp XDBFWrapper(const uint8_t* pBuffer, size_t bufferSize); ``` -------------------------------- ### XenonAnalyze command-line usage Source: https://github.com/hedge-dev/xenonrecomp/wiki/Overview Run XenonAnalyze.exe with an input .xex file and an output .toml file to generate jump table addresses. ```text XenonAnalyze.exe [input .xex file] [output jump_tables.toml file] ``` -------------------------------- ### xpointer struct template Source: https://github.com/hedge-dev/xenonrecomp Guest 32-bit pointer converted through the runtime-provided MmGetHostAddress. ```cpp extern "C" void* MmGetHostAddress(uint32_t ptr); template struct xpointer { be ptr; xpointer(); xpointer(T* p); T* get() const; operator T* () const; T* operator->() const; }; ``` -------------------------------- ### Analyze function with Function::Analyze and search discovered blocks Source: https://github.com/hedge-dev/xenonrecomp Use Function::Analyze to analyze a memory region of a given size and base address, then use SearchBlock to locate a discovered block by address. The example includes necessary includes and comments showing expected results. ```cpp #include #include #include // Analyze a 0x1000-byte region at 0x82E26000. const void* code = image.Find(0x82E26000); Function fn = Function::Analyze(code, 0x1000, 0x82E26000); // fn.base == 0x82E26000, fn.size == the discovered byte length. // Reusing discovered blocks: size_t blockIndex = fn.SearchBlock(0x82E26244); if (blockIndex != (size_t)-1) { const auto& block = fn.blocks[blockIndex]; // block.base + fn.base .. + block.size } ``` -------------------------------- ### GetResource method declaration Source: https://github.com/hedge-dev/xenonrecomp Searches the given namespace for a resource ID and returns a block pointing at pContent + entry.Offset with size entry.Length, or a falsy block (pBuffer == nullptr) when no matching entry exists. ```cpp XDBFBlock GetResource(EXDBFNamespace ns, uint64_t id) const; ``` -------------------------------- ### XenonRecomp main TOML configuration Source: https://github.com/hedge-dev/xenonrecomp/blob/main/README.md Main configuration section for XenonRecomp. All paths are relative to the directory where the TOML file is stored. The patched_file_path is created automatically if missing and reused in subsequent recompilations; it does nothing if no XEXP file is specified. out_directory_path must exist before running the recompiler. ```toml [main] file_path = "../private/default.xex" patch_file_path = "../private/default.xexp" patched_file_path = "../private/default_patched.xex" out_directory_path = "../ppc" switch_table_file_path = "SWA_switch_tables.toml" ``` -------------------------------- ### GetString method declaration Source: https://github.com/hedge-dev/xenonrecomp Looks up the string-table resource (XDBF_SPA_NAMESPACE_STRING_TABLE, language), walks XSTRHeader/XSTREntry pairs, and returns the string bytes for the matching ID. Returns an empty string when the table or ID is missing. ```cpp std::string GetString(EXDBFLanguage language, uint16_t id) const; ``` -------------------------------- ### Usage for XenonRecomp Source: https://github.com/hedge-dev/xenonrecomp Shows two invocation forms. The first runs recompiler on a TOML file; the second runs test mode on a directory. Placeholder arguments are replaced with file and directory paths. ```text XenonRecomp [input TOML file path] [PPC context header file path] XenonRecomp [input testing directory path] [PPC context header file path] [output directory path] ``` -------------------------------- ### LoadFile Source: https://github.com/hedge-dev/xenonrecomp Loads an entire binary file into memory as a vector of bytes. Returns an empty vector if the file cannot be opened or a read error occurs. ```APIDOC ## LoadFile ### Description Loads the entire contents of a binary file into a `std::vector`. Returns an empty vector if the file cannot be opened or a read error occurs. ### Method Function ### Signature ```cpp inline std::vector LoadFile(const std::filesystem::path& path) ``` ### Parameters - `path` (const std::filesystem::path&) - Required - Path to the binary file to read ### Returns - `std::vector` - The entire file contents, or an empty vector on failure. ### Request Example ```cpp #include const auto data = LoadFile("default.xex"); if (data.empty()) { // Unreadable file. return; } Image image = Image::ParseImage(data.data(), data.size()); ``` ``` -------------------------------- ### Switch table file (TOML) Source: https://github.com/hedge-dev/xenonrecomp Parsed from a separate TOML file (XenonAnalyse output). Each `[[switch]]` table defines a jump table: `base` is the `bctr` instruction address, `r` is the register holding the case index, and `labels` are case target addresses. The `default` key is ignored, and all three keys are required. ```toml [[switch]] base = 0x82E26200 r = 3 labels = [ 0x82E26220, 0x82E26228, ] ``` -------------------------------- ### Main function signature for XenonRecomp Source: https://github.com/hedge-dev/xenonrecomp Entry point for the XenonRecomp executable. Takes standard command-line arguments. ```cpp int main(int argc, char* argv[]) ``` -------------------------------- ### DisassemblerEngine constructor Source: https://github.com/hedge-dev/xenonrecomp Initializes a PowerPC disassembler engine with the specified byte order and disassembler options. Sets up the internal disassemble_info structure for PowerPC architecture. ```APIDOC ## DisassemblerEngine constructor ### Description Initializes a PowerPC disassembler engine with the specified byte order and disassembler options. Sets up the internal disassemble_info structure for PowerPC architecture. ### Method Constructor ### Signature ```cpp DisassemblerEngine(bfd_endian endian, const char* options); ``` ### Parameters - **endian** (bfd_endian) - Required - Target byte order; the codebase uses `BFD_ENDIAN_BIG` - **options** (const char*) - Required - Disassembler options string passed as `info.disassembler_options`; the codebase uses `"cell 64"` (Cell PPC64 syntax) ``` -------------------------------- ### Using be with Xex2Header Source: https://github.com/hedge-dev/xenonrecomp Shows how to read and write big-endian fields from a guest buffer using implicit conversion and assignment operators. ```cpp #include struct Xex2Header* hdr = (Xex2Header*)fileData; uint32_t moduleFlags = hdr->moduleFlags; // implicit get() -> host order hdr->reserved = 0x1234; // operator= -> stored swapped ``` -------------------------------- ### Define XenonUtils library target Source: https://github.com/hedge-dev/xenonrecomp/blob/main/XenonUtils/CMakeLists.txt Creates the XenonUtils library with the specified source files, including third-party sources. Sets NOMINMAX as a private compile definition. Exposes the current directory and simde as public include directories, while keeping third-party headers private. Links the disasm library publicly. ```cmake project("XenonUtils") add_library(XenonUtils "disasm.cpp" "xex.cpp" "image.cpp" "xdbf_wrapper.cpp" "xex_patcher.cpp" "memory_mapped_file.cpp" "${THIRDPARTY_ROOT}/libmspack/libmspack/mspack/lzxd.c" "${THIRDPARTY_ROOT}/tiny-AES-c/aes.c" ) target_compile_definitions(XenonUtils PRIVATE NOMINMAX ) target_include_directories(XenonUtils PUBLIC . "${THIRDPARTY_ROOT}/simde" PRIVATE "${THIRDPARTY_ROOT}/libmspack/libmspack/mspack" "${THIRDPARTY_ROOT}/tiny-AES-c" "${THIRDPARTY_ROOT}/TinySHA1" ) target_link_libraries(XenonUtils PUBLIC disasm ) ``` -------------------------------- ### XenonAnalyse main function declaration Source: https://github.com/hedge-dev/xenonrecomp Entry point for the XenonAnalyse tool. Called with command-line arguments: input XEX file path and output TOML file path. ```cpp int main(int argc, char** argv) ``` -------------------------------- ### Load RecompilerConfig and inspect fields Source: https://github.com/hedge-dev/xenonrecomp Shows how to load a TOML configuration file via RecompilerConfig::Load. After loading, check ctrAsLocalVariable for the TOML key 'ctr_as_local', iterate explicitly declared functions, and look up mid-asm hooks by address. Demonstrates accessing hook name and registers. ```cpp #include RecompilerConfig config; config.Load("SWA.toml"); // TOML: [main] ctr_as_local = true -> field ctrAsLocalVariable == true if (config.ctrAsLocalVariable) { // Recompiler will emit PPC_CONFIG_CTR_AS_LOCAL and use a local variable for ctr. } for (auto& [address, size] : config.functions) { // address .. address+size is an explicitly declared function. } auto hookIt = config.midAsmHooks.find(0x82E26244); if (hookIt != config.midAsmHooks.end()) { // hookIt->second.name == "IndexBufferLengthMidAsmHook" // hookIt->second.registers == {"r3"} } ``` -------------------------------- ### PPCContext struct definition (ppc_context.h) Source: https://github.com/hedge-dev/xenonrecomp Defines the guest CPU state aligned to 0x40. The displayed layout is condensed with '...' for omitted fields. Register presence is controlled by PPC_CONFIG_* macros. ```cpp struct alignas(0x40) PPCContext { PPCRegister r3; #ifndef PPC_CONFIG_NON_ARGUMENT_AS_LOCAL PPCRegister r0; #endif PPCRegister r1; ... }; ``` -------------------------------- ### PPC Memory Access Macros (Load/Store) from ppc_context.h Source: https://github.com/hedge-dev/xenonrecomp Macros for byte-swapped guest memory access. Loads use __builtin_bswap* to convert big-endian to host little-endian; stores reverse. All accesses are volatile to prevent Clang reordering. Each macro has an MMIO variant (PPC_MM_*) that currently aliases the plain ones; MMIO support is unimplemented. All are guarded by #ifndef so a runtime can override them. ```cpp #define PPC_LOAD_U8(x) *(volatile uint8_t*)(base + (x)) ``` ```cpp #define PPC_LOAD_U16(x) __builtin_bswap16(*(volatile uint16_t*)(base + (x))) ``` ```cpp #define PPC_LOAD_U32(x) __builtin_bswap32(*(volatile uint32_t*)(base + (x))) ``` ```cpp #define PPC_LOAD_U64(x) __builtin_bswap64(*(volatile uint64_t*)(base + (x))) ``` ```cpp #define PPC_STORE_U8(x, y) *(volatile uint8_t*)(base + (x)) = (y) ``` ```cpp #define PPC_STORE_U16(x, y) *(volatile uint16_t*)(base + (x)) = __builtin_bswap16(y) ``` ```cpp #define PPC_STORE_U32(x, y) *(volatile uint32_t*)(base + (x)) = __builtin_bswap32(y) ``` ```cpp #define PPC_STORE_U64(x, y) *(volatile uint64_t*)(base + (x)) = __builtin_bswap64(y) ``` -------------------------------- ### XenonAnalyse command-line usage Source: https://github.com/hedge-dev/xenonrecomp/blob/main/README.md Command-line usage for XenonAnalyse. Pass an XEX file as input and specify an output path for the TOML file containing detected jump tables. ```text XenonAnalyse [input XEX file path] [output jump table TOML file path] ``` -------------------------------- ### XenonAnalyze jump table patterns (C++) Source: https://github.com/hedge-dev/xenonrecomp/wiki/Overview Defines three PPC instruction patterns for detecting jump tables: absoluteSwitch, computedSwitch, and offsetSwitch. These patterns are from Sonic: Unleashed and may need adjustment for other games, along with the ReadTable function, if jump tables are empty or have incorrect offsets. ```cpp uint32_t absoluteSwitch[] = { PPC_INST_LIS, PPC_INST_ADDI, PPC_INST_RLWINM, PPC_INST_LWZX, PPC_INST_MTCTR, PPC_INST_BCTR, }; uint32_t computedSwitch[] = { PPC_INST_LIS, PPC_INST_ADDI, PPC_INST_LBZX, PPC_INST_RLWINM, PPC_INST_LIS, PPC_INST_ADDI, PPC_INST_ADD, PPC_INST_MTCTR, }; uint32_t offsetSwitch[] = { PPC_INST_LIS, PPC_INST_ADDI, PPC_INST_LBZX, PPC_INST_LIS, PPC_INST_ADDI, PPC_INST_ADD, PPC_INST_MTCTR, }; ``` -------------------------------- ### XDBFWrapper::XDBFWrapper Source: https://github.com/hedge-dev/xenonrecomp Constructs an XDBFWrapper from a caller-owned buffer containing XDBF file contents. Parses the header and locates the entry table, free-space table, and content region; if the buffer is invalid, the wrapper's pBuffer is nulled. ```APIDOC ## Constructor ### Description Constructs an XDBFWrapper from a caller-owned buffer containing XDBF file contents. Parses the header and locates the entry table, free-space table, and content region; if the buffer is invalid, the wrapper's pBuffer is nulled. ### Signature ```cpp XDBFWrapper(const uint8_t* pBuffer, size_t bufferSize); ``` ### Parameters - **pBuffer** (const uint8_t*) - Required - XDBF file contents - **bufferSize** (size_t) - Required - Buffer length ### Notes - The wrapper does not own or copy the data; the caller must keep pBuffer alive. - If the buffer is null, too small (<= sizeof(XDBFHeader)), or the header signature is not XDBF_SIGNATURE, the wrapper nulls out its own pBuffer. Check pBuffer after construction. ``` -------------------------------- ### MemoryMappedFile::open Source: https://github.com/hedge-dev/xenonrecomp Opens a file and maps it into memory for read-only access. Returns true on success, false on failure. ```APIDOC ## MemoryMappedFile::open ### Description Opens a file and maps it into memory for read-only access. Returns true on success, false on failure. ### Method open ### Signature ```cpp bool open(const std::filesystem::path &path); ``` ### Parameters - **path** (const std::filesystem::path&) - Required - Path to the file to open. ### Returns - **bool** - `true` if the file was opened and mapped successfully, `false` otherwise. ``` -------------------------------- ### MemoryMappedFile::open signature Source: https://github.com/hedge-dev/xenonrecomp Opens and maps the given file. Returns true on success; prints the OS error to stderr on failure. ```cpp bool open(const std::filesystem::path &path); ``` -------------------------------- ### IMAGE_CE_RUNTIME_FUNCTION struct Source: https://github.com/hedge-dev/xenonrecomp .pdata record, consumed by Recompiler::Analyse (8-byte static assert). ```cpp typedef struct _IMAGE_CE_RUNTIME_FUNCTION { uint32_t BeginAddress; union { uint32_t Data; struct { uint32_t PrologLength : 8; uint32_t FunctionLength : 22; uint32_t ThirtyTwoBit : 1; uint32_t ExceptionFlag : 1; }; }; } IMAGE_CE_RUNTIME_FUNCTION; ``` -------------------------------- ### Image::Find(string_view) Source: https://github.com/hedge-dev/xenonrecomp Finds a section by name and returns a pointer to the Section, or nullptr if not found. ```APIDOC ## Image::Find(string_view) ### Description Finds a section by name and returns a pointer to the Section, or nullptr if not found. ### Method Find ### Parameters - **name** (string_view) - Required - Section name ### Response - **const Section*** - Pointer to the Section, or nullptr if not found. ``` -------------------------------- ### XDBFWrapper::GetString Source: https://github.com/hedge-dev/xenonrecomp Retrieves a string from the XDBF file by language and ID. ```APIDOC ## XDBFWrapper::GetString ### Description Retrieves a string from the XDBF file by language and ID. ### Method Member function ### Signature ```cpp std::string GetString(EXDBFLanguage language, uint16_t id) const; ``` ### Parameters - `language` (EXDBFLanguage) - Required - Language of the string. - `id` (uint16_t) - Required - String ID. ### Return Value - `std::string` - The requested string. ``` -------------------------------- ### MemoryMappedFile::data and size signatures Source: https://github.com/hedge-dev/xenonrecomp data() returns a pointer to the mapped bytes (nullptr when closed); size() returns the file size in bytes (0 when closed or on Windows after a default move). ```cpp uint8_t *data() const; size_t size() const; ``` -------------------------------- ### Configure longjmp and setjmp addresses in TOML Source: https://github.com/hedge-dev/xenonrecomp/blob/main/README.md Specifies the addresses of the longjmp and setjmp functions in the executable. The recompiler directly redirects these functions to native versions. If the game does not use these functions, you can remove the properties from the TOML file. ```toml longjmp_address = 0x831B6790 setjmp_address = 0x831B6AB0 ``` -------------------------------- ### LoadFile Source: https://github.com/hedge-dev/xenonrecomp Loads a file from the filesystem and returns its contents as a byte vector. This function is part of the XenonUtils library. ```APIDOC ## LoadFile ### Description Loads a file from the filesystem and returns its contents as a byte vector. ### Function Signature `std::vector LoadFile(const std::filesystem::path& path)` ### Parameters - **path** (std::filesystem::path) - Required - The path to the file to load. ### Returns - **std::vector** - The contents of the file as a byte vector. ### Header `XenonUtils/file.h` ``` -------------------------------- ### Apply XEX patch with memory overload Source: https://github.com/hedge-dev/xenonrecomp Loads base and patch files, applies the patch in memory, and checks the result. The final argument 'false' performs the full patch; 'true' is a compatibility probe that skips heavy work. ```cpp #include #include const auto base = LoadFile("default.xex"); const auto patch = LoadFile("patch_1.xexp"); std::vector patched; XexPatcher::Result result = XexPatcher::apply(base.data(), base.size(), patch.data(), patch.size(), patched, false); if (result == XexPatcher::Result::Success) { // patched now holds a decrypted, decompressed, delta-applied XEX. } else { // See errors.md for per-value conditions. } // Compatibility probe without doing the heavy work: XexPatcher::Result probe = XexPatcher::apply(base.data(), base.size(), patch.data(), patch.size(), patched, true); ``` -------------------------------- ### ppc::Disassemble Source: https://github.com/hedge-dev/xenonrecomp Disassembles PowerPC instructions. This function is part of the XenonUtils library. ```APIDOC ## ppc::Disassemble ### Description Disassembles PowerPC instructions. ### Function Signature `ppc::Disassemble(...)` ### Parameters - **...** - Required - Parameters for disassembly. ### Returns - **void** - No return value. ### Header `XenonUtils/disasm.h` ``` -------------------------------- ### XenonAnalyse usage line Source: https://github.com/hedge-dev/xenonrecomp Printed when fewer than 3 arguments are supplied; the program then returns EXIT_SUCCESS. ```text XenonAnalyse [input XEX file path] [output jump table TOML file path] ``` -------------------------------- ### RecompilerConfig::Load method declaration Source: https://github.com/hedge-dev/xenonrecomp Declares the Load method for RecompilerConfig, taking a path to the recompiler TOML file. This method populates the configuration fields and prints validation errors to stdout. ```cpp void Load(const std::string_view& configFilePath); ``` -------------------------------- ### XexPatcher::apply (path overload) Source: https://github.com/hedge-dev/xenonrecomp Applies a patch to a base XEX file specified by file paths. Opens the input files, runs the memory overload, and writes the patched output to a new file. ```APIDOC ## XexPatcher::apply (path overload) ### Description Applies a patch to a base XEX file specified by file paths. Opens the input files, runs the memory overload, and writes the patched output to a new file. ### Method static Result apply(const std::filesystem::path &baseXexPath, const std::filesystem::path &patchXexPath, const std::filesystem::path &newXexPath) ### Parameters - `baseXexPath` (const std::filesystem::path&) - Required - Base XEX file path - `patchXexPath` (const std::filesystem::path&) - Required - Patch (XEXP) file path - `newXexPath` (const std::filesystem::path&) - Required - Output path of the patched XEX ### Response #### Success Response - `Result` - Returns the result of the apply operation. ### Behavior Opens both inputs with `MemoryMappedFile` (`FileOpenFailed` on open failure), runs the memory overload with `skipData = false`, writes the result to `newXexPath` (`FileOpenFailed` if the output stream can't open, `FileWriteFailed` if the write fails — in which case the partial output file is deleted), then returns the apply result. ``` -------------------------------- ### MemoryMappedFile::isOpen signature Source: https://github.com/hedge-dev/xenonrecomp Returns true when a file view is currently mapped. ```cpp bool isOpen() const; ``` -------------------------------- ### MemoryMappedFile::isOpen Source: https://github.com/hedge-dev/xenonrecomp Checks whether the file mapping is currently open. ```APIDOC ## MemoryMappedFile::isOpen ### Description Checks whether the file mapping is currently open. ### Method isOpen ### Signature ```cpp bool isOpen() const; ``` ### Returns - **bool** - `true` if the file is open, `false` otherwise. ``` -------------------------------- ### PPC function hooking pattern Source: https://github.com/hedge-dev/xenonrecomp/blob/main/README.md Defines a PPC function wrapper that aliases the original function to an implementation function and marks it weakly linked, allowing users to override it with a custom implementation while retaining access to the original. ```cpp PPC_FUNC_IMPL(__imp__sub_XXXXXXXX); PPC_FUNC(sub_XXXXXXXX) { __imp__sub_XXXXXXXX(ctx, base); } ```