Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
27 changes: 25 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
@@ -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.
52 changes: 48 additions & 4 deletions hook.lua
Original file line number Diff line number Diff line change
Expand Up @@ -83,22 +83,31 @@ qtype = {
INPUT = 0;
READ = 1;
WRITE = 2;
CLIENT = 3
CLIENT = 3;
}

-- Cient command types
ctype = {
ADVANCE = 0;
SAVE = 1;
LOAD = 2;
}

-- Response codes
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

Expand All @@ -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)
Expand All @@ -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,
Expand Down