forked from arthurdead/popspawner
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathextension.cpp
More file actions
614 lines (485 loc) · 15.6 KB
/
extension.cpp
File metadata and controls
614 lines (485 loc) · 15.6 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
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
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
/**
* vim: set ts=4 :
* =============================================================================
* SourceMod Sample Extension
* Copyright (C) 2004-2008 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 "extension.h"
#include <CDetour/detours.h>
#include <tier1/utlvector.h>
#include <ehandle.h>
#include <unordered_map>
#include <ICellArray.h>
/**
* @file extension.cpp
* @brief Implement extension code here.
*/
Sample g_Sample; /**< Global singleton for extension's main interface */
SMEXT_LINK(&g_Sample);
typedef CUtlVector< CHandle< CBaseEntity > > EntityHandleVector_t;
#define TF_CLASS_UNDEFINED 0
enum AttributeType : int;
class IPopulator;
class IPopulationSpawner
{
public:
// We need a virtual destructor or else the derived-class destructors won't be called,
// leading to memory leaks. Found via clang warning.
virtual ~IPopulationSpawner()
{
}
IPopulationSpawner( IPopulator *populator )
{
m_populator = populator;
}
IPopulator *GetPopulator( void ) const
{
return m_populator;
}
virtual bool Parse( KeyValues *data ) = 0;
virtual bool Spawn( const Vector &here, EntityHandleVector_t *result = NULL ) = 0;
virtual bool IsWhereRequired( void ) const = 0;
virtual bool IsVarious( void ) = 0;
virtual int GetClass( int nSpawnNum = -1 ) = 0;
virtual string_t GetClassIcon( int nSpawnNum = -1 ) = 0;
virtual int GetHealth( int nSpawnNum = -1 ) = 0;
virtual bool IsMiniBoss( int nSpawnNum = -1 ) = 0;
virtual bool HasAttribute( AttributeType type, int nSpawnNum = -1 ) = 0;
virtual bool HasEventChangeAttributes( const char* pszEventName ) const = 0;
protected:
IPopulator *m_populator = nullptr;
};
struct pop_entry_t;
class SPPopulationSpawner;
using pop_spawner_map_t = std::unordered_map<pop_entry_t *, SPPopulationSpawner *>;
pop_spawner_map_t popspawnermap{};
struct pop_entry_t
{
pop_entry_t(const std::string &name_);
~pop_entry_t();
IPluginFunction *Parse = nullptr;
IPluginFunction *Spawn = nullptr;
IPluginFunction *HasEventChangeAttributes = nullptr;
IPluginFunction *GetClass = nullptr;
IPluginFunction *GetHealth = nullptr;
IPluginFunction *GetClassIcon = nullptr;
IPluginFunction *IsMiniBoss = nullptr;
IPluginFunction *HasAttribute = nullptr;
bool WhereRequired = true;
bool IsVarious = false;
std::string name{};
IdentityToken_t *owner = nullptr;
};
template <typename T>
T void_to_func(void *ptr)
{
union { T f; void *p; };
p = ptr;
return f;
}
#include "icandowhateveriwantthefactthattheresnowaytodothisstillisridiculous.h"
class SPPopulationSpawner : public IPopulationSpawner
{
public:
SPPopulationSpawner(pop_entry_t *entry_, IPopulator *populator_)
: IPopulationSpawner{populator_}, entry{entry_}
{
popspawnermap[entry] = this;
}
~SPPopulationSpawner()
{
popspawnermap.erase(entry);
}
bool Parse( KeyValues *data )
{
if(!entry) {
return false;
}
IPluginFunction *func = entry->Parse;
if(!func) {
return false;
}
Handle_t hndl = ((HandleSystemHack *)handlesys)->CreateKeyValuesHandle(data, entry->owner);
func->PushCell(hndl);
cell_t res = 0;
func->Execute(&res);
handlesys->FreeHandle(hndl, nullptr);
return res;
}
bool Spawn( const Vector &here, EntityHandleVector_t *result = NULL )
{
if(!entry) {
return false;
}
IPluginFunction *func = entry->Spawn;
if(!func) {
return false;
}
cell_t vec[3];
vec[0] = sp_ftoc(here.x);
vec[1] = sp_ftoc(here.y);
vec[2] = sp_ftoc(here.z);
func->PushArray(vec, 3);
Handle_t hndl = 0;
ICellArray *arr = nullptr;
if(result) {
hndl = ((HandleSystemHack *)handlesys)->CreateCellArrayHandle(arr, entry->owner);
}
func->PushCell(hndl);
cell_t res = 0;
func->Execute(&res);
if(result) {
size_t len = arr->size();
for(int i = 0; i < len; ++i) {
cell_t index = arr->at(i)[0];
edict_t *pEnt = gamehelpers->EdictOfIndex(index);
if(pEnt) {
CBaseHandle hndl{};
gamehelpers->SetHandleEntity(hndl, pEnt);
result->AddToTail(hndl);
}
}
handlesys->FreeHandle(hndl, nullptr);
}
return res;
}
bool HasEventChangeAttributes( const char* pszEventName ) const
{
if(!entry) {
return false;
}
IPluginFunction *func = entry->HasEventChangeAttributes;
if(!func) {
return false;
}
func->PushString(pszEventName);
cell_t res = 0;
func->Execute(&res);
return res;
}
int GetClass( int nSpawnNum = -1 )
{
if(!entry) {
return TF_CLASS_UNDEFINED;
}
IPluginFunction *func = entry->GetClass;
if(!func) {
return TF_CLASS_UNDEFINED;
}
func->PushCell(nSpawnNum);
cell_t res = 0;
func->Execute(&res);
return res;
}
int GetHealth( int nSpawnNum = -1 )
{
if(!entry) {
return 0;
}
IPluginFunction *func = entry->GetHealth;
if(!func) {
return 0;
}
func->PushCell(nSpawnNum);
cell_t res = 0;
func->Execute(&res);
return res;
}
string_t GetClassIcon( int nSpawnNum = -1 )
{
if(!entry) {
return NULL_STRING;
}
IPluginFunction *func = entry->GetClassIcon;
if(!func) {
return NULL_STRING;
}
/*
func->PushCell(nSpawnNum);
func->PushArray(nullptr, 0);
cell_t len = 0;
func->PushCellByRef(&len);
cell_t res = 0;
func->Execute(&res);
*/
return NULL_STRING;
}
bool IsMiniBoss( int nSpawnNum = -1 )
{
if(!entry) {
return false;
}
IPluginFunction *func = entry->IsMiniBoss;
if(!func) {
return false;
}
func->PushCell(nSpawnNum);
cell_t res = 0;
func->Execute(&res);
return res;
}
bool HasAttribute( AttributeType type, int nSpawnNum = -1 )
{
if(!entry) {
return false;
}
IPluginFunction *func = entry->HasAttribute;
if(!func) {
return false;
}
func->PushCell(type);
func->PushCell(nSpawnNum);
cell_t res = 0;
func->Execute(&res);
return res;
}
bool IsWhereRequired( void ) const
{
return entry ? entry->WhereRequired : true;
}
bool IsVarious( void )
{
return entry ? entry->IsVarious : false;
}
pop_entry_t *entry = nullptr;
};
using pop_entry_map_t = std::unordered_map<std::string, pop_entry_t *>;
pop_entry_map_t poentrypmap{};
pop_entry_t::pop_entry_t(const std::string &name_)
: name{name_}
{
poentrypmap[name] = this;
}
pop_entry_t::~pop_entry_t()
{
poentrypmap.erase(name);
pop_spawner_map_t::iterator it{popspawnermap.begin()};
while(it != popspawnermap.end()) {
if(it->second->entry == this) {
it->second->entry = nullptr;
break;
}
++it;
}
}
DETOUR_DECL_STATIC2(ParseSpawner, IPopulationSpawner *, IPopulator *, populator, KeyValues *, data)
{
IPopulationSpawner *spawner = DETOUR_STATIC_CALL(ParseSpawner)(populator, data);
if(spawner) {
return spawner;
}
const char *name = data->GetName();
pop_entry_map_t::iterator it{poentrypmap.find(name)};
if(it != poentrypmap.end()) {
pop_entry_t *entry = it->second;
IPopulationSpawner *spawner = new SPPopulationSpawner{entry, populator};
if(!spawner->Parse(data)) {
delete spawner;
spawner = nullptr;
}
return spawner;
}
return nullptr;
}
HandleType_t popspawner_handle = 0;
cell_t register_popspawner(IPluginContext *pContext, const cell_t *params)
{
char *name = nullptr;
pContext->LocalToString(params[1], &name);
pop_entry_map_t::iterator it{poentrypmap.find(name)};
if(it != poentrypmap.end()) {
return pContext->ThrowNativeError("%s is already registered", name);
}
pop_entry_t *obj = new pop_entry_t{name};
obj->owner = pContext->GetIdentity();
return handlesys->CreateHandle(popspawner_handle, obj, pContext->GetIdentity(), myself->GetIdentity(), nullptr);
}
cell_t Parseset(IPluginContext *pContext, const cell_t *params)
{
HandleSecurity security(pContext->GetIdentity(), myself->GetIdentity());
pop_entry_t *obj = nullptr;
HandleError err = handlesys->ReadHandle(params[1], popspawner_handle, &security, (void **)&obj);
if(err != HandleError_None)
{
return pContext->ThrowNativeError("Invalid Handle %x (error: %d)", params[1], err);
}
obj->Parse = pContext->GetFunctionById(params[2]);
return 0;
}
cell_t Spawnset(IPluginContext *pContext, const cell_t *params)
{
HandleSecurity security(pContext->GetIdentity(), myself->GetIdentity());
pop_entry_t *obj = nullptr;
HandleError err = handlesys->ReadHandle(params[1], popspawner_handle, &security, (void **)&obj);
if(err != HandleError_None)
{
return pContext->ThrowNativeError("Invalid Handle %x (error: %d)", params[1], err);
}
obj->Spawn = pContext->GetFunctionById(params[2]);
return 0;
}
cell_t HasEventChangeAttributesset(IPluginContext *pContext, const cell_t *params)
{
HandleSecurity security(pContext->GetIdentity(), myself->GetIdentity());
pop_entry_t *obj = nullptr;
HandleError err = handlesys->ReadHandle(params[1], popspawner_handle, &security, (void **)&obj);
if(err != HandleError_None)
{
return pContext->ThrowNativeError("Invalid Handle %x (error: %d)", params[1], err);
}
obj->HasEventChangeAttributes = pContext->GetFunctionById(params[2]);
return 0;
}
cell_t GetClassset(IPluginContext *pContext, const cell_t *params)
{
HandleSecurity security(pContext->GetIdentity(), myself->GetIdentity());
pop_entry_t *obj = nullptr;
HandleError err = handlesys->ReadHandle(params[1], popspawner_handle, &security, (void **)&obj);
if(err != HandleError_None)
{
return pContext->ThrowNativeError("Invalid Handle %x (error: %d)", params[1], err);
}
obj->GetClass = pContext->GetFunctionById(params[2]);
return 0;
}
cell_t GetHealthset(IPluginContext *pContext, const cell_t *params)
{
HandleSecurity security(pContext->GetIdentity(), myself->GetIdentity());
pop_entry_t *obj = nullptr;
HandleError err = handlesys->ReadHandle(params[1], popspawner_handle, &security, (void **)&obj);
if(err != HandleError_None)
{
return pContext->ThrowNativeError("Invalid Handle %x (error: %d)", params[1], err);
}
obj->GetHealth = pContext->GetFunctionById(params[2]);
return 0;
}
cell_t GetClassIconset(IPluginContext *pContext, const cell_t *params)
{
HandleSecurity security(pContext->GetIdentity(), myself->GetIdentity());
pop_entry_t *obj = nullptr;
HandleError err = handlesys->ReadHandle(params[1], popspawner_handle, &security, (void **)&obj);
if(err != HandleError_None)
{
return pContext->ThrowNativeError("Invalid Handle %x (error: %d)", params[1], err);
}
obj->GetClassIcon = pContext->GetFunctionById(params[2]);
return 0;
}
cell_t IsMiniBossset(IPluginContext *pContext, const cell_t *params)
{
HandleSecurity security(pContext->GetIdentity(), myself->GetIdentity());
pop_entry_t *obj = nullptr;
HandleError err = handlesys->ReadHandle(params[1], popspawner_handle, &security, (void **)&obj);
if(err != HandleError_None)
{
return pContext->ThrowNativeError("Invalid Handle %x (error: %d)", params[1], err);
}
obj->IsMiniBoss = pContext->GetFunctionById(params[2]);
return 0;
}
cell_t HasAttributeset(IPluginContext *pContext, const cell_t *params)
{
HandleSecurity security(pContext->GetIdentity(), myself->GetIdentity());
pop_entry_t *obj = nullptr;
HandleError err = handlesys->ReadHandle(params[1], popspawner_handle, &security, (void **)&obj);
if(err != HandleError_None)
{
return pContext->ThrowNativeError("Invalid Handle %x (error: %d)", params[1], err);
}
obj->HasAttribute = pContext->GetFunctionById(params[2]);
return 0;
}
cell_t WhereRequiredset(IPluginContext *pContext, const cell_t *params)
{
HandleSecurity security(pContext->GetIdentity(), myself->GetIdentity());
pop_entry_t *obj = nullptr;
HandleError err = handlesys->ReadHandle(params[1], popspawner_handle, &security, (void **)&obj);
if(err != HandleError_None)
{
return pContext->ThrowNativeError("Invalid Handle %x (error: %d)", params[1], err);
}
obj->WhereRequired = params[2];
return 0;
}
cell_t IsVariousset(IPluginContext *pContext, const cell_t *params)
{
HandleSecurity security(pContext->GetIdentity(), myself->GetIdentity());
pop_entry_t *obj = nullptr;
HandleError err = handlesys->ReadHandle(params[1], popspawner_handle, &security, (void **)&obj);
if(err != HandleError_None)
{
return pContext->ThrowNativeError("Invalid Handle %x (error: %d)", params[1], err);
}
obj->IsVarious = params[2];
return 0;
}
sp_nativeinfo_t natives[] =
{
{"CustomPopulationSpawner.Parse.set", Parseset},
{"CustomPopulationSpawner.Spawn.set", Spawnset},
{"CustomPopulationSpawner.HasEventChangeAttributes.set", HasEventChangeAttributesset},
{"CustomPopulationSpawner.GetClass.set", GetClassset},
{"CustomPopulationSpawner.GetHealth.set", GetHealthset},
{"CustomPopulationSpawner.GetClassIcon.set", GetClassIconset},
{"CustomPopulationSpawner.IsMiniBoss.set", IsMiniBossset},
{"CustomPopulationSpawner.HasAttribute.set", HasAttributeset},
{"CustomPopulationSpawner.WhereRequired.set", WhereRequiredset},
{"CustomPopulationSpawner.IsVarious.set", IsVariousset},
{"register_popspawner", register_popspawner},
{NULL, NULL}
};
void Sample::OnPluginUnloaded(IPlugin *plugin)
{
}
void Sample::OnHandleDestroy(HandleType_t type, void *object)
{
if(type == popspawner_handle) {
pop_entry_t *obj = (pop_entry_t *)object;
delete obj;
}
}
CDetour *pParseSpawner = nullptr;
bool Sample::SDK_OnLoad(char *error, size_t maxlen, bool late)
{
IGameConfig *g_pGameConf = nullptr;
gameconfs->LoadGameConfigFile("popspawner", &g_pGameConf, error, maxlen);
CDetourManager::Init(g_pSM->GetScriptingEngine(), g_pGameConf);
pParseSpawner = DETOUR_CREATE_STATIC(ParseSpawner, "IPopulationSpawner::ParseSpawner")
pParseSpawner->EnableDetour();
gameconfs->CloseGameConfigFile(g_pGameConf);
popspawner_handle = handlesys->CreateType("popspawner", this, 0, nullptr, nullptr, myself->GetIdentity(), nullptr);
sharesys->AddNatives(myself, natives);
sharesys->RegisterLibrary(myself, "popspawner");
HandleSystemHack::init();
return true;
}
void Sample::SDK_OnUnload()
{
pParseSpawner->Destroy();
handlesys->RemoveType(popspawner_handle, myself->GetIdentity());
}