diff --git a/SDK b/SDK index eaa4cff25..1d1e78ca7 160000 --- a/SDK +++ b/SDK @@ -1 +1 @@ -Subproject commit eaa4cff250e7e31ef4d13a5f487312154e6512c8 +Subproject commit 1d1e78ca7b6dde02a6a89e8c312501cce255a06e diff --git a/Server/Components/Pawn/Script/Script.cpp b/Server/Components/Pawn/Script/Script.cpp index 6855d65b1..bd4076595 100644 --- a/Server/Components/Pawn/Script/Script.cpp +++ b/Server/Components/Pawn/Script/Script.cpp @@ -381,6 +381,15 @@ __attribute__((noinline)) int amx_FindPublic_impl(AMX* amx, const char* name, in __attribute__((noinline)) int AMXAPI amx_Register_impl(AMX* amx, const AMX_NATIVE_INFO* list, int number) { + // Register all natives in the global registry + if (list != NULL) + { + for (int j = 0; (j < number || number == -1) && list[j].name != NULL; j++) + { + GlobalNativeRegistry::RegisterNative(list[j].name, list[j].func); + } + } + AMX_FUNCPART* func; AMX_HEADER* hdr; int i, numnatives, err; diff --git a/Server/Components/Pawn/Script/Script.hpp b/Server/Components/Pawn/Script/Script.hpp index 32f56182f..b8ea1410b 100644 --- a/Server/Components/Pawn/Script/Script.hpp +++ b/Server/Components/Pawn/Script/Script.hpp @@ -17,8 +17,10 @@ #include #include +#include #include #include +#include #include #include @@ -32,6 +34,28 @@ struct AMXCache FlatHashMap publics; ///< A cache of AMX publics }; +// Global pawn native registry for all registered pawn natives +struct GlobalNativeRegistry +{ + static FlatHashMap& GetRegistry() + { + static FlatHashMap registry; + return registry; + } + + static void RegisterNative(const char* name, AMX_NATIVE func) + { + GetRegistry()[String(name)] = func; + } + + static AMX_NATIVE FindNative(const char* name) + { + auto& registry = GetRegistry(); + auto it = registry.find(String(name)); + return (it != registry.end()) ? it->second : nullptr; + } +}; + class PawnScript : public IPawnScript { public: @@ -108,6 +132,273 @@ class PawnScript : public IPawnScript void tryLoad(std::string const& path); + /// Find native from global registry (works even if script doesn't use it) + AMX_NATIVE FindNativeInRegistry(char const* name) const { return GlobalNativeRegistry::FindNative(name); } + + cell CallNativeArray(const char* name, Span params) override + { + return CallNativeArrayImpl(name, params); + } + +private: + cell CallNativeArrayImpl(const char* name, Span params) + { + AMX_NATIVE native = FindNativeInRegistry(name); + if (!native) + { + return 0; + } + + size_t argCount = params.size(); + if (argCount == 0) + { + cell paramsArray[1] = { 0 }; + return native(const_cast(&amx_), paramsArray); + } + + DynamicArray paramsArray(argCount + 1); + paramsArray[0] = argCount * sizeof(cell); + + cell amx_addr_save = GetHEA(); + + struct RefParamInfo + { + cell amx_addr; + void* ref_obj; + enum + { + INT, + FLOAT, + BOOL, + STRING, + VECTOR_INT, + VECTOR_FLOAT + } type; + size_t arraySize; + std::function readback; + }; + + DynamicArray refParams; + + for (size_t i = 0; i < argCount; ++i) + { + const auto& param = params[i]; + cell amx_addr; + cell* phys_addr; + + using ParamType = Impl::NativeParam::Type; + + switch (param.type) + { + case ParamType::Int: + paramsArray[i + 1] = static_cast(param.intValue); + break; + + case ParamType::Float: + paramsArray[i + 1] = amx_ftoc(param.floatValue); + break; + + case ParamType::Bool: + paramsArray[i + 1] = param.boolValue ? 1 : 0; + break; + + case ParamType::String: + PushString(&amx_addr, nullptr, StringView(param.stringValue), false, false); + paramsArray[i + 1] = amx_addr; + break; + + case ParamType::ArrayInt: + { + const int* arr = static_cast(param.arrayPtr); + DynamicArray cellArray(param.arraySize); + for (size_t j = 0; j < param.arraySize; ++j) + { + cellArray[j] = static_cast(arr[j]); + } + PushArray(&amx_addr, nullptr, cellArray.data(), cellArray.size()); + paramsArray[i + 1] = amx_addr; + break; + } + + case ParamType::ArrayFloat: + { + const float* arr = static_cast(param.arrayPtr); + DynamicArray cellArray(param.arraySize); + for (size_t j = 0; j < param.arraySize; ++j) + { + cellArray[j] = amx_ftoc(arr[j]); + } + PushArray(&amx_addr, nullptr, cellArray.data(), cellArray.size()); + paramsArray[i + 1] = amx_addr; + break; + } + + case ParamType::RefInt: + { + auto* ref = static_cast*>(param.refPtr); + Allot(1, &amx_addr, &phys_addr); + *phys_addr = static_cast(ref->get()); + refParams.push_back({ amx_addr, param.refPtr, RefParamInfo::INT, 0, nullptr }); + paramsArray[i + 1] = amx_addr; + break; + } + + case ParamType::RefFloat: + { + auto* ref = static_cast*>(param.refPtr); + Allot(1, &amx_addr, &phys_addr); + auto val = ref->get(); + *phys_addr = amx_ftoc(val); + refParams.push_back({ amx_addr, param.refPtr, RefParamInfo::FLOAT, 0, nullptr }); + paramsArray[i + 1] = amx_addr; + break; + } + + case ParamType::RefBool: + { + auto* ref = static_cast*>(param.refPtr); + Allot(1, &amx_addr, &phys_addr); + *phys_addr = ref->get() ? 1 : 0; + refParams.push_back({ amx_addr, param.refPtr, RefParamInfo::BOOL, 0, nullptr }); + paramsArray[i + 1] = amx_addr; + break; + } + + case ParamType::RefString: + { + auto* ref = static_cast*>(param.refPtr); + size_t bufferSize = param.arraySize; + Allot(bufferSize, &amx_addr, &phys_addr); + + const String& currentStr = ref->get(); + if (!currentStr.empty()) + { + SetString(phys_addr, StringView(currentStr.c_str(), currentStr.length()), false, false, bufferSize); + } + else + { + *phys_addr = 0; + } + + refParams.push_back({ amx_addr, param.refPtr, RefParamInfo::STRING, bufferSize, nullptr }); + paramsArray[i + 1] = amx_addr; + break; + } + + case ParamType::RefArrayInt: + { + auto* ref = static_cast>*>(param.refPtr); + size_t bufferSize = param.arraySize; + Allot(bufferSize, &amx_addr, &phys_addr); + + const auto& currentVec = ref->get(); + size_t initSize = std::min(currentVec.size(), bufferSize); + for (size_t j = 0; j < initSize; ++j) + { + phys_addr[j] = static_cast(currentVec[j]); + } + + auto readback = [ref, bufferSize](cell* data) + { + auto& vec = ref->ref(); + vec.resize(bufferSize, 0); + for (size_t j = 0; j < bufferSize; ++j) + { + vec[j] = static_cast(data[j]); + } + }; + + refParams.push_back({ amx_addr, param.refPtr, RefParamInfo::VECTOR_INT, bufferSize, readback }); + paramsArray[i + 1] = amx_addr; + break; + } + + case ParamType::RefArrayFloat: + { + auto* ref = static_cast>*>(param.refPtr); + size_t bufferSize = param.arraySize; + Allot(bufferSize, &amx_addr, &phys_addr); + + const auto& currentVec = ref->get(); + size_t initSize = std::min(currentVec.size(), bufferSize); + for (size_t j = 0; j < initSize; ++j) + { + phys_addr[j] = amx_ftoc(currentVec[j]); + } + + auto readback = [ref, bufferSize](cell* data) + { + auto& vec = ref->ref(); + vec.resize(bufferSize, 0.0f); + for (size_t j = 0; j < bufferSize; ++j) + { + vec[j] = amx_ctof(data[j]); + } + }; + + refParams.push_back({ amx_addr, param.refPtr, RefParamInfo::VECTOR_FLOAT, bufferSize, readback }); + paramsArray[i + 1] = amx_addr; + break; + } + } + } + + cell result = native(const_cast(&amx_), paramsArray.data()); + + // Read back reference parameters + for (auto& refInfo : refParams) + { + cell* phys_addr; + GetAddr(refInfo.amx_addr, &phys_addr); + + if (phys_addr) + { + switch (refInfo.type) + { + case RefParamInfo::FLOAT: + { + auto* ref = static_cast*>(refInfo.ref_obj); + ref->ref() = amx_ctof(*phys_addr); + break; + } + case RefParamInfo::BOOL: + { + auto* ref = static_cast*>(refInfo.ref_obj); + ref->ref() = (*phys_addr != 0); + break; + } + case RefParamInfo::INT: + { + auto* ref = static_cast*>(refInfo.ref_obj); + ref->ref() = static_cast(*phys_addr); + break; + } + case RefParamInfo::STRING: + { + auto* ref = static_cast*>(refInfo.ref_obj); + DynamicArray buffer(refInfo.arraySize + 1); + GetString(buffer.data(), phys_addr, false, refInfo.arraySize); + ref->ref() = String(buffer.data()); + break; + } + case RefParamInfo::VECTOR_INT: + case RefParamInfo::VECTOR_FLOAT: + { + if (refInfo.readback) + { + refInfo.readback(phys_addr); + } + break; + } + } + } + } + + Release(amx_addr_save); + + return result; + } + private: ICore* serverCore; AMX amx_;