Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- --[[
- pastebin get hfBQ4wCs evt
- --]]
- local evtPath = ".evt"
- local id = tostring(math.random(0,99999999999))
- bannedEvents = {
- ["timer"] = true,
- ["queue_event"] = true,
- ["self_event_check"] = true,
- }
- curEvent = {} --used to prevent feedback loops
- addEvent = function(evt) -- table evt
- if bannedEvents[evt[1]] then return end
- local file = fs.open(evtPath,"a")
- file.writeLine(textutils.serialise({id,evt}):gsub("\n",""):gsub(" ",""):gsub(",}","}"))
- file.close()
- end
- useEvent = function()
- local contents = {}
- local file = fs.open(evtPath,"r")
- local line = {}
- repeat
- line = file.readLine()
- if line then
- contents[#contents+1] = textutils.unserialize(line) or {}
- end
- until not line
- local usedLines = {}
- for a = 1, #contents do
- if type(contents[a]) == "table" then
- if contents[a][1] ~= id then
- curEvent = contents[a][2]
- os.queueEvent(unpack(contents[a][2]))
- os.queueEvent("self_event_check","true")
- table.remove(contents,a)
- usedLines[#usedLines+1] = a
- else
- os.queueEvent("self_event_check","true")
- end
- else
- os.queueEvent("self_event_check","true")
- end
- end
- for a = #usedLines,1,-1 do
- table.remove(contents,usedLines[a])
- end
- local file = fs.open(evtPath,"w")
- for a = 1, #contents do
- file.writeLine(textutils.serialize(contents[a]):gsub("\n",""):gsub(" ",""):gsub(",}","}"))
- end
- end
- loopAdd = function()
- while true do
- local event = {os.pullEvent()}
- if event ~= curEvent then
- local a,b = os.pullEvent("self_event_check")
- if b == true then
- addEvent(event)
- end
- end
- curEvent = {}
- end
- end
- loopUse = function()
- while true do
- useEvent()
- os.queueEvent("queue_event")
- os.pullEvent("queue_event")
- end
- end
- doShell = function()
- dofile("rom/programs/shell")
- end
- if not fs.exists(evtPath) then
- local file = fs.open(evtPath,"w")
- file.close()
- end
- parallel.waitForAny(loopUse,loopAdd,doShell)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement