Advertisement
killer64

Love_Infutil

Jul 26th, 2013
98
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 2.63 KB | None | 0 0
  1. infutil={}
  2. local function serializeImpl(t,TF)
  3.     local sType = type(t)
  4.     if sType == "table" then
  5.         local result = "{"
  6.         local aset=1
  7.         local comma=false
  8.         for k,v in pairs(t) do
  9.             comma=true
  10.             if k==aset then
  11.                 result = result..serializeImpl(v,TF)..","
  12.                 aset=aset+1
  13.             else
  14.                 local tmp=serializeImpl(k,TF)
  15.                 local tmp2=serializeImpl(v,TF)
  16.                 if type(k)=="string" then
  17.                     if pcall(loadstring,"return {"..k.."="..tmp2.."}") and not string.find(k,",") then
  18.                         result=result..k.."="..tmp2..","
  19.                     else
  20.                         result=result.."["..tmp.."]="..tmp2..","
  21.                     end
  22.                 else
  23.                     result=result.."["..tmp.."]="..tmp2..","
  24.                 end
  25.             end
  26.         end
  27.         if comma then
  28.             result=string.sub(result,1,-2)
  29.         end
  30.         result = result.."}"
  31.         return result
  32.     elseif sType == "string" then
  33.         return string.gsub(string.format("%q",t),"\\\n","\\n")
  34.     elseif sType == "number" or sType == "boolean" or sType == "nil" then
  35.         return tostring(t)
  36.     elseif sType == "function" and not TF then
  37.         local status,data=pcall(string.dump,t)
  38.         if status then
  39.             return 'func('..string.format("%q",data)..')'
  40.         else
  41.             error()
  42.         end
  43.     elseif sType == "function" and TF then
  44.         return tostring(t)
  45.     else
  46.         error()
  47.     end
  48. end
  49.  
  50. function infutil.split(T,func)
  51.     if func then
  52.         T=func(T)
  53.     end
  54.     local Out={}
  55.     if type(T)=="table" then
  56.         for k,v in pairs(T) do
  57.             Out[infutil.split(k)]=infutil.split(v)
  58.         end
  59.     else
  60.         Out=T
  61.     end
  62.     return Out
  63. end
  64. function infutil.serialize(t,TF)
  65.     t=infutil.split(t)
  66.     return serializeImpl(t,TF)
  67. end
  68. function infutil.unserialize(s,tf)
  69.     if type(s)~="string" then
  70.         error("String exepcted. got "..type(s),2)
  71.     end
  72.     local func,e=loadstring("return"..s,"unserialize")
  73.     local funcs={}
  74.     if not func then
  75.         error("Invalid string.")
  76.     end
  77.     setfenv(func,{
  78.         func=function(S)
  79.             local new={}
  80.             funcs[new]=S
  81.             return new
  82.         end,
  83.     })
  84.     return infutil.split(func(),function(val)
  85.         if funcs[val] then
  86.             return loadstring(funcs[val])
  87.         else
  88.             return val
  89.         end
  90.     end)
  91. end
  92. function infutil.savePrefs(name,data)
  93.     local S=infutil.serialize(data)
  94.     if not S then
  95.         return false
  96.     end
  97.     love.filesystem.write(name,S)
  98. end
  99. function infutil.readPrefs(name)
  100.     if not love.filesystem.exists(name) then
  101.         return false
  102.     end
  103.     return infutil.unserialize(love.filesystem.read(name))
  104. end
  105. function infutil.compress(...)
  106.     local E,D=pcall(infutil.serialize,{...})
  107.     if E then
  108.         return string.sub(D,2,-2)
  109.     else
  110.         return false
  111.     end
  112. end
  113. function infutil.decompress(S)
  114.     if not S then
  115.         return false
  116.     end
  117.     local E,D,C=pcall(infutil.unserialize,"{"..S.."}")
  118.     if C or not E then
  119.         return false
  120.     else
  121.         return type(D)=="table" and D
  122.     end
  123. end
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement