Skip to content

Commit 07281b6

Browse files
committed
Revert "Use red4ext state hooks"
This reverts commit 7e08372.
1 parent 2cbee2c commit 07281b6

File tree

5 files changed

+43
-95
lines changed

5 files changed

+43
-95
lines changed

src/CET.cpp

+3-3
Original file line numberDiff line numberDiff line change
@@ -8,9 +8,9 @@ using namespace std::chrono_literals;
88
static std::unique_ptr<CET> s_pInstance{nullptr};
99
static bool s_isRunning{true};
1010

11-
void CET::Initialize(const RED4ext::Sdk* aSdk)
11+
void CET::Initialize()
1212
{
13-
s_pInstance.reset(new CET(aSdk));
13+
s_pInstance.reset(new CET);
1414

1515
s_pInstance->GetVM().Prepare();
1616
}
@@ -67,7 +67,7 @@ bool CET::IsRunning() noexcept
6767
return s_isRunning;
6868
}
6969

70-
CET::CET(const RED4ext::Sdk* aSdk)
70+
CET::CET()
7171
: m_options(m_paths)
7272
, m_persistentState(m_paths, m_options)
7373
, m_bindings(m_paths, m_options)

src/CET.h

+2-3
Original file line numberDiff line numberDiff line change
@@ -6,14 +6,13 @@
66
#include "VKBindings.h"
77
#include "d3d12/D3D12.h"
88
#include "overlay/Overlay.h"
9-
#include "RED4ext/Api/Sdk.hpp"
109
#include "scripting/LuaVM.h"
1110

1211
struct CET
1312
{
1413
~CET();
1514

16-
static void Initialize(const RED4ext::Sdk* aSdk);
15+
static void Initialize();
1716
static void Shutdown();
1817
static CET& Get();
1918

@@ -28,7 +27,7 @@ struct CET
2827
static bool IsRunning() noexcept;
2928

3029
private:
31-
CET(const RED4ext::Sdk* aSdk);
30+
CET();
3231

3332
Paths m_paths;
3433
Options m_options;

src/dllmain.cpp

+4-5
Original file line numberDiff line numberDiff line change
@@ -21,15 +21,13 @@ static HANDLE s_modInstanceMutex = nullptr;
2121

2222
using namespace std::chrono_literals;
2323

24-
static bool Initialize(RED4ext::PluginHandle aHandle, const RED4ext::Sdk* aSdk)
24+
static bool Initialize()
2525
{
2626
try
2727
{
2828
MH_Initialize();
2929

30-
GameMainThread::Create(aHandle, aSdk);
31-
32-
CET::Initialize(aSdk);
30+
CET::Initialize();
3331

3432
const auto& options = CET::Get().GetOptions();
3533

@@ -92,12 +90,13 @@ static void Shutdown()
9290
RED4EXT_C_EXPORT bool RED4EXT_CALL Main(RED4ext::PluginHandle aHandle, RED4ext::EMainReason aReason, const RED4ext::Sdk* aSdk)
9391
{
9492
RED4EXT_UNUSED_PARAMETER(aHandle);
93+
RED4EXT_UNUSED_PARAMETER(aSdk);
9594

9695
switch (aReason)
9796
{
9897
case RED4ext::EMainReason::Load:
9998
{
100-
return Initialize(aHandle, aSdk);
99+
return Initialize();
101100
break;
102101
}
103102
case RED4ext::EMainReason::Unload:

src/scripting/GameHooks.cpp

+25-72
Original file line numberDiff line numberDiff line change
@@ -23,27 +23,42 @@ void GameMainThread::RepeatedTaskQueue::Drain()
2323
++taskIt;
2424
}
2525
}
26-
GameMainThread::StateTickOverride::StateTickOverride(const char* acpRealFunctionName)
26+
GameMainThread::StateTickOverride::StateTickOverride(uint32_t aHash, const char* acpRealFunctionName)
2727
{
28+
const RED4ext::UniversalRelocPtr<uint8_t> func(aHash);
29+
Location = func.GetAddr();
30+
31+
if (Location)
32+
{
33+
if (MH_CreateHook(Location, reinterpret_cast<void*>(&GameMainThread::HookStateTick), reinterpret_cast<void**>(&RealFunction)) != MH_OK || MH_EnableHook(Location) != MH_OK)
34+
Log::Error("Could not hook main thread function {}! Main thread is not completely hooked!", acpRealFunctionName);
35+
else
36+
Log::Info("Main thread function {} hook complete!", acpRealFunctionName);
37+
}
38+
else
39+
Log::Error("Could not locate {}! Main thread is not completely hooked!", acpRealFunctionName);
2840
}
2941

3042
GameMainThread::StateTickOverride::~StateTickOverride()
3143
{
44+
MH_DisableHook(Location);
45+
46+
Location = nullptr;
47+
RealFunction = nullptr;
3248
}
3349

3450
bool GameMainThread::StateTickOverride::OnTick(RED4ext::IGameState* apThisState, RED4ext::CGameApplication* apGameApplication)
3551
{
3652
Tasks.Drain();
3753

38-
return true;
54+
return RealFunction(apThisState, apGameApplication);
3955
}
4056

41-
static GameMainThread* s_gameMainThread = nullptr;
42-
4357
GameMainThread& GameMainThread::Get()
4458
{
45-
assert(s_gameMainThread);
46-
return *s_gameMainThread;
59+
static GameMainThread s_gameMainThread;
60+
61+
return s_gameMainThread;
4762
}
4863

4964
void GameMainThread::AddBaseInitializationTask(const std::function<bool()>& aFunction)
@@ -75,76 +90,14 @@ void GameMainThread::AddGenericTask(const std::function<bool()>& aFunction)
7590
m_genericQueue.AddTask(aFunction);
7691
}
7792

78-
void GameMainThread::Create(RED4ext::PluginHandle aHandle, const RED4ext::Sdk* aSdk)
79-
{
80-
if (!s_gameMainThread)
81-
s_gameMainThread = new GameMainThread(aHandle, aSdk);
82-
}
83-
84-
GameMainThread::GameMainThread(RED4ext::PluginHandle aHandle, const RED4ext::Sdk* sdk)
85-
{
86-
{
87-
RED4ext::GameState state{nullptr, &HookBaseInitStateTick, nullptr};
88-
sdk->gameStates->Add(aHandle, RED4ext::EGameStateType::BaseInitialization, &state);
89-
}
90-
{
91-
RED4ext::GameState state{nullptr, &HookInitStateTick, nullptr};
92-
sdk->gameStates->Add(aHandle, RED4ext::EGameStateType::Initialization, &state);
93-
}
94-
{
95-
RED4ext::GameState state{nullptr, &HookRunningStateTick, nullptr};
96-
sdk->gameStates->Add(aHandle, RED4ext::EGameStateType::Running, &state);
97-
}
98-
{
99-
RED4ext::GameState state{nullptr, &HookShutdownStateTick, nullptr};
100-
sdk->gameStates->Add(aHandle, RED4ext::EGameStateType::Shutdown, &state);
101-
}
102-
}
103-
104-
bool GameMainThread::HookBaseInitStateTick(RED4ext::CGameApplication* apGameApplication)
93+
bool GameMainThread::HookStateTick(RED4ext::IGameState* apThisState, RED4ext::CGameApplication* apGameApplication)
10594
{
10695
auto& gmt = Get();
10796

10897
// drain generic tasks
10998
gmt.m_genericQueue.Drain();
11099

111-
gmt.m_stateTickOverrides[0].OnTick(apGameApplication->currState, apGameApplication);
112-
113-
return true;
114-
}
115-
116-
bool GameMainThread::HookInitStateTick(RED4ext::CGameApplication* apGameApplication)
117-
{
118-
auto& gmt = Get();
119-
120-
// drain generic tasks
121-
gmt.m_genericQueue.Drain();
122-
123-
gmt.m_stateTickOverrides[1].OnTick(apGameApplication->currState, apGameApplication);
124-
125-
return true;
126-
}
127-
128-
bool GameMainThread::HookRunningStateTick(RED4ext::CGameApplication* apGameApplication)
129-
{
130-
auto& gmt = Get();
131-
132-
// drain generic tasks
133-
gmt.m_genericQueue.Drain();
134-
135-
gmt.m_stateTickOverrides[2].OnTick(apGameApplication->currState, apGameApplication);
136-
137-
return false;
138-
}
139-
140-
bool GameMainThread::HookShutdownStateTick(RED4ext::CGameApplication* apGameApplication)
141-
{
142-
auto& gmt = Get();
143-
144-
// drain generic tasks
145-
gmt.m_genericQueue.Drain();
146-
147-
gmt.m_stateTickOverrides[3].OnTick(apGameApplication->currState, apGameApplication);
148-
149-
return true;
100+
// execute specific state tasks, including original function
101+
const auto cStateIndex = static_cast<size_t>(apThisState->GetType());
102+
return gmt.m_stateTickOverrides[cStateIndex].OnTick(apThisState, apGameApplication);
150103
}

src/scripting/GameHooks.h

+9-12
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,7 @@
11
#pragma once
2-
#include "RED4ext/Api/Sdk.hpp"
32

43
struct GameMainThread
54
{
6-
static void Create(RED4ext::PluginHandle aHandle, const RED4ext::Sdk* aSdk);
75
static GameMainThread& Get();
86

97
void AddBaseInitializationTask(const std::function<bool()>& aFunction);
@@ -13,14 +11,11 @@ struct GameMainThread
1311
void AddGenericTask(const std::function<bool()>& aFunction);
1412

1513
private:
16-
GameMainThread(RED4ext::PluginHandle aHandle, const RED4ext::Sdk* sdk);
14+
GameMainThread() = default;
1715

1816
using TStateTick = bool(RED4ext::IGameState*, RED4ext::CGameApplication*);
1917

20-
static bool HookBaseInitStateTick(RED4ext::CGameApplication* apGameApplication);
21-
static bool HookInitStateTick(RED4ext::CGameApplication* apGameApplication);
22-
static bool HookRunningStateTick(RED4ext::CGameApplication* apGameApplication);
23-
static bool HookShutdownStateTick(RED4ext::CGameApplication* apGameApplication);
18+
static bool HookStateTick(RED4ext::IGameState* apThisState, RED4ext::CGameApplication* apGameApplication);
2419

2520
// helper task queue which executes added tasks each drain until they are finished
2621
struct RepeatedTaskQueue
@@ -35,19 +30,21 @@ struct GameMainThread
3530

3631
struct StateTickOverride
3732
{
38-
StateTickOverride(const char* acpRealFunctionName);
33+
StateTickOverride(uint32_t aHash, const char* acpRealFunctionName);
3934
~StateTickOverride();
4035

4136
bool OnTick(RED4ext::IGameState*, RED4ext::CGameApplication*);
4237

38+
uint8_t* Location = nullptr;
39+
TStateTick* RealFunction = nullptr;
4340
RepeatedTaskQueue Tasks;
4441
};
4542

4643
std::array<StateTickOverride, 4> m_stateTickOverrides{
47-
StateTickOverride("CBaseInitializationState::OnTick"),
48-
StateTickOverride("CInitializationState::OnTick"),
49-
StateTickOverride( "CRunningState::OnTick"),
50-
StateTickOverride("CShutdownState::OnTick")};
44+
StateTickOverride(CyberEngineTweaks::AddressHashes::CBaseInitializationState_OnTick, "CBaseInitializationState::OnTick"),
45+
StateTickOverride(CyberEngineTweaks::AddressHashes::CInitializationState_OnTick, "CInitializationState::OnTick"),
46+
StateTickOverride(CyberEngineTweaks::AddressHashes::CRunningState_OnTick, "CRunningState::OnTick"),
47+
StateTickOverride(CyberEngineTweaks::AddressHashes::CShutdownState_OnTick, "CShutdownState::OnTick")};
5148

5249
RepeatedTaskQueue m_genericQueue;
5350
};

0 commit comments

Comments
 (0)