Rewrite framework to use proper unicode delimiters
This commit is contained in:
@ -2,9 +2,8 @@ local debug = false
|
|||||||
|
|
||||||
local CMH = {}
|
local CMH = {}
|
||||||
local datacache = {}
|
local datacache = {}
|
||||||
local delim = {"♠", "♥", "♚", "♛", "♜"}
|
local delim = {"", "", "", "", ""}
|
||||||
local emptyStr = "˽"
|
local pck = {REQ = "", DAT = ""}
|
||||||
local pck = {REQ = 1, DAT = 2}
|
|
||||||
|
|
||||||
-- HELPERS START
|
-- HELPERS START
|
||||||
local function debugOut(prefix, x, msg)
|
local function debugOut(prefix, x, msg)
|
||||||
@ -52,7 +51,7 @@ local function ParseMessage(str)
|
|||||||
local varType = typeTemp[k]
|
local varType = typeTemp[k]
|
||||||
if(varType == 2) then -- Strings
|
if(varType == 2) then -- Strings
|
||||||
-- Special case for empty string parsing
|
-- Special case for empty string parsing
|
||||||
if(v == emptyStr) then
|
if(v == "") then
|
||||||
v = ""
|
v = ""
|
||||||
end
|
end
|
||||||
elseif(varType == 3) then -- Ints
|
elseif(varType == 3) then -- Ints
|
||||||
@ -76,11 +75,11 @@ local function ProcessVariables(reqId, ...)
|
|||||||
local arg = {...}
|
local arg = {...}
|
||||||
local msg = ""
|
local msg = ""
|
||||||
|
|
||||||
for _, v in pairs(arg) do
|
for k, v in pairs(arg) do
|
||||||
if(type(v) == "string") then
|
if(type(v) == "string") then
|
||||||
-- Special case for empty string parsing
|
-- Special case for empty string parsing
|
||||||
if(#v == 0) then
|
if(#v == 0) then
|
||||||
msg = msg .. emptyStr
|
v = ""
|
||||||
end
|
end
|
||||||
msg = msg .. delim[2]
|
msg = msg .. delim[2]
|
||||||
elseif(type(v) == "number") then
|
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
|
-- 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
|
if event == "CHAT_MSG_ADDON" and sender == UnitName("player") and Type == "WHISPER" then
|
||||||
-- unpack and validate addon message structure
|
-- 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
|
if not pfx or not source or not pckId then
|
||||||
return
|
return
|
||||||
end
|
end
|
||||||
|
|
||||||
-- Make sure we're only processing addon messages using our framework prefix character as well as client messages
|
-- 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
|
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
|
if(pckId == pck.REQ) then
|
||||||
debugOut("REQ", "Rx", "REQ received, data: "..data)
|
debugOut("REQ", "Rx", "REQ received, data: "..data)
|
||||||
CMH.OnREQ(sender, data)
|
CMH.OnREQ(sender, data)
|
||||||
@ -249,7 +245,7 @@ end
|
|||||||
-- Tx START
|
-- Tx START
|
||||||
|
|
||||||
function CMH.SendREQ(functionId, linkCount, reqId, addon)
|
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)
|
local data = string.format("%02d%03d%06s%0"..tostring(#addon).."s", functionId, linkCount, reqId, addon)
|
||||||
SendAddonMessage(header, data, "WHISPER", UnitName("player"))
|
SendAddonMessage(header, data, "WHISPER", UnitName("player"))
|
||||||
debugOut("REQ", "Tx", "Sent REQ with ID "..reqId..", sending DAT..")
|
debugOut("REQ", "Tx", "Sent REQ with ID "..reqId..", sending DAT..")
|
||||||
@ -257,7 +253,7 @@ end
|
|||||||
|
|
||||||
function CMH.SendDAT(reqId)
|
function CMH.SendDAT(reqId)
|
||||||
-- Build data message header
|
-- 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
|
-- 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
|
-- functions can also be trigger functions without any data, only send header and no payload
|
||||||
|
|||||||
@ -4,9 +4,8 @@ local debug = false
|
|||||||
|
|
||||||
local SMH = {}
|
local SMH = {}
|
||||||
local datacache = {}
|
local datacache = {}
|
||||||
local delim = {"♠", "♥", "♚", "♛", "♜"}
|
local delim = {"", "", "", "", ""}
|
||||||
local emptyStr = "˽"
|
local pck = {REQ = "", DAT = ""}
|
||||||
local pck = {REQ = 1, DAT = 2}
|
|
||||||
|
|
||||||
-- HELPERS START
|
-- HELPERS START
|
||||||
local function debugOut(prefix, x, msg)
|
local function debugOut(prefix, x, msg)
|
||||||
@ -54,7 +53,7 @@ local function ParseMessage(str)
|
|||||||
local varType = typeTemp[k]
|
local varType = typeTemp[k]
|
||||||
if(varType == 2) then -- strings
|
if(varType == 2) then -- strings
|
||||||
-- special case for empty string parsing
|
-- special case for empty string parsing
|
||||||
if(v == emptyStr) then
|
if(v == "") then
|
||||||
v = ""
|
v = ""
|
||||||
end
|
end
|
||||||
elseif(varType == 3) then -- Ints
|
elseif(varType == 3) then -- Ints
|
||||||
@ -82,7 +81,7 @@ local function ProcessVariables(sender, reqId, ...)
|
|||||||
if(type(v) == "string") then
|
if(type(v) == "string") then
|
||||||
-- Special case for empty string parsing
|
-- Special case for empty string parsing
|
||||||
if(#v == 0) then
|
if(#v == 0) then
|
||||||
msg = msg .. emptyStr
|
v = ""
|
||||||
end
|
end
|
||||||
msg = msg .. delim[2]
|
msg = msg .. delim[2]
|
||||||
elseif(type(v) == "number") then
|
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
|
-- Ensure the sender and receiver is the same, and the message type is WHISPER
|
||||||
if sender:GetName() == target:GetName() and _type == 7 then
|
if sender:GetName() == target:GetName() and _type == 7 then
|
||||||
-- unpack and validate addon message structure
|
-- 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
|
if not pfx or not source or not pckId then
|
||||||
return
|
return
|
||||||
end
|
end
|
||||||
|
|
||||||
-- Make sure we're only processing addon messages using our framework prefix character as well as client messages
|
-- 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
|
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
|
if(pckId == pck.REQ) then
|
||||||
debugOut("REQ", "Rx", "REQ received, data: "..data)
|
debugOut("REQ", "Rx", "REQ received, data: "..data)
|
||||||
SMH.OnREQ(sender, data)
|
SMH.OnREQ(sender, data)
|
||||||
@ -261,7 +257,7 @@ end
|
|||||||
-- Tx START
|
-- Tx START
|
||||||
|
|
||||||
function SMH.SendREQ(sender, functionId, linkCount, reqId, addon)
|
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)
|
local data = string.format("%02d%03d%06s%0"..tostring(#addon).."s", functionId, linkCount, reqId, addon)
|
||||||
sender:SendAddonMessage(header, data, 7, sender)
|
sender:SendAddonMessage(header, data, 7, sender)
|
||||||
debugOut("REQ", "Tx", "Sent REQ with ID "..reqId..", sending DAT..")
|
debugOut("REQ", "Tx", "Sent REQ with ID "..reqId..", sending DAT..")
|
||||||
@ -269,7 +265,7 @@ end
|
|||||||
|
|
||||||
function SMH.SendDAT(sender, reqId)
|
function SMH.SendDAT(sender, reqId)
|
||||||
-- Build data message header
|
-- 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
|
-- 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
|
-- functions can also be trigger functions without any data, only send header and no payload
|
||||||
|
|||||||
Reference in New Issue
Block a user