Skip to content

Commit 9ea4d6c

Browse files
committed
feat: Implemented platform header from SAR and update module macros
1 parent 2f9247a commit 9ea4d6c

File tree

7 files changed

+143
-26
lines changed

7 files changed

+143
-26
lines changed

src/modules/cbaseentity.cpp

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,7 @@ void CBaseEntity::RemoveEntity(CBaseEntity* pEntity)
2727
if (!pEntity)
2828
return;
2929

30-
static auto removeEntity = reinterpret_cast<void (__cdecl*)(void*)>(Memory::Scan<void*>(SERVER, "55 8B EC 57 8B 7D 08 85 FF 74 72"));
30+
static auto removeEntity = reinterpret_cast<void (__cdecl*)(void*)>(Memory::Scan<void*>(MODULE_SERVER, "55 8B EC 57 8B 7D 08 85 FF 74 72"));
3131
removeEntity((reinterpret_cast<IServerEntity*>(pEntity)->GetNetworkable()));
3232
}
3333

@@ -41,7 +41,7 @@ HSCRIPT CBaseEntity::GetScriptScope(CBaseEntity* pEntity)
4141
if (!pEntity)
4242
return nullptr;
4343

44-
return *reinterpret_cast<HSCRIPT*>(reinterpret_cast<uintptr_t>(pEntity) + 0x33c);
44+
return *reinterpret_cast<HSCRIPT*>(reinterpret_cast<uintptr_t>(pEntity) + 0x33C);
4545
}
4646

4747
/**
@@ -51,7 +51,7 @@ HSCRIPT CBaseEntity::GetScriptScope(CBaseEntity* pEntity)
5151
*/
5252
HSCRIPT CBaseEntity::GetScriptInstance(CBaseEntity* entity)
5353
{
54-
static auto getScriptInstance = reinterpret_cast<HSCRIPT (__thiscall*)(CBaseEntity*)>(Memory::Scan<void*>(SERVER, "55 8B EC 51 56 8B F1 83 BE 50"));
54+
static auto getScriptInstance = reinterpret_cast<HSCRIPT (__thiscall*)(CBaseEntity*)>(Memory::Scan<void*>(MODULE_SERVER, "55 8B EC 51 56 8B F1 83 BE 50"));
5555
if (!getScriptInstance)
5656
{
5757
Log(WARNING, false, "Could not get script instance for entity!");
@@ -73,6 +73,6 @@ HSCRIPT CBaseEntity::GetScriptInstance(CBaseEntity* entity)
7373
*/
7474
int CBaseEntity::EmitSound(CBaseEntity* pEntity, int entityIndex, IRecipientFilter* filter, const char* soundName, const Vector* pOrigin, const float soundTime)
7575
{
76-
static auto emitSound = reinterpret_cast<int (__thiscall*)(CBaseEntity*, IRecipientFilter*, int, const char*, const Vector*, float)>(Memory::Scan<void*>(SERVER, "55 8B EC 83 EC 4C 8B 0D"));
76+
static auto emitSound = reinterpret_cast<int (__thiscall*)(CBaseEntity*, IRecipientFilter*, int, const char*, const Vector*, float)>(Memory::Scan<void*>(MODULE_SERVER, "55 8B EC 83 EC 4C 8B 0D"));
7777
return emitSound(pEntity, filter, entityIndex, soundName, pOrigin, soundTime);
7878
}

src/modules/cbaseplayer.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,6 @@ void CBasePlayer::ShowViewPortPanel(const int playerIndex, const char* name, con
2323
Log(WARNING, false, "Couldn't get player to display view port panel to! playerIndex: %i", playerIndex);
2424
return;
2525
}
26-
static auto showViewPortPanel = reinterpret_cast<void(__thiscall*)(CBasePlayer*, const char*, bool, KeyValues*)>(Memory::Scan<void*>(SERVER, "55 8B EC 83 EC 20 53 56 8B F1 57 8D 4D ? E8 ? ? ? ? 56"));
26+
static auto showViewPortPanel = reinterpret_cast<void(__thiscall*)(CBasePlayer*, const char*, bool, KeyValues*)>(Memory::Scan<void*>(MODULE_SERVER, "55 8B EC 83 EC 20 53 56 8B F1 57 8D 4D ? E8 ? ? ? ? 56"));
2727
showViewPortPanel(pPlayer, name, bShow, data);
2828
}

src/modules/cportal_player.cpp

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,7 @@ void CPortal_Player::RespawnPlayer(const int playerEntityIndex)
2929
return;
3030
}
3131

32-
static auto respawnPlayer = reinterpret_cast<void(__thiscall*)(CPortal_Player*)>(Memory::Scan<void*>(SERVER, "0F 57 C0 56 8B F1 57 8D 8E"));
32+
static auto respawnPlayer = reinterpret_cast<void(__thiscall*)(CPortal_Player*)>(Memory::Scan<void*>(MODULE_SERVER, "0F 57 C0 56 8B F1 57 8D 8E"));
3333
respawnPlayer(pPlayer);
3434
}
3535

@@ -51,7 +51,7 @@ void CPortal_Player::SetFlashlightState(const int playerEntityIndex, const bool
5151
}
5252

5353
if (enabled)
54-
reinterpret_cast<void(__thiscall*)(CBaseEntity*, int)>(Memory::Scan<void*>(SERVER, "55 8B EC 53 8B D9 8B 83 A8"))(reinterpret_cast<CBaseEntity*>(pPlayer), EF_DIMLIGHT);
54+
reinterpret_cast<void(__thiscall*)(CBaseEntity*, int)>(Memory::Scan<void*>(MODULE_SERVER, "55 8B EC 53 8B D9 8B 83 A8"))(reinterpret_cast<CBaseEntity*>(pPlayer), EF_DIMLIGHT);
5555
else
56-
reinterpret_cast<void(__thiscall*)(CBaseEntity*, int)>(Memory::Scan<void*>(SERVER, "55 8B EC 53 56 8B 75 08 8B D9 8B 83"))(reinterpret_cast<CBaseEntity*>(pPlayer), EF_DIMLIGHT);
57-
}
56+
reinterpret_cast<void(__thiscall*)(CBaseEntity*, int)>(Memory::Scan<void*>(MODULE_SERVER, "55 8B EC 53 56 8B 75 08 8B D9 8B 83"))(reinterpret_cast<CBaseEntity*>(pPlayer), EF_DIMLIGHT);
57+
}

src/modules/utils.cpp

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -222,7 +222,7 @@ HSCRIPT Utils::EntIndexScriptHandle(const int entityIndex)
222222
*/
223223
CBasePlayer* Utils::PlayerByIndex(const int playerEntityIndex)
224224
{
225-
static auto playerByIndex = reinterpret_cast<CBasePlayer* (__cdecl*)(int)>(Memory::Scan<void*>(SERVER, "55 8B EC 8B 4D 08 33 C0 85 C9 7E 30"));
225+
static auto playerByIndex = reinterpret_cast<CBasePlayer* (__cdecl*)(int)>(Memory::Scan<void*>(MODULE_SERVER, "55 8B EC 8B 4D 08 33 C0 85 C9 7E 30"));
226226
return playerByIndex(playerEntityIndex);
227227
}
228228

@@ -239,7 +239,7 @@ CBasePlayer* Utils::PlayerByIndex(const int playerEntityIndex)
239239
*/
240240
void Utils::ClientPrint(CBasePlayer* player, const int msgDest, const char* msg, const char* param1, const char* param2, const char* param3, const char* param4)
241241
{
242-
static auto clientPrint = reinterpret_cast<void (__cdecl*)(CBasePlayer*, int, const char*, const char*, const char*, const char*, const char*)>(Memory::Scan<void*>(SERVER, "55 8B EC 83 EC 20 56 8B 75 08 85 F6 74 4C"));
242+
static auto clientPrint = reinterpret_cast<void (__cdecl*)(CBasePlayer*, int, const char*, const char*, const char*, const char*, const char*)>(Memory::Scan<void*>(MODULE_SERVER, "55 8B EC 83 EC 20 56 8B 75 08 85 F6 74 4C"));
243243
clientPrint(player, msgDest, msg, param1, param2, param3, param4);
244244
}
245245

@@ -252,7 +252,7 @@ void Utils::ClientPrint(CBasePlayer* player, const int msgDest, const char* msg,
252252
*/
253253
void Utils::HudMessage(CBasePlayer* pPlayer, const char* pMessage, const HudMessageParams& textParms)
254254
{
255-
static auto hudMessage = reinterpret_cast<void (__cdecl*)(CBasePlayer*, const HudMessageParams&, const char*)>(Memory::Scan(SERVER, "55 8B EC 83 EC 20 8D 4D ? E8 ? ? ? ? 8B 45 ? 8D 4D ? 85 C0 74 ? 50 E8 ? ? ? ? EB ? E8 ? ? ? ? 56"));
255+
static auto hudMessage = reinterpret_cast<void (__cdecl*)(CBasePlayer*, const HudMessageParams&, const char*)>(Memory::Scan(MODULE_SERVER, "55 8B EC 83 EC 20 8D 4D ? E8 ? ? ? ? 8B 45 ? 8D 4D ? 85 C0 74 ? 50 E8 ? ? ? ? EB ? E8 ? ? ? ? 56"));
256256
hudMessage(pPlayer, textParms, pMessage);
257257
}
258258

@@ -263,7 +263,7 @@ void Utils::HudMessage(CBasePlayer* pPlayer, const char* pMessage, const HudMess
263263
*/
264264
CBasePlayer* Utils::GetCommandClient()
265265
{
266-
static auto getCommandClient = reinterpret_cast<CBasePlayer* (__cdecl*)()>(Memory::Scan(SERVER, "A1 ? ? ? ? 40 85 C0"));
266+
static auto getCommandClient = reinterpret_cast<CBasePlayer* (__cdecl*)()>(Memory::Scan(MODULE_SERVER, "A1 ? ? ? ? 40 85 C0"));
267267
return getCommandClient();
268268
}
269269

@@ -274,7 +274,7 @@ CBasePlayer* Utils::GetCommandClient()
274274
*/
275275
int Utils::GetCommandClientIndex()
276276
{
277-
static auto getCommandClientIndex = reinterpret_cast<int (__cdecl*)()>(Memory::Scan(SERVER, "A1 ? ? ? ? 40 C3"));
277+
static auto getCommandClientIndex = reinterpret_cast<int (__cdecl*)()>(Memory::Scan(MODULE_SERVER, "A1 ? ? ? ? 40 C3"));
278278
return getCommandClientIndex();
279279
}
280280

@@ -286,6 +286,6 @@ int Utils::GetCommandClientIndex()
286286
*/
287287
void Utils::SetOrigin(CBaseEntity* entity, const Vector& vecOrigin, const bool fireTriggers)
288288
{
289-
static auto setOrigin = reinterpret_cast<void (__cdecl*)(CBaseEntity*, const Vector&, bool)>(Memory::Scan(SERVER, "55 8B EC 8B 45 0C 56 8B 75 08 50 8B"));
289+
static auto setOrigin = reinterpret_cast<void (__cdecl*)(CBaseEntity*, const Vector&, bool)>(Memory::Scan(MODULE_SERVER, "55 8B EC 8B 45 0C 56 8B 75 08 50 8B"));
290290
setOrigin(entity, vecOrigin, fireTriggers);
291291
}

src/p2sm.cpp

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -134,23 +134,23 @@ bool CP2SMPlusPlusPlugin::Load(CreateInterfaceFn interfaceFactory, CreateInterfa
134134
#if _WIN32
135135
// When a player, both client or host, goes through a linked_portal_door entity in multiplayer, the host will crash. This fixes that.
136136
Log(INFO, true, "Fixing linked portal doors for multiplayer...");
137-
Memory::ReplacePattern("server", "0F B6 87 04 05 00 00 8B 16", "EB 14 87 04 05 00 00 8B 16");
137+
Memory::ReplacePattern(MODULE_SERVER, "0F B6 87 04 05 00 00 8B 16", "EB 14 87 04 05 00 00 8B 16");
138138

139139
// Increase runtime max from 0.03 to 0.05.
140140
// Helps add some more leeway to some things we do in VScript without the engine complaining and shutting down the rest of the script.
141141
Log(INFO, true, "Patching max runtime for VScript...");
142-
Memory::ReplacePattern("vscript", "00 00 00 E0 51 B8 9E 3F", "9a 99 99 99 99 99 a9 3f");
142+
Memory::ReplacePattern(MODULE_VSCRIPT, "00 00 00 E0 51 B8 9E 3F", "9a 99 99 99 99 99 a9 3f");
143143

144144
// Increase the projected texture limit and disable the game auto-disabling others when there is more than one active. Thanks to \n and BetweenReality with help with these.
145145
//! Engine limit still exists though with a max of eight env_projectedtextures.
146146
Log(INFO, true, "Patching max amount of projected textures at once and auto disabling of projected textures...");
147147
// CEnvProjectedTexture::EnforceSingleProjectionRules
148-
Memory::ReplacePattern("server", "8B F0 3B F3 0F 84 95 00 00 00", "E9 9D 00 00 00 84 95 00 00 00"); // Skip for loop jump and jump to function return.
148+
Memory::ReplacePattern(MODULE_SERVER, "8B F0 3B F3 0F 84 95 00 00 00", "E9 9D 00 00 00 84 95 00 00 00"); // Skip for loop jump and jump to function return.
149149

150150
// CClientShadowMgr::CalculateRenderTargetsAndSizes
151-
Memory::ReplacePattern("client", "F7 D8 1B C0 83 E0 07 40 5F", "B8 08 00 00 00 90 90 90 5F"); // Force the number of max projected texture to be 8.
151+
Memory::ReplacePattern(MODULE_CLIENT, "F7 D8 1B C0 83 E0 07 40 5F", "B8 08 00 00 00 90 90 90 5F"); // Force the number of max projected texture to be 8.
152152
Log(INFO, true, "Increasing shadow map resolution for projected textures...");
153-
Memory::ReplacePattern("client", "0F 95 C3 89 8E", "0F 94 C3 89 8E"); // Allow for higher resolution shadow maps without tools mode.
153+
Memory::ReplacePattern(MODULE_CLIENT, "0F 95 C3 89 8E", "0F 94 C3 89 8E"); // Allow for higher resolution shadow maps without tools mode.
154154

155155
Log(INFO, true, "Patching all projected textures to have volumetrics...");
156156
// CShadowMgr::DrawVolumetrics
@@ -188,7 +188,7 @@ bool CP2SMPlusPlusPlugin::Load(CreateInterfaceFn interfaceFactory, CreateInterfa
188188
// Stop workshop map downloads by not returning false on the download request.
189189
//!! TEMP TO FIX ORIGINAL HOOK THAT SUDDENLY BROKE
190190
MH_CreateHook(
191-
Memory::Scan<void*>(CLIENT, "55 8B EC 81 EC 48 01 00 00 57"),
191+
Memory::Scan<void*>(MODULE_CLIENT, "55 8B EC 81 EC 48 01 00 00 57"),
192192
&CUGCFileRequestManager__Update_hook, reinterpret_cast<void**>(&CUGCFileRequestManager__Update_orig)
193193
);
194194

src/utils/memory.hpp

Lines changed: 8 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -19,12 +19,15 @@
1919
# define MAX_PATH 4096
2020
#endif
2121

22+
#include "utils/platform.hpp"
23+
2224
// Module file name macros.
23-
#define SERVER "server.dll"
24-
#define ENGINE "engine.dll"
25-
#define CLIENT "client.dll"
26-
#define APIDX9 "shaderapidx9.dll"
27-
#define APIVULKAN "shaderapidx9.dll"
25+
#define MODULE_SERVER "server" MODULE_EXTENSION
26+
#define MODULE_ENGINE "engine" MODULE_EXTENSION
27+
#define MODULE_CLIENT "client" MODULE_EXTENSION
28+
#define MODULE_VSCRIPT "vscript" MODULE_EXTENSION
29+
#define MODULE_SHADERAPIDX9 "shaderapidx9" MODULE_EXTENSION
30+
#define MODULE_APIVULKAN "shaderapidx9" MODULE_EXTENSION
2831

2932
namespace Memory
3033
{

src/utils/platform.hpp

Lines changed: 114 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,114 @@
1+
/*******************************************************************
2+
* @file platform.hpp
3+
* @brief Platform based macros.
4+
* @author SAR Team
5+
*********************************************************************/
6+
7+
#pragma once
8+
#ifdef _WIN32
9+
# include "minhook/include/MinHook.h"
10+
#endif
11+
12+
#ifdef _WIN32
13+
# define __rescalll __thiscall
14+
#else
15+
# define __rescalll __attribute__((__cdecl__))
16+
# define __stdcall
17+
#endif
18+
19+
#ifdef _WIN32
20+
# define MODULE_EXTENSION ".dll"
21+
# define __rescall __thiscall
22+
# define DLL_EXPORT extern "C" __declspec(dllexport)
23+
# define SEEK_DIR_CUR std::ios_base::_Seekdir::_Seekcur
24+
# define STDCALL_NAME(base, param_bytes) "_" base "@" #param_bytes
25+
26+
# define DECL_DETOUR(name, ...) \
27+
using _##name = int(__rescall *)(void *thisptr, ##__VA_ARGS__); \
28+
static _##name name; \
29+
static int __fastcall name##_Hook(void *thisptr, int edx, ##__VA_ARGS__)
30+
# define DECL_DETOUR_T(type, name, ...) \
31+
using _##name = type(__rescall *)(void *thisptr, ##__VA_ARGS__); \
32+
static _##name name; \
33+
static type __fastcall name##_Hook(void *thisptr, int edx, ##__VA_ARGS__)
34+
# define DECL_DETOUR_B(name, ...) \
35+
using _##name = int(__rescall *)(void *thisptr, ##__VA_ARGS__); \
36+
static _##name name; \
37+
static _##name name##Base; \
38+
static int __fastcall name##_Hook(void *thisptr, int edx, ##__VA_ARGS__)
39+
40+
# define DETOUR(name, ...) \
41+
int __fastcall name##_Hook(void *thisptr, int edx, ##__VA_ARGS__)
42+
# define DETOUR_T(type, name, ...) \
43+
type __fastcall name##_Hook(void *thisptr, int edx, ##__VA_ARGS__)
44+
# define DETOUR_B(name, ...) \
45+
int __fastcall name##_Hook(void *thisptr, int edx, ##__VA_ARGS__)
46+
47+
namespace {
48+
bool mhInitialized = false;
49+
}
50+
# define MH_HOOK(name, target) \
51+
if (!mhInitialized) { \
52+
MH_Initialize(); \
53+
mhInitialized = true; \
54+
} \
55+
name = reinterpret_cast<decltype(name)>(target); \
56+
MH_CreateHook(reinterpret_cast<LPVOID>(target), name##_Hook, reinterpret_cast<LPVOID *>(&name##_Trampoline)); \
57+
MH_EnableHook(reinterpret_cast<LPVOID>(target));
58+
# define MH_HOOK_MID(name, target) \
59+
if (!mhInitialized) { \
60+
MH_Initialize(); \
61+
mhInitialized = true; \
62+
} \
63+
name = target; \
64+
MH_CreateHook(reinterpret_cast<LPVOID>(name), name##_Hook, reinterpret_cast<LPVOID *>(&name##_Trampoline)); \
65+
MH_EnableHook(reinterpret_cast<LPVOID>(name));
66+
# define MH_UNHOOK(name) \
67+
if (name) { \
68+
MH_DisableHook(reinterpret_cast<LPVOID>(name)); \
69+
MH_RemoveHook(reinterpret_cast<LPVOID>(name)); \
70+
}
71+
# define DECL_DETOUR_MID_MH(name) \
72+
static uintptr_t name; \
73+
static uintptr_t name##_Trampoline; \
74+
static void name##_Hook()
75+
# define DECL_DETOUR_MH(name, ...) \
76+
using _##name = int(__thiscall *)(void *thisptr, __VA_ARGS__); \
77+
static _##name name; \
78+
static _##name name##_Trampoline; \
79+
static int __fastcall name##_Hook(void *thisptr, int edx, __VA_ARGS__)
80+
81+
# define DETOUR_MID_MH(name) \
82+
__declspec(naked) void name##_Hook()
83+
# define DETOUR_MH(name, ...) \
84+
int __fastcall name##_Hook(void *thisptr, int edx, __VA_ARGS__)
85+
#else
86+
# define MODULE_EXTENSION ".so"
87+
# define __rescall __attribute__((__cdecl__))
88+
# define __cdecl __attribute__((__cdecl__))
89+
# define __fastcall __attribute__((__fastcall__))
90+
# define DLL_EXPORT extern "C" __attribute__((visibility("default")))
91+
# define SEEK_DIR_CUR std::ios_base::cur
92+
# define STDCALL_NAME(base, param_bytes) base
93+
94+
# define DECL_DETOUR(name, ...) \
95+
using _##name = int(__rescall *)(void *thisptr, ##__VA_ARGS__); \
96+
static _##name name; \
97+
static int __rescall name##_Hook(void *thisptr, ##__VA_ARGS__)
98+
# define DECL_DETOUR_T(type, name, ...) \
99+
using _##name = type(__rescall *)(void *thisptr, ##__VA_ARGS__); \
100+
static _##name name; \
101+
static type __rescall name##_Hook(void *thisptr, ##__VA_ARGS__)
102+
# define DECL_DETOUR_B(name, ...) \
103+
using _##name = int(__rescall *)(void *thisptr, ##__VA_ARGS__); \
104+
static _##name name; \
105+
static _##name name##Base; \
106+
static int __rescall name##_Hook(void *thisptr, ##__VA_ARGS__)
107+
108+
# define DETOUR(name, ...) \
109+
int __rescall name##_Hook(void *thisptr, ##__VA_ARGS__)
110+
# define DETOUR_T(type, name, ...) \
111+
type __rescall name##_Hook(void *thisptr, ##__VA_ARGS__)
112+
# define DETOUR_B(name, ...) \
113+
int __rescall name##_Hook(void *thisptr, ##__VA_ARGS__)
114+
#endif

0 commit comments

Comments
 (0)