-
-
Notifications
You must be signed in to change notification settings - Fork 421
/
extension.cpp
502 lines (417 loc) · 12.5 KB
/
extension.cpp
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
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
/**
* vim: set ts=4 :
* =============================================================================
* SourceMod Team Fortress 2 Extension
* Copyright (C) 2004-2015 AlliedModders LLC. All rights reserved.
* =============================================================================
*
* This program is free software; you can redistribute it and/or modify it under
* the terms of the GNU General Public License, version 3.0, as published by the
* Free Software Foundation.
*
* This program is distributed in the hope that it will be useful, but WITHOUT
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
* FOR A PARTICULAR PURPOSE. See the GNU General Public License for more
* details.
*
* You should have received a copy of the GNU General Public License along with
* this program. If not, see <http://www.gnu.org/licenses/>.
*
* As a special exception, AlliedModders LLC gives you permission to link the
* code of this program (as well as its derivative works) to "Half-Life 2," the
* "Source Engine," the "SourcePawn JIT," and any Game MODs that run on software
* by the Valve Corporation. You must obey the GNU General Public License in
* all respects for all other code used. Additionally, AlliedModders LLC grants
* this exception to all derivative works. AlliedModders LLC defines further
* exceptions, found in LICENSE.txt (as of this writing, version JULY-31-2007),
* or <http://www.sourcemod.net/license.php>.
*
* Version: $Id$
*/
#include <sourcemod_version.h>
#include "extension.h"
#include "util.h"
#include "RegNatives.h"
#include "iplayerinfo.h"
#include "sm_trie_tpl.h"
#include "criticals.h"
#include "holiday.h"
#include "conditions.h"
#include "gameplayrules.h"
#include "teleporter.h"
#include "CDetour/detours.h"
#include "ISDKHooks.h"
#include "ISDKTools.h"
/**
* @file extension.cpp
* @brief Implement extension code here.
*/
TF2Tools g_TF2Tools;
IGameConfig *g_pGameConf = NULL;
IBinTools *g_pBinTools = NULL;
ISDKHooks *g_pSDKHooks = NULL;
ISDKTools *g_pSDKTools = NULL;
SMEXT_LINK(&g_TF2Tools);
ICvar *icvar = NULL;
CGlobalVars *gpGlobals = NULL;
sm_sendprop_info_t *playerSharedOffset;
extern sp_nativeinfo_t g_TFNatives[];
int g_resourceEntity;
SH_DECL_HOOK3_void(IServerGameDLL, ServerActivate, SH_NOATTRIB, 0, edict_t *, int , int);
bool TF2Tools::SDK_OnLoad(char *error, size_t maxlength, bool late)
{
if (strcmp(g_pSM->GetGameFolderName(), "tf") != 0)
{
UTIL_Format(error, maxlength, "Cannot Load TF2 Extension on mods other than TF2");
return false;
}
ServerClass *sc = UTIL_FindServerClass("CTFPlayer");
if (sc == NULL)
{
UTIL_Format(error, maxlength, "Could not find CTFPlayer server class");
return false;
}
playerSharedOffset = new sm_sendprop_info_t;
if (!UTIL_FindDataTable(sc->m_pTable, "DT_TFPlayerShared", playerSharedOffset, 0))
{
UTIL_Format(error, maxlength, "Could not find DT_TFPlayerShared data table");
return false;
}
sharesys->AddDependency(myself, "bintools.ext", true, true);
sharesys->AddDependency(myself, "sdkhooks.ext", true, true);
sharesys->AddDependency(myself, "sdktools.ext", false, true);
char conf_error[255] = "";
if (!gameconfs->LoadGameConfigFile("sm-tf2.games", &g_pGameConf, conf_error, sizeof(conf_error)))
{
if (conf_error[0])
{
UTIL_Format(error, maxlength, "Could not read sm-tf2.games.txt: %s", conf_error);
}
return false;
}
CDetourManager::Init(g_pSM->GetScriptingEngine(), g_pGameConf);
sharesys->AddNatives(myself, g_TFNatives);
sharesys->RegisterLibrary(myself, "tf2");
plsys->AddPluginsListener(this);
playerhelpers->RegisterCommandTargetProcessor(this);
g_critForward = forwards->CreateForward("TF2_CalcIsAttackCritical", ET_Hook, 4, NULL, Param_Cell, Param_Cell, Param_String, Param_CellByRef);
g_addCondForward = forwards->CreateForward("TF2_OnConditionAdded", ET_Ignore, 2, NULL, Param_Cell, Param_Cell);
g_removeCondForward = forwards->CreateForward("TF2_OnConditionRemoved", ET_Ignore, 2, NULL, Param_Cell, Param_Cell);
g_waitingPlayersStartForward = forwards->CreateForward("TF2_OnWaitingForPlayersStart", ET_Ignore, 0, NULL);
g_waitingPlayersEndForward = forwards->CreateForward("TF2_OnWaitingForPlayersEnd", ET_Ignore, 0, NULL);
g_teleportForward = forwards->CreateForward("TF2_OnPlayerTeleport", ET_Hook, 3, NULL, Param_Cell, Param_Cell, Param_CellByRef);
g_pCVar = icvar;
m_CritDetoursEnabled = false;
m_CondChecksEnabled = false;
m_RulesDetoursEnabled = false;
m_TeleportDetourEnabled = false;
g_HolidayManager.OnSDKLoad(late);
return true;
}
const char *TF2Tools::GetExtensionVerString()
{
return SOURCEMOD_VERSION;
}
const char *TF2Tools::GetExtensionDateString()
{
return SOURCEMOD_BUILD_TIME;
}
bool TF2Tools::SDK_OnMetamodLoad(ISmmAPI *ismm, char *error, size_t maxlen, bool late)
{
GET_V_IFACE_CURRENT(GetEngineFactory, engine, IVEngineServer, INTERFACEVERSION_VENGINESERVER);
gpGlobals = ismm->GetCGlobals();
SH_ADD_HOOK(IServerGameDLL, ServerActivate, gamedll, SH_STATIC(OnServerActivate), true);
GET_V_IFACE_CURRENT(GetEngineFactory, icvar, ICvar, CVAR_INTERFACE_VERSION);
GET_V_IFACE_CURRENT(GetEngineFactory, m_GameEventManager, IGameEventManager2, INTERFACEVERSION_GAMEEVENTSMANAGER2);
m_GameEventManager->AddListener(this, "teamplay_restart_round", true);
return true;
}
void TF2Tools::SDK_OnUnload()
{
SH_REMOVE_HOOK(IServerGameDLL, ServerActivate, gamedll, SH_STATIC(OnServerActivate), true);
g_HolidayManager.OnSDKUnload();
m_GameEventManager->RemoveListener(this);
if (m_CritDetoursEnabled)
{
g_CritManager.Disable();
m_CritDetoursEnabled = false;
}
if (m_CondChecksEnabled)
{
g_CondMgr.Shutdown();
m_CondChecksEnabled = false;
}
if (m_RulesDetoursEnabled)
{
RemoveRulesDetours();
m_RulesDetoursEnabled = false;
}
if (m_TeleportDetourEnabled)
{
RemoveTeleporterDetour();
m_TeleportDetourEnabled = false;
}
g_RegNatives.UnregisterAll();
gameconfs->CloseGameConfigFile(g_pGameConf);
playerhelpers->UnregisterCommandTargetProcessor(this);
plsys->RemovePluginsListener(this);
forwards->ReleaseForward(g_critForward);
forwards->ReleaseForward(g_addCondForward);
forwards->ReleaseForward(g_removeCondForward);
forwards->ReleaseForward(g_waitingPlayersStartForward);
forwards->ReleaseForward(g_waitingPlayersEndForward);
forwards->ReleaseForward(g_teleportForward);
if (g_pSDKHooks != NULL)
{
g_pSDKHooks->RemoveEntityListener(&g_CritManager);
}
}
void TF2Tools::SDK_OnAllLoaded()
{
SM_GET_LATE_IFACE(BINTOOLS, g_pBinTools);
SM_GET_LATE_IFACE(SDKHOOKS, g_pSDKHooks);
SM_GET_LATE_IFACE(SDKTOOLS, g_pSDKTools);
if (g_pSDKHooks != NULL)
{
g_pSDKHooks->AddEntityListener(&g_CritManager);
}
}
bool TF2Tools::RegisterConCommandBase(ConCommandBase *pVar)
{
return g_SMAPI->RegisterConCommandBase(g_PLAPI, pVar);
}
void TF2Tools::FireGameEvent( IGameEvent *event )
{
timersys->NotifyOfGameStart();
timersys->MapTimeLeftChanged();
}
bool TF2Tools::QueryRunning(char *error, size_t maxlength)
{
SM_CHECK_IFACE(BINTOOLS, g_pBinTools);
SM_GET_LATE_IFACE(SDKHOOKS, g_pSDKHooks);
return true;
}
bool TF2Tools::QueryInterfaceDrop(SMInterface *pInterface)
{
if (pInterface == g_pBinTools)
{
return false;
}
if (pInterface == g_pSDKHooks)
{
return false;
}
if (pInterface == g_pSDKTools)
{
g_pSDKTools = NULL;
}
return IExtensionInterface::QueryInterfaceDrop(pInterface);
}
void TF2Tools::NotifyInterfaceDrop(SMInterface *pInterface)
{
g_RegNatives.UnregisterAll();
}
void OnServerActivate(edict_t *pEdictList, int edictCount, int clientMax)
{
g_resourceEntity = FindResourceEntity();
g_HolidayManager.OnServerActivated();
}
bool TF2Tools::ProcessCommandTarget(cmd_target_info_t *info)
{
int max_clients;
IPlayerInfo *pInfo;
unsigned int team_index = 0;
IGamePlayer *pPlayer, *pAdmin;
if ((info->flags & COMMAND_FILTER_NO_MULTI) == COMMAND_FILTER_NO_MULTI)
{
return false;
}
if (info->admin)
{
if ((pAdmin = playerhelpers->GetGamePlayer(info->admin)) == NULL)
{
return false;
}
if (!pAdmin->IsInGame())
{
return false;
}
}
else
{
pAdmin = NULL;
}
if (strcmp(info->pattern, "@red") == 0 )
{
team_index = 2;
}
else if (strcmp(info->pattern, "@blue") == 0)
{
team_index = 3;
}
else
{
return false;
}
info->num_targets = 0;
max_clients = playerhelpers->GetMaxClients();
for (int i = 1;
i <= max_clients && (cell_t)info->num_targets < info->max_targets;
i++)
{
if ((pPlayer = playerhelpers->GetGamePlayer(i)) == NULL)
{
continue;
}
if (!pPlayer->IsInGame())
{
continue;
}
if ((pInfo = pPlayer->GetPlayerInfo()) == NULL)
{
continue;
}
if (pInfo->GetTeamIndex() != (int)team_index)
{
continue;
}
if (playerhelpers->FilterCommandTarget(pAdmin, pPlayer, info->flags)
!= COMMAND_TARGET_VALID)
{
continue;
}
info->targets[info->num_targets] = i;
info->num_targets++;
}
if (info->num_targets == 0)
{
info->reason = COMMAND_TARGET_EMPTY_FILTER;
}
else
{
info->reason = COMMAND_TARGET_VALID;
}
info->target_name_style = COMMAND_TARGETNAME_RAW;
if (team_index == 2)
{
UTIL_Format(info->target_name, info->target_name_maxlength, "Red Team");
}
else if (team_index == 3)
{
UTIL_Format(info->target_name, info->target_name_maxlength, "Blue Team");
}
return true;
}
void TF2Tools::OnPluginLoaded(IPlugin *plugin)
{
if (!m_CritDetoursEnabled && g_critForward->GetFunctionCount())
{
m_CritDetoursEnabled = g_CritManager.TryEnable();
}
if (!m_CondChecksEnabled
&& ( g_addCondForward->GetFunctionCount() || g_removeCondForward->GetFunctionCount() )
)
{
m_CondChecksEnabled = g_CondMgr.Init();
}
if (!m_RulesDetoursEnabled
&& ( g_waitingPlayersStartForward->GetFunctionCount() || g_waitingPlayersEndForward->GetFunctionCount() )
)
{
m_RulesDetoursEnabled = InitialiseRulesDetours();
}
if (!m_TeleportDetourEnabled && g_teleportForward->GetFunctionCount())
{
m_TeleportDetourEnabled = InitialiseTeleporterDetour();
}
}
void TF2Tools::OnPluginUnloaded(IPlugin *plugin)
{
if (m_CritDetoursEnabled && !g_critForward->GetFunctionCount())
{
g_CritManager.Disable();
m_CritDetoursEnabled = false;
}
if (m_CondChecksEnabled)
{
if (!g_addCondForward->GetFunctionCount() && !g_removeCondForward->GetFunctionCount())
{
g_CondMgr.Shutdown();
m_CondChecksEnabled = false;
}
}
if (m_RulesDetoursEnabled)
{
if (!g_waitingPlayersStartForward->GetFunctionCount() && !g_waitingPlayersEndForward->GetFunctionCount())
{
RemoveRulesDetours();
m_RulesDetoursEnabled = false;
}
}
if (m_TeleportDetourEnabled && !g_teleportForward->GetFunctionCount())
{
RemoveTeleporterDetour();
m_TeleportDetourEnabled = false;
}
}
int FindResourceEntity()
{
return FindEntityByNetClass(-1, "CTFPlayerResource");
}
int FindEntityByNetClass(int start, const char *classname)
{
edict_t *current;
for (int i = ((start != -1) ? start : 0); i < gpGlobals->maxEntities; i++)
{
CBaseEntity *pEntity = gamehelpers->ReferenceToEntity(i);
if (pEntity == nullptr)
{
continue;
}
ServerClass *pServerClass = gamehelpers->FindEntityServerClass(pEntity);
if (pServerClass == nullptr)
{
continue;
}
const char *name = pServerClass->GetName();
if (strcmp(name, classname) == 0)
{
return i;
}
}
return -1;
}
TFClassType ClassnameToType(const char *classname)
{
static KTrie<TFClassType> trie;
static bool filled = false;
if (!filled)
{
trie.insert("scout", TFClass_Scout);
trie.insert("sniper", TFClass_Sniper);
trie.insert("soldier", TFClass_Soldier);
trie.insert("demoman", TFClass_DemoMan);
trie.insert("demo", TFClass_DemoMan);
trie.insert("medic", TFClass_Medic);
trie.insert("heavy", TFClass_Heavy);
trie.insert("heavyweap", TFClass_Heavy);
trie.insert("heavyweapons", TFClass_Heavy);
trie.insert("hwg", TFClass_Heavy);
trie.insert("pyro", TFClass_Pyro);
trie.insert("spy", TFClass_Spy);
trie.insert("engineer", TFClass_Engineer);
trie.insert("engy", TFClass_Engineer);
filled = true;
}
TFClassType *value;
if (!(value = trie.retrieve(classname)))
{
return TFClass_Unknown;
}
return *value;
}
/**
* A picture of a blue crab given to me as a gift and stored here for safe keeping
*
* http://sourcemod.net/Clown%20car.jpg
*/