From 7deb2ff29393a0663a144b906ca6554dd5289b34 Mon Sep 17 00:00:00 2001 From: Drew C Date: Fri, 13 Mar 2026 11:44:25 -0500 Subject: [PATCH 1/3] Support in StarWarsG for display text --- functions/game_object_type.cpp | 8 +++----- lua_hook.cpp | 31 +++++++++++++++++++++++++++---- lua_hook.h | 7 +++++-- rvas.h | 22 ++++++++++++++-------- 4 files changed, 49 insertions(+), 19 deletions(-) diff --git a/functions/game_object_type.cpp b/functions/game_object_type.cpp index 5c91e48..ae80ea7 100644 --- a/functions/game_object_type.cpp +++ b/functions/game_object_type.cpp @@ -54,12 +54,10 @@ static void* Lua_Get_Display_Name(void* this_wrapper, void* /*script*/, void* /* std::string narrow(narrow_len, '\0'); WideCharToMultiByte(CP_UTF8, 0, wdata, wlen, &narrow[0], narrow_len, nullptr, nullptr); - // 5. Create a LuaString (LuaValue, 0x78 bytes) and return it. - void* lua_value = ::operator new(0x78); + // 5. Create a LuaString (LuaValue) and return it. + void* lua_value = CreateLuaValueString(narrow); if (!lua_value) return nullptr; - memset(lua_value, 0, 0x78); - pfn_lua_value_ctor(lua_value, &narrow); return pfn_return_variable(this_wrapper, lua_value); } @@ -70,7 +68,7 @@ void Register_GOTMembers(void* wrapper) return; if (!pfn_get_text_name_id || !pfn_game_text_get || !g_the_game_text) return; - if (!pfn_lua_value_ctor || !pfn_return_variable) + if (!pfn_return_variable) return; // Allocate a LuaMemberFunctionWrapper (0x88 bytes). diff --git a/lua_hook.cpp b/lua_hook.cpp index 0dfefe8..a2951c0 100644 --- a/lua_hook.cpp +++ b/lua_hook.cpp @@ -2,6 +2,12 @@ #include "functions/functions.h" #include #include +#include +#include + +// Cached during init for CreateLuaValueString +static const LuaRVAs* g_rvas = nullptr; +static uintptr_t g_base = 0; // Resolved function pointers fn_lua_open real_lua_open = nullptr; @@ -22,7 +28,6 @@ fn_RemoveTutorialText pfn_remove_tutorial_text = nullptr; void* g_command_bar = nullptr; fn_DebugPrint pfn_debug_print = nullptr; fn_RegisterMember pfn_register_member = nullptr; -fn_LuaValueCtor pfn_lua_value_ctor = nullptr; fn_ReturnVariable pfn_return_variable = nullptr; fn_GotWrapperCtor real_got_wrapper_ctor = nullptr; fn_LmfwCtor pfn_lmfw_ctor = nullptr; @@ -36,6 +41,24 @@ void register_global(lua_State* L, const char* name, lua_CFunction fn) { pfn_settable(L, LUA_GLOBALSINDEX); } +void* CreateLuaValueString(const std::string& str) { + if (!g_rvas || !g_rvas->lua_value_string_vftable) + return nullptr; + + void* obj = ::operator new(g_rvas->lua_value_string_size); + if (!obj) + return nullptr; + memset(obj, 0, g_rvas->lua_value_string_size); + + // Set the vftable pointer + *(uintptr_t*)obj = g_base + g_rvas->lua_value_string_vftable; + + // Placement-new the string into the member offset + new ((char*)obj + g_rvas->lua_value_string_offset) std::string(str); + + return obj; +} + // Fired every time the game constructs a GameObjectTypeWrapper static void* Hook_GOTWrapperCtor(void* this_wrapper) { void* result = real_got_wrapper_ctor(this_wrapper); @@ -73,6 +96,8 @@ bool LuaHook_Init() { return false; uintptr_t base = (uintptr_t)GetModuleHandleA(nullptr); + g_rvas = rvas; + g_base = base; pfn_pushstring = (fn_lua_pushstring)(base + rvas->lua_pushstring); pfn_pushcclosure = (fn_lua_pushcclosure)(base + rvas->lua_pushcclosure); @@ -101,8 +126,6 @@ bool LuaHook_Init() { if (rvas->register_member) pfn_register_member = (fn_RegisterMember)(base + rvas->register_member); - if (rvas->lua_value_ctor) - pfn_lua_value_ctor = (fn_LuaValueCtor)(base + rvas->lua_value_ctor); if (rvas->return_variable) pfn_return_variable = (fn_ReturnVariable)(base + rvas->return_variable); @@ -138,7 +161,7 @@ bool LuaHook_Init() { if (rvas->got_wrapper_ctor && rvas->got_lmfw_ctor && pfn_register_member && pfn_lmfw_ctor && pfn_get_text_name_id && pfn_game_text_get && g_the_game_text && - pfn_lua_value_ctor && pfn_return_variable) + rvas->lua_value_string_vftable && pfn_return_variable) { void* got_ctor_target = (void*)(base + rvas->got_wrapper_ctor); if (MH_CreateHook(got_ctor_target, &Hook_GOTWrapperCtor, (void**)&real_got_wrapper_ctor) == MH_OK) { diff --git a/lua_hook.h b/lua_hook.h index 5bfe20a..18d8070 100644 --- a/lua_hook.h +++ b/lua_hook.h @@ -1,5 +1,6 @@ #pragma once #include "rvas.h" +#include struct lua_State; typedef int (*lua_CFunction)(lua_State*); @@ -22,7 +23,6 @@ typedef void (*fn_RemoveTutorialText)(void*, const char*); typedef void (*fn_DebugPrint)(const char*); // Debug_Print // Scripting internals (LuaUserVar) -- usable by any wrapper module typedef void (*fn_RegisterMember)(void* uservar, const char* name, void* fn_ptr); // LuaUserVar::Register_Member -typedef void (*fn_LuaValueCtor)(void* lua_value, void* basic_string_ptr); // LuaValue<>::LuaValue<>(basic_string<>*) typedef void* (*fn_ReturnVariable)(void* uservar, void* lua_var); // LuaUserVar::Return_Variable(LuaVar*) -> LuaTable* // Game Object Type Wrapper internals typedef void* (*fn_GotWrapperCtor)(void* this_wrapper); // GameObjectTypeWrapper::GameObjectTypeWrapper() @@ -49,7 +49,6 @@ extern fn_RemoveTutorialText pfn_remove_tutorial_text; extern void* g_command_bar; extern fn_DebugPrint pfn_debug_print; extern fn_RegisterMember pfn_register_member; -extern fn_LuaValueCtor pfn_lua_value_ctor; extern fn_ReturnVariable pfn_return_variable; extern fn_GotWrapperCtor real_got_wrapper_ctor; extern fn_LmfwCtor pfn_lmfw_ctor; @@ -62,5 +61,9 @@ constexpr int LUA_GLOBALSINDEX = -10001; // Equivalent of lua_setglobal — registers a C function in the Lua global table void register_global(lua_State* L, const char* name, lua_CFunction fn); +// Creates a LuaValue> from a std::string. +// Reproduces the inlined constructor pattern using per-executable layout constants. +void* CreateLuaValueString(const std::string& str); + bool LuaHook_Init(); void LuaHook_Shutdown(); diff --git a/rvas.h b/rvas.h index 9fade65..60faef5 100644 --- a/rvas.h +++ b/rvas.h @@ -16,7 +16,9 @@ struct LuaRVAs { uintptr_t get_script_from_state; // LuaScriptClass::Get_Script_From_State (static) uintptr_t map_var_to_lua; // LuaScriptClass::Map_Var_To_Lua (member) uintptr_t register_member; // LuaUserVar::Register_Member(char*, LuaVar*) - uintptr_t lua_value_ctor; // LuaValue<>::LuaValue<>(basic_string<>*) + uintptr_t lua_value_string_vftable; // LuaValue>::vftable + size_t lua_value_string_size; // allocation size of LuaValue> + size_t lua_value_string_offset; // offset of basic_string member within LuaValue uintptr_t return_variable; // LuaUserVar::Return_Variable(LuaVar*) // --- TheGameText --- @@ -58,7 +60,9 @@ constexpr LuaRVAs RVAs_StarWarsI = { 0x0537BB0, // LuaScriptClass::Get_Script_From_State 0x0536FE0, // LuaScriptClass::Map_Var_To_Lua 0x0046f51, // LuaUserVar::Register_Member - 0x004df5e, // LuaValue<>::LuaValue<> + 0x1453310, // LuaValue>::vftable + 0x78, // LuaValue> allocation size + 0x58, // basic_string member offset within LuaValue 0x0058887, // LuaUserVar::Return_Variable // --- TheGameText --- @@ -99,13 +103,15 @@ constexpr LuaRVAs RVAs_StarWarsG = { // --- Scripts --- 0x0245790, // LuaScriptClass::Get_Script_From_State 0x0247700, // LuaScriptClass::Map_Var_To_Lua - 0, // LuaUserVar::Register_Member -- not mapped - 0, // LuaString::LuaString -- not mapped - 0, // LuaUserVar::Return_Variable -- not mapped + 0x024be40, // LuaUserVar::Register_Member + 0x0855a98, // LuaValue>::vftable + 0x30, // LuaValue> allocation size + 0x10, // basic_string member offset within LuaValue + 0x0256d40, // LuaUserVar::Return_Variable // --- TheGameText --- - 0, // TheGameText global object - 0, // GameTextClass::Get + 0x0a7bc58, // TheGameText global object + 0x01fa680, // GameTextClass::Get // --- Players --- 0x0294bc0, // PlayerListClass::Get_Player_By_Index @@ -118,7 +124,7 @@ constexpr LuaRVAs RVAs_StarWarsG = { 0x02ff290, // CommandBarClass::Remove_Tutorial_Text // --- Game Object Type Wrapper --- - 0, // GameObjectTypeWrapper::GameObjectTypeWrapper() -- not mapped + 0x0604a10, // GameObjectTypeWrapper::GameObjectTypeWrapper() 0, // GameObjectTypeClass::Get_Text_Name_ID() -- not mapped 0, // LuaMemberFunctionWrapper::ctor -- not mapped -- 2.39.5 From 67adfd1e082dfbe0bbb2e542270545482295d454 Mon Sep 17 00:00:00 2001 From: Drew C Date: Sat, 14 Mar 2026 13:04:22 -0500 Subject: [PATCH 2/3] Use inline ctors for GameObjectTypeWrapper, LuaMemberFunctionWrapper --- functions/game_object_type.cpp | 50 ++++++++++++++++++++++++---------- lua_hook.cpp | 24 ++++++++-------- lua_hook.h | 10 ++++--- rvas.h | 28 +++++++++++++++---- 4 files changed, 76 insertions(+), 36 deletions(-) diff --git a/functions/game_object_type.cpp b/functions/game_object_type.cpp index ae80ea7..353e34b 100644 --- a/functions/game_object_type.cpp +++ b/functions/game_object_type.cpp @@ -8,6 +8,19 @@ // SmartPtr stores the raw pointer as its first member. static constexpr int OFFSET_OBJECT = 0x70; +// MSVC std::string layout (x64, MSVC 14.0+). +// Used to read the std::string at a known field offset within GameObjectTypeClass. +struct MSVCString { + union { + char buf[16]; // SSO buffer (up to 15 chars + null) + char* ptr; // heap pointer when capacity > 15 + }; + size_t size; + size_t capacity; // <= 15 means SSO, > 15 means heap + + const char* c_str() const { return (capacity <= 15) ? buf : ptr; } +}; + // MSVC std::wstring layout (x64, MSVC 14.0+). // Used to read the wstring* returned by GameTextClass::Get. struct MSVCWString { @@ -31,11 +44,10 @@ static void* Lua_Get_Display_Name(void* this_wrapper, void* /*script*/, void* /* if (!got_class) return nullptr; - // 2. Get the text ID string (e.g. "TEXT_UNIT_X_WING") via Get_Text_Name_ID. - // The game's function constructs a std::string into our buffer via copy-ctor. - // Assumes shared CRT (both DLL and game link to vcruntime140.dll dynamically). - std::string text_id; - pfn_get_text_name_id(got_class, &text_id); + // 2. Read the TextNameID string directly from the GameObjectTypeClass field. + // Get_Text_Name_ID() is always inlined by the compiler, so we use the field offset. + MSVCString* text_name = (MSVCString*)((char*)got_class + g_text_name_id_offset); + std::string text_id(text_name->c_str(), text_name->size); // 3. Resolve the localized display name via TheGameText.Get(text_id, false). // Returns a pointer to an internal wstring (the translated text). @@ -64,26 +76,36 @@ static void* Lua_Get_Display_Name(void* this_wrapper, void* /*script*/, void* /* void Register_GOTMembers(void* wrapper) { - if (!pfn_lmfw_ctor || !pfn_register_member) + if (!pfn_lua_uservar_ctor || !pfn_register_member) return; - if (!pfn_get_text_name_id || !pfn_game_text_get || !g_the_game_text) + if (!g_text_name_id_offset || !pfn_game_text_get || !g_the_game_text) return; - if (!pfn_return_variable) + if (!pfn_return_variable || !g_rvas || !g_rvas->lmfw_vftable) return; - // Allocate a LuaMemberFunctionWrapper (0x88 bytes). - void* lmfw = ::operator new(0x88); + const LuaRVAs* rvas = g_rvas; + + // Allocate a LuaMemberFunctionWrapper. + void* lmfw = ::operator new(rvas->lmfw_alloc_size); if (!lmfw) return; - memset(lmfw, 0, 0x88); + memset(lmfw, 0, rvas->lmfw_alloc_size); - // Build the 16-byte pointer-to-member-function structure. + // Initialize the LuaUserVar base class. + pfn_lua_uservar_ctor(lmfw, 0, false); + + // Set the LuaMemberFunctionWrapper<> vftable. + *(uintptr_t*)lmfw = g_base + rvas->lmfw_vftable; + + // Write the 16-byte pointer-to-member-function (PMF) into the MemberFunction field. // MSVC x64 multiple-inheritance PMF: [8-byte func addr][8-byte this-adjustment]. // this-adjustment is 0 (our function expects the full GameObjectTypeWrapper*). alignas(8) uint8_t pmf[16] = {}; *(uintptr_t*)pmf = (uintptr_t)&Lua_Get_Display_Name; + memcpy((char*)lmfw + rvas->lmfw_pmf_offset, pmf, 16); - // Construct the LuaMemberFunctionWrapper via the game's ctor. - pfn_lmfw_ctor(lmfw, wrapper, pmf, false); + // Set the Object (wrapper pointer) and UseMaps flag. + *(void**)((char*)lmfw + rvas->lmfw_object_offset) = wrapper; + *(bool*)((char*)lmfw + rvas->lmfw_usemaps_offset) = false; // Register as a member callable via wrapper.Get_Display_Name() pfn_register_member(wrapper, "Get_Display_Name", lmfw); diff --git a/lua_hook.cpp b/lua_hook.cpp index a2951c0..515a6f0 100644 --- a/lua_hook.cpp +++ b/lua_hook.cpp @@ -5,9 +5,9 @@ #include #include -// Cached during init for CreateLuaValueString -static const LuaRVAs* g_rvas = nullptr; -static uintptr_t g_base = 0; +// Cached during init — used by CreateLuaValueString and Register_GOTMembers +const LuaRVAs* g_rvas = nullptr; +uintptr_t g_base = 0; // Resolved function pointers fn_lua_open real_lua_open = nullptr; @@ -30,8 +30,8 @@ fn_DebugPrint pfn_debug_print = nullptr; fn_RegisterMember pfn_register_member = nullptr; fn_ReturnVariable pfn_return_variable = nullptr; fn_GotWrapperCtor real_got_wrapper_ctor = nullptr; -fn_LmfwCtor pfn_lmfw_ctor = nullptr; -fn_GetTextNameId pfn_get_text_name_id = nullptr; +fn_LuaUserVarCtor pfn_lua_uservar_ctor = nullptr; +size_t g_text_name_id_offset = 0; fn_GameTextGet pfn_game_text_get = nullptr; void* g_the_game_text = nullptr; @@ -133,10 +133,10 @@ bool LuaHook_Init() { g_the_game_text = (void*)(base + rvas->the_game_text); if (rvas->get_string) pfn_game_text_get = (fn_GameTextGet)(base + rvas->get_string); - if (rvas->got_get_text_name_id) - pfn_get_text_name_id = (fn_GetTextNameId)(base + rvas->got_get_text_name_id); - if (rvas->got_lmfw_ctor) - pfn_lmfw_ctor = (fn_LmfwCtor)(base + rvas->got_lmfw_ctor); + if (rvas->got_text_name_id_offset) + g_text_name_id_offset = rvas->got_text_name_id_offset; + if (rvas->lua_uservar_ctor) + pfn_lua_uservar_ctor = (fn_LuaUserVarCtor)(base + rvas->lua_uservar_ctor); if (rvas->lua_newtable && rvas->lua_rawseti) { pfn_newtable = (fn_lua_newtable)(base + rvas->lua_newtable); @@ -158,9 +158,9 @@ bool LuaHook_Init() { } // Hook GameObjectTypeWrapper constructor to register custom member functions - if (rvas->got_wrapper_ctor && rvas->got_lmfw_ctor && - pfn_register_member && pfn_lmfw_ctor && - pfn_get_text_name_id && pfn_game_text_get && g_the_game_text && + if (rvas->got_wrapper_ctor && rvas->lmfw_vftable && pfn_lua_uservar_ctor && + pfn_register_member && + g_text_name_id_offset && pfn_game_text_get && g_the_game_text && rvas->lua_value_string_vftable && pfn_return_variable) { void* got_ctor_target = (void*)(base + rvas->got_wrapper_ctor); diff --git a/lua_hook.h b/lua_hook.h index 18d8070..6515dd1 100644 --- a/lua_hook.h +++ b/lua_hook.h @@ -26,8 +26,8 @@ typedef void (*fn_RegisterMember)(void* uservar, const char* name, void* typedef void* (*fn_ReturnVariable)(void* uservar, void* lua_var); // LuaUserVar::Return_Variable(LuaVar*) -> LuaTable* // Game Object Type Wrapper internals typedef void* (*fn_GotWrapperCtor)(void* this_wrapper); // GameObjectTypeWrapper::GameObjectTypeWrapper() -typedef void* (*fn_LmfwCtor)(void* lmfw, void* wrapper, void* pmf, bool use_maps); // LuaMemberFunctionWrapper::ctor -typedef void* (*fn_GetTextNameId)(void* got_class, void* out_string); // GameObjectTypeClass::Get_Text_Name_ID() -- returns via hidden out-param +typedef void* (*fn_LuaUserVarCtor)(void* this_ptr, int param, bool flag); // LuaUserVar::LuaUserVar(int, bool) +// GameObjectTypeClass::TextNameID is accessed via field offset (always inlined by compiler) typedef void* (*fn_GameTextGet)(void* game_text, void* text_id_str, bool insert); // GameTextClass::Get(basic_string*, bool) -- returns wstring* // Resolved function pointers (defined in lua_hook.cpp) @@ -51,10 +51,12 @@ extern fn_DebugPrint pfn_debug_print; extern fn_RegisterMember pfn_register_member; extern fn_ReturnVariable pfn_return_variable; extern fn_GotWrapperCtor real_got_wrapper_ctor; -extern fn_LmfwCtor pfn_lmfw_ctor; -extern fn_GetTextNameId pfn_get_text_name_id; +extern fn_LuaUserVarCtor pfn_lua_uservar_ctor; +extern size_t g_text_name_id_offset; extern fn_GameTextGet pfn_game_text_get; extern void* g_the_game_text; +extern const LuaRVAs* g_rvas; +extern uintptr_t g_base; constexpr int LUA_GLOBALSINDEX = -10001; diff --git a/rvas.h b/rvas.h index 60faef5..16362fb 100644 --- a/rvas.h +++ b/rvas.h @@ -37,8 +37,14 @@ struct LuaRVAs { // --- Game Object Type Wrapper --- uintptr_t got_wrapper_ctor; // GameObjectTypeWrapper::GameObjectTypeWrapper() - uintptr_t got_get_text_name_id; // GameObjectTypeClass::Get_Text_Name_ID() - uintptr_t got_lmfw_ctor; // LuaMemberFunctionWrapper::LuaMemberFunctionWrapper<>() + size_t got_text_name_id_offset; // offset of TextNameID (std::string) within GameObjectTypeClass + // LuaMemberFunctionWrapper layout (ctor always inlined) + uintptr_t lmfw_vftable; // LuaMemberFunctionWrapper<>::vftable + uintptr_t lua_uservar_ctor; // LuaUserVar::LuaUserVar(int, bool) + size_t lmfw_alloc_size; // allocation size of LuaMemberFunctionWrapper<> + size_t lmfw_pmf_offset; // offset of MemberFunction (16-byte PMF) + size_t lmfw_object_offset; // offset of Object (wrapper pointer) + size_t lmfw_usemaps_offset; // offset of UseMaps (bool) // --- Debug --- uintptr_t debug_print; // Debug_Print(char*) @@ -81,8 +87,13 @@ constexpr LuaRVAs RVAs_StarWarsI = { // --- Game Object Type Wrapper --- 0x004f6c9, // GameObjectTypeWrapper::GameObjectTypeWrapper() - 0x007b747, // GameObjectTypeClass::Get_Text_Name_ID() - 0x0f08450, // LuaMemberFunctionWrapper::ctor + 0x160, // GameObjectTypeClass::TextNameID field offset + 0x158a680, // LuaMemberFunctionWrapper<>::vftable + 0x0074f55, // LuaUserVar::LuaUserVar + 0x88, // LuaMemberFunctionWrapper<> allocation size + 0x68, // MemberFunction offset + 0x78, // Object offset + 0x80, // UseMaps offset // --- Debug --- 0x0476AE0, // Debug_Print @@ -125,8 +136,13 @@ constexpr LuaRVAs RVAs_StarWarsG = { // --- Game Object Type Wrapper --- 0x0604a10, // GameObjectTypeWrapper::GameObjectTypeWrapper() - 0, // GameObjectTypeClass::Get_Text_Name_ID() -- not mapped - 0, // LuaMemberFunctionWrapper::ctor -- not mapped + 0x120, // GameObjectTypeClass::TextNameID field offset + 0x08ab770, // LuaMemberFunctionWrapper<>::vftable + 0x0249c20, // LuaUserVar::LuaUserVar + 0x48, // LuaMemberFunctionWrapper<> allocation size + 0x28, // MemberFunction offset + 0x38, // Object offset + 0x40, // UseMaps offset // --- Debug --- 0, // Debug_Print (not mapped) -- 2.39.5 From 4987ee81f604a58bd799379d67c28e698b2b27f5 Mon Sep 17 00:00:00 2001 From: "Drew C." Date: Sat, 14 Mar 2026 22:40:13 -0500 Subject: [PATCH 3/3] Fix display text in StarWarsG --- functions/game_object_type.cpp | 19 +++++------ functions/screen_text.cpp | 25 ++++++++++++++ lua_hook.cpp | 59 +++++++++++++++++++++++++++++++--- lua_hook.h | 8 +++++ rvas.h | 9 ++++++ 5 files changed, 105 insertions(+), 15 deletions(-) diff --git a/functions/game_object_type.cpp b/functions/game_object_type.cpp index 353e34b..41c14bc 100644 --- a/functions/game_object_type.cpp +++ b/functions/game_object_type.cpp @@ -4,9 +4,8 @@ #include #include -// Offset of SmartPtr within GameObjectTypeWrapper. -// SmartPtr stores the raw pointer as its first member. -static constexpr int OFFSET_OBJECT = 0x70; +// SmartPtr offset is per-executable (0x70 StarWarsI, 0x28 StarWarsG). +// Read from g_rvas->got_object_offset at runtime. // MSVC std::string layout (x64, MSVC 14.0+). // Used to read the std::string at a known field offset within GameObjectTypeClass. @@ -39,23 +38,21 @@ struct MSVCWString { // On x64 this is the standard calling convention: this_wrapper in RCX, script in RDX, params in R8. static void* Lua_Get_Display_Name(void* this_wrapper, void* /*script*/, void* /*params*/) { - // 1. Get GameObjectTypeClass* from SmartPtr at offset 0x70 - void* got_class = *(void**)((char*)this_wrapper + OFFSET_OBJECT); + // 1. Get GameObjectTypeClass* from SmartPtr at offset + void* got_class = *(void**)((char*)this_wrapper + g_rvas->got_object_offset); if (!got_class) return nullptr; - // 2. Read the TextNameID string directly from the GameObjectTypeClass field. - // Get_Text_Name_ID() is always inlined by the compiler, so we use the field offset. + // 2. Read the TextNameID string from the GameObjectTypeClass field MSVCString* text_name = (MSVCString*)((char*)got_class + g_text_name_id_offset); std::string text_id(text_name->c_str(), text_name->size); - // 3. Resolve the localized display name via TheGameText.Get(text_id, false). - // Returns a pointer to an internal wstring (the translated text). + // 3. Resolve the localized display name via TheGameText.Get(text_id, false) MSVCWString* display_wstr = (MSVCWString*)pfn_game_text_get(g_the_game_text, &text_id, false); if (!display_wstr) return nullptr; - // 4. Convert the wide display name to a narrow (UTF-8) string for Lua. + // 4. Convert the wide display name to a narrow (UTF-8) string for Lua const wchar_t* wdata = display_wstr->c_str(); int wlen = (int)display_wstr->size; @@ -66,7 +63,7 @@ static void* Lua_Get_Display_Name(void* this_wrapper, void* /*script*/, void* /* std::string narrow(narrow_len, '\0'); WideCharToMultiByte(CP_UTF8, 0, wdata, wlen, &narrow[0], narrow_len, nullptr, nullptr); - // 5. Create a LuaString (LuaValue) and return it. + // 5. Create a LuaString and return it void* lua_value = CreateLuaValueString(narrow); if (!lua_value) return nullptr; diff --git a/functions/screen_text.cpp b/functions/screen_text.cpp index 0e75de7..b6c7aa2 100644 --- a/functions/screen_text.cpp +++ b/functions/screen_text.cpp @@ -9,8 +9,16 @@ // before its own null check, so we always pass a valid buffer. static int L_Add_Tutorial_Text(lua_State* L) { + if (pfn_debug_print) pfn_debug_print("[FoCAPI] Add_Tutorial_Text: entered\n"); + const char* key = pfn_tostring(L, 1); const char* text = pfn_tostring(L, 2); + if (pfn_debug_print) { + char msg[512]; + wsprintfA(msg, "[FoCAPI] Add_Tutorial_Text: key = \"%s\", text = \"%s\"\n", + key ? key : "(null)", text ? text : "(null)"); + pfn_debug_print(msg); + } if (!key || !text) return 0; @@ -35,12 +43,29 @@ static int L_Add_Tutorial_Text(lua_State* L) } } + if (pfn_debug_print) { + char msg[128]; + wsprintfA(msg, "[FoCAPI] Add_Tutorial_Text: duration = %d, color = [%d,%d,%d,%d]\n", + (int)(duration * 1000), (int)(color[0] * 255), (int)(color[1] * 255), + (int)(color[2] * 255), (int)(color[3] * 255)); + pfn_debug_print(msg); + } + int wlen = MultiByteToWideChar(CP_ACP, 0, text, -1, nullptr, 0); std::wstring wtext(wlen - 1, L'\0'); MultiByteToWideChar(CP_ACP, 0, text, -1, &wtext[0], wlen); + if (pfn_debug_print) { + char msg[128]; + wsprintfA(msg, "[FoCAPI] Add_Tutorial_Text: wlen = %d, calling pfn_add_tutorial_text(%p, ...)\n", + wlen, g_command_bar); + pfn_debug_print(msg); + } + pfn_add_tutorial_text(g_command_bar, &wtext, (char*)key, duration, 0, false, color); + if (pfn_debug_print) pfn_debug_print("[FoCAPI] Add_Tutorial_Text: returned OK\n"); + return 0; } diff --git a/lua_hook.cpp b/lua_hook.cpp index 515a6f0..fdcf62d 100644 --- a/lua_hook.cpp +++ b/lua_hook.cpp @@ -3,8 +3,28 @@ #include #include #include +#include +#include #include +void debug_log(const char* fmt, ...) { + char buf[1024]; + va_list args; + va_start(args, fmt); + vsnprintf(buf, sizeof(buf), fmt, args); + va_end(args); + + // Also route through in-game debug if available + if (pfn_debug_print) + pfn_debug_print(buf); + + FILE* f = nullptr; + if (fopen_s(&f, "FoCAPI_debug.log", "a") == 0 && f) { + fputs(buf, f); + fclose(f); + } +} + // Cached during init — used by CreateLuaValueString and Register_GOTMembers const LuaRVAs* g_rvas = nullptr; uintptr_t g_base = 0; @@ -32,6 +52,8 @@ fn_ReturnVariable pfn_return_variable = nullptr; fn_GotWrapperCtor real_got_wrapper_ctor = nullptr; fn_LuaUserVarCtor pfn_lua_uservar_ctor = nullptr; size_t g_text_name_id_offset = 0; +fn_LuaValueStringCtor pfn_lua_value_string_ctor = nullptr; +fn_GameOperatorNew pfn_game_operator_new = nullptr; fn_GameTextGet pfn_game_text_get = nullptr; void* g_the_game_text = nullptr; @@ -50,11 +72,35 @@ void* CreateLuaValueString(const std::string& str) { return nullptr; memset(obj, 0, g_rvas->lua_value_string_size); - // Set the vftable pointer - *(uintptr_t*)obj = g_base + g_rvas->lua_value_string_vftable; + if (pfn_lua_value_string_ctor) { + // Use the game's own constructor (StarWarsI). + // Handles all base class init (RefCountClass, RTTI, PooledObjectClass). + pfn_lua_value_string_ctor(obj, (void*)&str); + } else { + // Manual construction for builds where the ctor is inlined (StarWarsG). + *(uintptr_t*)obj = g_base + g_rvas->lua_value_string_vftable; - // Placement-new the string into the member offset - new ((char*)obj + g_rvas->lua_value_string_offset) std::string(str); + char* s = (char*)obj + g_rvas->lua_value_string_offset; + size_t len = str.size(); + + if (len < 16) { + memcpy(s, str.c_str(), len + 1); + *(size_t*)(s + 16) = len; + *(size_t*)(s + 24) = 15; + } else { + size_t cap = len | 0xF; + // Use the game's operator new so the game's destructor can free + // the buffer with its own operator delete (same CRT heap). + char* buf = pfn_game_operator_new + ? (char*)pfn_game_operator_new(cap + 1) + : (char*)::operator new(cap + 1); + memcpy(buf, str.c_str(), len); + buf[len] = '\0'; + *(char**)(s) = buf; + *(size_t*)(s + 16) = len; + *(size_t*)(s + 24) = cap; + } + } return obj; } @@ -129,6 +175,11 @@ bool LuaHook_Init() { if (rvas->return_variable) pfn_return_variable = (fn_ReturnVariable)(base + rvas->return_variable); + if (rvas->lua_value_string_ctor) + pfn_lua_value_string_ctor = (fn_LuaValueStringCtor)(base + rvas->lua_value_string_ctor); + if (rvas->game_operator_new) + pfn_game_operator_new = (fn_GameOperatorNew)(base + rvas->game_operator_new); + if (rvas->the_game_text) g_the_game_text = (void*)(base + rvas->the_game_text); if (rvas->get_string) diff --git a/lua_hook.h b/lua_hook.h index 6515dd1..932e353 100644 --- a/lua_hook.h +++ b/lua_hook.h @@ -27,6 +27,9 @@ typedef void* (*fn_ReturnVariable)(void* uservar, void* lua_var); // Game Object Type Wrapper internals typedef void* (*fn_GotWrapperCtor)(void* this_wrapper); // GameObjectTypeWrapper::GameObjectTypeWrapper() typedef void* (*fn_LuaUserVarCtor)(void* this_ptr, int param, bool flag); // LuaUserVar::LuaUserVar(int, bool) +// LuaValue> constructor -- available in StarWarsI, inlined in StarWarsG +typedef void* (*fn_LuaValueStringCtor)(void* this_ptr, void* str_ptr); // LuaValue>::LuaValue(basic_string*) +typedef void* (*fn_GameOperatorNew)(size_t size); // operator new(size_t) — game's CRT allocator // GameObjectTypeClass::TextNameID is accessed via field offset (always inlined by compiler) typedef void* (*fn_GameTextGet)(void* game_text, void* text_id_str, bool insert); // GameTextClass::Get(basic_string*, bool) -- returns wstring* @@ -53,6 +56,8 @@ extern fn_ReturnVariable pfn_return_variable; extern fn_GotWrapperCtor real_got_wrapper_ctor; extern fn_LuaUserVarCtor pfn_lua_uservar_ctor; extern size_t g_text_name_id_offset; +extern fn_LuaValueStringCtor pfn_lua_value_string_ctor; +extern fn_GameOperatorNew pfn_game_operator_new; extern fn_GameTextGet pfn_game_text_get; extern void* g_the_game_text; extern const LuaRVAs* g_rvas; @@ -67,5 +72,8 @@ void register_global(lua_State* L, const char* name, lua_CFunction fn); // Reproduces the inlined constructor pattern using per-executable layout constants. void* CreateLuaValueString(const std::string& str); +// File-based debug logging (works on both executables; pfn_debug_print is unavailable on StarWarsG). +void debug_log(const char* fmt, ...); + bool LuaHook_Init(); void LuaHook_Shutdown(); diff --git a/rvas.h b/rvas.h index 16362fb..2fd14cb 100644 --- a/rvas.h +++ b/rvas.h @@ -19,6 +19,8 @@ struct LuaRVAs { uintptr_t lua_value_string_vftable; // LuaValue>::vftable size_t lua_value_string_size; // allocation size of LuaValue> size_t lua_value_string_offset; // offset of basic_string member within LuaValue + uintptr_t lua_value_string_ctor; // LuaValue>::LuaValue(basic_string*) — 0 if inlined + uintptr_t game_operator_new; // operator new(size_t) — game's CRT allocator, 0 if ctor available uintptr_t return_variable; // LuaUserVar::Return_Variable(LuaVar*) // --- TheGameText --- @@ -37,6 +39,7 @@ struct LuaRVAs { // --- Game Object Type Wrapper --- uintptr_t got_wrapper_ctor; // GameObjectTypeWrapper::GameObjectTypeWrapper() + size_t got_object_offset; // offset of SmartPtr within GameObjectTypeWrapper size_t got_text_name_id_offset; // offset of TextNameID (std::string) within GameObjectTypeClass // LuaMemberFunctionWrapper layout (ctor always inlined) uintptr_t lmfw_vftable; // LuaMemberFunctionWrapper<>::vftable @@ -69,6 +72,8 @@ constexpr LuaRVAs RVAs_StarWarsI = { 0x1453310, // LuaValue>::vftable 0x78, // LuaValue> allocation size 0x58, // basic_string member offset within LuaValue + 0x0122920, // LuaValue>::LuaValue(basic_string*) + 0, // operator new(size_t) — not needed, ctor available 0x0058887, // LuaUserVar::Return_Variable // --- TheGameText --- @@ -87,6 +92,7 @@ constexpr LuaRVAs RVAs_StarWarsI = { // --- Game Object Type Wrapper --- 0x004f6c9, // GameObjectTypeWrapper::GameObjectTypeWrapper() + 0x70, // SmartPtr offset within GameObjectTypeWrapper 0x160, // GameObjectTypeClass::TextNameID field offset 0x158a680, // LuaMemberFunctionWrapper<>::vftable 0x0074f55, // LuaUserVar::LuaUserVar @@ -118,6 +124,8 @@ constexpr LuaRVAs RVAs_StarWarsG = { 0x0855a98, // LuaValue>::vftable 0x30, // LuaValue> allocation size 0x10, // basic_string member offset within LuaValue + 0, // LuaValue>::LuaValue(basic_string*) — inlined in StarWarsG + 0x0769c58, // operator new(size_t) — game's CRT allocator for manual string construction 0x0256d40, // LuaUserVar::Return_Variable // --- TheGameText --- @@ -136,6 +144,7 @@ constexpr LuaRVAs RVAs_StarWarsG = { // --- Game Object Type Wrapper --- 0x0604a10, // GameObjectTypeWrapper::GameObjectTypeWrapper() + 0x28, // SmartPtr offset within GameObjectTypeWrapper 0x120, // GameObjectTypeClass::TextNameID field offset 0x08ab770, // LuaMemberFunctionWrapper<>::vftable 0x0249c20, // LuaUserVar::LuaUserVar -- 2.39.5