Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- local debug = false
- local trustedPull = os.pullEvent
- local trustedPullRaw = os.pullEventRaw
- function doCall(id, func, ...)
- if func ~= "new" and func ~= "loadAPI" then
- local argStr = ""
- for i=1, #arg-1 do
- if type(arg[i]) ~= "string" then
- argStr = argStr..textutils.serialize(arg[i])..", "
- else
- argStr = argStr.."\""..arg[i].."\""..", "
- end
- end
- if type(arg[#arg]) ~= "string" then
- argStr = argStr..textutils.serialize(arg[#arg])
- else
- argStr = argStr.."\""..arg[#arg].."\""
- end
- if debug then
- print("return rpc."..func.."("..argStr..")")
- end
- local fn, err = loadstring("return rpc."..func.."("..argStr..")")
- if fn then
- setfenv(fn, getfenv(2))
- local pcall_return = {pcall(fn)}
- if pcall_return[1] then
- local returns = {}
- for i=2, #pcall_return do
- table.insert(returns, pcall_return[i])
- end
- return returns
- else
- if string.find(pcall_return[2], "attempt to call nil") ~= nil then
- return {"attempt to call nil"}
- elseif string.find(pcall_return[2], "attempt to call table") ~= nil then
- return {"attempt to call table"}
- else
- return {pcall_return[2]}
- end
- end
- else
- return {err}
- end
- end
- end
- function remoteCall(remoteID, remoteFunc, ...)
- local msg = textutils.serialize({"rpc", "call", remoteFunc, unpack(arg)})
- rednet.send(remoteID, msg)
- local event, id, func, returns = os.pullEvent("rpc_return")
- returns = textutils.unserialize(returns)
- if id == remoteID and func == remoteFunc then
- return unpack(returns)
- end
- end
- function broadcastRemoteCall(remoteID, remoteFunc, ...)
- local msg = textutils.serialize({"rpc", "call", remoteFunc, unpack(arg)})
- rednet.broadcast(msg)
- end
- function setDebug(d)
- debug = d
- end
- function new(func, body)
- if func ~= "doCall" and func ~= "remoteCall" and func ~= "new" and func ~= "loadAPI" and func ~= "run" then
- if type(body) == "string" then
- _G["rpc"][func] = loadstring(body)
- elseif type(body) == "function" then
- _G["rpc"][func] = body
- else
- error("Invalid function passed to rpc.new")
- end
- end
- end
- function loadAPI(file)
- local env = {}
- setmetatable(env, {__index = _G})
- local func, err = loadfile(file)
- if func then
- setfenv(func, env)
- func()
- local api = {}
- for i,v in pairs(env) do
- api[i] = v
- end
- rpc[fs.getName(file)] = api
- return true
- else
- return false, err
- end
- end
- function run()
- while true do
- local event, id, msg = trustedPull("rednet_message")
- msg = textutils.unserialize(msg)
- if type(msg) == "table" then
- if msg[1] == "rpc" then
- if msg[2] == "call" then
- local args = {}
- for i=4, #msg do
- table.insert(args, msg[i])
- end
- os.queueEvent("rpc_call", msg[3], args)
- local returns = doCall(id, msg[3], unpack(args))
- rednet.send(id, textutils.serialize({"rpc", "return", msg[3], returns}))
- elseif msg[2] == "return" then
- os.queueEvent("rpc_return", id, msg[3], textutils.serialize(msg[4]))
- end
- end
- end
- end
- end
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement