reworked antiflood. it now has some logic in core to take care of loading order nastiness, and to fully prevent trigger spamming
3 * =============================================================================
5 * Copyright (C) 2004-2007 AlliedModders LLC. All rights reserved.
6 * =============================================================================
8 * This program is free software; you can redistribute it and/or modify it under
9 * the terms of the GNU General Public License, version 3.0, as published by the
10 * Free Software Foundation.
12 * This program is distributed in the hope that it will be useful, but WITHOUT
13 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
14 * FOR A PARTICULAR PURPOSE. See the GNU General Public License for more
17 * You should have received a copy of the GNU General Public License along with
18 * this program. If not, see <http://www.gnu.org/licenses/>.
20 * As a special exception, AlliedModders LLC gives you permission to link the
21 * code of this program (as well as its derivative works) to "Half-Life 2," the
22 * "Source Engine," the "SourcePawn JIT," and any Game MODs that run on software
23 * by the Valve Corporation. You must obey the GNU General Public License in
24 * all respects for all other code used. Additionally, AlliedModders LLC grants
25 * this exception to all derivative works. AlliedModders LLC defines further
26 * exceptions, found in LICENSE.txt (as of this writing, version JULY-31-2007),
27 * or <http://www.sourcemod.net/license.php>.
32 #include "sm_globals.h"
33 #include <IHandleSys.h>
36 #include "HandleSys.h"
37 #include "CellArray.h"
39 /***********************************
40 * About the double array hack *
41 ***************************
43 Double arrays in Pawn are vectors offset by the current offset. For example:
47 In this array, index 0 contains the offset from the current offset which
48 results in the final vector [2] (at [0][2]). Meaning, to dereference [1][2],
51 address = &array[1] + array[1] + 2 * sizeof(cell)
53 The fact that each offset is from the _current_ position rather than the _base_
54 position is very important. It means that if you to try to swap vector positions,
55 the offsets will no longer match, because their current position has changed. A
56 simple and ingenious way around this is to back up the positions in a separate array,
57 then to overwrite each position in the old array with absolute indices. Pseudo C++ code:
59 cell *array; //assumed to be set to the 2+D array
60 cell *old_offsets = new cell[2];
61 for (int i=0; i<2; i++)
63 old_offsets = array[i];
67 Now, you can swap the array indices with no problem, and do a reverse-lookup to find the original addresses.
68 After sorting/modification is done, you must relocate the new indices. For example, if the two vectors in our
69 demo array were swapped, array[0] would be 1 and array[1] would be 0. This is invalid to the virtual machine.
70 Luckily, this is also simple -- all the information is there.
72 for (int i=0; i<2; i++)
74 //get the # of the vector we want to relocate in
75 cell vector_index = array[i];
76 //get the real address of this vector
77 char *real_address = (char *)array + (vector_index * sizeof(cell)) + old_offsets[vector_index];
78 //calc and store the new distance offset
79 array[i] = real_address - ( (char *)array + (vector_index + sizeof(cell)) )
82 Note that the inner expression can be heavily reduced; it is expanded for readability.
83 **********************************/
92 void sort_random(cell_t *array, cell_t size)
94 srand((unsigned int)time(NULL));
96 for (int i = size-1; i > 0; i--)
98 int n = (rand() % i) + 1;
100 if (array[i] != array[n])
102 array[i] ^= array[n];
103 array[n] ^= array[i];
104 array[i] ^= array[n];
109 int sort_ints_asc(const void *int1, const void *int2)
111 return (*(int *)int1) - (*(int *)int2);
114 int sort_ints_desc(const void *int1, const void *int2)
116 return (*(int *)int2) - (*(int *)int1);
119 static cell_t sm_SortIntegers(IPluginContext *pContext, const cell_t *params)
122 cell_t array_size = params[2];
123 cell_t type = params[3];
125 pContext->LocalToPhysAddr(params[1], &array);
127 if (type == Sort_Ascending)
129 qsort(array, array_size, sizeof(cell_t), sort_ints_asc);
131 else if (type == Sort_Descending)
133 qsort(array, array_size, sizeof(cell_t), sort_ints_desc);
137 sort_random(array, array_size);
143 int sort_floats_asc(const void *float1, const void *float2)
145 float r1 = *(float *)float1;
146 float r2 = *(float *)float2;
151 } else if (r2 < r1) {
158 int sort_floats_desc(const void *float1, const void *float2)
160 float r1 = *(float *)float1;
161 float r2 = *(float *)float2;
166 } else if (r2 < r1) {
173 static cell_t sm_SortFloats(IPluginContext *pContext, const cell_t *params)
176 cell_t array_size = params[2];
177 cell_t type = params[3];
179 pContext->LocalToPhysAddr(params[1], &array);
181 if (type == Sort_Ascending)
183 qsort(array, array_size, sizeof(cell_t), sort_floats_asc);
185 else if (type == Sort_Descending)
187 qsort(array, array_size, sizeof(cell_t), sort_floats_desc);
191 sort_random(array, array_size);
197 static cell_t *g_CurStringArray = NULL;
198 static cell_t *g_CurRebaseMap = NULL;
200 int sort_strings_asc(const void *blk1, const void *blk2)
202 cell_t reloc1 = *(cell_t *)blk1;
203 cell_t reloc2 = *(cell_t *)blk2;
205 char *str1 = ((char *)(&g_CurStringArray[reloc1]) + g_CurRebaseMap[reloc1]);
206 char *str2 = ((char *)(&g_CurStringArray[reloc2]) + g_CurRebaseMap[reloc2]);
208 return strcmp(str1, str2);
211 int sort_strings_desc(const void *blk1, const void *blk2)
213 cell_t reloc1 = *(cell_t *)blk1;
214 cell_t reloc2 = *(cell_t *)blk2;
216 char *str1 = ((char *)(&g_CurStringArray[reloc1]) + g_CurRebaseMap[reloc1]);
217 char *str2 = ((char *)(&g_CurStringArray[reloc2]) + g_CurRebaseMap[reloc2]);
219 return strcmp(str2, str1);
222 static cell_t sm_SortStrings(IPluginContext *pContext, const cell_t *params)
225 cell_t array_size = params[2];
226 cell_t type = params[3];
228 pContext->LocalToPhysAddr(params[1], &array);
230 /** HACKHACK - back up the old indices, replace the indices with something easier */
231 cell_t amx_addr, *phys_addr;
233 if ((err=pContext->HeapAlloc(array_size, &amx_addr, &phys_addr)) != SP_ERROR_NONE)
235 pContext->ThrowNativeErrorEx(err, "Ran out of memory to sort");
238 g_CurStringArray = array;
239 g_CurRebaseMap = phys_addr;
241 for (int i=0; i<array_size; i++)
243 phys_addr[i] = array[i];
247 if (type == Sort_Ascending)
249 qsort(array, array_size, sizeof(cell_t), sort_strings_asc);
251 else if (type == Sort_Descending)
253 qsort(array, array_size, sizeof(cell_t), sort_strings_desc);
257 sort_random(array, array_size);
260 /* END HACKHACK - restore what we damaged so Pawn doesn't throw up.
261 * We'll browse through each index of the array and patch up the distance.
263 for (int i=0; i<array_size; i++)
265 /* Compute the final address of the old array and subtract the new location.
266 * This is the fixed up distance.
268 array[i] = ((char *)&array[array[i]] + phys_addr[array[i]]) - (char *)&array[i];
271 pContext->HeapPop(amx_addr);
273 g_CurStringArray = NULL;
274 g_CurRebaseMap = NULL;
281 IPluginFunction *pFunc;
288 sort_info g_SortInfo;
290 int sort1d_amx_custom(const void *elem1, const void *elem2)
292 cell_t c1 = *(cell_t *)elem1;
293 cell_t c2 = *(cell_t *)elem2;
296 IPluginFunction *pf = g_SortInfo.pFunc;
299 pf->PushCell(g_SortInfo.array_addr);
300 pf->PushCell(g_SortInfo.hndl);
301 pf->Execute(&result);
306 static cell_t sm_SortCustom1D(IPluginContext *pContext, const cell_t *params)
309 cell_t array_size = params[2];
311 IPluginFunction *pFunction = pContext->GetFunctionById(params[3]);
314 return pContext->ThrowNativeError("Function %x is not a valid function", params[3]);
317 pContext->LocalToPhysAddr(params[1], &array);
319 sort_info oldinfo = g_SortInfo;
322 g_SortInfo.hndl = params[4];
323 g_SortInfo.array_addr = params[1];
324 g_SortInfo.array_remap = NULL;
325 g_SortInfo.array_base = NULL;
326 g_SortInfo.pFunc = pFunction;
328 qsort(array, array_size, sizeof(cell_t), sort1d_amx_custom);
330 g_SortInfo = oldinfo;
335 int sort2d_amx_custom(const void *elem1, const void *elem2)
337 cell_t c1 = *(cell_t *)elem1;
338 cell_t c2 = *(cell_t *)elem2;
340 cell_t c1_addr = g_SortInfo.array_addr + (c1 * sizeof(cell_t)) + g_SortInfo.array_remap[c1];
341 cell_t c2_addr = g_SortInfo.array_addr + (c2 * sizeof(cell_t)) + g_SortInfo.array_remap[c2];
343 IPluginContext *pContext = g_SortInfo.pFunc->GetParentContext();
345 pContext->LocalToPhysAddr(c1_addr, &c1_r);
346 pContext->LocalToPhysAddr(c2_addr, &c2_r);
349 g_SortInfo.pFunc->PushCell(c1_addr);
350 g_SortInfo.pFunc->PushCell(c2_addr);
351 g_SortInfo.pFunc->PushCell(g_SortInfo.array_addr);
352 g_SortInfo.pFunc->PushCell(g_SortInfo.hndl);
353 g_SortInfo.pFunc->Execute(&result);
358 static cell_t sm_SortCustom2D(IPluginContext *pContext, const cell_t *params)
361 cell_t array_size = params[2];
362 IPluginFunction *pFunction;
364 pContext->LocalToPhysAddr(params[1], &array);
366 if ((pFunction=pContext->GetFunctionById(params[3])) == NULL)
368 return pContext->ThrowNativeError("Function %x is not a valid function", params[3]);
371 /** back up the old indices, replace the indices with something easier */
372 cell_t amx_addr, *phys_addr;
374 if ((err=pContext->HeapAlloc(array_size, &amx_addr, &phys_addr)) != SP_ERROR_NONE)
376 pContext->ThrowNativeErrorEx(err, "Ran out of memory to sort");
380 sort_info oldinfo = g_SortInfo;
382 g_SortInfo.pFunc = pFunction;
383 g_SortInfo.hndl = params[4];
384 g_SortInfo.array_addr = params[1];
386 /** Same process as in strings, back up the old indices for later fixup */
387 g_SortInfo.array_base = array;
388 g_SortInfo.array_remap = phys_addr;
390 for (int i=0; i<array_size; i++)
392 phys_addr[i] = array[i];
396 qsort(array, array_size, sizeof(cell_t), sort2d_amx_custom);
398 /** Fixup process! */
399 for (int i=0; i<array_size; i++)
401 /* Compute the final address of the old array and subtract the new location.
402 * This is the fixed up distance.
404 array[i] = ((char *)&array[array[i]] + phys_addr[array[i]]) - (char *)&array[i];
407 pContext->HeapPop(amx_addr);
409 g_SortInfo = oldinfo;
421 int sort_adtarray_strings_asc(const void *str1, const void *str2)
423 return strcmp((char *) str1, (char *) str2);
426 int sort_adtarray_strings_desc(const void *str1, const void *str2)
428 return strcmp((char *) str2, (char *) str1);
431 void sort_adt_random(CellArray *cArray)
433 size_t arraysize = cArray->size();
435 srand((unsigned int)time(NULL));
437 for (int i = arraysize-1; i > 0; i--)
439 int n = (rand() % i) + 1;
445 static cell_t sm_SortADTArray(IPluginContext *pContext, const cell_t *params)
449 HandleSecurity sec(pContext->GetIdentity(), g_pCoreIdent);
451 if ((err = g_HandleSys.ReadHandle(params[1], htCellArray, &sec, (void **)&cArray))
454 return pContext->ThrowNativeError("Invalid Handle %x (error: %d)", params[1], err);
457 cell_t order = params[2];
459 if (order == Sort_Random)
461 sort_adt_random(cArray);
466 cell_t type = params[3];
467 size_t arraysize = cArray->size();
468 size_t blocksize = cArray->blocksize();
469 cell_t *array = cArray->base();
471 if (type == Sort_Integer)
473 if (order == Sort_Ascending)
475 qsort(array, arraysize, blocksize * sizeof(cell_t), sort_ints_asc);
479 qsort(array, arraysize, blocksize * sizeof(cell_t), sort_ints_desc);
482 else if (type == Sort_Float)
484 if (order == Sort_Ascending)
486 qsort(array, arraysize, blocksize * sizeof(cell_t), sort_floats_asc);
490 qsort(array, arraysize, blocksize * sizeof(cell_t), sort_floats_desc);
493 else if (type == Sort_String)
495 if (order == Sort_Ascending)
497 qsort(array, arraysize, blocksize * sizeof(cell_t), sort_adtarray_strings_asc);
501 qsort(array, arraysize, blocksize * sizeof(cell_t), sort_adtarray_strings_desc);
510 IPluginFunction *pFunc;
517 sort_infoADT g_SortInfoADT;
519 int sort_adtarray_custom(const void *elem1, const void *elem2)
522 IPluginFunction *pf = g_SortInfoADT.pFunc;
523 pf->PushCell(((cell_t) ((cell_t *) elem1 - g_SortInfoADT.array_base)) / g_SortInfoADT.array_bsize);
524 pf->PushCell(((cell_t) ((cell_t *) elem2 - g_SortInfoADT.array_base)) / g_SortInfoADT.array_bsize);
525 pf->PushCell(g_SortInfoADT.array_hndl);
526 pf->PushCell(g_SortInfoADT.hndl);
527 pf->Execute(&result);
532 static cell_t sm_SortADTArrayCustom(IPluginContext *pContext, const cell_t *params)
536 HandleSecurity sec(pContext->GetIdentity(), g_pCoreIdent);
538 if ((err = g_HandleSys.ReadHandle(params[1], htCellArray, &sec, (void **)&cArray))
541 return pContext->ThrowNativeError("Invalid Handle %x (error: %d)", params[1], err);
544 IPluginFunction *pFunction = pContext->GetFunctionById(params[2]);
547 return pContext->ThrowNativeError("Function %x is not a valid function", params[2]);
550 size_t arraysize = cArray->size();
551 size_t blocksize = cArray->blocksize();
552 cell_t *array = cArray->base();
554 sort_infoADT oldinfo = g_SortInfoADT;
556 g_SortInfoADT.pFunc = pFunction;
557 g_SortInfoADT.array_base = array;
558 g_SortInfoADT.array_bsize = (cell_t) blocksize;
559 g_SortInfoADT.array_hndl = params[1];
560 g_SortInfoADT.hndl = params[3];
562 qsort(array, arraysize, blocksize * sizeof(cell_t), sort_adtarray_custom);
564 g_SortInfoADT = oldinfo;
569 REGISTER_NATIVES(sortNatives)
571 {"SortIntegers", sm_SortIntegers},
572 {"SortFloats", sm_SortFloats},
573 {"SortStrings", sm_SortStrings},
574 {"SortCustom1D", sm_SortCustom1D},
575 {"SortCustom2D", sm_SortCustom2D},
576 {"SortADTArray", sm_SortADTArray},
577 {"SortADTArrayCustom", sm_SortADTArrayCustom},