Advertisement
ben_mkiv

wirelessRedstone_bios.lua

Apr 20th, 2018
150
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 2.96 KB | None | 0 0
  1. dP=42001
  2. range=400
  3. --modem
  4. m=component.proxy(component.list("modem")())
  5. r=component.proxy(component.list("redstone")())
  6. --serialization
  7. local ser={}
  8. local local_pairs=function(tbl)
  9.     local mt=getmetatable(tbl)
  10.     return (mt and mt.__pairs or pairs)(tbl)
  11. end
  12. function ser.seril(value)
  13.     local kw={["and"]=true,["break"]=true,["do"]=true,["else"]=true,["elseif"]=true,["end"]=true,["false"]=true,["for"]=true,["function"]=true,["goto"]=true,["if"]=true,["in"]=true,["local"]=true,["nil"]=true,["not"]=true,["or"]=true,["repeat"]=true,["return"]=true,["then"]=true,["true"]=true,["until"]=true,["while"]=true}
  14.     local id="^[%a_][%w_]*$"
  15.     local ts={}
  16.     local function s(v,l)
  17.         local t=type(v)
  18.         if t=="nil" then return "nil"
  19.         elseif t=="boolean" then return v and "true" or "false"
  20.         elseif t=="number" then
  21.             if v~=v then return "0/0"
  22.             elseif v==math.huge then return "math.huge"
  23.             elseif v==-math.huge then return "-math.huge"
  24.             else return tostring(v) end
  25.         elseif t=="string" then return string.format("%q",v):gsub("\\\n","\\n")
  26.         elseif t=="table" then
  27.             if ts[v] then error("tcyc") end
  28.             ts[v]=true
  29.             local i,r=1, nil
  30.             local f=table.pack(local_pairs(v))
  31.             for k,v in table.unpack(f) do
  32.                 if r then r=r..","..(("\n"..string.rep(" ",l)) or "")
  33.                 else r="{" end
  34.                 local tk=type(k)
  35.                 if tk=="number" and k==i then
  36.                     i=i+1
  37.                     r=r..s(v,l+1)
  38.                 else
  39.                     if tk == "string" and not kw[k] and string.match(k,id) then r=r..k
  40.                     else r=r.."["..s(k,l+1).."]" end
  41.                     r=r.."="..s(v,l+1) end end
  42.             ts[v]=nil
  43.             return (r or "{").."}"
  44.         else error("ut "..t) end end
  45.     return s(value, 1)
  46. end
  47. function unserialize(data)
  48.     checkArg(1, data, "string")
  49.     local result, reason = load("return " .. data, "=data", _, {math={huge=math.huge}})
  50.     if not result then return nil, reason end
  51.     local ok, output = pcall(result)
  52.     if not ok then return nil, output end
  53.     return output
  54. end
  55. --remote replyfunc
  56. local function respond(...) local args = table.pack(...) pcall(function() m.broadcast(dP,table.unpack(args)) end) end
  57. local function receive()
  58.     while true do
  59.         local tmp = select(6, computer.pullSignal("modem_message"))
  60.         if tmp and tmp ~= "initRSNetwork" then
  61.             local data = unserialize(tmp)
  62.             if data.a == m.address or data.a == "all" then return load(data.c) end
  63.         end
  64.     end end
  65. --init
  66. m.setWakeMessage("initRSNetwork")
  67. m.open(dP)
  68. m.setStrength(range)
  69. --m.broadcast(dP, '{init="'..m.address..'"}')
  70. --mainloop
  71. while true do
  72.     local r1,r2=pcall(function() local r1,r2=receive()
  73.         if r1 then respond(ser.seril({r1()})) end end)
  74.     if not r1 and r2 then respond(ser.seril({r2})) end
  75. end
  76. m.close()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement