Skip to content

Commit 80b28dd

Browse files
committed
Switch RakClient intf hooks to kthook
A plugin named as connd was setting up interface hook to the same location where RakLua it does. To prevent crash, we have to add compatibility with 3rd party hooks too, but rtdhook can't do this So, let's use kthook, yeah...
1 parent f089088 commit 80b28dd

File tree

6 files changed

+23
-16
lines changed

6 files changed

+23
-16
lines changed

RakLua.lua

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -11,15 +11,15 @@ local events = {
1111

1212
local addEventHandler_orig = addEventHandler
1313

14-
function hookAddEventHandler(event, func)
14+
local function hookAddEventHandler(event, func)
1515
if events[event] then
1616
RakLuaDll.registerHandler(events[event], func)
1717
else
1818
addEventHandler_orig(event, func)
1919
end
2020
end
2121

22-
function defineSampLuaCompatibility()
22+
local function defineSampLuaCompatibility()
2323
RakLuaDll.defineGlobals()
2424

2525
addEventHandler = hookAddEventHandler

src/RakLua.cpp

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,8 @@ RakLua::eInitState RakLua::initialize()
2222

2323
mState = eInitState::INITIALIZING;
2424

25-
mRakClientIntfConstructor = new rtdhook(sampGetRakClientIntfConstructorPtr(), &hookRakClientIntfConstructor, 7);
25+
mRakClientIntfConstructor = new kthook::kthook_simple<uintptr_t(*)()>(sampGetRakClientIntfConstructorPtr());
26+
mRakClientIntfConstructor->set_cb(hookRakClientIntfConstructor);
2627
mRakClientIntfConstructor->install();
2728

2829
returnState:
@@ -229,9 +230,9 @@ bool __fastcall handleIncomingRpc(void* ptr, void*, unsigned char* data, int len
229230
(gRakLua.getRpcHook()->getTrampoline())(ptr, bs.GetData(), bs.GetNumberOfBytesUsed(), playerId);
230231
}
231232

232-
uintptr_t hookRakClientIntfConstructor()
233+
uintptr_t hookRakClientIntfConstructor(const kthook::kthook_simple<uintptr_t(*)()> &hook)
233234
{
234-
uintptr_t rakClientInterface = reinterpret_cast<uintptr_t(*)()>(gRakLua.getIntfConstructorHook()->getTrampoline())();
235+
uintptr_t rakClientInterface = gRakLua.getIntfConstructorHook()->call_trampoline();
235236
if (rakClientInterface)
236237
{
237238
gRakPeer = reinterpret_cast<void*>(rakClientInterface - 0xDDE);

src/RakLua.h

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
#pragma once
22

33
#include "RakLuaBitStream.h"
4+
#include "kthook/kthook.hpp"
45
#include "samp.hpp"
56

67
class RakLua
@@ -12,9 +13,9 @@ class RakLua
1213
private:
1314
eInitState mState = eInitState::NOT_INITIALIZED;
1415

15-
rtdhook_vmt* mVmtHook = nullptr;
16-
rtdhook* mIncomingRpcHandlerHook = nullptr;
17-
rtdhook* mRakClientIntfConstructor = nullptr;
16+
rtdhook_vmt* mVmtHook = nullptr;
17+
rtdhook* mIncomingRpcHandlerHook = nullptr;
18+
kthook::kthook_simple<uintptr_t(*)()>* mRakClientIntfConstructor = nullptr;
1819

1920
struct handlers {
2021
std::vector<event_handler_t> incomingRpc;
@@ -37,7 +38,7 @@ class RakLua
3738
inline rtdhook_vmt* getVmtHook() { return mVmtHook; };
3839
inline handlers& getHandlers() { return mHandlers; };
3940
inline rtdhook* getRpcHook() { return mIncomingRpcHandlerHook; };
40-
inline rtdhook* getIntfConstructorHook() { return mRakClientIntfConstructor; }
41+
inline auto getIntfConstructorHook() { return mRakClientIntfConstructor; }
4142

4243
template <typename... Args>
4344
static bool safeCall(sol::function handler, Args&&... args);
@@ -66,4 +67,4 @@ bool __fastcall handleOutgoingPacket(void* ptr, void*, BitStream* bitStream, Pac
6667
Packet* __fastcall handleIncomingPacket(void* ptr, void*);
6768
bool __fastcall handleOutgoingRpc(void* ptr, void*, int* id, BitStream* bitStream, PacketPriority priority, PacketReliability reliability, char orderingChannel, bool shiftTimestamp);
6869
bool __fastcall handleIncomingRpc(void* ptr, void*, unsigned char* data, int length, PlayerID playerId);
69-
uintptr_t hookRakClientIntfConstructor();
70+
uintptr_t hookRakClientIntfConstructor(const kthook::kthook_simple<uintptr_t(*)()>& hook);

src/libs/kthook

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
Subproject commit bdba30e12a15ffce733428789dcc6b3e014df4a3

src/main.cpp

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -234,7 +234,7 @@ sol::table open(sol::this_state ts)
234234
gRakLua.initialize();
235235

236236
sol::table module = lua.create_table();
237-
module["VERSION"] = 2.1;
237+
module["VERSION"] = 2.11;
238238
module.set_function("getState", &getState);
239239

240240
module.set_function("registerHandler", &registerHandler);
@@ -261,3 +261,7 @@ BOOL WINAPI DllMain(HINSTANCE hinstDLL, DWORD fdwReason, LPVOID lpvReserved)
261261
}
262262
return TRUE;
263263
}
264+
265+
// the dumbest linker error hack
266+
#include "hde/hde32.h"
267+
#include "hde/hde32.c"

src/moonloader-module.vcxproj

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -21,14 +21,14 @@
2121
<PropertyGroup Condition="'$(Configuration)|$(Platform)'=='Release|Win32'" Label="Configuration">
2222
<ConfigurationType>DynamicLibrary</ConfigurationType>
2323
<UseDebugLibraries>false</UseDebugLibraries>
24-
<PlatformToolset>v142</PlatformToolset>
24+
<PlatformToolset>v143</PlatformToolset>
2525
<WholeProgramOptimization>true</WholeProgramOptimization>
2626
<CharacterSet>MultiByte</CharacterSet>
2727
</PropertyGroup>
2828
<PropertyGroup Condition="'$(Configuration)|$(Platform)'=='Debug|Win32'" Label="Configuration">
2929
<ConfigurationType>DynamicLibrary</ConfigurationType>
3030
<UseDebugLibraries>false</UseDebugLibraries>
31-
<PlatformToolset>v142</PlatformToolset>
31+
<PlatformToolset>v143</PlatformToolset>
3232
<WholeProgramOptimization>false</WholeProgramOptimization>
3333
<CharacterSet>MultiByte</CharacterSet>
3434
</PropertyGroup>
@@ -61,7 +61,7 @@
6161
<IntrinsicFunctions>true</IntrinsicFunctions>
6262
<PreprocessorDefinitions>WIN32;NDEBUG;_WINDOWS;_USRDLL;LUA_BUILD_AS_DLL;MODULE_NAME=$(ProjectName);%(PreprocessorDefinitions)</PreprocessorDefinitions>
6363
<PrecompiledHeaderFile>pch.h</PrecompiledHeaderFile>
64-
<AdditionalIncludeDirectories>.\libs\lua;.\libs\sol2;.\libs\samp;.\libs\rtdhook;.\libs\raknet;%(AdditionalIncludeDirectories)</AdditionalIncludeDirectories>
64+
<AdditionalIncludeDirectories>.\libs\lua;.\libs\sol2;.\libs\samp;.\libs\rtdhook;.\libs\raknet;.\libs\kthook\xbyak;.\libs\kthook\include;.\libs\kthook;.\libs\kthook\ktsignal\include;%(AdditionalIncludeDirectories)</AdditionalIncludeDirectories>
6565
<AdditionalOptions>/Zc:threadSafeInit- %(AdditionalOptions)</AdditionalOptions>
6666
<RuntimeLibrary>MultiThreadedDLL</RuntimeLibrary>
6767
<LanguageStandard>stdcpp17</LanguageStandard>
@@ -85,11 +85,11 @@
8585
<IntrinsicFunctions>true</IntrinsicFunctions>
8686
<PreprocessorDefinitions>WIN32;_DEBUG;_WINDOWS;_USRDLL;LUA_BUILD_AS_DLL;MODULE_NAME=$(ProjectName);%(PreprocessorDefinitions)</PreprocessorDefinitions>
8787
<PrecompiledHeaderFile>pch.h</PrecompiledHeaderFile>
88-
<AdditionalIncludeDirectories>.\libs\lua;.\libs\sol2;.\libs\samp;.\libs\rtdhook;.\libs\raknet;%(AdditionalIncludeDirectories)</AdditionalIncludeDirectories>
88+
<AdditionalIncludeDirectories>.\libs\lua;.\libs\sol2;.\libs\samp;.\libs\rtdhook;.\libs\raknet;.\libs\kthook\xbyak;.\libs\kthook\include;.\libs\kthook;.\libs\kthook\ktsignal\include;%(AdditionalIncludeDirectories)</AdditionalIncludeDirectories>
8989
<AdditionalOptions>/Zc:threadSafeInit- %(AdditionalOptions)</AdditionalOptions>
9090
<RuntimeLibrary>MultiThreadedDebugDLL</RuntimeLibrary>
9191
<LanguageStandard>stdcpp17</LanguageStandard>
92-
<LanguageStandard_C>stdc17</LanguageStandard_C>
92+
<LanguageStandard_C>Default</LanguageStandard_C>
9393
<DisableSpecificWarnings>26812</DisableSpecificWarnings>
9494
</ClCompile>
9595
<Link>

0 commit comments

Comments
 (0)