-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathhook.lua
More file actions
265 lines (218 loc) · 7.35 KB
/
hook.lua
File metadata and controls
265 lines (218 loc) · 7.35 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
---------------------------------------------------------
-- The Lua side of things would not have been possible --
-- if it weren't for the help of one lovely individual --
-- --
-- Tumn --
-- * Matrix: @autumn:raincloud.dev --
-- * GitHub: github.com/rosemash --
---------------------------------------------------------
address, port = "127.0.0.1", 16154
---------------------------------------------------------
-- # HOW TO USE --
-- --
-- Requests can be made by connecting to the socket --
-- at the specified address and port and sending --
-- a message following the pattern below. --
-- --
-- OBS! Every request requires a new connection. --
-- --
-- --
-- Pattern: DOMN/ADDR/TSLE/VLUE --
-- --
-- DOMN - Memory domain --
-- ADDR - Address --
-- TSLE--.-------------------. --
-- Type: Signage: --
-- | * [b]yte * [u]nsigned --
-- | * [i]nteger * [s]igned --
-- | * [f]loat --
-- | --
-- .-------------------. --
-- Size: Endianness: --
-- * [1] byte * [l]ittle endian --
-- * [2] bytes * [b]ig endian --
-- * [3] bytes --
-- * [4] bytes --
-- --
-- VLUE - Integer or float, ex. 12 or -1.2 --
-- --
-- --
-- # EXAMPLES --
-- --
-- VRAM/11423051/iu4b/23 --
-- Write (23) to (11423051) in (VRAM) --
-- an [i]nteger, [u]nsigned, --
-- [4] bytes long and [b]ig endian --
-- --
-- WRAM/21512962/fs2l/ --
-- Read from (21512962) in (WRAM) --
-- a [f]loat, [s]igned, --
-- [2] bytes long and [l]ittle endian --
-- --
---------------------------------------------------------
-- Include necessary socket modules using a hack
local version = _VERSION:match("%d+%.%d+")
package.path = 'lua_modules/share/lua/'
.. version
.. '/?.lua;lua_modules/share/lua/'
.. version
.. '/?/init.lua;'
.. package.path
package.cpath = 'lua_modules/lib/lua/'
.. version
.. '/?.so;'
.. package.cpath
-- Import modules and set up socket connection
socket = require("socket")
copas = require("copas")
server = socket.bind(address, port)
-- Query types
qtype = {
INPUT = 0;
READ = 1;
WRITE = 2;
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
CLIENT = 3; -- Successfully controlled client
ERROR = 4; -- Generic error
}
button_table = {}
function format_response(code, message)
-- emu.frameadvance()
-- Format response code and message into a valid response
-- console.log(code, "_", message)
return tostring(code) .. '_' .. tostring(message)
end
local function handleRequest(data)
-- Handle incoming requests for reading from
-- and writing to memory with the BizHawk emulator
form = tonumber(string.match(data, "(%d)%/.+"))
if form == qtype["INPUT"] then
-- TODO: make a proper lua table
query_type, button_name, button_state = string.match(data, "(%d)%/(.+)%/(.+)%/")
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)
-- Use default domain if none is provided
if domain == "" then
domain = nil
end
-- [ INPUT ]
if form == qtype["INPUT"] then
return format_response(
rcodes.INPUT,
joypad.set(button_table)
)
end
-- [ READ ]
if form == qtype["READ"] then
-- [ BYTE ]
return format_response(
rcodes.BYTE,
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,
-- let the client know that something's gone wrong
return format_response(rcodes.ERROR, 'INVALID_REQUEST')
end
local function clientHandler(client)
-- Reads data until client is disconnected
-- and processes the data with handleRequest
local data = ""
-- --
while true do
-- Read 1 byte at a time
chunk, errmsg = client:receive(1)
-- Quit reading if an error has occured
-- or no data was received
if not chunk then
if errmsg == 'timeout' then
break
end
print(('Socket error: %s'):format(errmsg))
return
end
-- Append new byte to data
data = data .. chunk
end
if not data then return end
-- Handle the request
-- and formulate a response
response = handleRequest(data)
if not response then return end
-- Make sure response is string
-- and send back response to client
client:send(tostring(response))
end
console.log("Adding server")
copas.addserver(server, clientHandler)
console.log("Added server")
-- Open up socket with a clear sign
while emu.framecount() < 120 do
gui.text(20, 20, '.Opening socket at ' .. address .. ':' .. port)
emu.frameadvance()
end
console.log("Done showing intro")
-- I spent days trying to track down the origin of a mysterious error in copas
-- I've given up and just overriden its error handler as it kills performance
copas.setErrorHandler("", "")
console.log("Disabled COPAS error handler")
while true do
-- Communicate with client
local handled, errmsg = copas.step(0)
if handled == nil then
print(('Socket error: %s'):format(errmsg))
end
-- Advance the game by a frame
-- emu.yield()
end