Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- local serverID = false
- local remoteExecuteProtocol = "remoteExecute"
- local function findFunction(functionTable)
- if type(functionTable) == "table" then
- local func = _G
- for index, tableKey in ipairs(functionTable) do
- if type(func) == "table" and func[tableKey] ~= nil then
- func = func[tableKey]
- else
- return false, index
- end
- end
- if type(func) == "function" then
- return func
- else
- return false, -1
- end
- else
- return false, 0
- end
- end
- local forwardEvent = {
- modem_message = true,
- rednet_message = true,
- peripheral = true,
- peripheral_detach = true,
- }
- local currCommandNum = false
- local function run()
- local event, forward
- while serverID do
- event, forward = {coroutine.yield()}, true
- if event[1] == "rednet_message" then
- local id, message, protocol = event[2], event[3], event[4]
- if id == serverID and protocol == remoteExecuteProtocol and type(message) == "table" then
- forward = false
- if message.commandNum then
- currCommandNum = message.commandNum
- local func, index = findFunction(message.func)
- if func then
- local result = {func(unpack(message.args))}
- local reply = {
- commandResponse = true,
- commandNum = currCommandNum,
- success = true,
- result = result,
- }
- rednet.send(serverID, reply, remoteExecuteProtocol)
- else
- local reply = {
- commandResponse = true,
- commandNum = currCommandNum,
- success = false,
- result = {"invalid_function", index},
- }
- rednet.send(serverID, reply, remoteExecuteProtocol)
- end
- currCommandNum = false
- elseif message.connection then
- if message.connect then
- rednet.send(serverID, "connected", remoteExecuteProtocol)
- elseif message.disconnect then
- print("disconnected from server")
- serverID = false
- end
- end
- end
- elseif event[1] == "modem_message" and type(event[5]) == "table" and event[5].sProtocol == remoteExecuteProtocol then
- forward = false
- end
- if forward and forwardEvent[event[1]] then
- local forwardedEvent = {
- remoteEvent = true,
- event = event,
- }
- rednet.send(serverID, forwardedEvent, remoteExecuteProtocol)
- end
- if event[1] == "terminate" then
- error("terminate")
- end
- end
- end
- local found = false
- for _, side in ipairs(rs.getSides()) do
- if peripheral.getType(side) == "modem" then
- rednet.open(side)
- found = true
- break
- end
- end
- if not found then
- error("no modem found")
- end
- while true do
- while not serverID do
- local id, message, protocol = rednet.receive(remoteExecuteProtocol)
- if type(message) == "table" and message.connection and message.connect then
- serverID = id
- rednet.send(serverID, "connected", remoteExecuteProtocol)
- print("connected to ", serverID)
- end
- end
- --main loop
- while serverID do
- local ok, err = pcall(run)
- if not ok then
- if serverID then
- if currCommandNum then
- local reply = {
- commandNum = currCommandNum,
- success = false,
- result = {"runtime_error", err},
- }
- rednet.send(serverID, reply, remoteExecuteProtocol)
- end
- end
- print("errored with: ", err)
- if string.find(err, "terminate") then
- return
- end
- end
- end
- end
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement