diff --git a/README.md b/README.md index 9a1afe9..d3733c3 100644 --- a/README.md +++ b/README.md @@ -1,2 +1,25 @@ -# lua-components -The actual lua contents that Bizhook unzips +# Lua Socket Server and API for Bizhawk + +The Lua socket server usually extracted by Bizhook for use with [AutoHawk](https://github.com/SuperBlueHenBros/middle-tier) in via [AutoHawk-API-Client](https://github.com/SuperBlueHenBros/Bizhook). + +## Information + +hook.lua is a based on OpenDisrupt's Bizhook library but is not compatible with the original API located on [GitLab](https://gitlab.com/OpenDisrupt/bizhook). Please don't bother them regarding any issues encountered when using this fork. This implementation has been completely reworked to support manual frame advancement and automated input as supported in [our fork of the Bizhook API](https://github.com/SuperBlueHenBros/Bizhook). Full credit to [Maximillian Strand](https://gitlab.com/deepadmax) and [Autumn](https://github.com/rosemash/luape) for getting this originally working. + +## Usage + +### Manual Server Start-Up + +#### Opening socket + +In Bizhawk, go to `Tools` > `Lua Console`. Select `Open script` and open `hook.lua` from the exported components. + +##### Is it working? + +If it starts successfully, you should see output in Bizhawk's Lua console stating it is running. + +**Note**: Do not try to communicate with the socket *before* the text has disappeared, as it isn't actually opened yet. The message is there to make it clear that the script is running successfully. + +### Automatic Server Start-Up + +Automatically launched with Bizhawk via [AutoHawk](https://github.com/SuperBlueHenBros/middle-tier) once configured. \ No newline at end of file diff --git a/hook.lua b/hook.lua index 1bdf4ae..8e8a010 100644 --- a/hook.lua +++ b/hook.lua @@ -83,7 +83,14 @@ qtype = { INPUT = 0; READ = 1; WRITE = 2; - CLIENT = 3 + CLIENT = 3; +} + +-- Cient command types +ctype = { + ADVANCE = 0; + SAVE = 1; + LOAD = 2; } -- Response codes @@ -91,14 +98,16 @@ rcodes = { INPUT = 0; -- Successfully wrote to memory BYTE = 1; -- Successfully read byte INTEGER = 2; -- Successfully read integer - FLOAT = 3; -- Successfully read float + CLIENT = 3; -- Successfully controlled client ERROR = 4; -- Generic error } +button_table = {} function format_response(code, message) - emu.frameadvance() + -- emu.frameadvance() -- Format response code and message into a valid response + -- console.log(code, "_", message) return tostring(code) .. '_' .. tostring(message) end @@ -109,13 +118,18 @@ local function handleRequest(data) if form == qtype["INPUT"] then -- TODO: make a proper lua table query_type, button_name, button_state = string.match(data, "(%d)%/(.+)%/(.+)%/") - button_table = {} button_table[button_name] = button_state -- console.log("Sending Input:") -- console.log(button_table) elseif form == qtype["READ"] then query_type, domain, mem_address = string.match(data, "(%d)%/(.+)%/(.+)%/") mem_address = tonumber(mem_address) + elseif form == qtype["CLIENT"] then + query_type, client_type = string.match(data, "(%d)%/(%d)%/") + client_type = tonumber(client_type) + if client_type == ctype["ADVANCE"] then + frames = string.match(data, "%d%/%d%/(%d)") + end end query_type = tonumber(query_type) @@ -141,6 +155,36 @@ local function handleRequest(data) memory.readbyte(mem_address, domain) ) end + -- [ CLIENT ] + if form == qtype["CLIENT"] then + -- [ FRAME ADVANCE ] + if client_type == ctype["ADVANCE"] then + repeat + emu.frameadvance() + frames = frames - 1 + joypad.set(button_table) + until (frame <= 0) + + return format_response( + rcodes.CLIENT, + true + ) + elseif client_type == ctype["SAVE"] then + savestate.saveslot(0) + + return format_response( + rcodes.CLIENT, + true + ) + elseif client_type == ctype["LOAD"] then + savestate.loadslot(0) + + return format_response( + rcodes.CLIENT, + true + ) + end + end -- If nothing is matched,