BizHawkClient: Use callbacks in connector script instead of else/ifs (#2784)
This commit is contained in:
parent
539307cf0b
commit
687af30d14
|
@ -22,6 +22,10 @@ SOFTWARE.
|
||||||
|
|
||||||
local SCRIPT_VERSION = 1
|
local SCRIPT_VERSION = 1
|
||||||
|
|
||||||
|
-- Set to log incoming requests
|
||||||
|
-- Will cause lag due to large console output
|
||||||
|
local DEBUG = false
|
||||||
|
|
||||||
--[[
|
--[[
|
||||||
This script expects to receive JSON and will send JSON back. A message should
|
This script expects to receive JSON and will send JSON back. A message should
|
||||||
be a list of 1 or more requests which will be executed in order. Each request
|
be a list of 1 or more requests which will be executed in order. Each request
|
||||||
|
@ -271,10 +275,6 @@ local base64 = require("base64")
|
||||||
local socket = require("socket")
|
local socket = require("socket")
|
||||||
local json = require("json")
|
local json = require("json")
|
||||||
|
|
||||||
-- Set to log incoming requests
|
|
||||||
-- Will cause lag due to large console output
|
|
||||||
local DEBUG = false
|
|
||||||
|
|
||||||
local SOCKET_PORT_FIRST = 43055
|
local SOCKET_PORT_FIRST = 43055
|
||||||
local SOCKET_PORT_RANGE_SIZE = 5
|
local SOCKET_PORT_RANGE_SIZE = 5
|
||||||
local SOCKET_PORT_LAST = SOCKET_PORT_FIRST + SOCKET_PORT_RANGE_SIZE
|
local SOCKET_PORT_LAST = SOCKET_PORT_FIRST + SOCKET_PORT_RANGE_SIZE
|
||||||
|
@ -330,18 +330,28 @@ function unlock ()
|
||||||
client_socket:settimeout(0)
|
client_socket:settimeout(0)
|
||||||
end
|
end
|
||||||
|
|
||||||
function process_request (req)
|
request_handlers = {
|
||||||
local res = {}
|
["PING"] = function (req)
|
||||||
|
local res = {}
|
||||||
|
|
||||||
if req["type"] == "PING" then
|
|
||||||
res["type"] = "PONG"
|
res["type"] = "PONG"
|
||||||
|
|
||||||
elseif req["type"] == "SYSTEM" then
|
return res
|
||||||
|
end,
|
||||||
|
|
||||||
|
["SYSTEM"] = function (req)
|
||||||
|
local res = {}
|
||||||
|
|
||||||
res["type"] = "SYSTEM_RESPONSE"
|
res["type"] = "SYSTEM_RESPONSE"
|
||||||
res["value"] = emu.getsystemid()
|
res["value"] = emu.getsystemid()
|
||||||
|
|
||||||
elseif req["type"] == "PREFERRED_CORES" then
|
return res
|
||||||
|
end,
|
||||||
|
|
||||||
|
["PREFERRED_CORES"] = function (req)
|
||||||
|
local res = {}
|
||||||
local preferred_cores = client.getconfig().PreferredCores
|
local preferred_cores = client.getconfig().PreferredCores
|
||||||
|
|
||||||
res["type"] = "PREFERRED_CORES_RESPONSE"
|
res["type"] = "PREFERRED_CORES_RESPONSE"
|
||||||
res["value"] = {}
|
res["value"] = {}
|
||||||
res["value"]["NES"] = preferred_cores.NES
|
res["value"]["NES"] = preferred_cores.NES
|
||||||
|
@ -354,14 +364,21 @@ function process_request (req)
|
||||||
res["value"]["PCECD"] = preferred_cores.PCECD
|
res["value"]["PCECD"] = preferred_cores.PCECD
|
||||||
res["value"]["SGX"] = preferred_cores.SGX
|
res["value"]["SGX"] = preferred_cores.SGX
|
||||||
|
|
||||||
elseif req["type"] == "HASH" then
|
return res
|
||||||
|
end,
|
||||||
|
|
||||||
|
["HASH"] = function (req)
|
||||||
|
local res = {}
|
||||||
|
|
||||||
res["type"] = "HASH_RESPONSE"
|
res["type"] = "HASH_RESPONSE"
|
||||||
res["value"] = rom_hash
|
res["value"] = rom_hash
|
||||||
|
|
||||||
elseif req["type"] == "GUARD" then
|
return res
|
||||||
res["type"] = "GUARD_RESPONSE"
|
end,
|
||||||
local expected_data = base64.decode(req["expected_data"])
|
|
||||||
|
|
||||||
|
["GUARD"] = function (req)
|
||||||
|
local res = {}
|
||||||
|
local expected_data = base64.decode(req["expected_data"])
|
||||||
local actual_data = memory.read_bytes_as_array(req["address"], #expected_data, req["domain"])
|
local actual_data = memory.read_bytes_as_array(req["address"], #expected_data, req["domain"])
|
||||||
|
|
||||||
local data_is_validated = true
|
local data_is_validated = true
|
||||||
|
@ -372,39 +389,83 @@ function process_request (req)
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
|
res["type"] = "GUARD_RESPONSE"
|
||||||
res["value"] = data_is_validated
|
res["value"] = data_is_validated
|
||||||
res["address"] = req["address"]
|
res["address"] = req["address"]
|
||||||
|
|
||||||
elseif req["type"] == "LOCK" then
|
return res
|
||||||
|
end,
|
||||||
|
|
||||||
|
["LOCK"] = function (req)
|
||||||
|
local res = {}
|
||||||
|
|
||||||
res["type"] = "LOCKED"
|
res["type"] = "LOCKED"
|
||||||
lock()
|
lock()
|
||||||
|
|
||||||
elseif req["type"] == "UNLOCK" then
|
return res
|
||||||
|
end,
|
||||||
|
|
||||||
|
["UNLOCK"] = function (req)
|
||||||
|
local res = {}
|
||||||
|
|
||||||
res["type"] = "UNLOCKED"
|
res["type"] = "UNLOCKED"
|
||||||
unlock()
|
unlock()
|
||||||
|
|
||||||
elseif req["type"] == "READ" then
|
return res
|
||||||
|
end,
|
||||||
|
|
||||||
|
["READ"] = function (req)
|
||||||
|
local res = {}
|
||||||
|
|
||||||
res["type"] = "READ_RESPONSE"
|
res["type"] = "READ_RESPONSE"
|
||||||
res["value"] = base64.encode(memory.read_bytes_as_array(req["address"], req["size"], req["domain"]))
|
res["value"] = base64.encode(memory.read_bytes_as_array(req["address"], req["size"], req["domain"]))
|
||||||
|
|
||||||
elseif req["type"] == "WRITE" then
|
return res
|
||||||
|
end,
|
||||||
|
|
||||||
|
["WRITE"] = function (req)
|
||||||
|
local res = {}
|
||||||
|
|
||||||
res["type"] = "WRITE_RESPONSE"
|
res["type"] = "WRITE_RESPONSE"
|
||||||
memory.write_bytes_as_array(req["address"], base64.decode(req["value"]), req["domain"])
|
memory.write_bytes_as_array(req["address"], base64.decode(req["value"]), req["domain"])
|
||||||
|
|
||||||
elseif req["type"] == "DISPLAY_MESSAGE" then
|
return res
|
||||||
|
end,
|
||||||
|
|
||||||
|
["DISPLAY_MESSAGE"] = function (req)
|
||||||
|
local res = {}
|
||||||
|
|
||||||
res["type"] = "DISPLAY_MESSAGE_RESPONSE"
|
res["type"] = "DISPLAY_MESSAGE_RESPONSE"
|
||||||
message_queue:push(req["message"])
|
message_queue:push(req["message"])
|
||||||
|
|
||||||
elseif req["type"] == "SET_MESSAGE_INTERVAL" then
|
return res
|
||||||
|
end,
|
||||||
|
|
||||||
|
["SET_MESSAGE_INTERVAL"] = function (req)
|
||||||
|
local res = {}
|
||||||
|
|
||||||
res["type"] = "SET_MESSAGE_INTERVAL_RESPONSE"
|
res["type"] = "SET_MESSAGE_INTERVAL_RESPONSE"
|
||||||
message_interval = req["value"]
|
message_interval = req["value"]
|
||||||
|
|
||||||
else
|
return res
|
||||||
|
end,
|
||||||
|
|
||||||
|
["default"] = function (req)
|
||||||
|
local res = {}
|
||||||
|
|
||||||
res["type"] = "ERROR"
|
res["type"] = "ERROR"
|
||||||
res["err"] = "Unknown command: "..req["type"]
|
res["err"] = "Unknown command: "..req["type"]
|
||||||
end
|
|
||||||
|
|
||||||
return res
|
return res
|
||||||
|
end,
|
||||||
|
}
|
||||||
|
|
||||||
|
function process_request (req)
|
||||||
|
if request_handlers[req["type"]] then
|
||||||
|
return request_handlers[req["type"]](req)
|
||||||
|
else
|
||||||
|
return request_handlers["default"](req)
|
||||||
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
-- Receive data from AP client and send message back
|
-- Receive data from AP client and send message back
|
||||||
|
|
Loading…
Reference in New Issue