From 9e1d2ae26aac6ce4ea294ab895906e0b34cabcb5 Mon Sep 17 00:00:00 2001 From: 42 <33488710+FortyTwoFortyTwo@users.noreply.github.com> Date: Sat, 16 Nov 2019 23:23:07 +0000 Subject: [PATCH 1/3] Allow custom sort natives and callback use any types and optional data --- core/logic/smn_sorting.cpp | 16 ++++++++-------- plugins/include/adt_array.inc | 4 ++-- plugins/include/sorting.inc | 30 +++++++++++++++++++----------- 3 files changed, 29 insertions(+), 21 deletions(-) diff --git a/core/logic/smn_sorting.cpp b/core/logic/smn_sorting.cpp index b67ae0fd83..f94cae9813 100644 --- a/core/logic/smn_sorting.cpp +++ b/core/logic/smn_sorting.cpp @@ -280,7 +280,7 @@ static cell_t sm_SortStrings(IPluginContext *pContext, const cell_t *params) struct sort_info { IPluginFunction *pFunc; - Handle_t hndl; + cell_t data; cell_t array_addr; cell_t *array_base; cell_t *array_remap; @@ -302,7 +302,7 @@ int sort1d_amx_custom(const void *elem1, const void *elem2) pf->PushCell(c1); pf->PushCell(c2); pf->PushCell(g_SortInfo.array_addr); - pf->PushCell(g_SortInfo.hndl); + pf->PushCell(g_SortInfo.data); pf->Invoke(&result); return result; @@ -324,7 +324,7 @@ static cell_t sm_SortCustom1D(IPluginContext *pContext, const cell_t *params) sort_info oldinfo = g_SortInfo; DetectExceptions eh(pContext); - g_SortInfo.hndl = params[4]; + g_SortInfo.data = params[4]; g_SortInfo.array_addr = params[1]; g_SortInfo.array_remap = NULL; g_SortInfo.array_base = NULL; @@ -357,7 +357,7 @@ int sort2d_amx_custom(const void *elem1, const void *elem2) 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->PushCell(g_SortInfo.data); g_SortInfo.pFunc->Invoke(&result); return result; @@ -389,7 +389,7 @@ static cell_t sm_SortCustom2D(IPluginContext *pContext, const cell_t *params) DetectExceptions eh(pContext); g_SortInfo.pFunc = pFunction; - g_SortInfo.hndl = params[4]; + g_SortInfo.data = params[4]; g_SortInfo.array_addr = params[1]; g_SortInfo.eh = &eh; @@ -521,7 +521,7 @@ struct sort_infoADT cell_t *array_base; cell_t array_bsize; Handle_t array_hndl; - Handle_t hndl; + cell_t data; ExceptionHandler *eh; }; @@ -537,7 +537,7 @@ int sort_adtarray_custom(const void *elem1, const void *elem2) pf->PushCell(((cell_t) ((cell_t *) elem1 - g_SortInfoADT.array_base)) / g_SortInfoADT.array_bsize); pf->PushCell(((cell_t) ((cell_t *) elem2 - g_SortInfoADT.array_base)) / g_SortInfoADT.array_bsize); pf->PushCell(g_SortInfoADT.array_hndl); - pf->PushCell(g_SortInfoADT.hndl); + pf->PushCell(g_SortInfoADT.data); pf->Invoke(&result); return result; @@ -572,7 +572,7 @@ static cell_t sm_SortADTArrayCustom(IPluginContext *pContext, const cell_t *para g_SortInfoADT.array_base = array; g_SortInfoADT.array_bsize = (cell_t) blocksize; g_SortInfoADT.array_hndl = params[1]; - g_SortInfoADT.hndl = params[3]; + g_SortInfoADT.data = params[3]; g_SortInfoADT.eh = &eh; qsort(array, arraysize, blocksize * sizeof(cell_t), sort_adtarray_custom); diff --git a/plugins/include/adt_array.inc b/plugins/include/adt_array.inc index 7056e33601..df41ce94a0 100644 --- a/plugins/include/adt_array.inc +++ b/plugins/include/adt_array.inc @@ -219,8 +219,8 @@ methodmap ArrayList < Handle { // Custom sorts an ADT Array. You must pass in a comparison function. // // @param sortfunc Sort comparison function to use - // @param hndl Optional Handle to pass through the comparison calls. - public native void SortCustom(SortFuncADTArray sortfunc, Handle hndl=INVALID_HANDLE); + // @param data Optional data to pass through the comparison calls. + public native void SortCustom(SortFuncADTArray sortfunc, any data=0); // Retrieve the size of the array. property int Length { diff --git a/plugins/include/sorting.inc b/plugins/include/sorting.inc index ee777d8666..6f3787cc87 100644 --- a/plugins/include/sorting.inc +++ b/plugins/include/sorting.inc @@ -90,12 +90,16 @@ native void SortStrings(char[][] array, int array_size, SortOrder order = Sort_A * @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. + * @param data Data 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 */ -typedef SortFunc1D = function int (int elem1, int elem2, const int[] array, Handle hndl); +typeset SortFunc1D +{ + function int (any elem1, any elem2, const any[] array); + function int (any elem1, any elem2, const any[] array, any data); +} /** * Sorts a custom 1D array. You must pass in a comparison function. @@ -103,9 +107,9 @@ typedef SortFunc1D = function int (int elem1, int elem2, const int[] array, Hand * @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. + * @param data Optional data to pass through the comparison calls. */ -native void SortCustom1D(int[] array, int array_size, SortFunc1D sortfunc, Handle hndl=INVALID_HANDLE); +native void SortCustom1D(any[] array, int array_size, SortFunc1D sortfunc, any data=0); /** * Sort comparison function for 2D array elements (sub-arrays). @@ -114,15 +118,15 @@ native void SortCustom1D(int[] array, int array_size, SortFunc1D sortfunc, Handl * @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. + * @param data Data 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 */ typeset SortFunc2D { - function int (int[] elem1, int[] elem2, const int[][] array, Handle hndl); - function int (char[] elem1, char[] elem2, const char[][] array, Handle hndl); + function int (any[] elem1, any[] elem2, const any[][] array); + function int (any[] elem1, any[] elem2, const any[][] array, any data); }; /** @@ -131,9 +135,9 @@ typeset SortFunc2D * @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. + * @param data Optional data to pass through the comparison calls. */ -native void SortCustom2D(any[][] array, int array_size, SortFunc2D sortfunc, Handle hndl=INVALID_HANDLE); +native void SortCustom2D(any[][] array, int array_size, SortFunc2D sortfunc, any data=0); /** * Sort an ADT Array. Specify the type as Integer, Float, or String. @@ -152,12 +156,16 @@ native void SortADTArray(Handle array, SortOrder order, SortType type); * @param index1 First index to compare. * @param index2 Second index to compare. * @param array Array that is being sorted (order is undefined). - * @param hndl Handle optionally passed in while sorting. + * @param data Data 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 */ -typedef SortFuncADTArray = function int (int index1, int index2, Handle array, Handle hndl); +typeset SortFuncADTArray +{ + function int (int index1, int index2, ArrayList array); + function int (int index1, int index2, ArrayList array, any data); +} /** * Custom sorts an ADT Array. You must pass in a comparison function. From dc95eab89032aa72525533b29d2c4df89601d0b0 Mon Sep 17 00:00:00 2001 From: FortyTwoFortyTwo Date: Wed, 27 Oct 2021 16:22:04 +0100 Subject: [PATCH 2/3] Fix compile from master branch changes --- core/logic/smn_sorting.cpp | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/core/logic/smn_sorting.cpp b/core/logic/smn_sorting.cpp index affd2a7b4e..97cf6158f1 100644 --- a/core/logic/smn_sorting.cpp +++ b/core/logic/smn_sorting.cpp @@ -495,7 +495,7 @@ static int sort2d_amx_custom(const void *elem1, const void *elem2) g_SortInfo.pFunc->PushCell(iv1); g_SortInfo.pFunc->PushCell(iv2); g_SortInfo.pFunc->PushCell(g_SortInfo.array_addr); - g_SortInfo.pFunc->PushCell(g_SortInfo.hndl); + g_SortInfo.pFunc->PushCell(g_SortInfo.data); g_SortInfo.pFunc->Invoke(&result); return result; @@ -522,7 +522,7 @@ static cell_t sm_SortCustom2D(IPluginContext *pContext, const cell_t *params) DetectExceptions eh(pContext); g_SortInfo.pFunc = pFunction; - g_SortInfo.hndl = params[4]; + g_SortInfo.data = params[4]; g_SortInfo.array_addr = params[1]; g_SortInfo.eh = &eh; From 0af17b5565173a135b7474a340dd8ea5baef9ea5 Mon Sep 17 00:00:00 2001 From: FortyTwoFortyTwo Date: Wed, 5 Jun 2024 11:15:53 +0100 Subject: [PATCH 3/3] Bring back char support in SortFunc2D callback --- plugins/include/sorting.inc | 2 ++ 1 file changed, 2 insertions(+) diff --git a/plugins/include/sorting.inc b/plugins/include/sorting.inc index 6f3787cc87..50c3cde3de 100644 --- a/plugins/include/sorting.inc +++ b/plugins/include/sorting.inc @@ -127,6 +127,8 @@ typeset SortFunc2D { function int (any[] elem1, any[] elem2, const any[][] array); function int (any[] elem1, any[] elem2, const any[][] array, any data); + function int (char[] elem1, char[] elem2, const char[][] array); + function int (char[] elem1, char[] elem2, const char[][] array, any data); }; /**