zombiesurvival-evolved/gamemodes/zombiesurvival/gamemode/sh_serialization.lua

87 lines
1.8 KiB
Lua
Raw Normal View History

2014-10-02 08:49:54 +08:00
function Deserialize(sIn)
SRL = nil
if #sIn == 0 then return {} end
if string.sub(sIn, 1, 4) ~= "SRL=" then sIn = "SRL="..sIn end RunString(sIn)
return SRL
end
local allowedtypes = {}
allowedtypes["string"] = true
allowedtypes["number"] = true
allowedtypes["table"] = true
allowedtypes["Vector"] = true
allowedtypes["Angle"] = true
allowedtypes["boolean"] = true
local function MakeTable(tab, done)
local str = ""
local done = done or {}
local sequential = table.IsSequential(tab)
for key, value in pairs(tab) do
local keytype = type(key)
local valuetype = type(value)
if allowedtypes[keytype] and allowedtypes[valuetype] then
if sequential then
key = ""
else
if keytype == "number" or keytype == "boolean" then
key ="["..tostring(key).."]="
else
key = "["..string.format("%q", tostring(key)).."]="
end
end
if valuetype == "table" and not done[value] then
done[value] = true
if type(value._serialize) == "function" then
str = str..key..value:_serialize()..","
else
str = str..key.."{"..MakeTable(value, done).."},"
end
else
if valuetype == "string" then
value = string.format("%q", value)
elseif valuetype == "Vector" then
value = "Vector("..value.x..","..value.y..","..value.z..")"
elseif valuetype == "Angle" then
value = "Angle("..value.pitch..","..value.yaw..","..value.roll..")"
else
value = tostring(value)
end
str = str .. key .. value .. ","
end
end
end
if string.sub(str, -1) == "," then
return string.sub(str, 1, #str - 1)
else
return str
end
end
function Serialize(tIn, bRaw)
if #tIn == 0 then
local empty = true
for k in pairs(tIn) do
empty = false
break
end
if empty then
return ""
end
end
if bRaw then
return "{"..MakeTable(tIn).."}"
end
return "SRL={"..MakeTable(tIn).."}"
end