Skip to content

Commit

Permalink
Merge pull request #629 from psiberx/master
Browse files Browse the repository at this point in the history
Overrides fixes, TweakDB hook
  • Loading branch information
Yamashi authored Dec 4, 2021
2 parents b570a16 + 60f96c8 commit a11e02f
Show file tree
Hide file tree
Showing 11 changed files with 279 additions and 170 deletions.
225 changes: 121 additions & 104 deletions src/scripting/FunctionOverride.cpp

Large diffs are not rendered by default.

7 changes: 4 additions & 3 deletions src/scripting/FunctionOverride.h
Original file line number Diff line number Diff line change
Expand Up @@ -38,8 +38,8 @@ struct FunctionOverride
static bool HookRunPureScriptFunction(RED4ext::CClassFunction* apFunction, RED4ext::CScriptStack* apContext, RED4ext::CStackFrame* a3);
static bool ExecuteChain(const CallChain& aChain, std::shared_lock<std::shared_mutex>& aLock,
RED4ext::IScriptable* apContext, TiltedPhoques::Vector<sol::object>* apArgs,
RED4ext::CStackType* apResult, RED4ext::CScriptStack* apStack,
RED4ext::CStackFrame* apFrame, char* apCode, uint8_t aParam);
RED4ext::CStackType* apResult, TiltedPhoques::Vector<RED4ext::CStackType>* apOutArgs,
RED4ext::CScriptStack* apStack, RED4ext::CStackFrame* apFrame, char* apCode, uint8_t aParam);
static sol::function WrapNextOverride(const CallChain& aChain, int aStep, sol::state& aLuaState,
sol::object& aLuaContext, TiltedPhoques::Vector<sol::object>& aLuaArgs,
RED4ext::CBaseFunction* apRealFunction, RED4ext::IScriptable* apRealContext,
Expand All @@ -57,7 +57,8 @@ struct FunctionOverride
void* m_pBufferStart;
void* m_pBuffer;
size_t m_size{ kExecutableSize };
TiltedPhoques::Map<RED4ext::CClassFunction*, CallChain> m_functions;
TiltedPhoques::Map<RED4ext::CBaseFunction*, CallChain> m_functions;
TiltedPhoques::Map<RED4ext::CBaseFunction*, RED4ext::CBaseFunction*> m_trampolines;
Scripting* m_pScripting;
std::shared_mutex m_lock;
};
14 changes: 10 additions & 4 deletions src/scripting/LuaVM.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ void LuaVM::Update(float aDeltaTime)
if (!m_initialized)
{
if (m_logCount.load(std::memory_order_relaxed) > 0)
PostInitialize();
PostInitializeStage2();

return;
}
Expand All @@ -46,6 +46,7 @@ void LuaVM::ReloadAllMods()
if (m_initialized)
{
m_scripting.ReloadAllMods();
m_scripting.TriggerOnTweak();
m_scripting.TriggerOnInit();

if (CET::Get().GetOverlay().IsEnabled())
Expand Down Expand Up @@ -180,12 +181,17 @@ void LuaVM::RegisterTDBIDString(uint64_t aValue, uint64_t aBase, const std::stri
m_tdbidDerivedLookup[aBase].insert(aValue);
}

void LuaVM::PostInitialize()
void LuaVM::PostInitializeStage1()
{
assert(!m_initialized);
m_scripting.PostInitializeStage1();
m_scripting.TriggerOnTweak();
}

m_scripting.PostInitialize();
void LuaVM::PostInitializeStage2()
{
assert(!m_initialized);

m_scripting.PostInitializeStage2();
m_scripting.TriggerOnInit();
if (CET::Get().GetOverlay().IsEnabled())
m_scripting.TriggerOnOverlayOpen();
Expand Down
6 changes: 5 additions & 1 deletion src/scripting/LuaVM.h
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ using TSetMousePosition = BOOL(void*, HWND, long, long);
using TTDBIDCtorDerive = TDBID*(const TDBID*, TDBID*, const char*);
using TRunningStateRun = bool(uintptr_t, uintptr_t);
using TSetLoadingState = uintptr_t(uintptr_t, int);
using TTweakDBLoad = uint64_t(uintptr_t, uintptr_t);

struct TDBIDLookupEntry
{
Expand Down Expand Up @@ -51,7 +52,8 @@ struct LuaVM

void RegisterTDBIDString(uint64_t aValue, uint64_t aBase, const std::string& acString);

void PostInitialize();
void PostInitializeStage1();
void PostInitializeStage2();

protected:

Expand All @@ -63,6 +65,7 @@ struct LuaVM
static TDBID* HookTDBIDCtorDerive(TDBID* apBase, TDBID* apThis, const char* acpName);
static bool HookRunningStateRun(uintptr_t aThis, uintptr_t aApp);
static uintptr_t HookSetLoadingState(uintptr_t aThis, int aState);
static uint64_t HookTweakDBLoad(uintptr_t aThis, uintptr_t aParam);

private:

Expand All @@ -77,6 +80,7 @@ struct LuaVM
TTDBIDCtorDerive* m_realTDBIDCtorDerive{ nullptr };
TRunningStateRun* m_realRunningStateRun{ nullptr };
TSetLoadingState* m_realSetLoadingState{ nullptr };
TTweakDBLoad* m_realTweakDBLoad{ nullptr };

std::chrono::time_point<std::chrono::high_resolution_clock> m_lastframe;

Expand Down
30 changes: 29 additions & 1 deletion src/scripting/LuaVM_Hooks.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -141,13 +141,24 @@ uintptr_t LuaVM::HookSetLoadingState(uintptr_t aThis, int aState)
{
std::call_once(s_initBarrier, [&luavm]()
{
luavm.PostInitialize();
luavm.PostInitializeStage2();
});
}

return luavm.m_realSetLoadingState(aThis, aState);
}

uint64_t LuaVM::HookTweakDBLoad(uintptr_t aThis, uintptr_t aParam)
{
auto& luavm = CET::Get().GetVM();

const auto ret = luavm.m_realTweakDBLoad(aThis, aParam);

luavm.PostInitializeStage1();

return ret;
}

void LuaVM::Hook(Options& aOptions)
{
auto& gameImage = aOptions.GameImage;
Expand Down Expand Up @@ -241,6 +252,23 @@ void LuaVM::Hook(Options& aOptions)
}
}
}

{
const mem::pattern cPattern("48 89 5C 24 10 48 89 7C 24 18 4C 89 74 24 20 55 48 8B EC 48 83 EC 70 48");
const mem::default_scanner cScanner(cPattern);
uint8_t* pLocation = cScanner(gameImage.TextRegion).as<uint8_t*>();

if (pLocation)
{
if (MH_CreateHook(pLocation, &HookTweakDBLoad, reinterpret_cast<void**>(&m_realTweakDBLoad)) != MH_OK ||
MH_EnableHook(pLocation) != MH_OK)
spdlog::error("Could not hook TweakDB::Load function!");
else
{
spdlog::info("TweakDB::Load function hook complete!");
}
}
}

// Disable SetLoadingState hook temporarily and get back to log count workaround
// as it introduces major breaking change for onInit handler.
Expand Down
9 changes: 9 additions & 0 deletions src/scripting/ScriptContext.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,8 @@ ScriptContext::ScriptContext(LuaSandbox& aLuaSandbox, const std::filesystem::pat
{
if(acName == "onInit")
m_onInit = aCallback;
else if(acName == "onTweak")
m_onTweak = aCallback;
else if(acName == "onShutdown")
m_onShutdown = aCallback;
else if(acName == "onUpdate")
Expand Down Expand Up @@ -155,6 +157,13 @@ const TiltedPhoques::Vector<VKBindInfo>& ScriptContext::GetBinds() const
return m_vkBindInfos;
}

void ScriptContext::TriggerOnTweak() const
{
auto state = m_sandbox.GetState();

TryLuaFunction(m_logger, m_onTweak);
}

void ScriptContext::TriggerOnInit() const
{
auto state = m_sandbox.GetState();
Expand Down
2 changes: 2 additions & 0 deletions src/scripting/ScriptContext.h
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ struct ScriptContext

const TiltedPhoques::Vector<VKBindInfo>& GetBinds() const;

void TriggerOnTweak() const;
void TriggerOnInit() const;
void TriggerOnUpdate(float aDeltaTime) const;
void TriggerOnDraw() const;
Expand All @@ -30,6 +31,7 @@ struct ScriptContext
LuaSandbox& m_sandbox;
size_t m_sandboxID;
sol::object m_object{ };
sol::function m_onTweak{ };
sol::function m_onInit{ };
sol::function m_onShutdown{ };
sol::function m_onUpdate{ };
Expand Down
20 changes: 16 additions & 4 deletions src/scripting/ScriptStore.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -25,10 +25,16 @@ void ScriptStore::LoadAll()

auto fPath = file.path();

if (is_symlink(fPath))
fPath = read_symlink(fPath);
else if (is_symlink(fPath / "init.lua"))
fPath = read_symlink(fPath / "init.lua").parent_path();
try
{
if (is_symlink(fPath))
fPath = read_symlink(fPath);
else if (is_symlink(fPath / "init.lua"))
fPath = read_symlink(fPath / "init.lua").parent_path();
}
catch (std::exception& e)
{
}

fPath = absolute(fPath);
auto fPathStr = fPath.string();
Expand Down Expand Up @@ -68,6 +74,12 @@ const TiltedPhoques::Vector<VKBindInfo>& ScriptStore::GetBinds() const
return m_vkBindInfos;
}

void ScriptStore::TriggerOnTweak() const
{
for (const auto& kvp : m_contexts)
kvp.second.TriggerOnTweak();
}

void ScriptStore::TriggerOnInit() const
{
for (const auto& kvp : m_contexts)
Expand Down
1 change: 1 addition & 0 deletions src/scripting/ScriptStore.h
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ struct ScriptStore

const TiltedPhoques::Vector<VKBindInfo>& GetBinds() const;

void TriggerOnTweak() const;
void TriggerOnInit() const;
void TriggerOnUpdate(float aDeltaTime) const;
void TriggerOnDraw() const;
Expand Down
Loading

0 comments on commit a11e02f

Please sign in to comment.