diff --git a/core/msvc8/sourcemod_mm.vcproj b/core/msvc8/sourcemod_mm.vcproj index e79697fd..345569a5 100644 --- a/core/msvc8/sourcemod_mm.vcproj +++ b/core/msvc8/sourcemod_mm.vcproj @@ -681,6 +681,10 @@ RelativePath="..\smn_player.cpp" > + + diff --git a/core/sm_stringutil.cpp b/core/sm_stringutil.cpp index 75db7263..47abe741 100644 --- a/core/sm_stringutil.cpp +++ b/core/sm_stringutil.cpp @@ -660,7 +660,12 @@ reswitch: { CHECK_ARGS(0); char *str; - pCtx->LocalToString(params[arg], &str); + int err; + if ((err=pCtx->LocalToString(params[arg], &str)) != SP_ERROR_NONE) + { + pCtx->ThrowNativeErrorEx(err, "Could not deference string"); + return 0; + } AddString(&buf_p, llen, str, width, prec); arg++; break; diff --git a/core/smn_sorting.cpp b/core/smn_sorting.cpp new file mode 100644 index 00000000..4807598b --- /dev/null +++ b/core/smn_sorting.cpp @@ -0,0 +1,365 @@ +/** + * =============================================================== + * SourceMod (C)2004-2007 AlliedModders LLC. All rights reserved. + * =============================================================== + * + * This file is not open source and may not be copied without explicit + * written permission of AlliedModders LLC. This file may not be redistributed + * in whole or significant part. + * For information, see LICENSE.txt or http://www.sourcemod.net/license.php + * + * Version: $Id$ + */ + +#include "sm_globals.h" +#include +#include + +/*********************************** + * About the double array hack * + *************************** + + Double arrays in Pawn are vectors offset by the current offset. For example: + + new array[2][2] + + In this array, index 0 contains the offset from the current offset which + results in the final vector [2] (at [0][2]). Meaning, to dereference [1][2], + it is equivalent to: + + address = &array[1] + array[1] + 2 * sizeof(cell) + + The fact that each offset is from the _current_ position rather than the _base_ + position is very important. It means that if you to try to swap vector positions, + the offsets will no longer match, because their current position has changed. A + simple and ingenious way around this is to back up the positions in a separate array, + then to overwrite each position in the old array with absolute indices. Pseudo C++ code: + + cell *array; //assumed to be set to the 2+D array + cell *old_offsets = new cell[2]; + for (int i=0; i<2; i++) + { + old_offsets = array[i]; + array[i] = i; + } + + Now, you can swap the array indices with no problem, and do a reverse-lookup to find the original addresses. + After sorting/modification is done, you must relocate the new indices. For example, if the two vectors in our + demo array were swapped, array[0] would be 1 and array[1] would be 0. This is invalid to the virtual machine. + Luckily, this is also simple -- all the information is there. + + for (int i=0; i<2; i++) + { + //get the # of the vector we want to relocate in + cell vector_index = array[i]; + //get the real address of this vector + char *real_address = (char *)array + (vector_index * sizeof(cell)) + old_offsets[vector_index]; + //calc and store the new distance offset + array[i] = real_address - ( (char *)array + (vector_index + sizeof(cell)) ) + } + + Note that the inner expression can be heavily reduced; it is expanded for readability. + **********************************/ + +enum SortOrder +{ + Sort_Ascending = 0, + Sort_Descending = 1, +}; + +int sort_ints_asc(const void *int1, const void *int2) +{ + return (*(int *)int1) - (*(int *)int2); +} + +int sort_ints_desc(const void *int1, const void *int2) +{ + return (*(int *)int2) - (*(int *)int1); +} + +static cell_t sm_SortIntegers(IPluginContext *pContext, const cell_t *params) +{ + cell_t *array; + cell_t array_size = params[2]; + cell_t type = params[3]; + + pContext->LocalToPhysAddr(params[1], &array); + + if (type == Sort_Ascending) + { + qsort(array, array_size, sizeof(cell_t), sort_ints_asc); + } else { + qsort(array, array_size, sizeof(cell_t), sort_ints_desc); + } + + return 1; +} + +int sort_floats_asc(const void *float1, const void *float2) +{ + float r1 = *(float *)float1; + float r2 = *(float *)float2; + + if (r1 < r2) + { + return -1; + } else if (r2 < r1) { + return 1; + } else { + return 0; + } +} + +int sort_floats_desc(const void *float1, const void *float2) +{ + float r1 = *(float *)float1; + float r2 = *(float *)float2; + + if (r1 < r2) + { + return 1; + } else if (r2 < r1) { + return -1; + } else { + return 0; + } +} + +static cell_t sm_SortFloats(IPluginContext *pContext, const cell_t *params) +{ + cell_t *array; + cell_t array_size = params[2]; + cell_t type = params[3]; + + pContext->LocalToPhysAddr(params[1], &array); + + if (type == Sort_Ascending) + { + qsort(array, array_size, sizeof(cell_t), sort_floats_asc); + } else { + qsort(array, array_size, sizeof(cell_t), sort_floats_desc); + } + + return 1; +} + +static cell_t *g_CurStringArray = NULL; +static cell_t *g_CurRebaseMap = NULL; + +int sort_strings_asc(const void *blk1, const void *blk2) +{ + cell_t reloc1 = *(cell_t *)blk1; + cell_t reloc2 = *(cell_t *)blk2; + + char *str1 = ((char *)(&g_CurStringArray[reloc1]) + g_CurRebaseMap[reloc1]); + char *str2 = ((char *)(&g_CurStringArray[reloc2]) + g_CurRebaseMap[reloc2]); + + return strcmp(str1, str2); +} + +int sort_strings_desc(const void *blk1, const void *blk2) +{ + cell_t reloc1 = *(cell_t *)blk1; + cell_t reloc2 = *(cell_t *)blk2; + + char *str1 = ((char *)(&g_CurStringArray[reloc1]) + g_CurRebaseMap[reloc1]); + char *str2 = ((char *)(&g_CurStringArray[reloc2]) + g_CurRebaseMap[reloc2]); + + return strcmp(str2, str1); +} + +static cell_t sm_SortStrings(IPluginContext *pContext, const cell_t *params) +{ + cell_t *array; + cell_t array_size = params[2]; + cell_t type = params[3]; + + pContext->LocalToPhysAddr(params[1], &array); + + /** HACKHACK - back up the old indices, replace the indices with something easier */ + cell_t amx_addr, *phys_addr; + int err; + if ((err=pContext->HeapAlloc(array_size, &amx_addr, &phys_addr)) != SP_ERROR_NONE) + { + pContext->ThrowNativeErrorEx(err, "Ran out of memory to sort"); + } + + g_CurStringArray = array; + g_CurRebaseMap = phys_addr; + + for (int i=0; iHeapPop(amx_addr); + + g_CurStringArray = NULL; + g_CurRebaseMap = NULL; + + return 1; +} + +struct sort_info +{ + IPluginFunction *pFunc; + Handle_t hndl; + cell_t array_addr; + cell_t *array_base; + cell_t *array_remap; +}; + +sort_info g_SortInfo; + +int sort1d_amx_custom(const void *elem1, const void *elem2) +{ + cell_t c1 = *(cell_t *)elem1; + cell_t c2 = *(cell_t *)elem2; + + cell_t result = 0; + IPluginFunction *pf = g_SortInfo.pFunc; + pf->PushCell(c1); + pf->PushCell(c2); + pf->PushCell(g_SortInfo.array_addr); + pf->PushCell(g_SortInfo.hndl); + pf->Execute(&result); + + return result; +} + +static cell_t sm_SortCustom1D(IPluginContext *pContext, const cell_t *params) +{ + cell_t *array; + cell_t array_size = params[2]; + + IPluginFunction *pFunction = pContext->GetFunctionById(params[3]); + if (!pFunction) + { + return pContext->ThrowNativeError("Function %x is not a valid function", params[3]); + } + + pContext->LocalToPhysAddr(params[1], &array); + + sort_info oldinfo = g_SortInfo; + + + g_SortInfo.hndl = params[4]; + g_SortInfo.array_addr = params[1]; + g_SortInfo.array_remap = NULL; + g_SortInfo.array_base = NULL; + g_SortInfo.pFunc = pFunction; + + qsort(array, array_size, sizeof(cell_t), sort1d_amx_custom); + + g_SortInfo = oldinfo; + + return 1; +} + +int sort2d_amx_custom(const void *elem1, const void *elem2) +{ + cell_t c1 = *(cell_t *)elem1; + cell_t c2 = *(cell_t *)elem2; + + cell_t c1_addr = g_SortInfo.array_addr + (c1 * sizeof(cell_t)) + g_SortInfo.array_remap[c1]; + cell_t c2_addr = g_SortInfo.array_addr + (c2 * sizeof(cell_t)) + g_SortInfo.array_remap[c2]; + + IPluginContext *pContext = g_SortInfo.pFunc->GetParentContext(); + cell_t *c1_r, *c2_r; + pContext->LocalToPhysAddr(c1_addr, &c1_r); + pContext->LocalToPhysAddr(c2_addr, &c2_r); + + cell_t result = 0; + g_SortInfo.pFunc->PushCell(c1_addr); + g_SortInfo.pFunc->PushCell(c2_addr); + g_SortInfo.pFunc->PushCell(g_SortInfo.array_addr); + g_SortInfo.pFunc->PushCell(g_SortInfo.hndl); + g_SortInfo.pFunc->Execute(&result); + + return result; +} + +static cell_t sm_SortCustom2D(IPluginContext *pContext, const cell_t *params) +{ + cell_t *array; + cell_t array_size = params[2]; + IPluginFunction *pFunction; + + pContext->LocalToPhysAddr(params[1], &array); + + if ((pFunction=pContext->GetFunctionById(params[3])) == NULL) + { + return pContext->ThrowNativeError("Function %x is not a valid function", params[3]); + } + + /** back up the old indices, replace the indices with something easier */ + cell_t amx_addr, *phys_addr; + int err; + if ((err=pContext->HeapAlloc(array_size, &amx_addr, &phys_addr)) != SP_ERROR_NONE) + { + pContext->ThrowNativeErrorEx(err, "Ran out of memory to sort"); + return 0; + } + + sort_info oldinfo = g_SortInfo; + + g_SortInfo.pFunc = pFunction; + g_SortInfo.hndl = params[4]; + g_SortInfo.array_addr = params[1]; + + /** Same process as in strings, back up the old indices for later fixup */ + g_SortInfo.array_base = array; + g_SortInfo.array_remap = phys_addr; + + for (int i=0; iHeapPop(amx_addr); + + g_SortInfo = oldinfo; + + return 1; +} + +REGISTER_NATIVES(sortNatives) +{ + {"SortIntegers", sm_SortIntegers}, + {"SortFloats", sm_SortFloats}, + {"SortStrings", sm_SortStrings}, + {"SortCustom1D", sm_SortCustom1D}, + {"SortCustom2D", sm_SortCustom2D}, + {NULL, NULL}, +}; diff --git a/plugins/include/sorting.inc b/plugins/include/sorting.inc new file mode 100644 index 00000000..14f689f2 --- /dev/null +++ b/plugins/include/sorting.inc @@ -0,0 +1,113 @@ +/** + * vim: set ts=4 : + * =============================================================== + * SourceMod (C)2004-2007 AlliedModders LLC. All rights reserved. + * =============================================================== + * + * This file is part of the SourceMod/SourcePawn SDK. This file may only be used + * or modified under the Terms and Conditions of its License Agreement, which is found + * in LICENSE.txt. The Terms and Conditions for making SourceMod extensions/plugins + * may change at any time. To view the latest information, see: + * http://www.sourcemod.net/license.php + * + * Version: $Id$ + */ + + +#if defined _sorting_included + #endinput +#endif +#define _sorting_included + +/** + * @brief Contains sorting orders. + */ +enum SortOrder +{ + Sort_Ascending = 0, /**< Ascending order */ + Sort_Descending = 1, /**< Descending order */ +}; + +/** + * Sorts an array of integers. + * + * @param array Array of integers to sort in-place. + * @param array_size Size of the array. + * @param order Sorting order to use. + * @noreturn + */ +native SortIntegers(array[], array_size, SortOrder:order = Sort_Ascending); + +/** + * Sorts an array of float point numbers. + * + * @param array Array of floating point numbers to sort in-place. + * @param array_size Size of the array. + * @param order Sorting order to use. + * @noreturn + */ +native SortFloats(Float:array[], array_size, SortOrder:order = Sort_Ascending); + +/** + * Sorts an array of strings. + * + * @param array Array of strings to sort in-place. + * @param array_size Size of the array. + * @param order Sorting order to use. + * @noreturn + */ +native SortStrings(String:array[][], num_strings, SortOrder:order = Sort_Ascending); + +/** + * Sort comparison function for 1D array elements. + * @note You may need to use explicit tags in order to use data properly. + * + * @param elem1 First element to compare. + * @param elem2 Second element to compare. + * @param array Array that is being sorted (order is undefined). + * @param hndl Handle optionally passed in while sorting. + * @return -1 if first should go before second + * 0 if first is equal to second + * 1 if first should go after second + */ +functag SortFunc1D public(elem1, elem2, const array[], Handle:hndl); + +/** + * Sorts a custom 1D array. You must pass in a comparison function. + * + * @param array Array to sort. + * @param array_size Size of the array to sort. + * @param sortfunc Sort function. + * @param hndl Optional Handle to pass through the comparison calls. + * @noreturn + */ +native SortCustom1D(array[], array_size, SortFunc1D:sortfunc, Handle:hndl=INVALID_HANDLE); + +/** + * Sort comparison function for 2D array elements (sub-arrays). + * @note You may need to use explicit tags in order to use data properly. + * + * @param elem1 First array to compare. + * @param elem2 Second array to compare. + * @param array Array that is being sorted (order is undefined). + * @param hndl Handle optionally passed in while sorting. + * @return -1 if first should go before second + * 0 if first is equal to second + * 1 if first should go after second + */ +funcenum SortFunc2D +{ + public(array[], array[], const array[][], Handle:hndl), + public(String:array[], String:array[], const String:array[][], Handle:hndl), +}; + +/** + * Sorts a custom 2D array. You must pass in a comparison function. + * + * @param array Array to sort. + * @param array_size Size of the major array to sort (first index, outermost). + * @param sortfunc Sort comparison function to use. + * @param hndl Optional Handle to pass through the comparison calls. + * @noreturn + */ +native SortCustom2D(array[][], array_size, SortFunc2D:sortfunc, Handle:hndl=INVALID_HANDLE); \ No newline at end of file diff --git a/plugins/include/sourcemod.inc b/plugins/include/sourcemod.inc index fcdd3eef..4410d151 100644 --- a/plugins/include/sourcemod.inc +++ b/plugins/include/sourcemod.inc @@ -35,6 +35,7 @@ struct Plugin #include #include #include +#include /** * Declare this as a struct in your plugin to expose its information. diff --git a/plugins/testsuite/sorttest.sp b/plugins/testsuite/sorttest.sp new file mode 100644 index 00000000..1398a7d5 --- /dev/null +++ b/plugins/testsuite/sorttest.sp @@ -0,0 +1,164 @@ +#include + +public Plugin:myinfo = +{ + name = "Sorting Test", + author = "AlliedModders LLC", + description = "Tests sorting functions", + version = "1.0.0.0", + url = "http://www.sourcemod.net/" +}; + +public OnPluginStart(Handle:myself) +{ + RegServerCmd("test_sort_ints", Command_TestSortInts) + RegServerCmd("test_sort_floats", Command_TestSortFloats) + RegServerCmd("test_sort_strings", Command_TestSortStrings) + RegServerCmd("test_sort_1d", Command_TestSort1D) + RegServerCmd("test_sort_2d", Command_TestSort2D) +} + +/***************** + * INTEGER TESTS * + *****************/ +// Note that integer comparison is just int1-int2 (or a variation therein) + +PrintIntegers(const array[], size) +{ + for (new i=0; i f2) + { + return -1; + } else if (f1 < f2) { + return 1; + } + + return 0; +} + +public Action:Command_TestSort1D(args) +{ + new Float:array[10] = {6.3, 7.6, 3.2, 2.1, 8.5, 5.2, 0.4, 1.7, 4.8, 8.2} + + SortCustom1D(_:array, 10, Custom1DSort) + PrintFloats(array, 10) + + return Plugin_Handled +} + +/*************************** + * String comparison tests * + ***************************/ + +PrintStrings(const String:array[][], size) +{ + for (new i=0; i