|
6 | 6 | import vdf |
7 | 7 | import requests |
8 | 8 | import threading |
| 9 | +import pkgutil |
9 | 10 | from steam.utils.appcache import parse_appinfo |
10 | 11 |
|
11 | 12 | from PySide6.QtCore import Signal |
12 | 13 | from PySide6.QtWidgets import QMessageBox, QApplication |
13 | 14 |
|
| 15 | +from pupgui2.constants import APP_NAME, APP_ID, APP_ICON_FILE |
14 | 16 | from pupgui2.constants import LOCAL_AWACY_GAME_LIST, PROTONDB_API_URL |
15 | 17 | from pupgui2.constants import STEAM_STL_INSTALL_PATH, STEAM_STL_CONFIG_PATH, STEAM_STL_SHELL_FILES, STEAM_STL_FISH_VARIABLES |
16 | 18 | from pupgui2.datastructures import SteamApp, AWACYStatus, BasicCompatTool, CTType |
@@ -497,3 +499,78 @@ def remove_steamtinkerlaunch(compat_folder='', remove_config=True, ctmod_object= |
497 | 499 | except IOError as e: |
498 | 500 | print('Something went wrong trying to uninstall SteamTinkerLaunch. Aborting...', e) |
499 | 501 | return False |
| 502 | + |
| 503 | + |
| 504 | +def install_steam_library_shortcut(steam_config_folder: str, remove_shortcut=False) -> int: |
| 505 | + """ |
| 506 | + Adds a shortcut to launch this app to the Steam Library |
| 507 | + Return: 0=success, 1=error, 2=already installed |
| 508 | + """ |
| 509 | + users_folder = os.path.realpath(os.path.join(os.path.expanduser(steam_config_folder), os.pardir, 'userdata')) |
| 510 | + |
| 511 | + try: |
| 512 | + if not os.path.isfile(APP_ICON_FILE): |
| 513 | + with open(APP_ICON_FILE, 'wb') as f: |
| 514 | + f.write(pkgutil.get_data(__name__, 'resources/img/appicon256.png')) |
| 515 | + |
| 516 | + for userf in os.listdir(users_folder): |
| 517 | + user_cfg_dir = os.path.join(users_folder, userf, 'config') |
| 518 | + shortcuts_file = os.path.join(user_cfg_dir, 'shortcuts.vdf') |
| 519 | + |
| 520 | + if not os.path.exists(user_cfg_dir): |
| 521 | + continue |
| 522 | + |
| 523 | + shortcuts_vdf = {} |
| 524 | + sid=-1 |
| 525 | + if os.path.exists(shortcuts_file): |
| 526 | + with open(shortcuts_file, 'rb') as f: |
| 527 | + shortcuts_vdf = vdf.binary_load(f) |
| 528 | + |
| 529 | + for sid in list(shortcuts_vdf.get('shortcuts', {}).keys()): |
| 530 | + svalue = shortcuts_vdf.get('shortcuts', {}).get(sid) |
| 531 | + if APP_NAME in svalue.get('AppName', ''): |
| 532 | + if remove_shortcut: |
| 533 | + shortcuts_vdf.get('shortcuts', {}).pop(sid) |
| 534 | + else: |
| 535 | + return 2 |
| 536 | + |
| 537 | + with open(shortcuts_file, 'wb') as f: |
| 538 | + if not remove_shortcut: |
| 539 | + run_config = ['', ''] |
| 540 | + if os.path.exists('/.flatpak-info'): |
| 541 | + run_config = [f'/usr/bin/flatpak', f'run {APP_ID}'] |
| 542 | + elif exe := subprocess.run(['which', APP_ID], universal_newlines=True, stdout=subprocess.PIPE).stdout.strip(): |
| 543 | + run_config = [exe, ''] |
| 544 | + elif exe := os.getenv('APPIMAGE'): |
| 545 | + if APP_NAME in exe: |
| 546 | + exe = os.path.join(exe, os.pardir, APP_NAME + '*.AppImage') # remove version from file name |
| 547 | + run_config = [exe, ''] |
| 548 | + else: |
| 549 | + return 1 |
| 550 | + |
| 551 | + sid = str(int(sid) + 1) |
| 552 | + shortcuts_vdf.setdefault('shortcuts', {})[sid] = { |
| 553 | + 'appid': 1621167219, |
| 554 | + 'AppName': APP_NAME, |
| 555 | + 'Exe': f'"{run_config[0]}"', |
| 556 | + 'StartDir': './', |
| 557 | + 'icon': APP_ICON_FILE, |
| 558 | + 'ShortcutPath': '', |
| 559 | + 'LaunchOptions': run_config[1], |
| 560 | + 'IsHidden': 0, |
| 561 | + 'AllowDesktopConfig': 1, |
| 562 | + 'AllowOverlay': 1, |
| 563 | + 'OpenVR': 0, |
| 564 | + 'Devkit': 0, |
| 565 | + 'DevkitGameID': '', |
| 566 | + 'DevkitOverrideAppID': 0, |
| 567 | + 'LastPlayTime': 0, |
| 568 | + 'FlatpakAppID': '', |
| 569 | + 'tags': {} |
| 570 | + } |
| 571 | + |
| 572 | + f.write(vdf.binary_dumps(shortcuts_vdf)) |
| 573 | + except Exception as e: |
| 574 | + print(f'Error: Could not add {APP_NAME} as Steam shortcut:', e) |
| 575 | + |
| 576 | + return 0 |
0 commit comments