-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathpython_server.py
More file actions
455 lines (359 loc) · 12.4 KB
/
python_server.py
File metadata and controls
455 lines (359 loc) · 12.4 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
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
import time, socket, json, threading
RECEIVE_BUF = 1024*1024
conn = None
callbacksThread = None
def waitForConnection():
global conn, callbacksThread
s = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
s.setsockopt(socket.IPPROTO_TCP, socket.TCP_NODELAY, 1)
s.bind(("127.0.0.1", 81))
s.listen(1)
s.setblocking(1)
print("Waiting connection from emulator...")
conn, addr = s.accept()
conn.setblocking(1)
conn.settimeout(0.001)
print("Connected: ", conn)
callbacksThread = startCallbacksThread()
print("Thread for listening callbacks from emulator started")
class callbacks:
functions = {}
callbackList = [
"emu.registerbefore_callback",
"emu.registerafter_callback",
"memory.registerexecute_callback",
"memory.registerwrite_callback",
]
@classmethod
def registerfunction(cls, func):
if func == None:
return 0
hfunc = hash(func)
callbacks.functions[hfunc] = func
return hfunc
@classmethod
def error(cls, e):
emu.message("Python error: " + str(e))
@classmethod
def checkAllCallbacks(cls, cmd):
#print("check:", cmd)
for callbackName in callbacks.callbackList:
if cmd[0] == callbackName:
hfunc = cmd[1]
#print("hfunc:", hfunc)
func = callbacks.functions.get(hfunc)
#print("func:", func)
if func:
try:
func(*cmd[2:]) #skip function name and function hash and save others arguments
except Exception as e:
callbacks.error(e)
pass
#TODO: thread locking
sender.send(callbackName + "_finished")
class asyncCall:
@classmethod
def waitAnswer(cls):
buf = None
try:
buf = conn.recv(RECEIVE_BUF)
except socket.timeout:
pass
return buf
class syncCall:
@classmethod
def waitUntil(cls, messageName):
"""cycle for reading data from socket until needed message was read from it. All other messages will added in message queue"""
while True:
cmd = messages.parseMessages(asyncCall.waitAnswer(), [messageName])
#print(cmd)
if cmd != None:
if len(cmd)>1:
return cmd[1]
return
@classmethod
def call(cls, *params):
"""wrapper for sending [functionName, [param1, param2, ...]] to socket and wait until client return [functionName_finished, [result1,...]] answer"""
sender.send(*params)
funcName = params[0]
return syncCall.waitUntil(funcName + "_finished")
class sender:
@classmethod
def send(cls, *params):
j = json.dumps(params)
#print(j)
return conn.send(j.encode("utf-8"))
class emu:
@classmethod
def poweron(cls):
return syncCall.call("emu.poweron")
@classmethod
def pause(cls):
return syncCall.call("emu.pause")
@classmethod
def unpause(cls):
return syncCall.call("emu.unpause")
@classmethod
def message(cls, str):
return syncCall.call("emu.message", str)
@classmethod
def softreset(cls):
return syncCall.call("emu.softreset")
@classmethod
def speedmode(cls, str):
return syncCall.call("emu.speedmode", str)
@classmethod
def setrenderplanes(cls, sprites, background):
return syncCall.call("emu.setrenderplanes", sprites, background)
@classmethod
def framecount(cls):
return syncCall.call("emu.framecount")
@classmethod
def lagged(cls):
return syncCall.call("emu.lagged")
@classmethod
def lagcount(cls):
return syncCall.call("emu.lagcount")
@classmethod
def setlagflag(cls, flag):
return syncCall.call("emu.setlagflag", flag)
@classmethod
def emulating(cls):
return syncCall.call("emu.emulating")
@classmethod
def paused(cls):
return syncCall.call("emu.paused")
@classmethod
def readonly(cls):
return syncCall.call("emu.readonly")
@classmethod
def setreadonly(cls, flag):
return syncCall.call("emu.setreadonly", flag)
@classmethod
def getdir(cls):
return syncCall.call("emu.getdir")
@classmethod
def loadrom(cls, str):
return syncCall.call("emu.loadrom", str)
@classmethod
def addgamegenie(cls, str):
return syncCall.call("emu.addgamegenie", str)
@classmethod
def delgamegenie(cls, str):
return syncCall.call("emu.delgamegenie", str)
@classmethod
def print(cls, str):
return syncCall.call("emu.print", str)
@classmethod
def getscreenpixel(cls, x, y, getemuscreen):
return syncCall.call("emu.getscreenpixel", x, y, getemuscreen)
@classmethod
def registerbefore(cls, func):
hfunc = callbacks.registerfunction(func)
return syncCall.call("emu.registerbefore", hfunc)
@classmethod
def registerafter(cls, func):
hfunc = callbacks.registerfunction(func)
return syncCall.call("emu.registerafter", hfunc)
class memory:
@classmethod
def readbyte(cls, addr):
return syncCall.call("memory.readbyte", addr)
@classmethod
def readbytesigned(cls, addr):
return syncCall.call("memory.readbytesigned", addr)
@classmethod
def readword(cls, addr):
return syncCall.call("memory.readword", addr)
@classmethod
def readwordsigned(cls, addr):
return syncCall.call("memory.readwordsigned", addr)
@classmethod
def writebyte(cls, addr, val):
return syncCall.call("memory.writebyte", addr, val)
@classmethod
def readbyterange(cls, addr, size):
return syncCall.call("memory.readbyterange", addr, size)
@classmethod
def getregister(cls, name):
return syncCall.call("memory.getregister", name)
@classmethod
def setregister(cls, name, value):
return syncCall.call("memory.setregister", name, value)
@classmethod
def registerexecute(cls, addr, size, func):
hfunc = callbacks.registerfunction(func)
return syncCall.call("memory.registerexecute", addr, size, hfunc)
@classmethod
def registerwrite(cls, addr, size, func):
hfunc = callbacks.registerfunction(func)
return syncCall.call("memory.registerwrite", addr, size, hfunc)
class rom:
@classmethod
def readbyte(cls, addr):
return syncCall.call("rom.readbyte", addr)
@classmethod
def readbytesigned(cls, addr):
return syncCall.call("rom.readbytesigned", addr)
@classmethod
def writebyte(cls, addr, val):
return syncCall.call("rom.writebyte", addr, val)
class debugger:
@classmethod
def hitbreakpoint(cls):
return syncCall.call("debugger.hitbreakpoint")
@classmethod
def getcyclescount(cls):
return syncCall.call("debugger.getcyclescount")
@classmethod
def getinstructionscount(cls):
return syncCall.call("debugger.getinstructionscount")
@classmethod
def resetcyclescount(cls):
return syncCall.call("debugger.resetcyclescount")
@classmethod
def resetinstructionscount(cls):
return syncCall.call("debugger.resetinstructionscount")
class joypad:
@classmethod
def read(cls, player):
return syncCall.call("joypad.read", player)
@classmethod
def readimmediate(cls, player):
return syncCall.call("joypad.readimmediate", player)
@classmethod
def readdown(cls, player):
return syncCall.call("joypad.readdown", player)
@classmethod
def readup(cls, player):
return syncCall.call("joypad.readup", player)
@classmethod
def write(cls, player, table):
return syncCall.call("joypad.write", player, table)
class zapper:
@classmethod
def read(cls):
return syncCall.call("zapper.read")
class input:
@classmethod
def read(cls):
return syncCall.call("input.read")
class sound:
@classmethod
def get(cls):
return syncCall.call("sound.get")
class movie:
@classmethod
def active(cls):
return syncCall.call("movie.active")
@classmethod
def mode(cls):
return syncCall.call("movie.mode")
@classmethod
def rerecordcounting(cls, counting):
return syncCall.call("movie.rerecordcounting", counting)
@classmethod
def stop(cls):
return syncCall.call("movie.stop")
@classmethod
def length(cls):
return syncCall.call("movie.length")
@classmethod
def name(cls):
return syncCall.call("movie.name")
@classmethod
def getfilename(cls):
return syncCall.call("movie.getfilename")
@classmethod
def rerecordcount(cls):
return syncCall.call("movie.rerecordcount")
@classmethod
def replay(cls):
return syncCall.call("movie.replay")
@classmethod
def readonly(cls):
return syncCall.call("movie.readonly")
@classmethod
def setreadonly(cls, readonly):
return syncCall.call("movie.setreadonly", readonly)
@classmethod
def recording(cls):
return syncCall.call("movie.recording")
@classmethod
def playing(cls):
return syncCall.call("movie.playing")
@classmethod
def isfromsavestate(cls):
return syncCall.call("movie.isfromsavestate")
class gui:
@classmethod
def pixel(cls, x, y, color):
return syncCall.call("gui.pixel", x, y, color)
@classmethod
def getpixel(cls, x, y):
return syncCall.call("gui.getpixel", x, y)
@classmethod
def line(cls, x1, y1, x2, y2, color, firstskip):
return syncCall.call("gui.line", x1, y1, x2, y2, color, firstskip)
@classmethod
def box(cls, x1, y1, x2, y2, fillcolor, outlinecolor):
return syncCall.call("gui.box", x1, y1, x2, y2, fillcolor, outlinecolor)
@classmethod
def text(cls, x, y, str, textcolor, backcolor):
return syncCall.call("gui.text", x, y, str, textcolor, backcolor)
#@classmethod
#def parsecolor(cls, col):
# return syncCall.call("gui.parsecolor", col)
@classmethod
def savescreenshot(cls):
return syncCall.call("gui.savescreenshot")
@classmethod
def savescreenshotas(cls, fname):
return syncCall.call("gui.savescreenshotas", fname)
@classmethod
def opacity(cls, alpha):
return syncCall.call("gui.opacity", alpha)
@classmethod
def transparency(cls, trans):
return syncCall.call("gui.transparency", trans)
class messages:
queue = []
@classmethod
def parseMessages(cls, buf, nameFilterList):
if buf != None:
#print(buf, nameFilterList)
splited = buf.split(b"json")
for b in splited[1:]:
try:
val = json.loads(b)
except json.JSONDecodeError as e:
print("Can't decode json:", b)
raise e
messages.queue.append(val)
for message in messages.queue:
if nameFilterList == None or message[0] in nameFilterList:
messages.queue.remove(message)
return message
return None
def callbacksThread():
cycle = 0
while True:
cycle += 1
try:
cmd = messages.parseMessages(asyncCall.waitAnswer(), callbacks.callbackList)
if cmd:
#print("Callback received:", cmd)
callbacks.checkAllCallbacks(cmd)
pass
except socket.timeout:
pass
time.sleep(0.001)
def startCallbacksThread():
t = threading.Thread(target = callbacksThread)
t.daemon = True
t.start()
return t
if __name__=="__main__":
waitForConnection()
while True:
time.sleep(60)