Skip to content

Commit

Permalink
Switch RakClient intf hooks to kthook
Browse files Browse the repository at this point in the history
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...
  • Loading branch information
Northn committed Nov 9, 2022
1 parent f089088 commit 80b28dd
Show file tree
Hide file tree
Showing 6 changed files with 23 additions and 16 deletions.
4 changes: 2 additions & 2 deletions RakLua.lua
Expand Up @@ -11,15 +11,15 @@ local events = {

local addEventHandler_orig = addEventHandler

function hookAddEventHandler(event, func)
local function hookAddEventHandler(event, func)
if events[event] then
RakLuaDll.registerHandler(events[event], func)
else
addEventHandler_orig(event, func)
end
end

function defineSampLuaCompatibility()
local function defineSampLuaCompatibility()
RakLuaDll.defineGlobals()

addEventHandler = hookAddEventHandler
Expand Down
7 changes: 4 additions & 3 deletions src/RakLua.cpp
Expand Up @@ -22,7 +22,8 @@ RakLua::eInitState RakLua::initialize()

mState = eInitState::INITIALIZING;

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

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

uintptr_t hookRakClientIntfConstructor()
uintptr_t hookRakClientIntfConstructor(const kthook::kthook_simple<uintptr_t(*)()> &hook)
{
uintptr_t rakClientInterface = reinterpret_cast<uintptr_t(*)()>(gRakLua.getIntfConstructorHook()->getTrampoline())();
uintptr_t rakClientInterface = gRakLua.getIntfConstructorHook()->call_trampoline();
if (rakClientInterface)
{
gRakPeer = reinterpret_cast<void*>(rakClientInterface - 0xDDE);
Expand Down
11 changes: 6 additions & 5 deletions src/RakLua.h
@@ -1,6 +1,7 @@
#pragma once

#include "RakLuaBitStream.h"
#include "kthook/kthook.hpp"
#include "samp.hpp"

class RakLua
Expand All @@ -12,9 +13,9 @@ class RakLua
private:
eInitState mState = eInitState::NOT_INITIALIZED;

rtdhook_vmt* mVmtHook = nullptr;
rtdhook* mIncomingRpcHandlerHook = nullptr;
rtdhook* mRakClientIntfConstructor = nullptr;
rtdhook_vmt* mVmtHook = nullptr;
rtdhook* mIncomingRpcHandlerHook = nullptr;
kthook::kthook_simple<uintptr_t(*)()>* mRakClientIntfConstructor = nullptr;

struct handlers {
std::vector<event_handler_t> incomingRpc;
Expand All @@ -37,7 +38,7 @@ class RakLua
inline rtdhook_vmt* getVmtHook() { return mVmtHook; };
inline handlers& getHandlers() { return mHandlers; };
inline rtdhook* getRpcHook() { return mIncomingRpcHandlerHook; };
inline rtdhook* getIntfConstructorHook() { return mRakClientIntfConstructor; }
inline auto getIntfConstructorHook() { return mRakClientIntfConstructor; }

template <typename... Args>
static bool safeCall(sol::function handler, Args&&... args);
Expand Down Expand Up @@ -66,4 +67,4 @@ bool __fastcall handleOutgoingPacket(void* ptr, void*, BitStream* bitStream, Pac
Packet* __fastcall handleIncomingPacket(void* ptr, void*);
bool __fastcall handleOutgoingRpc(void* ptr, void*, int* id, BitStream* bitStream, PacketPriority priority, PacketReliability reliability, char orderingChannel, bool shiftTimestamp);
bool __fastcall handleIncomingRpc(void* ptr, void*, unsigned char* data, int length, PlayerID playerId);
uintptr_t hookRakClientIntfConstructor();
uintptr_t hookRakClientIntfConstructor(const kthook::kthook_simple<uintptr_t(*)()>& hook);
1 change: 1 addition & 0 deletions src/libs/kthook
Submodule kthook added at bdba30
6 changes: 5 additions & 1 deletion src/main.cpp
Expand Up @@ -234,7 +234,7 @@ sol::table open(sol::this_state ts)
gRakLua.initialize();

sol::table module = lua.create_table();
module["VERSION"] = 2.1;
module["VERSION"] = 2.11;
module.set_function("getState", &getState);

module.set_function("registerHandler", &registerHandler);
Expand All @@ -261,3 +261,7 @@ BOOL WINAPI DllMain(HINSTANCE hinstDLL, DWORD fdwReason, LPVOID lpvReserved)
}
return TRUE;
}

// the dumbest linker error hack
#include "hde/hde32.h"
#include "hde/hde32.c"
10 changes: 5 additions & 5 deletions src/moonloader-module.vcxproj
Expand Up @@ -21,14 +21,14 @@
<PropertyGroup Condition="'$(Configuration)|$(Platform)'=='Release|Win32'" Label="Configuration">
<ConfigurationType>DynamicLibrary</ConfigurationType>
<UseDebugLibraries>false</UseDebugLibraries>
<PlatformToolset>v142</PlatformToolset>
<PlatformToolset>v143</PlatformToolset>
<WholeProgramOptimization>true</WholeProgramOptimization>
<CharacterSet>MultiByte</CharacterSet>
</PropertyGroup>
<PropertyGroup Condition="'$(Configuration)|$(Platform)'=='Debug|Win32'" Label="Configuration">
<ConfigurationType>DynamicLibrary</ConfigurationType>
<UseDebugLibraries>false</UseDebugLibraries>
<PlatformToolset>v142</PlatformToolset>
<PlatformToolset>v143</PlatformToolset>
<WholeProgramOptimization>false</WholeProgramOptimization>
<CharacterSet>MultiByte</CharacterSet>
</PropertyGroup>
Expand Down Expand Up @@ -61,7 +61,7 @@
<IntrinsicFunctions>true</IntrinsicFunctions>
<PreprocessorDefinitions>WIN32;NDEBUG;_WINDOWS;_USRDLL;LUA_BUILD_AS_DLL;MODULE_NAME=$(ProjectName);%(PreprocessorDefinitions)</PreprocessorDefinitions>
<PrecompiledHeaderFile>pch.h</PrecompiledHeaderFile>
<AdditionalIncludeDirectories>.\libs\lua;.\libs\sol2;.\libs\samp;.\libs\rtdhook;.\libs\raknet;%(AdditionalIncludeDirectories)</AdditionalIncludeDirectories>
<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>
<AdditionalOptions>/Zc:threadSafeInit- %(AdditionalOptions)</AdditionalOptions>
<RuntimeLibrary>MultiThreadedDLL</RuntimeLibrary>
<LanguageStandard>stdcpp17</LanguageStandard>
Expand All @@ -85,11 +85,11 @@
<IntrinsicFunctions>true</IntrinsicFunctions>
<PreprocessorDefinitions>WIN32;_DEBUG;_WINDOWS;_USRDLL;LUA_BUILD_AS_DLL;MODULE_NAME=$(ProjectName);%(PreprocessorDefinitions)</PreprocessorDefinitions>
<PrecompiledHeaderFile>pch.h</PrecompiledHeaderFile>
<AdditionalIncludeDirectories>.\libs\lua;.\libs\sol2;.\libs\samp;.\libs\rtdhook;.\libs\raknet;%(AdditionalIncludeDirectories)</AdditionalIncludeDirectories>
<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>
<AdditionalOptions>/Zc:threadSafeInit- %(AdditionalOptions)</AdditionalOptions>
<RuntimeLibrary>MultiThreadedDebugDLL</RuntimeLibrary>
<LanguageStandard>stdcpp17</LanguageStandard>
<LanguageStandard_C>stdc17</LanguageStandard_C>
<LanguageStandard_C>Default</LanguageStandard_C>
<DisableSpecificWarnings>26812</DisableSpecificWarnings>
</ClCompile>
<Link>
Expand Down

0 comments on commit 80b28dd

Please sign in to comment.