billysback

EStack

Feb 1st, 2013
102
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 2.92 KB | None | 0 0
  1.  
  2. local event_stack = {}
  3. local output = ""
  4. local ticks = 0
  5. local block = false
  6.  
  7. local function getLines(dir)
  8.     if fs.exists(dir) then
  9.         local file = fs.open(dir, "r")
  10.         local lines = {}
  11.         local line = file.readLine()
  12.         while line ~= nil do
  13.             lines[#lines + 1] = line
  14.             line = file.readLine()
  15.         end
  16.         file.close()
  17.         return lines
  18.     else
  19.         return {}
  20.     end
  21. end
  22.  
  23. local function writeLines(dir, lines)
  24.     if #lines > 0 then
  25.         if fs.exists(dir) then fs.delete(dir) end
  26.         local file = fs.open(dir, "a")
  27.         local tick = 0
  28.         for i=1,#lines do
  29.             file.writeLine(lines[i])
  30.             tick = tick + 1
  31.             if tick > 100 then
  32.                 tick = 0
  33.                 sleep(0)
  34.             end
  35.         end
  36.         file.close()
  37.         return true
  38.     else
  39.         return false
  40.     end
  41. end
  42.  
  43. local function getFirst(line, char)
  44.     local index = -1
  45.     for i=1,string.len(line) do
  46.         local c = line:sub(i, i)
  47.         if c == char and index == -1 then index = i end
  48.     end
  49.     return index
  50. end
  51.  
  52. local function flushStack()
  53.     local lines = getLines(output)
  54.     for i=1,#event_stack do
  55.         local event = event_stack[i]
  56.         local ev = ""
  57.         for i=2,#event do
  58.             if ev == "" then ev = type(event[i])..","..event[i] else ev = ev..";"..type(event[i])..","..event[i] end
  59.         end
  60.         local nln = event[1]..";"..ev
  61.         if #lines > 0 then
  62.             local index = getFirst(lines[#lines], "@")
  63.             if index ~= -1 then
  64.                 if (lines[#lines]:sub(index+1, -1) == nln) then
  65.                     lines[#lines] = (tonumber(lines[#lines]:sub(1, index-1))+1).."@"..lines[#lines]:sub(index+1, -1)
  66.                 else
  67.                     lines[#lines + 1] = "1@"..nln
  68.                 end
  69.             else
  70.                 lines[#lines + 1] = "1@"..nln
  71.             end
  72.         else
  73.             lines[#lines + 1] = "1@"..nln
  74.         end
  75.     end
  76.     if writeLines(output, lines) then
  77.         event_stack = {}
  78.     end
  79. end
  80.  
  81.  
  82. old_eventPull = os.pullEvent
  83. new_eventPull = function(args)
  84.     local event, p1, p2, p3 = old_eventPull(args)
  85.     detectToggle(event, p1)
  86.     event_stack[#event_stack + 1] = {event, p1, p2, p3}
  87.     ticks = ticks + 1
  88.     if ticks > 100 then
  89.         ticks = 0
  90.         flushStack()
  91.     end
  92.     return event, p1, p2, p3
  93. end
  94.  
  95. old_eventRawPull = os.pullEventRaw
  96. new_eventRawPull = function()
  97.     local event, p1, p2, p3 = old_eventRawPull()
  98.     detectToggle(event, p1)
  99.     event_stack[#event_stack + 1] = {event, p1, p2, p3}
  100.     ticks = ticks + 1
  101.     if ticks > 100 then
  102.         ticks = 0
  103.         flushStack()
  104.     end
  105.     return event, p1, p2, p3
  106. end
  107.  
  108. local function revert()
  109.     flushStack()
  110.     os.pullEvent = old_eventPull
  111.     os.pullEventRaw = old_eventRawPull
  112.     event_stack = {}
  113.     ticks = 0
  114.     block = true
  115.     print("Ended recording, flushed to: "..output)
  116.     output = ""
  117. end
  118.  
  119. local function start()
  120.     if not block then
  121.         print("Output?")
  122.         output = read()
  123.         print("Target program?")
  124.         local pname = read()
  125.         print("F12 to toggle, loading program...")
  126.         sleep(1)
  127.         os.pullEvent = new_eventPull
  128.         os.pullEventRaw = new_eventRawPull
  129.         os.run({}, pname)
  130.     end
  131. end
  132.  
  133. function detectToggle(event, p1)
  134.     if event == "key" and p1 == 88 then
  135.         if output ~= "" then revert() else start() end
  136.     end
  137. end
  138.  
  139. start()
Advertisement
Add Comment
Please, Sign In to add comment