local modem = peripheral.wrap("back")
local oldos = {}
for k, v in pairs(os) do
oldos[k] = v
end
local function test(event)
if event.type == "peripheral" or event.type == "terminal" then
return true
elseif event.type == "turtle" or event.type == "fs" then
return true
else
return false
end
end
function os.pullEventRaw(...)
local args = {...}
local event = {oldos.pullEventRaw(unpack(args))}
if event[1] == "modem_message" and type(event[5]) == "table" and test(event[5]) then
event[5].info[1] = event[3].."_"..event[5].info[1]
return unpack(event[5].info)
else
return unpack(event)
end
end
function setModem(side)
if peripheral.getType(side) == "modem" then
modem = peripheral.wrap(side)
else
error("Modem expected, got "..peripheral.getType(side).."!", 2)
end
if not modem.isWireless() then
modem = peripheral.wrap("back")
error("Expected Wireless Modem, got Wired Modem!", 2)
end
end
function wrap(side, channela, channelb)
if not tonumber(channela) then
error("Expected Number, got ".. type(channela).."!", 2)
end
if not tonumber(channelb) then
error("Expected Number, got ".. type(channela).."!", 2)
end
local per, call = {
side = side,
channela = channela,
channelb = channelb,
}, {}
modem.open(channela)
modem.transmit(channela, channelb, {side = side, type = "wrap_peripheral"})
repeat
local msg = {os.pullEvent("modem_message")}
if type(msg[5]) == "table" and msg[5].type == "peripheral" then
for k, v in pairs(msg[5].info) do
call[v] = function(...)
modem.transmit(channela, channelb, {type = "call", side = side, call = v, params = {...}})
repeat
local event = {os.pullEvent("modem_message")}
if event[5].type == "peripheral" then
return unpack(event[5].info)
end
until(event[5].type == "peripheral")
end
end
end
until(msg[5].type == "peripheral")
setmetatable(per, {__index = call})
return per
end
function call(side, channela, channelb, method, ...)
if not tonumber(channela) then
error("Expected number, got "..type(channela).."!", 2)
end
if not tonumber(channelb) then
error("Expected number, got "..type(channelb).."!", 2)
end
if not tostring(method) then
error("Expected string, got "..type(method).."!", 2)
end
modem.open(channela)
modem.transmit(channela, channelb, {type = "call", side = side, call = method, params = {...}})
while true do
local event = {os.pullEvent("modem_message")}
if event[5].type == "peripheral" then
return(unpack(event[5].info))
end
end
end
function term(channela, channelb)
local per, call = {
channela = channela,
channelb = channelb,
}, {}
modem.open(channela)
modem.transmit(channela, channelb, {type = "wrap_term"})
repeat
local msg = {os.pullEvent("modem_message")}
if type(msg[5]) == "table" and msg[5].type == "terminal" then
for k, v in pairs(msg[5].info) do
call[k] = function(...)
modem.transmit(channela, channelb, {type = "terminal_call", call = k, params = {...}})
repeat
local event = {os.pullEvent("modem_message")}
if event[5].type == "terminal" then
return unpack(event[5].info)
end
until(event[5].type == "terminal")
end
end
end
until(msg[5].type == "terminal")
setmetatable(per, {__index = call})
return per
end
function turtle(channela, channelb)
local per, call = {
channela = channela,
channelb = channelb,
}, {}
modem.open(channela)
modem.transmit(channela, channelb, {type = "wrap_turtle"})
repeat
local msg = {os.pullEvent("modem_message")}
if type(msg[5]) == "table" and msg[5].type == "turtle" then
for k, v in pairs(msg[5].info) do
call[k] = function(...)
modem.transmit(channela, channelb, {type = "turtle_call", call = k, params = {...}})
repeat
local event = {os.pullEvent("modem_message")}
if event[5].type == "turtle" then
return unpack(event[5].info)
end
until(event[5].type == "turtle")
end
end
end
until(event[5].type == "turtle")
setmetatable(per, {__index = call})
return per
end
function fs(channela, channelb)
local per, call = {
channela = channel,
channelb = channelb,
}, {}
modem.open(channela)
modem.transmit(channela, channelb, {type = "wrap_fs"})
repeat
local msg = {os.pullEvent("modem_message")}
if type(msg[5]) == "table" and msg[5].type == "fs" then
for k, v in pairs(msg[5].info) do
call[k] = function(...)
modem.transmit(channela, channelb, {type = "fs_call", call = k, params = {...}})
repeat
local event = {os.pullEvent("modem_message")}
if event[5].type == "fs" then
return unpack(event[5].info)
end
until(event[5].type == "fs")
end
end
end
until(event[5].type == "fs")
setmetatable(per, {__index = call})
return per
end