t8rt0t

mirror.lua

Jun 5th, 2024 (edited)
199
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 3.51 KB | None | 0 0
  1. local HOST_EVENTS,CLIENT_EVENTS = "events/host.txt","events/client.txt"
  2. local actions = {}
  3. local IS_HOST = turtle ~= nil or commands ~= nil
  4. local PE,PER = os.pullEvent,os.pullEventRaw
  5.  
  6. local writeAction,readAction
  7. local windows = {current=term.current()}
  8.  
  9. local function writeAction(path,...)
  10.     local args = {...}
  11.     local str = ""
  12.     for i=1,#args do
  13.         v = args[i]
  14.         if type(v) == "table" then
  15.             pcall(function() str = str .. textutils.serialise(v,{compact=true}) end)
  16.         elseif type(v) == "string" then
  17.             pcall(function() str = str .. textutils.serialiseJSON({v}):sub(2,-2) end)
  18.         else
  19.             str = str .. tostring(v)
  20.         end
  21.         str = str .. ","
  22.     end
  23.     str = str:gsub(",$","")
  24.     local f
  25.     repeat
  26.         f = fs.open(path,fs.exists(path) and "a" or "w")
  27.         pcall(function() f.write(str .. "\n") end)
  28.     until pcall(function() f.close() end)
  29. end
  30. local function readActionRaw(path)
  31.     if not fs.exists(path) then return end
  32.     local f = fs.open(path,"r")
  33.     local line = f.readLine()
  34.     local s = false
  35.     while line do
  36.         s = true
  37.         pcall(function() table.insert(actions,{loadstring("return " .. line)()}) end)
  38.         line = f.readLine()
  39.     end
  40.     f.close()
  41.     if s then fs.delete(path) end
  42. end
  43. local function wrapper(path)
  44.     local epoch = os.epoch()
  45.     return function(path)
  46.         if epoch > epoch + 5000 then
  47.             epoch = os.epoch()
  48.             readActionRaw(path)
  49.         end
  50.     end
  51. end
  52. local function updateTerm()
  53.     while true do
  54.         os.pullEventRaw()
  55.     end
  56. end
  57.  
  58. if IS_HOST then
  59.     local OLD = {term={}}
  60.     for k,v in pairs(term.native()) do
  61.         if type(v) == "function" then
  62.             OLD.term[k] = v
  63.             term.native()[k] = function(...)
  64.                 writeAction(CLIENT_EVENTS,k,...)
  65.                 return OLD.term[k](...)
  66.             end
  67.         end
  68.     end
  69.     os.pullEventRaw = function(sFilter)
  70.         readActionRaw(HOST_EVENTS)
  71.         while #actions > 0 do
  72.             os.queueEvent(unpack(table.remove(actions,1)))
  73.         end
  74.         return PER(sFilter)
  75.     end
  76.     os.pullEvent = function(sFilter)
  77.         local eventData = table.pack(os.pullEventRaw(sFilter))
  78.         if eventData[1] == "terminate" then
  79.             error("Terminated", 0)
  80.         end
  81.         return table.unpack(eventData, 1, eventData.n)
  82.     end
  83.     term.setTextColor(colors.white)
  84.     term.setBackgroundColor(colors.black)
  85.     term.clear()
  86.     term.setCursorPos(1,1)
  87. else
  88.     local epoch = os.epoch("utc")
  89.     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}}
  90.     term.clear()
  91.     os.pullEventRaw = function(sFilter)
  92.         local args = {PER(sFilter)}
  93.         if valid_events[args[1]] and (valid_events[args[1]][1] + 50 < os.epoch("utc") or valid_events[args[1]][2] ~= args[2]) then
  94.             valid_events[args[1]] = {os.epoch("utc"),args[2]}
  95.             writeAction(HOST_EVENTS,unpack(args))
  96.         end
  97.         return unpack(args)
  98.     end
  99.     parallel.waitForAll(function() while true do os.pullEventRaw() end end,function() while true do
  100.                 pcall(function()
  101.                 readActionRaw(CLIENT_EVENTS)
  102.                 while #actions > 0 do
  103.                     pcall(function() local action = table.remove(actions,1);term.native()[table.remove(action,1)](unpack(action))end)
  104.                 end sleep(0) end) end end)
  105. end
Advertisement
Add Comment
Please, Sign In to add comment