-
Notifications
You must be signed in to change notification settings - Fork 4
/
gogverify.py
executable file
·201 lines (165 loc) · 6.56 KB
/
gogverify.py
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
#!/usr/bin/env python3
# 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 3 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, see <http://www.gnu.org/licenses/>.
import argparse
import json
import os
import sys
import glob
import urllib.request
import urllib.error
import zlib
import hashlib
from collections import namedtuple
from pathlib import Path, PureWindowsPath, PurePosixPath
args = None
def log(msg, err=False):
if args.quiet:
return
out = sys.stderr if err else sys.stdout
out.write(str(msg))
out.write('\n')
def error(msg):
log(msg, err=True)
exit(1)
def get_info(path):
glob_path = os.path.join(path, "goggame-*.info")
files = glob.glob(glob_path)
if not files:
error(f'Failed to find info file "{glob_path}".')
with open(files[0]) as f:
info = json.load(f)
if "buildId" not in info:
glob_path = os.path.join(path, "goggame-*.id")
files = glob.glob(glob_path)
if not files:
error(f'Failed to find id file "{glob_path}".')
with open(files[0]) as f:
info["buildId"] = json.load(f)["buildId"]
return info
def compute_md5(path, chunk_size=4096):
h = hashlib.md5()
with open(path, "rb") as f:
for chunk in iter(lambda: f.read(chunk_size), b""):
h.update(chunk)
return h.hexdigest()
def download_json(url, use_zlib=False):
try:
response = urllib.request.urlopen(url)
except urllib.error.HTTPError as e:
error(f'Failed to retrieve URL {url}\nReason: {e.reason}\nCode: {e.code}')
except urllib.error.URLError as e:
error(f'Failed to retrieve URL {url}\nReason: {e.reason}')
data = response.read()
if use_zlib:
data = zlib.decompress(data)
return json.loads(data.decode("utf-8"))
FileInfo = namedtuple("FileInfo", ("path", "md5", "is_dir"))
def get_files(game_id, build_id, os, language):
builds = download_json(f"https://content-system.gog.com/products/{game_id}/os/{os}/builds?generation=2")
for build in builds["items"]:
if build["build_id"] == build_id:
break
else:
error("Could not find build with correct build id.")
link = build["link"]
content = download_json(link, use_zlib=True)
files = []
for depot in content["depots"]:
if not (language == "*" or language in depot["languages"] or "*" in depot["languages"]):
continue
manifest = depot["manifest"]
depot_files = download_json(f"https://cdn.gog.com/content-system/v2/meta/{manifest[:2]}/{manifest[2:4]}/{manifest}", use_zlib=True)
for item in depot_files["depot"]["items"]:
path = str(Path({"windows": PureWindowsPath, "osx": PurePosixPath}[os](item["path"])))
if item["type"] == "DepotDirectory":
files.append(FileInfo(path, None, True))
else:
chunks = item["chunks"]
if len(chunks) > 1:
md5 = item["md5"]
elif len(chunks) == 1:
md5 = chunks[0]["md5"]
else:
md5 = hashlib.md5(b"").hexdigest()
files.append(FileInfo(path, md5, False))
return files
def files_in_dir(path):
for root, folders, files in os.walk(path):
for file in files:
yield os.path.relpath(os.path.join(root, file), path)
def main():
parser = argparse.ArgumentParser(description="Verify the installation of a game from GOG against the official MD5 hashes.")
parser.add_argument("path", help="Directory where the game is installed", nargs="?")
parser.add_argument("-q", "--quiet", default=False, action="store_true",
help="Suppress all output")
parser.add_argument("-o", "--os", choices=("windows", "osx"), default="windows",
help="OS of the game installation")
parser.add_argument("-l", "--language", default="en-US",
help="Language of the game installation")
parser.add_argument("--dump-md5sums", nargs=2, metavar=("GAME_ID", "BUILD_ID"),
help="Dump all md5 checksums for a given gameID and buildID to stdout (md5sum format)")
global args
args = parser.parse_args()
if args.dump_md5sums:
info = {"gameId": args.dump_md5sums[0], "buildId": args.dump_md5sums[1]}
else:
if not args.path:
parser.error("the following arguments are required: path")
info = get_info(args.path)
log(f"# Name: {info['name']}\n# Game ID: {info['gameId']}\n# Build ID: {info['buildId']}")
# game_id = "1207664643"
# build_id = "51727259307363981"
files = get_files(info["gameId"], info["buildId"], args.os, args.language)
if args.dump_md5sums:
for file in files:
if not file.is_dir:
log(f"{file.md5} {file.path}")
exit(0)
file_paths = {file.path for file in files}
printed_unexpected = False
for file in files_in_dir(args.path):
if file not in file_paths:
if not printed_unexpected:
log("\n# Unexpected files:")
printed_unexpected = True
log(file)
log("\n# Expected files:")
errors = []
for file in files:
msg = "OK"
local_path = os.path.join(args.path, file.path)
description = "directory" if file.is_dir else file.md5
if not os.path.exists(local_path):
msg = "Missing"
else:
if file.is_dir:
if not os.path.isdir(local_path):
msg = "Not a directory"
else:
if not os.path.isfile(local_path):
msg = "Not a file"
else:
md5 = compute_md5(local_path)
if md5 != file.md5:
msg = f"MD5 mismatch ({md5})"
if msg != "OK":
errors.append((file.path, msg))
log(f"{file.path} ({description}): {msg}")
if errors:
log("\n# Errors:")
for path, msg in errors:
log(f"{path}: {msg}")
exit(1)
if __name__ == '__main__':
main()