Advertisement
Alakazard12

Recording

Dec 15th, 2012
144
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 3.22 KB | None | 0 0
  1. local isRecording = false
  2. local data = {}
  3. local toggle = 61
  4. local tnat = {}
  5. local tArgs = {...}
  6. local lastt = os.clock()
  7. local bos = os.pullEvent
  8. os.pullEvent = os.pullEventRaw
  9.  
  10. for i,v in pairs(term) do
  11.     tnat[i] = v
  12. end
  13.  
  14. function term.write(txt)
  15.     if isRecording == true then
  16.         table.insert(data, {os.clock()-lastt, "write", txt})
  17.         lastt = os.clock()
  18.     end
  19.     tnat.write(txt)
  20. end
  21.  
  22. function term.setCursorPos(x, y)
  23.     if isRecording == true then
  24.         table.insert(data, {os.clock()-lastt, "setpos", x, y})
  25.         lastt = os.clock()
  26.     end
  27.     tnat.setCursorPos(x, y)
  28. end
  29.  
  30. function term.setCursorBlink(bool)
  31.     if isRecording == true then
  32.         table.insert(data, {os.clock()-lastt, "blink", bool})
  33.         lastt = os.clock()
  34.     end
  35.     tnat.setCursorBlink(bool)
  36. end
  37.  
  38. function term.clear()
  39.     if isRecording == true then
  40.         table.insert(data, {os.clock()-lastt, "clear"})
  41.         lastt = os.clock()
  42.     end
  43.     tnat.clear()
  44. end
  45.  
  46. function term.clearLine()
  47.     if isRecording == true then
  48.         table.insert(data, {os.clock()-lastt, "clearline"})
  49.         lastt = os.clock()
  50.     end
  51.     tnat.clearLine()
  52. end
  53.  
  54. function term.setTextColor(col)
  55.     if isRecording == true then
  56.         table.insert(data, {os.clock()-lastt, "setcol", col})
  57.         lastt = os.clock()
  58.     end
  59.     tnat.setTextColor(col)
  60. end
  61.  
  62. function term.scroll(num)
  63.     if isRecording == true then
  64.         table.insert(data, {os.clock()-lastt, "scroll", num})
  65.         lastt = os.clock()
  66.     end
  67.     tnat.scroll(num)
  68. end
  69.  
  70. function term.setBackgroundColor(col)
  71.     if isRecording == true then
  72.         table.insert(data, {os.clock()-lastt, "setbg", col})
  73.         lastt = os.clock()
  74.     end
  75.     tnat.setBackgroundColor(col)
  76. end
  77.  
  78. term.setTextColour = term.setTextColor
  79. term.setBackgroundColour = term.setBackgroundColor
  80.  
  81. if #tArgs == 0 then
  82.     print("Usage: srec <record/play> [path]")
  83.     return
  84. end
  85.  
  86. local function a1()
  87.     term.clear()
  88.     term.setCursorPos(1, 1)
  89.     shell.run("shell")
  90. end
  91.  
  92. local function a2()
  93.     while true do
  94.         local event, p1, p2 = os.pullEvent("key")
  95.         if p1 == toggle then
  96.             isRecording = false
  97.             term.setTextColor(colors.white)
  98.             print("What would you like to save recording as?")
  99.             local ty = fs.open("/"..read(), "w")
  100.             ty.write(textutils.serialize(data))
  101.             ty.close()
  102.             os.pullEvent = bos
  103.             break
  104.         end
  105.     end
  106. end
  107.  
  108. local function play(path)
  109.     path = shell.resolve(path)
  110.     if fs.exists(path) and not fs.isDir(path) then
  111.         term.clear()
  112.         term.setCursorPos(1, 1)
  113.         local fr = fs.open(path, "r")
  114.         local sdata = textutils.unserialize(fr.readAll())
  115.         fr.close()
  116.         for i,v in pairs(sdata) do
  117.  
  118.             if v[2] == "write" then
  119.                 term.write(v[3])
  120.             elseif v[2] == "setpos" then
  121.                 term.setCursorPos(v[3], v[4])
  122.             elseif v[2] == "blink" then
  123.                 term.setCursorBlink(v[3])
  124.             elseif v[2] == "clear" then
  125.                 term.clear()
  126.             elseif v[2] == "clearline" then
  127.                 term.clearLine()
  128.             elseif v[2] == "setcol" then
  129.                 term.setTextColor(v[3])
  130.             elseif v[2] == "setbg" then
  131.                 term.setBackgroundColor(v[3])
  132.             elseif v[2] == "scroll" then
  133.                 term.scroll(v[3])
  134.             end
  135.            
  136.             if v[1] > 0.00001 then
  137.                 sleep(v[1])
  138.             end
  139.         end
  140.     end
  141. end
  142.  
  143. if tArgs[1]:lower() == "record" then
  144.     isRecording = true
  145.     lastt = os.clock()
  146.     parallel.waitForAny(a1, a2)
  147. elseif tArgs[1]:lower() == "play" then
  148.     play(tArgs[2])
  149. end
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement