Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- if clearEventLog then
- print("Already initialised!")
- return false
- end
- local KeyToClearLog = 59
- local TasksBetweenSleeps = 50
- local stepsTaken = 0
- local isRestoring = fs.exists(".events")
- local fileInstance
- local waitingFor
- local function overwrite(originaltable,originalname, fake, funcType)
- originaltable[originalname] = function(...)
- if isRestoring then
- if not fileInstance then
- fileInstance = fs.open(".events","r")
- end
- stepsTaken = stepsTaken + 1
- if funcType == 1 and stepsTaken > TasksBetweenSleeps then
- stepsTaken = 0
- os.queueEvent("isomnia")
- fake("isomnia")
- end
- local fakeEvent = fileInstance.readLine()
- if not fakeEvent or #fakeEvent == 0 then
- isRestoring = false
- fileInstance.close()
- fileInstance = fs.open(".events","a")
- if waitingFor then
- if waitingFor[1] == "timer" then
- fileInstance.write(textutils.serialize(waitingFor))
- fileInstance.write("\n")
- return unpack(waitingFor)
- end
- end
- return originaltable[originalname](...) --recursive pull
- end
- local fakeTable = textutils.unserialize(fakeEvent)
- if fakeTable and fakeTable[1] then
- if fakeTable[1] == "terminate" then
- error( "Terminated", 0 )
- end
- if fakeTable[2] and fakeTable[2] == "timerStart" then
- waitingFor = {"timer",fakeTable[1]}
- return fakeTable[1]
- end
- if fakeTable[1] == "timer" then
- waitingFor = nil
- end
- end
- return unpack(fakeTable)
- else
- if not fileInstance then
- fileInstance = fs.open(".events","w")
- end
- local event = {fake(...)}
- if event ~= nil and event[1] ~= nil then
- if funcType == 2 and tonumber(event[1]) then
- event[2] = "timerStart"
- end
- fileInstance.write(textutils.serialize(event))
- fileInstance.write("\n")
- if #event==2 and event[1] == "key" and event[2] == KeyToClearLog then
- clearEventLog()
- end
- if event[1] == "terminate" then
- error( "Terminated", 0 )
- end
- end
- return unpack(event)
- end
- end
- end
- function clearEventLog(noOutput)
- fileInstance.close()
- fileInstance = nil
- fs.delete(".events")
- if not noOutput then
- local x,y = term.getCursorPos()
- write("Event Log deleted!")
- term.setCursorPos(x,y)
- end
- return true
- end
- local pull = os.pullEventRaw
- overwrite(os,"pullEvent",pull,1)
- local rand = math.random
- overwrite(math,"random",rand,0)
- local timer = os.startTimer
- overwrite(os,"startTimer",timer,2)
- local peri = peripheral.call
- overwrite(peripheral,"call",peri,0)
- local shutdown = os.shutdown
- os.shutdown = function()
- clearEventLog(true)
- shutdown()
- end
- local reboot = os.reboot
- os.reboot = function()
- clearEventLog(true)
- reboot()
- end
- if turtle then
- for name,func in pairs(turtle.native) do
- local privateFunc = func
- overwrite(turtle,name,privateFunc,3)
- end
- end
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement