daily pastebin goal
64%
SHARE
TWEET

OpenComputer RCMicroBios

EdTheDog May 16th, 2018 103 Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. local haveSign=false
  2. local haveTunnel=false
  3. local haveModem=false
  4. for _,componentType in component.list() do
  5. if componentType=="sign" then haveSign=true end
  6. if componentType=="tunnel" then haveTunnel=true end
  7. if componentType=="modem" then haveModem=true end
  8. end
  9. local canSend=haveModem or haveTunnel
  10. if haveSign and canSend then
  11. function serialize(value, pretty)
  12. local kw={["and"]=true,["break"]=true,["do"]=true,["else"]=true,
  13. ["elseif"]=true,["end"]=true,["false"]=true,["for"]=true,
  14. ["function"]=true,["goto"]=true,["if"]=true,["in"]=true,
  15. ["local"]=true,["nil"]=true,["not"]=true,["or"]=true,
  16. ["repeat"]=true,["return"]=true,["then"]=true,["true"]=true,
  17. ["until"]=true,["while"]=true}
  18. local id="^[%a_][%w_]*$"
  19. local ts={}
  20. local function s(v,l)
  21. local t=type(v)
  22. if t=="nil" then
  23. return "nil"
  24. elseif t=="boolean" then
  25. return v and "true" or "false"
  26. elseif t=="number" then
  27. if v~=v then
  28. return "0/0"
  29. elseif v==math.huge then
  30. return "math.huge"
  31. elseif v==-math.huge then
  32. return "-math.huge"
  33. else
  34. return tostring(v)
  35. end
  36. elseif t=="string" then
  37. return string.format("%q",v):gsub("\\\n","\\n")
  38. elseif t=="table" and pretty and getmetatable(v) and getmetatable(v).__tostring then
  39. return tostring(v)
  40. elseif t=="table" then
  41. if ts[v] then
  42. if pretty then
  43. return "recursion"
  44. else
  45. error("tables with cycles are not supported")
  46. end
  47. end
  48. ts[v]=true
  49. local i,r=1,nil
  50. local f
  51. if pretty then
  52. local ks,sks,oks={},{},{}
  53. for k in pairs(v) do
  54. if type(k)=="number" then
  55. table.insert(ks,k)
  56. elseif type(k)=="string" then
  57. table.insert(sks,k)
  58. else
  59. table.insert(oks,k)
  60. end
  61. end
  62. table.sort(sks)
  63. for _,k in ipairs(sks) do
  64. table.insert(ks,k)
  65. end
  66. for _,k in ipairs(oks) do
  67. table.insert(ks, k)
  68. end
  69. local n=0
  70. f = table.pack(function()
  71. n = n+1
  72. local k=ks[n]
  73. if k~=nil then
  74. return k,v[k]
  75. else
  76. return nil
  77. end
  78. end)
  79. else
  80. f = table.pack(pairs(v))
  81. end
  82. for k,v in table.unpack(f) do
  83. if r then
  84. r=r..","..(pretty and ("\n"..string.rep(" ",l)) or "")
  85. else
  86. r = "{"
  87. end
  88. local tk = type(k)
  89. if tk=="number" and k==i then
  90. i = i + 1
  91. r = r .. s(v, l + 1)
  92. else
  93. if tk=="string" and not kw[k] and string.match(k,id) then
  94. r=r..k
  95. else
  96. r=r.."["..s(k,l+1).."]"
  97. end
  98. r=r.."="..s(v,l+1)
  99. end
  100. end
  101. ts[v]=nil
  102. return (r or "{").."}"
  103. else
  104. if pretty then
  105. return tostring(t)
  106. else
  107. error("unsupported type: "..t)
  108. end
  109. end
  110. end
  111. local result=s(value,1)
  112. local limit=type(pretty)=="number" and pretty or 10
  113. if pretty then
  114. local truncate=0
  115. while limit>0 and truncate do
  116. truncate=string.find(result,"\n",truncate+1,true)
  117. limit=limit-1
  118. end
  119. if truncate then
  120. return result:sub(1,truncate).."..."
  121. end
  122. end
  123. return result
  124. end
  125. function split(source,delimiters)
  126. local elements={}
  127. local pattern='([^'..delimiters..']+)'
  128. string.gsub(source,pattern,function(value) elements[#elements+1]=value;end);
  129. return elements
  130. end
  131. local signInput=component.proxy(component.list("sign")()).getValue()
  132. if signInput~=nil then
  133. local newStuff = split(signInput,"\n")
  134. if newStuff[1]~=nil then _G.name = newStuff[1] end
  135. if newStuff[2]~=nil then _G.code = newStuff[2] end
  136. if newStuff[3]~=nil and haveModem then
  137. _G.port=tonumber(newStuff[3])
  138. component.proxy(component.list("modem")()).close()
  139. end
  140. else error("no sign of face") end
  141. if _G.port~=nil then component.proxy(component.list("modem")()).open(_G.port) end
  142. if _G.code~=nil and _G.name~=nil then
  143. if _G.port==nil and haveModem then error("modem but not port on line 3 of sign") end
  144. while true do
  145. local evt,_,from,port,_,receivedName,receivedCode,command=computer.pullSignal()
  146. if evt=="modem_message" and haveTunnel and receivedName==_G.name and receivedCode==_G.code then component.proxy(component.list("tunnel")()).send(_G.name,from,serialize(load(command)(),false)) end
  147. if evt=="modem_message" and haveModem and receivedName==_G.name and receivedCode==_G.code then component.proxy(component.list("modem")()).broadcast(_G.port,_G.name,from,serialize(load(command)(),false)) end
  148. end
  149. end
  150. end
  151. computer.shutdown()
RAW Paste Data
Top