#!/bin/bash run_game() { # Initialize variables emulator="" system="" manual_mode=false usage="Usage: flatpak run net.retrodeck.retrodeck [-e emulator] [-s system] [-m] game" # Parse options for system, emulator, and manual mode while getopts ":e:s:m" opt; do case ${opt} in e) emulator=$OPTARG # Emulator provided via -e ;; s) system=$OPTARG # System provided via -s ;; m) manual_mode=true # Manual mode enabled via -m log i "Run game: manual mode enabled" ;; \?) echo "$usage" exit 1 ;; esac done shift $((OPTIND - 1)) # Check for game argument if [[ -z "$1" ]]; then log e "Game path is required." log i "$usage" exit 1 fi game="$(realpath "$1")" # Check if the game is a .desktop file if [[ "$game" == *.desktop ]]; then # Extract the Exec command from the .desktop file exec_cmd=$(grep '^Exec=' "$game" | sed 's/^Exec=//') # Workaround for RPCS3 games, replace placeholder with actual game ID exec_cmd=$(echo "$exec_cmd" | sed 's/%%RPCS3_GAMEID%%/%RPCS3_GAMEID%/g') if [[ -n "$exec_cmd" ]]; then log i "-------------------------------------------" log i " RetroDECK is now booting the game" log i " Game path: \"$game\"" log i " Recognized system: desktop file" log i " Command line: $exec_cmd" log i "-------------------------------------------" # Execute the command from the .desktop file eval "$exec_cmd" exit 1 else log e "No Exec command found in .desktop file." exit 1 fi fi if [[ -d "$game" ]]; then log d "$(basename "$game") is a directory, parsing it like a \"directory as a file\"" game="$game/$(basename "$game")" log d "Actual file is in \"$game\"" fi game_basename="./$(basename "$game")" # Check if realpath succeeded if [[ -z "$game" || ! -e "$game" ]]; then rd_zenity --icon-name=net.retrodeck.retrodeck --info --no-wrap --ok-label="OK" \ --window-icon="/app/share/icons/hicolor/scalable/apps/net.retrodeck.retrodeck.svg" \ --title "RetroDECK - File not found" \ --text="ERROR: File \"$game\" not found.\n\nPlease make sure that RetroDECK's Flatpak is correctly configured to reach the given path and try again." log e "File \"$game\" not found.\n\nPlease make sure that RetroDECK's Flatpak is correctly configured to reach the given path and try again." exit 1 fi # Step 1: System Recognition if [[ -z "$system" ]]; then # Automatically detect system from game path system=$(echo "$game" | grep -oP '(?<=roms/)[^/]+') if [[ -z "$system" ]]; then log i "Failed to detect system from game path, asking user action" system=$(find_system_by_extension "$game_basename") fi fi # Step 2: Emulator Definition if [[ -n "$emulator" ]]; then log d "Emulator provided via command-line: $emulator" elif [[ "$manual_mode" = true ]]; then log d "Manual mode: showing Zenity emulator selection" emulator=$(find_system_commands "$system") if [[ -z "$emulator" ]]; then log e "No emulator selected in manual mode." exit 1 fi else log d "Automatically searching for an emulator for system: $system" # Check for in the game block in gamelist.xml altemulator=$(awk -v path="$game_basename" ' //,/<\/game>/ { if ($0 ~ "" path "<\/path>") found = 1 if (found && $0 ~ //) { gsub(/.*|<\/altemulator>.*/,"") print exit } if (found && $0 ~ /<\/game>/) exit } ' "$rdhome/ES-DE/gamelists/$system/gamelist.xml" 2>/dev/null) if [[ -n "$altemulator" ]]; then log d "Found for game: $altemulator" emulator=$(xmllint --recover --xpath "string(//system[name=\"$system\"]/command[@label=\"$altemulator\"])" "$es_systems" 2>/dev/null) else # if no altemulator is found we search if a global one is set log d "No altemulator found in the game entry, searching for alternativeEmulator to check if a global emulator is set for the system $system" alternative_emulator=$(awk ' //,/<\/alternativeEmulator>/ { if ($0 ~ /