From 34d91789c40268c6409ed7c9213894bb3d4aca4c Mon Sep 17 00:00:00 2001 From: Arkshine Date: Fri, 16 Jan 2015 22:27:24 +0100 Subject: [PATCH] Cvars: Hook Cvar_DirectSet from engine library --- amxmodx/AMBuilder | 3 + amxmodx/cvars.cpp | 70 +++++++++++++++++++++++ amxmodx/cvars.h | 19 ++++++ amxmodx/meta_api.cpp | 3 + amxmodx/msvc12/amxmodx_mm.vcxproj | 15 +++-- amxmodx/msvc12/amxmodx_mm.vcxproj.filters | 30 ++++++++++ public/memtools/CDetour/detours.h | 4 +- 7 files changed, 139 insertions(+), 5 deletions(-) create mode 100644 amxmodx/cvars.cpp create mode 100644 amxmodx/cvars.h diff --git a/amxmodx/AMBuilder b/amxmodx/AMBuilder index 1665ec89..00a8c2b5 100644 --- a/amxmodx/AMBuilder +++ b/amxmodx/AMBuilder @@ -96,6 +96,9 @@ binary.sources = [ 'stackstructs.cpp', 'CTextParsers.cpp', 'textparse.cpp', + 'cvars.cpp', + '../public/memtools/CDetour/detours.cpp', + '../public/memtools/CDetour/asm/asm.c', ] if builder.target_platform == 'windows': diff --git a/amxmodx/cvars.cpp b/amxmodx/cvars.cpp new file mode 100644 index 00000000..a6eee812 --- /dev/null +++ b/amxmodx/cvars.cpp @@ -0,0 +1,70 @@ +// vim: set ts=4 sw=4 tw=99 noet: +// +// AMX Mod X, based on AMX Mod by Aleksander Naszko ("OLO"). +// Copyright (C) The AMX Mod X Development Team. +// +// This software is licensed under the GNU General Public License, version 3 or higher. +// Additional exceptions apply. For full license details, see LICENSE.txt or visit: +// https://alliedmods.net/amxmodx-license + +#include "cvars.h" +#include "amxmodx.h" +#include + +CDetour *Cvar_DirectSetDetour; + +DETOUR_DECL_STATIC2(Cvar_DirectSet, void, struct cvar_s*, var, const char*, value) +{ + printf("Cvar_DirectSet - %s -> %s\n", var->name, value); + + DETOUR_STATIC_CALL(Cvar_DirectSet)(var, value); +} + +void CreateCvarHook(void) +{ + // void PF_Cvar_DirectSet(struct cvar_s *var, const char *value) // = pfnCvar_DirectSet + // { + // Cvar_DirectSet(var, value); // <- We want to hook this. + // } + + byte *baseAddress = (byte *)g_engfuncs.pfnCvar_DirectSet; + uintptr_t *functionAddress = nullptr; + +#if defined(WIN32) + // 55 push ebp + // 8B EC mov ebp, esp + // 8B 45 0C mov eax, [ebp+arg_4] + // 8B 4D 08 mov ecx, [ebp+arg_0] + // 50 push eax + // 51 push ecx + // E8 XX XX XX XX call Cvar_DirectSet + const byte opcodeJump = 0xE8; +#else + // E9 XX XX XX XX jmp Cvar_DirectSet + const byte opcodeJump = 0xE9; +#endif + + const byte opcodeJumpSize = 5; + const byte opcodeJumpByteSize = 1; + + const int maxBytesLimit = 20; + + for (size_t i = 0; i < maxBytesLimit; ++i, ++baseAddress) + { + if (*baseAddress == opcodeJump) + { + functionAddress = (uintptr_t *)(&baseAddress[opcodeJumpSize] + *(uintptr_t *)&baseAddress[opcodeJumpByteSize]); + break; + } + } + + if (functionAddress) + { + CDetour *Cvar_DirectSetDetour = DETOUR_CREATE_STATIC_FIXED(Cvar_DirectSet, (void *)functionAddress); + + if (Cvar_DirectSetDetour) + { + Cvar_DirectSetDetour->EnableDetour(); + } + } +} \ No newline at end of file diff --git a/amxmodx/cvars.h b/amxmodx/cvars.h new file mode 100644 index 00000000..a495f643 --- /dev/null +++ b/amxmodx/cvars.h @@ -0,0 +1,19 @@ +// vim: set ts=4 sw=4 tw=99 noet: +// +// AMX Mod X, based on AMX Mod by Aleksander Naszko ("OLO"). +// Copyright (C) The AMX Mod X Development Team. +// +// This software is licensed under the GNU General Public License, version 3 or higher. +// Additional exceptions apply. For full license details, see LICENSE.txt or visit: +// https://alliedmods.net/amxmodx-license + +#ifndef CVARS_H +#define CVARS_H + +class CDetour; + +void CreateCvarHook(void); + +extern CDetour *Cvar_DirectSetDetour; + +#endif // CVARS_H diff --git a/amxmodx/meta_api.cpp b/amxmodx/meta_api.cpp index 2fe486fa..6cfecb41 100755 --- a/amxmodx/meta_api.cpp +++ b/amxmodx/meta_api.cpp @@ -31,6 +31,7 @@ #include "trie_natives.h" #include "CDataPack.h" #include "textparse.h" +#include "cvars.h" plugin_info_t Plugin_info = { @@ -1488,6 +1489,8 @@ C_DLLEXPORT int Meta_Attach(PLUG_LOADTIME now, META_FUNCTIONS *pFunctionTable, m GET_HOOK_TABLES(PLID, &g_pEngTable, NULL, NULL); FlagMan.SetFile("cmdaccess.ini"); + + CreateCvarHook(); return (TRUE); } diff --git a/amxmodx/msvc12/amxmodx_mm.vcxproj b/amxmodx/msvc12/amxmodx_mm.vcxproj index 80d6961c..ca876873 100644 --- a/amxmodx/msvc12/amxmodx_mm.vcxproj +++ b/amxmodx/msvc12/amxmodx_mm.vcxproj @@ -96,7 +96,7 @@ Disabled - ..\;..\..\public;..\..\public\sdk;..\..\public\amtl;$(METAMOD)\metamod;$(HLSDK)\common;$(HLSDK)\engine;$(HLSDK)\dlls;$(HLSDK)\pm_shared;$(HLSDK)\public;%(AdditionalIncludeDirectories) + ..\;..\..\public;..\..\public\memtools;..\..\public\sdk;..\..\public\amtl;$(METAMOD)\metamod;$(HLSDK)\common;$(HLSDK)\engine;$(HLSDK)\dlls;$(HLSDK)\pm_shared;$(HLSDK)\public;%(AdditionalIncludeDirectories) WIN32;_DEBUG;_WINDOWS;_USRDLL;amxmodx_EXPORTS;PAWN_CELL_SIZE=32;ASM32;JIT;_CRT_SECURE_NO_DEPRECATE;HAVE_STDINT_H;%(PreprocessorDefinitions) EnableFastChecks MultiThreadedDebug @@ -149,7 +149,7 @@ true Speed true - ..\;..\..\public;..\..\public\sdk;..\..\public\amtl;$(METAMOD)\metamod;$(HLSDK)\common;$(HLSDK)\engine;$(HLSDK)\dlls;$(HLSDK)\pm_shared;$(HLSDK)\public;%(AdditionalIncludeDirectories) + ..\;..\..\public;..\..\public\memtools;..\..\public\sdk;..\..\public\amtl;$(METAMOD)\metamod;$(HLSDK)\common;$(HLSDK)\engine;$(HLSDK)\dlls;$(HLSDK)\pm_shared;$(HLSDK)\public;%(AdditionalIncludeDirectories) WIN32;NDEBUG;_WINDOWS;_USRDLL;amxmodx_EXPORTS;JIT;ASM32;PAWN_CELL_SIZE=32;_CRT_SECURE_NO_DEPRECATE;HAVE_STDINT_H;%(PreprocessorDefinitions) false true @@ -200,7 +200,7 @@ Disabled - ..\;..\..\public;..\..\public\sdk;..\..\public\amtl;$(METAMOD)\metamod;$(HLSDK)\common;$(HLSDK)\engine;$(HLSDK)\dlls;$(HLSDK)\pm_shared;$(HLSDK)\public;%(AdditionalIncludeDirectories) + ..\;..\..\public;..\..\public\memtools;..\..\public\sdk;..\..\public\amtl;$(METAMOD)\metamod;$(HLSDK)\common;$(HLSDK)\engine;$(HLSDK)\dlls;$(HLSDK)\pm_shared;$(HLSDK)\public;%(AdditionalIncludeDirectories) WIN32;_DEBUG;_WINDOWS;_USRDLL;amxmodx_EXPORTS;PAWN_CELL_SIZE=32;ASM32;JIT;BINLOG_ENABLED;_CRT_SECURE_NO_DEPRECATE;HAVE_STDINT_H;%(PreprocessorDefinitions) EnableFastChecks MultiThreaded @@ -252,7 +252,7 @@ true Speed true - ..\;..\..\public;..\..\public\sdk;..\..\public\amtl;$(METAMOD)\metamod;$(HLSDK)\common;$(HLSDK)\engine;$(HLSDK)\dlls;$(HLSDK)\pm_shared;$(HLSDK)\public;%(AdditionalIncludeDirectories) + ..\;..\..\public;..\..\public\memtools;..\..\public\sdk;..\..\public\amtl;$(METAMOD)\metamod;$(HLSDK)\common;$(HLSDK)\engine;$(HLSDK)\dlls;$(HLSDK)\pm_shared;$(HLSDK)\public;%(AdditionalIncludeDirectories) WIN32;NDEBUG;_WINDOWS;HAVE_STDINT_H;_USRDLL;amxmodx_EXPORTS;JIT;ASM32;PAWN_CELL_SIZE=32;BINLOG_ENABLED;_CRT_SECURE_NO_DEPRECATE;%(PreprocessorDefinitions) false true @@ -292,6 +292,8 @@ + + @@ -317,6 +319,7 @@ + @@ -360,6 +363,9 @@ + + + @@ -383,6 +389,7 @@ + diff --git a/amxmodx/msvc12/amxmodx_mm.vcxproj.filters b/amxmodx/msvc12/amxmodx_mm.vcxproj.filters index db6aeada..adabf960 100644 --- a/amxmodx/msvc12/amxmodx_mm.vcxproj.filters +++ b/amxmodx/msvc12/amxmodx_mm.vcxproj.filters @@ -25,6 +25,15 @@ {4022451d-eb5f-4f14-b8d8-2ce23fec6e59} + + {a1f7babf-acb1-4a06-92e9-e8a411e1f02a} + + + {8b35d490-2b01-4997-ba02-5e2cfd9c9017} + + + {64a22cd4-3715-45de-8af2-e54017733be6} + @@ -183,6 +192,15 @@ SDK + + Source Files + + + Memtools\CDetour + + + Memtools\CDetour\asm + @@ -329,6 +347,18 @@ SDK + + Header Files + + + Memtools\CDetour + + + Memtools\CDetour + + + Memtools\CDetour\asm + diff --git a/public/memtools/CDetour/detours.h b/public/memtools/CDetour/detours.h index 89c2b238..b0335191 100644 --- a/public/memtools/CDetour/detours.h +++ b/public/memtools/CDetour/detours.h @@ -32,10 +32,12 @@ #ifndef _INCLUDE_SOURCEMOD_DETOURS_H_ #define _INCLUDE_SOURCEMOD_DETOURS_H_ -#include "amxxmodule.h" //#include //#include #include "detourhelpers.h" +#include + +typedef int32_t cell; /** * CDetours class for SourceMod Extensions by pRED*