Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- -- node.lua
- local modem = peripheral.find("modem")
- if not modem then error("No modem found") end
- modem.open(1) -- Open channel 1 for communication
- local running = false
- local currentProgram = nil
- local accessMode = false
- local function waitForResponse(timeout)
- local timer = os.startTimer(timeout)
- while true do
- local event, p1, p2, p3, p4, p5 = os.pullEvent()
- if event == "modem_message" then
- return p4
- elseif event == "timer" and p1 == timer then
- return nil
- end
- end
- end
- local function networkFS(operation, ...)
- modem.transmit(1, 1, {type="FS_OP", operation=operation, args={...}})
- local response = waitForResponse(5)
- if response and response.success then
- return table.unpack(response.result)
- else
- return nil
- end
- end
- local sandboxEnv = {
- -- Standard Lua functions
- assert = assert,
- error = error,
- ipairs = ipairs,
- next = next,
- pairs = pairs,
- pcall = pcall,
- select = select,
- tonumber = tonumber,
- tostring = tostring,
- type = type,
- unpack = unpack,
- _VERSION = _VERSION,
- xpcall = xpcall,
- -- Tables
- table = table,
- -- Strings
- string = string,
- -- Math
- math = math,
- -- Redirected file system operations
- fs = {
- list = function(...) return networkFS("list", ...) end,
- exists = function(...) return networkFS("exists", ...) end,
- isDir = function(...) return networkFS("isDir", ...) end,
- isReadOnly = function(...) return networkFS("isReadOnly", ...) end,
- getName = function(...) return networkFS("getName", ...) end,
- getDrive = function(...) return networkFS("getDrive", ...) end,
- getSize = function(...) return networkFS("getSize", ...) end,
- getFreeSpace = function(...) return networkFS("getFreeSpace", ...) end,
- makeDir = function(...) return networkFS("makeDir", ...) end,
- move = function(...) return networkFS("move", ...) end,
- copy = function(...) return networkFS("copy", ...) end,
- delete = function(...) return networkFS("delete", ...) end,
- combine = function(...) return networkFS("combine", ...) end,
- open = function(path, mode)
- local handle = networkFS("open", path, mode)
- if not handle then return nil end
- return {
- close = function() networkFS("close", handle) end,
- write = function(data) networkFS("write", handle, data) end,
- writeLine = function(data) networkFS("writeLine", handle, data) end,
- read = function(count) return networkFS("read", handle, count) end,
- readLine = function() return networkFS("readLine", handle) end,
- readAll = function() return networkFS("readAll", handle) end,
- flush = function() networkFS("flush", handle) end,
- }
- end,
- },
- -- Other necessary CC functions
- sleep = sleep,
- write = write,
- print = print,
- printError = printError,
- read = read,
- os = {
- clock = os.clock,
- date = os.date,
- time = os.time,
- startTimer = os.startTimer,
- cancelTimer = os.cancelTimer,
- },
- peripheral = peripheral,
- term = term,
- }
- local function redirectIO()
- local oldTerm = term.current()
- local redirectTerm = {}
- function redirectTerm.write(text)
- oldTerm.write(text)
- modem.transmit(1, 1, {type="OUTPUT", output=text})
- end
- function redirectTerm.clear()
- oldTerm.clear()
- modem.transmit(1, 1, {type="OUTPUT", output="\n--- Screen Cleared ---\n"})
- end
- function redirectTerm.read(replaceChar, history)
- modem.transmit(1, 1, {type="INPUT_REQUEST"})
- local event, _, _, _, message = os.pullEvent("modem_message")
- while message.type ~= "INPUT" do
- event, _, _, _, message = os.pullEvent("modem_message")
- end
- return message.input
- end
- setmetatable(redirectTerm, {__index = oldTerm})
- term.redirect(redirectTerm)
- end
- local function runProgram(programCode)
- local func, err = load(programCode, "=program", "t", sandboxEnv)
- if not func then
- return false, "Error loading program: " .. err
- end
- redirectIO()
- local success, result = pcall(func)
- term.redirect(term.native())
- if not success then
- return false, "Error running program: " .. result
- end
- return true, "Program completed successfully"
- end
- while true do
- local event, side, channel, replyChannel, message = os.pullEvent("modem_message")
- if message.type == "SCAN" then
- modem.transmit(replyChannel, channel, {type="NODE", id=os.getComputerID()})
- elseif message.type == "RUN" and message.nodeId == os.getComputerID() then
- running = true
- currentProgram = message.program
- local success, result = runProgram(message.program)
- if success then
- modem.transmit(replyChannel, channel, {type="RESULT", result=result})
- else
- modem.transmit(replyChannel, channel, {type="ERROR", error=result})
- end
- running = false
- currentProgram = nil
- elseif message.type == "STOP" and message.nodeId == os.getComputerID() then
- running = false
- currentProgram = nil
- term.redirect(term.native())
- modem.transmit(replyChannel, channel, {type="STOPPED", id=os.getComputerID()})
- end
- end
Advertisement
Add Comment
Please, Sign In to add comment