-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathutils.py
More file actions
1617 lines (1390 loc) · 58.9 KB
/
utils.py
File metadata and controls
1617 lines (1390 loc) · 58.9 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
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 datetime
import hashlib
import json
import os
import re
import shutil
import subprocess
import sys
import tarfile
import time
import vdf
import platform
import requests
DEFAULT_LOG_FILENAME = "PatchOpsIII.log"
LAUNCH_COUNTER_FILENAME = "launch_counter.txt"
EXE_VARIANT_STATE_FILENAME = ".patchops_exe_variant.json"
PATCHOPS_BACKUP_SUFFIX = ".patchops.bak"
LEGACY_BACKUP_SUFFIX = ".bak"
_steam_library_paths_cache = None
def patchops_backup_path(path):
return f"{path}{PATCHOPS_BACKUP_SUFFIX}"
def legacy_backup_path(path):
return f"{path}{LEGACY_BACKUP_SUFFIX}"
def existing_backup_path(path):
for candidate in (patchops_backup_path(path), legacy_backup_path(path)):
if os.path.exists(candidate):
return candidate
return None
def _exe_variant_state_path(game_dir):
return os.path.join(game_dir, EXE_VARIANT_STATE_FILENAME)
def write_exe_variant(game_dir, variant):
if not game_dir:
return
try:
os.makedirs(game_dir, exist_ok=True)
with open(_exe_variant_state_path(game_dir), "w", encoding="utf-8") as handle:
json.dump({"variant": variant}, handle, indent=2)
except Exception:
pass
def read_exe_variant(game_dir):
if not game_dir:
return None
path = _exe_variant_state_path(game_dir)
if not os.path.exists(path):
return None
try:
with open(path, "r", encoding="utf-8") as handle:
data = json.load(handle)
value = data.get("variant")
return value if isinstance(value, str) else None
except Exception:
return None
def file_sha256(path):
if not path or not os.path.exists(path):
return None
digest = hashlib.sha256()
try:
with open(path, "rb") as handle:
for chunk in iter(lambda: handle.read(1024 * 1024), b""):
digest.update(chunk)
return digest.hexdigest()
except Exception:
return None
def get_app_data_dir():
system = platform.system()
home = os.path.expanduser("~")
if system == "Windows":
base = os.environ.get("APPDATA") or os.path.join(home, "AppData", "Roaming")
elif system == "Darwin":
base = os.path.join(home, "Library", "Application Support")
else:
base = os.environ.get("XDG_DATA_HOME") or os.path.join(home, ".local", "share")
return os.path.join(base, "PatchOpsIII")
def _resolve_log_path(log_file):
if not log_file:
log_file = DEFAULT_LOG_FILENAME
if os.path.isabs(log_file):
return log_file
filename = os.path.basename(log_file) or DEFAULT_LOG_FILENAME
return os.path.join(get_app_data_dir(), filename)
def write_log(message, category="Info", log_widget=None, log_file=DEFAULT_LOG_FILENAME):
timestamp = datetime.datetime.now().strftime("%Y-%m-%d %H:%M:%S")
full_message = f"{timestamp} - {category}: {message}"
if log_widget:
# Define colors based on category
if category == "Info":
color = "white"
elif category == "Error":
color = "red"
elif category == "Warning":
color = "yellow"
elif category == "Success":
color = "green"
else:
color = "blue"
html_message = f'<span style="color:{color};">{full_message}</span>'
handler = getattr(log_widget, "handle_write_log", None)
if callable(handler):
handler(full_message=full_message, category=category, html_message=html_message, plain_message=message)
else:
log_widget.append(html_message)
log_path = _resolve_log_path(log_file)
try:
directory = os.path.dirname(log_path)
if directory:
os.makedirs(directory, exist_ok=True)
with open(log_path, "a", encoding="utf-8") as f:
f.write(full_message + "\n")
except Exception as exc:
try:
print(f"Failed to write log entry to {log_path}: {exc}", file=sys.stderr)
except Exception:
pass
def get_log_file_path(log_file=DEFAULT_LOG_FILENAME):
"""Return the resolved path for the given log file."""
return _resolve_log_path(log_file)
def _launch_counter_path():
return os.path.join(get_app_data_dir(), LAUNCH_COUNTER_FILENAME)
def _read_launch_count():
try:
with open(_launch_counter_path(), "r", encoding="utf-8") as f:
value = f.read().strip()
return int(value) if value else 0
except (FileNotFoundError, ValueError):
return 0
except Exception:
return 0
def _write_launch_count(count):
try:
os.makedirs(get_app_data_dir(), exist_ok=True)
with open(_launch_counter_path(), "w", encoding="utf-8") as f:
f.write(str(count))
except Exception:
pass
def clear_log_file(log_file=DEFAULT_LOG_FILENAME):
"""Truncate the specified log file."""
path = _resolve_log_path(log_file)
try:
directory = os.path.dirname(path)
if directory:
os.makedirs(directory, exist_ok=True)
with open(path, "w", encoding="utf-8"):
pass
return True
except Exception as exc:
try:
print(f"Failed to clear log file at {path}: {exc}", file=sys.stderr)
except Exception:
pass
return False
def manage_log_retention_on_launch(threshold=3):
"""Increment launch counter and clear logs every `threshold` launches."""
if threshold is None or threshold <= 0:
return False
count = _read_launch_count() + 1
cleared = False
if count >= threshold:
cleared = clear_log_file()
count = 0
_write_launch_count(count)
return cleared
def _find_windows_steam_root():
candidates = []
try:
import winreg
registry_targets = [
(winreg.HKEY_CURRENT_USER, r"Software\\Valve\\Steam", "SteamPath"),
(winreg.HKEY_LOCAL_MACHINE, r"SOFTWARE\\WOW6432Node\\Valve\\Steam", "InstallPath"),
(winreg.HKEY_LOCAL_MACHINE, r"SOFTWARE\\Valve\\Steam", "InstallPath"),
]
for hive, subkey, value in registry_targets:
try:
with winreg.OpenKey(hive, subkey) as key:
location, _ = winreg.QueryValueEx(key, value)
if location:
candidates.append(location)
except (FileNotFoundError, OSError):
continue
except ImportError:
pass
possible_program_files = [
os.environ.get("PROGRAMFILES(X86)"),
os.environ.get("PROGRAMFILES"),
]
for root in possible_program_files:
if root:
candidates.append(os.path.join(root, "Steam"))
candidates.extend([
r"C:\\Program Files (x86)\\Steam",
r"C:\\Program Files\\Steam",
])
for candidate in candidates:
if not candidate:
continue
normalized = os.path.normpath(candidate)
exe_path = os.path.join(normalized, "steam.exe")
if os.path.exists(exe_path):
return normalized
return None
def get_steam_paths():
system = platform.system()
if system == "Windows":
steam_root = _find_windows_steam_root()
if steam_root:
return {
'userdata': os.path.join(steam_root, "userdata"),
'steam_exe': os.path.join(steam_root, "steam.exe"),
}
default_base = os.environ.get("PROGRAMFILES(X86)") or os.environ.get("PROGRAMFILES") or r"C:\\Program Files (x86)"
default_root = os.path.join(default_base, "Steam")
return {
'userdata': os.path.join(default_root, "userdata"),
'steam_exe': os.path.join(default_root, "steam.exe"),
}
if system == "Linux":
home = os.path.expanduser("~")
return {
'userdata': os.path.join(home, ".steam/steam/userdata"),
'steam_exe': "steam",
}
if system == "Darwin":
home = os.path.expanduser("~")
return {
'userdata': os.path.join(home, "Library/Application Support/Steam/userdata"),
'steam_exe': "open",
}
return None
steam_paths = get_steam_paths()
steam_userdata_path = None
steam_exe_path = None
if steam_paths:
steam_userdata_path = steam_paths.get('userdata')
steam_exe_path = steam_paths.get('steam_exe')
else:
write_log("Unsupported operating system", "Error", None)
if platform.system() == "Windows" and steam_exe_path and not os.path.exists(steam_exe_path):
write_log(f"Steam executable not found at {steam_exe_path}. Update your configuration or reinstall Steam.", "Warning", None)
app_id = "311210" # Black Ops III AppID
ENHANCED_TOOL_NAME = "BO3 Enhanced"
ENHANCED_LAUNCH_OPTIONS = 'WINEDLLOVERRIDES="WindowsCodecs=n,b" %command%'
ENHANCED_PROTON_TAG = "release10-32"
ENHANCED_PROTON_ARCHIVE_URL = (
"https://github.com/Weather-OS/GDK-Proton/releases/download/"
f"{ENHANCED_PROTON_TAG}/GDK-Proton10-32.tar.gz"
)
def _normalize_launch_options(launch_options):
if not launch_options:
return ""
return re.sub(r"\s+", " ", launch_options).strip()
def _strip_enhanced_launch_override(launch_options):
cleaned = launch_options or ""
override = r'WINEDLLOVERRIDES="WindowsCodecs=n,b"'
cleaned = re.sub(rf"(?<!\S){override}(?!\S)", "", cleaned, flags=re.IGNORECASE)
return _normalize_launch_options(cleaned)
def find_steam_user_id():
if not steam_userdata_path or not os.path.exists(steam_userdata_path):
write_log("Steam userdata path not found!", "Warning", None)
return None
user_ids = [f for f in os.listdir(steam_userdata_path) if f.isdigit()]
if not user_ids:
write_log("No Steam user ID found!", "Warning", None)
return None
return user_ids[0]
def get_steam_root_path():
if steam_userdata_path:
candidate = os.path.dirname(steam_userdata_path)
if candidate and os.path.isdir(candidate):
return os.path.normpath(candidate)
roots = _candidate_steam_roots()
return roots[0] if roots else None
def _dedupe_existing_dirs(paths):
unique = []
seen = set()
for path in paths:
if not path:
continue
normalized = os.path.normcase(os.path.normpath(os.path.abspath(path)))
if normalized in seen:
continue
seen.add(normalized)
if os.path.isdir(path):
unique.append(os.path.normpath(path))
return unique
def _candidate_steam_roots():
roots = []
if steam_exe_path and os.path.isabs(steam_exe_path):
roots.append(os.path.dirname(steam_exe_path))
if steam_userdata_path:
roots.append(os.path.dirname(steam_userdata_path))
system = platform.system()
home = os.path.expanduser("~")
if system == "Linux":
roots.extend([
os.path.join(home, ".steam", "steam"),
os.path.join(home, ".local", "share", "Steam"),
])
elif system == "Windows":
detected_root = _find_windows_steam_root()
if detected_root:
roots.append(detected_root)
elif system == "Darwin":
roots.append(os.path.join(home, "Library", "Application Support", "Steam"))
return _dedupe_existing_dirs(roots)
def _extract_library_paths_from_vdf(library_vdf_path):
if not library_vdf_path or not os.path.exists(library_vdf_path):
return []
try:
with open(library_vdf_path, "r", encoding="utf-8") as handle:
data = vdf.load(handle)
except Exception:
return []
libraryfolders = data.get("libraryfolders")
if not isinstance(libraryfolders, dict):
return []
libraries = []
for key, value in libraryfolders.items():
if key == "contentstatsid":
continue
if isinstance(value, str):
libraries.append(value)
continue
if isinstance(value, dict):
path_value = value.get("path")
if isinstance(path_value, str):
libraries.append(path_value)
return _dedupe_existing_dirs(libraries)
def get_steam_library_paths():
global _steam_library_paths_cache
roots = _candidate_steam_roots()
cache_key = []
for root in roots:
library_vdf = os.path.join(root, "steamapps", "libraryfolders.vdf")
try:
mtime = os.path.getmtime(library_vdf)
except OSError:
mtime = None
cache_key.append((os.path.normcase(os.path.normpath(root)), mtime))
cache_key = tuple(cache_key)
if _steam_library_paths_cache and _steam_library_paths_cache[0] == cache_key:
return list(_steam_library_paths_cache[1])
libraries = []
for root in roots:
libraries.append(root)
library_vdf = os.path.join(root, "steamapps", "libraryfolders.vdf")
libraries.extend(_extract_library_paths_from_vdf(library_vdf))
resolved = _dedupe_existing_dirs(libraries)
_steam_library_paths_cache = (cache_key, list(resolved))
return resolved
def _workshop_item_installed_in_library(steam_library, game_app_id, workshop_id):
install_dir = os.path.join(
steam_library,
"steamapps",
"workshop",
"content",
str(game_app_id),
str(workshop_id),
)
if not os.path.isdir(install_dir):
return False, None
try:
has_files = any(os.scandir(install_dir))
except OSError:
has_files = False
return has_files, install_dir
def _contains_workshop_id(value, workshop_id):
if isinstance(value, dict):
if str(workshop_id) in value:
return True
return any(_contains_workshop_id(item, workshop_id) for item in value.values())
if isinstance(value, list):
return any(_contains_workshop_id(item, workshop_id) for item in value)
return str(value) == str(workshop_id)
def _workshop_item_subscribed_in_library(steam_library, game_app_id, workshop_id):
acf_path = os.path.join(
steam_library,
"steamapps",
"workshop",
f"appworkshop_{game_app_id}.acf",
)
if not os.path.exists(acf_path):
return False
try:
with open(acf_path, "r", encoding="utf-8") as handle:
data = vdf.load(handle)
except Exception:
return False
workshop_data = data.get("AppWorkshop", {})
if not isinstance(workshop_data, dict):
return False
return _contains_workshop_id(workshop_data, workshop_id)
def get_workshop_item_state(game_app_id, workshop_id):
"""Return the Steam Workshop state for a BO3 workshop item."""
workshop_id = str(workshop_id)
game_app_id = str(game_app_id)
subscribed = False
for steam_library in get_steam_library_paths():
installed, install_dir = _workshop_item_installed_in_library(steam_library, game_app_id, workshop_id)
if installed:
return {
"state": "Installed",
"installed": True,
"subscribed": True,
"path": install_dir,
}
if _workshop_item_subscribed_in_library(steam_library, game_app_id, workshop_id):
subscribed = True
if subscribed:
return {
"state": "Subscribed (not installed yet)",
"installed": False,
"subscribed": True,
"path": None,
}
return {
"state": "Not Subscribed",
"installed": False,
"subscribed": False,
"path": None,
}
def get_backup_locations():
user_backup_dir = os.path.join(get_app_data_dir(), "backups")
module_backup_dir = os.path.join(os.path.dirname(os.path.abspath(__file__)), "backups")
locations = [user_backup_dir]
if os.path.normpath(module_backup_dir) != os.path.normpath(user_backup_dir):
locations.append(module_backup_dir)
return locations
def _backup_file(config_path, backup_filename, log_widget):
backup_dirs = get_backup_locations()
primary_dir = backup_dirs[0]
try:
os.makedirs(primary_dir, exist_ok=True)
except Exception as e:
write_log(f"Failed to prepare backup directory '{primary_dir}': {e}", "Error", log_widget)
return False
backup_file_path = os.path.join(primary_dir, backup_filename)
try:
shutil.copy2(config_path, backup_file_path)
write_log(f"Config backup created at {backup_file_path}", "Success", log_widget)
return True
except Exception as e:
write_log(f"Failed to create backup: {e}", "Error", log_widget)
return False
def _restore_file(config_path, backup_filename, log_widget):
for directory in get_backup_locations():
backup_file_path = os.path.join(directory, backup_filename)
if not os.path.exists(backup_file_path):
continue
try:
shutil.copy2(backup_file_path, config_path)
write_log("Config restored from backup", "Success", log_widget)
return True
except Exception as e:
write_log(f"Failed to restore backup from {backup_file_path}: {e}", "Error", log_widget)
return False
write_log("No backup file found", "Warning", log_widget)
return False
def backup_config_file(config_path, log_widget):
return _backup_file(config_path, "localconfig_backup.vdf", log_widget)
def restore_config_file(config_path, log_widget):
return _restore_file(config_path, "localconfig_backup.vdf", log_widget)
def _linux_running_pids(process_name):
try:
result = subprocess.run(
["pgrep", "-x", process_name],
capture_output=True,
text=True,
check=False,
timeout=5,
)
except (subprocess.SubprocessError, OSError):
return []
if result.returncode != 0:
return []
pids = []
for line in (result.stdout or "").splitlines():
line = line.strip()
if not line.isdigit():
continue
pid = line
try:
stat = subprocess.run(
["ps", "-o", "stat=", "-p", pid],
capture_output=True,
text=True,
check=False,
timeout=5,
)
except (subprocess.SubprocessError, OSError):
continue
if stat.returncode != 0:
continue
state = (stat.stdout or "").strip()
# Exclude zombie/dead processes to avoid false positives.
if not state or "Z" in state or "X" in state:
continue
pids.append(int(pid))
return pids
def _is_linux_process_running(process_name):
return bool(_linux_running_pids(process_name))
def is_steam_running():
system = platform.system()
try:
if system == "Windows":
result = subprocess.run(
["tasklist", "/FI", "IMAGENAME eq steam.exe"],
capture_output=True,
text=True,
timeout=5
)
output = (result.stdout or "").lower()
return "steam.exe" in output
return _is_linux_process_running("steam")
except (subprocess.SubprocessError, OSError):
return False
def close_steam(log_widget):
system = platform.system()
try:
if system == "Windows":
result = subprocess.run(
["taskkill", "/F", "/IM", "steam.exe"],
check=False,
timeout=10,
capture_output=True,
text=True
)
if result.returncode != 0:
if not is_steam_running():
write_log("Steam was not running.", "Info", log_widget)
else:
write_log(f"Failed to close Steam: {result.stderr.strip() or result.stdout.strip()}", "Error", log_widget)
return
elif system == "Linux":
result = subprocess.run(
["pkill", "-x", "steam"],
check=False,
timeout=10,
stdout=subprocess.DEVNULL,
stderr=subprocess.DEVNULL
)
if result.returncode == 1:
write_log("Steam was not running.", "Info", log_widget)
elif result.returncode != 0:
write_log(f"Failed to close Steam (return code {result.returncode}).", "Error", log_widget)
return
time.sleep(5)
except subprocess.TimeoutExpired:
write_log("Timed out while attempting to close Steam.", "Warning", log_widget)
except FileNotFoundError as e:
write_log(f"Steam control command not found: {e}", "Error", log_widget)
except subprocess.SubprocessError as e:
write_log(f"Failed to close Steam: {e}", "Error", log_widget)
def open_steam(log_widget):
system = platform.system()
try:
if system == "Windows":
write_log("Opening Steam...", "Info", log_widget)
if steam_exe_path and os.path.exists(steam_exe_path):
subprocess.Popen([steam_exe_path, "-silent"],
stdout=subprocess.DEVNULL,
stderr=subprocess.DEVNULL)
else:
write_log("Steam executable path not found; attempting to use system URI handler.", "Warning", log_widget)
try:
os.startfile("steam://open/main") # type: ignore[attr-defined]
except AttributeError:
subprocess.Popen(["cmd", "/c", "start", "", "steam://"])
else:
# Check if Steam is already running
was_running = _is_linux_process_running("steam")
if was_running:
write_log("Closing Steam...", "Info", log_widget)
subprocess.run(
["pkill", "-x", "steam"],
check=False,
stdout=subprocess.DEVNULL,
stderr=subprocess.DEVNULL,
timeout=5,
)
time.sleep(2) # Allow time for Steam to shut down
write_log("Setting launch options...", "Info", log_widget)
# (Place here any code that sets your launch options.)
write_log("Opening Steam...", "Info", log_widget)
steam_cmd = None
if steam_exe_path:
if os.path.isabs(steam_exe_path) and os.path.exists(steam_exe_path):
steam_cmd = steam_exe_path
else:
steam_cmd = shutil.which(steam_exe_path)
if not steam_cmd:
steam_cmd = shutil.which("steam")
launched = False
launch_errors = []
if steam_cmd:
try:
subprocess.Popen(
[steam_cmd, "-silent"],
stdout=subprocess.DEVNULL,
stderr=subprocess.DEVNULL,
)
launched = True
except Exception as exc:
launch_errors.append(f"{steam_cmd} -silent failed: {exc}")
if not launched:
xdg_open = shutil.which("xdg-open")
if xdg_open:
try:
result = subprocess.run(
[xdg_open, "steam://open/main"],
check=False,
stdout=subprocess.DEVNULL,
stderr=subprocess.DEVNULL,
timeout=10,
)
if result.returncode == 0:
launched = True
else:
launch_errors.append(f"{xdg_open} exited with code {result.returncode}")
except Exception as exc:
launch_errors.append(f"{xdg_open} failed: {exc}")
if not launched:
detail = "; ".join(launch_errors) if launch_errors else "no launch command available"
write_log(f"Steam launch command failed: {detail}", "Error", log_widget)
def steam_running():
if system == "Windows":
try:
result = subprocess.run(["tasklist", "/FI", "IMAGENAME eq steam.exe"],
capture_output=True,
text=True,
timeout=5)
except (subprocess.SubprocessError, OSError):
return False
output = (result.stdout or "").lower()
return "steam.exe" in output
return _is_linux_process_running("steam")
max_wait = 15 # maximum total wait time (seconds)
poll_interval = 0.5 # poll every 0.5 seconds
stable_duration = 2 # require Steam to be present for 2 consecutive seconds
elapsed = 0
stable_time = 0
while elapsed < max_wait:
if steam_running():
stable_time += poll_interval
if stable_time >= stable_duration:
break
else:
stable_time = 0
time.sleep(poll_interval)
elapsed += poll_interval
if stable_time >= stable_duration and steam_running():
write_log("Steam launched successfully", "Success", log_widget)
else:
level = "Warning" if system == "Windows" else "Error"
write_log("Steam did not launch successfully", level, log_widget)
except Exception as e:
write_log(f"Failed to open Steam: {e}", "Error", log_widget)
write_log("Please start Steam manually", "Info", log_widget)
def launch_game_via_steam(app_id, log_widget=None):
uri = f"steam://rungameid/{app_id}"
system = platform.system()
last_error = None
try:
if system == "Windows":
if steam_exe_path and os.path.exists(steam_exe_path):
subprocess.Popen(
[steam_exe_path, "-applaunch", str(app_id)],
stdout=subprocess.DEVNULL,
stderr=subprocess.DEVNULL
)
else:
write_log("Steam executable path not found; attempting to use system URI handler.", "Warning", log_widget)
try:
os.startfile(uri) # type: ignore[attr-defined]
except AttributeError:
subprocess.Popen(["cmd", "/c", "start", "", uri])
elif system == "Linux":
commands = []
steam_cmd = None
if steam_exe_path:
if os.path.isabs(steam_exe_path) and os.path.exists(steam_exe_path):
steam_cmd = steam_exe_path
else:
steam_cmd = shutil.which(steam_exe_path)
if not steam_cmd:
steam_cmd = shutil.which("steam")
if steam_cmd:
commands.append(
{
"cmd": [steam_cmd, "-applaunch", str(app_id)],
"description": f"{os.path.basename(steam_cmd)} -applaunch"
}
)
xdg = shutil.which("xdg-open")
if xdg:
commands.append(
{
"cmd": [xdg, uri],
"description": "xdg-open steam URI",
"check": True
}
)
# Fallback if nothing else is available
if not commands:
commands.append(
{
"cmd": [steam_exe_path or "steam", uri],
"description": "steam URI fallback"
}
)
for entry in commands:
try:
if entry.get("check"):
result = subprocess.run(
entry["cmd"],
check=False,
stdout=subprocess.DEVNULL,
stderr=subprocess.DEVNULL
)
if result.returncode != 0:
last_error = f"{entry['cmd'][0]} exited with code {result.returncode}"
continue
else:
subprocess.Popen(
entry["cmd"],
stdout=subprocess.DEVNULL,
stderr=subprocess.DEVNULL
)
write_log(
f"Launched Black Ops III via Steam (AppID: {app_id}) using {entry['description']}",
"Success",
log_widget
)
return
except FileNotFoundError:
last_error = f"{entry['cmd'][0]} not found"
continue
except Exception as exc:
last_error = str(exc)
continue
raise RuntimeError(last_error or "No suitable launcher command found")
elif system == "Darwin":
subprocess.Popen(["open", uri])
else:
subprocess.Popen([steam_exe_path or "steam", uri])
write_log(f"Launched Black Ops III via Steam (AppID: {app_id})", "Success", log_widget)
except FileNotFoundError:
write_log("Steam client not found. Please verify your Steam installation path.", "Error", log_widget)
except Exception as exc:
write_log(f"Error launching game via Steam: {exc}", "Error", log_widget)
def set_launch_options(user_id, app_id, launch_options, log_widget, preserve_fs_game=False):
config_path = os.path.join(steam_userdata_path, user_id, "config", "localconfig.vdf")
if not os.path.exists(config_path):
write_log("localconfig.vdf not found!", "Error", log_widget)
return False
if not backup_config_file(config_path, log_widget):
write_log("Aborting due to backup failure", "Error", log_widget)
return False
try:
with open(config_path, "r", encoding="utf-8") as file:
data = vdf.load(file)
# Navigate to the apps section
steam_config = data.setdefault("UserLocalConfigStore", {})
software = steam_config.setdefault("Software", {})
valve = software.setdefault("Valve", {})
steam = valve.setdefault("Steam", {})
apps = steam.setdefault("apps", {})
app_entry = apps.setdefault(app_id, {})
current_options = app_entry.get("LaunchOptions", "")
requested_options = launch_options or ""
# Parse existing options
wine_override = 'WINEDLLOVERRIDES="dsound=n,b"'
command_marker = "%command%"
fs_game_pattern = r'\+set\s+fs_game\s+\S+'
def strip_token(option_str, token):
if not option_str or not token:
return option_str
return re.sub(rf'(?<!\S){re.escape(token)}(?!\S)', '', option_str)
def normalize(option_str):
if not option_str:
return ''
return re.sub(r'\s+', ' ', option_str).strip()
existing_fs_games = re.findall(fs_game_pattern, current_options) if current_options else []
new_fs_games = re.findall(fs_game_pattern, requested_options)
# Keep existing fs_game entries when requested and no new ones are provided
fs_game_entries = new_fs_games or (existing_fs_games if preserve_fs_game else [])
# Always strip fs_game from the strings we merge, then append the chosen entries
cleaned_current = re.sub(fs_game_pattern, '', current_options).strip()
cleaned_launch = re.sub(fs_game_pattern, '', requested_options).strip()
has_current_wine = wine_override in cleaned_current
has_current_command = command_marker in cleaned_current
has_new_wine = wine_override in requested_options
has_new_command = command_marker in requested_options
cleaned_current = strip_token(strip_token(cleaned_current, wine_override), command_marker)
cleaned_launch = strip_token(strip_token(cleaned_launch, wine_override), command_marker)
include_wine = has_new_wine or has_current_wine
include_command = has_new_command or has_current_command
if include_wine and not include_command:
include_command = True
segments = []
if include_wine:
segments.append(wine_override)
if include_command:
segments.append(command_marker)
if cleaned_current:
segments.append(normalize(cleaned_current))
if cleaned_launch:
segments.append(normalize(cleaned_launch))
if fs_game_entries:
unique_fs_games = []
for entry in fs_game_entries:
if entry not in unique_fs_games:
unique_fs_games.append(entry)
segments.append(normalize(' '.join(unique_fs_games)))
final_options = normalize(' '.join(segments))
app_entry["LaunchOptions"] = final_options
write_log(f"Setting launch options to: {final_options}", "Info", log_widget)
with open(config_path, "w", encoding="utf-8") as file:
vdf.dump(data, file, pretty=True)
return True
except Exception as e:
write_log(f"Error updating launch options: {e}", "Error", log_widget)
restore_config_file(config_path, log_widget)
return False
def set_launch_options_exact(user_id, app_id, launch_options, log_widget):
config_path = os.path.join(steam_userdata_path, user_id, "config", "localconfig.vdf")
if not os.path.exists(config_path):
write_log("localconfig.vdf not found!", "Error", log_widget)
return False
if not backup_config_file(config_path, log_widget):
write_log("Aborting due to backup failure", "Error", log_widget)
return False
try:
with open(config_path, "r", encoding="utf-8") as file:
data = vdf.load(file)
steam_config = data.setdefault("UserLocalConfigStore", {})
software = steam_config.setdefault("Software", {})
valve = software.setdefault("Valve", {})
steam = valve.setdefault("Steam", {})
apps = steam.setdefault("apps", {})
app_entry = apps.setdefault(app_id, {})
app_entry["LaunchOptions"] = launch_options or ""
write_log(f"Setting launch options to: {app_entry['LaunchOptions']}", "Info", log_widget)
with open(config_path, "w", encoding="utf-8") as file:
vdf.dump(data, file, pretty=True)
return True
except Exception as e:
write_log(f"Error updating launch options: {e}", "Error", log_widget)
restore_config_file(config_path, log_widget)
return False
def _read_launch_options(user_id, game_app_id):
config_path = os.path.join(steam_userdata_path, user_id, "config", "localconfig.vdf")
if not os.path.exists(config_path):
return None
try:
with open(config_path, "r", encoding="utf-8") as file:
data = vdf.load(file)
return (
data.get("UserLocalConfigStore", {})
.get("Software", {})
.get("Valve", {})
.get("Steam", {})
.get("apps", {})
.get(str(game_app_id), {})