forked from dreamer/roberta
-
Notifications
You must be signed in to change notification settings - Fork 0
/
run-vm
executable file
·210 lines (163 loc) · 6.55 KB
/
run-vm
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
#!/usr/bin/python3
# pylint: disable=invalid-name
# pylint: disable=missing-docstring
# SPDX-License-Identifier: GPL-2.0-or-later
# Copyright (C) 2019-2021 Patryk Obara <[email protected]>
#
# This program is free software; you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation; either version 2 of the License, or
# (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License along
# with this program; if not, write to the Free Software Foundation, Inc.,
# 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
import argparse
import configparser
import os
import subprocess
import sys
import fakescripteval
import toolbox
import version
from log import log, log_err, log_warn
from settings import SETTINGS as settings
STEAM_APP_ID = os.environ.get('SteamAppId', '0')
def setup_bundle(distdir):
extend_env = (
('PATH', os.path.join(distdir, 'bin')),
('LD_LIBRARY_PATH', os.path.join(distdir, 'lib')),
)
for env_var, path in extend_env:
if os.path.isdir(path):
sys_path_str = os.getenv(env_var, None)
sys_path = sys_path_str.split(os.pathsep) if sys_path_str else []
if path not in sys_path:
log('adding {} to {}'.format(path, env_var))
sys_path.append(path)
os.environ[env_var] = os.pathsep.join(sys_path)
def zenity_err(msg):
steam_zenity = os.environ.get('STEAM_ZENITY', '/usr/bin/zenity')
cmd = [steam_zenity, '--error', '--no-wrap', '--title=Roberta Error']
log_err(msg)
subprocess.call(cmd + ['--text={}'.format(msg)])
def detect_games(scummvm_cmd, path, config_file):
detect_args = ['-p', path, '-c', config_file, '--recursive', '--add']
err = subprocess.call(scummvm_cmd + detect_args)
if err:
zenity_err('ScummVM exited with error code {}'.format(err))
sys.exit(1)
def adjust_scummvm_config(config_file):
cfg = configparser.ConfigParser(delimiters='=')
cfg.read(config_file)
if not cfg.has_section('scummvm'):
log_warn('config file is missing scummvm section')
return
resolution = settings.get_scummvm_fullresolution()
cfg['scummvm']['gfx_mode'] = 'opengl'
if resolution:
cfg['scummvm']['fullscreen'] = 'true'
with open(config_file, 'w') as output:
cfg.write(output)
def adjust_scummvm_env_config(config_file):
assert config_file
resolution = settings.get_scummvm_fullresolution()
if resolution is None:
return
width, height = resolution
cfg = configparser.ConfigParser(delimiters='=')
cfg.read(config_file)
if not cfg.has_section('scummvm'):
log_warn('config file is missing scummvm section')
return
cfg['scummvm']['last_fullscreen_mode_width'] = str(width)
cfg['scummvm']['last_fullscreen_mode_height'] = str(height)
with open(config_file, 'w') as output:
cfg.write(output)
def count_detected_games(config_file):
cfg = configparser.ConfigParser(delimiters='=')
cfg.read(config_file)
sections = cfg.sections().copy()
sections.remove('scummvm')
return len(sections)
def run_scummvm():
log('working dir: "{}"'.format(os.getcwd()))
cmd = settings.get_scummvm_cmd()
install_dir = toolbox.guess_game_install_dir()
if install_dir:
cmd = [x.replace('%install_dir%', install_dir) for x in cmd]
else:
log_warn('unrecognized installation directory')
# install_dir has some escaped characters (on purpose)
# TODO: split escaping into a separate function
install_dir = install_dir.replace('\\ ', ' ')
with toolbox.PidFile(fakescripteval.PID_FILE):
try:
scummvm_conf = 'roberta_scummvm.ini'
if settings.get_confgen_force():
toolbox.rm_force(scummvm_conf)
if not os.path.isfile(scummvm_conf):
detect_games(cmd, install_dir, scummvm_conf)
adjust_scummvm_config(scummvm_conf)
assert os.path.isfile(scummvm_conf)
adjust_scummvm_env_config(scummvm_conf)
if count_detected_games(scummvm_conf) == 0:
zenity_err('This game is not supported by ScummVM.')
os.remove(scummvm_conf)
sys.exit(1)
game_arg = os.environ.get('LUX_SCUMMVM_GAME', None)
if game_arg:
# In this case combining -p is unreliable with passing game arg;
# ScummVM might hang. However, at this point all game
# directories should be detected and stored in scummvm_conf
# file, so we don't need -p any more.
subprocess.call(cmd + ['-c', scummvm_conf, game_arg])
else:
subprocess.call(cmd + ['-p', install_dir, '-c', scummvm_conf])
except FileNotFoundError as err:
log_err(err)
def run(cmd_line, wait=False):
log('working dir: "{}"'.format(os.getcwd()))
log('original command:', cmd_line)
if wait:
fakescripteval.wait_for_previous_process()
exe_path, exe = os.path.split(cmd_line[0]) if cmd_line else (None, '')
if exe == 'iscriptevaluator.exe':
status = fakescripteval.iscriptevaluator(cmd_line)
sys.exit(status)
# we don't want to detect hardware until we're sure we are starting
# the actual game:
settings.setup()
run_file(exe_path, exe, cmd_line)
def run_file(path, exe, cmd_line):
log(path)
log(exe)
log(cmd_line)
run_scummvm()
def main():
parser = argparse.ArgumentParser()
group = parser.add_mutually_exclusive_group()
group.add_argument('--get-native-path', action='store_true')
group.add_argument('--get-compat-path', action='store_true')
group.add_argument('--wait-before-run', action='store_true')
group.add_argument('--version', action='store_true')
args, run_cmd_line = parser.parse_known_args()
setup_bundle(distdir=settings.distdir)
if len(sys.argv) == 1:
parser.print_help()
sys.exit(0)
if args.version:
print('Roberta version {0}'.format(version.VERSION[1:]))
sys.exit(0)
if args.get_native_path:
sys.exit(1)
if args.get_compat_path:
sys.exit(1)
run(run_cmd_line, wait=args.wait_before_run)
if __name__ == "__main__":
main()