Skip to content

Commit

Permalink
onHook handler for early overrides/observes, safe onInit not using lo…
Browse files Browse the repository at this point in the history
…g count
  • Loading branch information
WSSDude committed Nov 7, 2022
1 parent cf1d54b commit e17d61b
Show file tree
Hide file tree
Showing 11 changed files with 64 additions and 29 deletions.
3 changes: 3 additions & 0 deletions ida/patterns.py
Original file line number Diff line number Diff line change
Expand Up @@ -78,5 +78,8 @@ def get_groups() -> List[Group]:
Group(name='CGame', functions=[
Item(name='Main', pattern='40 57 48 83 EC 70 48 8B F9 0F 29 7C 24 50 48 8D 4C 24 38', expected=1)
]),
Group(name='PlayerSystem', functions=[
Item(name='OnPlayerSpawned', pattern='48 8B C4 4C 89 48 20 55 56 57 48 8B EC 48 81 EC 80 00 00 00 44 8B 15 25 60 54 02 48 8B F1', expected=1)
]),
]

4 changes: 4 additions & 0 deletions src/reverse/Addresses.h
Original file line number Diff line number Diff line change
Expand Up @@ -68,4 +68,8 @@ constexpr uintptr_t gameIGameSystem_Spawn = 0x142DBE400 - ImageBase; // 48 89 5C
constexpr uintptr_t gameIGameSystem_Despawn = 0x142DBBEA0 - ImageBase; // 48 89 5C 24 10 48 89 6C 24 18 56 57 41 54 41 56 41 57 48 83 EC 50 4C 8B F9 0F 57 C0 48 83 C1 41, expected: 1, index: 0
constexpr uintptr_t gameIGameSystem_SpawnCallback = 0x1410F1780 - ImageBase; // 48 89 5C 24 18 48 89 6C 24 20 56 57 41 56 48 83 EC 70 48 8B F1 48 8B EA 48 83 C1 48 E8, expected: 1, index: 0
#pragma endregion

#pragma region PlayerSystem
constexpr uintptr_t PlayerSystem_OnPlayerSpawned = 0x14271CBD0 - ImageBase; // 48 8B C4 4C 89 48 20 55 56 57 48 8B EC 48 81 EC 80 00 00 00 44 8B 15 25 60 54 02 48 8B F1, expected: 1, index: 0
#pragma endregion
} // namespace CyberEngineTweaks::Addresses
5 changes: 0 additions & 5 deletions src/scripting/LuaVM.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -33,12 +33,7 @@ bool LuaVM::ExecuteLua(const std::string& acCommand) const
void LuaVM::Update(float aDeltaTime)
{
if (!m_initialized)
{
if (m_logCount.load(std::memory_order_relaxed) > 2)
PostInitializeMods();

return;
}

CET::Get().GetBindings().Update();

Expand Down
5 changes: 3 additions & 2 deletions src/scripting/LuaVM.h
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ using TShutdownStateRun = bool(uintptr_t, uintptr_t);
using TSetLoadingState = uintptr_t(uintptr_t, int);
using TTweakDBLoad = uint64_t(uintptr_t, uintptr_t);
using TTranslateBytecode = bool(uintptr_t, uintptr_t);
using TPlayerSpawned = uint64_t(uint64_t, uint64_t, uint64_t, uint64_t);

struct TDBIDLookupEntry
{
Expand Down Expand Up @@ -73,6 +74,7 @@ struct LuaVM
static uintptr_t HookSetLoadingState(uintptr_t aThis, int aState);
static uint64_t HookTweakDBLoad(uintptr_t aThis, uintptr_t aParam);
static bool HookTranslateBytecode(uintptr_t aBinder, uintptr_t aData);
static uint64_t HookPlayerSpawned(uint64_t a1, uint64_t a2, uint64_t a3, uint64_t a4);

private:

Expand All @@ -90,8 +92,7 @@ struct LuaVM
TSetLoadingState* m_realSetLoadingState{ nullptr };
TTweakDBLoad* m_realTweakDBLoad{ nullptr };
TTranslateBytecode* m_realTranslateBytecode{ nullptr };

std::atomic<uint64_t> m_logCount{ 0 };
TPlayerSpawned* m_realPlayerSpawned{ nullptr };

Scripting m_scripting;

Expand Down
45 changes: 25 additions & 20 deletions src/scripting/LuaVM_Hooks.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -252,8 +252,6 @@ void LuaVM::HookLogChannel(RED4ext::IScriptable*, RED4ext::CStackFrame* apStack,
else
spdlog::get("gamelog")->info("[{}] {}", channelSV, ref.ref->c_str());
}

s_vm->m_logCount.fetch_add(1);
}

LuaVM::LuaVM(const Paths& aPaths, VKBindings& aBindings, D3D12& aD3D12)
Expand Down Expand Up @@ -352,6 +350,16 @@ bool LuaVM::HookTranslateBytecode(uintptr_t aBinder, uintptr_t aData)
return ret;
}

uint64_t LuaVM::HookPlayerSpawned(uint64_t a1, uint64_t a2, uint64_t a3, uint64_t a4)
{
const auto ret = s_vm->m_realPlayerSpawned(a1, a2, a3, a4);

if (!s_vm->m_initialized)
s_vm->PostInitializeMods();

return ret;
}

uint64_t LuaVM::HookTweakDBLoad(uintptr_t aThis, uintptr_t aParam)
{
const auto ret = s_vm->m_realTweakDBLoad(aThis, aParam);
Expand Down Expand Up @@ -487,23 +495,20 @@ void LuaVM::Hook()
}
}

// Disable SetLoadingState hook temporarily and get back to log count workaround
// as it introduces major breaking change for onInit handler.
//{
// const mem::pattern cPattern("48 89 5C 24 18 89 54 24 10 57 48 83 EC 20 48 8B D9 C7");
// const mem::default_scanner cScanner(cPattern);
// uint8_t* pLocation = cScanner(gameImage.TextRegion).as<uint8_t*>();
//
// if (pLocation)
// {
// if (MH_CreateHook(pLocation, &HookSetLoadingState, reinterpret_cast<void**>(&m_realSetLoadingState)) != MH_OK
// || MH_EnableHook(pLocation) != MH_OK)
// Log::Error("Could not hook SetLoadingState function!");
// else
// {
// Log::Info("SetLoadingState function hook complete!");
// }
// }
//}
{
const RED4ext::RelocPtr<uint8_t> func(CyberEngineTweaks::Addresses::PlayerSystem_OnPlayerSpawned);
uint8_t* pLocation = func.GetAddr();

if (pLocation)
{
if (MH_CreateHook(pLocation, reinterpret_cast<LPVOID>(HookPlayerSpawned), reinterpret_cast<void**>(&m_realPlayerSpawned)) != MH_OK ||
MH_EnableHook(pLocation) != MH_OK)
Log::Error("Could not hook PlayerSystem::OnPlayerSpawned function!");
else
{
Log::Info("PlayerSystem::OnPlayerSpawned function hook complete!");
}
}
}

}
13 changes: 11 additions & 2 deletions src/scripting/ScriptContext.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -46,10 +46,12 @@ ScriptContext::ScriptContext(LuaSandbox& aLuaSandbox, const std::filesystem::pat

env["registerForEvent"] = [this](const std::string& acName, sol::function aCallback)
{
if(acName == "onInit")
m_onInit = aCallback;
if(acName == "onHook")
m_onHook = aCallback;
else if(acName == "onTweak")
m_onTweak = aCallback;
else if(acName == "onInit")
m_onInit = aCallback;
else if(acName == "onShutdown")
m_onShutdown = aCallback;
else if(acName == "onUpdate")
Expand Down Expand Up @@ -235,6 +237,13 @@ const TiltedPhoques::Vector<VKBind>& ScriptContext::GetBinds() const
return m_vkBinds;
}

void ScriptContext::TriggerOnHook() const
{
auto lockedState = m_sandbox.GetLockedState();

TryLuaFunction(m_logger, m_onHook);
}

void ScriptContext::TriggerOnTweak() const
{
auto lockedState = m_sandbox.GetLockedState();
Expand Down
2 changes: 2 additions & 0 deletions src/scripting/ScriptContext.h
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ struct ScriptContext
[[nodiscard]] const VKBind* GetBind(const std::string& acId) const;
[[nodiscard]] const TiltedPhoques::Vector<VKBind>& GetBinds() const;

void TriggerOnHook() const;
void TriggerOnTweak() const;
void TriggerOnInit() const;
void TriggerOnUpdate(float aDeltaTime) const;
Expand All @@ -32,6 +33,7 @@ struct ScriptContext
LuaSandbox& m_sandbox;
uint64_t m_sandboxID;
sol::object m_object{ };
sol::function m_onHook{ };
sol::function m_onTweak{ };
sol::function m_onInit{ };
sol::function m_onShutdown{ };
Expand Down
6 changes: 6 additions & 0 deletions src/scripting/ScriptStore.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -99,6 +99,12 @@ const TiltedPhoques::Map<std::string, std::reference_wrapper<const TiltedPhoques
return m_vkBinds;
}

void ScriptStore::TriggerOnHook() const
{
for (const auto& mod : m_contexts | std::views::values)
mod.TriggerOnHook();
}

void ScriptStore::TriggerOnTweak() const
{
for (const auto& mod : m_contexts | std::views::values)
Expand Down
1 change: 1 addition & 0 deletions src/scripting/ScriptStore.h
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ struct ScriptStore
[[nodiscard]] const TiltedPhoques::Vector<VKBind>* GetBinds(const std::string& acModName) const;
[[nodiscard]] const TiltedPhoques::Map<std::string, std::reference_wrapper<const TiltedPhoques::Vector<VKBind>>>& GetAllBinds() const;

void TriggerOnHook() const;
void TriggerOnTweak() const;
void TriggerOnInit() const;
void TriggerOnUpdate(float aDeltaTime) const;
Expand Down
8 changes: 8 additions & 0 deletions src/scripting/Scripting.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -462,6 +462,8 @@ void Scripting::PostInitializeScripting()
"List", &GameOptions::List);

m_sandbox.PostInitializeScripting();

TriggerOnHook();
}

void Scripting::PostInitializeTweakDB()
Expand Down Expand Up @@ -643,6 +645,11 @@ const TiltedPhoques::Map<std::string, std::reference_wrapper<const TiltedPhoques
return m_store.GetAllBinds();
}

void Scripting::TriggerOnHook() const
{
m_store.TriggerOnHook();
}

void Scripting::TriggerOnTweak() const
{
m_store.TriggerOnTweak();
Expand Down Expand Up @@ -691,6 +698,7 @@ void Scripting::ReloadAllMods()

m_store.LoadAll();

TriggerOnHook();
TriggerOnTweak();
TriggerOnInit();

Expand Down
1 change: 1 addition & 0 deletions src/scripting/Scripting.h
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ struct Scripting
[[nodiscard]] const TiltedPhoques::Vector<VKBind>* GetBinds(const std::string& acModName) const;
[[nodiscard]] const TiltedPhoques::Map<std::string, std::reference_wrapper<const TiltedPhoques::Vector<VKBind>>>& GetAllBinds() const;

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

0 comments on commit e17d61b

Please sign in to comment.