Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- local a=http.get"https://pastebin.com/raw/QTwgLZMA"local b=fs.open("bundlenet","w")b.write(a.readAll())a.close()b.close()
- local bnet = require "/bundlenet"
- local m = peripheral.find "modem"
- local function compact_serialize(x)
- local t = type(x)
- if t == "number" then
- return tostring(x)
- elseif t == "string" then
- return textutils.serialise(x)
- elseif t == "table" then
- local out = "{"
- for k, v in pairs(x) do
- out = out .. string.format("[%s]=%s,", compact_serialize(k), compact_serialize(v))
- end
- return out .. "}"
- elseif t == "boolean" then
- return tostring(x)
- else
- error("Unsupported type " .. t)
- end
- end
- local commands = {
- open = function(channel)
- m.open(channel)
- return true
- end,
- transmit = function(channel, reply_channel, message)
- m.transmit(channel, reply_channel, message)
- return true
- end,
- close = function(channel)
- m.close(channel)
- return true
- end,
- listen = function()
- local _, _, c, rc, msg, dist = os.pullEvent "modem_message"
- return { channel = c, reply_channel = rc, message = msg, distance = dist }
- end
- }
- if m then
- print "Configured as modem end."
- while true do
- local msg = bnet.receive()
- local data = textutils.unserialize(msg)
- if data then
- local command = table.remove(data, 1)
- if commands[command] then
- local ok, res = pcall(commands[command], unpack(data))
- local type = "result"
- if not ok then type = "error" end
- sleep(0.1)
- bnet.send(compact_serialize { type, res })
- else
- sleep(0.1)
- bnet.send(compact_serialize { "error", "command not found" })
- end
- end
- end
- else
- print "No modem found. Acting as BundleModem remote."
- return function(...)
- bnet.send(compact_serialize {...})
- sleep(0.3)
- local result = textutils.unserialize(bnet.receive())
- if result[1] == "result" then return result[2]
- elseif result[2] == "error" then error(result[2]) end
- return
- end
- end
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement