CapsAdmin

Untitled

May 16th, 2011
331
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 3.19 KB | None | 0 0
  1. --[[
  2.     luadata by CapsAdmin (fuck copyright, do what you want with this)
  3.  
  4.     -- encodes table to string
  5.         string  luadata.Encode(tbl)
  6.    
  7.     -- decodes string to table
  8.     -- it will throw an error if there's a syntax error in the table
  9.         table   luadata.Decode(str)
  10.    
  11.     -- writes the table to file ( it's just "file.Write(path, luadata.Encode(str))" )
  12.         nil     luadata.WriteFile(path, tbl)
  13.         table   luadata.ReadFile(path)
  14.    
  15.     -- returns a string of how the variable is typically initialized
  16.         string  luadata.ToString(var)
  17.        
  18.     -- will let you add your own tostring function for a custom type
  19.     -- if you have made a custom data object, you can do this "mymatrix.LuaDataType = "Matrix33""
  20.     -- and it will make luadata.Type return that instead
  21.         nil     luadata.SetModifier(type, callback)
  22.    
  23. ]]
  24. luadata = luadata or {} local s = luadata
  25.  
  26. luadata.EscapeSequences = {
  27.     [("\a"):byte()] = [[\a]],
  28.     [("\b"):byte()] = [[\b]],
  29.     [("\f"):byte()] = [[\f]],
  30.     [("\t"):byte()] = [[\t]],
  31.     [("\r"):byte()] = [[\r]],
  32.     [("\v"):byte()] = [[\v]],
  33. }
  34.  
  35. local tab = 0
  36.  
  37. luadata.Types = {
  38.     ["number"] = function(var)
  39.         return ("%s"):format(var)
  40.     end,
  41.     ["string"] = function(var)
  42.    
  43.         local str = ""
  44.        
  45.         for char in var:gmatch(".") do
  46.             str = str  .. (s.EscapeSequences[str:byte()] or char)
  47.         end
  48.        
  49.         if str:find('"', nil, true) then
  50.             str = "'" .. str .. "'"
  51.         else
  52.             str = '"' .. str .. '"'
  53.         end
  54.    
  55.         return str
  56.     end,
  57.     ["boolean"] = function(var)
  58.         return ("%s"):format(var and "true" or "false")
  59.     end,
  60.     ["Vector"] = function(var)
  61.         return ("Vector(%s, %s, %s)"):format(var.x, var.y, var.z)
  62.     end,
  63.     ["Angle"] = function(var)
  64.         return ("Angle(%s, %s, %s)"):format(var.p, var.y, var.r)
  65.     end,
  66.    
  67.     -- comment these out if you don't want shit like this to be storeable
  68.     ["Entity"] = function(var)
  69.         return ("Entity(%i)"):format(var:EntIndex())
  70.     end,
  71.     ["Panel"] = function(var)
  72.         return "NULL"
  73.     end,
  74.     ["NULL"] = function(var)
  75.         return "NULL"
  76.     end,
  77.    
  78.     ["table"] = function(var)
  79.         tab = tab + 1
  80.         local str = luadata.Encode(var, true)
  81.         tab = tab - 1
  82.         return str
  83.     end,
  84. }
  85.  
  86. function luadata.SetModifier(type, callback)
  87.     luadata.Types[type] = callback
  88. end
  89.  
  90. function luadata.Type(var)
  91.     local t
  92.    
  93.     if IsEntity(var) then
  94.         if var:IsValid() then
  95.             t = "Entity"
  96.         else
  97.             t = "NULL"
  98.         end
  99.     else
  100.         t = type(var)
  101.     end
  102.    
  103.     if t == "table" then
  104.         if var.LuaDataType then
  105.             t = var.LuaDataType
  106.         end
  107.     end
  108.    
  109.     return t
  110. end
  111.  
  112. function luadata.ToString(var) 
  113.     local func = s.Types[s.Type(var)]
  114.     return func and func(var)
  115. end
  116.  
  117. function luadata.Encode(tbl, __brackets)
  118.     local str = __brackets and "{\n" or ""
  119.    
  120.     for key, value in pairs(tbl) do
  121.         value = s.ToString(value)
  122.         key = s.ToString(key)
  123.        
  124.         if key and value and key ~= "__index" then 
  125.             str = str .. ("\t"):rep(tab) ..  ("[%s] = %s,\n"):format(key, value)
  126.         end
  127.     end
  128.    
  129.     str = str .. ("\t"):rep(tab-1) .. (__brackets and "}" or "")
  130.        
  131.     return str
  132. end
  133.  
  134. function luadata.Decode(str)
  135.     local func = CompileString("return {\n" .. str .. "\n}", "luadata")
  136.     return func()
  137. end
  138.  
  139. function luadata.WriteFile(path, tbl)
  140.     file.Write(path, luadata.Encode(tbl))
  141. end
  142.  
  143. function luadata.ReadFile(path, tbl)
  144.     return luadata.Decode(file.Read(path))
  145. end
Advertisement
Add Comment
Please, Sign In to add comment