Enable Get_Display_Name in StarWarsG #3

Merged
drew merged 3 commits from display-text into main 2026-03-14 22:41:24 -05:00
4 changed files with 76 additions and 36 deletions
Showing only changes of commit 67adfd1e08 - Show all commits

View File

@@ -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<GameObjectTypeWrapper> (0x88 bytes).
void* lmfw = ::operator new(0x88);
const LuaRVAs* rvas = g_rvas;
// Allocate a LuaMemberFunctionWrapper<GameObjectTypeWrapper>.
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);

View File

@@ -5,9 +5,9 @@
#include <cstring>
#include <new>
// 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);

View File

@@ -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<GameObjectTypeWrapper>::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;

28
rvas.h
View File

@@ -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<GameObjectTypeWrapper>::LuaMemberFunctionWrapper<>()
size_t got_text_name_id_offset; // offset of TextNameID (std::string) within GameObjectTypeClass
// LuaMemberFunctionWrapper<GameObjectTypeWrapper> 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<GameObjectTypeWrapper>::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<GameObjectTypeWrapper>::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)