forked from merlink01/RetroArchPythonAPI
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathretroarchpythonapi.py
More file actions
466 lines (357 loc) · 13.2 KB
/
retroarchpythonapi.py
File metadata and controls
466 lines (357 loc) · 13.2 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
456
457
458
459
460
461
462
463
464
465
466
#!/usr/bin/env python
"""Retroarch Python API
This is a Python API for the RetroArch /
libretro Libraries for Console Emulators
Target:
Create an easy to use API to implement it in your own UI
"""
import os
import time
import Queue
import logging
import threading
import subprocess
__version__ = 0.02
__copyright__ = 'GPL V2'
class RetroArchPythonApi(object):
"""Usage:
api = RetroArch(retroarch_path='<Path>',settings_path='<Path>')
#Options:
#retroarch_path='/usr/bin/retroarch' --> path to retroarch executable
#settings_path='settings' --> path where all settings are saved
#resolution=('800x600') --> Setup the Resulution
#Default=800x600
#fullscreen=('fullscreen') --> Startup in fullscreen mode
#Default=True
api.start(<Game Path>,<Libretro Plugin Path>)
api.toggle_pause()
save_state = api.save()
api.reset()
api.load(save_state)
api.stop()
"""
_stderr_queue = Queue.Queue()
rom_path = None
_fullscreen = None
_process = None
_running = False
_pause = False
def __init__(self, **kwargs):
# Logging
self.logger = logging.getLogger('RetroArchPythonApi')
# Settings
self.settings = {}
if 'resolution' in kwargs:
self.settings['resolution'] = kwargs['resolution']
else:
self.settings['resolution'] = '800x600'
if 'fullscreen' in kwargs:
self.settings['fullscreen'] = kwargs['fullscreen']
else:
self.settings['fullscreen'] = True
# Pathes
self.pathes = {}
# RetroArch Executable Path
assert 'retroarch_path' in kwargs, 'RetroArch Path must be set.'
retroarch_path = os.path.abspath(kwargs['retroarch_path'])
assert os.path.isfile(retroarch_path),\
'RetroArch execuatable not found'
self.pathes['retroarch_path'] = retroarch_path
# Settings Path
assert 'settings_path' in kwargs
settings_path = os.path.abspath(kwargs['settings_path'])
self.pathes['settings'] = settings_path
if not os.path.isdir(settings_path):
os.makedirs(settings_path)
# Bios Path
bios_path = os.path.join(self.pathes['settings'], 'bios')
self.pathes['bios'] = bios_path
if not os.path.isdir(bios_path):
os.makedirs(bios_path)
# Controller Autoconfig Path
controller_path = os.path.join(self.pathes['settings'], 'controller')
self.pathes['controller'] = controller_path
if not os.path.isdir(controller_path):
os.makedirs(controller_path)
# RetroArch Config File
configfile = os.path.join(settings_path, 'retroarch.cfg')
self.pathes['configfile'] = configfile
self._create_configfile()
# Start Check Alive Thread
cath = threading.Thread(target=self._thread_check_alive)
cath.daemon = True
cath.start()
self.logger.info('Starting Retroarch Python API (Version:%s)'
% __version__)
self.logger.debug('Start in Fullscreen: %s'
% self.settings['fullscreen'])
self.logger.debug('Resolution: %s'
% self.settings['resolution'])
self.logger.debug('RetroArch Path: %s' % retroarch_path)
self.logger.debug('Settings Path: %s'
% self.pathes['settings'])
self.logger.debug('Bios Path: %s'
% self.pathes['bios'])
self.logger.debug('Gamecontroller Path: %s'
% self.pathes['controller'])
def _thread_stderr_read(self):
"""Read the output from retroarch stderr
and put it to self.stderr.queue"""
while 1:
try:
line = self._process.stderr.readline().rstrip()
if 'checkalive' in line:
continue
self.logger.debug(line)
except:
self._running = False
self._stderr_queue.put('error')
break
self._stderr_queue.put(line)
def _thread_check_alive(self):
"""This Thread checks if retroarch is alive"""
self.logger.debug('Starting Check Alive Thread')
while 1:
try:
self._process.stdin.write('checkalive\n')
self._running = True
except:
self._running = False
time.sleep(0.1)
def start(self, rom_path, core_path):
"""Start a Game ROM
This Function needs the Path of the Plugin that could be used
rom_path: Path to the Game ROM
plugin_path: Path to the suitable Plugin from libretro
Returns: True if everything was fine
Returns: False an Error occured
"""
self.logger.info('Send: Start ROM')
rom_path = os.path.expanduser(rom_path)
if not os.path.isfile(rom_path):
self.logger.error('Error: Cant Start, Romfile not exist')
self.logger.error(rom_path)
return False
if not os.path.isfile(core_path):
self.logger.error('Error: Cant Start, Corefile not exist')
self.logger.error(core_path)
return False
if self._running:
self.logger.warning('Error: Cant Start, Rom already running')
return False
self.rom_path = rom_path
self._running = True
self.logger.info('Starting Rom: %s' % rom_path)
self.logger.info('With Core: %s' % core_path)
cmd = [self.pathes['retroarch_path'],
'--config', self.pathes['configfile'],
'--libretro', core_path, rom_path,
'--size', self.settings['resolution'], '--verbose']
if self.settings['fullscreen']:
cmd.append('--fullscreen')
self.logger.debug(str(cmd))
self._process = subprocess.Popen(cmd, stdin=subprocess.PIPE,
stdout=subprocess.PIPE,
stderr=subprocess.PIPE)
rth = threading.Thread(target=self._thread_stderr_read)
rth.daemon = True
rth.start()
time.sleep(0.5)
if self._running:
self.logger.info('Done')
return True
else:
self.logger.error('Could not start')
return False
def stop(self):
"""Exit a running ROM"""
if not self._running:
self.logger.error('Rom is not running')
return False
self.logger.info('Send: Quit')
if self._pause:
self.toggle_pause()
time.sleep(0.2)
self._process.stdin.write('QUIT\n')
while self._running:
time.sleep(0.1)
self.logger.info('Rom Exited Successfull')
def toggle_pause(self):
"""Toggle from Pause to Unpause mode
Returns: The new State: "Pause" or "Unpause" Mode
Returns: False an Error occured
"""
if not self._running:
self.logger.error('Rom is not running')
return False
self._clear_stderr_queue()
self.logger.info('Send: Toggle Pause')
self._process.stdin.write('PAUSE_TOGGLE\n')
time.sleep(0.1)
for _ in xrange(len(self._stderr_queue.queue)):
answer = self._stderr_queue.get()
if "Paused" in answer:
self.logger.info('Paused')
self._pause = True
return 'paused'
elif "Unpaused" in answer:
self.logger.info('Unpaused')
self._pause = False
return 'unpaused'
self.logger.warning(answer)
self._pause = False
return False
def toggle_fullscreen(self):
"""Toggle from Window to Fullscreen mode
Returns: The new State: "Fullscreen" or "Window" Mode
Returns: False an Error occured
"""
if not self._running:
self.logger.error('Rom is not running')
return False
self._clear_stderr_queue()
self.logger.info('Send: Fullscreen Toggle')
self._process.stdin.write('FULLSCREEN_TOGGLE\n')
time.sleep(0.5)
found = False
for _ in xrange(len(self._stderr_queue.queue)):
answer = self._stderr_queue.get()
if 'Video @' in answer:
found = True
break
if not found:
self.logger.error('Error in Fullscreen Toggle Answer')
return False
if 'Video @ fullscreen' in answer:
self._fullscreen = True
self.logger.info('Fullscreen Mode')
return 'fullscreen'
else:
self._fullscreen = False
self.logger.info('Window Mode')
return 'window'
def load(self, state):
"""Load a Savestate
state: Raw State Data from save function
"""
if not self._running:
self.logger.error('Rom is not running')
return False
savepath = os.path.splitext(self.rom_path)[0]
savepath = '%s.state' % savepath
t_file = open(savepath, 'wb')
t_file.write(state)
t_file.close()
# Savestate not possible in Pause Mode?
toggle_pause = False
if toggle_pause:
self.toggle_pause()
self.logger.info('Send: Load State')
if self._pause:
toggle_pause = True
self.toggle_pause()
self._process.stdin.write('LOAD_STATE\n')
for _ in xrange(len(self._stderr_queue.queue)):
answer = self._stderr_queue.get()
if 'State size' in answer:
self.logger.info('Done: Loaded State')
return True
elif 'Failed to load state' in answer:
break
self.logger.info('Error: Load State Failed')
return False
def _clear_stderr_queue(self):
"""Clear the Queue that holds the
stderr Informations from retroarch"""
while len(self._stderr_queue.queue) > 0:
self._stderr_queue.get()
def save(self):
"""Saves the State of the current Rom
and returns the RAW Game Data.
Return None: An Error occured
"""
if not self._running:
self.logger.error('Rom is not running')
return False
self._clear_stderr_queue()
self.logger.info('Send: Save State')
time.sleep(0.2)
# Savestate not possible in Pause Mode?
toggle_pause = False
if self._pause:
toggle_pause = True
self.toggle_pause()
self._process.stdin.write('SAVE_STATE\n')
time.sleep(0.1)
# Recieve Infos
for _ in xrange(len(self._stderr_queue.queue)):
answer = self._stderr_queue.get()
if 'Saving state' in answer:
break
try:
save_path = answer.split('"')[1]
except:
self.logger.error('Didnt recieve correct Infos from Stderr')
self.logger.error('Got: %s' % answer)
# Restore Pause Mode
if toggle_pause:
self.toggle_pause()
return False
s_file = open(save_path, 'rb')
data = s_file.read()
s_file.close()
os.remove(save_path)
self.logger.info('Saved')
# Restore Pause Mode
if toggle_pause:
self.toggle_pause()
return data
def reset(self):
"""Reset a running Rom and start from beginning"""
if not self._running:
self.logger.error('Rom is not running')
return False
time.sleep(0.3)
self._clear_stderr_queue()
self.logger.info('Send: Reset')
if self._pause:
self.toggle_pause()
self._process.stdin.write('RESET\n')
time.sleep(0.3)
for _ in xrange(len(self._stderr_queue.queue)):
answer = self._stderr_queue.get()
if 'Reset' in answer:
break
if 'Reset' in answer:
self.logger.info('Reset')
return True
else:
self.logger.error('Reset Failure')
return False
def _create_configfile(self):
"""Create the RetroArch Configfile"""
if os.path.exists(self.pathes['configfile']):
self.logger.info('Configfile exists already: %s'
% self.pathes['configfile'])
self.logger.info('Dont overwrite!')
return
configfile = """
stdin_cmd_enable = "true"
# Configuration
system_directory = "%s"
config_save_on_exit = false
# Controllers
joypad_autoconfig_dir = "%s"
input_autodetect_enable = true
# Toggles eject for disks. Used for multiple-disk games.
# input_disk_eject_toggle =
# Cycles through disk images. Use after ejecting.
# Complete by toggling eject again.
# input_disk_next =
""" % (self.pathes['bios'], self.pathes['controller'])
self.logger.debug('Write Configfile:%s' % configfile)
while '\n ' in configfile:
configfile = configfile.replace('\n ', '\n')
f_handle = open(self.pathes['configfile'], 'w')
f_handle.write(configfile)
f_handle.close()