From 13865ec3c57f47599d5f3d40257ae54d2d900640 Mon Sep 17 00:00:00 2001 From: rejchev Date: Fri, 18 Aug 2023 21:13:22 +0500 Subject: [PATCH] self token getter added --- extension.cpp | 8 ++++---- extension.h | 4 ++-- natives.cpp | 28 +++++++++++++++++++++++++++ natives.h | 1 + pawn/scripting/clown-core-test.sp | 6 +++--- pawn/scripting/include/clown-core.inc | 3 +++ 6 files changed, 41 insertions(+), 9 deletions(-) diff --git a/extension.cpp b/extension.cpp index 779878b..5f7bccc 100644 --- a/extension.cpp +++ b/extension.cpp @@ -62,13 +62,13 @@ void ClownCore::OnCoreMapStart(edict_t *pEdictList, int edictCount, int clientMa if(object == nullptr) return; - if(m_pConfig != nullptr) + if(pConfig != nullptr) { - json_decref(m_pConfig); - m_pConfig = nullptr; + json_decref(pConfig); + pConfig = nullptr; } - m_pConfig = object; + pConfig = object; IExtensionInterface::OnCoreMapStart(pEdictList, edictCount, clientMax); } diff --git a/extension.h b/extension.h index 316d981..ccbd07c 100644 --- a/extension.h +++ b/extension.h @@ -37,8 +37,8 @@ class ClownCore : public SDKExtension // void OnCore -private: - json_t* m_pConfig = nullptr; +public: + static json_t* pConfig; }; extern ClownCore g_ClownCoreExt; diff --git a/natives.cpp b/natives.cpp index d2780a4..d058459 100644 --- a/natives.cpp +++ b/natives.cpp @@ -1,3 +1,4 @@ +#include "extension.h" #include "natives.h" cell_t SendData(IPluginContext *pContext, const cell_t *params) @@ -22,8 +23,35 @@ cell_t SendSignal(IPluginContext *pContext, const cell_t *params) return OnDataReceived(path, data); } +cell_t ClownGetMyKey(IPluginContext *pContext, const cell_t *params) +{ + char *buffer; + pContext->LocalToString(params[1], &buffer); + + auto& size = (size_t&)params[2]; + + static const char* TOKEN = "api.key"; + + json_t* obj; + if(!(obj = json_object_get(ClownCore::pConfig, TOKEN))) + return 0; + + const char* str = nullptr; + if(json_is_string(obj) && (str = json_string_value(obj)) != nullptr) + ke::SafeStrcpyN(buffer, size, str, strlen(str) + 1); + + bool done; + if((done = str != nullptr)) + free((void*) str); + + json_decref(obj); + + return done; +} + const sp_nativeinfo_t natives[] = { { "ClownCore.SendData", SendData }, { "ClownCore.SendSignal", SendSignal }, + { "ClownCore.GetMyToken", ClownGetMyKey }, { nullptr, nullptr } }; \ No newline at end of file diff --git a/natives.h b/natives.h index dbe16b4..fad1b31 100644 --- a/natives.h +++ b/natives.h @@ -7,6 +7,7 @@ extern cell_t SendData(SourcePawn::IPluginContext *pContext, const cell_t *params); extern cell_t SendSignal(SourcePawn::IPluginContext *pContext, const cell_t *params); +extern cell_t ClownGetMyKey(SourcePawn::IPluginContext *pContext, const cell_t *params); extern const sp_nativeinfo_t natives[]; diff --git a/pawn/scripting/clown-core-test.sp b/pawn/scripting/clown-core-test.sp index e519261..0466ca4 100644 --- a/pawn/scripting/clown-core-test.sp +++ b/pawn/scripting/clown-core-test.sp @@ -2,12 +2,12 @@ #include +#define PING "core.ping" + public void OnMapStart() { - char data[] = "Hello"; + ClownCore.SendSignal(PING, ); - LogMessage("OnReceiveCode: %d", ClownCore.SendData("self.ping", data, sizeof(data))); - LogMessage("OnDataReceived: %s", data); } public DataAction clown_OnDataSent(const char[] path, char[] data, int maxLen) { diff --git a/pawn/scripting/include/clown-core.inc b/pawn/scripting/include/clown-core.inc index 4977073..23b98a7 100644 --- a/pawn/scripting/include/clown-core.inc +++ b/pawn/scripting/include/clown-core.inc @@ -37,6 +37,9 @@ methodmap ClownCore // // @return custom things public static native DataAction SendSignal(const char[] path, const char[] data = NULL_STRING); + + // @return Self API key + public static native bool GetMyToken(char[] buffer, int maxLen); }; /**