User File #638406821710992453

Upload All User Files

#638406821710992453 - script for use with Bizhawk to dump to the .r08 format

r08-bizhawk.lua
32 downloads
Uploaded 1/12/2024 6:49 PM by feos (see all 184)

local handle = nil

local wasMovieLoaded = false
local readyToDump = false

local writtenFrames = 0

local playerKeys = {
    {"P1 Right", "P1 Left", "P1 Down", "P1 Up", "P1 Start", "P1 Select", "P1 B", "P1 A"},
    {"P2 Right", "P2 Left", "P2 Down", "P2 Up", "P2 Start", "P2 Select", "P2 B", "P2 A"}
}

--local inspect = require("inspect")
local char = string.char

console.clear()

function getDumpFilename()
    local _, _, path, filename, ext = string.find(movie.filename(), "(.-)([^\\/]-%.?)([^%.\\/]*)$")
    return path..filename.."r08"
end

function writeFrame()
    local input = movie.getinput(emu.framecount() - 1)
    
    local data = {0xFF, 0xFF}
    
    for i = 1, 2 do
        for k = 7, 0, -1 do
            local key = playerKeys[i][k + 1]
            if input[key] == true then
                data[i] = bit.clear(data[i], k)
            end
        end
        handle:write(char(bit.bxor(data[i], 0xFF)))
    end
end

while not movie.isloaded() do
    emu.yield()
end

while true do
    if movie.isloaded() and not wasMovieLoaded then
        if emu.framecount() == 0 then
            wasMovieLoaded = true
            local filename = getDumpFilename()
            handle = io.open(filename, "wb+")
            
            if handle == nil then
                print("Error opening dump file!")
                break
            else
                handle:flush()
            end
            
            print("Dumping has started...")
            client.unpause()
            readyToDump = true
            writtenFrames = 0
        elseif emu.framecount() > 0 then
            client.pause()
            print("Sorry! You cannot activate/start this script after the first frame of a movie!")
            print("Use: File > Movie > Play from Beginning")
            print("Then while the movie is still paused, activate this script again.")
            break
        end
    elseif not movie.isloaded() then
        wasMovieLoaded = false
        readyToDump = false
    end
    
    
    if readyToDump then
        if not emu.islagged() and emu.framecount() <= movie.length() and emu.framecount() > 0 then
            writeFrame()
            handle:flush()
            writtenFrames = writtenFrames + 1
        end
        
        if emu.framecount() <= movie.length() and emu.framecount() > 0 then
            local input = movie.getinput(emu.framecount() - 1)
            if input["Reset"] == true then
                print("Reset occured on movie frame: "..(emu.framecount() - 1)..", dump frame: "..writtenFrames)
            elseif input["Power"] == true then
                print("Power-reset occured on movie frame: "..(emu.framecount() - 1)..", dump frame: "..writtenFrames)
            end
        end
    end
    
    
    if movie.mode() == "FINISHED" then
        wasMovieLoaded = false
        readyToDump = false
        finalFrame = false
        client.pause()
        movie.stop()
        handle:close()
        print("Movie dump complete!")
    end
    
    emu.frameadvance()
end