osmarks

ULTRADUMP

Oct 8th, 2018
158
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 2.64 KB | None | 0 0
  1. local a=http.get"https://pastebin.com/raw/KXHSsHkt"local b=fs.open("ser","w")b.write(a.readAll())a.close()b.close()
  2.  
  3. local ultradump = {}
  4. local ser = require "/ser"
  5. local e = ser.serialize
  6.  
  7. local function copy(x)
  8.     if type(x) == "table" then
  9.         local out = {}
  10.         for k, v in pairs(x) do
  11.             out[k] = v
  12.         end
  13.         return out
  14.     else return x end
  15. end
  16.  
  17. function ultradump.dump(x)
  18.     local objects = {}
  19.     local enclookup = {}
  20.     local count = 0
  21.  
  22.     local function addobj(o)
  23.         objects[o] = count
  24.         local c = count
  25.         count = count + 1
  26.         return c
  27.     end
  28.  
  29.     local function mkref(id)
  30.         return { _to = id }
  31.     end
  32.  
  33.     local function recurse(x)
  34.         if enclookup[x] then print("Using From Cache", x) return enclookup[x] end -- If we already have an encoded copy cached, use it
  35.         local t = type(x)
  36.  
  37.         if t == "string" or t == "number" then
  38.             return x
  39.         elseif t == "table" then
  40.             local mt = debug.getmetatable(x)
  41.             local out = {}
  42.             local id = addobj(out)
  43.             local ref = mkref(id)
  44.             enclookup[x] = ref
  45.             for k, v in pairs(x) do
  46.                 out[recurse(k)] = recurse(v) -- copy table
  47.             end
  48.             if mt then out._mt = recurse(mt) end -- If table has metatable, add it to output table
  49.             return ref
  50.         elseif t == "function" then
  51.             local ok, code = pcall(string.dump, x)
  52.             if ok then
  53.                 local info = debug.getinfo(x, "u") -- contains number of upvalues of function
  54.                 local upvalues = {}
  55.                 for i = 1, info.nups do
  56.                     local name, value = debug.getupvalue(x, i)
  57.                     upvalues[i] = value -- upvalues seem to be handled by index, so the name's not important
  58.                 end
  59.                 local env
  60.                 if getfenv then env = getfenv(x)
  61.                 else env = upvalues[1] end -- it seems that in Lua 5.3 the first upvalue is the function environment.
  62.                 local out = {
  63.                     _t = "f", -- type: function
  64.                     c = code,
  65.                     u = recurse(upvalues),
  66.                     e = recurse(env)
  67.                 }
  68.                 local id = addobj(out)
  69.                 local ref = mkref(id)
  70.                 enclookup[x] = ref
  71.                 return ref
  72.             else
  73.                 return nil -- is a non-Lua-defined function, so we can't operate on it very much
  74.             end
  75.         end
  76.     end
  77.  
  78.     local root = recurse(x)
  79.  
  80.     local inverted = {}
  81.     for k, v in pairs(objects) do
  82.         inverted[v] = k
  83.     end
  84.  
  85.     print(e(root), e(objects), e(inverted))
  86.  
  87.     inverted._root = copy(root)
  88.  
  89.     return inverted
  90. end
  91.  
  92. function ultradump.load(objects)
  93.     local function recurse(x)
  94.         local t = type(x)
  95.         if t == "string" or t == "number" then
  96.             return x
  97.         elseif t == "table" then
  98.  
  99.         else
  100.             error("Unexpected Type " .. t)
  101.         end
  102.     end
  103.  
  104.     return recurse(objects._root)
  105. end
  106.  
  107. local input = {
  108.     1, 2, 3, function() print "HI!" end
  109. }
  110. input[5] = input
  111.  
  112. local out = ultradump.dump(input)
  113. --print(e(out))
  114.  
  115. return ultradump
Add Comment
Please, Sign In to add comment