mirror of
https://github.com/alliedmodders/amxmodx.git
synced 2024-12-25 14:25:38 +03:00
Cstrike: Refactor code to seperate detours for readability/maintainability + Fix OSX symbols.
This commit is contained in:
parent
c96456503c
commit
f1e206fde6
@ -145,14 +145,16 @@
|
|||||||
#define OFFSET_HOSTAGEID 516 + EXTRAOFFSET // +29
|
#define OFFSET_HOSTAGEID 516 + EXTRAOFFSET // +29
|
||||||
#endif
|
#endif
|
||||||
|
|
||||||
#if defined __linux__
|
#if defined(__linux__)
|
||||||
#define CS_DETOURCOPYBYTES_CLIENTCOMMAND 6
|
#define CS_DETOURCOPYBYTES_CLIENTCOMMAND 6
|
||||||
#define CS_SYM_USEBOTARGS "UseBotArgs"
|
#define CS_IDENT_USEBOTARGS "UseBotArgs"
|
||||||
#define CS_SYM_BOTARGS "BotArgs"
|
#define CS_IDENT_BOTARGS "BotArgs"
|
||||||
#elif defined __APPLE__
|
#define CS_IDENT_HIDDEN_STATE false
|
||||||
|
#elif defined(__APPLE__)
|
||||||
#define CS_DETOURCOPYBYTES_CLIENTCOMMAND 5
|
#define CS_DETOURCOPYBYTES_CLIENTCOMMAND 5
|
||||||
#define CS_SYM_USEBOTARGS "UseBotArgs"
|
#define CS_IDENT_USEBOTARGS "UseBotArgs"
|
||||||
#define CS_SYM_BOTARGS "BotArgs"
|
#define CS_IDENT_BOTARGS "BotArgs"
|
||||||
|
#define CS_IDENT_HIDDEN_STATE true
|
||||||
#else
|
#else
|
||||||
#define CS_DETOURCOPYBYTES_CLIENTCOMMAND 6
|
#define CS_DETOURCOPYBYTES_CLIENTCOMMAND 6
|
||||||
#define CS_CLICMD_OFFS_USEBOTARGS 2
|
#define CS_CLICMD_OFFS_USEBOTARGS 2
|
||||||
@ -163,17 +165,20 @@
|
|||||||
* CS_OnBuy forward
|
* CS_OnBuy forward
|
||||||
*/
|
*/
|
||||||
#if defined(__linux__)
|
#if defined(__linux__)
|
||||||
#define CS_SYM_CANBUYTHIS "_Z10CanBuyThisP11CBasePlayeri"
|
#define CS_IDENT_CANBUYTHIS "_Z10CanBuyThisP11CBasePlayeri"
|
||||||
#define CS_SYM_BUYITEM "_Z7BuyItemP11CBasePlayeri"
|
#define CS_IDENT_BUYITEM "_Z7BuyItemP11CBasePlayeri"
|
||||||
#define CS_SYM_BUYGUNAMMO "_Z10BuyGunAmmoR11CBasePlayerR15CBasePlayerItemb"
|
#define CS_IDENT_BUYGUNAMMO "_Z10BuyGunAmmoR11CBasePlayerR15CBasePlayerItemb"
|
||||||
|
#define CS_IDENT_HIDDEN_STATE false
|
||||||
#elif defined(__APPLE__)
|
#elif defined(__APPLE__)
|
||||||
#define CS_SYM_CANBUYTHIS "__Z10CanBuyThisP11CBasePlayeri"
|
#define CS_IDENT_CANBUYTHIS "_Z10CanBuyThisP11CBasePlayeri"
|
||||||
#define CS_SYM_BUYITEM "__Z7BuyItemP11CBasePlayeri"
|
#define CS_IDENT_BUYITEM "_Z7BuyItemP11CBasePlayeri"
|
||||||
#define CS_SYM_BUYGUNAMMO "__Z10BuyGunAmmoR11CBasePlayerR15CBasePlayerItemb"
|
#define CS_IDENT_BUYGUNAMMO "_Z10BuyGunAmmoR11CBasePlayerR15CBasePlayerItemb"
|
||||||
|
#define CS_IDENT_HIDDEN_STATE true
|
||||||
#elif defined(WIN32)
|
#elif defined(WIN32)
|
||||||
#define CS_SIG_CANBUYTHIS "\x53\x8B\x2A\x2A\x2A\x2A\x2A\x56\x8B\x2A\x2A\x2A\x57"
|
#define CS_IDENT_CANBUYTHIS "\\x53\\x8B\\x2A\\x2A\\x2A\\x2A\\x2A\\x56\\x8B\\x2A\\x2A\\x2A\\x57"
|
||||||
#define CS_SIG_BUYITEM "\x53\x56\x8B\x2A\x2A\x2A\xBB\x2A\x2A\x2A\x2A\x57\x53"
|
#define CS_IDENT_BUYITEM "\\x53\\x56\\x8B\\x2A\\x2A\\x2A\\xBB\\x2A\\x2A\\x2A\\x2A\\x57\\x53"
|
||||||
#define CS_SIG_BUYGUNAMMO "\x56\x57\x8B\x2A\x2A\x2A\x6A\x2A\x8B\x2A\xE8\x2A\x2A\x2A\x2A\x84\x2A\x0F"
|
#define CS_IDENT_BUYGUNAMMO "\\x56\\x57\\x8B\\x2A\\x2A\\x2A\\x6A\\x2A\\x8B\\x2A\\xE8\\x2A\\x2A\\x2A\\x2A\\x84\\x2A\\x0F"
|
||||||
|
#define CS_IDENT_HIDDEN_STATE false
|
||||||
#endif
|
#endif
|
||||||
|
|
||||||
#define CSI_P228 CSW_P228
|
#define CSI_P228 CSW_P228
|
||||||
|
@ -32,14 +32,10 @@
|
|||||||
*/
|
*/
|
||||||
#include "CstrikeDatas.h"
|
#include "CstrikeDatas.h"
|
||||||
#include "CstrikeUtils.h"
|
#include "CstrikeUtils.h"
|
||||||
#include <MemoryUtils.h>
|
|
||||||
#include "CDetour/detours.h"
|
#include "CDetour/detours.h"
|
||||||
|
|
||||||
#if defined(__APPLE__)
|
void CtrlDetours_ClientCommand(bool set);
|
||||||
#include <mach-o/nlist.h>
|
void CtrlDetours_BuyCommands(bool set);
|
||||||
#endif
|
|
||||||
|
|
||||||
void CtrlDetours(bool set);
|
|
||||||
|
|
||||||
int g_CSCliCmdFwd = -1;
|
int g_CSCliCmdFwd = -1;
|
||||||
int g_CSBuyCmdFwd = -1;
|
int g_CSBuyCmdFwd = -1;
|
||||||
@ -55,12 +51,18 @@ CDetour *g_BuyGunAmmoDetour = NULL;
|
|||||||
|
|
||||||
void InitializeHacks()
|
void InitializeHacks()
|
||||||
{
|
{
|
||||||
CtrlDetours(true);
|
#if defined AMD64
|
||||||
|
#error UNSUPPORTED
|
||||||
|
#endif
|
||||||
|
|
||||||
|
CtrlDetours_ClientCommand(true);
|
||||||
|
CtrlDetours_BuyCommands(true);
|
||||||
}
|
}
|
||||||
|
|
||||||
void ShutdownHacks()
|
void ShutdownHacks()
|
||||||
{
|
{
|
||||||
CtrlDetours(false);
|
CtrlDetours_ClientCommand(false);
|
||||||
|
CtrlDetours_BuyCommands(false);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
@ -136,68 +138,47 @@ DETOUR_DECL_STATIC3(BuyGunAmmo, bool, void*, pvPlayer, void*, pvWeapon, bool, bB
|
|||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
void CtrlDetours(bool set)
|
void CtrlDetours_ClientCommand(bool set)
|
||||||
{
|
{
|
||||||
#if defined AMD64
|
|
||||||
#error UNSUPPORTED
|
|
||||||
#endif
|
|
||||||
|
|
||||||
if (set)
|
if (set)
|
||||||
{
|
{
|
||||||
char libName[256];
|
|
||||||
uintptr_t base;
|
|
||||||
|
|
||||||
void *target = (void *)MDLL_ClientCommand;
|
void *target = (void *)MDLL_ClientCommand;
|
||||||
|
|
||||||
if (!g_MemUtils.GetLibraryOfAddress(target, libName, sizeof(libName), &base))
|
|
||||||
{
|
|
||||||
return;
|
|
||||||
}
|
|
||||||
|
|
||||||
void *canBuyThisAddress = NULL;
|
|
||||||
void *buyItemAddress = NULL;
|
|
||||||
void *buyGunAmmoAddress = NULL;
|
|
||||||
|
|
||||||
#if defined(WIN32)
|
#if defined(WIN32)
|
||||||
|
|
||||||
canBuyThisAddress = g_MemUtils.DecodeAndFindPattern(target, CS_SIG_CANBUYTHIS);
|
|
||||||
buyItemAddress = g_MemUtils.DecodeAndFindPattern(target, CS_SIG_BUYITEM);
|
|
||||||
buyGunAmmoAddress = g_MemUtils.DecodeAndFindPattern(target, CS_SIG_BUYGUNAMMO);
|
|
||||||
|
|
||||||
g_UseBotArgs = *(int **)((unsigned char *)target + CS_CLICMD_OFFS_USEBOTARGS);
|
g_UseBotArgs = *(int **)((unsigned char *)target + CS_CLICMD_OFFS_USEBOTARGS);
|
||||||
g_BotArgs = (const char **)*(const char **)((unsigned char *)target + CS_CLICMD_OFFS_BOTARGS);
|
g_BotArgs = (const char **)*(const char **)((unsigned char *)target + CS_CLICMD_OFFS_BOTARGS);
|
||||||
|
|
||||||
#elif defined(__linux__) || defined(__APPLE__)
|
#elif defined(__linux__) || defined(__APPLE__)
|
||||||
|
|
||||||
Dl_info info;
|
g_UseBotArgs = (int *)UTIL_FindAddressFromEntry(CS_IDENT_USEBOTARGS, CS_IDENT_HIDDEN_STATE);
|
||||||
void *handle = NULL;
|
g_BotArgs = (const char **)UTIL_FindAddressFromEntry(CS_IDENT_BOTARGS, CS_IDENT_HIDDEN_STATE);
|
||||||
|
|
||||||
if (dladdr(target, &info) == 0) || (handle = dlopen(info.dli_fname, RTLD_NOW)) == NULL)
|
|
||||||
{
|
|
||||||
return;
|
|
||||||
}
|
|
||||||
|
|
||||||
canBuyThisAddress = g_MemUtils.ResolveSymbol(handle, CS_SYM_CANBUYTHIS);
|
|
||||||
buyItemAddress = g_MemUtils.ResolveSymbol(handle, CS_SYM_BUYITEM);
|
|
||||||
buyGunAmmoAddress = g_MemUtils.ResolveSymbol(handle, CS_SYM_BUYGUNAMMO);
|
|
||||||
|
|
||||||
g_UseBotArgs = (int *)g_MemUtils.ResolveSymbol(handle, CS_SYM_USEBOTARGS);
|
|
||||||
g_BotArgs = (const char **)g_MemUtils.ResolveSymbol(handle, CS_SYM_BOTARGS);
|
|
||||||
|
|
||||||
dlclose(handle);
|
|
||||||
|
|
||||||
#endif
|
#endif
|
||||||
g_ClientCommandDetour = DETOUR_CREATE_STATIC_FIXED(C_ClientCommand, target);
|
g_ClientCommandDetour = DETOUR_CREATE_STATIC_FIXED(C_ClientCommand, target);
|
||||||
g_CanBuyThisDetour = DETOUR_CREATE_STATIC_FIXED(CanBuyThis, canBuyThisAddress);
|
|
||||||
g_BuyItemDetour = DETOUR_CREATE_STATIC_FIXED(BuyItem, buyItemAddress);
|
|
||||||
g_BuyGunAmmoDetour = DETOUR_CREATE_STATIC_FIXED(BuyGunAmmo, buyGunAmmoAddress);
|
|
||||||
|
|
||||||
if (g_ClientCommandDetour != NULL)
|
if (g_ClientCommandDetour != NULL)
|
||||||
g_ClientCommandDetour->EnableDetour();
|
g_ClientCommandDetour->EnableDetour();
|
||||||
else
|
else
|
||||||
{
|
MF_Log("No Client Commands detour could be initialized - Disabled Client Command forward.");
|
||||||
MF_Log("No Client Commands detours could be initialized - Disabled Client Command forward.");
|
}
|
||||||
}
|
else
|
||||||
|
{
|
||||||
|
g_ClientCommandDetour->Destroy();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
void CtrlDetours_BuyCommands(bool set)
|
||||||
|
{
|
||||||
|
if (set)
|
||||||
|
{
|
||||||
|
void *canBuyThisAddress = UTIL_FindAddressFromEntry(CS_IDENT_CANBUYTHIS, CS_IDENT_HIDDEN_STATE);
|
||||||
|
void *buyItemAddress = UTIL_FindAddressFromEntry(CS_IDENT_BUYITEM, CS_IDENT_HIDDEN_STATE);
|
||||||
|
void *buyGunAmmoAddress = UTIL_FindAddressFromEntry(CS_IDENT_BUYGUNAMMO, CS_IDENT_HIDDEN_STATE);
|
||||||
|
|
||||||
|
g_CanBuyThisDetour = DETOUR_CREATE_STATIC_FIXED(CanBuyThis, canBuyThisAddress);
|
||||||
|
g_BuyItemDetour = DETOUR_CREATE_STATIC_FIXED(BuyItem, buyItemAddress);
|
||||||
|
g_BuyGunAmmoDetour = DETOUR_CREATE_STATIC_FIXED(BuyGunAmmo, buyGunAmmoAddress);
|
||||||
|
|
||||||
if (g_CanBuyThisDetour != NULL && g_BuyItemDetour != NULL && g_BuyGunAmmoDetour != NULL)
|
if (g_CanBuyThisDetour != NULL && g_BuyItemDetour != NULL && g_BuyGunAmmoDetour != NULL)
|
||||||
{
|
{
|
||||||
@ -212,9 +193,8 @@ void CtrlDetours(bool set)
|
|||||||
}
|
}
|
||||||
else
|
else
|
||||||
{
|
{
|
||||||
g_CanBuyThisDetour->Destroy();
|
|
||||||
g_BuyItemDetour->Destroy();
|
g_BuyItemDetour->Destroy();
|
||||||
g_BuyGunAmmoDetour->Destroy();
|
g_BuyGunAmmoDetour->Destroy();
|
||||||
g_ClientCommandDetour->Destroy();
|
g_ClientCommandDetour->Destroy();
|
||||||
}
|
}
|
||||||
}
|
}
|
@ -31,6 +31,7 @@
|
|||||||
* version.
|
* version.
|
||||||
*/
|
*/
|
||||||
#include "amxxmodule.h"
|
#include "amxxmodule.h"
|
||||||
|
#include "MemoryUtils.h"
|
||||||
|
|
||||||
bool UTIL_IsPlayer(AMX* amx, edict_t* pPlayer)
|
bool UTIL_IsPlayer(AMX* amx, edict_t* pPlayer)
|
||||||
{
|
{
|
||||||
@ -59,4 +60,63 @@ void UTIL_TextMsg_Generic(edict_t* pPlayer, const char* message)
|
|||||||
#define HUD_PRINTRADIO 5
|
#define HUD_PRINTRADIO 5
|
||||||
However both 1 and 2 seems to go to console with Steam CS.
|
However both 1 and 2 seems to go to console with Steam CS.
|
||||||
*/
|
*/
|
||||||
|
}
|
||||||
|
|
||||||
|
void *UTIL_FindAddressFromEntry(const char *entry, bool isHidden, const char *library)
|
||||||
|
{
|
||||||
|
void *addressInBase = NULL;
|
||||||
|
void *finalAddress;
|
||||||
|
|
||||||
|
if (strcmp(library, "mod") == 0)
|
||||||
|
{
|
||||||
|
addressInBase = (void *)MDLL_Spawn;
|
||||||
|
}
|
||||||
|
else if (strcmp(library, "engine") == 0)
|
||||||
|
{
|
||||||
|
addressInBase = (void *)gpGlobals;
|
||||||
|
}
|
||||||
|
|
||||||
|
finalAddress = NULL;
|
||||||
|
|
||||||
|
if (*entry != '\\')
|
||||||
|
{
|
||||||
|
#if defined(WIN32)
|
||||||
|
|
||||||
|
MEMORY_BASIC_INFORMATION mem;
|
||||||
|
|
||||||
|
if (VirtualQuery(addressInBase, &mem, sizeof(mem)))
|
||||||
|
{
|
||||||
|
finalAddress = g_MemUtils.ResolveSymbol(mem.AllocationBase, entry);
|
||||||
|
}
|
||||||
|
|
||||||
|
#elif defined(__linux__) || defined(__APPLE__)
|
||||||
|
|
||||||
|
Dl_info info;
|
||||||
|
void *handle = NULL;
|
||||||
|
|
||||||
|
if (dladdr(addressInBase, &info) != 0)
|
||||||
|
{
|
||||||
|
void *handle = dlopen(info.dli_fname, RTLD_NOW);
|
||||||
|
if (handle)
|
||||||
|
{
|
||||||
|
if (isHidden)
|
||||||
|
{
|
||||||
|
finalAddress = g_MemUtils.ResolveSymbol(handle, entry);
|
||||||
|
}
|
||||||
|
else
|
||||||
|
{
|
||||||
|
finalAddress = dlsym(handle, entry);
|
||||||
|
}
|
||||||
|
|
||||||
|
dlclose(handle);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
#endif
|
||||||
|
}
|
||||||
|
else
|
||||||
|
{
|
||||||
|
finalAddress = g_MemUtils.DecodeAndFindPattern(addressInBase, entry);
|
||||||
|
}
|
||||||
|
|
||||||
|
return finalAddress != NULL ? finalAddress : NULL;
|
||||||
}
|
}
|
@ -35,6 +35,7 @@
|
|||||||
|
|
||||||
bool UTIL_IsPlayer(AMX* amx, edict_t* pPlayer);
|
bool UTIL_IsPlayer(AMX* amx, edict_t* pPlayer);
|
||||||
void UTIL_TextMsg_Generic(edict_t* pPlayer, const char* message);
|
void UTIL_TextMsg_Generic(edict_t* pPlayer, const char* message);
|
||||||
|
void *UTIL_FindAddressFromEntry(const char *entry, bool isHidden = false, const char *library = "mod");
|
||||||
|
|
||||||
#define GETINFOKEYBUFFER (*g_engfuncs.pfnGetInfoKeyBuffer)
|
#define GETINFOKEYBUFFER (*g_engfuncs.pfnGetInfoKeyBuffer)
|
||||||
#define SETCLIENTKEYVALUE (*g_engfuncs.pfnSetClientKeyValue)
|
#define SETCLIENTKEYVALUE (*g_engfuncs.pfnSetClientKeyValue)
|
||||||
|
Loading…
Reference in New Issue
Block a user