local y,w="\065\099\101\083\101\114\105\097\108\105\122\101\114-\051\046\048",5 local O,H=LibStub:NewLibrary(y,w)if not O then return end local C,M,T,I,b=string.byte,string.char,string.gsub,string.gmatch,string.format local t,z,x=assert,error,pcall local e,W,X=type,tostring,tonumber local R,D,l=pairs,select,math.frexp local Z=table.concat local U=math.huge local q local u,J="\049\046\035\073\078\070","\105\110\102"local g,n="-\049\046\035\073\078\070","-\105\110\102"local function N(y)local w=C(y)if w==30 then return"~\122"elseif w<=32 then return"~"..M(w+64)elseif w==94 then return"~\125"elseif w==126 then return"~\124"elseif w==127 then return"~\123"else t(false)end end local function c(w,O,H)local C=e(w)if C=="\115\116\114\105\110\103"then O[H+1]="\094\083"O[H+2]=T(w,"\091\037\099 \094~\127\093",N)H=H+2 elseif C=="\110\117\109\098\101\114"then local y=W(w)if X(y)==w then O[H+1]="\094\078"O[H+2]=y H=H+2 elseif w==U or w==-U then O[H+1]="\094\078"O[H+2]=w==U and u or g H=H+2 else local y,C=l(w)O[H+1]="\094\070"O[H+2]=b("\037\046\048\102",y*9.007199254741e+15)O[H+3]="\094\102"O[H+4]=W(C-53)H=H+4 end elseif C=="\116\097\098\108\101"then H=H+1 O[H]="\094\084"for y,w in R(w)do H=c(y,O,H)H=c(w,O,H)end H=H+1 O[H]="\094\116"elseif C=="\098\111\111\108\101\097\110"then H=H+1 if w then O[H]="\094\066"else O[H]="\094\098"end elseif C=="\110\105\108"then H=H+1 O[H]="\094\090"else z(y..("\058 \067\097\110\110\111\116 \115\101\114\105\097\108\105\122\101 \097 \118\097\108\117\101 \111\102 \116\121\112\101 \'"..(C.."\'")))end return H end local F={"\094\049"}function O.Serialize(y,...)local w=1 for y=1,D("\035",...),1 do local O=D(y,...)w=c(O,F,w)end F[w+1]="\094\094"return Z(F,"",1,w+1)end local function r(y)if y<"~\122"then return M(C(y,2,2)-64)elseif y=="~\122"then return"\030"elseif y=="~\123"then return"\127"elseif y=="~\124"then return"~"elseif y=="~\125"then return"\094"end z("\068\101\115\101\114\105\097\108\105\122\101\083\116\114\105\110\103\072\101\108\112\101\114 \103\111\116 \099\097\108\108\101\100 \102\111\114 \'"..(y.."\'\063\033\063"))end local function Q(y)if y==g or y==n then return-U elseif y==u or y==J then return U else return X(y)end end local function h(y,w,O,H)if not w then O,H=y()end if not O then z("\083\117\112\112\108\105\101\100 \100\097\116\097 \109\105\115\115\101\115 \065\099\101\083\101\114\105\097\108\105\122\101\114 \116\101\114\109\105\110\097\116\111\114 \040\'\094\094\'\041")end if O=="\094\094"then return end local C if O=="\094\083"then C=T(H,"~\046",r)elseif O=="\094\078"then C=Q(H)if not C then z("\073\110\118\097\108\105\100 \115\101\114\105\097\108\105\122\101\100 \110\117\109\098\101\114\058 \'"..(W(H).."\'"))end elseif O=="\094\070"then local w,O=y()if w~="\094\102"then z("\073\110\118\097\108\105\100 \115\101\114\105\097\108\105\122\101\100 \102\108\111\097\116\105\110\103-\112\111\105\110\116 \110\117\109\098\101\114\044 \101\120\112\101\099\116\101\100 \'\094\102\'\044 \110\111\116 \'"..(W(w).."\'"))end local M=X(H)O=X(O)if not(M and O)then z("\073\110\118\097\108\105\100 \115\101\114\105\097\108\105\122\101\100 \102\108\111\097\116\105\110\103-\112\111\105\110\116 \110\117\109\098\101\114\044 \101\120\112\101\099\116\101\100 \109\097\110\116\105\115\115\097 \097\110\100 \101\120\112\111\110\101\110\116\044 \103\111\116 \'"..(W(M)..("\' \097\110\100 \'"..(W(O).."\'"))))end C=M*2^O elseif O=="\094\066"then C=true elseif O=="\094\098"then C=false elseif O=="\094\090"then C=nil elseif O=="\094\084"then C={}local w,M while true do O,H=y()if O=="\094\116"then break end w=h(y,true,O,H)if w==nil then z("\073\110\118\097\108\105\100 \065\099\101\083\101\114\105\097\108\105\122\101\114 \116\097\098\108\101 \102\111\114\109\097\116 \040\110\111 \116\097\098\108\101 \101\110\100 \109\097\114\107\101\114\041")end O,H=y()M=h(y,true,O,H)if M==nil then z("\073\110\118\097\108\105\100 \065\099\101\083\101\114\105\097\108\105\122\101\114 \116\097\098\108\101 \102\111\114\109\097\116 \040\110\111 \116\097\098\108\101 \101\110\100 \109\097\114\107\101\114\041")end C[w]=M end else z("\073\110\118\097\108\105\100 \065\099\101\083\101\114\105\097\108\105\122\101\114 \099\111\110\116\114\111\108 \099\111\100\101 \'"..(O.."\'"))end if not w then return C,h(y)else return C end end function O.Deserialize(w,y)y=T(y,"\091\037\099 \093","")local O=I(y,"\040\094\046\041\040\091\094\094\093\042\041")local H,C=O()if not H or H~="\094\049"then return false,"\083\117\112\112\108\105\101\100 \100\097\116\097 \105\115 \110\111\116 \065\099\101\083\101\114\105\097\108\105\122\101\114 \100\097\116\097 \040\114\101\118 \049\041"end return x(h,O)end O.internals={SerializeValue=c,SerializeStringHelper=N}local f={"\083\101\114\105\097\108\105\122\101","\068\101\115\101\114\105\097\108\105\122\101"}O.embeds=O.embeds or{}function O.Embed(w,y)for O,H in R(f)do y[H]=w[H]end w.embeds[y]=true return y end for y,w in R(O.embeds)do O:Embed(y)end
--- **AceSerializer-3.0** can serialize any variable (except functions or userdata) into a string format,
-- that can be send over the addon comm channel. AceSerializer was designed to keep all data intact, especially
-- very large numbers or floating point numbers, and table structures. The only caveat currently is, that multiple
-- references to the same table will be send individually.
--
-- **AceSerializer-3.0** can be embeded into your addon, either explicitly by calling AceSerializer:Embed(MyAddon) or by
-- specifying it as an embeded library in your AceAddon. All functions will be available on your addon object
-- and can be accessed directly, without having to explicitly call AceSerializer itself.\\
-- It is recommended to embed AceSerializer, otherwise you'll have to specify a custom `self` on all calls you
-- make into AceSerializer.
-- @class file
-- @name AceSerializer-3.0
-- @release $Id: AceSerializer-3.0.lua 1284 2022-09-25 09:15:30Z nevcairiel $
local MAJOR,MINOR = "AceSerializer-3.0", 5
local AceSerializer, oldminor = LibStub:NewLibrary(MAJOR, MINOR)

if not AceSerializer then return end

-- Lua APIs
local strbyte, strchar, gsub, gmatch, format = string.byte, string.char, string.gsub, string.gmatch, string.format
local assert, error, pcall = assert, error, pcall
local type, tostring, tonumber = type, tostring, tonumber
local pairs, select, frexp = pairs, select, math.frexp
local tconcat = table.concat

-- quick copies of string representations of wonky numbers
local inf = math.huge

local serNaN  -- can't do this in 4.3, see ace3 ticket 268
local serInf, serInfMac = "1.#INF", "inf"
local serNegInf, serNegInfMac = "-1.#INF", "-inf"


-- Serialization functions

local function SerializeStringHelper(ch)	-- Used by SerializeValue for strings
	-- We use \126 ("~") as an escape character for all nonprints plus a few more
	local n = strbyte(ch)
	if n==30 then           -- v3 / ticket 115: catch a nonprint that ends up being "~^" when encoded... DOH
		return "\126\122"
	elseif n<=32 then 			-- nonprint + space
		return "\126"..strchar(n+64)
	elseif n==94 then		-- value separator
		return "\126\125"
	elseif n==126 then		-- our own escape character
		return "\126\124"
	elseif n==127 then		-- nonprint (DEL)
		return "\126\123"
	else
		assert(false)	-- can't be reached if caller uses a sane regex
	end
end

local function SerializeValue(v, res, nres)
	-- We use "^" as a value separator, followed by one byte for type indicator
	local t=type(v)

	if t=="string" then		-- ^S = string (escaped to remove nonprints, "^"s, etc)
		res[nres+1] = "^S"
		res[nres+2] = gsub(v,"[%c \94\126\127]", SerializeStringHelper)
		nres=nres+2

	elseif t=="number" then	-- ^N = number (just tostring()ed) or ^F (float components)
		local str = tostring(v)
		if tonumber(str)==v  --[[not in 4.3 or str==serNaN]] then
			-- translates just fine, transmit as-is
			res[nres+1] = "^N"
			res[nres+2] = str
			nres=nres+2
		elseif v == inf or v == -inf then
			res[nres+1] = "^N"
			res[nres+2] = v == inf and serInf or serNegInf
			nres=nres+2
		else
			local m,e = frexp(v)
			res[nres+1] = "^F"
			res[nres+2] = format("%.0f",m*2^53)	-- force mantissa to become integer (it's originally 0.5--0.9999)
			res[nres+3] = "^f"
			res[nres+4] = tostring(e-53)	-- adjust exponent to counteract mantissa manipulation
			nres=nres+4
		end

	elseif t=="table" then	-- ^T...^t = table (list of key,value pairs)
		nres=nres+1
		res[nres] = "^T"
		for key,value in pairs(v) do
			nres = SerializeValue(key, res, nres)
			nres = SerializeValue(value, res, nres)
		end
		nres=nres+1
		res[nres] = "^t"

	elseif t=="boolean" then	-- ^B = true, ^b = false
		nres=nres+1
		if v then
			res[nres] = "^B"	-- true
		else
			res[nres] = "^b"	-- false
		end

	elseif t=="nil" then		-- ^Z = nil (zero, "N" was taken :P)
		nres=nres+1
		res[nres] = "^Z"

	else
		error(MAJOR..": Cannot serialize a value of type '"..t.."'")	-- can't produce error on right level, this is wildly recursive
	end

	return nres
end



local serializeTbl = { "^1" }	-- "^1" = Hi, I'm data serialized by AceSerializer protocol rev 1

--- Serialize the data passed into the function.
-- Takes a list of values (strings, numbers, booleans, nils, tables)
-- and returns it in serialized form (a string).\\
-- May throw errors on invalid data types.
-- @param ... List of values to serialize
-- @return The data in its serialized form (string)
function AceSerializer:Serialize(...)
	local nres = 1

	for i=1,select("#", ...) do
		local v = select(i, ...)
		nres = SerializeValue(v, serializeTbl, nres)
	end

	serializeTbl[nres+1] = "^^"	-- "^^" = End of serialized data

	return tconcat(serializeTbl, "", 1, nres+1)
end

-- Deserialization functions
local function DeserializeStringHelper(escape)
	if escape<"~\122" then
		return strchar(strbyte(escape,2,2)-64)
	elseif escape=="~\122" then	-- v3 / ticket 115: special case encode since 30+64=94 ("^") - OOPS.
		return "\030"
	elseif escape=="~\123" then
		return "\127"
	elseif escape=="~\124" then
		return "\126"
	elseif escape=="~\125" then
		return "\94"
	end
	error("DeserializeStringHelper got called for '"..escape.."'?!?")  -- can't be reached unless regex is screwed up
end

local function DeserializeNumberHelper(number)
	--[[ not in 4.3 if number == serNaN then
		return 0/0
	else]]if number == serNegInf or number == serNegInfMac then
		return -inf
	elseif number == serInf or number == serInfMac then
		return inf
	else
		return tonumber(number)
	end
end

-- DeserializeValue: worker function for :Deserialize()
-- It works in two modes:
--   Main (top-level) mode: Deserialize a list of values and return them all
--   Recursive (table) mode: Deserialize only a single value (_may_ of course be another table with lots of subvalues in it)
--
-- The function _always_ works recursively due to having to build a list of values to return
--
-- Callers are expected to pcall(DeserializeValue) to trap errors

local function DeserializeValue(iter,single,ctl,data)

	if not single then
		ctl,data = iter()
	end

	if not ctl then
		error("Supplied data misses AceSerializer terminator ('^^')")
	end

	if ctl=="^^" then
		-- ignore extraneous data
		return
	end

	local res

	if ctl=="^S" then
		res = gsub(data, "~.", DeserializeStringHelper)
	elseif ctl=="^N" then
		res = DeserializeNumberHelper(data)
		if not res then
			error("Invalid serialized number: '"..tostring(data).."'")
		end
	elseif ctl=="^F" then     -- ^F<mantissa>^f<exponent>
		local ctl2,e = iter()
		if ctl2~="^f" then
			error("Invalid serialized floating-point number, expected '^f', not '"..tostring(ctl2).."'")
		end
		local m=tonumber(data)
		e=tonumber(e)
		if not (m and e) then
			error("Invalid serialized floating-point number, expected mantissa and exponent, got '"..tostring(m).."' and '"..tostring(e).."'")
		end
		res = m*(2^e)
	elseif ctl=="^B" then	-- yeah yeah ignore data portion
		res = true
	elseif ctl=="^b" then   -- yeah yeah ignore data portion
		res = false
	elseif ctl=="^Z" then	-- yeah yeah ignore data portion
		res = nil
	elseif ctl=="^T" then
		-- ignore ^T's data, future extensibility?
		res = {}
		local k,v
		while true do
			ctl,data = iter()
			if ctl=="^t" then break end	-- ignore ^t's data
			k = DeserializeValue(iter,true,ctl,data)
			if k==nil then
				error("Invalid AceSerializer table format (no table end marker)")
			end
			ctl,data = iter()
			v = DeserializeValue(iter,true,ctl,data)
			if v==nil then
				error("Invalid AceSerializer table format (no table end marker)")
			end
			res[k]=v
		end
	else
		error("Invalid AceSerializer control code '"..ctl.."'")
	end

	if not single then
		return res,DeserializeValue(iter)
	else
		return res
	end
end

--- Deserializes the data into its original values.
-- Accepts serialized data, ignoring all control characters and whitespace.
-- @param str The serialized data (from :Serialize)
-- @return true followed by a list of values, OR false followed by an error message
function AceSerializer:Deserialize(str)
	str = gsub(str, "[%c ]", "")	-- ignore all control characters; nice for embedding in email and stuff

	local iter = gmatch(str, "(^.)([^^]*)")	-- Any ^x followed by string of non-^
	local ctl,data = iter()
	if not ctl or ctl~="^1" then
		-- we purposefully ignore the data portion of the start code, it can be used as an extension mechanism
		return false, "Supplied data is not AceSerializer data (rev 1)"
	end

	return pcall(DeserializeValue, iter)
end


----------------------------------------
-- Base library stuff
----------------------------------------

AceSerializer.internals = {	-- for test scripts
	SerializeValue = SerializeValue,
	SerializeStringHelper = SerializeStringHelper,
}

local mixins = {
	"Serialize",
	"Deserialize",
}

AceSerializer.embeds = AceSerializer.embeds or {}

function AceSerializer:Embed(target)
	for k, v in pairs(mixins) do
		target[v] = self[v]
	end
	self.embeds[target] = true
	return target
end

-- Update embeds
for target, v in pairs(AceSerializer.embeds) do
	AceSerializer:Embed(target)
end
