Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- local HOST_EVENTS,CLIENT_EVENTS = "events/host.txt","events/client.txt"
- local actions = {}
- local IS_HOST = turtle ~= nil or commands ~= nil
- local PE,PER = os.pullEvent,os.pullEventRaw
- local writeAction,readAction
- local windows = {current=term.current()}
- local function writeAction(path,...)
- local args = {...}
- local str = ""
- for i=1,#args do
- v = args[i]
- if type(v) == "table" then
- pcall(function() str = str .. textutils.serialise(v,{compact=true}) end)
- elseif type(v) == "string" then
- pcall(function() str = str .. textutils.serialiseJSON({v}):sub(2,-2) end)
- else
- str = str .. tostring(v)
- end
- str = str .. ","
- end
- str = str:gsub(",$","")
- local f
- repeat
- f = fs.open(path,fs.exists(path) and "a" or "w")
- pcall(function() f.write(str .. "\n") end)
- until pcall(function() f.close() end)
- end
- local function readActionRaw(path)
- if not fs.exists(path) then return end
- local f = fs.open(path,"r")
- local line = f.readLine()
- local s = false
- while line do
- s = true
- pcall(function() table.insert(actions,{loadstring("return " .. line)()}) end)
- line = f.readLine()
- end
- f.close()
- if s then fs.delete(path) end
- end
- local function wrapper(path)
- local epoch = os.epoch()
- return function(path)
- if epoch > epoch + 5000 then
- epoch = os.epoch()
- readActionRaw(path)
- end
- end
- end
- local function updateTerm()
- while true do
- os.pullEventRaw()
- end
- end
- if IS_HOST then
- local OLD = {term={}}
- for k,v in pairs(term.native()) do
- if type(v) == "function" then
- OLD.term[k] = v
- term.native()[k] = function(...)
- writeAction(CLIENT_EVENTS,k,...)
- return OLD.term[k](...)
- end
- end
- end
- os.pullEventRaw = function(sFilter)
- readActionRaw(HOST_EVENTS)
- while #actions > 0 do
- os.queueEvent(unpack(table.remove(actions,1)))
- end
- return PER(sFilter)
- end
- os.pullEvent = function(sFilter)
- local eventData = table.pack(os.pullEventRaw(sFilter))
- if eventData[1] == "terminate" then
- error("Terminated", 0)
- end
- return table.unpack(eventData, 1, eventData.n)
- end
- term.setTextColor(colors.white)
- term.setBackgroundColor(colors.black)
- term.clear()
- term.setCursorPos(1,1)
- else
- local epoch = os.epoch("utc")
- local valid_events = {key={epoch},char={epoch},mouse_click={epoch},key_up={epoch},mouse_drag={epoch},mouse_scroll={epoch},mouse_up={epoch},terminate={epoch},paste={epoch}}
- term.clear()
- os.pullEventRaw = function(sFilter)
- local args = {PER(sFilter)}
- if valid_events[args[1]] and (valid_events[args[1]][1] + 50 < os.epoch("utc") or valid_events[args[1]][2] ~= args[2]) then
- valid_events[args[1]] = {os.epoch("utc"),args[2]}
- writeAction(HOST_EVENTS,unpack(args))
- end
- return unpack(args)
- end
- parallel.waitForAll(function() while true do os.pullEventRaw() end end,function() while true do
- pcall(function()
- readActionRaw(CLIENT_EVENTS)
- while #actions > 0 do
- pcall(function() local action = table.remove(actions,1);term.native()[table.remove(action,1)](unpack(action))end)
- end sleep(0) end) end end)
- end
Advertisement
Add Comment
Please, Sign In to add comment