-
Notifications
You must be signed in to change notification settings - Fork 5
/
Copy pathinstall-gog-game
executable file
·179 lines (143 loc) · 5.75 KB
/
install-gog-game
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
#!/usr/bin/python3
# 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.
"""
Install DOS games from gog.com into the Steam library.
You can drop this script anywhere in your PATH (it has no dependencies besides
python3 standard library and Wine).
"""
# pylint: disable=invalid-name
# pylint: disable=missing-docstring
import argparse
import glob
import json
import os
import subprocess
import sys
import tempfile
import shutil
DATA_HOME = os.environ.get('XDG_DATA_HOME') or \
os.path.expanduser('~/.local/share')
# TODO Check for Wine 3.16 or newer; older Wine will leave desktop entries.
def run_setup_file(setup_exe, pfx):
tmp_env = os.environ
tmp_env.update({
'WINEDLLOVERRIDES': 'winemenubuilder.exe=d',
'WINEPREFIX': pfx
})
inno_flags = ['/NOGUI', '/SUPPRESSMSGBOXES', '/SILENT', '/DIR=C:\\game']
cmd = ['wine', setup_exe] + inno_flags
process = subprocess.run(cmd,
check=False,
env=tmp_env,
stderr=subprocess.DEVNULL)
if process.returncode != 0:
print('{} finished with code {}'.format(setup_exe, process.returncode))
sys.exit(10 + process.returncode)
def find_gog_info(game_dir):
info_files = glob.glob(game_dir + '/goggame-*.info')
if not info_files:
print('goggame-*.info file not found')
sys.exit(3)
with open(info_files[0], 'r') as gog_info_file:
info = json.load(gog_info_file)
return info
# Trying to emulate GOG name -> slug conversion as close as possible:
#
def name_to_slug(name):
name = name.strip()
if name.endswith(', The'):
name = 'The ' + name[:-5]
name = name + '_' # to make it easier to transform Roman numerals
name = name.replace('&', 'and')
name = name.translate(str.maketrans(' ', '_', r""":™®,."-'!?"""))
name = name.replace('_IX_', '_9_')
name = name.replace('_VIII_', '_8_')
name = name.replace('_VII_', '_7_')
name = name.replace('_VI_', '_6_')
name = name.replace('_V_', '_5_')
name = name.replace('_IV_', '_4_')
name = name.replace('_III_', '_3_')
name = name.replace('_II_', '_2_')
name = name.replace('__', '_')
name = name.strip('_')
return name.lower()
def available_dir(dirname):
if not os.path.exists(dirname):
return True
if os.path.isdir(dirname) and any(True for _ in os.scandir(dirname)):
return False
return True
def remove_prefix(pfx, word):
if word.startswith(pfx):
return word[len(pfx):]
return word
def create_launcher_files(path, gog_info):
name = gog_info['name']
slug = name_to_slug(name)
task = gog_info['playTasks'][0]
args = task.get('arguments') or ''
wdir = task.get('workingDir') or ''
launcher_bat = os.path.join(path, wdir, '{}.bat'.format(slug))
gog_path = task.get('path')
exe = remove_prefix(wdir + '\\', gog_path) if wdir else gog_path
with open(launcher_bat, 'w') as batch:
batch.write('{} {}\n'.format(exe, args))
os.chmod(launcher_bat, 0o775)
with open('{path}/{slug}.desktop'.format(**locals()), 'w') as entry:
entry.write('[Desktop Entry]\n')
entry.write('Name={name}\n'.format(**locals()))
entry.write('Type=Application\n')
entry.write('Exec={launcher_bat}\n'.format(**locals()))
entry.write('Encoding=UTF-8\n')
entry.write('StartupNotify=true\n')
def main():
parser = argparse.ArgumentParser(description=__doc__)
parser.add_argument('setup_file', help='.exe installer for GOG game')
args = parser.parse_args()
setup_exe = args.setup_file
if not os.path.isfile(setup_exe):
print('File {} does not exist'.format(setup_exe), file=sys.stderr)
sys.exit(1)
if not setup_exe.lower().endswith('.exe'):
print('Only .exe installers supported', file=sys.stderr)
sys.exit(1)
with tempfile.TemporaryDirectory() as tmp_dir:
print('Unpacking game files to temporary directory: ' + tmp_dir,
file=sys.stderr)
run_setup_file(setup_exe, tmp_dir)
game_dir = os.path.join(tmp_dir, 'drive_c/game')
gog_info = find_gog_info(game_dir)
game_name = gog_info['name']
game_slug = name_to_slug(game_name)
install_path = os.path.join(DATA_HOME, 'games', game_slug)
if not available_dir(install_path):
print('Path {} is occupied already'.format(install_path))
sys.exit(4)
# ensure all directories leading to install_path exist
os.makedirs(install_path, exist_ok=True)
os.rmdir(install_path)
print('Movig game files to {}'.format(install_path))
shutil.move(game_dir, install_path)
os.chmod(install_path, 0o775)
create_launcher_files(install_path, gog_info)
print('\nInstallation finished.\n')
print('Select the following file when adding non-Steam game',
'to your library:')
print('{}/{}.desktop'.format(install_path, game_slug))
if __name__ == "__main__":
main()