Advertisement
Ahlforn

Drone bios

May 15th, 2017
109
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 4.35 KB | None | 0 0
  1. local serialization = {}
  2. local local_pairs = function(tbl)
  3.   local mt = getmetatable(tbl)
  4.   return (mt and mt.__pairs or pairs)(tbl)
  5. end
  6. function serialization.serialize(value, pretty)
  7.   local kw =  {["and"]=true, ["break"]=true, ["do"]=true, ["else"]=true,
  8.                ["elseif"]=true, ["end"]=true, ["false"]=true, ["for"]=true,
  9.                ["function"]=true, ["goto"]=true, ["if"]=true, ["in"]=true,
  10.                ["local"]=true, ["nil"]=true, ["not"]=true, ["or"]=true,
  11.                ["repeat"]=true, ["return"]=true, ["then"]=true, ["true"]=true,
  12.                ["until"]=true, ["while"]=true}
  13.   local id = "^[%a_][%w_]*$"
  14.   local ts = {}
  15.   local function s(v, l)
  16.     local t = type(v)
  17.     if t == "nil" then
  18.       return "nil"
  19.     elseif t == "boolean" then
  20.       return v and "true" or "false"
  21.     elseif t == "number" then
  22.       if v ~= v then
  23.         return "0/0"
  24.       elseif v == math.huge then
  25.         return "math.huge"
  26.       elseif v == -math.huge then
  27.         return "-math.huge"
  28.       else
  29.         return tostring(v)
  30.       end
  31.     elseif t == "string" then
  32.       return string.format("%q", v):gsub("\\\n","\\n")
  33.     elseif t == "table" and pretty and getmetatable(v) and getmetatable(v).__tostring then
  34.       return tostring(v)
  35.     elseif t == "table" then
  36.       if ts[v] then
  37.         if pretty then
  38.           return "recursion"
  39.         else
  40.           error("tables with cycles are not supported")
  41.         end
  42.       end
  43.       ts[v] = true
  44.       local i, r = 1, nil
  45.       local f
  46.       if pretty then
  47.         local ks, sks, oks = {}, {}, {}
  48.         for k in local_pairs(v) do
  49.           if type(k) == "number" then
  50.             table.insert(ks, k)
  51.           elseif type(k) == "string" then
  52.             table.insert(sks, k)
  53.           else
  54.             table.insert(oks, k)
  55.           end
  56.         end
  57.         table.sort(ks)
  58.         table.sort(sks)
  59.         for _, k in ipairs(sks) do
  60.           table.insert(ks, k)
  61.         end
  62.         for _, k in ipairs(oks) do
  63.           table.insert(ks, k)
  64.         end
  65.         local n = 0
  66.         f = table.pack(function()
  67.           n = n + 1
  68.           local k = ks[n]
  69.           if k ~= nil then
  70.             return k, v[k]
  71.           else
  72.             return nil
  73.           end
  74.         end)
  75.       else
  76.         f = table.pack(local_pairs(v))
  77.       end
  78.       for k, v in table.unpack(f) do
  79.         if r then
  80.           r = r .. "," .. (pretty and ("\n" .. string.rep(" ", l)) or "")
  81.         else
  82.           r = "{"
  83.         end
  84.         local tk = type(k)
  85.         if tk == "number" and k == i then
  86.           i = i + 1
  87.           r = r .. s(v, l + 1)
  88.         else
  89.           if tk == "string" and not kw[k] and string.match(k, id) then
  90.             r = r .. k
  91.           else
  92.             r = r .. "[" .. s(k, l + 1) .. "]"
  93.           end
  94.           r = r .. "=" .. s(v, l + 1)
  95.         end
  96.       end
  97.       ts[v] = nil -- allow writing same table more than once
  98.       return (r or "{") .. "}"
  99.     else
  100.       if pretty then
  101.         return tostring(v)
  102.       else
  103.         error("unsupported type: " .. t)
  104.       end
  105.     end
  106.   end
  107.   local result = s(value, 1)
  108.   local limit = type(pretty) == "number" and pretty or 10
  109.   if pretty then
  110.     local truncate = 0
  111.     while limit > 0 and truncate do
  112.       truncate = string.find(result, "\n", truncate + 1, true)
  113.       limit = limit - 1
  114.     end
  115.     if truncate then
  116.       return result:sub(1, truncate) .. "..."
  117.     end
  118.   end
  119.   return result
  120. end
  121. function serialization.unserialize(data)
  122.   checkArg(1, data, "string")
  123.   local result, reason = load("return " .. data, "=data", _, {math={huge=math.huge}})
  124.   if not result then
  125.     return nil, reason
  126.   end
  127.   local ok, output = pcall(result)
  128.   if not ok then
  129.     return nil, output
  130.   end
  131.   return output
  132. end
  133. local m=component.proxy(component.list("modem")())
  134.  
  135. m.open(2412)
  136. local function respond(...)
  137.   local args=table.pack(...)
  138.   pcall(function() m.broadcast(2412, table.unpack(args)) end)
  139. end
  140. local function receive()
  141.   while true do
  142.     local evt,_,_,_,_,cmd=computer.pullSignal()
  143.     if evt=="modem_message" then return load(cmd) end
  144.   end
  145. end
  146. while true do
  147.   local result,reason=pcall(function()
  148.     local result,reason=receive()
  149.     if not result then return respond(reason) end
  150.     respond(serialization.serialize(result()))
  151.   end)
  152.   if not result then respond(reason) end
  153. end
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement