Advertisement
Guest User

syncro

a guest
Nov 18th, 2016
106
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 2.46 KB | None | 0 0
  1. local filename = ".syncro"
  2.  
  3. local tArg = {...}
  4. _G.isClient = false
  5.  
  6. local evtqueue = {}
  7.  
  8. if parallel.waitForAny(function()
  9.   sleep(0.25) end, function()
  10.   while true do
  11.     local _,key = os.pullEvent("key")
  12.     if key == keys.home then
  13.       return
  14.     end
  15.   end
  16. end) == 2 then
  17.   _G.isClient = true
  18.   print("You are client.")
  19. end
  20.  
  21. sliceEventLog = function(newSize)
  22.   local file = fs.open(filename,"r")
  23.   local line = ""
  24.   local input = {}
  25.   repeat
  26.     line = file.readLine()
  27.     if line then
  28.       table.insert(input,line)
  29.     end
  30.   until not line
  31.   file.close()
  32.   file = fs.open(filename,"w")
  33.   for a = #input-(newSize or 5), #input do
  34.     file.writeLine(input[a])
  35.   end
  36.   file.close()
  37. end
  38.  
  39. readEvents = function()
  40.   local lines = 0
  41.   while true do
  42.     sleep(0)
  43.     local file = fs.open(filename,"r")
  44.     if file then
  45.       local line = ""
  46.       while true do
  47.         line = file.readLine()
  48.         if line and (#(line or {}) > 1) then
  49.           lines = lines + 1
  50.           local isc = line:sub(1,1) == "t"
  51.           line = line:sub(2)
  52.           if isc ~= _G.isClient and textutils.unserialise(line) then
  53.             os.queueEvent(unpack(textutils.unserialise(line)))
  54.           end
  55.         end
  56.         if not line then sleep(0) end
  57.         if lines > 50 then
  58.           sliceEventLog(5)
  59.           lines = 5
  60.         end
  61.       end
  62.     end
  63.   end
  64. end
  65.  
  66. getEvents = function()
  67.   while true do
  68.     local evt = {os.pullEvent()}
  69.     table.insert(evtqueue,evt)
  70.   end
  71. end
  72.  
  73. writeEvents = function()
  74.   while true do
  75.     if #evtqueue > 0 then
  76.       local evt = evtqueue[1]
  77.       --if evt[1] ~= "timer" then
  78.         local file = fs.open(filename,"a")
  79.         file.writeLine(tostring(_G.isClient):sub(1,1)..textutils.serialise(evt):gsub("\n",""))
  80.         file.close()
  81.       --end
  82.       table.remove(evtqueue,1)
  83.     else
  84.       sleep(0)
  85.     end
  86.   end
  87. end
  88.  
  89. carryOn = function()
  90.   local path
  91.   if not shell then
  92.     os.loadAPI("shell")
  93.   end
  94.   if #tArg > 0 then
  95.     path = shell.resolve(tArg[1])
  96.   else
  97.     path = "/rom/programs/shell"
  98.   end
  99.   local file = fs.open(path,"r")
  100.   if not file then
  101.     return "No such program", false
  102.   end
  103.   local func = loadstring(file.readAll())
  104.   setfenv(func,getfenv())
  105.   table.remove(tArg,1)
  106.   func(unpack(tArg))
  107.   os.shutdown()
  108. end
  109.  
  110. local funky = {
  111.   writeEvents,
  112.   readEvents,
  113.   getEvents,
  114. }
  115.  
  116. if not isClient then
  117.   table.insert(funky,carryOn)
  118. end
  119.  
  120. parallel.waitForAny(unpack(funky))
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement