fixed inheritance not being used in the base menu style when removing clients from the watch list
3 * ===============================================================
4 * SourceMod (C)2004-2007 AlliedModders LLC. All rights reserved.
5 * ===============================================================
7 * This file is not open source and may not be copied without explicit
8 * written permission of AlliedModders LLC. This file may not be redistributed
9 * in whole or significant part.
10 * For information, see LICENSE.txt or http://www.sourcemod.net/license.php
15 #include "sm_globals.h"
16 #include <IHandleSys.h>
20 /***********************************
21 * About the double array hack *
22 ***************************
24 Double arrays in Pawn are vectors offset by the current offset. For example:
28 In this array, index 0 contains the offset from the current offset which
29 results in the final vector [2] (at [0][2]). Meaning, to dereference [1][2],
32 address = &array[1] + array[1] + 2 * sizeof(cell)
34 The fact that each offset is from the _current_ position rather than the _base_
35 position is very important. It means that if you to try to swap vector positions,
36 the offsets will no longer match, because their current position has changed. A
37 simple and ingenious way around this is to back up the positions in a separate array,
38 then to overwrite each position in the old array with absolute indices. Pseudo C++ code:
40 cell *array; //assumed to be set to the 2+D array
41 cell *old_offsets = new cell[2];
42 for (int i=0; i<2; i++)
44 old_offsets = array[i];
48 Now, you can swap the array indices with no problem, and do a reverse-lookup to find the original addresses.
49 After sorting/modification is done, you must relocate the new indices. For example, if the two vectors in our
50 demo array were swapped, array[0] would be 1 and array[1] would be 0. This is invalid to the virtual machine.
51 Luckily, this is also simple -- all the information is there.
53 for (int i=0; i<2; i++)
55 //get the # of the vector we want to relocate in
56 cell vector_index = array[i];
57 //get the real address of this vector
58 char *real_address = (char *)array + (vector_index * sizeof(cell)) + old_offsets[vector_index];
59 //calc and store the new distance offset
60 array[i] = real_address - ( (char *)array + (vector_index + sizeof(cell)) )
63 Note that the inner expression can be heavily reduced; it is expanded for readability.
64 **********************************/
72 int sort_ints_asc(const void *int1, const void *int2)
74 return (*(int *)int1) - (*(int *)int2);
77 int sort_ints_desc(const void *int1, const void *int2)
79 return (*(int *)int2) - (*(int *)int1);
82 static cell_t sm_SortIntegers(IPluginContext *pContext, const cell_t *params)
85 cell_t array_size = params[2];
86 cell_t type = params[3];
88 pContext->LocalToPhysAddr(params[1], &array);
90 if (type == Sort_Ascending)
92 qsort(array, array_size, sizeof(cell_t), sort_ints_asc);
94 qsort(array, array_size, sizeof(cell_t), sort_ints_desc);
100 int sort_floats_asc(const void *float1, const void *float2)
102 float r1 = *(float *)float1;
103 float r2 = *(float *)float2;
108 } else if (r2 < r1) {
115 int sort_floats_desc(const void *float1, const void *float2)
117 float r1 = *(float *)float1;
118 float r2 = *(float *)float2;
123 } else if (r2 < r1) {
130 static cell_t sm_SortFloats(IPluginContext *pContext, const cell_t *params)
133 cell_t array_size = params[2];
134 cell_t type = params[3];
136 pContext->LocalToPhysAddr(params[1], &array);
138 if (type == Sort_Ascending)
140 qsort(array, array_size, sizeof(cell_t), sort_floats_asc);
142 qsort(array, array_size, sizeof(cell_t), sort_floats_desc);
148 static cell_t *g_CurStringArray = NULL;
149 static cell_t *g_CurRebaseMap = NULL;
151 int sort_strings_asc(const void *blk1, const void *blk2)
153 cell_t reloc1 = *(cell_t *)blk1;
154 cell_t reloc2 = *(cell_t *)blk2;
156 char *str1 = ((char *)(&g_CurStringArray[reloc1]) + g_CurRebaseMap[reloc1]);
157 char *str2 = ((char *)(&g_CurStringArray[reloc2]) + g_CurRebaseMap[reloc2]);
159 return strcmp(str1, str2);
162 int sort_strings_desc(const void *blk1, const void *blk2)
164 cell_t reloc1 = *(cell_t *)blk1;
165 cell_t reloc2 = *(cell_t *)blk2;
167 char *str1 = ((char *)(&g_CurStringArray[reloc1]) + g_CurRebaseMap[reloc1]);
168 char *str2 = ((char *)(&g_CurStringArray[reloc2]) + g_CurRebaseMap[reloc2]);
170 return strcmp(str2, str1);
173 static cell_t sm_SortStrings(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 /** HACKHACK - back up the old indices, replace the indices with something easier */
182 cell_t amx_addr, *phys_addr;
184 if ((err=pContext->HeapAlloc(array_size, &amx_addr, &phys_addr)) != SP_ERROR_NONE)
186 pContext->ThrowNativeErrorEx(err, "Ran out of memory to sort");
189 g_CurStringArray = array;
190 g_CurRebaseMap = phys_addr;
192 for (int i=0; i<array_size; i++)
194 phys_addr[i] = array[i];
198 if (type == Sort_Ascending)
200 qsort(array, array_size, sizeof(cell_t), sort_strings_asc);
202 qsort(array, array_size, sizeof(cell_t), sort_strings_desc);
205 /* END HACKHACK - restore what we damaged so Pawn doesn't throw up.
206 * We'll browse through each index of the array and patch up the distance.
208 for (int i=0; i<array_size; i++)
210 /* Compute the final address of the old array and subtract the new location.
211 * This is the fixed up distance.
213 array[i] = ((char *)&array[array[i]] + phys_addr[array[i]]) - (char *)&array[i];
216 pContext->HeapPop(amx_addr);
218 g_CurStringArray = NULL;
219 g_CurRebaseMap = NULL;
226 IPluginFunction *pFunc;
233 sort_info g_SortInfo;
235 int sort1d_amx_custom(const void *elem1, const void *elem2)
237 cell_t c1 = *(cell_t *)elem1;
238 cell_t c2 = *(cell_t *)elem2;
241 IPluginFunction *pf = g_SortInfo.pFunc;
244 pf->PushCell(g_SortInfo.array_addr);
245 pf->PushCell(g_SortInfo.hndl);
246 pf->Execute(&result);
251 static cell_t sm_SortCustom1D(IPluginContext *pContext, const cell_t *params)
254 cell_t array_size = params[2];
256 IPluginFunction *pFunction = pContext->GetFunctionById(params[3]);
259 return pContext->ThrowNativeError("Function %x is not a valid function", params[3]);
262 pContext->LocalToPhysAddr(params[1], &array);
264 sort_info oldinfo = g_SortInfo;
267 g_SortInfo.hndl = params[4];
268 g_SortInfo.array_addr = params[1];
269 g_SortInfo.array_remap = NULL;
270 g_SortInfo.array_base = NULL;
271 g_SortInfo.pFunc = pFunction;
273 qsort(array, array_size, sizeof(cell_t), sort1d_amx_custom);
275 g_SortInfo = oldinfo;
280 int sort2d_amx_custom(const void *elem1, const void *elem2)
282 cell_t c1 = *(cell_t *)elem1;
283 cell_t c2 = *(cell_t *)elem2;
285 cell_t c1_addr = g_SortInfo.array_addr + (c1 * sizeof(cell_t)) + g_SortInfo.array_remap[c1];
286 cell_t c2_addr = g_SortInfo.array_addr + (c2 * sizeof(cell_t)) + g_SortInfo.array_remap[c2];
288 IPluginContext *pContext = g_SortInfo.pFunc->GetParentContext();
290 pContext->LocalToPhysAddr(c1_addr, &c1_r);
291 pContext->LocalToPhysAddr(c2_addr, &c2_r);
294 g_SortInfo.pFunc->PushCell(c1_addr);
295 g_SortInfo.pFunc->PushCell(c2_addr);
296 g_SortInfo.pFunc->PushCell(g_SortInfo.array_addr);
297 g_SortInfo.pFunc->PushCell(g_SortInfo.hndl);
298 g_SortInfo.pFunc->Execute(&result);
303 static cell_t sm_SortCustom2D(IPluginContext *pContext, const cell_t *params)
306 cell_t array_size = params[2];
307 IPluginFunction *pFunction;
309 pContext->LocalToPhysAddr(params[1], &array);
311 if ((pFunction=pContext->GetFunctionById(params[3])) == NULL)
313 return pContext->ThrowNativeError("Function %x is not a valid function", params[3]);
316 /** back up the old indices, replace the indices with something easier */
317 cell_t amx_addr, *phys_addr;
319 if ((err=pContext->HeapAlloc(array_size, &amx_addr, &phys_addr)) != SP_ERROR_NONE)
321 pContext->ThrowNativeErrorEx(err, "Ran out of memory to sort");
325 sort_info oldinfo = g_SortInfo;
327 g_SortInfo.pFunc = pFunction;
328 g_SortInfo.hndl = params[4];
329 g_SortInfo.array_addr = params[1];
331 /** Same process as in strings, back up the old indices for later fixup */
332 g_SortInfo.array_base = array;
333 g_SortInfo.array_remap = phys_addr;
335 for (int i=0; i<array_size; i++)
337 phys_addr[i] = array[i];
341 qsort(array, array_size, sizeof(cell_t), sort2d_amx_custom);
343 /** Fixup process! */
344 for (int i=0; i<array_size; i++)
346 /* Compute the final address of the old array and subtract the new location.
347 * This is the fixed up distance.
349 array[i] = ((char *)&array[array[i]] + phys_addr[array[i]]) - (char *)&array[i];
352 pContext->HeapPop(amx_addr);
354 g_SortInfo = oldinfo;
359 REGISTER_NATIVES(sortNatives)
361 {"SortIntegers", sm_SortIntegers},
362 {"SortFloats", sm_SortFloats},
363 {"SortStrings", sm_SortStrings},
364 {"SortCustom1D", sm_SortCustom1D},
365 {"SortCustom2D", sm_SortCustom2D},