2 * ===============================================================
3 * SourceMod (C)2004-2007 AlliedModders LLC. All rights reserved.
4 * ===============================================================
6 * This file is not open source and may not be copied without explicit
7 * written permission of AlliedModders LLC. This file may not be redistributed
8 * in whole or significant part.
9 * For information, see LICENSE.txt or http://www.sourcemod.net/license.php
14 #include "sm_globals.h"
15 #include <IHandleSys.h>
18 /***********************************
19 * About the double array hack *
20 ***************************
22 Double arrays in Pawn are vectors offset by the current offset. For example:
26 In this array, index 0 contains the offset from the current offset which
27 results in the final vector [2] (at [0][2]). Meaning, to dereference [1][2],
30 address = &array[1] + array[1] + 2 * sizeof(cell)
32 The fact that each offset is from the _current_ position rather than the _base_
33 position is very important. It means that if you to try to swap vector positions,
34 the offsets will no longer match, because their current position has changed. A
35 simple and ingenious way around this is to back up the positions in a separate array,
36 then to overwrite each position in the old array with absolute indices. Pseudo C++ code:
38 cell *array; //assumed to be set to the 2+D array
39 cell *old_offsets = new cell[2];
40 for (int i=0; i<2; i++)
42 old_offsets = array[i];
46 Now, you can swap the array indices with no problem, and do a reverse-lookup to find the original addresses.
47 After sorting/modification is done, you must relocate the new indices. For example, if the two vectors in our
48 demo array were swapped, array[0] would be 1 and array[1] would be 0. This is invalid to the virtual machine.
49 Luckily, this is also simple -- all the information is there.
51 for (int i=0; i<2; i++)
53 //get the # of the vector we want to relocate in
54 cell vector_index = array[i];
55 //get the real address of this vector
56 char *real_address = (char *)array + (vector_index * sizeof(cell)) + old_offsets[vector_index];
57 //calc and store the new distance offset
58 array[i] = real_address - ( (char *)array + (vector_index + sizeof(cell)) )
61 Note that the inner expression can be heavily reduced; it is expanded for readability.
62 **********************************/
70 int sort_ints_asc(const void *int1, const void *int2)
72 return (*(int *)int1) - (*(int *)int2);
75 int sort_ints_desc(const void *int1, const void *int2)
77 return (*(int *)int2) - (*(int *)int1);
80 static cell_t sm_SortIntegers(IPluginContext *pContext, const cell_t *params)
83 cell_t array_size = params[2];
84 cell_t type = params[3];
86 pContext->LocalToPhysAddr(params[1], &array);
88 if (type == Sort_Ascending)
90 qsort(array, array_size, sizeof(cell_t), sort_ints_asc);
92 qsort(array, array_size, sizeof(cell_t), sort_ints_desc);
98 int sort_floats_asc(const void *float1, const void *float2)
100 float r1 = *(float *)float1;
101 float r2 = *(float *)float2;
106 } else if (r2 < r1) {
113 int sort_floats_desc(const void *float1, const void *float2)
115 float r1 = *(float *)float1;
116 float r2 = *(float *)float2;
121 } else if (r2 < r1) {
128 static cell_t sm_SortFloats(IPluginContext *pContext, const cell_t *params)
131 cell_t array_size = params[2];
132 cell_t type = params[3];
134 pContext->LocalToPhysAddr(params[1], &array);
136 if (type == Sort_Ascending)
138 qsort(array, array_size, sizeof(cell_t), sort_floats_asc);
140 qsort(array, array_size, sizeof(cell_t), sort_floats_desc);
146 static cell_t *g_CurStringArray = NULL;
147 static cell_t *g_CurRebaseMap = NULL;
149 int sort_strings_asc(const void *blk1, const void *blk2)
151 cell_t reloc1 = *(cell_t *)blk1;
152 cell_t reloc2 = *(cell_t *)blk2;
154 char *str1 = ((char *)(&g_CurStringArray[reloc1]) + g_CurRebaseMap[reloc1]);
155 char *str2 = ((char *)(&g_CurStringArray[reloc2]) + g_CurRebaseMap[reloc2]);
157 return strcmp(str1, str2);
160 int sort_strings_desc(const void *blk1, const void *blk2)
162 cell_t reloc1 = *(cell_t *)blk1;
163 cell_t reloc2 = *(cell_t *)blk2;
165 char *str1 = ((char *)(&g_CurStringArray[reloc1]) + g_CurRebaseMap[reloc1]);
166 char *str2 = ((char *)(&g_CurStringArray[reloc2]) + g_CurRebaseMap[reloc2]);
168 return strcmp(str2, str1);
171 static cell_t sm_SortStrings(IPluginContext *pContext, const cell_t *params)
174 cell_t array_size = params[2];
175 cell_t type = params[3];
177 pContext->LocalToPhysAddr(params[1], &array);
179 /** HACKHACK - back up the old indices, replace the indices with something easier */
180 cell_t amx_addr, *phys_addr;
182 if ((err=pContext->HeapAlloc(array_size, &amx_addr, &phys_addr)) != SP_ERROR_NONE)
184 pContext->ThrowNativeErrorEx(err, "Ran out of memory to sort");
187 g_CurStringArray = array;
188 g_CurRebaseMap = phys_addr;
190 for (int i=0; i<array_size; i++)
192 phys_addr[i] = array[i];
196 if (type == Sort_Ascending)
198 qsort(array, array_size, sizeof(cell_t), sort_strings_asc);
200 qsort(array, array_size, sizeof(cell_t), sort_strings_desc);
203 /* END HACKHACK - restore what we damaged so Pawn doesn't throw up.
204 * We'll browse through each index of the array and patch up the distance.
206 for (int i=0; i<array_size; i++)
208 /* Compute the final address of the old array and subtract the new location.
209 * This is the fixed up distance.
211 array[i] = ((char *)&array[array[i]] + phys_addr[array[i]]) - (char *)&array[i];
214 pContext->HeapPop(amx_addr);
216 g_CurStringArray = NULL;
217 g_CurRebaseMap = NULL;
224 IPluginFunction *pFunc;
231 sort_info g_SortInfo;
233 int sort1d_amx_custom(const void *elem1, const void *elem2)
235 cell_t c1 = *(cell_t *)elem1;
236 cell_t c2 = *(cell_t *)elem2;
239 IPluginFunction *pf = g_SortInfo.pFunc;
242 pf->PushCell(g_SortInfo.array_addr);
243 pf->PushCell(g_SortInfo.hndl);
244 pf->Execute(&result);
249 static cell_t sm_SortCustom1D(IPluginContext *pContext, const cell_t *params)
252 cell_t array_size = params[2];
254 IPluginFunction *pFunction = pContext->GetFunctionById(params[3]);
257 return pContext->ThrowNativeError("Function %x is not a valid function", params[3]);
260 pContext->LocalToPhysAddr(params[1], &array);
262 sort_info oldinfo = g_SortInfo;
265 g_SortInfo.hndl = params[4];
266 g_SortInfo.array_addr = params[1];
267 g_SortInfo.array_remap = NULL;
268 g_SortInfo.array_base = NULL;
269 g_SortInfo.pFunc = pFunction;
271 qsort(array, array_size, sizeof(cell_t), sort1d_amx_custom);
273 g_SortInfo = oldinfo;
278 int sort2d_amx_custom(const void *elem1, const void *elem2)
280 cell_t c1 = *(cell_t *)elem1;
281 cell_t c2 = *(cell_t *)elem2;
283 cell_t c1_addr = g_SortInfo.array_addr + (c1 * sizeof(cell_t)) + g_SortInfo.array_remap[c1];
284 cell_t c2_addr = g_SortInfo.array_addr + (c2 * sizeof(cell_t)) + g_SortInfo.array_remap[c2];
286 IPluginContext *pContext = g_SortInfo.pFunc->GetParentContext();
288 pContext->LocalToPhysAddr(c1_addr, &c1_r);
289 pContext->LocalToPhysAddr(c2_addr, &c2_r);
292 g_SortInfo.pFunc->PushCell(c1_addr);
293 g_SortInfo.pFunc->PushCell(c2_addr);
294 g_SortInfo.pFunc->PushCell(g_SortInfo.array_addr);
295 g_SortInfo.pFunc->PushCell(g_SortInfo.hndl);
296 g_SortInfo.pFunc->Execute(&result);
301 static cell_t sm_SortCustom2D(IPluginContext *pContext, const cell_t *params)
304 cell_t array_size = params[2];
305 IPluginFunction *pFunction;
307 pContext->LocalToPhysAddr(params[1], &array);
309 if ((pFunction=pContext->GetFunctionById(params[3])) == NULL)
311 return pContext->ThrowNativeError("Function %x is not a valid function", params[3]);
314 /** back up the old indices, replace the indices with something easier */
315 cell_t amx_addr, *phys_addr;
317 if ((err=pContext->HeapAlloc(array_size, &amx_addr, &phys_addr)) != SP_ERROR_NONE)
319 pContext->ThrowNativeErrorEx(err, "Ran out of memory to sort");
323 sort_info oldinfo = g_SortInfo;
325 g_SortInfo.pFunc = pFunction;
326 g_SortInfo.hndl = params[4];
327 g_SortInfo.array_addr = params[1];
329 /** Same process as in strings, back up the old indices for later fixup */
330 g_SortInfo.array_base = array;
331 g_SortInfo.array_remap = phys_addr;
333 for (int i=0; i<array_size; i++)
335 phys_addr[i] = array[i];
339 qsort(array, array_size, sizeof(cell_t), sort2d_amx_custom);
341 /** Fixup process! */
342 for (int i=0; i<array_size; i++)
344 /* Compute the final address of the old array and subtract the new location.
345 * This is the fixed up distance.
347 array[i] = ((char *)&array[array[i]] + phys_addr[array[i]]) - (char *)&array[i];
350 pContext->HeapPop(amx_addr);
352 g_SortInfo = oldinfo;
357 REGISTER_NATIVES(sortNatives)
359 {"SortIntegers", sm_SortIntegers},
360 {"SortFloats", sm_SortFloats},
361 {"SortStrings", sm_SortStrings},
362 {"SortCustom1D", sm_SortCustom1D},
363 {"SortCustom2D", sm_SortCustom2D},