Advertisement
LDDestroier

Event Transmitter (ComputerCraft)

Mar 3rd, 2017
232
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 1.93 KB | None | 0 0
  1. --[[
  2.     pastebin get hfBQ4wCs evt
  3. --]]
  4.  
  5. local evtPath = ".evt"
  6. local id = tostring(math.random(0,99999999999))
  7.  
  8. bannedEvents = {
  9.     ["timer"] = true,
  10.     ["queue_event"] = true,
  11.     ["self_event_check"] = true,
  12. }
  13.  
  14. curEvent = {} --used to prevent feedback loops
  15.  
  16. addEvent = function(evt) -- table evt
  17.     if bannedEvents[evt[1]] then return end
  18.     local file = fs.open(evtPath,"a")
  19.     file.writeLine(textutils.serialise({id,evt}):gsub("\n",""):gsub("  ",""):gsub(",}","}"))
  20.     file.close()
  21. end
  22.  
  23. useEvent = function()
  24.     local contents = {}
  25.     local file = fs.open(evtPath,"r")
  26.     local line = {}
  27.     repeat
  28.         line = file.readLine()
  29.         if line then
  30.             contents[#contents+1] = textutils.unserialize(line) or {}
  31.         end
  32.     until not line
  33.     local usedLines = {}
  34.     for a = 1, #contents do
  35.         if type(contents[a]) == "table" then
  36.             if contents[a][1] ~= id then
  37.                 curEvent = contents[a][2]
  38.                 os.queueEvent(unpack(contents[a][2]))
  39.                 os.queueEvent("self_event_check","true")
  40.                 table.remove(contents,a)
  41.                 usedLines[#usedLines+1] = a
  42.             else
  43.                 os.queueEvent("self_event_check","true")
  44.             end
  45.         else
  46.             os.queueEvent("self_event_check","true")
  47.         end
  48.     end
  49.     for a = #usedLines,1,-1 do
  50.         table.remove(contents,usedLines[a])
  51.     end
  52.     local file = fs.open(evtPath,"w")
  53.     for a = 1, #contents do
  54.         file.writeLine(textutils.serialize(contents[a]):gsub("\n",""):gsub("  ",""):gsub(",}","}"))
  55.     end
  56. end
  57.  
  58. loopAdd = function()
  59.     while true do
  60.         local event = {os.pullEvent()}
  61.         if event ~= curEvent then
  62.             local a,b = os.pullEvent("self_event_check")
  63.             if b == true then
  64.                 addEvent(event)
  65.             end
  66.         end
  67.         curEvent = {}
  68.     end
  69. end
  70.  
  71. loopUse = function()
  72.     while true do
  73.         useEvent()
  74.         os.queueEvent("queue_event")
  75.         os.pullEvent("queue_event")
  76.     end
  77. end
  78.  
  79. doShell = function()
  80.     dofile("rom/programs/shell")
  81. end
  82.  
  83. if not fs.exists(evtPath) then
  84.     local file = fs.open(evtPath,"w")
  85.     file.close()
  86. end
  87.  
  88. parallel.waitForAny(loopUse,loopAdd,doShell)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement