From 2a06680252324fc8f81fc717027de7cc10f320d1 Mon Sep 17 00:00:00 2001 From: David Anderson Date: Mon, 25 Oct 2021 22:31:09 -0700 Subject: [PATCH] Update to the new SourcePawn compiler. This imports the brand new SourcePawn compiler. The new compiler is much faster to compile and generates significantly improved code around array generation and array access. There are a number of compatibility changes in the new compiler. Most of these are due to improved type checking and error detection. The full list of notes can be found here: https://github.com/alliedmodders/sourcepawn/blob/master/docs/upgrading-1.11.md Additionally, .smx files generated by the new compiler will NOT load on earlier versions of SourceMod, including earlier versions of 1.11. Old plugins will continue to load as normal. --- core/logic/smn_sorting.cpp | 143 +++++++++++++++++++++++++++++++++---- plugins/mapchooser.sp | 2 +- public/amtl | 2 +- sourcepawn | 2 +- 4 files changed, 131 insertions(+), 18 deletions(-) diff --git a/core/logic/smn_sorting.cpp b/core/logic/smn_sorting.cpp index 3499e4b5..82ad1958 100644 --- a/core/logic/smn_sorting.cpp +++ b/core/logic/smn_sorting.cpp @@ -35,6 +35,7 @@ #include "common_logic.h" #include "CellArray.h" #include +#include /*********************************** * About the double array hack * @@ -95,7 +96,7 @@ void sort_random(cell_t *array, cell_t size) { int n = rand() % (i + 1); - if (array[i] != array[n]) + if (array[i] != array[n]) { array[i] ^= array[n]; array[n] ^= array[i]; @@ -125,7 +126,7 @@ static cell_t sm_SortIntegers(IPluginContext *pContext, const cell_t *params) if (type == Sort_Ascending) { qsort(array, array_size, sizeof(cell_t), sort_ints_asc); - } + } else if (type == Sort_Descending) { qsort(array, array_size, sizeof(cell_t), sort_ints_desc); @@ -195,7 +196,7 @@ static cell_t sm_SortFloats(IPluginContext *pContext, const cell_t *params) static cell_t *g_CurStringArray = NULL; static cell_t *g_CurRebaseMap = NULL; -int sort_strings_asc(const void *blk1, const void *blk2) +int sort_strings_asc_legacy(const void *blk1, const void *blk2) { cell_t reloc1 = *(cell_t *)blk1; cell_t reloc2 = *(cell_t *)blk2; @@ -206,7 +207,7 @@ int sort_strings_asc(const void *blk1, const void *blk2) return strcmp(str1, str2); } -int sort_strings_desc(const void *blk1, const void *blk2) +int sort_strings_desc_legacy(const void *blk1, const void *blk2) { cell_t reloc1 = *(cell_t *)blk1; cell_t reloc2 = *(cell_t *)blk2; @@ -217,7 +218,7 @@ int sort_strings_desc(const void *blk1, const void *blk2) return strcmp(str2, str1); } -static cell_t sm_SortStrings(IPluginContext *pContext, const cell_t *params) +static cell_t sm_SortStrings_Legacy(IPluginContext *pContext, const cell_t *params) { cell_t *array; cell_t array_size = params[2]; @@ -245,11 +246,11 @@ static cell_t sm_SortStrings(IPluginContext *pContext, const cell_t *params) if (type == Sort_Ascending) { - qsort(array, array_size, sizeof(cell_t), sort_strings_asc); - } + qsort(array, array_size, sizeof(cell_t), sort_strings_asc_legacy); + } else if (type == Sort_Descending) { - qsort(array, array_size, sizeof(cell_t), sort_strings_desc); + qsort(array, array_size, sizeof(cell_t), sort_strings_desc_legacy); } else { @@ -275,6 +276,69 @@ static cell_t sm_SortStrings(IPluginContext *pContext, const cell_t *params) return 1; } +static IPluginContext* sSortContext = nullptr; + +int sort_strings_asc(const void *blk1, const void *blk2) +{ + cell_t str_addr1 = *(cell_t *)blk1; + cell_t str_addr2 = *(cell_t *)blk2; + + char *str1; + char *str2; + if (sSortContext->LocalToString(str_addr1, &str1) != SP_ERROR_NONE || + sSortContext->LocalToString(str_addr2, &str2) != SP_ERROR_NONE) + { + return 0; + } + + return strcmp(str1, str2); +} + +int sort_strings_desc(const void *blk1, const void *blk2) +{ + cell_t str_addr1 = *(cell_t *)blk1; + cell_t str_addr2 = *(cell_t *)blk2; + + char *str1; + char *str2; + if (sSortContext->LocalToString(str_addr1, &str1) != SP_ERROR_NONE || + sSortContext->LocalToString(str_addr2, &str2) != SP_ERROR_NONE) + { + return 0; + } + + return strcmp(str2, str1); +} + +static cell_t sm_SortStrings(IPluginContext *pContext, const cell_t *params) +{ + auto rt = pContext->GetRuntime(); + if (!rt->UsesDirectArrays()) + return sm_SortStrings_Legacy(pContext, params); + + cell_t *array; + cell_t array_size = params[2]; + cell_t type = params[3]; + + pContext->LocalToPhysAddr(params[1], &array); + + ke::SaveAndSet set_context(&sSortContext, pContext); + + if (type == Sort_Ascending) + { + qsort(array, array_size, sizeof(cell_t), sort_strings_asc); + } + else if (type == Sort_Descending) + { + qsort(array, array_size, sizeof(cell_t), sort_strings_desc); + } + else + { + sort_random(array, array_size); + } + return 1; +} + struct sort_info { IPluginFunction *pFunc; @@ -335,7 +399,7 @@ static cell_t sm_SortCustom1D(IPluginContext *pContext, const cell_t *params) return 1; } -int sort2d_amx_custom(const void *elem1, const void *elem2) +static int sort2d_amx_custom_legacy(const void *elem1, const void *elem2) { if (g_SortInfo.eh->HasException()) return 0; @@ -361,7 +425,7 @@ int sort2d_amx_custom(const void *elem1, const void *elem2) return result; } -static cell_t sm_SortCustom2D(IPluginContext *pContext, const cell_t *params) +static cell_t sm_SortCustom2D_Legacy(IPluginContext *pContext, const cell_t *params) { cell_t *array; cell_t array_size = params[2]; @@ -401,7 +465,7 @@ static cell_t sm_SortCustom2D(IPluginContext *pContext, const cell_t *params) array[i] = i; } - qsort(array, array_size, sizeof(cell_t), sort2d_amx_custom); + qsort(array, array_size, sizeof(cell_t), sort2d_amx_custom_legacy); /** Fixup process! */ for (int i=0; iHasException()) + return 0; + + cell_t iv1 = *(cell_t *)elem1; + cell_t iv2 = *(cell_t *)elem2; + + cell_t result = 0; + 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->Invoke(&result); + + return result; +} + +static cell_t sm_SortCustom2D(IPluginContext *pContext, const cell_t *params) +{ + auto rt = pContext->GetRuntime(); + if (!rt->UsesDirectArrays()) + return sm_SortCustom2D_Legacy(pContext, 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]); + } + + sort_info oldinfo = g_SortInfo; + + DetectExceptions eh(pContext); + g_SortInfo.pFunc = pFunction; + g_SortInfo.hndl = params[4]; + g_SortInfo.array_addr = params[1]; + g_SortInfo.eh = &eh; + + qsort(array, array_size, sizeof(cell_t), sort2d_amx_custom); + + g_SortInfo = oldinfo; + return 1; +} + enum SortType { Sort_Integer = 0, @@ -454,7 +567,7 @@ static cell_t sm_SortADTArray(IPluginContext *pContext, const cell_t *params) HandleError err; HandleSecurity sec(pContext->GetIdentity(), g_pCoreIdent); - if ((err = handlesys->ReadHandle(params[1], htCellArray, &sec, (void **)&cArray)) + if ((err = handlesys->ReadHandle(params[1], htCellArray, &sec, (void **)&cArray)) != HandleError_None) { return pContext->ThrowNativeError("Invalid Handle %x (error: %d)", params[1], err); @@ -491,7 +604,7 @@ static cell_t sm_SortADTArray(IPluginContext *pContext, const cell_t *params) { qsort(array, arraysize, blocksize * sizeof(cell_t), sort_floats_asc); } - else + else { qsort(array, arraysize, blocksize * sizeof(cell_t), sort_floats_desc); } @@ -502,7 +615,7 @@ static cell_t sm_SortADTArray(IPluginContext *pContext, const cell_t *params) { qsort(array, arraysize, blocksize * sizeof(cell_t), sort_adtarray_strings_asc); } - else + else { qsort(array, arraysize, blocksize * sizeof(cell_t), sort_adtarray_strings_desc); } @@ -545,7 +658,7 @@ static cell_t sm_SortADTArrayCustom(IPluginContext *pContext, const cell_t *para HandleError err; HandleSecurity sec(pContext->GetIdentity(), g_pCoreIdent); - if ((err = handlesys->ReadHandle(params[1], htCellArray, &sec, (void **)&cArray)) + if ((err = handlesys->ReadHandle(params[1], htCellArray, &sec, (void **)&cArray)) != HandleError_None) { return pContext->ThrowNativeError("Invalid Handle %x (error: %d)", params[1], err); diff --git a/plugins/mapchooser.sp b/plugins/mapchooser.sp index 439cc3a5..4cb1c799 100644 --- a/plugins/mapchooser.sp +++ b/plugins/mapchooser.sp @@ -1286,7 +1286,7 @@ void WritePreviousMapsToText() file.Close(); } -char GetTextFilePath() +char[] GetTextFilePath() { static char path[PLATFORM_MAX_PATH]; if (path[0] == '\0') diff --git a/public/amtl b/public/amtl index 9472470b..636074b7 160000 --- a/public/amtl +++ b/public/amtl @@ -1 +1 @@ -Subproject commit 9472470b1a668c2d292d304136ce6280a7fd966b +Subproject commit 636074b70532e85677d593fc6f9c70a05b7fa5c1 diff --git a/sourcepawn b/sourcepawn index 6c6d8648..1d8f052a 160000 --- a/sourcepawn +++ b/sourcepawn @@ -1 +1 @@ -Subproject commit 6c6d8648423a5db6d2b3b097efb03cc8fd80f9a9 +Subproject commit 1d8f052af6e12eacf8b93ce8ec98113f6735e4e0