-
Notifications
You must be signed in to change notification settings - Fork 4
Expand file tree
/
Copy pathlaunch.py
More file actions
executable file
·218 lines (191 loc) · 6.18 KB
/
launch.py
File metadata and controls
executable file
·218 lines (191 loc) · 6.18 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
#!/usr/bin/python
import sys
import os
import collections
from zipfile import ZipFile
import subprocess
import itertools
import tempfile
ROM_ROOT = '/home/drew/roms'
def snes_launch(flags):
core = 'bsnes_mercury_balanced'
config = {}
if 'high-accuracy' in flags:
core = 'bsnes_mercury_accuracy'
if 'multitap' in flags:
core = 'snes9x' # bsnes multitap seems broken
config['input_libretro_device_p2'] = 257
return retroarch_launch(core, config)
SYSTEMS = {
'32x': {
'launch': lambda _: retroarch_launch('picodrive'),
'extensions': ['32x'],
# core lacks multi-tap support
},
'fds': {
'launch': lambda _: retroarch_launch('nestopia'),
'extensions': ['fds'],
# bios: disksys.rom
# multitap: TODO
},
'gameboy': {
'launch': lambda _: retroarch_launch('gambatte'),
'extensions': ['gb'],
},
'gamegear': {
'launch': lambda _: retroarch_launch('genesis_plus_gx'),
'extensions': ['gg'],
},
'gba': {
'launch': lambda _: retroarch_launch('mgba'),
'extensions': ['gba'],
},
'gbc': {
'launch': lambda _: retroarch_launch('gambatte'),
'extensions': ['gbc'],
},
'genesis': {
'launch': lambda _: retroarch_launch('genesis_plus_gx'),
'extensions': ['md'],
# multitap: TODO
},
'mame': {
'launch': lambda _: ['mame'],
'extensions': [],
},
'n64': {
'launch': lambda _: retroarch_launch('mupen64plus'),
'extensions': ['n64'],
},
'nes': {
'launch': lambda _: retroarch_launch('nestopia'),
'extensions': ['nes'],
# multitap: built-in, handled by NstDatabase.xml (in bios dir)
},
'psx': {
'launch': lambda _: retroarch_launch('mednafen_psx'),
'extensions': ['cue', 'iso'],
# bios: scph*.bin
# multitap: TODO
},
'segacd': {
'launch': lambda _: retroarch_launch('genesis_plus_gx'),
'extensions': ['cue', 'iso'],
# bios: bios_CD_U.bin
# multitap: TODO handled as with geneis?
},
'snes': {
'launch': snes_launch,
'extensions': ['sfc'],
# bios: specific games require co-processor ROMs
# http://emulation.gametechwiki.com/index.php/Emulator_Files#Super_Famicom_.28SNES.29
# "SNES Coprocessor ROMs for bsnes"
# multitap: TODO
},
'turbografx': {
'launch': lambda _: retroarch_launch('mednafen_pce_fast'),
'extensions': ['pce'],
# multitap: built-in
},
'virtualboy': {
'launch': lambda _: retroarch_launch('mednafen_vb'),
'extensions': ['vb'],
# sbs rendering handled via custom default shader setting
},
'wii': {
'launch': lambda _: ['dolphin', '-e'],
'extensions': ['wad'],
# experimental
},
}
GAME_OVERRIDES = {
'high-accuracy': [
'snes/A.S.P. - Air Strike Patrol (USA)',
],
'multitap': [
'snes/Super Bomberman (USA)',
'snes/Super Bomberman 2 (USA)',
'snes/Super Bomberman 3 (Europe)',
'snes/Super Bomber Man 4 (Japan)',
'snes/Super Bomber Man 5 (Japan)',
'snes/Super Bomber Man - Panic Bomber W (Japan)',
'snes/Top Gear 3000 (USA)',
],
}
def retroarch_launch(core, custom_config={}):
cmd = ['retroarch', '-L', '/usr/lib/x86_64-linux-gnu/libretro/%s_libretro.so' % core]
if custom_config:
custom_config['config_save_on_exit'] = 'false'
configpath = tempfile.mktemp()
with open(configpath, 'w') as f:
f.write('\n'.join('%s = "%s"' % (k, v) for k, v in custom_config.iteritems()))
cmd.extend(['--appendconfig', configpath])
return cmd
def extensions():
return map_reduce(SYSTEMS.iteritems(), lambda (name, meta): [(ext, name) for ext in meta['extensions']])
def no_ext():
return [sys for sys, meta in SYSTEMS.iteritems() if not meta['extensions']]
def get_ext(path):
return os.path.splitext(path)[1][1:]
def map_reduce(data, emitfunc=lambda rec: [(rec,)], reducefunc=lambda v: v):
"""perform a "map-reduce" on the data
emitfunc(datum): return an iterable of key-value pairings as (key, value). alternatively, may
simply emit (key,) (useful for reducefunc=len)
reducefunc(values): applied to each list of values with the same key; defaults to just
returning the list
data: iterable of data to operate on
"""
mapped = collections.defaultdict(list)
for rec in data:
for emission in emitfunc(rec):
try:
k, v = emission
except ValueError:
k, v = emission[0], None
mapped[k].append(v)
return dict((k, reducefunc(v)) for k, v in mapped.iteritems())
def romdir(sys):
return os.path.join(ROM_ROOT, sys)
def in_romdir(sys, rom):
parent = os.path.join(romdir(sys), '')
child = os.path.abspath(rom)
return child.startswith(parent)
def inspect_zip(rom):
archive = ZipFile(rom)
# TODO could be multiple relevant extensions
return get_ext(archive.namelist()[0])
def match_system(rom):
COMPRESSED_FORMATS = {
'zip': inspect_zip,
}
for sys in no_ext():
if in_romdir(sys, rom):
return sys
ext = get_ext(rom)
if ext in COMPRESSED_FORMATS:
ext = COMPRESSED_FORMATS[ext](rom)
matches = extensions()[ext]
if len(matches) == 1:
return matches[0]
else:
for sys in matches:
if in_romdir(sys, rom):
return sys
def make_command(launch, rom):
rom_id = os.path.splitext(os.path.relpath(os.path.abspath(rom), ROM_ROOT))[0]
rom_flags = map_reduce(GAME_OVERRIDES.iteritems(), lambda (k, vs): [(v, k) for v in vs], set).get(rom_id, set())
cmd = launch(rom_flags)
if hasattr(cmd, '__call__'):
return cmd(rom)
else:
return cmd + [rom]
def _log(msg):
sys.stderr.write(msg + '\n')
if __name__ == "__main__":
rom = sys.argv[1]
system = match_system(rom)
if not system:
raise RuntimeError('could not determine system')
cmd = make_command(SYSTEMS[system]['launch'], rom)
_log('running: %s' % cmd)
subprocess.call(cmd)