-
Notifications
You must be signed in to change notification settings - Fork 2
/
main.py
574 lines (525 loc) · 21.3 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
import os
import hashlib
import traceback
import json
import base64
import tarfile
import urllib.request
from helpers import get_ssl_context # type: ignore
import shutil
import subprocess
# The decky plugin module is located at decky-loader/plugin
# For easy intellisense checkout the decky-loader code one directory up
# or add the `decky-loader/plugin` path to `python.analysis.extraPaths` in `.vscode/settings.json`
import decky_plugin
from shutil import copyfile
from vdf import binary_dump, binary_load
from pathlib import Path
def get_steam_path():
return Path(decky_plugin.DECKY_USER_HOME) / ".local" / "share" / "Steam"
def get_steam_userdata():
return get_steam_path() / "userdata"
def get_userdata_config(steam32):
return get_steam_userdata() / steam32 / "config"
def shortcut_already_exists(shortcuts, name):
for key, shortcut in shortcuts.items():
decky_plugin.logger.info("shortcut: {}".format(json.dumps(shortcut)))
if "AppName" in shortcut and shortcut["AppName"] == name:
decky_plugin.logger.info("found existing shortcut!")
return True
return False
def version_supports_game_flag(version):
parts = version.replace("v", "").split(".")
if int(parts[0]) > 0 or int(parts[1]) > 1:
return True
elif int(parts[1]) == 0 and int(parts[2]) >= 44:
return True
return False
def _install_game_impl(game):
try:
decky_plugin.logger.info("installing game: {}".format(game))
# GitHub repository information
owner = "open-goal"
repo = "jak-project"
# Directory where you want to extract the files
extract_directory = os.path.join(
decky_plugin.DECKY_USER_HOME, "OpenGOAL", "games", game
)
# Reset the directory
if os.path.exists(extract_directory):
shutil.rmtree(extract_directory)
os.makedirs(extract_directory)
# Get the latest release URL using GitHub API
release_url = "https://api.github.com/repos/{}/{}/releases/latest".format(
owner, repo
)
release_resp = urllib.request.urlopen(release_url, context=get_ssl_context())
if release_resp.status == 200:
release_info = json.loads(release_resp.read().decode("utf-8"))
use_game_flag = version_supports_game_flag(release_info["tag_name"])
decky_plugin.logger.info(
"received response from github: {}".format(release_info)
)
# Find the asset with the desired format (opengoal-linux-*.tar.gz)
asset_to_download = None
for asset in release_info["assets"]:
if "opengoal-linux-" in asset["name"] and asset["name"].endswith(
".tar.gz"
):
asset_to_download = asset
break
if asset_to_download:
# Construct the download URL
asset_url = asset_to_download["browser_download_url"]
# Download the asset
asset_file = os.path.join(extract_directory, asset_to_download["name"])
asset_resp = urllib.request.urlopen(
asset_url, context=get_ssl_context()
)
if asset_resp.status == 200:
with open(asset_file, mode="wb") as f:
f.write(asset_resp.read())
# Extract the downloaded .tar.gz file
with tarfile.open(asset_file, "r:gz") as tar:
tar.extractall(extract_directory)
decky_plugin.logger.info(
"Downloaded and extracted: {}".format(asset_file)
)
# Delete the downloaded .tar.gz file
os.remove(asset_file)
decky_plugin.logger.info("Deleted: {}".format(asset_file))
# Run the extractor to install the game
iso_path = os.path.join(
decky_plugin.DECKY_USER_HOME,
"OpenGOAL",
"isos",
"{}.iso".format(game),
)
args = [
"./extractor",
iso_path,
"--extract",
"--validate",
"--decompile",
"--compile",
]
if use_game_flag:
args.append("--game")
args.append(game)
completed_process = subprocess.run(
args,
cwd=extract_directory,
check=True,
stdout=subprocess.PIPE,
stderr=subprocess.PIPE,
universal_newlines=True,
)
# Retrieve the exit status
return_code = completed_process.returncode
if return_code != 0:
decky_plugin.logger.error(
"Could not install successfully, status code: {}".format(
return_code
)
)
decky_plugin.logger.error(
"Logs: ERROR LOGS:\n{}\n\nSTDOUT:\n{}".format(
completed_process.stderr, completed_process.stdout
)
)
else:
# Delete directories that aren't needed after installation to free up space
if os.path.exists(
os.path.join(extract_directory, "data", "decompiler_out")
):
shutil.rmtree(
os.path.join(extract_directory, "data", "decompiler_out")
)
if os.path.exists(
os.path.join(extract_directory, "data", "iso_data")
):
shutil.rmtree(
os.path.join(extract_directory, "data", "iso_data")
)
# Finally, write out a simple file so we know what version is currently installed
with open(
os.path.join(extract_directory, "version.json"), mode="w"
) as f:
f.write(json.dumps({"version": release_info["tag_name"]}))
return True
else:
decky_plugin.logger.info("No matching asset found.")
else:
decky_plugin.logger.error(
"received unexpected status code from github: {}".format(
release_resp.status
)
)
return None
except Exception as error:
decky_plugin.logger.error(
"[install_game] An exception occurred: {}".format(traceback.format_exc())
)
return None
def _remove_game_impl(game):
try:
if game == "jak1":
if os.path.exists(
os.path.join(decky_plugin.DECKY_USER_HOME, "OpenGOAL", "games", "jak1")
):
shutil.rmtree(
os.path.join(
decky_plugin.DECKY_USER_HOME, "OpenGOAL", "games", "jak1"
)
)
elif game == "jak2":
if os.path.exists(
os.path.join(decky_plugin.DECKY_USER_HOME, "OpenGOAL", "games", "jak2")
):
shutil.rmtree(
os.path.join(
decky_plugin.DECKY_USER_HOME, "OpenGOAL", "games", "jak2"
)
)
return True
except:
decky_plugin.logger.error(
"[shortcut_already_created] An exception occurred: {}".format(
traceback.format_exc()
)
)
return False
class Plugin:
async def create_shortcut(self, owner_id, game):
try:
decky_plugin.logger.info("creating shortcut for game: {}".format(game))
shortcuts_vdf = get_userdata_config(owner_id) / "shortcuts.vdf"
decky_plugin.logger.info("loading file: {}".format(shortcuts_vdf))
d = binary_load(open(shortcuts_vdf, "rb"))
decky_plugin.logger.info(
"existing shortcuts: {}".format(json.dumps(d["shortcuts"]))
)
if game == "jak1":
if not shortcut_already_exists(d["shortcuts"], "OpenGOAL - Jak 1"):
app_id = (
int(
hashlib.sha256(
os.path.join(
decky_plugin.DECKY_USER_HOME,
"OpenGOAL",
"games",
"jak1",
"gk",
).encode()
).hexdigest(),
16,
)
% 1_000_000_000
) * -1
d["shortcuts"]["opengoal-jak1"] = {
"appid": app_id,
"AppName": "OpenGOAL - Jak 1",
"Exe": os.path.join(
decky_plugin.DECKY_USER_HOME,
"OpenGOAL",
"games",
"jak1",
"gk",
),
"StartDir": os.path.join(
decky_plugin.DECKY_USER_HOME, "OpenGOAL", "games", "jak1"
),
"icon": os.path.join(
decky_plugin.DECKY_PLUGIN_DIR, "img", "jak1", "icon.png"
),
"AllowOverlay": 1,
}
binary_dump(d, open(shortcuts_vdf, "wb"))
decky_plugin.logger.info(
"Created shortcut with appId: {}".format(app_id)
)
return app_id
elif game == "jak2":
if not shortcut_already_exists(d["shortcuts"], "OpenGOAL - Jak 2"):
app_id = (
int(
hashlib.sha256(
os.path.join(
decky_plugin.DECKY_USER_HOME,
"OpenGOAL",
"games",
"jak2",
"gk",
).encode()
).hexdigest(),
16,
)
% 1_000_000_000
) * -1
d["shortcuts"]["opengoal-jak2"] = {
"appid": app_id,
"AppName": "OpenGOAL - Jak 2",
"Exe": os.path.join(
decky_plugin.DECKY_USER_HOME,
"OpenGOAL",
"games",
"jak2",
"gk",
),
"LaunchOptions": "--game jak2",
"StartDir": os.path.join(
decky_plugin.DECKY_USER_HOME, "OpenGOAL", "games", "jak2"
),
"icon": os.path.join(
decky_plugin.DECKY_PLUGIN_DIR, "img", "jak2", "icon.png"
),
"AllowOverlay": 1,
}
binary_dump(d, open(shortcuts_vdf, "wb"))
decky_plugin.logger.info(
"Created shortcut with appId: {}".format(app_id)
)
return app_id
decky_plugin.logger.info("shortcut already created")
return None
except:
decky_plugin.logger.error(
"[create_shortcut] An exception occurred: {}".format(
traceback.format_exc()
)
)
return None
async def shortcut_already_created(self, owner_id, game):
try:
shortcuts_vdf = get_userdata_config(owner_id) / "shortcuts.vdf"
decky_plugin.logger.info("loading file: {}".format(shortcuts_vdf))
d = binary_load(open(shortcuts_vdf, "rb"))
decky_plugin.logger.info(
"existing shortcuts: {}".format(json.dumps(d["shortcuts"]))
)
if game == "jak1":
return shortcut_already_exists(d["shortcuts"], "OpenGOAL - Jak 1")
elif game == "jak2":
return shortcut_already_exists(d["shortcuts"], "OpenGOAL - Jak 2")
return False
except:
decky_plugin.logger.error(
"[shortcut_already_created] An exception occurred: {}".format(
traceback.format_exc()
)
)
return False
async def read_small_image_as_base64(self, game):
try:
if game == "jak1":
with open(
os.path.join(
decky_plugin.DECKY_PLUGIN_DIR, "img", "jak1", "small.png"
),
"rb",
) as image_file:
return base64.b64encode(image_file.read()).decode("utf-8")
elif game == "jak2":
with open(
os.path.join(
decky_plugin.DECKY_PLUGIN_DIR, "img", "jak2", "small.png"
),
"rb",
) as image_file:
return base64.b64encode(image_file.read()).decode("utf-8")
return None
except:
decky_plugin.logger.error(
"[read_small_image_as_base64] An exception occurred: {}".format(
traceback.format_exc()
)
)
return None
async def read_wide_image_as_base64(self, game):
try:
if game == "jak1":
with open(
os.path.join(
decky_plugin.DECKY_PLUGIN_DIR, "img", "jak1", "wide.png"
),
"rb",
) as image_file:
return base64.b64encode(image_file.read()).decode("utf-8")
elif game == "jak2":
with open(
os.path.join(
decky_plugin.DECKY_PLUGIN_DIR, "img", "jak2", "wide.png"
),
"rb",
) as image_file:
return base64.b64encode(image_file.read()).decode("utf-8")
return None
except:
decky_plugin.logger.error(
"[read_wide_image_as_base64] An exception occurred: {}".format(
traceback.format_exc()
)
)
return None
async def read_logo_image_as_base64(self, game):
try:
if game == "jak1":
with open(
os.path.join(
decky_plugin.DECKY_PLUGIN_DIR, "img", "jak1", "logo.png"
),
"rb",
) as image_file:
return base64.b64encode(image_file.read()).decode("utf-8")
elif game == "jak2":
with open(
os.path.join(
decky_plugin.DECKY_PLUGIN_DIR, "img", "jak2", "logo.png"
),
"rb",
) as image_file:
return base64.b64encode(image_file.read()).decode("utf-8")
return None
except:
decky_plugin.logger.error(
"[read_logo_image_as_base64] An exception occurred: {}".format(
traceback.format_exc()
)
)
return None
async def read_hero_image_as_base64(self, game):
try:
if game == "jak1":
with open(
os.path.join(
decky_plugin.DECKY_PLUGIN_DIR, "img", "jak1", "hero.png"
),
"rb",
) as image_file:
return base64.b64encode(image_file.read()).decode("utf-8")
elif game == "jak2":
with open(
os.path.join(
decky_plugin.DECKY_PLUGIN_DIR, "img", "jak2", "hero.png"
),
"rb",
) as image_file:
return base64.b64encode(image_file.read()).decode("utf-8")
return None
except:
decky_plugin.logger.error(
"[read_hero_image_as_base64] An exception occurred: {}".format(
traceback.format_exc()
)
)
return None
async def is_game_installed(self, game):
try:
game_dir = os.path.join("{}/OpenGOAL/games".format(decky_plugin.DECKY_USER_HOME), game)
if os.path.exists(game_dir) and len(os.listdir(game_dir)) > 0:
return True
return False
except:
decky_plugin.logger.error(
"[is_game_installed] An exception occurred: {}".format(
traceback.format_exc()
)
)
return False
async def get_users_home_dir(self):
return decky_plugin.DECKY_USER_HOME
async def is_game_out_of_date(self, game):
try:
# Get the saved version info from the file
extract_directory = os.path.join(
decky_plugin.DECKY_USER_HOME, "OpenGOAL", "games", game
)
if os.path.exists(os.path.join(extract_directory, "version.json")):
with open(
os.path.join(extract_directory, "version.json"), mode="r"
) as f:
current_info = json.load(f)
# GitHub repository information
owner = "open-goal"
repo = "jak-project"
# Get the latest release URL using GitHub API
release_url = (
"https://api.github.com/repos/{}/{}/releases/latest".format(
owner, repo
)
)
release_resp = urllib.request.urlopen(
release_url, context=get_ssl_context()
)
if release_resp.status == 200:
release_info = json.loads(release_resp.read().decode("utf-8"))
return release_info["tag_name"] != current_info["version"]
return False
return False
except:
decky_plugin.logger.error(
"[is_game_out_of_date] An exception occurred: {}".format(
traceback.format_exc()
)
)
return False
async def does_iso_exist_for_installation(self, game):
try:
iso_path = os.path.join("{}/OpenGOAL/isos".format(decky_plugin.DECKY_USER_HOME), "{}.iso".format(game))
if os.path.exists(iso_path):
return True
return False
except:
decky_plugin.logger.error(
"[is_game_installed] An exception occurred: {}".format(
traceback.format_exc()
)
)
return False
async def install_game(self, game):
return _install_game_impl(game)
async def remove_game(self, game):
return _remove_game_impl(game)
async def update_game(self, game):
try:
if _remove_game_impl(game):
return _install_game_impl(game)
return None
except:
decky_plugin.logger.error(
"[shortcut_already_created] An exception occurred: {}".format(
traceback.format_exc()
)
)
return None
# Asyncio-compatible long-running code, executed in a task when the plugin is loaded
async def _main(self):
decky_plugin.logger.info("OpenGOAL Loaded!")
# Function called first during the unload process, utilize this to handle your plugin being removed
async def _unload(self):
decky_plugin.logger.info("OpenGOAL Unloaded!")
pass
# Migrations that should be performed before entering `_main()`.
async def _migration(self):
decky_plugin.logger.info("Ensuring OpenGOAL paths are created")
if not os.path.exists(
os.path.join(decky_plugin.DECKY_USER_HOME, "OpenGOAL", "isos")
):
os.makedirs(
os.path.join(decky_plugin.DECKY_USER_HOME, "OpenGOAL", "isos"),
exist_ok=True,
)
if not os.path.exists(
os.path.join(decky_plugin.DECKY_USER_HOME, "OpenGOAL", "games", "jak1")
):
os.makedirs(
os.path.join(decky_plugin.DECKY_USER_HOME, "OpenGOAL", "games", "jak1"),
exist_ok=True,
)
if not os.path.exists(
os.path.join(decky_plugin.DECKY_USER_HOME, "OpenGOAL", "games", "jak2")
):
os.makedirs(
os.path.join(decky_plugin.DECKY_USER_HOME, "OpenGOAL", "games", "jak2"),
exist_ok=True,
)