tikevin83

Bizhawk Cycle-Timestamp Inputs Lua Script

Apr 18th, 2018 (edited)
684
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. local coreName = movie.getheader()["Core"]
  2. local gameName = string.gsub(movie.getheader()["GameName"], "%s+", "")
  3. local powerOnCycles = 0
  4. local modeSwitchGBAToGBCDelay = 948.84375
  5. local inputlabels
  6. local powerButtonLabel
  7. local coreToGBAAudioSampleRatio
  8.  
  9. if coreName == "Gambatte" then
  10.   inputlabels = {"A", "B", "Select", "Start", "Right", "Left", "Up", "Down"}
  11.   powerButtonLabel = "Power"
  12.   coreToGBAAudioSampleRatio = 512
  13. elseif coreName == "GBHawk" or coreName == "SubGBHawk" then
  14.   inputlabels = {"P1 A", "P1 B", "P1 Select", "P1 Start", "P1 Right", "P1 Left", "P1 Up", "P1 Down"}
  15.   powerButtonLabel = "P1 Power"
  16.   coreToGBAAudioSampleRatio = 1024
  17. else
  18.   error("Unknown Emulator Core")
  19. end
  20.  
  21. function getinputbyte(t)
  22.   if not movie.isloaded() then
  23.     return 0
  24.   end
  25.  
  26.   local b = 0
  27.  
  28.   for i = 0, 7 do
  29.     if t[inputlabels[i + 1]] then
  30.       b = b + bit.lshift(1, i)
  31.     end
  32.   end
  33.  
  34.   return b
  35. end
  36.  
  37. function tryinput(file)
  38.   local t = movie.getinput(emu.framecount())
  39.   local input = getinputbyte(t)
  40.   local cycles = emu.totalexecutedcycles()
  41.   if t[powerButtonLabel] then
  42.     powerOnCycles = cycles
  43.   end
  44.  
  45.   if input ~= lastinput then
  46.       cycles = math.ceil(((cycles - powerOnCycles) / coreToGBAAudioSampleRatio) + modeSwitchGBAToGBCDelay)
  47.     file:write(string.format("%08X %04X", cycles, input))
  48.     file:write("\n")
  49.     lastinput = input
  50.   end
  51. end
  52.  
  53. local timestamps = io.open(gameName .. ".txt", "w")
  54. local lastinput = -1
  55.  
  56. tryinput(timestamps)
  57.  
  58. while emu.framecount() <= movie.length() do
  59.  
  60.     tryinput(timestamps)
  61.  
  62.   emu.frameadvance()
  63. end
  64.  
  65. tryinput(timestamps)
  66. timestamps:close()
  67.  
  68. client.pause()
Add Comment
Please, Sign In to add comment