36 lines
996 B
Lua
36 lines
996 B
Lua
local backend = {}
|
|
local network = require("entrypointlib")
|
|
local loop = coroutine.create(network.loop)
|
|
local filter = nil
|
|
|
|
-- some black magic bs that i pulled from https://gist.github.com/MCJack123/473475f07b980d57dd2bd818026c97e8
|
|
local env = getfenv(rednet.run)
|
|
env.os = setmetatable({
|
|
pullEventRaw = function ()
|
|
local ev = table.pack(coroutine.yield())
|
|
if filter == nil or filter == ev[1] then
|
|
_,filter = coroutine.resume(loop,table.unpack(ev, 1, ev.n))
|
|
end
|
|
return table.unpack(ev, 1, ev.n)
|
|
end
|
|
},{__index=env.os})
|
|
|
|
local target = nil
|
|
function backend.setTarget(id)
|
|
target = id
|
|
end
|
|
|
|
function backend.sendMessage(msg, id)
|
|
if not (id or target) then error("failed to imply or specify target id",2) end
|
|
network.send(msg,id or target)
|
|
end
|
|
|
|
function backend.receiveMessage()
|
|
local _,msg,id,hops
|
|
repeat
|
|
_,msg,id,hops = os.pullEvent("network_packet")
|
|
until id == (target or id)
|
|
return msg,id
|
|
end
|
|
|
|
return backend |