From 2d336798e25913fe859e60ad061d072b02115b7b Mon Sep 17 00:00:00 2001 From: BotoX Date: Sat, 12 Oct 2019 20:25:26 +0200 Subject: [PATCH] SMJSONAPI: Add Forwards --- SMJSONAPI/scripting/Forwards.inc | 13 +++ SMJSONAPI/scripting/Subscribe.inc | 159 ++++++++++++++++++++++++++++++ 2 files changed, 172 insertions(+) create mode 100644 SMJSONAPI/scripting/Forwards.inc diff --git a/SMJSONAPI/scripting/Forwards.inc b/SMJSONAPI/scripting/Forwards.inc new file mode 100644 index 00000000..9821ab06 --- /dev/null +++ b/SMJSONAPI/scripting/Forwards.inc @@ -0,0 +1,13 @@ + +public void OnClientPostAdminCheck(int client) +{ + JSONObject jEvent = new JSONObject(); + jEvent.SetString("name", "OnClientPostAdminCheck"); + + JSONObject jEventData = new JSONObject(); + jEventData.SetInt("client", client); + + jEvent.Set("data", jEventData); + + Subscribe_Forwards_Publish("OnClientPostAdminCheck", jEvent); +} diff --git a/SMJSONAPI/scripting/Subscribe.inc b/SMJSONAPI/scripting/Subscribe.inc index ab5e9976..f9bbb151 100644 --- a/SMJSONAPI/scripting/Subscribe.inc +++ b/SMJSONAPI/scripting/Subscribe.inc @@ -1,4 +1,5 @@ #include "GameEvents.inc" +#include "Forwards.inc" #define MAX_SUBSCRIBERS MAX_CLIENTS @@ -7,12 +8,14 @@ enum eSubscribeError_OutOfRange = -1, eSubscribeError_Inactive = -2, eSubscribeError_GameEvent = -3, + eSubscribeError_Forward = -4, } static bool g_Subscriber_Active[MAX_SUBSCRIBERS] = { false, ... }; static int g_Subscriber_Client[MAX_SUBSCRIBERS] = { -1, ... }; static ArrayList g_Subscriber_GameEvents[MAX_SUBSCRIBERS]; +static ArrayList g_Subscriber_Forwards[MAX_SUBSCRIBERS]; bool Subscribe_OnPluginStart() { @@ -33,6 +36,7 @@ int Subscriber_Create(int Client) g_Subscriber_Client[Index] = Client; g_Subscriber_GameEvents[Index] = new ArrayList(ByteCountToCells(32)); + g_Subscriber_Forwards[Index] = new ArrayList(ByteCountToCells(32)); return Index; } @@ -53,6 +57,7 @@ int Subscriber_Destroy(int Index) } delete g_Subscriber_GameEvents[Index]; + delete g_Subscriber_Forwards[Index]; g_Subscriber_Active[Index] = false; g_Subscriber_Client[Index] = -1; @@ -122,6 +127,54 @@ int Subscriber_HandleRequest(int Index, JSONObject Request, JSONObject Response) Response.Set("events", EventArrayResponse); return 0; } + else if(StrEqual(sModule, "forwards")) + { + JSONArray EventArray = view_as(Request.Get("events")); + if(EventArray == null || !EventArray.IsArray) + { + delete EventArray; + JSONObject jError = new JSONObject(); + jError.SetString("error", "Request has no 'events' array-value."); + Response.Set("error", jError); + return -1; + } + + int Method = 0; + if(StrEqual(sMethod, "subscribe")) + Method = 1; + else if(StrEqual(sMethod, "unsubscribe")) + Method = 2; + else if(StrEqual(sMethod, "replay")) + Method = 3; + + JSONArray EventArrayResponse = new JSONArray(); + + for(int i = 0; i < EventArray.Length; i++) + { + static char sEventName[32]; + if(EventArray.GetString(i, sEventName, sizeof(sEventName)) < 0) + { + JSONObject jError = new JSONObject(); + jError.SetString("error", "not a string-value"); + EventArrayResponse.Append(jError); + continue; + } + + int Res; + if(Method == 1) + Res = Subscribe_Forwards_Subscribe(Index, sEventName); + else if(Method == 2) + Res = Subscribe_Forwards_Unsubscribe(Index, sEventName); + else if(Method == 3) + Res = Subscribe_Forwards_Replay(Index, sEventName); + + EventArrayResponse.AppendInt(Res); + } + delete EventArray; + + Response.Set("events", EventArrayResponse); + return 0; + } JSONObject jError = new JSONObject(); jError.SetString("error", "No handler found for requested module."); @@ -275,3 +328,109 @@ void Subscribe_GameEvents_Publish(const char[] sEventName, JSONObject jEvent) delete jPublish; } /* GameEvents */ + +/* Forwards */ +static int Subscribe_Forwards_Subscribe(int Index, const char[] sEventName) +{ + if(Index < 0 || Index >= MAX_SUBSCRIBERS) + return eSubscribeError_OutOfRange; + + if(!g_Subscriber_Active[Index]) + return eSubscribeError_Inactive; + + int Find = g_Subscriber_Forwards[Index].FindString(sEventName); + if(Find != -1) + return Find; + + // TODO: forward exists? + + return g_Subscriber_Forwards[Index].PushString(sEventName); +} + +static int Subscribe_Forwards_Unsubscribe(int Index, const char[] sEventName) +{ + if(Index < 0 || Index >= MAX_SUBSCRIBERS) + return eSubscribeError_OutOfRange; + + if(!g_Subscriber_Active[Index]) + return eSubscribeError_Inactive; + + int Find = g_Subscriber_Forwards[Index].FindString(sEventName); + if(Find == -1) + return 0; + + g_Subscriber_Forwards[Index].Erase(Find); + return Find; +} + +static int Subscribe_Forwards_Replay(int Index, const char[] sEventName) +{ + if(Index < 0 || Index >= MAX_SUBSCRIBERS) + return eSubscribeError_OutOfRange; + + if(!g_Subscriber_Active[Index]) + return eSubscribeError_Inactive; + + if(StrEqual(sEventName, "OnClientPostAdminCheck")) + { + for(int client = 1; client <= MaxClients; client++) + { + if(!IsClientInGame(client) || !IsClientAuthorized(client)) + continue; + + JSONObject jEventData = new JSONObject(); + jEventData.SetInt("client", client); + + Subscribe_Forwards_FakePublish(g_Subscriber_Client[Index], sEventName, jEventData); + } + } + else + return eSubscribeError_Forward; + + return 0; +} + +static int Subscribe_Forwards_FakePublish(int Index, const char[] sEventName, JSONObject jEventData) +{ + if(Index < 0 || Index >= MAX_SUBSCRIBERS) + return eSubscribeError_OutOfRange; + + if(!g_Subscriber_Active[Index]) + return eSubscribeError_Inactive; + + JSONObject jEvent = new JSONObject(); + jEvent.SetString("name", sEventName); + jEvent.Set("data", jEventData); + + JSONObject jPublish = new JSONObject(); + jPublish.SetString("method", "publish"); + jPublish.SetString("module", "forwards"); + jPublish.Set("event", jEvent); + + PublishEvent(g_Subscriber_Client[Index], jPublish); + delete jPublish; + + return 0; +} + +void Subscribe_Forwards_Publish(const char[] sEventName, JSONObject jEvent) +{ + JSONObject jPublish = new JSONObject(); + jPublish.SetString("method", "publish"); + jPublish.SetString("module", "forwards"); + jPublish.Set("event", jEvent); + + for(int Index = 0; Index < MAX_SUBSCRIBERS; Index++) + { + if(!g_Subscriber_Active[Index]) + continue; + + if(g_Subscriber_Forwards[Index].FindString(sEventName) == -1) + continue; + + PublishEvent(g_Subscriber_Client[Index], jPublish); + } + + delete jPublish; +} +/* Forwards */