Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- local screen = {}
- local screenW,screenH = term.getSize()
- function initScreen()
- screen = {}
- screen.data = {}
- for i=1,screenH,1 do
- screen.data[i] = string.rep(" ",screenW)
- end
- screen.cX = 1
- screen.cY = 1
- screen.blink = true
- screen.setCursorPos = function(x,y) screen.cX = x; screen.cY = y end
- screen.setCursorBlink = function(b) screen.blink = b end
- screen.write = function(txt) return screenWrite(screen,txt) end
- screen.clear = function() for i=1,screenH,1 do screen.data[i] = string.rep(" ",screenW) end end
- screen.clearLine = function() screen.data[screen.cY] = string.rep(" ",screenW) end
- screen.scroll = function(i)
- if i >= screenH then
- screen.clear()
- else
- for p=1,screenH-1,1 do
- if screen.data[p+i] ~= nil then
- screen.data[p] = screen.data[p+i]
- else
- screen.data[p] = string.rep(" ",screenW)
- end
- end
- end screen.setCursorPos(term.getCursorPos())
- end
- end
- function screenWrite(wScreen,txt)
- local screenLine = wScreen.data[wScreen.cY]
- local s = wScreen.cX
- local e = wScreen.cX + string.len(txt)-1
- local i = s
- local st = true
- while i <= wScreen.cX + string.len(txt)-1 do
- if st then
- --print( string.char(string.byte(screenLine,i)).."<=>"..string.char(string.byte(txt,i-wScreen.cX+1)) )
- if string.byte(screenLine,i) ~= string.byte(txt,i-wScreen.cX+1) then
- st = false
- s = i
- e = i
- end
- else
- if string.byte(screenLine,i) ~= string.byte(txt,i-wScreen.cX+1) then
- e = i
- end
- end
- i = i+1
- end
- local outStr = string.sub(txt,s-wScreen.cX+1,e-wScreen.cX+1)
- screenLine = string.sub(string.sub(screenLine,1,s-1)..outStr..( e+1 <= screenW and string.sub(screenLine,e+1) or ""),1,screenW)
- wScreen.data[wScreen.cY] = screenLine
- -- return screenStartPosX, screenEndPosX, stringStartPos, writtenString
- return s,e,s-wScreen.cX+1,outStr
- end
- local startClock = 0
- local oldClock = 0
- function getClock()
- return os.clock() - startClock
- end
- initScreen()
- local orgFuncs = {}
- local hookFuncs = {}
- hookFuncs.term = {}
- hookFuncs.os = {}
- function hookFuncs.os.pullEventRaw(yyield)
- local ev,p1,p2,p3,p4,p5,p6 = orgFuncs.os.pullEventRaw(yyield)
- if ev == "key" and p1 == 59 then
- endRec()
- term.setCursorPos(1,1)
- term.clearLine()
- term.write("Saved recording.")
- end
- return ev,p1,p2,p3,p4,p5,p6
- end
- local dataTypes = {
- [0] = {bitCount=8}, -- number
- [1] = {bitCount=1}, -- boolean
- [2] = {bitCount=8}, -- string
- [3] = {bitCount=8} -- character
- }
- local funcHooks = { }
- funcHooks.term = {"write", "scroll", "setCursorBlink", "setCursorPos", "clear", "clearLine"}
- local cmdIndex = {
- [1] = { name="string",func="",argBits={} },
- -- [2] = {name="string_end",func="",argBits={}},
- [3] = {name="char",func="",argBits={0}},
- [4] = {name="sleep",func="",argBits={}},
- [5] = {name="scroll",func="term.scroll", argBits = {0}},
- [6] = {name="clear",func="term.clear", argBits = {}},
- [7] = {name="clearLine",func="term.clearLine",argBits = {}},
- [8] = {name="setCursorPos",func="term.setCursorPos",argBits = {0,0}},
- [9] = {name="setCursorBlink",func="term.setCursorBlink",argBits = {1}},
- -- [10] = {name="subSleep",func="",argBits={}},
- -- [11] = {name="longSleep",func="",argBits={}},
- [15] = {name="end",func="",argBits={}}
- }
- local bitStreamBuffer = 0
- local bitIndex = 0x80
- local bitStartIndex = 0x80
- local b64 = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/"
- local b64String = ""
- local asciiString = ""
- function pushBase64(num)
- b64String = b64String .. string.sub(b64,num+1,num+1)
- end
- function pushAscii(num)
- asciiString = asciiString..string.char(num)
- end
- function endRec()
- unhook()
- pushBufferRemainder()
- local file = fs.open("recFile","wb")
- for i=1,string.len(asciiString),1 do
- file.write(string.byte(asciiString,i))
- end
- file.close()
- end
- local ff = fs.open("pplog","w")
- function closeLog() ff.close() end
- function pushBitsToStream(val,bits)
- --if type(val) ~= "number" then error("TABVAL") end
- --if type(bitStreamBuffer) ~= "number" then error("TABVAL1") end
- --if type(val) == "table" then error("TABVAL3") end
- local mask = bit.blshift(1, (bits-1))
- while mask > 0 do
- local maskVal = bit.brshift(bit.band(val,mask),bits-1)
- bitStreamBuffer = bit.bor(bitStreamBuffer,maskVal*bitIndex)
- mask = bit.brshift( mask, 1 )
- bits = bits - 1
- bitIndex = bit.brshift( bitIndex, 1 )
- if bitIndex == 0 then
- bitIndex = bitStartIndex
- pushAscii(bitStreamBuffer)
- bitStreamBuffer = 0
- end
- end
- end
- function pushBitsToNextByte()
- while bitIndex ~= bitStartIndex do pushBitsToStream(0,1) end
- end
- function pushBufferRemainder()
- pushBitsToStream(15,4)
- if bitStreamBuffer then
- pushAscii(bitStreamBuffer)
- end
- end
- function pushToStream(func,ret,...)
- local cmdI = 0
- for i,v in pairs(cmdIndex) do
- if v.func == func then
- cmdI = i
- break
- end
- end
- if cmdI ~= 0 then
- pushBitsToStream(cmdI,4)
- if #cmdIndex[cmdI].argBits > 0 then
- pushBitsToStream((#cmdIndex[cmdI].argBits),2)
- for i,v in ipairs(cmdIndex[cmdI].argBits) do
- local dType = v
- local bitCount = dataTypes[dType].bitCount
- pushBitsToStream(dType,2)
- if dType == 1 then arg[i] = arg[i] == true and 1 or 0 end
- pushBitsToStream(arg[i],bitCount)
- end
- end
- else
- if func == "term.write" then
- if string.len(arg[1]) == 1 then
- cmdI = 3
- pushBitsToStream(cmdI,4)
- pushBitsToStream(1,2)
- pushBitsToStream(3,2)
- pushBitsToNextByte()
- pushBitsToStream(string.byte(arg[1],1),dataTypes[3].bitCount)
- else
- cmdI = 1
- pushBitsToStream(cmdI,4)
- pushBitsToStream(1,2)
- pushBitsToNextByte()
- for i=1,string.len(arg[1]),1 do
- pushBitsToStream(string.byte(arg[1],i),dataTypes[2].bitCount)
- end
- pushBitsToStream(0,dataTypes[2].bitCount)
- --pushBitsToStream(2,4)
- end
- end
- end
- end
- function logCall(func,ret,...)
- local argstr = ""
- local dX,dY = nil,nil
- if string.sub(func,1,4) == "term" then
- local subFunc = string.sub(func,6)
- if screen[subFunc] and type(screen[subFunc]) == "function" then
- if subFunc == "write" then
- local subRet = {screen.write(unpack(arg))}
- dX,dY = term.getCursorPos()
- local nX = subRet[1]
- term.setCursorPos(nX,dY)
- arg[1] = subRet[4]
- else
- screen[subFunc](unpack(arg))
- end
- end
- end
- local cClock = getClock()
- if cClock ~= oldClock then
- local diff = cClock - oldClock
- oldClock = cClock
- local intC = math.floor(diff)
- local lowC = math.floor((diff - intC)*100)
- while intC > 0 do
- pushBitsToStream(4,4)
- local argC = 1
- if intC > 255 then argC = 3 else if intC > 0 then argC = 2 end end
- pushBitsToStream(argC,2)
- pushBitsToStream(0,2)
- pushBitsToStream(lowC,dataTypes[0].bitCount)
- if intC > 0 then
- if intC > 255 then
- intC = intC - 255
- pushBitsToStream(0,2)
- pushBitsToStream(255,dataTypes[0].bitCount)
- else
- pushBitsToStream(0,2)
- pushBitsToStream(intC,dataTypes[0].bitCount)
- end
- end
- intC = intC - (255*2)
- end
- end
- pushToStream(func,ret,unpack(arg))
- if dX ~= nil then term.setCursorPos(dX,dY) end
- end
- function genHooks(t)
- local tb = funcHooks[t] or funcHooks
- for k,v in pairs(tb) do
- if type(v) == "table" then
- genHooks(k)
- else
- if t then
- if hookFuncs[t] == nil then hookFuncs[t] = {} end
- hookFuncs[t][v] = function(...)
- local ret = orgFuncs[t][v](unpack(arg))
- logCall(t.."."..v,ret,unpack(arg))
- return ret
- end
- else
- hookFuncs[v] = function(...)
- local ret = orgFuncs[v](unpack(arg))
- logCall(v,ret,unpack(arg))
- return ret
- end
- end
- end
- end
- end
- genHooks()
- function hook(t)
- if t ~= nil then tb = hookFuncs[t] else tb = hookFuncs end
- for k,v in pairs(tb) do
- if type(v) == "table" then
- hook(k)
- else
- if t then
- if orgFuncs[t] == nil then orgFuncs[t] = {} end
- orgFuncs[t][k] = _G[t][k]
- _G[t][k] = v
- else
- orgFuncs[k] = _G[k]
- _G[k] = v
- end
- end
- end
- startClock = os.clock()
- end
- function unhook(t)
- if t ~= nil then tb = orgFuncs[t] else tb = orgFuncs end
- for k,v in pairs(tb) do
- if type(v) == "table" then
- unhook(k)
- else
- if t then
- _G[t][k] = v
- else
- _G[k] = v
- end
- end
- end
- end
- print("Recording... Press Enter to start.")
- while true do
- local ev,p1 = os.pullEvent("key")
- if ev == "key" and p1 == 28 then break end
- end
- term.clear()
- term.setCursorPos(1,1)
- hook()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement