Repository navigation
Expand file tree
/
Copy pathmain.py
More file actions
executable file
·1636 lines (1481 loc) · 72.8 KB
/
Copy pathmain.py
File metadata and controls
executable file
·1636 lines (1481 loc) · 72.8 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 asyncio
import os
import json
import subprocess
import sys
import time
from aiohttp import web
import shlex
import decky_plugin
import zipfile
import shutil
import aiohttp
import os
import concurrent.futures
# Decky registers its OWN `updater` module in sys.modules → a plain
# `import updater` returns Decky's (without is_autoupdate_enabled) instead of
# ours, silently breaking auto-update after a Decky update. Load our file
# explicitly by path, under a unique name, to avoid the collision.
import importlib.util as _ilu
_uspec = _ilu.spec_from_file_location(
"skullkey_updater", os.path.join(os.path.dirname(os.path.abspath(__file__)), "updater.py")
)
updater = _ilu.module_from_spec(_uspec)
_uspec.loader.exec_module(updater)
_easpec = _ilu.spec_from_file_location(
"skullkey_ea_browser", os.path.join(os.path.dirname(os.path.abspath(__file__)), "scripts", "skullkey_ea_browser.py")
)
_ea_browser = _ilu.module_from_spec(_easpec)
_easpec.loader.exec_module(_ea_browser)
# Xbox sign-in (Steam browser) imports its sibling modules from scripts/.
_scripts_dir = os.path.join(os.path.dirname(os.path.abspath(__file__)), "scripts")
if _scripts_dir not in sys.path:
sys.path.append(_scripts_dir)
_xbspec = _ilu.spec_from_file_location("skullkey_xbox_login", os.path.join(_scripts_dir, "xbox_login.py"))
_xbox_login = _ilu.module_from_spec(_xbspec)
_xbspec.loader.exec_module(_xbox_login)
_xkspec = _ilu.spec_from_file_location("skullkey_xbox_keyring", os.path.join(_scripts_dir, "xbox_keyring.py"))
_xbox_keyring = _ilu.module_from_spec(_xkspec)
_xkspec.loader.exec_module(_xbox_keyring)
# Background tasks started by _main, kept here for two reasons: asyncio only
# holds a weak reference to a running task, so one with no reference left can be
# collected mid-flight, and _unload needs to know which tasks are *ours* rather
# than cancelling everything the loop happens to be running.
_bg_tasks: set = set()
def _spawn(coro):
task = asyncio.create_task(coro)
_bg_tasks.add(task)
task.add_done_callback(_bg_tasks.discard)
return task
# How many release checks, and how long between two. The check runs a few
# seconds after the backend, which is often BEFORE the network is reachable:
# the logs on the test machine show three boots out of four dying on
# "Temporary failure in name resolution". Nothing retried, so the plugin stayed
# on its version until the next boot — which failed the same way.
UPDATE_CHECK_TRIES = 10
UPDATE_CHECK_DELAY_S = 30
async def _recheck(updater):
"""`updater.check()`, retried for as long as it is the network that is missing."""
from asyncio import sleep as _sleep
info = await updater.check()
for _ in range(UPDATE_CHECK_TRIES - 1):
if not info.get("error"):
break
await _sleep(UPDATE_CHECK_DELAY_S)
info = await updater.check()
return info
async def _auto_update_check():
"""Silent release-based auto-update, a little after startup so the plugin
is fully usable first. Module-level (no `self`) because decky's dispatch
doesn't bind an instance to a task spawned from inside _main."""
try:
await asyncio.sleep(20)
if not updater.is_autoupdate_enabled():
return
info = await _recheck(updater)
if not info.get("update_available"):
return
decky_plugin.logger.info(
f"[updater] {info['latest']} available (have {info['current']}); applying"
)
# apply() returns a dict: {"ok": False, "error": …} is always truthy,
# so a failure used to pass for a success and the loader was restarted
# anyway — on a loop, since the installed version had not changed.
# Read the field, not the truthiness of the dict.
# We apply it OURSELVES. The plugin directory is root-owned, but every
# file inside it belongs to us (except plugin.json) — measured on
# 2026-09-13: overwriting an existing file works, creating an entry does
# not. The updater now sorts that out BEFORE writing anything.
#
# ⛔ Do NOT delegate to `utilities/install_plugin`: that is the Decky
# Store route, and it reports the install to plugins.deckbrew.xyz. Our
# plugins are not there → 404 → the rest never runs: files written,
# plugin never reloaded, and a frozen modal across the Steam UI.
# Measured here on 2026-09-13.
global _PENDING_UPDATE
res = await updater.apply(info["url"])
if res.get("ok"):
if updater.restart_loader():
return
# Refused: plugin_loader is a system unit and this plugin is not
# root, so polkit asks for an authentication nobody can give here
# (measured 2026-09-22). The files are in place; the code is not.
decky_plugin.logger.info(f"[updater] {info['latest']} written to disk; loader restart "
"refused (plugin is not root) — active at next Steam start")
_PENDING_UPDATE = {"version": info["latest"], "reload": True}
return
# Failed: say so, instead of leaving someone on a stale version without
# knowing it. The frontend does the telling — it is the only side that
# can raise a notification.
decky_plugin.logger.error(
f"[updater] update aborted: {res.get('error', 'unknown reason')}"
)
_PENDING_UPDATE = {"version": info["latest"], "error": res.get("error", "")}
except Exception as e:
decky_plugin.logger.error(f"[updater] auto-check error: {e}")
# Failure notice parked by _auto_update_check, taken by the frontend that notifies.
_PENDING_UPDATE = None
async def _ensure_deps():
"""Boot-time self-heal: install any MISSING GOG/Amazon store deps (small,
self-contained venvs) so a fresh machine is ready without the user pressing
"Install dependencies". Detached from Helper.lock so a first-run install
never blocks the UI; the presence check is a cheap `test -x`, so on every
later boot this is a quick no-op. Epic's heavier flatpak deps stay manual."""
try:
await asyncio.sleep(30) # let the plugin finish coming up first
log_path = os.path.join(decky_plugin.DECKY_PLUGIN_LOG_DIR, "ensure_deps.log")
with open(log_path, "a") as log:
proc = await asyncio.create_subprocess_shell(
"./scripts/install_deps.sh ensure",
stdout=log,
stderr=log,
stdin=asyncio.subprocess.DEVNULL,
cwd=Helper.working_directory,
env=Helper.get_environment(),
start_new_session=True,
)
await proc.wait()
if proc.returncode == 0:
decky_plugin.logger.info("[deps] ensure finished successfully")
else:
decky_plugin.logger.error(
f"[deps] ensure failed with exit code {proc.returncode}; "
f"see {log_path}"
)
except Exception as e:
decky_plugin.logger.error(f"[deps] ensure error: {e}")
async def _resume_downloads():
"""Boot-time resume: respawn miHoYo install workers that a reboot/crash
interrupted mid-download. The worker restarts from the already-finished
segments and the idempotence guard makes this a no-op for completed
installs, so it is always safe."""
try:
await asyncio.sleep(35) # let the plugin finish coming up first
log_path = os.path.join(decky_plugin.DECKY_PLUGIN_LOG_DIR,
"resume_downloads.log")
with open(log_path, "a") as log:
proc = await asyncio.create_subprocess_shell(
"python3 ./scripts/Extensions/MiHoYo/mihoyo.py resume-pending",
stdout=log,
stderr=log,
stdin=asyncio.subprocess.DEVNULL,
cwd=Helper.working_directory,
env=Helper.get_environment(),
start_new_session=True,
)
await proc.wait()
decky_plugin.logger.info("[resume] pending-downloads check finished")
except Exception as e:
decky_plugin.logger.error(f"[resume] error: {e}")
async def _games_autoupdate():
"""Unattended game updates for every store, at most once a day, without
the user ever opening the plugin UI. The orchestrator
(scripts/autoupdate_games.py) does per-store detection and dispatches the
same Update actions the UI uses."""
stamp_path = os.path.join(decky_plugin.DECKY_PLUGIN_RUNTIME_DIR,
"autoupdate_games.json")
try:
await asyncio.sleep(180) # after boot tasks (deps/resume/update)
while True:
last = 0
try:
with open(stamp_path) as f:
last = json.load(f).get("last", 0)
except Exception:
pass
if time.time() - last >= 86400:
log_path = os.path.join(decky_plugin.DECKY_PLUGIN_LOG_DIR,
"autoupdate_games.log")
with open(log_path, "a") as log:
proc = await asyncio.create_subprocess_shell(
"python3 ./scripts/autoupdate_games.py",
stdout=log,
stderr=log,
stdin=asyncio.subprocess.DEVNULL,
cwd=Helper.working_directory,
env=Helper.get_environment(),
start_new_session=True,
)
await proc.wait()
with open(stamp_path, "w") as f:
json.dump({"last": time.time()}, f)
decky_plugin.logger.info("[gamesupd] daily run finished")
await asyncio.sleep(6 * 3600) # re-check a few times a day
except Exception as e:
decky_plugin.logger.error(f"[gamesupd] error: {e}")
class Helper:
websocket_port = 8765
action_cache = {}
working_directory = decky_plugin.DECKY_PLUGIN_RUNTIME_DIR
ws_loop = None
app = None
site = None
runner = None
wsServerIsRunning = False
verbose = False
lock = asyncio.Lock()
@staticmethod
async def pyexec_subprocess(
cmd: str,
input: str = "",
unprivilege: bool = False,
env=None,
websocket=None,
stream_output: bool = False,
app_id="",
game_id="",
):
decky_plugin.logger.info(f"creating lock")
async with Helper.lock:
try:
decky_plugin.logger.info(f"inside lock")
if unprivilege:
cmd = f"sudo -u {decky_plugin.DECKY_USER} {cmd}"
decky_plugin.logger.info(f"running cmd: {cmd}")
if env is None:
env = Helper.get_environment()
env["APP_ID"] = app_id
env["SteamOverlayGameId"] = game_id
env["SteamGameId"] = game_id
proc = await asyncio.create_subprocess_shell(
cmd,
stdout=asyncio.subprocess.PIPE,
stderr=asyncio.subprocess.PIPE,
stdin=asyncio.subprocess.PIPE,
shell=True,
env=env,
cwd=Helper.working_directory,
start_new_session=True,
)
if stream_output:
async def read_stream(stream, stream_type):
while True:
line = await stream.readline()
if line:
line = line.decode()
if stream_output:
await websocket.send_str(
json.dumps(
{
"status": "open",
"data": line,
"type": stream_type,
}
)
)
else:
break
await asyncio.gather(
read_stream(proc.stdout, "stdout"),
read_stream(proc.stderr, "stderr"),
)
await proc.wait()
await websocket.send_str(
json.dumps({"status": "closed", "data": ""})
)
return {"returncode": proc.returncode}
else:
try:
stdout, stderr = await proc.communicate(input.encode())
stdout = stdout.decode()
stderr = stderr.decode()
if Helper.verbose:
decky_plugin.logger.info(
f"Returncode: {proc.returncode}\nSTDOUT: {stdout[:300]}\nSTDERR: {stderr[:300]}"
)
return {
"returncode": proc.returncode,
"stdout": stdout,
"stderr": stderr,
}
finally:
# Ensure process is terminated and cleaned up
if proc.returncode is None:
try:
proc.terminate()
await asyncio.wait_for(proc.wait(), timeout=5.0)
except asyncio.TimeoutError:
proc.kill()
await proc.wait()
except Exception:
pass
except Exception as e:
decky_plugin.logger.error(f"Error in pyexec_subprocess: {e}")
# Clean up process on error
try:
if "proc" in locals() and proc.returncode is None:
proc.terminate()
await asyncio.wait_for(proc.wait(), timeout=5.0)
except Exception:
if "proc" in locals():
try:
proc.kill()
await proc.wait()
except Exception:
pass
return None
@staticmethod
def get_environment(platform=""):
env = {
"DECKY_HOME": decky_plugin.DECKY_HOME,
"DECKY_PLUGIN_DIR": decky_plugin.DECKY_PLUGIN_DIR,
"DECKY_PLUGIN_LOG_DIR": decky_plugin.DECKY_PLUGIN_LOG_DIR,
"DECKY_PLUGIN_NAME": "skullkey",
"DECKY_PLUGIN_RUNTIME_DIR": decky_plugin.DECKY_PLUGIN_RUNTIME_DIR,
"DECKY_PLUGIN_SETTINGS_DIR": decky_plugin.DECKY_PLUGIN_SETTINGS_DIR,
"WORKING_DIR": Helper.working_directory,
"CONTENT_SERVER": "http://localhost:1337/plugins",
"DECKY_USER_HOME": decky_plugin.DECKY_USER_HOME,
"HOME": os.path.abspath(decky_plugin.DECKY_USER_HOME),
"PLATFORM": platform,
}
# Drive picked in the install dialog, only for the Download action
# that follows it (see execute_action).
if Helper.active_install_root:
env["SK_INSTALL_ROOT"] = Helper.active_install_root
return env
pending_install_root = ""
active_install_root = ""
@staticmethod
async def call_script(cmd: str, *args, input_data="", app_id="", game_id=""):
try:
decky_plugin.logger.info(f"call_script: {cmd} {args} {input_data}")
encoded_args = [shlex.quote(arg) for arg in args]
decky_plugin.logger.info(f"call_script: {cmd} {' '.join(encoded_args)}")
decky_plugin.logger.info(f"input_data: {input_data}")
decky_plugin.logger.info(f"args: {args}")
cmd = f"{cmd} {' '.join(encoded_args)}"
res = await Helper.pyexec_subprocess(
cmd, input_data, app_id=app_id, game_id=game_id
)
if Helper.verbose:
decky_plugin.logger.info(f"call_script result: {res['stdout'][:100]}")
return res["stdout"]
except Exception as e:
decky_plugin.logger.error(f"Error in call_script: {e}")
return None
@staticmethod
def get_action(actionSet, actionName):
result = None
if set := Helper.action_cache.get(actionSet):
for action in set:
if action["Id"] == actionName:
result = action
if not result:
file_path = os.path.join(Helper.working_directory, f"{actionSet}.json")
if not os.path.exists(file_path):
file_path = os.path.join(
decky_plugin.DECKY_PLUGIN_RUNTIME_DIR, ".cache", f"{actionSet}.json"
)
if os.path.exists(file_path):
with open(file_path) as f:
data = json.load(f)
for action in data:
if action["Id"] == actionName:
result = action
if not result:
# A game page opened straight from the Steam library after a
# plugin reload: its tab was never opened, so load the set.
actions = Helper.load_static_action_set(actionSet)
if actions:
Helper.action_cache[actionSet] = actions
result = next((a for a in actions if a.get("Id") == actionName), None)
return result
@staticmethod
def load_static_action_set(actionSet):
"""XboxActions -> get-json.py xbox-actions (the static.json key);
MainMenu -> main-menu-actions."""
if actionSet == "MainMenu":
key = "main-menu-actions"
elif actionSet.endswith("Actions") and actionSet[:-7].isalnum():
key = actionSet[:-7].lower() + "-actions"
else:
return None
try:
# Decky is a PyInstaller binary: its LD_LIBRARY_PATH breaks the
# system Python, so run it with the original library path.
env = dict(os.environ)
orig = env.pop("LD_LIBRARY_PATH_ORIG", None)
if orig is not None:
env["LD_LIBRARY_PATH"] = orig
else:
env.pop("LD_LIBRARY_PATH", None)
out = subprocess.run(["/usr/bin/python3", os.path.join(Helper.working_directory, "scripts", "get-json.py"), key],
cwd=Helper.working_directory, capture_output=True, text=True, timeout=15, env=env)
data = json.loads(out.stdout or "{}")
except Exception:
return None
content = data.get("Content") if isinstance(data, dict) else None
if data.get("Type") != "ActionSet" or not isinstance(content, dict) or content.get("SetName") != actionSet:
return None
return content.get("Actions") or None
@staticmethod
async def execute_action(
actionSet, actionName, *args, input_data="", app_id="", game_id=""
):
try:
result = ""
json_result = {}
action = Helper.get_action(actionSet, actionName)
cmd = action["Command"]
if cmd:
decky_plugin.logger.info(f"execute_action cmd: {cmd}")
decky_plugin.logger.info(f"execute_action args: {args}")
decky_plugin.logger.info(f"execute_action app_id: {app_id}")
decky_plugin.logger.info(f"execute_action game_id: {game_id}")
decky_plugin.logger.info(f"execute_action input_data: {input_data}")
result = await Helper.call_script(
os.path.expanduser(cmd),
*args,
input_data=input_data,
app_id=app_id,
game_id=game_id,
)
if Helper.verbose:
decky_plugin.logger.info(f"execute_action result: {result}")
try:
json_result = json.loads(result)
if json_result["Type"] == "ActionSet":
decky_plugin.logger.info(
f"Init action set {json_result['Content']['SetName']}"
)
Helper.write_action_set_to_cache(
json_result["Content"]["SetName"],
json_result["Content"]["Actions"],
)
except Exception as e:
decky_plugin.logger.info("Error parsing json result", e)
json_result = {
"Type": "Error",
"Content": {
"Message": f"Error parsing json result {e}",
"Data": result,
"ActionName": actionName,
"ActionSet": actionSet,
},
}
return json_result
return {
"Type": "Error",
"Content": {
"Message": f"Action not found {actionSet}, {actionName}",
"Data": result[:300],
},
"ActionName": actionName,
"ActionSet": actionSet,
}
except Exception as e:
decky_plugin.logger.error(f"Error executing action: {e}")
return {
"Type": "Error",
"Content": {
"Message": "Action not found",
"Data": str(e),
"ActionName": actionName,
"ActionSet": actionSet,
},
}
@staticmethod
def write_action_set_to_cache(setName, actionSet, writeToDisk: bool = False):
# Prevent cache from growing unbounded - limit to 100 entries
if len(Helper.action_cache) > 100:
# Remove oldest entries (FIFO)
oldest_keys = list(Helper.action_cache.keys())[:50]
for key in oldest_keys:
del Helper.action_cache[key]
Helper.action_cache[setName] = actionSet
if writeToDisk:
cache_dir = os.path.join(decky_plugin.DECKY_PLUGIN_RUNTIME_DIR, ".cache")
if not os.path.exists(cache_dir):
os.makedirs(cache_dir)
file_path = os.path.join(cache_dir, f"{setName}.json")
# if not os.path.exists(file_path):
with open(file_path, "w") as f:
json.dump(actionSet, f)
@staticmethod
async def ws_handler(request):
websocket = web.WebSocketResponse()
await websocket.prepare(request)
try:
async for message in websocket:
decky_plugin.logger.info(f"ws_handler message: {message.data}")
data = json.loads(message.data)
if data["action"] == "install_dependencies":
await Helper.pyexec_subprocess(
"./scripts/install_deps.sh",
websocket=websocket,
stream_output=True,
)
if data["action"] == "uninstall_dependencies":
await Helper.pyexec_subprocess(
"./scripts/install_deps.sh uninstall",
websocket=websocket,
stream_output=True,
)
except Exception as e:
decky_plugin.logger.error(f"Error in ws_handler: {e}")
finally:
# Ensure websocket is properly closed
if not websocket.closed:
await websocket.close()
return websocket
async def start_ws_server():
Helper.ws_loop = asyncio.get_event_loop()
# Don't use ThreadPoolExecutor for async tasks - just call directly
await Helper._start_ws_server_thread()
@staticmethod
async def _start_ws_server_thread():
try:
Helper.wsServerIsRunning = True
port = 8765
while Helper.wsServerIsRunning:
try:
decky_plugin.logger.info(
f"Starting WebSocket server on port {port}"
)
# Helper.runner.setup()
Helper.app = web.Application()
Helper.app.router.add_get("/ws", Helper.ws_handler)
Helper.runner = web.AppRunner(Helper.app)
await Helper.runner.setup()
Helper.site = web.TCPSite(Helper.runner, "localhost", port)
Helper.websocket_port = port
await Helper.site.start()
break
except OSError:
port += 1
decky_plugin.logger.info("WebSocket server started")
except Exception as e:
decky_plugin.logger.error(f"Error in start_ws_server: {e}")
async def stop_ws_server():
try:
decky_plugin.logger.info("Stopping WebSocket server")
# Signal the server to stop
Helper.wsServerIsRunning = False
# Stop the site
if Helper.site:
decky_plugin.logger.info("Stopping site")
await Helper.site.stop()
decky_plugin.logger.info("Site stopped")
# Cleanup the runner
if Helper.runner:
await Helper.runner.cleanup()
decky_plugin.logger.info("Runner cleaned up")
# Clear references
Helper.site = None
Helper.runner = None
Helper.app = None
except Exception as e:
decky_plugin.logger.error(f"Error in stop_ws_server: {e}")
finally:
# Stop the event loop if it exists
if Helper.ws_loop and Helper.ws_loop.is_running():
Helper.ws_loop.stop()
Helper.ws_loop = None
Helper.wsServerIsRunning = False
decky_plugin.logger.info("WebSocket server stopped")
@staticmethod
def get_installed_extensions():
"""
Get list of installed extension directory names by checking for static.json files
Searches in both plugin dir and runtime dir (data)
Returns a list of unique extension names (directory names containing static.json)
"""
extensions = set()
# Search paths
search_paths = [
os.path.join(decky_plugin.DECKY_PLUGIN_DIR, "scripts", "Extensions"),
os.path.join(
decky_plugin.DECKY_PLUGIN_RUNTIME_DIR, "scripts", "Extensions"
),
]
for base_path in search_paths:
if not os.path.exists(base_path):
continue
try:
# Walk through the Extensions directory
for root, dirs, files in os.walk(base_path):
# If this directory contains static.json
if "static.json" in files:
# Get the directory name relative to Extensions
rel_path = os.path.relpath(root, base_path)
# If it's directly under Extensions (not the Extensions dir itself)
if rel_path != ".":
# Get just the top-level directory name
ext_name = rel_path.split(os.sep)[0]
extensions.add(ext_name)
except Exception as e:
decky_plugin.logger.error(
f"Error scanning extensions in {base_path}: {e}"
)
# Convert to sorted list
result = sorted(list(extensions))
decky_plugin.logger.info(f"Found installed extensions: {result}")
return result
# import requests
# ── Connexion dans le navigateur de Steam (SkullKey #4) ─────────────────────
# Les fenêtres de connexion Epic / GOG / Amazon sont des applis GTK + WebKit2
# lancées comme un jeu. SteamOS d'origine n'a NI PyGObject NI WebKit2 (et son
# système est en lecture seule) → connexion impossible sur un Steam Deck.
# Repli : la page de connexion s'ouvre dans le navigateur intégré de Steam
# (Navigation.NavigateToExternalWeb côté interface) et on lit le code par le
# CDP de Steam (127.0.0.1:8080). Ce navigateur N'APPARAÎT PAS dans /json : seul
# Target.getTargets au niveau navigateur le liste (mesuré 04/10, Chrome 126).
BROWSER_LOGIN = {
# Epic : legendary.gl renvoie vers la connexion Epic, qui finit sur une page
# JSON contenant authorizationCode (l'URL ne contient pas le code).
"Epic": {"match": "epicgames.com/id/api/redirect", "param": None},
"GOG": {"match": "embed.gog.com/on_login_success", "param": "code"},
"Amazon": {"match": "openid.oa2.authorization_code",
"param": "openid.oa2.authorization_code"},
}
class BrowserLogin:
state = {"status": "idle"} # idle | waiting | finishing | done | error
task = None
ea = None
start_lock = asyncio.Lock()
@staticmethod
async def _cdp(ws, method, params=None, session=None, _ids=[0]):
_ids[0] += 1
my = _ids[0]
msg = {"id": my, "method": method, "params": params or {}}
if session:
msg["sessionId"] = session
await ws.send_str(json.dumps(msg))
while True:
m = await asyncio.wait_for(ws.receive(), timeout=10)
if m.type != aiohttp.WSMsgType.TEXT:
raise RuntimeError(f"CDP fermé ({m.type})")
d = json.loads(m.data)
if d.get("id") == my:
if "error" in d:
raise RuntimeError(d["error"].get("message"))
return d.get("result") or {}
@staticmethod
async def _find_code(session, platform):
"""Une passe : cherche la page de redirection et en extrait le code."""
from urllib.parse import urlparse, parse_qs
rule = BROWSER_LOGIN[platform]
async with session.get("http://127.0.0.1:8080/json/version") as r:
ver = await r.json(content_type=None)
async with session.ws_connect(ver["webSocketDebuggerUrl"], max_msg_size=0) as ws:
targets = (await BrowserLogin._cdp(ws, "Target.getTargets")).get("targetInfos", [])
for t in targets:
url = t.get("url") or ""
if t.get("type") != "page" or rule["match"] not in url:
continue
if rule["param"]:
vals = parse_qs(urlparse(url).query).get(rule["param"])
if vals and vals[0]:
return vals[0]
continue
# Epic : lire la page JSON.
att = await BrowserLogin._cdp(ws, "Target.attachToTarget",
{"targetId": t["targetId"], "flatten": True})
sid = att.get("sessionId")
try:
res = await BrowserLogin._cdp(ws, "Runtime.evaluate",
{"expression": "document.body ? document.body.innerText : ''",
"returnByValue": True}, session=sid)
finally:
try:
await BrowserLogin._cdp(ws, "Target.detachFromTarget", {"sessionId": sid})
except Exception:
pass
text = ((res.get("result") or {}).get("value") or "").strip()
try:
code = json.loads(text).get("authorizationCode")
except Exception:
code = None
if code:
return code
return None
@staticmethod
async def _watch(platform, deadline):
async with aiohttp.ClientSession() as session:
while time.time() < deadline and BrowserLogin.state.get("status") == "waiting":
try:
code = await BrowserLogin._find_code(session, platform)
except Exception as e:
decky_plugin.logger.warning(f"[browser-login] {e!r}")
code = None
if code:
BrowserLogin.state = {"status": "finishing", "platform": platform}
out = await Helper.call_script(
"./scripts/skullkey.sh",
platform, "login-code", code)
ok = '"Error"' not in (out or "")
decky_plugin.logger.info(f"[browser-login] {platform} : code reçu, "
f"connexion {'OK' if ok else 'en échec'}")
BrowserLogin.state = ({"status": "done", "platform": platform} if ok else
{"status": "error", "platform": platform,
"message": (out or "")[-300:]})
return
await asyncio.sleep(1)
if BrowserLogin.state.get("status") == "waiting":
BrowserLogin.state = {"status": "error", "platform": platform, "message": "timeout"}
# ── Gestionnaire de Proton (demande user 07/10) ─────────────────────────────
# Proton communautaires installés dans compatibilitytools.d depuis la DERNIÈRE
# release GitHub de chaque projet, archive vérifiée en SHA-512 avant extraction
# (leçons du script GE de bc250-tweaks : lire le vrai nom d'archive dans l'API,
# ne jamais le deviner ; une archive tronquée casse Steam en silence). Le Proton
# de Valve, lui, s'installe par Steam (assistant d'installation, côté interface).
PROTON_SOURCES = {
"ge": {"name": "GE-Proton", "repo": "GloriousEggroll/proton-ge-custom",
"asset": r"-x86_64\.tar\.gz$"},
"cachyos": {"name": "Proton-CachyOS", "repo": "CachyOS/proton-cachyos",
"asset": r"-slr-x86_64\.tar\.xz$"},
}
class ProtonManager:
state = {"status": "idle"} # idle | downloading | verifying | extracting | done | error
task = None
@staticmethod
def compat_dir():
return os.path.join(decky_plugin.DECKY_USER_HOME, ".local", "share", "Steam",
"compatibilitytools.d")
@staticmethod
def _ssl():
import ssl
# Le Python de Decky n'a pas de magasin de certificats : on pointe celui
# du système (chemins Fedora/Bazzite, Debian/SteamOS, Arch).
for ca in ("/etc/pki/tls/certs/ca-bundle.crt", "/etc/ssl/certs/ca-certificates.crt",
"/etc/ssl/cert.pem"):
if os.path.exists(ca):
return ssl.create_default_context(cafile=ca)
return ssl.create_default_context()
@staticmethod
def installed():
d = ProtonManager.compat_dir()
try:
return sorted(n for n in os.listdir(d) if os.path.isdir(os.path.join(d, n)))
except OSError:
return []
@staticmethod
async def latest(key):
import re
src = PROTON_SOURCES[key]
url = f"https://api.github.com/repos/{src['repo']}/releases/latest"
async with aiohttp.ClientSession(connector=aiohttp.TCPConnector(ssl=ProtonManager._ssl())) as s:
async with s.get(url, headers={"Accept": "application/vnd.github+json",
"User-Agent": "SkullKey"}, timeout=aiohttp.ClientTimeout(total=20)) as r:
rel = await r.json(content_type=None)
assets = rel.get("assets") or []
arc = next((a for a in assets if re.search(src["asset"], a.get("name", ""))), None)
if not arc:
return {"key": key, "name": src["name"], "error": "no x86_64 archive in latest release"}
base = re.sub(r"\.tar\.(gz|xz)$", "", arc["name"])
sha = next((a for a in assets if a.get("name") == base + ".sha512sum"), None)
inst = ProtonManager.installed()
tag = rel.get("tag_name") or base
# Dossier extrait : GE = « GE-Proton11-7 » (ou suffixé -x86_64),
# CachyOS = nom de l'archive sans extension.
is_inst = any(n == tag or n.startswith(tag + "-") or n == base for n in inst)
return {"key": key, "name": src["name"], "tag": tag, "url": arc["browser_download_url"],
"size": arc.get("size", 0), "sha_url": sha["browser_download_url"] if sha else "",
"installed": is_inst}
@staticmethod
async def install(key):
import hashlib, tarfile, tempfile
st = ProtonManager.state
tmp = None
try:
info = await ProtonManager.latest(key)
if info.get("error"):
raise RuntimeError(info["error"])
if not info.get("sha_url"):
raise RuntimeError("no SHA-512 checksum published for this release")
ProtonManager.state = st = {"status": "downloading", "key": key, "tag": info["tag"],
"done": 0, "total": info["size"]}
ssl_ctx = ProtonManager._ssl()
tmp = tempfile.NamedTemporaryFile(prefix="skullkey-proton-", delete=False,
dir=decky_plugin.DECKY_PLUGIN_RUNTIME_DIR)
h = hashlib.sha512()
async with aiohttp.ClientSession(connector=aiohttp.TCPConnector(ssl=ssl_ctx)) as s:
async with s.get(info["sha_url"], headers={"User-Agent": "SkullKey"}) as r:
expected = (await r.text()).split()[0].strip().lower()
async with s.get(info["url"], headers={"User-Agent": "SkullKey"},
timeout=aiohttp.ClientTimeout(total=None, sock_read=60)) as r:
r.raise_for_status()
async for chunk in r.content.iter_chunked(1 << 20):
tmp.write(chunk)
h.update(chunk)
st["done"] += len(chunk)
tmp.close()
st["status"] = "verifying"
if h.hexdigest() != expected:
raise RuntimeError("checksum mismatch — download corrupted, nothing was installed")
st["status"] = "extracting"
dest = ProtonManager.compat_dir()
os.makedirs(dest, exist_ok=True)
def _extract():
with tarfile.open(tmp.name) as tf:
for m in tf.getmembers(): # pas de chemin qui sort du dossier
if m.name.startswith("/") or ".." in m.name.split("/"):
raise RuntimeError(f"unsafe path in archive: {m.name}")
tf.extractall(dest)
await asyncio.get_event_loop().run_in_executor(None, _extract)
ProtonManager.state = {"status": "done", "key": key, "tag": info["tag"]}
decky_plugin.logger.info(f"[proton] {info['tag']} installé dans {dest}")
except Exception as e:
decky_plugin.logger.error(f"[proton] install {key}: {e!r}")
ProtonManager.state = {"status": "error", "key": key, "message": str(e)}
finally:
if tmp is not None:
try:
tmp.close()
os.unlink(tmp.name)
except Exception:
pass
class Plugin:
async def _main(self):
decky_plugin.logger.info("SkullKey starting up...")
try:
Helper.action_cache = {}
if os.path.exists(
os.path.join(decky_plugin.DECKY_PLUGIN_RUNTIME_DIR, "init.json")
):
Helper.working_directory = decky_plugin.DECKY_PLUGIN_RUNTIME_DIR
else:
Helper.working_directory = decky_plugin.DECKY_PLUGIN_DIR
decky_plugin.logger.info(
f"plugin: {decky_plugin.DECKY_PLUGIN_NAME} dir: {decky_plugin.DECKY_PLUGIN_RUNTIME_DIR}"
)
# pass cmd argument to _call_script method
decky_plugin.logger.info("SkullKey initializing")
result = await Helper.execute_action("init", "init")
decky_plugin.logger.info("SkullKey initialized")
try:
repaired = self._prefixes_mod().repair_existing()
if repaired:
decky_plugin.logger.info(f"[prefixes] checked {repaired} existing custom prefixes")
except Exception as e:
decky_plugin.logger.warning(f"[prefixes] repair: {e!r}")
if Helper.verbose:
decky_plugin.logger.info(f"init result: {result}")
await Helper.start_ws_server()
decky_plugin.logger.info("SkullKey started")
# Schedule the silent auto-update as a module-level coroutine —
# decky's method dispatch doesn't bind `self` for a task created
# from inside _main, so keep it self-free.
_spawn(_auto_update_check())
_spawn(_ensure_deps())
_spawn(_resume_downloads())
_spawn(_games_autoupdate())
except Exception as e:
decky_plugin.logger.error(f"Error in _main: {e}")
async def check_update(self):
return await updater.check()
async def get_version(self):
return updater.get_current_version()
async def apply_update(self, url):
res = await updater.apply(url)
# Writing the files is not loading them. The frontend takes it from
# here: it asks the loader to re-import THIS plugin. We no longer
# restart plugin_loader from the backend — it is not allowed to (see
# restart_loader) and it would bounce every plugin for nothing.
return res
async def take_pending_update(self):
"""Hand the failed-update notice to the frontend, once.
Cleared on read: the notification must fire ONCE, not on every QAM open.
"""
global _PENDING_UPDATE
pending, _PENDING_UPDATE = _PENDING_UPDATE, None
return pending or {}
async def get_autoupdate(self):
return updater.is_autoupdate_enabled()
async def take_port_update_events(self):
"""Relay completed automatic game updates once, even with QAM closed."""
directory = os.path.join(decky_plugin.DECKY_PLUGIN_RUNTIME_DIR, "ports_update_events")
events = []
if not os.path.isdir(directory):
return events
for name in sorted(os.listdir(directory))[:20]:
if not name.endswith(".json"):
continue
path = os.path.join(directory, name)
try:
with open(path) as handle:
event = json.load(handle)
if isinstance(event, dict) and event.get("name"):
events.append(event)
os.unlink(path)
except Exception as error: