-
Notifications
You must be signed in to change notification settings - Fork 2
/
fceux_listener.lua
286 lines (246 loc) · 9.16 KB
/
fceux_listener.lua
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
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
local socket = require("socket.core")
local json = require("json")
function connect(address, port, laddress, lport)
local sock, err = socket.tcp()
if not sock then return nil, err end
if laddress then
local res, err = sock:bind(laddress, lport, -1)
if not res then return nil, err end
end
local res, err = sock:connect(address, port)
if not res then return nil, err end
return sock
end
function bind(host, port, backlog)
local sock, err = socket.tcp()
if not sock then return nil, err end
sock:setoption("reuseaddr", true)
local res, err = sock:bind(host, port)
if not res then return nil, err end
res, err = sock:listen(backlog)
if not res then return nil, err end
return sock
end
--sock, err = bind("127.0.0.1", 80, -1)
--print(sock, err)
sock2, err2 = connect("127.0.0.1", 81)
sock2:settimeout(0)
print("Connected", sock2, err2)
function parseEndCallbackCommand(cmd, callbackName)
if cmd[1] == callbackName then
return true
end
return false
end
function waitUntilCallbackFinished(callbackName)
--print("waitToEndCallback", callbackName)
while true do
local message, err, part = sock2:receive("*all")
if not message then
message = part
end
if message and string.len(message)>0 then
--print(message)
local recCommand = json.decode(message)
if parseEndCallbackCommand(recCommand, callbackName) then
return
else
--!!! if this is other command, not we are waiting, just send it to global parseCommand coroutine
table.insert(commandsQueue, recCommand)
coroutine.resume(parseCommandCoroutine)
end
end
end
end
function emu_registerbeforewrapper(hfunc)
sendToHost({"emu.registerbefore_callback", hfunc})
waitUntilCallbackFinished("emu.registerbefore_callback_finished")
end
function emu_registerbefore(hfunc)
if hfunc == 0 then
emu.registerbefore(nil)
return
end
emu.registerbefore(function() emu_registerbeforewrapper(hfunc) end)
end
function emu_registerafterwrapper(hfunc)
sendToHost({"emu.registerafter_callback", hfunc})
waitUntilCallbackFinished("emu.registerafter_callback_finished")
end
function emu_registerafter(hfunc)
if hfunc == 0 then
emu.registerafter(nil)
return
end
emu.registerafter(function() emu_registerafterwrapper(hfunc) end)
end
function memory_registerexecutewrapper(hfunc, addr, size)
sendToHost({"memory.registerexecute_callback", hfunc, addr, size})
waitUntilCallbackFinished("memory.registerexecute_callback_finished")
end
function memory_registerexecute(addr, size, hfunc)
if hfunc == 0 then
memory.registerexecute(addr, size, nil)
return
end
memory.registerexecute(addr, size, function(addr, size) memory_registerexecutewrapper(hfunc, addr, size) end)
end
function memory_registerwritewrapper(hfunc, addr, size)
sendToHost({"memory.registerwrite_callback", hfunc, addr, size})
waitUntilCallbackFinished("memory.registerwrite_callback_finished")
end
function memory_registerwrite(addr, size, hfunc)
if hfunc == 0 then
memory.memory_registerwrite(addr, size, nil)
return
end
memory.registerwrite(addr, size, function(addr, size) memory_registerwritewrapper(hfunc, addr, size) end)
end
function sendToHost(cmd)
local command = json.encode(cmd)
sock2:send("json"..command) --json prefix for easy splitting group of commands
end
commandTable = {}
methodsNoArgs = {
["emu.poweron"] = emu.poweron,
["emu.pause"] = emu.pause,
["emu.unpause"] = emu.unpause,
["emu.message"] = emu.message,
["emu.softreset"] = emu.softreset,
["emu.speedmode"] = emu.speedmode,
--["emu.frameadvance"] = emu.frameadvance, --we can't call it via socket anyway
["emu.setrenderplanes"] = emu.setrenderplanes,
["emu.framecount"] = emu.framecount,
["emu.lagcount"] = emu.lagcount,
["emu.lagged"] = emu.lagged,
["emu.setlagflag"] = emu.setlagflag,
["emu.paused"] = emu.paused,
["emu.emulating"] = emu.emulating,
["emu.readonly"] = emu.readonly,
["emu.setreadonly"] = emu.setreadonly,
["emu.getdir"] = emu.getdir,
["emu.loadrom"] = emu.loadrom,
["emu.registerafter"] = emu_registerafter,
["emu.registerbefore"] = emu_registerbefore,
["emu.addgamegenie"] = emu.addgamegenie,
["emu.delgamegenie"] = emu.delgamegenie,
["emu.print"] = emu.print,
--["emu.getscreenpixel"] = emu.getscreenpixel, --add this manually
["rom.readbyte"] = rom.readbyte,
["rom.readbytesigned"] = rom.readbytesigned,
["rom.writebyte"] = rom.writebyte,
["memory.readbyte"] = memory.readbyte,
["memory.readbytesigned"] = memory.readbytesigned,
["memory.readword"] = memory.readword,
["memory.readwordsigned"] = memory.readwordsigned,
--["memory.readbyterange"] = memory.readbyterange, --need to encode string to byte values
["memory.writebyte"] = memory.writebyte,
["memory.registerexecute"] = memory_registerexecute,
["memory.registerwrite"] = memory_registerwrite,
["memory.getregister"] = memory.getregister,
["memory.setregister"] = memory.setregister,
["joypad.read"] = joypad.read,
["joypad.readimmediate"] = joypad.readimmediate,
["joypad.readdown"] = joypad.readdown,
["joypad.readup"] = joypad.readup,
["joypad.write"] = joypad.write,
["zapper.read"] = zapper.read,
["input.read"] = input.read,
["sound.get"] = sound.get,
["gui.pixel"] = gui.pixel,
["gui.getpixel"] = gui.getpixel,
["gui.line"] = gui.line,
["gui.box"] = gui.box,
["gui.text"] = gui.text,
--["gui.parsecolor"] = gui.parsecolor,
["gui.savescreenshot"] = gui.savescreenshot,
["gui.savescreenshotas"] = gui.savescreenshotas,
--["gui.gdscreenshot"] = gui.gdscreenshot,
--[gui.gdoverlay] = gui.gdoverlay,
["gui.opacity"] = gui.opacity,
["gui.transparency"] = gui.transparency,
--["gui.popup"] = gui.popup
["debugger.hitbreakpoint"] = debugger.hitbreakpoint,
["debugger.getcyclescount"] = debugger.getcyclescount,
["debugger.getinstructionscount"] = debugger.getinstructionscount,
["debugger.resetcyclescount"] = debugger.resetcyclescount,
["debugger.resetinstructionscount"] = debugger.resetinstructionscount,
["movie.active"] = movie.active,
["movie.mode"] = movie.mode,
["movie.rerecordcounting"] = movie.rerecordcounting,
["movie.stop"] = movie.stop,
["movie.length"] = movie.length,
["movie.name"] = movie.name,
["movie.getfilename"] = movie.getfilename,
["movie.rerecordcount"] = movie.rerecordcount,
["movie.replay"] = movie.replay,
["movie.readonly"] = movie.readonly,
["movie.setreadonly"] = movie.setreadonly,
["movie.recording"] = movie.recording,
["movie.playing"] = movie.ispoweron,
["movie.isfromsavestate"] = movie.isfromsavestate,
}
--for avoiding copypaste, really we can call methods by loadstring(methodName), by it's slower
for methodName, method in pairs(methodsNoArgs) do
commandTable[methodName] = function(currentCmd)
--print("cmd:", currentCmd)
table.remove(currentCmd, 1) -- now currentCmd is argument list
local result = method(unpack(currentCmd))
--print("result:", result)
sendToHost({methodName.."_finished", result})
end
end
--special case for method that return several values
--emu.getscreenpixel
commandTable["emu.getscreenpixel"] = function(currentCmd)
--print("cmd:", currentCmd)
table.remove(currentCmd, 1) -- now currentCmd is argument list
local r,g,b,pal = emu.getscreenpixel(unpack(currentCmd))
--print("result:", {r,g,b,pal})
sendToHost({"emu.getscreenpixel".."_finished", {r,g,b,pal}})
end
--special case for method that return string, need to reencoding it to array, as json can't encode string with non ascii characters
--memory.readbyterange
commandTable["memory.readbyterange"] = function(currentCmd)
--print("cmd:", currentCmd)
table.remove(currentCmd, 1) -- now currentCmd is argument list
local str = memory.readbyterange(unpack(currentCmd))
arrayWithData = {}
--reencoode string to table
str:gsub(".",function(c) table.insert(arrayWithData, string.byte(c)) end)
--print("result:", arrayWithData)
sendToHost({"memory.readbyterange".."_finished", arrayWithData})
end
commandsQueue = {}
function parseCommand()
while true do
if commandsQueue[1] then
currentCmd = table.remove(commandsQueue, 1)
--print("parseCommand:", currentCmd)
local cmdFunction = commandTable[currentCmd[1]]
if cmdFunction then cmdFunction(currentCmd) end
end
coroutine.yield()
end
end
parseCommandCoroutine = coroutine.create(parseCommand)
function passiveUpdate()
local message, err, part = sock2:receive("*all")
if not message then
message = part
end
if message and string.len(message)>0 then
--print(message)
local recCommand = json.decode(message)
table.insert(commandsQueue, recCommand)
coroutine.resume(parseCommandCoroutine)
end
end
function main()
while true do
passiveUpdate()
emu.frameadvance()
end
end
gui.register(passiveUpdate) --undocumented. this function will call even if emulator paused
main()