diff --git a/lutristosunshine.py b/lutristosunshine.py index 36b6bbb..a587f48 100644 --- a/lutristosunshine.py +++ b/lutristosunshine.py @@ -5,7 +5,7 @@ from config.constants import COVERS_PATH, DEFAULT_IMAGE, SOURCE_COLORS, RESET_COLOR from utils.utils import handle_interrupt, run_command, get_games_found_message, parse_json_output from utils.input import get_yes_no_input, get_user_selection -from sunshine.sunshine import detect_sunshine_installation, add_game_to_sunshine, get_existing_apps, get_auth_token +from sunshine.sunshine import detect_sunshine_installation, add_game_to_sunshine, get_existing_apps, get_auth_token, is_sunshine_running from utils.steamgriddb import manage_api_key, download_image_from_steamgriddb from launchers.heroic import list_heroic_games, get_heroic_command, HEROIC_PATHS from launchers.lutris import list_lutris_games, get_lutris_command, is_lutris_running @@ -24,9 +24,14 @@ def main(): print("Error: Sunshine Flatpak is not supported. Please use the native installation of Sunshine.") return - token = None - while token is None: - token = get_auth_token() + if not is_sunshine_running(): + print("Error: Sunshine is not running. Please start Sunshine and try again.") + return + + token = get_auth_token() + if not token: + print("Error: Could not obtain a valid authentication token. Exiting.") + return lutris_command = get_lutris_command() heroic_command, _ = get_heroic_command() diff --git a/sunshine/sunshine.py b/sunshine/sunshine.py index eac37b7..7ef7948 100644 --- a/sunshine/sunshine.py +++ b/sunshine/sunshine.py @@ -4,6 +4,7 @@ import requests import getpass import urllib3 +import subprocess from typing import Tuple, Optional, Dict, List from config.constants import DEFAULT_IMAGE, CREDENTIALS_PATH, SUNSHINE_API_URL from utils.utils import run_command @@ -53,11 +54,25 @@ def get_sunshine_credentials() -> Tuple[str, str]: password = getpass.getpass("Enter your Sunshine password: ") return username, password +def is_sunshine_running() -> bool: + """Checks if Sunshine is currently running.""" + try: + # Run the ps command to check for the Sunshine process + output = subprocess.check_output(["ps", "-A"], stderr=subprocess.STDOUT).decode() + return "sunshine" in output.lower() # Check if "sunshine" is present in the process list + except subprocess.CalledProcessError: + return False + def get_auth_token() -> Optional[str]: """Retrieves or generates an authentication token.""" token_path = os.path.join(CREDENTIALS_PATH, "auth_token.txt") - # Check for an existing token + # Check if Sunshine is running BEFORE attempting any authentication + if not is_sunshine_running(): + print("Error: Sunshine is not running. Please start Sunshine and try again.") + return None + + # Check for an existing token (only if Sunshine is running) if os.path.exists(token_path): with open(token_path, 'r') as f: token = f.read().strip() @@ -70,7 +85,7 @@ def get_auth_token() -> Optional[str]: else: return token - # If no valid token exists, prompt for credentials + # If no valid token exists, prompt for credentials (only if Sunshine is running) username, password = get_sunshine_credentials() if not username or not password: return None