Skip to content

Commit 4f0ad2a

Browse files
committed
- add user profile getter hook to store DNGUserProfileName in Lua
1 parent 268407e commit 4f0ad2a

File tree

5 files changed

+44
-14
lines changed

5 files changed

+44
-14
lines changed

DriverNGHook/Delegates/ILuaDelegate.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@ namespace DriverNG
1414
static ILuaDelegate& GetInstance();
1515

1616
virtual ~ILuaDelegate() noexcept = default;
17-
virtual void OnInitialised(lua_State* gameState, CallLuaFunction_t callFunc) = 0;
17+
virtual void OnInitialised(lua_State* gameState, CallLuaFunction_t callFunc, const char* profileName) = 0;
1818
virtual void OnDeleted() = 0;
1919

2020
virtual void DoCommands(lua_State* gameState) = 0;

DriverNGHook/Delegates/LuaDelegate.cpp

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -241,7 +241,7 @@ namespace DriverNG
241241
m_gameState = nullptr;
242242
}
243243

244-
void LuaDelegate::OnInitialised(lua_State* gameState, CallLuaFunction_t callFunc)
244+
void LuaDelegate::OnInitialised(lua_State* gameState, CallLuaFunction_t callFunc, const char* profileName)
245245
{
246246
bool firstTimeInit = m_gameState == nullptr;
247247

@@ -306,6 +306,10 @@ namespace DriverNG
306306
{
307307
ExecuteFile(Consts::luaScriptsPath + "game_autoexec.lua");
308308
}
309+
310+
// try setting profile name
311+
lua_pushstring(m_gameState, profileName);
312+
lua_setglobal(m_gameState, "DNGUserProfileName");
309313
}
310314

311315
void LuaDelegate::BeginRender()

DriverNGHook/Delegates/LuaDelegate.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@ namespace DriverNG
99
class LuaDelegate final : public ILuaDelegate
1010
{
1111
public:
12-
void OnInitialised(lua_State* gameState, CallLuaFunction_t callFunc) override;
12+
void OnInitialised(lua_State* gameState, CallLuaFunction_t callFunc, const char* profileName) override;
1313
void OnDeleted() override;
1414

1515
void DoCommands(lua_State* gameState) override;

DriverNGHook/Patches/All/LuaPatches.cpp

Lines changed: 35 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -22,25 +22,19 @@ namespace DriverNG
2222
static constexpr uintptr_t kStepLuaCallAddress = 0x0079B811; // to the call CState_Main::Step
2323
static constexpr uintptr_t kStepLuaOrigAddress = 0x005E4A70;
2424

25+
static constexpr uintptr_t kGetProfileNameCallAddress = 0x008534C2;
2526
static constexpr uintptr_t ksafe_vsprintfOrigAdress = 0x00903EBA;
2627
static constexpr uintptr_t kgameLuaStateAddr = 0x0122B498;
2728
}
2829

2930
namespace Globals
3031
{
3132
extern std::unique_ptr<IInputDelegate> g_pInputDelegate;
32-
extern std::mutex g_drawMutex[2];
33-
extern volatile bool g_dataDrawn;
33+
static std::string g_profileName;
34+
extern std::mutex g_drawMutex[2];
35+
extern volatile bool g_dataDrawn;
3436

3537
static ILuaDelegate& g_luaDelegate = ILuaDelegate::GetInstance();
36-
37-
static void InitializeLuaStateBindings(lua_State* newState)
38-
{
39-
// report to the game state
40-
auto callLuaFunction = (CallLuaFunction_t)Consts::kCallLuaFunctionAddress;
41-
42-
g_luaDelegate.OnInitialised(newState, callLuaFunction);
43-
}
4438
}
4539

4640
namespace Callbacks
@@ -54,7 +48,9 @@ namespace DriverNG
5448
origOpenScriptLoader(state);
5549

5650
// get the lua state from the address
57-
Globals::InitializeLuaStateBindings(state);
51+
// report to the game state
52+
auto callLuaFunction = (CallLuaFunction_t)Consts::kCallLuaFunctionAddress;
53+
Globals::g_luaDelegate.OnInitialised(state, callLuaFunction, Globals::g_profileName.c_str());
5854
}
5955

6056
void StepLua_Hooked(bool consolePaused)
@@ -124,6 +120,13 @@ namespace DriverNG
124120

125121
return ret;
126122
}
123+
124+
const char* GetProfileNameHooked(const void* _this)
125+
{
126+
const char* profileName = (const char*)_this + 4;
127+
Globals::g_profileName = profileName;
128+
return profileName;
129+
}
127130
}
128131

129132
const char* LuaPatches::GetName() const { return "Lua Patch"; }
@@ -219,6 +222,27 @@ namespace DriverNG
219222
return false;
220223
}
221224

225+
226+
// Do not revert this patch!
227+
if (!HF::Hook::FillMemoryByNOPs(process, Consts::kGetProfileNameCallAddress, kprofileNamePatchSize))
228+
{
229+
MsgError("Failed to cleanup memory\n");
230+
return false;
231+
}
232+
233+
m_getProfileNameHook = HF::Hook::HookFunction<const char*(*)(const void*), kprofileNamePatchSize>(
234+
process,
235+
Consts::kGetProfileNameCallAddress,
236+
&Callbacks::GetProfileNameHooked,
237+
{ HF::X86::PUSH_ECX },
238+
{ HF::X86::POP_ECX }
239+
);
240+
241+
if (!m_getProfileNameHook->setup())
242+
{
243+
MsgError("Failed to setup patch to DeleteLuaState!\n");
244+
return false;
245+
}
222246
return BasicPatch::Apply(modules);
223247
}
224248

DriverNGHook/Patches/All/LuaPatches.h

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,11 +14,13 @@ namespace DriverNG
1414
static constexpr size_t kStepLuaPatchSize = 5;
1515
static constexpr size_t kDeleteLuaStatePatchSize = 5;
1616
static constexpr size_t ksafe_vsprintfPatchSize = 5;
17+
static constexpr size_t kprofileNamePatchSize = 5;
1718

1819
HF::Hook::TrampolinePtr<kOpenScriptLoaderPatchSize> m_openScriptLoaderHook;
1920
HF::Hook::TrampolinePtr<kDeleteLuaStatePatchSize> m_deleteLuaStateHook;
2021
HF::Hook::TrampolinePtr<kStepLuaPatchSize> m_stepLuaHook;
2122
HF::Hook::TrampolinePtr<ksafe_vsprintfPatchSize> m_safe_vsprintfHook;
23+
HF::Hook::TrampolinePtr<kprofileNamePatchSize> m_getProfileNameHook;
2224
public:
2325
LuaPatches() = default;
2426

0 commit comments

Comments
 (0)