-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.py
1369 lines (1028 loc) · 51 KB
/
main.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
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
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
import argparse
import copy
import glob
import json
import logging
import os
import re
import shutil
import subprocess
import sys
import tempfile
from datetime import datetime
from hashlib import md5
from sys import exit
from typing import Dict, List, Optional
import requests
import win32api
from errorhandler import ErrorHandler
from __init__ import __version__
USER_AGENT = "Mozilla/5.0 (Windows NT 10.0; Win64; x64; rv:122.0) Gecko/20100101 Firefox/122.0"
INSTALLER_REGEX = (r"(?:\\|/|^)setup((?:_+[A-Za-zÁ-Úá-úÑñ0-9\-.]+)+)(_.+)?(_+\([0-9]+\)|_[0-9]+(?:\.[0-9]+)+)(_\(["
r"^\)]+\))?\.exe$")
PRODUCT_ID_REGEX_1 = r"^.+?\"tmp\\([0-9]+)\.ini\""
PRODUCT_ID_REGEX_2 = r"^.+?\"(?:.+?\\)?goggame-([0-9]+)\.(?:hashdb|info|script|id)\""
OLD_VERSION_REGEX = r"_([0-9]+(?:\.[0-9]+)+)\.exe"
EXTRACT_VERSION_REGEX = r"_+((?:v\.?)?(?:[a-zá-úñ0-9]+\-)?(?:[0-9\-]+(?:\.[0-9a-z\-_]+?(?:\([^\)]+?\))?)*))_\("
BUILD_ID_REGEX = r".+?\.\[([0-9]+)\]"
VERSION_NAME_REGEX = r"(.+?)\.(?:\[[0-9]*\]?)?$"
UNKNOWN = "Unknown"
CURRENT_DATE = datetime.today().strftime("%Y%m%d_%H%M%S")
global_exe_info = {}
global STR_TO_REMOVE_FROM_NAME
global DATA_FILE_CONTENT
global REPLACE_NAMES
global MATCH_VERSIONS
global ROMAN_NUMERALS
global GOODIES_ID
global DELISTED_GAMES
# TODO:
# - Store API responses to use with get_local_version_from_gog() instead of re-downloading it.
# - Maybe add a function to retrieve account games for delisted games, mainly for old gen installers as we rely on
# public search to get their product ID.
def main():
parser = argparse.ArgumentParser(prog="gog-installer-update-checker",
description="Check GOG installer for updates")
parser.add_argument("-v", "--version",
action="version",
version=f"%(prog)s v{__version__}")
parser.add_argument("--path",
help="Path(s) to directories containing GOG installers.",
nargs="*",
required=True,
action="extend")
parser.add_argument("--innoextract-path",
help="Path to the innoextract executable. By default taken from PATH.",
nargs="?",
default=shutil.which("innoextract"))
parser.add_argument("--output-file",
help="Path to the file where the installers with found updates will be listed. Current date is "
"appended to the name. Default is no output file.",
nargs="?",
default=None)
parser.add_argument("--log-level",
help="How much stuff is logged. Can be 'debug', 'info', 'warning', 'error'.",
default="warning",
choices=["debug", "info", "warning", "error"],
type=str.lower)
parser.add_argument("--log-file",
help="Where to store the log file. "
"Default: 'gog_installer_update_checker_{CURRENT_DATE}.log' "
"in the current working directory.",
nargs="?",
default=None)
parser.add_argument("--data-file",
help="Path to the data file. "
"By default data.json in the current working directory is loaded if found, "
"otherwise nothing.",
nargs="?",
required=False,
default=os.path.join(os.getcwd(), "data.json"))
args = parser.parse_args()
paths = args.path # type: List[str]
innoextract_path = args.innoextract_path
output_file = args.output_file
data_file = args.data_file
log_file = args.log_file
if log_file is None:
log_file = os.path.join(os.getcwd(), f"gog_installer_update_checker_{CURRENT_DATE}.log")
else:
dirname = os.path.dirname(log_file)
basename, ext = os.path.splitext(log_file)
log_file = os.path.join(dirname, f"{basename}_{CURRENT_DATE}{ext}")
file_handler = logging.FileHandler(filename=log_file, encoding="utf-8")
stdout_handler = logging.StreamHandler(stream=sys.stdout)
handlers = [file_handler, stdout_handler]
log_level = logging.getLevelName(args.log_level.upper())
logging.basicConfig(level=log_level, format="%(asctime)s %(levelname)s: %(message)s", handlers=handlers)
error_handler = ErrorHandler()
if innoextract_path is None:
logging.error("Innoextract not found in PATH and not specified. Exiting...")
exit(1)
if not os.path.exists(innoextract_path):
logging.error("Innoextract path does not exist.")
exit(1)
if not os.path.isfile(innoextract_path):
logging.error("Innoextract path is not a file.")
exit(1)
for path in paths:
if not os.path.exists(path):
logging.error(f"Path {path} does not exist.")
exit(1)
if not os.path.isdir(path):
logging.error(f"Path {path} is not a directory.")
exit(1)
logging.info("Starting update checking...")
set_data_content(data_file=data_file)
start_processing(paths=paths,
innoextract_path=innoextract_path,
output_file=output_file)
logging.shutdown()
if os.lstat(log_file).st_size == 0:
os.remove(log_file)
if error_handler.fired:
exit(1)
else:
exit(0)
def set_data_content(data_file: str) -> None:
global DATA_FILE_CONTENT
global STR_TO_REMOVE_FROM_NAME
global REPLACE_NAMES
global MATCH_VERSIONS
global ROMAN_NUMERALS
global GOODIES_ID
global DELISTED_GAMES
logging.info("Loading data file content...")
try:
with open(data_file, "r", encoding="utf_8") as f:
DATA_FILE_CONTENT = json.load(f)
except FileNotFoundError:
logging.warning(f"File \"{data_file}\" doesn't exist. Loading empty content.")
DATA_FILE_CONTENT = {}
except json.decoder.JSONDecodeError as e:
logging.error(f"Error decoding JSON data file \"{data_file}\".")
logging.error(e)
exit(1)
if len(DATA_FILE_CONTENT) > 0:
logging.info("Setting up constants...")
# setup STR_TO_REMOVE_FROM_NAME
if isinstance(DATA_FILE_CONTENT.get("Strings_To_Remove"), list):
STR_TO_REMOVE_FROM_NAME = tuple(DATA_FILE_CONTENT.get("Strings_To_Remove"))
elif DATA_FILE_CONTENT.get("Strings_To_Remove") is None:
STR_TO_REMOVE_FROM_NAME = tuple()
else:
logging.warning(f"Content type of \"Strings_To_Remove\" is invalid, it should be a list and currently is "
f"{type(DATA_FILE_CONTENT.get("Strings_To_Remove"))}")
STR_TO_REMOVE_FROM_NAME = tuple()
# setup REPLACE_NAMES
if isinstance(DATA_FILE_CONTENT.get("Replace_Names"), dict):
REPLACE_NAMES = DATA_FILE_CONTENT.get("Replace_Names")
elif DATA_FILE_CONTENT.get("Replace_Names") is None:
REPLACE_NAMES = {}
else:
logging.warning(f"Content type of \"Replace_Names\" is invalid, it should be a dict and currently is "
f"{type(DATA_FILE_CONTENT.get("Replace_Names"))}")
REPLACE_NAMES = {}
# setup MATCH_VERSIONS
if isinstance(DATA_FILE_CONTENT.get("Match_Versions"), dict):
MATCH_VERSIONS = DATA_FILE_CONTENT.get("Match_Versions")
elif DATA_FILE_CONTENT.get("Match_Versions") is None:
MATCH_VERSIONS = {}
else:
logging.warning(f"Content type of \"Match_Versions\" is invalid, it should be a dict and currently is "
f"{type(DATA_FILE_CONTENT.get("Match_Versions"))}")
MATCH_VERSIONS = {}
# setup ROMAN_NUMERALS
if isinstance(DATA_FILE_CONTENT.get("Roman_Numerals"), dict):
ROMAN_NUMERALS = DATA_FILE_CONTENT.get("Roman_Numerals")
elif DATA_FILE_CONTENT.get("Roman_Numerals") is None:
ROMAN_NUMERALS = {}
else:
logging.warning(f"Content type of \"Roman_Numerals\" is invalid, it should be a dict and currently is "
f"{type(DATA_FILE_CONTENT.get("Roman_Numerals"))}")
ROMAN_NUMERALS = {}
# setup GOODIES_ID
if isinstance(DATA_FILE_CONTENT.get("Goodies_ID"), dict):
GOODIES_ID = DATA_FILE_CONTENT.get("Goodies_ID")
elif DATA_FILE_CONTENT.get("Goodies_ID") is None:
GOODIES_ID = {}
else:
logging.warning(f"Content type of \"Goodies_ID\" is invalid, it should be a dict and currently is "
f"{type(DATA_FILE_CONTENT.get("Goodies_ID"))}")
GOODIES_ID = {}
# setup DELISTED_GAMES
if isinstance(DATA_FILE_CONTENT.get("Delisted_Games"), list):
DELISTED_GAMES = DATA_FILE_CONTENT.get("Delisted_Games")
elif DATA_FILE_CONTENT.get("Delisted_Games") is None:
DELISTED_GAMES = {}
else:
logging.warning(f"Content type of \"Delisted_Games\" is invalid, it should be a list and currently is "
f"{type(DATA_FILE_CONTENT.get("Delisted_Games"))}")
DELISTED_GAMES = {}
else:
logging.info("No data file or data file empty, setting empty constants...")
STR_TO_REMOVE_FROM_NAME = tuple()
REPLACE_NAMES = {}
MATCH_VERSIONS = {}
ROMAN_NUMERALS = {}
GOODIES_ID = {}
DELISTED_GAMES = {}
def start_processing(paths: List[str],
innoextract_path: str,
output_file: Optional[str]) -> None:
logging.info("Starting processing...")
logging.info("Paths:")
for path in paths:
logging.info("\t" + path)
logging.info(f"InnoExtract path: \"{innoextract_path}\"")
logging.info("Getting installers list...")
installers_list = get_installers_list(paths=paths)
if len(installers_list) == 0:
logging.critical("No installers found, exiting...")
exit(1)
logging.info("Finished getting installers.")
logging.info("Mapping installers to their product ID...")
installers_dict = map_product_id(installers_list=installers_list,
innoextract_path=innoextract_path)
logging.info("Finished mapping installers.")
logging.info("De-duplicating installers...")
installers_dict = dedup_installers_id(installers_dict=installers_dict)
logging.info("Finished de-duplicating installers.")
logging.info("Retrieving information for the installers and removing non-base game installers...")
local_info = insert_missing_info(installers_dict=installers_dict,
innoextract_path=innoextract_path)
logging.info("Finished retrieving information...")
logging.info("Retrieving updated installer data from GOG...")
online_info = get_online_data(local_info=local_info)
logging.info("Finished retrieving updated data.")
logging.info("Comparing installer versions...")
new_versions_dict = {}
compare_versions(local_info=local_info,
online_info=online_info,
new_versions_dict=new_versions_dict)
logging.info("Finished comparing installer versions.")
if len(new_versions_dict) > 0:
if output_file is not None:
logging.info("Dumping installers with available updated versions to output file...")
write_installer_list(new_versions_dict=new_versions_dict,
output_file=output_file)
logging.info("Finished dumping installers.")
else:
logging.info("No output file set, list of updated installers wont be stored.")
else:
logging.info("No updated installers were found.")
def write_installer_list(new_versions_dict: dict,
output_file: str) -> None:
dirname = os.path.dirname(output_file)
name, ext = os.path.splitext(os.path.basename(output_file))
name = f"{name}_{CURRENT_DATE}"
output_file = os.path.join(dirname, name + ext)
try:
file_stream = open(output_file, "w", encoding="utf-8")
except PermissionError as e:
logging.critical("Couldn't open output file.")
logging.critical(e)
exit(1)
for product_id in new_versions_dict.keys():
product_name = new_versions_dict[product_id].get("product_name")
local_version = new_versions_dict[product_id].get("local_version", UNKNOWN)
local_build = new_versions_dict[product_id].get("local_build", UNKNOWN)
online_version = new_versions_dict[product_id].get("online_version", UNKNOWN)
online_build = new_versions_dict[product_id].get("online_build", UNKNOWN)
local_old_version = new_versions_dict[product_id].get("local_old_version") # type: bool
online_old_version = new_versions_dict[product_id].get("online_old_version") # type: bool
file_stream.write(f"{product_name} ({product_id})" + "\n")
if local_old_version and not online_old_version:
file_stream.write(f"{local_version} {{OLD GEN INSTALLER}} -> {online_version}" + "\n")
else:
file_stream.write(f"{local_version} -> {online_version}" + "\n")
file_stream.write(f"{local_build} -> {online_build}" + "\n")
file_stream.write("\n\n")
def get_installers_list(paths: List[str]) -> List[str]:
installer_list_aux = []
installers_list = []
logging.info("Retrieving executables from given paths...")
for path in paths:
executable_list = glob.glob("**" + os.path.sep + "*.exe", root_dir=path, recursive=True)
if len(executable_list) == 0:
logging.info(f"No executables found in \"{path}\".")
for item in executable_list:
installer_list_aux.append(os.path.join(path, item))
if len(installer_list_aux) == 0:
logging.error("No executables found in any of the paths.")
return []
for item in sorted(installer_list_aux):
if re.search(INSTALLER_REGEX, item, re.IGNORECASE):
installers_list.append(item)
return copy.deepcopy(sorted(installers_list))
def map_product_id(installers_list: List[str],
innoextract_path: str) -> Dict[str, str]:
mapping = {}
for installer in sorted(installers_list):
basename = os.path.basename(installer)
logging.info(f"Retrieving product ID for \"{basename}\"...")
product_id = get_product_id(installer_path=installer,
innoextract_path=innoextract_path)
if product_id is not None:
mapping[installer] = product_id
logging.info(f"Product ID for \"{basename}\" retrieved successfully.")
logging.info(f"Product ID: {product_id}")
return copy.deepcopy(mapping)
def get_product_id(installer_path: str,
innoextract_path: str) -> Optional[str]:
global REPLACE_NAMES
global DELISTED_GAMES
basename = os.path.basename(installer_path)
cmd = [innoextract_path,
"-l",
installer_path]
try:
output = subprocess.run(cmd, encoding="utf_8", capture_output=True, check=True).stdout.strip()
except subprocess.CalledProcessError as e:
logging.critical("Couldn't get the product id, there was an error executing innoextract.")
logging.critical(f"ERROR:\n{e}")
exit(1)
product_id = None
for line in output.splitlines():
try:
product_id = re.match(PRODUCT_ID_REGEX_1, line, re.IGNORECASE).groups()[0]
break
except (AttributeError, IndexError):
pass
try:
product_id = re.match(PRODUCT_ID_REGEX_2, line, re.IGNORECASE).groups()[0]
break
except (AttributeError, IndexError):
pass
if product_id is not None:
return product_id
# try by retrieving the product name from the properties and searching it on GOG
logging.info("Could not find the product ID with innoextract. Falling to search for product name on GOG and "
"retrieve the product ID from the response.")
exe_info = get_exe_info(installer_path)
if exe_info is not None:
product_name = exe_info.get("ProductName")
if product_name is None:
logging.warning(f"Couldn't get the product name & ID for \"{basename}\". Please report this.")
return None
if product_name in DELISTED_GAMES:
logging.info(f"Delisted game: {product_name}, skipping...")
return None
for string in STR_TO_REMOVE_FROM_NAME:
product_name = re.sub(string, "", product_name, flags=re.IGNORECASE)
if len(REPLACE_NAMES) > 0:
product_name = REPLACE_NAMES.get(product_name, REPLACE_NAMES.get(exe_info.get("ProductName"), product_name))
while re.search(r"[a-zá-úñ]-", product_name, flags=re.IGNORECASE):
product_name = re.sub(r"([a-zá-úñ])-\s", r"\1 - ", product_name, flags=re.IGNORECASE)
else:
logging.warning(f"Couldn't get the product ID for \"{basename}\". Please report this.")
return None
product_id = search_product_id_on_gog(product_name=product_name)
return product_id
def search_product_id_on_gog(product_name: str) -> Optional[str]:
logging.info(f"Performing public search to get the product ID of: {product_name}...")
gog_url = "https://embed.gog.com/games/ajax/filtered?mediaType=game&search={0}"
gog_info = download_data(gog_url=gog_url,
product_name=product_name)
if gog_info is None:
return None
if gog_info.get("totalGamesFound") == 0:
found_numeral = False
for numeral in ROMAN_NUMERALS.keys():
if numeral in product_name.split():
found_numeral = True
product_name = product_name.replace(numeral, str(ROMAN_NUMERALS[numeral]))
if found_numeral:
logging.info(f"Roman number found in \"{product_name}\", replacing with decimal equivalent and searching "
f"again...")
return search_product_id_on_gog(product_name)
else:
logging.warning(f"No games found for \"{product_name}\".")
return None
if gog_info.get("totalGamesFound") == 1:
logging.info(f"A single game found for \"{product_name}\".")
return str(gog_info["products"][0]["id"])
for product in gog_info["products"]:
if product_name.lower() == product["title"].lower():
logging.info(f"Product ID for \"{product_name}\" found.")
return str(product["id"])
logging.warning(f"No games found for \"{product_name}\".")
return None
def get_exe_info(file_path: str) -> Optional[dict]:
"""
| Available keys (their value may or may not be Nonetype):
- *Comments*
- *InternalName*
- *CompanyName*
- *FileDescription*
- *FileVersion*
- *LegalCopyright*
- *OriginalFileName*
- *ProductName*
- *ProductVersion*
- *LegalTrademarks*
- *PrivateBuild*
- *SpecialBuild*
:param file_path: Path to executable file.
:return: Dictionary with information about the executable file.
"""
logging.info(f"Extracting information from executable file: {os.path.basename(file_path)}")
if global_exe_info.get(file_path, "") != "":
logging.info(f"Information for executable file: \"{file_path}\" was found, reusing...")
return copy.deepcopy(global_exe_info[file_path])
properties = ("Comments", "InternalName", "ProductName", "CompanyName", "LegalCopyright", "ProductVersion",
"FileDescription", "LegalTrademarks", "PrivateBuild", "FileVersion", "OriginalFilename",
"SpecialBuild")
try:
lang, codepage = win32api.GetFileVersionInfo(file_path, "\\VarFileInfo\\Translation")[0]
properties_dict = {}
for property_name in properties:
info_path = "\\StringFileInfo\\{:04X}{:04X}\\{}".format(lang, codepage, property_name)
try:
key = property_name.strip()
except AttributeError:
key = property_name
if key is None:
continue
try:
value = win32api.GetFileVersionInfo(file_path, info_path).strip()
except AttributeError:
value = win32api.GetFileVersionInfo(file_path, info_path)
if value == "":
value = None
properties_dict[key] = value
return copy.deepcopy(properties_dict)
except:
return None
def dedup_installers_id(installers_dict: dict) -> Dict[str, Dict[str, str]]:
id_list = set()
for key in sorted(installers_dict.keys()):
id_list.add(str(installers_dict[key]))
deduped_installers_dict_aux = {}
for product_id in sorted(id_list):
found = False
while not found:
for key in sorted(installers_dict.keys()): # type: str
if installers_dict[key] == product_id:
found = True
deduped_installers_dict_aux[key] = {"product_id": product_id}
break
deduped_installers_dict = {}
for item in sorted(deduped_installers_dict_aux.keys()):
deduped_installers_dict[item] = copy.deepcopy(deduped_installers_dict_aux[item])
return copy.deepcopy(deduped_installers_dict)
def insert_missing_info(installers_dict: dict,
innoextract_path: str) -> dict:
"""
Insert missing installer information into the installers_dict.
:return: A new dictionary containing the complete installer information
"""
local_info = copy.deepcopy(installers_dict)
for installer in sorted(installers_dict.keys()):
basename = os.path.basename(installer)
logging.info(f"Processing \"{basename}\"...")
if re.search(OLD_VERSION_REGEX, installer, re.IGNORECASE):
logging.info(f"\"{basename}\" detected as old gen.")
old_installer = True
else:
logging.info(f"\"{basename}\" detected as current gen.")
old_installer = False
product_id = installers_dict[installer]["product_id"]
logging.info("Retrieving installer information from file properties...")
local_info.update(get_local_info_from_exe(file_path=installer,
old_installer=old_installer))
local_info[installer].update({"product_id": product_id})
logging.info("Finished retrieving installer information from file properties.")
tmp_dir = tempfile.mkdtemp()
logging.info("Starting extraction of info file from installer...")
if old_installer:
info_file = extract_info_file_old(product_id=product_id,
innoextract_path=innoextract_path,
tmp_dir=tmp_dir,
file_path=installer)
else:
info_file = extract_info_file(product_id=product_id,
innoextract_path=innoextract_path,
tmp_dir=tmp_dir,
file_path=installer)
if info_file is None:
continue
logging.info("Finished extraction of info file.")
# When using 7-Zip to extract the info file, the file is extracted with the original directory structure which
# might be a subdirectory, in this case we have to move the info file to the root of the temporary directory.
move_info_file_to_root(tmp_dir=tmp_dir)
logging.info("Reading info file...")
out_stream = open(os.path.join(tmp_dir, info_file), mode="r", encoding="utf_8", errors="backslashreplace")
info_file_content = json.load(out_stream)
out_stream.close()
logging.info("Checking for non-base game installer")
if not is_main_game(installer_info=info_file_content):
logging.info("Installer is not a base game installer, removing from update checking...")
local_info.pop(installer)
continue
logging.info("Filling all the missing info for the installer...")
if old_installer:
if local_info[installer].get("version_name") is None:
logging.info("Trying to retrieve the version from the filename...")
version_name = get_old_version_from_filename(filename=os.path.basename(installer))
if version_name is not None:
# If the version_name variable is None, we don't need to do anything as that's the fallback value
# when getting the info from the exe properties.
local_info[installer]["version_name"] = version_name
if info_file_content.get("name") is not None:
# Game's name from the info file is preferred against the ProductName property of the executable file
local_info[installer]["product_name"] = info_file_content["name"]
else:
if local_info[installer].get("build_id") is None:
try:
local_info[installer]["build_id"] = info_file_content["buildId"]
except KeyError:
logging.info("Build ID not found in info file and file properties.")
if local_info[installer].get("version_name") is None:
logging.info("Trying to retrieve the version from the filename...")
local_info[installer]["version_name"] = get_version_from_filename(filename=os.path.basename(installer))
if local_info[installer].get("version_name") is None:
logging.info("Could not get the version from the filename.")
if info_file_content.get("name") is not None:
local_info[installer]["product_name"] = info_file_content["name"]
logging.info("Finished filling missing info for the installer.")
try:
shutil.rmtree(tmp_dir)
except (FileNotFoundError, PermissionError):
pass
logging.info(f"Finished processing \"{basename}\".")
return copy.deepcopy(local_info)
def get_local_info_from_exe(file_path: str,
old_installer: bool) -> dict:
exe_info = get_exe_info(file_path=file_path)
product_name = exe_info["ProductName"]
if re.match(BUILD_ID_REGEX, exe_info["ProductVersion"]):
build_id = re.search(BUILD_ID_REGEX, exe_info["ProductVersion"]).groups()[0]
else:
build_id = None
if not old_installer:
if re.match(VERSION_NAME_REGEX, exe_info["ProductVersion"]):
version_name = re.search(VERSION_NAME_REGEX, exe_info["ProductVersion"]).groups()[0]
else:
version_name = None
else:
version_name = exe_info["ProductVersion"]
info_dict = {file_path: {
"build_id": build_id,
"product_name": product_name,
# old_version refers to the installer, not the game version.
"old_version": old_installer,
# If both local and online installers are old versions, we use the version name found in the filename to
# compare versions, so we have to retrieve and store this.
"version_name": version_name
}}
return copy.deepcopy(info_dict)
def extract_info_file(product_id: str,
innoextract_path: str,
tmp_dir: str,
file_path: str) -> Optional[str]:
"""
Extract the info file from the given installer to the specified temporary directory,
returning the name of the info file.
:param product_id: ID of the product to extract the info from
:param innoextract_path: Path to innoextract
:param tmp_dir: Temporary directory where to extract the info file
:param file_path: Installer path
:return: The name of the info file or *None* if no info file could be extracted
"""
info_file = "goggame-" + product_id + ".info"
cmd_extract = [innoextract_path,
"-e",
"-I",
info_file,
"-d",
tmp_dir,
file_path]
try:
subprocess.run(cmd_extract, stdout=subprocess.DEVNULL, stderr=subprocess.STDOUT)
except subprocess.CalledProcessError as e:
print(e)
return None
if len(os.listdir(tmp_dir)) != 0:
return info_file
else:
logging.info("Couldn't extract info file from installer.")
return None
def extract_info_file_old(product_id: str,
innoextract_path: str,
tmp_dir: str,
file_path: str) -> Optional[str]:
"""
Extract the info file from the given old gen installer to the specified temporary directory,
returning the name of the info file.
:param product_id: ID of the product to extract the info from
:param innoextract_path: Path to innoextract
:param tmp_dir: Temporary directory where to extract the info file
:param file_path: Installer path
:return: The name of the info file or *None* if no info file could be extracted
"""
file_basename = os.path.basename(file_path).replace(".exe", "")
file_parent = os.path.dirname(file_path)
bin_list = glob.glob(file_basename + "-*.bin", root_dir=file_parent)
if len(bin_list) == 0:
bin_list = glob.glob(file_basename + ".bin", root_dir=file_parent)
if len(bin_list) != 0:
info_file = "goggame-{}.info".format(product_id)
info_file_in_bin = "game\\" + info_file
password = md5(product_id.encode()).hexdigest()
cmd_extract_orig = ["7z",
"e",
"",
"-o" + tmp_dir,
info_file_in_bin,
"-aoa",
"-y",
"-p" + password]
bin_path = os.path.join(file_parent, bin_list[0])
cmd_extract = copy.deepcopy(cmd_extract_orig)
cmd_extract[2] = bin_path
subprocess.run(cmd_extract, capture_output=True)
if len(os.listdir(tmp_dir)) != 0:
return info_file
else:
return None
else:
logging.info("Installer is not using the old RAR compression, falling to innoextract...")
return extract_info_file(product_id=product_id,
innoextract_path=innoextract_path,
tmp_dir=tmp_dir,
file_path=file_path)
def move_info_file_to_root(tmp_dir: str) -> None:
logging.info("Checking if info file is not located in the root of the temporary directory...")
file_path = glob.glob("**\\*.info", root_dir=tmp_dir, recursive=True)[0]
file_path = os.path.join(tmp_dir, file_path)
correct_path = os.path.join(tmp_dir, os.path.split(file_path)[1])
if file_path != correct_path:
logging.info("Moving info file to the root...")
import shutil
shutil.move(file_path, tmp_dir)
logging.info("Removing subdirectory from the temp directory...")
path_to_remove = os.path.split(file_path)[0]
while True:
if os.path.split(path_to_remove)[0] == tmp_dir:
break
path_to_remove = os.path.split(path_to_remove)[0]
shutil.rmtree(path_to_remove)
else:
logging.info("Nothing to move.")
def is_main_game(installer_info: dict) -> bool:
"""
Detect if the installer belongs to a main game and not a DLC/Expansion/Goodie.
:param installer_info: Installer information
:return: Whether the installer belongs to a main game
"""
global GOODIES_ID
if installer_info.get("dependencyGameId") is not None:
if installer_info.get("dependencyGameId") != "":
return False
else:
return True
elif installer_info.get("gameId") != installer_info.get("rootGameId"):
return False
elif str(installer_info.get("rootGameId")) in GOODIES_ID.keys():
return False
else:
return True
def get_old_version_from_filename(filename: str):
"""
Try to extract the version from the filename for old gen installers.
:param filename: Filename where to extract the version from
:return: Version string if found, else *None*
"""
try:
version_name = re.search(OLD_VERSION_REGEX, filename, re.IGNORECASE).groups()[0]
except (AttributeError, IndexError):
version_name = None
logging.error("Couldn't get the version from the filename.")
return version_name
def get_version_from_filename(filename: str) -> Optional[str]:
"""
| Try to extract the version from the filename.
| Versioning (at least in games) varies wildly, so it's extremely complicated to extract the correct version from
the filename, which is why this is the last option.
:param filename: Filename where to extract the version from
:return: Version string if found, else *None*
"""
filename = os.path.basename(filename)
try:
version_name = re.search(EXTRACT_VERSION_REGEX, filename).groups()[0]
except (AttributeError, IndexError, ValueError, TypeError):
version_name = None
logging.warning(f"Could not extract version from \"{filename}\"")
if version_name is not None:
version_name = version_name.strip().replace("_", " ")
return version_name
def get_online_data(local_info) -> dict:
online_info = {}
for installer in sorted(local_info.keys()):
basename = os.path.basename(installer)
logging.info(f"Downloading updated data for \"{basename}\"...")
product_id = local_info[installer]["product_id"]
load_online_data(product_id=product_id,
online_info=online_info,
file_path=installer)
logging.info(f"Finished downloading data for \"{basename}\".")
return copy.deepcopy(online_info)
def load_online_data(product_id: str,
online_info: dict,
file_path: str) -> None:
logging.info(f"Retrieving latest build and version for \"{product_id}\"...")
gog_url = "https://content-system.gog.com/products/{0}/os/windows/builds?generation=2"
gog_dict = download_data(product_id=product_id,
gog_url=gog_url)
if gog_dict is None:
online_info[product_id] = {"version_name": None,
"build_id": None,
"old_version": None}
return
if gog_dict["count"] == 0:
logging.info("Trying to find if the product ID belongs to a pack and extract the (actual) game ID...")
new_product_id = get_product_id_from_pack(product_id=product_id)
if new_product_id is not None and new_product_id != product_id:
gog_dict = download_data(product_id=new_product_id,
gog_url=gog_url)
if gog_dict["count"] == 0:
logging.warning(f"Product \"{os.path.basename(file_path)}\" ({product_id}) build information wasn't found on "
f"GOG.")
online_info[product_id] = {"version_name": None,
"build_id": None,
"old_version": None}
return
if gog_dict["items"][0].get("legacy_build_id") is None:
last_version = gog_dict["items"][0]["version_name"]
last_build = gog_dict["items"][0]["build_id"]
online_info[product_id] = {"version_name": last_version,
"build_id": last_build,
# old_version refers to the online installer version, not the game version
"old_version": False}
else:
logging.info("Only old gen installers are available for this game.")
last_legacy_build_id = str(gog_dict["items"][0].get("legacy_build_id"))
# if the last online installer is old_version, then we have to get the version from the (online) filename, for
# this we have to download the repository info.
logging.info("Retrieving latest legacy installer version...")
last_version = get_last_version_old_installer(last_legacy_build_id=last_legacy_build_id,
product_id=product_id)
online_info[product_id] = {"version_name": last_version,
"build_id": last_legacy_build_id,
"old_version": True}