Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- local event_stack = {}
- local output = ""
- local ticks = 0
- local block = false
- local function getLines(dir)
- if fs.exists(dir) then
- local file = fs.open(dir, "r")
- local lines = {}
- local line = file.readLine()
- while line ~= nil do
- lines[#lines + 1] = line
- line = file.readLine()
- end
- file.close()
- return lines
- else
- return {}
- end
- end
- local function writeLines(dir, lines)
- if #lines > 0 then
- if fs.exists(dir) then fs.delete(dir) end
- local file = fs.open(dir, "a")
- local tick = 0
- for i=1,#lines do
- file.writeLine(lines[i])
- tick = tick + 1
- if tick > 100 then
- tick = 0
- sleep(0)
- end
- end
- file.close()
- return true
- else
- return false
- end
- end
- local function getFirst(line, char)
- local index = -1
- for i=1,string.len(line) do
- local c = line:sub(i, i)
- if c == char and index == -1 then index = i end
- end
- return index
- end
- local function flushStack()
- local lines = getLines(output)
- for i=1,#event_stack do
- local event = event_stack[i]
- local ev = ""
- for i=2,#event do
- if ev == "" then ev = type(event[i])..","..event[i] else ev = ev..";"..type(event[i])..","..event[i] end
- end
- local nln = event[1]..";"..ev
- if #lines > 0 then
- local index = getFirst(lines[#lines], "@")
- if index ~= -1 then
- if (lines[#lines]:sub(index+1, -1) == nln) then
- lines[#lines] = (tonumber(lines[#lines]:sub(1, index-1))+1).."@"..lines[#lines]:sub(index+1, -1)
- else
- lines[#lines + 1] = "1@"..nln
- end
- else
- lines[#lines + 1] = "1@"..nln
- end
- else
- lines[#lines + 1] = "1@"..nln
- end
- end
- if writeLines(output, lines) then
- event_stack = {}
- end
- end
- old_eventPull = os.pullEvent
- new_eventPull = function(args)
- local event, p1, p2, p3 = old_eventPull(args)
- detectToggle(event, p1)
- event_stack[#event_stack + 1] = {event, p1, p2, p3}
- ticks = ticks + 1
- if ticks > 100 then
- ticks = 0
- flushStack()
- end
- return event, p1, p2, p3
- end
- old_eventRawPull = os.pullEventRaw
- new_eventRawPull = function()
- local event, p1, p2, p3 = old_eventRawPull()
- detectToggle(event, p1)
- event_stack[#event_stack + 1] = {event, p1, p2, p3}
- ticks = ticks + 1
- if ticks > 100 then
- ticks = 0
- flushStack()
- end
- return event, p1, p2, p3
- end
- local function revert()
- flushStack()
- os.pullEvent = old_eventPull
- os.pullEventRaw = old_eventRawPull
- event_stack = {}
- ticks = 0
- block = true
- print("Ended recording, flushed to: "..output)
- output = ""
- end
- local function start()
- if not block then
- print("Output?")
- output = read()
- print("Target program?")
- local pname = read()
- print("F12 to toggle, loading program...")
- sleep(1)
- os.pullEvent = new_eventPull
- os.pullEventRaw = new_eventRawPull
- os.run({}, pname)
- end
- end
- function detectToggle(event, p1)
- if event == "key" and p1 == 88 then
- if output ~= "" then revert() else start() end
- end
- end
- start()
Advertisement
Add Comment
Please, Sign In to add comment