-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathpatch.cpp
More file actions
420 lines (343 loc) · 11.8 KB
/
Copy pathpatch.cpp
File metadata and controls
420 lines (343 loc) · 11.8 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
#include "CDetour/detours.h"
#include "GameSystem.h"
#include "CCombatCharacter.h"
#include "player_pickup.h"
#include "CPlayer.h"
#include "CAI_Network.h"
#include "MonsterConfig.h"
#include "CPhysicsProp.h"
#include "CTrigger.h"
void PatchFunction()
{
}
class CCleanupUtlSymbolTable : public CBaseGameSystem
{
public:
CCleanupUtlSymbolTable(const char *name) : CBaseGameSystem(name)
{
}
bool CUtlSymbol_Initialize()
{
void *func = NULL;
if(!func)
{
if(!g_pGameConf->GetMemSig("CUtlSymbol::Initialize", &func))
{
assert(0);
return false;
}
}
typedef void (*_func)();
_func thisfunc = (_func)func;
thisfunc();
return true;
}
void SDKShutdown()
{
// server.dll CCleanupUtlSymbolTable will delete s_pSymbolTable at exit
// we set our s_pSymbolTable to NULL
CUtlSymbol::s_pSymbolTable = old_s_pSymbolTable;
}
bool SDKInit()
{
// link server.dll s_pSymbolTable our s_pSymbolTable (tier1.lib)
if(!CUtlSymbol_Initialize()) // init server.dll s_pSymbolTable
return false;
CUtlSymbol::Initialize(); // init out s_pSymbolTable, set symbolsInitialized = true
old_s_pSymbolTable = CUtlSymbol::s_pSymbolTable;
CUtlSymbolTableMT *s_pSymbolTable;
GET_VARIABLE_POINTER(s_pSymbolTable, CUtlSymbolTableMT *);
CUtlSymbol::s_pSymbolTable = s_pSymbolTable;
return true;
}
CUtlSymbolTableMT *old_s_pSymbolTable;
};
static CCleanupUtlSymbolTable g_CCleanupUtlSymbolTable("CUtlSymbol_Patch");
void CleanupUtlSymbolTable_Shutdown()
{
g_CCleanupUtlSymbolTable.SDKShutdown();
}
class PatchSystem : public CBaseGameSystem
{
public:
PatchSystem(const char *name) : CBaseGameSystem(name)
{
}
bool SDKInit();
void SDKShutdown();
void FixParentedPreCleanup();
void FixParentedPostCleanup();
};
static PatchSystem g_patchsystem("PatchSystem");
CDetour *AllocateDefaultRelationships_CDetour = NULL;
CDetour *UTIL_BloodDrips_CDetour = NULL;
CDetour *ShouldRemoveThisRagdoll_CDetour = NULL;
CDetour *FindInList_CDetour = NULL;
CDetour *Pickup_ForcePlayerToDropThisObject_CDetour = NULL;
CDetour *UTIL_GetLocalPlayer_CDetour = NULL;
CDetour *CleanUpMap_CDetour = NULL;
CDetour *CAI_NetworkBuilder_Build_CDetour = NULL;
CDetour *CAI_NetworkBuilder_InitNodePosition_CDetour = NULL;
CDetour *CBreakableProp_HandleFirstCollisionInteractions_CDetour = NULL;
CDetour *CCSPlayer_GetBulletTypeParameters_CDetour = NULL;
CDetour *CTriggerHurt_HurtAllTouchers_CDetour = NULL;
#define MAX_BROKEN_PARENTING_ENTS 4
const char broken_parenting_ents[][32] = {"move_rope",
"keyframe_rope",
"info_target",
"func_brush"};
// fix CLASS_
DETOUR_DECL_MEMBER0(CDetour_AllocateDefaultRelationships, void)
{
CCombatCharacter::AllocateDefaultRelationships();
}
// fix npc blood
DETOUR_DECL_STATIC4(CDetour_UTIL_BloodDrips, void, const Vector &, origin, const Vector &, direction, int, color, int, amount)
{
IPredictionSystem::SuppressHostEvents(NULL);
DETOUR_STATIC_CALL(CDetour_UTIL_BloodDrips)(origin, direction, color, amount);
}
// fix crash
DETOUR_DECL_STATIC0(CDetour_ShouldRemoveThisRagdoll, bool)
{
return true;
}
// fix env_gunfire target miss on CleanUpMap()
DETOUR_DECL_STATIC2(CDetour_FindInList, bool, const char **,pStrings, const char *,pToFind)
{
if(strcmp(pToFind,"info_target") == 0 ||
strcmp(pToFind,"func_brush") == 0 ||
strcmp(pToFind,"move_rope") == 0 ||
strcmp(pToFind,"keyframe_rope") == 0 ||
strcmp(pToFind,"env_beam") == 0)
return true;
if(strcmp(pToFind, "info_player_start") == 0)
return true;
bool ret = DETOUR_STATIC_CALL(CDetour_FindInList)(pStrings,pToFind);
return ret;
}
// fix UTIL_GetLocalPlayer
DETOUR_DECL_STATIC1(CDetour_Pickup_ForcePlayerToDropThisObject, void, CBaseEntity *, pTarget)
{
Pickup_ForcePlayerToDropThisObject(CEntity::Instance(pTarget));
}
// fix All UTIL_GetLocalPlayer
DETOUR_DECL_STATIC0(CDetour_UTIL_GetLocalPlayer, CBaseEntity *)
{
for(int i=1;i<=gpGlobals->maxClients;i++)
{
CPlayer *pPlayer = UTIL_PlayerByIndex(i);
if(!pPlayer)
continue;
return pPlayer->BaseEntity();
}
g_pSM->LogError(myself, "UTIL_GetLocalPlayer return NULL!");
return NULL;
}
// fix parent entity
DETOUR_DECL_MEMBER0(CDetour_CleanUpMap, void)
{
if(g_MonsterConfig.m_bEnable_CleanUpMap)
{
g_patchsystem.FixParentedPreCleanup();
DETOUR_MEMBER_CALL(CDetour_CleanUpMap)();
g_patchsystem.FixParentedPostCleanup();
}
}
// patch CAI_NetworkBuildHelper
static bool g_In_CAI_NetworkBuilder_Build = false;
extern void AdjustStriderNodePosition( CAI_Network *pNetwork, CAI_Node *pNode );
DETOUR_DECL_MEMBER1(CDetour_CAI_NetworkBuilder_Build, void, CAI_Network *,pNetwork )
{
g_In_CAI_NetworkBuilder_Build = true;
DETOUR_MEMBER_CALL(CDetour_CAI_NetworkBuilder_Build)(pNetwork);
g_In_CAI_NetworkBuilder_Build = false;
}
DETOUR_DECL_MEMBER2(CDetour_CAI_NetworkBuilder_InitNodePosition, void, CAI_Network *, pNetwork, CAI_Node *, pNode)
{
DETOUR_MEMBER_CALL(CDetour_CAI_NetworkBuilder_InitNodePosition)(pNetwork, pNode);
if(g_In_CAI_NetworkBuilder_Build)
{
AdjustStriderNodePosition(pNetwork, pNode);
}
}
// patch IParentPropInteraction
DETOUR_DECL_MEMBER2(CDetour_CBreakableProp_HandleFirstCollisionInteractions, void, int, index, gamevcollisionevent_t *, pEvent)
{
DETOUR_MEMBER_CALL(CDetour_CBreakableProp_HandleFirstCollisionInteractions)(index, pEvent);
CEntity *pEntity = CEntity::Instance((CBaseEntity *)this);
Assert(pEntity);
if(pEntity) {
CE_CPhysicsProp *prop = dynamic_cast<CE_CPhysicsProp *>(pEntity);
Assert(prop);
if(prop)
{
prop->HandleFirstCollisionInteractions(index, pEvent);
}
}
}
DETOUR_DECL_MEMBER3(CDetour_CCSPlayer_GetBulletTypeParameters, void, int, iBulletType, float &, float_1, float &, float_2)
{
DETOUR_MEMBER_CALL(CDetour_CCSPlayer_GetBulletTypeParameters)(iBulletType, float_1, float_2);
if(g_MonsterConfig.m_bEnable_CSS_Weapon_Penetration == false)
{
float_2 = 0.0f;
}
}
// fix crash
DETOUR_DECL_MEMBER1(CDetour_CTriggerHurt_HurtAllTouchers, int, float, dt)
{
CTriggerHurt *trigger = (CTriggerHurt *)CEntity::Instance((CBaseEntity *)this);
Assert(trigger);
return trigger->HurtAllTouchers(dt);
}
//fix bsp ain
#define NETWORK_GRAPH_PATCH_SIZE 7
unsigned char backup_network_graph_data[NETWORK_GRAPH_PATCH_SIZE];
void *network_graph_callback_addr = NULL;
void *network_graph_patch_addr = NULL;
void Local_LoadNetworkGraph(CUtlBuffer &buf)
{
int version = buf.GetInt();
if(version != 37) {
char szNrpFilename[MAX_PATH];
snprintf(szNrpFilename, sizeof(szNrpFilename), "maps/graphs/%s.ain", gpGlobals->mapname);
FileHandle_t fh = filesystem->Open(szNrpFilename, "r", "MOD");
if(fh) {
buf.Clear();
filesystem->ReadToBuffer(fh, buf);
filesystem->Close(fh);
}
}
buf.SeekGet( CUtlBuffer::SEEK_HEAD, 0 );
}
__declspec(naked) void patch_LoadNetworkGraph()
{
__asm {
lea eax, [ebp-3Ch]
push eax
call Local_LoadNetworkGraph
add esp, 4
call network_graph_callback_addr
}
}
void UnLoadNetworkGraph_Fix()
{
if(network_graph_patch_addr == NULL) {
return;
}
DWORD oldflags;
VirtualProtect(network_graph_patch_addr, NETWORK_GRAPH_PATCH_SIZE, PAGE_EXECUTE_READWRITE, &oldflags);
memcpy(network_graph_patch_addr, &backup_network_graph_data, NETWORK_GRAPH_PATCH_SIZE);
VirtualProtect(network_graph_patch_addr, NETWORK_GRAPH_PATCH_SIZE, oldflags, &oldflags);
}
void LoadNetworkGraph_Fix()
{
void *func = NULL;
if(!g_pGameConf->GetMemSig("CAI_NetworkManager::LoadNetworkGraph", &func)) {
return;
}
int offset = 389;
network_graph_patch_addr = reinterpret_cast<void *>(((int)func)+offset);
memcpy(&backup_network_graph_data, network_graph_patch_addr, sizeof(backup_network_graph_data));
if(backup_network_graph_data[0] != 0x68) {
META_CONPRINTF("[%s] LoadNetworkGraph_Fix fail!\n",g_Monster.GetLogTag());
return;
}
DWORD oldflags;
VirtualProtect(network_graph_patch_addr, NETWORK_GRAPH_PATCH_SIZE, PAGE_EXECUTE_READWRITE, &oldflags);
memset(network_graph_patch_addr,0x90,NETWORK_GRAPH_PATCH_SIZE);
unsigned char patch_data[5];
patch_data[0] = 0xE9;
*((long*)((long)(patch_data) + sizeof(unsigned char))) = (char*)patch_LoadNetworkGraph - (char*)network_graph_patch_addr - 5;
memcpy(network_graph_patch_addr, &patch_data, sizeof(patch_data));
VirtualProtect(network_graph_patch_addr, NETWORK_GRAPH_PATCH_SIZE, oldflags, &oldflags);
network_graph_callback_addr = reinterpret_cast<void *>(((int)network_graph_patch_addr)+5);
}
bool PatchSystem::SDKInit()
{
GET_DETOUR(AllocateDefaultRelationships, DETOUR_CREATE_MEMBER(CDetour_AllocateDefaultRelationships, "AllocateDefaultRelationships"));
GET_DETOUR(UTIL_BloodDrips, DETOUR_CREATE_STATIC(CDetour_UTIL_BloodDrips, "UTIL_BloodDrips"));
GET_DETOUR(ShouldRemoveThisRagdoll, DETOUR_CREATE_STATIC(CDetour_ShouldRemoveThisRagdoll, "ShouldRemoveThisRagdoll"));
GET_DETOUR(FindInList, DETOUR_CREATE_STATIC(CDetour_FindInList, "FindInList"));
GET_DETOUR(Pickup_ForcePlayerToDropThisObject, DETOUR_CREATE_STATIC(CDetour_Pickup_ForcePlayerToDropThisObject, "Pickup_ForcePlayerToDropThisObject"));
GET_DETOUR(UTIL_GetLocalPlayer, DETOUR_CREATE_STATIC(CDetour_UTIL_GetLocalPlayer, "UTIL_GetLocalPlayer"));
GET_DETOUR(CleanUpMap, DETOUR_CREATE_MEMBER(CDetour_CleanUpMap, "CleanUpMap"));
GET_DETOUR(CAI_NetworkBuilder_Build, DETOUR_CREATE_MEMBER(CDetour_CAI_NetworkBuilder_Build, "CAI_NetworkBuilder::Build"));
GET_DETOUR(CAI_NetworkBuilder_InitNodePosition, DETOUR_CREATE_MEMBER(CDetour_CAI_NetworkBuilder_InitNodePosition, "CAI_NetworkBuilder::InitNodePosition"));
GET_DETOUR(CBreakableProp_HandleFirstCollisionInteractions, DETOUR_CREATE_MEMBER(CDetour_CBreakableProp_HandleFirstCollisionInteractions, "CBreakableProp::HandleFirstCollisionInteractions"));
GET_DETOUR(CCSPlayer_GetBulletTypeParameters, DETOUR_CREATE_MEMBER(CDetour_CCSPlayer_GetBulletTypeParameters, "CCSPlayer::GetBulletTypeParameters"));
GET_DETOUR(CTriggerHurt_HurtAllTouchers, DETOUR_CREATE_MEMBER(CDetour_CTriggerHurt_HurtAllTouchers, "CTriggerHurt::HurtAllTouchers"));
LoadNetworkGraph_Fix();
return true;
}
#define DestoryDetour(d) \
if(d != NULL) \
d->DisableDetour(); \
d = NULL;
void PatchSystem::SDKShutdown()
{
DestoryDetour(AllocateDefaultRelationships_CDetour);
DestoryDetour(UTIL_BloodDrips_CDetour);
DestoryDetour(ShouldRemoveThisRagdoll_CDetour);
DestoryDetour(FindInList_CDetour);
DestoryDetour(Pickup_ForcePlayerToDropThisObject_CDetour);
DestoryDetour(UTIL_GetLocalPlayer_CDetour);
DestoryDetour(CleanUpMap_CDetour);
DestoryDetour(CAI_NetworkBuilder_Build_CDetour);
DestoryDetour(CAI_NetworkBuilder_InitNodePosition_CDetour);
DestoryDetour(CBreakableProp_HandleFirstCollisionInteractions_CDetour);
DestoryDetour(CCSPlayer_GetBulletTypeParameters_CDetour);
DestoryDetour(CTriggerHurt_HurtAllTouchers_CDetour);
UnLoadNetworkGraph_Fix();
}
void PatchSystem::FixParentedPreCleanup()
{
for(int i=1;i<=gpGlobals->maxClients;i++)
{
CPlayer *pPlayer = UTIL_PlayerByIndex(i);
if(!pPlayer)
continue;
pPlayer->LeaveVehicle();
}
for(int i=0;i<MAX_BROKEN_PARENTING_ENTS;i++)
{
const char *name = broken_parenting_ents[i];
CEntity *pSearch = NULL;
while ( ( pSearch = g_helpfunc.FindEntityByClassname( pSearch, name ) ) != NULL )
{
CEntity *parent = pSearch->GetParent();
if(parent)
{
pSearch->szCashedParentName = parent->GetEntityName_String();
pSearch->SetParent(NULL);
if(pSearch->m_vecOriginalPos == vec3_origin)
{
pSearch->m_vecOriginalPos = pSearch->GetLocalOrigin();
}
}
}
}
}
void PatchSystem::FixParentedPostCleanup()
{
for(int i=0;i<MAX_BROKEN_PARENTING_ENTS;i++)
{
const char *name = broken_parenting_ents[i];
CEntity *pSearch = NULL;
while ( ( pSearch = g_helpfunc.FindEntityByClassname( pSearch, name ) ) != NULL )
{
if(pSearch->szCashedParentName == NULL_STRING)
continue;
if(pSearch->m_vecOriginalPos != vec3_origin)
{
pSearch->SetLocalOrigin(pSearch->m_vecOriginalPos);
}
CEntity *parent = g_helpfunc.FindEntityByName( (CBaseEntity *)NULL, STRING(pSearch->szCashedParentName) );
if(parent)
{
pSearch->SetParent(parent->BaseEntity());
}
}
}
}