Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- remoteTerminal = {}
- local tArgs = {...}
- remoteTerminal.termCommands = {}
- remoteTerminal.termServerFunctions = {}
- remoteTerminal.queuedFunctionCalls = {}
- local scrWidth, scrHeight = term.getSize()
- local sides = peripheral.getNames()
- function getModem()
- while true do
- for k, v in ipairs(sides) do
- if peripheral.getType(v) == "modem" then
- selectedside = v
- return v
- end
- end
- sleep()
- end
- end
- getModem()
- rednet.open(selectedside)
- function yield(time)
- for k=1, time or 1 do
- os.queueEvent("yield")
- os.pullEvent("yield")
- end
- end
- function addToQueuedFunctionCalls(name, ...)
- if type(name) == "function" then
- error("Name cannot be of type function", 2)
- end
- local tArgs = {...}
- local valuesToRemove = {}
- local varsRemoved = 0
- for k, v in pairs(tArgs) do
- if type(v) == "function" then
- valuesToRemove[#valuesToRemove + 1] = k
- elseif v == "true" then
- tArgs[k] = true
- elseif v == "false" then
- tArgs[k] = false
- end
- end
- for k, v in pairs(valuesToRemove) do
- table.remove(tArgs, k - varsRemoved)
- varsRemoved = varsRemoved + 1
- end
- remoteTerminal.queuedFunctionCalls[#remoteTerminal.queuedFunctionCalls + 1] = {tostring(name), unpack(tArgs)}
- end
- function newCommand(name, funct)
- function onUse(...)
- local tArgs = {...}
- local removedElements = 0
- for k=1, #tArgs do
- if checkTableForFunctions(tArgs[k]) then
- table.remove(tArgs, k - removedElements)
- removedElements = removedElements + 1
- end
- end
- addToQueuedFunctionCalls(name, unpack(tArgs))
- return funct(...)
- end
- remoteTerminal.termServerFunctions[name] = onUse
- return onUse
- end
- for k, v in pairs(term) do
- remoteTerminal.termCommands[k] = v
- end
- local redrawTimer = os.startTimer(.1)
- function checkTableForFunctions(tab)
- if type(tab) ~= "table" then return false end
- for k, v in pairs(tab) do
- if type(v) == "function" then
- return true
- elseif type(v) == "table" then
- if checkTableForFunctions(tab[k]) then
- return true
- end
- end
- end
- return false
- end
- function server(id)
- if id == os.getComputerID() then
- error("The client ID must not be the same as the server computer's ID.", 0)
- end
- remoteTerminal.id = id or tonumber(tArgs[2]) or 4 or rednet.receive("Connect")
- sleep(.1)
- for k, v in pairs(term) do
- term[k] = newCommand(k, term[k])
- end
- for k, v in pairs(remoteTerminal.termServerFunctions) do
- term[k] = remoteTerminal.termServerFunctions[k]
- end
- local oldRaw = os.pullEventRaw
- function os.pullEventRaw(...)
- local event, p1, p2, p3, p4, p5 = oldRaw(...)
- if event == "rednet_message" then
- if p1 == remoteTerminal.id or id or p1 then
- local eTab = textutils.unserialize(p2)
- local sE, sE1, sE2, sE3, sE4, sE5 = eTab[1], eTab[2], eTab[3], eTab[4], eTab[5], eTab[6]
- if not (sE == le and sE1 == lp1 and sE2 == lp2 and sE3 == lp3 and sE4 == lp4 and sE5 == lp5) then
- os.queueEvent(sE, sE1, sE2, sE3, sE4, sE5)
- end
- le, lp1, lp2, lp3, lp4, lp5 = sE, sE1, sE2, sE3, sE4, sE5
- end
- elseif event == "timer" and p1 == redrawTimer then
- local removedElements = 0
- for k=1, #remoteTerminal.queuedFunctionCalls do
- if checkTableForFunctions(remoteTerminal.queuedFunctionCalls[k]) then
- table.remove(remoteTerminal.queuedFunctionCalls, k - removedElements)
- removedElements = removedElements + 1
- end
- end
- local str = textutils.serialize(remoteTerminal.queuedFunctionCalls)
- for k=1, 2 do
- yield()
- rednet.send(id, str)
- end
- remoteTerminal.queuedFunctionCalls = {}
- redrawTimer = os.startTimer(.1)
- else
- return event, p1, p2, p3, p4, p5
- end
- return event, p1, p2, p3, p4, p5
- end
- function os.pullEvent( sFilter )
- local eventData = { os.pullEventRaw( sFilter ) }
- if eventData[1] == "terminate" then
- error( "Terminated", 0 )
- end
- return unpack( eventData )
- end
- remoteTerminal.queuedFunctionCalls = {}
- redrawTimer = os.startTimer(.1)
- print("To disconnect, reboot the computer.")
- end
- function drawDebug(functName, ...)
- local tArgs = {...}
- local ox, oy = term.getCursorPos()
- term.setCursorPos(1, scrHeight)
- term.clearLine()
- term.write(functName)
- for k, v in ipairs(tArgs) do
- term.write(", "..tostring(v))
- end
- term.setCursorPos(ox, oy)
- end
- function client(id)
- remoteTerminal.id = id or tonumber(tArgs[2]) or 3
- print("Redirected to remote terminal")
- local clearScreenOnFirstDrawMessageSent = true
- while true do
- local event, p1, p2, p3, p4, p5 = os.pullEventRaw()
- if event == "rednet_message" then
- if p1 == remoteTerminal.id or id or p1 then
- local drawTab = textutils.unserialize(p2)
- for k, v in pairs(drawTab) do
- if v[1] == "print" then
- local f = _G[v[1]]
- f(v[2], v[3], v[4], v[5], v[6], v[7])
- else
- local f = term[v[1]]
- if v[1] ~= "scroll" then
- if v[1] ~= "setCursorPos" and v[1] ~= "getCursorPos" then
- --drawDebug(v[1], v[2], v[3], v[4], v[5], v[6])
- end
- end
- if v[1] == "redirect" then
- f(v[2] or term.native())
- elseif v[1] == "current" then
- elseif v[1] == "setCursorBlink" then
- if type(v[2]) == "string" then
- v[2] = v[2] == "true"
- end
- f(v[2])
- elseif v[1] == "scroll" then
- f(tonumber(v[2]) or 0)
- elseif v[1] == "setCursorPos" then
- f(tonumber(v[2]) or 0, tonumber(v[3]) or 0)
- elseif v[1] == "setTextColor" or v[1] == "setTextColour" or v[1] == "setBackgroundColor" or v[1] == "setBackgroundColour" then
- f(tonumber(v[2]) or 0)
- else
- f(v[2], v[3], v[4], v[5], v[6], v[7])
- if clearScreenOnFirstDrawMessageSent then
- term.clear()
- clearScreenOnFirstDrawMessageSent = false
- end
- end
- end
- end
- else
- rednet.send(id, textutils.serialize({event, p1, p2, p3, p4, p5}))
- end
- elseif event == "terminate" then
- error("Terminated", 0)
- else
- rednet.send(id, textutils.serialize({event, p1, p2, p3, p4, p5}))
- end
- end
- end
- if tArgs[1] == "server" then
- if not tArgs[2] then
- print("Usage: "..shell.getRunningProgram().." server <computer id>")
- print("Only the computer with the ID you typed in will be able to affect this computer.")
- return false
- end
- server(tonumber(tArgs[2]))
- elseif tArgs[1] == "client" then
- if not tArgs[2] then
- print("Usage: "..shell.getRunningProgram().." client <computer id>")
- print("The computer ID must be the same as the server you want to connect to.")
- return false
- end
- local canceled, err = pcall(client, tonumber(tArgs[2]))
- if not canceled then
- term.setCursorPos(1, 1)
- error(err, 0)
- end
- else
- print("On the computer you want to connect to, run remoteTerminal server <computer id>")
- print("Only the computer with the ID you typed in will be able to affect this computer.")
- print("Then, on this computer run "..shell.getRunningProgram().." client <computer id>")
- print("The computer ID must be the same as the server you want to connect to.")
- end
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement