From e17c6268e4280f2523bae2624aede27929bf3c83 Mon Sep 17 00:00:00 2001 From: Foereaper Date: Tue, 2 Aug 2022 20:04:31 +0200 Subject: [PATCH] Rewrite framework to use proper unicode delimiters --- Client/CMH.lua | 20 ++++++++------------ Server/SMH.lua | 18 +++++++----------- 2 files changed, 15 insertions(+), 23 deletions(-) diff --git a/Client/CMH.lua b/Client/CMH.lua index f5e3243..20180de 100644 --- a/Client/CMH.lua +++ b/Client/CMH.lua @@ -2,9 +2,8 @@ local debug = false local CMH = {} local datacache = {} -local delim = {"♠", "♥", "♚", "♛", "♜"} -local emptyStr = "˽" -local pck = {REQ = 1, DAT = 2} +local delim = {"", "", "", "", ""} +local pck = {REQ = "", DAT = ""} -- HELPERS START local function debugOut(prefix, x, msg) @@ -52,7 +51,7 @@ local function ParseMessage(str) local varType = typeTemp[k] if(varType == 2) then -- Strings -- Special case for empty string parsing - if(v == emptyStr) then + if(v == "") then v = "" end elseif(varType == 3) then -- Ints @@ -76,11 +75,11 @@ local function ProcessVariables(reqId, ...) local arg = {...} local msg = "" - for _, v in pairs(arg) do + for k, v in pairs(arg) do if(type(v) == "string") then -- Special case for empty string parsing if(#v == 0) then - msg = msg .. emptyStr + v = "" end msg = msg .. delim[2] elseif(type(v) == "number") then @@ -116,16 +115,13 @@ function CMH.OnReceive(self, event, header, data, Type, sender) -- Ensure the sender and receiver is the same, the message is an addon message, and the message type is WHISPER if event == "CHAT_MSG_ADDON" and sender == UnitName("player") and Type == "WHISPER" then -- unpack and validate addon message structure - local pfx, source, pckId = header:match("(...)(%u)(%d%d)") + local pfx, source, pckId = header:match("(.)(%u)(.)") if not pfx or not source or not pckId then return end -- Make sure we're only processing addon messages using our framework prefix character as well as client messages if(pfx == delim[1] and source == "S") then - -- convert ID to number so we can compare with our packet list - pckId = tonumber(pckId) - if(pckId == pck.REQ) then debugOut("REQ", "Rx", "REQ received, data: "..data) CMH.OnREQ(sender, data) @@ -249,7 +245,7 @@ end -- Tx START function CMH.SendREQ(functionId, linkCount, reqId, addon) - local header = string.format("%01s%01s%02d", delim[1], "C", pck.REQ) + local header = string.format("%01s%01s%01s", delim[1], "C", pck.REQ) local data = string.format("%02d%03d%06s%0"..tostring(#addon).."s", functionId, linkCount, reqId, addon) SendAddonMessage(header, data, "WHISPER", UnitName("player")) debugOut("REQ", "Tx", "Sent REQ with ID "..reqId..", sending DAT..") @@ -257,7 +253,7 @@ end function CMH.SendDAT(reqId) -- Build data message header - local header = string.format("%01s%01s%02d", delim[1], "C", pck.DAT) + local header = string.format("%01s%01s%01s", delim[1], "C", pck.DAT) -- iterate all items in the message data cache and send -- functions can also be trigger functions without any data, only send header and no payload diff --git a/Server/SMH.lua b/Server/SMH.lua index 4310f0b..fd397b4 100644 --- a/Server/SMH.lua +++ b/Server/SMH.lua @@ -4,9 +4,8 @@ local debug = false local SMH = {} local datacache = {} -local delim = {"♠", "♥", "♚", "♛", "♜"} -local emptyStr = "˽" -local pck = {REQ = 1, DAT = 2} +local delim = {"", "", "", "", ""} +local pck = {REQ = "", DAT = ""} -- HELPERS START local function debugOut(prefix, x, msg) @@ -54,7 +53,7 @@ local function ParseMessage(str) local varType = typeTemp[k] if(varType == 2) then -- strings -- special case for empty string parsing - if(v == emptyStr) then + if(v == "") then v = "" end elseif(varType == 3) then -- Ints @@ -82,7 +81,7 @@ local function ProcessVariables(sender, reqId, ...) if(type(v) == "string") then -- Special case for empty string parsing if(#v == 0) then - msg = msg .. emptyStr + v = "" end msg = msg .. delim[2] elseif(type(v) == "number") then @@ -126,16 +125,13 @@ function SMH.OnReceive(event, sender, _type, header, data, target) -- Ensure the sender and receiver is the same, and the message type is WHISPER if sender:GetName() == target:GetName() and _type == 7 then -- unpack and validate addon message structure - local pfx, source, pckId = header:match("(...)(%u)(%d%d)") + local pfx, source, pckId = header:match("(.)(%u)(.)") if not pfx or not source or not pckId then return end -- Make sure we're only processing addon messages using our framework prefix character as well as client messages if(pfx == delim[1] and source == "C") then - -- convert ID to number so we can compare with our packet list - pckId = tonumber(pckId) - if(pckId == pck.REQ) then debugOut("REQ", "Rx", "REQ received, data: "..data) SMH.OnREQ(sender, data) @@ -261,7 +257,7 @@ end -- Tx START function SMH.SendREQ(sender, functionId, linkCount, reqId, addon) - local header = string.format("%01s%01s%02d", delim[1], "S", pck.REQ) + local header = string.format("%01s%01s%01s", delim[1], "S", pck.REQ) local data = string.format("%02d%03d%06s%0"..tostring(#addon).."s", functionId, linkCount, reqId, addon) sender:SendAddonMessage(header, data, 7, sender) debugOut("REQ", "Tx", "Sent REQ with ID "..reqId..", sending DAT..") @@ -269,7 +265,7 @@ end function SMH.SendDAT(sender, reqId) -- Build data message header - local header = string.format("%01s%01s%02d", delim[1], "S", pck.DAT) + local header = string.format("%01s%01s%01s", delim[1], "S", pck.DAT) -- iterate all items in the message data cache and send -- functions can also be trigger functions without any data, only send header and no payload