feat(steam): using steam cef when deleting a shortcut

This commit is contained in:
Alex Smith
2025-08-10 01:08:58 +05:00
committed by Boris Yumankulov
parent 279f7ec36b
commit 24a7c2e657

View File

@@ -857,10 +857,12 @@ def call_steam_api(js_cmd: str, *args) -> dict | None:
async function setGrid(id, i, ext, image) { async function setGrid(id, i, ext, image) {
await SteamClient.Apps.SetCustomArtworkForApp(id, image, ext, i); await SteamClient.Apps.SetCustomArtworkForApp(id, image, ext, i);
return true;
}; };
async function removeShortcut(id) { async function removeShortcut(id) {
await SteamClient.Apps.RemoveShortcut(+id); await SteamClient.Apps.RemoveShortcut(+id);
return true;
}; };
""" """
try: try:
@@ -1007,7 +1009,6 @@ export START_FROM_STEAM=1
icon_path, icon_path,
"" ""
) )
logger.info(f"### api_response: {api_response}")
if api_response and isinstance(api_response, dict) and 'id' in api_response: if api_response and isinstance(api_response, dict) and 'id' in api_response:
appid = api_response['id'] appid = api_response['id']
@@ -1192,19 +1193,8 @@ def remove_from_steam(game_name: str, exec_line: str) -> tuple[bool, str]:
logger.info(f"shortcuts.vdf not found at {steam_shortcuts_path}") logger.info(f"shortcuts.vdf not found at {steam_shortcuts_path}")
return (False, f"Game '{game_name}' not found in Steam") return (False, f"Game '{game_name}' not found in Steam")
# Generate appid for identifying cover files appid = None
unique_string = f"{script_path}{game_name}" was_api_used = False
baseid = zlib.crc32(unique_string.encode('utf-8')) & 0xffffffff
appid = baseid | 0x80000000
# Create backup of shortcuts.vdf
backup_path = f"{steam_shortcuts_path}.backup"
try:
shutil.copy2(steam_shortcuts_path, backup_path)
logger.info(f"Created backup of shortcuts.vdf at {backup_path}")
except Exception as e:
logger.error(f"Failed to create backup of shortcuts.vdf: {e}")
return (False, f"Failed to create backup of shortcuts.vdf: {e}")
# Load and modify shortcuts.vdf # Load and modify shortcuts.vdf
try: try:
@@ -1218,37 +1208,52 @@ def remove_from_steam(game_name: str, exec_line: str) -> tuple[bool, str]:
return (False, f"Failed to load shortcuts.vdf: {load_err}") return (False, f"Failed to load shortcuts.vdf: {load_err}")
shortcuts = shortcuts_data.get("shortcuts", {}) shortcuts = shortcuts_data.get("shortcuts", {})
found = False
new_shortcuts = {} new_shortcuts = {}
index = 0 index = 0
# Filter out the matching shortcut # Filter out the matching shortcut
for _key, entry in shortcuts.items(): for _key, entry in shortcuts.items():
if entry.get("AppName") == game_name and entry.get("Exe") == f'"{script_path}"': if entry.get("AppName") == game_name and entry.get("Exe") == f'"{script_path}"':
found = True appid = convert_steam_id(int(entry.get("appid")))
logger.info(f"Found matching shortcut for '{game_name}' to remove") logger.info(f"Found matching shortcut for '{game_name}' to remove")
continue continue
new_shortcuts[str(index)] = entry new_shortcuts[str(index)] = entry
index += 1 index += 1
if not found: if not appid:
logger.info(f"Game '{game_name}' not found in Steam shortcuts") logger.info(f"Game '{game_name}' not found in Steam shortcuts")
return (False, f"Game '{game_name}' not found in Steam") return (False, f"Game '{game_name}' not found in Steam")
# Save updated shortcuts.vdf api_response = call_steam_api("removeShortcut", appid)
try: if api_response is not None: # API ответил, даже если ответ пустой
with open(steam_shortcuts_path, 'wb') as f: was_api_used = True
vdf.binary_dump({"shortcuts": new_shortcuts}, f) logger.info(f"Ярлык для AppID {appid} успешно удален через API.")
logger.info(f"Successfully updated shortcuts.vdf, removed '{game_name}'") else:
except Exception as e: logger.warning("Не удалось удалить ярлык через API. Используется запасной метод (редактирование shortcuts.vdf).")
logger.error(f"Failed to update shortcuts.vdf: {e}")
if os.path.exists(backup_path): # Create backup of shortcuts.vdf
try: backup_path = f"{steam_shortcuts_path}.backup"
shutil.copy2(backup_path, steam_shortcuts_path) try:
logger.info("Restored shortcuts.vdf from backup due to update failure") shutil.copy2(steam_shortcuts_path, backup_path)
except Exception as restore_err: logger.info(f"Created backup of shortcuts.vdf at {backup_path}")
logger.error(f"Failed to restore shortcuts.vdf from backup: {restore_err}") except Exception as e:
return (False, f"Failed to update shortcuts.vdf: {e}") logger.error(f"Failed to create backup of shortcuts.vdf: {e}")
return (False, f"Failed to create backup of shortcuts.vdf: {e}")
# Save updated shortcuts.vdf
try:
with open(steam_shortcuts_path, 'wb') as f:
vdf.binary_dump({"shortcuts": new_shortcuts}, f)
logger.info(f"Successfully updated shortcuts.vdf, removed '{game_name}'")
except Exception as e:
logger.error(f"Failed to update shortcuts.vdf: {e}")
if os.path.exists(backup_path):
try:
shutil.copy2(backup_path, steam_shortcuts_path)
logger.info("Restored shortcuts.vdf from backup due to update failure")
except Exception as restore_err:
logger.error(f"Failed to restore shortcuts.vdf from backup: {restore_err}")
return (False, f"Failed to update shortcuts.vdf: {e}")
# Delete cover files # Delete cover files
cover_files = [ cover_files = [