From 80b28ddbf930c7982416f5979873bd2486c7f0d5 Mon Sep 17 00:00:00 2001 From: Northn Date: Wed, 9 Nov 2022 14:58:14 +0400 Subject: [PATCH] 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... --- RakLua.lua | 4 ++-- src/RakLua.cpp | 7 ++++--- src/RakLua.h | 11 ++++++----- src/libs/kthook | 1 + src/main.cpp | 6 +++++- src/moonloader-module.vcxproj | 10 +++++----- 6 files changed, 23 insertions(+), 16 deletions(-) create mode 160000 src/libs/kthook diff --git a/RakLua.lua b/RakLua.lua index 2bde607..c31f247 100644 --- a/RakLua.lua +++ b/RakLua.lua @@ -11,7 +11,7 @@ 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 @@ -19,7 +19,7 @@ function hookAddEventHandler(event, func) end end -function defineSampLuaCompatibility() +local function defineSampLuaCompatibility() RakLuaDll.defineGlobals() addEventHandler = hookAddEventHandler diff --git a/src/RakLua.cpp b/src/RakLua.cpp index fbbe939..5f9affb 100644 --- a/src/RakLua.cpp +++ b/src/RakLua.cpp @@ -22,7 +22,8 @@ RakLua::eInitState RakLua::initialize() mState = eInitState::INITIALIZING; - mRakClientIntfConstructor = new rtdhook(sampGetRakClientIntfConstructorPtr(), &hookRakClientIntfConstructor, 7); + mRakClientIntfConstructor = new kthook::kthook_simple(sampGetRakClientIntfConstructorPtr()); + mRakClientIntfConstructor->set_cb(hookRakClientIntfConstructor); mRakClientIntfConstructor->install(); returnState: @@ -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 &hook) { - uintptr_t rakClientInterface = reinterpret_cast(gRakLua.getIntfConstructorHook()->getTrampoline())(); + uintptr_t rakClientInterface = gRakLua.getIntfConstructorHook()->call_trampoline(); if (rakClientInterface) { gRakPeer = reinterpret_cast(rakClientInterface - 0xDDE); diff --git a/src/RakLua.h b/src/RakLua.h index 9fb10b6..16810a9 100644 --- a/src/RakLua.h +++ b/src/RakLua.h @@ -1,6 +1,7 @@ #pragma once #include "RakLuaBitStream.h" +#include "kthook/kthook.hpp" #include "samp.hpp" class RakLua @@ -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* mRakClientIntfConstructor = nullptr; struct handlers { std::vector incomingRpc; @@ -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 static bool safeCall(sol::function handler, Args&&... args); @@ -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& hook); diff --git a/src/libs/kthook b/src/libs/kthook new file mode 160000 index 0000000..bdba30e --- /dev/null +++ b/src/libs/kthook @@ -0,0 +1 @@ +Subproject commit bdba30e12a15ffce733428789dcc6b3e014df4a3 diff --git a/src/main.cpp b/src/main.cpp index fce2954..9dc2c91 100644 --- a/src/main.cpp +++ b/src/main.cpp @@ -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", ®isterHandler); @@ -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" diff --git a/src/moonloader-module.vcxproj b/src/moonloader-module.vcxproj index 6bca95c..f42a9f2 100644 --- a/src/moonloader-module.vcxproj +++ b/src/moonloader-module.vcxproj @@ -21,14 +21,14 @@ DynamicLibrary false - v142 + v143 true MultiByte DynamicLibrary false - v142 + v143 false MultiByte @@ -61,7 +61,7 @@ true WIN32;NDEBUG;_WINDOWS;_USRDLL;LUA_BUILD_AS_DLL;MODULE_NAME=$(ProjectName);%(PreprocessorDefinitions) pch.h - .\libs\lua;.\libs\sol2;.\libs\samp;.\libs\rtdhook;.\libs\raknet;%(AdditionalIncludeDirectories) + .\libs\lua;.\libs\sol2;.\libs\samp;.\libs\rtdhook;.\libs\raknet;.\libs\kthook\xbyak;.\libs\kthook\include;.\libs\kthook;.\libs\kthook\ktsignal\include;%(AdditionalIncludeDirectories) /Zc:threadSafeInit- %(AdditionalOptions) MultiThreadedDLL stdcpp17 @@ -85,11 +85,11 @@ true WIN32;_DEBUG;_WINDOWS;_USRDLL;LUA_BUILD_AS_DLL;MODULE_NAME=$(ProjectName);%(PreprocessorDefinitions) pch.h - .\libs\lua;.\libs\sol2;.\libs\samp;.\libs\rtdhook;.\libs\raknet;%(AdditionalIncludeDirectories) + .\libs\lua;.\libs\sol2;.\libs\samp;.\libs\rtdhook;.\libs\raknet;.\libs\kthook\xbyak;.\libs\kthook\include;.\libs\kthook;.\libs\kthook\ktsignal\include;%(AdditionalIncludeDirectories) /Zc:threadSafeInit- %(AdditionalOptions) MultiThreadedDebugDLL stdcpp17 - stdc17 + Default 26812