From ad10c9d535e451fb5d967fa12a060cd4ec4b9231 Mon Sep 17 00:00:00 2001
From: XargonWan <XargonWan@gmail.com>
Date: Thu, 12 Sep 2024 00:56:20 +0900
Subject: [PATCH 1/5] RUN_GAME: checkpoint but BROKEN

---
 functions/other_functions.sh | 496 ++++++++++++++++++++---------------
 1 file changed, 290 insertions(+), 206 deletions(-)

diff --git a/functions/other_functions.sh b/functions/other_functions.sh
index b9c7f74a..4e58ebcf 100644
--- a/functions/other_functions.sh
+++ b/functions/other_functions.sh
@@ -892,6 +892,282 @@ start_retrodeck() {
   es-de
 }
 
+run_game() {
+  # Initialize variables
+  emulator=""
+  system=""
+  manual_mode=false
+  es_systems="/app/share/es-de/resources/systems/linux/es_systems.xml"
+
+  # Parse options
+  while getopts ":e:s:m" opt; do  # Use `m` for manual mode flag
+      case ${opt} in
+          e )
+              emulator=$OPTARG
+              ;;
+          s )
+              system=$OPTARG
+              ;;
+          m )
+              manual_mode=true
+              log i "Run game: manual mode enabled"
+              ;;
+          \? )
+              echo "Usage: $0 --run [-e emulator] [-s system] [-m manual] game"
+              exit 1
+              ;;
+      esac
+  done
+  shift $((OPTIND -1))
+
+  # Check for game argument
+  if [[ -z "$1" ]]; then
+      log e "Game path is required."
+      log i "Usage: $0 start [-e emulator] [-s system] [-m manual] game"
+      exit 1
+  fi
+
+  game=$1
+
+  # If no system is provided, extract it from the game path
+  if [[ -z "$system" ]]; then
+    system=$(echo "$game" | grep -oP '(?<=roms/)[^/]+')
+  fi
+
+  log d "Game: \"$game\""
+  log d "System: \"$system\""
+  
+  # Try finding the <altemulator> inside the specific game block
+  altemulator=$(xmllint --recover --xpath "string(//game[path='$game']/altemulator)" "$rdhome/ES-DE/gamelists/$system/gamelist.xml" 2>/dev/null)
+
+  if [[ -n "$altemulator" ]]; then
+      log d "Alternate emulator found in <altemulator>: $altemulator"
+      emulator_name=$(echo "$altemulator" | sed -e 's/ (Standalone)//')  # Strip " (Standalone)" from name
+      emulator=$(find_emulator "$emulator_name")
+
+      if [[ -n "$emulator" ]]; then
+          log d "Using alternate emulator: $emulator"
+      else
+          log e "No valid path found for emulator: $altemulator"
+          exit 1
+      fi
+  else
+    # Try to fetch <alternativeEmulator> from anywhere in the document
+    alternative_emulator=$(xmllint --recover --xpath 'string(//alternativeEmulator/label)' "$rdhome/ES-DE/gamelists/$system/gamelist.xml" 2>/dev/null)
+
+    if [[ -n "$alternative_emulator" ]]; then
+        log d "Alternate emulator found in <alternativeEmulator> header: $alternative_emulator"
+        
+        # Find the emulator name from the label in es_systems.xml
+        emulator_name=$(find_emulator_name_from_label "$alternative_emulator")
+        
+        if [[ -n "$emulator_name" ]]; then
+            # Pass the extracted emulator name to find_emulator function
+            emulator=$(find_emulator "$emulator_name")
+        fi
+        
+        if [[ -n "$emulator" ]]; then
+            log d "Using alternate emulator from <alternativeEmulator>: $emulator"
+        else
+            log e "No valid path found for emulator: $alternative_emulator"
+            exit 1
+        fi
+    else
+        log i "No alternate emulator found in game block or header, proceeding to auto mode."
+    fi
+  fi
+
+  # If an emulator is found, substitute placeholders in the command before running
+  if [[ -n "$emulator" ]]; then
+    # Ensure command substitution
+    find_system_commands "$emulator"
+    # TODO: almost there, we need just to start the emulator without Zenity: maybe we have to edit THAT function to pass the emulator to run
+    log d "Final command: $command"
+    eval "$command"
+  else
+    log e "No emulator found or selected. Exiting."
+    return 1
+  fi
+  
+  # If the emulator is not specified or manual mode is set, ask the user to select it via Zenity
+  if [[ -z "$emulator" && "$manual_mode" == true ]]; then
+    emulator=$(find_system_commands)
+  fi
+
+  # If emulator is still not set, fall back to the first available emulator
+  if [[ -z "$emulator" ]]; then
+    emulator=$(get_first_emulator)
+  fi
+
+}
+
+# Function to extract commands from es_systems.xml and present them in Zenity
+find_system_commands() {
+    local system_name=$system
+    # Use xmllint to extract the system commands from the XML
+    system_section=$(xmllint --xpath "//system[name='$system_name']" "$es_systems" 2>/dev/null)
+    
+    if [ -z "$system_section" ]; then
+        log e "System not found: $system_name"
+        exit 1
+    fi
+
+    # Extract commands and labels
+    commands=$(echo "$system_section" | xmllint --xpath "//command" - 2>/dev/null)
+
+    # Prepare Zenity command list
+    command_list=()
+    while IFS= read -r line; do
+        label=$(echo "$line" | sed -n 's/.*label="\([^"]*\)".*/\1/p')
+        command=$(echo "$line" | sed -n 's/.*<command[^>]*>\(.*\)<\/command>.*/\1/p')
+        
+        # Substitute placeholders in the command
+        command=$(substitute_placeholders "$command")
+        
+        # Add label and command to Zenity list (label first, command second)
+        command_list+=("$label" "$command")
+    done <<< "$commands"
+
+    # Check if there's only one command
+    if [ ${#command_list[@]} -eq 2 ]; then
+        log d "Only one command found for $system_name, running it directly: ${command_list[1]}"
+        selected_command="${command_list[1]}"
+    else
+        # Show the list with Zenity and return the **command** (second column) selected
+        selected_command=$(zenity --list \
+            --title="Select an emulator for $system_name" \
+            --column="Emulator" --column="Hidden Command" "${command_list[@]}" \
+            --width=800 --height=400 --print-column=2 --hide-column=2)
+    fi
+
+    echo "$selected_command"
+}
+
+# Function to substitute placeholders in the command
+substitute_placeholders() {
+    local cmd="$1"
+    local rom_path="$game"
+    local rom_dir=$(dirname "$rom_path")
+    
+    # Strip all file extensions from the base name
+    local base_name=$(basename "$rom_path")
+    base_name="${base_name%%.*}"
+
+    local file_name=$(basename "$rom_path")
+    local rom_raw="$rom_path"
+    local rom_dir_raw="$rom_dir"
+    local es_path=""
+    local emulator_path=""
+
+    # Manually replace %EMULATOR_*% placeholders
+    while [[ "$cmd" =~ (%EMULATOR_[A-Z0-9_]+%) ]]; do
+        placeholder="${BASH_REMATCH[1]}"
+        emulator_path=$(replace_emulator_placeholder "$placeholder")
+        cmd="${cmd//$placeholder/$emulator_path}"
+    done
+
+    # Substitute %BASENAME% and other placeholders
+    cmd="${cmd//"%BASENAME%"/"'$base_name'"}"
+    cmd="${cmd//"%FILENAME%"/"'$file_name'"}"
+    cmd="${cmd//"%ROMRAW%"/"'$rom_raw'"}"
+    cmd="${cmd//"%ROMPATH%"/"'$rom_dir'"}"
+    
+    # Ensure paths are quoted correctly
+    cmd="${cmd//"%ROM%"/"'$rom_path'"}"
+    cmd="${cmd//"%GAMEDIR%"/"'$rom_dir'"}"
+    cmd="${cmd//"%GAMEDIRRAW%"/"'$rom_dir_raw'"}"
+    cmd="${cmd//"%CORE_RETROARCH%"/"/var/config/retroarch/cores"}"
+
+    log d "Command after %BASENAME% and other substitutions: $cmd"
+
+    # Now handle %INJECT% after %BASENAME% has been substituted
+    cmd=$(handle_inject_placeholder "$cmd")
+
+    echo "$cmd"
+}
+
+# Function to replace %EMULATOR_SOMETHING% with the actual path of the emulator
+replace_emulator_placeholder() {
+    local placeholder=$1
+    # Extract emulator name from placeholder without changing case
+    local emulator_name="${placeholder//"%EMULATOR_"/}"  # Extract emulator name after %EMULATOR_
+    emulator_name="${emulator_name//"%"/}"  # Remove the trailing %
+
+    # Use the find_emulator function to get the emulator path using the correct casing
+    local emulator_exec=$(find_emulator "$emulator_name")
+    
+    if [[ -z "$emulator_exec" ]]; then
+        log e "Emulator '$emulator_name' not found."
+        exit 1
+    fi
+    echo "$emulator_exec"
+}
+
+# Function to handle the %INJECT% placeholder
+handle_inject_placeholder() {
+    local cmd="$1"
+    local rom_dir=$(dirname "$game") # Get the ROM directory based on the game path
+
+    # Find and process all occurrences of %INJECT%='something'.extension
+    while [[ "$cmd" =~ (%INJECT%=\'([^\']+)\')(.[^ ]+)? ]]; do
+        inject_file="${BASH_REMATCH[2]}"  # Extract the quoted file name
+        extension="${BASH_REMATCH[3]}"    # Extract the extension (if any)
+        inject_file_full_path="$rom_dir/$inject_file$extension"  # Form the full path
+
+        log d "Found inject part: %INJECT%='$inject_file'$extension"
+
+        # Check if the file exists
+        if [[ -f "$inject_file_full_path" ]]; then
+            # Read the content of the file and replace newlines with spaces
+            inject_content=$(cat "$inject_file_full_path" | tr '\n' ' ')
+            log i "File \"$inject_file_full_path\" found. Replacing %INJECT% with content."
+
+            # Escape special characters in the inject part for the replacement
+            escaped_inject_part=$(printf '%s' "%INJECT%='$inject_file'$extension" | sed 's/[]\/$*.^[]/\\&/g')
+
+            # Replace the entire %INJECT%=...'something'.extension part with the file content
+            cmd=$(echo "$cmd" | sed "s|$escaped_inject_part|$inject_content|g")
+
+            log d "Replaced cmd: $cmd"
+        else
+            log e "File \"$inject_file_full_path\" not found. Removing %INJECT% placeholder."
+
+            # Use sed to remove the entire %INJECT%=...'something'.extension
+            escaped_inject_part=$(printf '%s' "%INJECT%='$inject_file'$extension" | sed 's/[]\/$*.^[]/\\&/g')
+            cmd=$(echo "$cmd" | sed "s|$escaped_inject_part||g")
+
+            log d "sedded cmd: $cmd"
+        fi
+    done
+
+    log d "Returning the command with injected content: $cmd"
+    echo "$cmd"
+}
+
+# Function to get the first available emulator in the list
+get_first_emulator() {
+    local system_name=$system
+    system_section=$(xmllint --xpath "//system[name='$system_name']" "$es_systems" 2>/dev/null)
+
+    if [ -z "$system_section" ]; then
+        log e "System not found: $system_name"
+        exit 1
+    fi
+
+    # Extract the first command and use it as the selected emulator
+    first_command=$(echo "$system_section" | xmllint --xpath "string(//command[1])" - 2>/dev/null)
+
+    if [[ -n "$first_command" ]]; then
+        # Substitute placeholders in the command
+        first_command=$(substitute_placeholders "$first_command")
+        log d "Automatically selected the first emulator: $first_command"
+        echo "$first_command"
+    else
+        log e "No command found for the system: $system_name"
+        return 1
+    fi
+}
+
 find_emulator() {
   local emulator_name=$1
   local found_path=""
@@ -935,210 +1211,18 @@ find_emulator() {
   fi
 }
 
+# Function to find the emulator name from the label in es_systems.xml
+find_emulator_name_from_label() {
+    local label="$1"
+    
+    # Search for the emulator matching the label in the es_systems.xml file
+    extracted_emulator_name=$(xmllint --recover --xpath "string(//system[name='$system']/command[@label='$label']/text())" "$es_systems" 2>/dev/null | sed 's/%//g' | sed 's/EMULATOR_//g' | cut -d' ' -f1)
 
-# TODO: add the logic of alt emulator and default emulator
-
-run_game() {
-  # call me with (-e emulator) (-s system) game/path, examples:
-  # run_game -e gambatte_libretro ~/retrodeck/roms/gb/Capumon.gb
-  # run_game ~/retrodeck/roms/gb/Capumon.gb
-  # run_game -s gbc ~/retrodeck/roms/gb/Capumon.gb
-
-  # Initialize variables
-  emulator=""
-  system=""
-  es_systems="/app/share/es-de/resources/systems/linux/es_systems.xml"
-
-  # Parse options
-  while getopts ":e:s:" opt; do
-      case ${opt} in
-          e )
-              emulator=$OPTARG
-              ;;
-          s )
-              system=$OPTARG
-              ;;
-          \? )
-              echo "Usage: $0 --run [-e emulator] [-s system] game"
-              exit 1
-              ;;
-      esac
-  done
-  shift $((OPTIND -1))
-
-  # Check for game argument
-  if [[ -z "$1" ]]; then
-      log e "Game path is required."
-      log i "Usage: $0 start [-e emulator] [-s system] game"
-      exit 1
-  fi
-
-  game=$1
-
-  # If no system is provided, extract it from the game path
-  if [[ -z "$system" ]]; then
-    system=$(echo "$game" | grep -oP '(?<=roms/)[^/]+')
-  fi
-
-  log d "Game: \"$game\""
-  log d "System: \"$system\""
-
-  # Function to handle the %INJECT% placeholder
-  # When %INJECT%=filename is found in the game command it will check for the existence of the file
-  # If the file is found the "%INJECT%=file" will be replaced with the contents of the found file
-  handle_inject_placeholder() {
-      local cmd="$1"
-      local rom_dir=$(dirname "$game") # Get the ROM directory based on the game path
-
-      # Find and process all occurrences of %INJECT%='something'.extension
-      while [[ "$cmd" =~ (%INJECT%=\'([^\']+)\')(.[^ ]+)? ]]; do
-          inject_file="${BASH_REMATCH[2]}"  # Extract the quoted file name
-          extension="${BASH_REMATCH[3]}"    # Extract the extension (if any)
-          inject_file_full_path="$rom_dir/$inject_file$extension"  # Form the full path
-
-          log d "Found inject part: %INJECT%='$inject_file'$extension"
-
-          # Check if the file exists
-          if [[ -f "$inject_file_full_path" ]]; then
-              # Read the content of the file and replace newlines with spaces
-              inject_content=$(cat "$inject_file_full_path" | tr '\n' ' ')
-              log i "File \"$inject_file_full_path\" found. Replacing %INJECT% with content."
-
-              # Escape special characters in the inject part for the replacement
-              escaped_inject_part=$(printf '%s' "%INJECT%='$inject_file'$extension" | sed 's/[]\/$*.^[]/\\&/g')
-
-              # Replace the entire %INJECT%=...'something'.extension part with the file content
-              cmd=$(echo "$cmd" | sed "s|$escaped_inject_part|$inject_content|g")
-
-              log d "Replaced cmd: $cmd"
-          else
-              log e "File \"$inject_file_full_path\" not found. Removing %INJECT% placeholder."
-
-              # Use sed to remove the entire %INJECT%=...'something'.extension
-              escaped_inject_part=$(printf '%s' "%INJECT%='$inject_file'$extension" | sed 's/[]\/$*.^[]/\\&/g')
-              cmd=$(echo "$cmd" | sed "s|$escaped_inject_part||g")
-
-              log d "sedded cmd: $cmd"
-          fi
-      done
-
-      log d "Returning the command with injected content: $cmd"
-      echo "$cmd"
-  }
-
-
-  # Function to replace %EMULATOR_SOMETHING% with the actual path of the emulator
-  replace_emulator_placeholder() {
-      local placeholder=$1
-      # Extract emulator name from placeholder without changing case
-      local emulator_name="${placeholder//"%EMULATOR_"/}"  # Extract emulator name after %EMULATOR_
-      emulator_name="${emulator_name//"%"/}"  # Remove the trailing %
-
-      # Use the find_emulator function to get the emulator path using the correct casing
-      local emulator_exec=$(find_emulator "$emulator_name")
-      
-      if [[ -z "$emulator_exec" ]]; then
-          log e "Emulator '$emulator_name' not found."
-          exit 1
-      fi
-      echo "$emulator_exec"
-  }
-
-  # Function to substitute the placeholders
-  substitute_placeholders() {
-      local cmd="$1"
-      local rom_path="$game"
-      local rom_dir=$(dirname "$rom_path")
-      
-      # Strip all file extensions from the base name
-      local base_name=$(basename "$rom_path")
-      base_name="${base_name%%.*}"
-
-      local file_name=$(basename "$rom_path")
-      local rom_raw="$rom_path"
-      local rom_dir_raw="$rom_dir"
-      local es_path=""
-      local emulator_path=""
-
-      # Manually replace %EMULATOR_*% placeholders
-      while [[ "$cmd" =~ (%EMULATOR_[A-Z0-9_]+%) ]]; do
-          placeholder="${BASH_REMATCH[1]}"
-          emulator_path=$(replace_emulator_placeholder "$placeholder")
-          cmd="${cmd//$placeholder/$emulator_path}"
-      done
-
-      # Substitute %BASENAME% and other placeholders
-      cmd="${cmd//"%BASENAME%"/"'$base_name'"}"
-      cmd="${cmd//"%FILENAME%"/"'$file_name'"}"
-      cmd="${cmd//"%ROMRAW%"/"'$rom_raw'"}"
-      cmd="${cmd//"%ROMPATH%"/"'$rom_dir'"}"
-      
-      # Ensure paths are quoted correctly
-      cmd="${cmd//"%ROM%"/"'$rom_path'"}"
-      cmd="${cmd//"%GAMEDIR%"/"'$rom_dir'"}"
-      cmd="${cmd//"%GAMEDIRRAW%"/"'$rom_dir_raw'"}"
-      cmd="${cmd//"%CORE_RETROARCH%"/"/var/config/retroarch/cores"}"
-
-      log d "Command after %BASENAME% and other substitutions: $cmd"
-
-      # Now handle %INJECT% after %BASENAME% has been substituted
-      cmd=$(handle_inject_placeholder "$cmd")
-
-      echo "$cmd"
-  }
-
-  # Extracting the commands from es_systems.xml for the selected system
-  find_system_commands() {
-      local system_name=$system
-      # Use xmllint to extract the system commands from the XML
-      system_section=$(xmllint --xpath "//system[name='$system_name']" "$es_systems" 2>/dev/null)
-      
-      if [ -z "$system_section" ]; then
-          log e "System not found: $system_name"
-          exit 1
-      fi
-
-      # Extract commands and labels
-      commands=$(echo "$system_section" | xmllint --xpath "//command" - 2>/dev/null)
-
-      # Prepare Zenity command list
-      command_list=()
-      while IFS= read -r line; do
-          label=$(echo "$line" | sed -n 's/.*label="\([^"]*\)".*/\1/p')
-          command=$(echo "$line" | sed -n 's/.*<command[^>]*>\(.*\)<\/command>.*/\1/p')
-          
-          # Substitute placeholders in the command
-          command=$(substitute_placeholders "$command")
-          
-          # Add label and command to Zenity list (label first, command second)
-          command_list+=("$label" "$command")
-      done <<< "$commands"
-
-      # Check if there's only one command
-      if [ ${#command_list[@]} -eq 2 ]; then
-          log d "Only one command found for $system_name, running it directly: ${command_list[1]}"
-          selected_command="${command_list[1]}"
-      else
-          # Show the list with Zenity and return the **command** (second column) selected
-          selected_command=$(zenity --list \
-              --title="Select an emulator for $system_name" \
-              --column="Emulator" --column="Hidden Command" "${command_list[@]}" \
-              --width=800 --height=400 --print-column=2 --hide-column=2)
-      fi
-
-      echo "$selected_command"
-  }
-
-  # If the emulator is not specified, ask the user to select it or get it from the XML file
-  if [[ -z "$emulator" ]]; then
-    emulator=$(find_system_commands)
-  fi
-
-  if [[ -n "$emulator" ]]; then
-    log d "Running: $emulator"
-    eval "$emulator"
-  else
-    log e "No emulator found or selected. Exiting."
-    return 1
-  fi
-}
\ No newline at end of file
+    if [[ -n "$extracted_emulator_name" ]]; then
+        log d "Found emulator by label: $extracted_emulator_name"
+        echo "$extracted_emulator_name"
+    else
+        log e "Emulator name not found for label: $label"
+        return 1
+    fi
+}

From 6b92f8c187641df26db367b73a92d5adad523bb8 Mon Sep 17 00:00:00 2001
From: XargonWan <XargonWan@gmail.com>
Date: Thu, 12 Sep 2024 08:25:07 +0900
Subject: [PATCH 2/5] RUN_GAME: moved into its own file + code cleanup +
 es_variables outsourced [skip ci]

---
 functions/global.sh          |  51 +++---
 functions/other_functions.sh | 337 +----------------------------------
 functions/run_game.sh        | 334 ++++++++++++++++++++++++++++++++++
 3 files changed, 362 insertions(+), 360 deletions(-)
 create mode 100755 functions/run_game.sh

diff --git a/functions/global.sh b/functions/global.sh
index b0a62c0f..5792a51a 100644
--- a/functions/global.sh
+++ b/functions/global.sh
@@ -18,40 +18,43 @@ source /app/libexec/post_update.sh
 source /app/libexec/prepare_component.sh
 source /app/libexec/presets.sh
 source /app/libexec/configurator_functions.sh
+source /app/libexec/run_game.sh
 
 # Static variables
-rd_conf="/var/config/retrodeck/retrodeck.cfg"                                                                         # RetroDECK config file path
-rd_conf_backup="/var/config/retrodeck/retrodeck.bak"                                                                  # Backup of RetroDECK config file from update
-rd_logs_folder="/var/config/retrodeck/logs"                                                                           # Static location to write all RetroDECK-related logs
-config="/app/retrodeck/config"                                                                               # folder with all the default emulator configs
+rd_conf="/var/config/retrodeck/retrodeck.cfg"                                                            # RetroDECK config file path
+rd_conf_backup="/var/config/retrodeck/retrodeck.bak"                                                     # Backup of RetroDECK config file from update
+rd_logs_folder="/var/config/retrodeck/logs"                                                              # Static location to write all RetroDECK-related logs
+config="/app/retrodeck/config"                                                                           # folder with all the default emulator configs
 rd_defaults="$config/retrodeck/retrodeck.cfg"                                                            # A default RetroDECK config file
-rd_update_patch="/var/config/retrodeck/rd_update.patch"                                                               # A static location for the temporary patch file used during retrodeck.cfg updates
+rd_update_patch="/var/config/retrodeck/rd_update.patch"                                                  # A static location for the temporary patch file used during retrodeck.cfg updates
 bios_checklist="$config/retrodeck/reference_lists/bios_checklist.cfg"                                    # A config file listing BIOS file information that can be verified
 input_validation="$config/retrodeck/reference_lists/input_validation.cfg"                                # A config file listing valid CLI inputs
 finit_options_list="$config/retrodeck/reference_lists/finit_options_list.cfg"                            # A config file listing available optional installs during finit
-splashscreen_dir="/var/config/ES-DE/resources/graphics/extra_splashes"                                                # The default location of extra splash screens
-current_splash_file="/var/config/ES-DE/resources/graphics/splash.svg"                                                 # The active splash file that will be shown on boot
-default_splash_file="/var/config/ES-DE/resources/graphics/splash-orig.svg"                                            # The default RetroDECK splash screen
+splashscreen_dir="/var/config/ES-DE/resources/graphics/extra_splashes"                                   # The default location of extra splash screens
+current_splash_file="/var/config/ES-DE/resources/graphics/splash.svg"                                    # The active splash file that will be shown on boot
+default_splash_file="/var/config/ES-DE/resources/graphics/splash-orig.svg"                               # The default RetroDECK splash screen
 # TODO: instead of this maybe we can iterate the features.json
 multi_user_emulator_config_dirs="$config/retrodeck/reference_lists/multi_user_emulator_config_dirs.cfg"  # A list of emulator config folders that can be safely linked/unlinked entirely in multi-user mode
-rd_es_themes="/app/share/es-de/themes"                                                                                # The directory where themes packaged with RetroDECK are stored
-lockfile="/var/config/retrodeck/.lock"                                                                                # Where the lockfile is located
-default_sd="/run/media/mmcblk0p1"                                                                                     # Steam Deck SD default path                                                                        # A static location for RetroDECK logs to be written
-hard_version="$(cat '/app/retrodeck/version')"                                                                        # hardcoded version (in the readonly filesystem)
-rd_repo="https://github.com/RetroDECK/RetroDECK"                                                                      # The URL of the main RetroDECK GitHub repo
-es_themes_list="https://gitlab.com/es-de/themes/themes-list/-/raw/master/themes.json"                                 # The URL of the ES-DE 2.0 themes list
-remote_network_target_1="https://flathub.org"                                                                         # The URL of a common internet target for testing network access
-remote_network_target_2="$rd_repo"                                                                                    # The URL of a common internet target for testing network access
-remote_network_target_3="https://one.one.one.one"                                                                     # The URL of a common internet target for testing network access
+rd_es_themes="/app/share/es-de/themes"                                                                   # The directory where themes packaged with RetroDECK are stored
+lockfile="/var/config/retrodeck/.lock"                                                                   # Where the lockfile is located
+default_sd="/run/media/mmcblk0p1"                                                                        # Steam Deck SD default path
+hard_version="$(cat '/app/retrodeck/version')"                                                           # hardcoded version (in the readonly filesystem)
+rd_repo="https://github.com/RetroDECK/RetroDECK"                                                         # The URL of the main RetroDECK GitHub repo
+es_themes_list="https://gitlab.com/es-de/themes/themes-list/-/raw/master/themes.json"                    # The URL of the ES-DE 2.0 themes list
+remote_network_target_1="https://flathub.org"                                                            # The URL of a common internet target for testing network access
+remote_network_target_2="$rd_repo"                                                                       # The URL of a common internet target for testing network access
+remote_network_target_3="https://one.one.one.one"                                                        # The URL of a common internet target for testing network access
 helper_files_folder="$config/retrodeck/helper_files"                                                     # The parent folder of RetroDECK documentation files for deployment
-rd_appdata="/app/share/appdata/net.retrodeck.retrodeck.appdata.xml"                                                   # The shipped appdata XML file for this version
-rpcs3_firmware="http://dus01.ps3.update.playstation.net/update/ps3/image/us/2024_0227_3694eb3fb8d9915c112e6ab41a60c69f/PS3UPDAT.PUP"
-RA_API_URL="https://retroachievements.org/dorequest.php"                                                              # API URL for RetroAchievements.org
+rd_appdata="/app/share/appdata/net.retrodeck.retrodeck.appdata.xml"                                      # The shipped appdata XML file for this version
+rpcs3_firmware="http://dus01.ps3.update.playstation.net/update/ps3/image/us/2024_0227_3694eb3fb8d9915c112e6ab41a60c69f/PS3UPDAT.PUP" # RPCS3 Firmware download location
+RA_API_URL="https://retroachievements.org/dorequest.php"                                                 # API URL for RetroAchievements.org
 presets_dir="$config/retrodeck/presets"                                                                  # Repository for all system preset config files
-git_organization_name="RetroDECK"                                                                       # The name of the organization in our git repository such as GitHub
-cooker_repository_name="Cooker"                                                                         # The name of the cooker repository under RetroDECK organization
-main_repository_name="RetroDECK"                                                                        # The name of the main repository under RetroDECK organization
-features="$config/retrodeck/reference_lists/features.json"                                                 # A file where all the RetroDECK and component capabilities are kept for querying
+git_organization_name="RetroDECK"                                                                        # The name of the organization in our git repository such as GitHub
+cooker_repository_name="Cooker"                                                                          # The name of the cooker repository under RetroDECK organization
+main_repository_name="RetroDECK"                                                                         # The name of the main repository under RetroDECK organization
+features="$config/retrodeck/reference_lists/features.json"                                               # A file where all the RetroDECK and component capabilities are kept for querying
+es_systems="/app/share/es-de/resources/systems/linux/es_systems.xml"                                     # ES-DE supported system list   
+es_find_rules="/app/share/es-de/resources/systems/linux/es_find_rules.xml"                               # ES-DE emulator find rules
 
 
 # Godot data transfer temp files
diff --git a/functions/other_functions.sh b/functions/other_functions.sh
index 4e58ebcf..f8d6a012 100644
--- a/functions/other_functions.sh
+++ b/functions/other_functions.sh
@@ -890,339 +890,4 @@ start_retrodeck() {
   ponzu
   log i "Starting RetroDECK v$version"
   es-de
-}
-
-run_game() {
-  # Initialize variables
-  emulator=""
-  system=""
-  manual_mode=false
-  es_systems="/app/share/es-de/resources/systems/linux/es_systems.xml"
-
-  # Parse options
-  while getopts ":e:s:m" opt; do  # Use `m` for manual mode flag
-      case ${opt} in
-          e )
-              emulator=$OPTARG
-              ;;
-          s )
-              system=$OPTARG
-              ;;
-          m )
-              manual_mode=true
-              log i "Run game: manual mode enabled"
-              ;;
-          \? )
-              echo "Usage: $0 --run [-e emulator] [-s system] [-m manual] game"
-              exit 1
-              ;;
-      esac
-  done
-  shift $((OPTIND -1))
-
-  # Check for game argument
-  if [[ -z "$1" ]]; then
-      log e "Game path is required."
-      log i "Usage: $0 start [-e emulator] [-s system] [-m manual] game"
-      exit 1
-  fi
-
-  game=$1
-
-  # If no system is provided, extract it from the game path
-  if [[ -z "$system" ]]; then
-    system=$(echo "$game" | grep -oP '(?<=roms/)[^/]+')
-  fi
-
-  log d "Game: \"$game\""
-  log d "System: \"$system\""
-  
-  # Try finding the <altemulator> inside the specific game block
-  altemulator=$(xmllint --recover --xpath "string(//game[path='$game']/altemulator)" "$rdhome/ES-DE/gamelists/$system/gamelist.xml" 2>/dev/null)
-
-  if [[ -n "$altemulator" ]]; then
-      log d "Alternate emulator found in <altemulator>: $altemulator"
-      emulator_name=$(echo "$altemulator" | sed -e 's/ (Standalone)//')  # Strip " (Standalone)" from name
-      emulator=$(find_emulator "$emulator_name")
-
-      if [[ -n "$emulator" ]]; then
-          log d "Using alternate emulator: $emulator"
-      else
-          log e "No valid path found for emulator: $altemulator"
-          exit 1
-      fi
-  else
-    # Try to fetch <alternativeEmulator> from anywhere in the document
-    alternative_emulator=$(xmllint --recover --xpath 'string(//alternativeEmulator/label)' "$rdhome/ES-DE/gamelists/$system/gamelist.xml" 2>/dev/null)
-
-    if [[ -n "$alternative_emulator" ]]; then
-        log d "Alternate emulator found in <alternativeEmulator> header: $alternative_emulator"
-        
-        # Find the emulator name from the label in es_systems.xml
-        emulator_name=$(find_emulator_name_from_label "$alternative_emulator")
-        
-        if [[ -n "$emulator_name" ]]; then
-            # Pass the extracted emulator name to find_emulator function
-            emulator=$(find_emulator "$emulator_name")
-        fi
-        
-        if [[ -n "$emulator" ]]; then
-            log d "Using alternate emulator from <alternativeEmulator>: $emulator"
-        else
-            log e "No valid path found for emulator: $alternative_emulator"
-            exit 1
-        fi
-    else
-        log i "No alternate emulator found in game block or header, proceeding to auto mode."
-    fi
-  fi
-
-  # If an emulator is found, substitute placeholders in the command before running
-  if [[ -n "$emulator" ]]; then
-    # Ensure command substitution
-    find_system_commands "$emulator"
-    # TODO: almost there, we need just to start the emulator without Zenity: maybe we have to edit THAT function to pass the emulator to run
-    log d "Final command: $command"
-    eval "$command"
-  else
-    log e "No emulator found or selected. Exiting."
-    return 1
-  fi
-  
-  # If the emulator is not specified or manual mode is set, ask the user to select it via Zenity
-  if [[ -z "$emulator" && "$manual_mode" == true ]]; then
-    emulator=$(find_system_commands)
-  fi
-
-  # If emulator is still not set, fall back to the first available emulator
-  if [[ -z "$emulator" ]]; then
-    emulator=$(get_first_emulator)
-  fi
-
-}
-
-# Function to extract commands from es_systems.xml and present them in Zenity
-find_system_commands() {
-    local system_name=$system
-    # Use xmllint to extract the system commands from the XML
-    system_section=$(xmllint --xpath "//system[name='$system_name']" "$es_systems" 2>/dev/null)
-    
-    if [ -z "$system_section" ]; then
-        log e "System not found: $system_name"
-        exit 1
-    fi
-
-    # Extract commands and labels
-    commands=$(echo "$system_section" | xmllint --xpath "//command" - 2>/dev/null)
-
-    # Prepare Zenity command list
-    command_list=()
-    while IFS= read -r line; do
-        label=$(echo "$line" | sed -n 's/.*label="\([^"]*\)".*/\1/p')
-        command=$(echo "$line" | sed -n 's/.*<command[^>]*>\(.*\)<\/command>.*/\1/p')
-        
-        # Substitute placeholders in the command
-        command=$(substitute_placeholders "$command")
-        
-        # Add label and command to Zenity list (label first, command second)
-        command_list+=("$label" "$command")
-    done <<< "$commands"
-
-    # Check if there's only one command
-    if [ ${#command_list[@]} -eq 2 ]; then
-        log d "Only one command found for $system_name, running it directly: ${command_list[1]}"
-        selected_command="${command_list[1]}"
-    else
-        # Show the list with Zenity and return the **command** (second column) selected
-        selected_command=$(zenity --list \
-            --title="Select an emulator for $system_name" \
-            --column="Emulator" --column="Hidden Command" "${command_list[@]}" \
-            --width=800 --height=400 --print-column=2 --hide-column=2)
-    fi
-
-    echo "$selected_command"
-}
-
-# Function to substitute placeholders in the command
-substitute_placeholders() {
-    local cmd="$1"
-    local rom_path="$game"
-    local rom_dir=$(dirname "$rom_path")
-    
-    # Strip all file extensions from the base name
-    local base_name=$(basename "$rom_path")
-    base_name="${base_name%%.*}"
-
-    local file_name=$(basename "$rom_path")
-    local rom_raw="$rom_path"
-    local rom_dir_raw="$rom_dir"
-    local es_path=""
-    local emulator_path=""
-
-    # Manually replace %EMULATOR_*% placeholders
-    while [[ "$cmd" =~ (%EMULATOR_[A-Z0-9_]+%) ]]; do
-        placeholder="${BASH_REMATCH[1]}"
-        emulator_path=$(replace_emulator_placeholder "$placeholder")
-        cmd="${cmd//$placeholder/$emulator_path}"
-    done
-
-    # Substitute %BASENAME% and other placeholders
-    cmd="${cmd//"%BASENAME%"/"'$base_name'"}"
-    cmd="${cmd//"%FILENAME%"/"'$file_name'"}"
-    cmd="${cmd//"%ROMRAW%"/"'$rom_raw'"}"
-    cmd="${cmd//"%ROMPATH%"/"'$rom_dir'"}"
-    
-    # Ensure paths are quoted correctly
-    cmd="${cmd//"%ROM%"/"'$rom_path'"}"
-    cmd="${cmd//"%GAMEDIR%"/"'$rom_dir'"}"
-    cmd="${cmd//"%GAMEDIRRAW%"/"'$rom_dir_raw'"}"
-    cmd="${cmd//"%CORE_RETROARCH%"/"/var/config/retroarch/cores"}"
-
-    log d "Command after %BASENAME% and other substitutions: $cmd"
-
-    # Now handle %INJECT% after %BASENAME% has been substituted
-    cmd=$(handle_inject_placeholder "$cmd")
-
-    echo "$cmd"
-}
-
-# Function to replace %EMULATOR_SOMETHING% with the actual path of the emulator
-replace_emulator_placeholder() {
-    local placeholder=$1
-    # Extract emulator name from placeholder without changing case
-    local emulator_name="${placeholder//"%EMULATOR_"/}"  # Extract emulator name after %EMULATOR_
-    emulator_name="${emulator_name//"%"/}"  # Remove the trailing %
-
-    # Use the find_emulator function to get the emulator path using the correct casing
-    local emulator_exec=$(find_emulator "$emulator_name")
-    
-    if [[ -z "$emulator_exec" ]]; then
-        log e "Emulator '$emulator_name' not found."
-        exit 1
-    fi
-    echo "$emulator_exec"
-}
-
-# Function to handle the %INJECT% placeholder
-handle_inject_placeholder() {
-    local cmd="$1"
-    local rom_dir=$(dirname "$game") # Get the ROM directory based on the game path
-
-    # Find and process all occurrences of %INJECT%='something'.extension
-    while [[ "$cmd" =~ (%INJECT%=\'([^\']+)\')(.[^ ]+)? ]]; do
-        inject_file="${BASH_REMATCH[2]}"  # Extract the quoted file name
-        extension="${BASH_REMATCH[3]}"    # Extract the extension (if any)
-        inject_file_full_path="$rom_dir/$inject_file$extension"  # Form the full path
-
-        log d "Found inject part: %INJECT%='$inject_file'$extension"
-
-        # Check if the file exists
-        if [[ -f "$inject_file_full_path" ]]; then
-            # Read the content of the file and replace newlines with spaces
-            inject_content=$(cat "$inject_file_full_path" | tr '\n' ' ')
-            log i "File \"$inject_file_full_path\" found. Replacing %INJECT% with content."
-
-            # Escape special characters in the inject part for the replacement
-            escaped_inject_part=$(printf '%s' "%INJECT%='$inject_file'$extension" | sed 's/[]\/$*.^[]/\\&/g')
-
-            # Replace the entire %INJECT%=...'something'.extension part with the file content
-            cmd=$(echo "$cmd" | sed "s|$escaped_inject_part|$inject_content|g")
-
-            log d "Replaced cmd: $cmd"
-        else
-            log e "File \"$inject_file_full_path\" not found. Removing %INJECT% placeholder."
-
-            # Use sed to remove the entire %INJECT%=...'something'.extension
-            escaped_inject_part=$(printf '%s' "%INJECT%='$inject_file'$extension" | sed 's/[]\/$*.^[]/\\&/g')
-            cmd=$(echo "$cmd" | sed "s|$escaped_inject_part||g")
-
-            log d "sedded cmd: $cmd"
-        fi
-    done
-
-    log d "Returning the command with injected content: $cmd"
-    echo "$cmd"
-}
-
-# Function to get the first available emulator in the list
-get_first_emulator() {
-    local system_name=$system
-    system_section=$(xmllint --xpath "//system[name='$system_name']" "$es_systems" 2>/dev/null)
-
-    if [ -z "$system_section" ]; then
-        log e "System not found: $system_name"
-        exit 1
-    fi
-
-    # Extract the first command and use it as the selected emulator
-    first_command=$(echo "$system_section" | xmllint --xpath "string(//command[1])" - 2>/dev/null)
-
-    if [[ -n "$first_command" ]]; then
-        # Substitute placeholders in the command
-        first_command=$(substitute_placeholders "$first_command")
-        log d "Automatically selected the first emulator: $first_command"
-        echo "$first_command"
-    else
-        log e "No command found for the system: $system_name"
-        return 1
-    fi
-}
-
-find_emulator() {
-  local emulator_name=$1
-  local found_path=""
-  local es_find_rules="/app/share/es-de/resources/systems/linux/es_find_rules.xml"
-
-  # Search the es_find_rules.xml file for the emulator
-  emulator_section=$(xmllint --xpath "//emulator[@name='$emulator_name']" "$es_find_rules" 2>/dev/null)
-  
-  if [ -z "$emulator_section" ]; then
-      log e "Emulator not found: $emulator_name"
-      return 1
-  fi
-  
-  # Search systempath entries
-  while IFS= read -r line; do
-      command_path=$(echo "$line" | sed -n 's/.*<entry>\(.*\)<\/entry>.*/\1/p')
-      if [ -x "$(command -v $command_path)" ]; then
-          found_path=$command_path
-          break
-      fi
-  done <<< "$(echo "$emulator_section" | xmllint --xpath "//rule[@type='systempath']/entry" - 2>/dev/null)"
-  
-  # If not found, search staticpath entries
-  if [ -z "$found_path" ]; then
-      while IFS= read -r line; do
-          command_path=$(eval echo "$line" | sed -n 's/.*<entry>\(.*\)<\/entry>.*/\1/p')
-          if [ -x "$command_path" ]; then
-              found_path=$command_path
-              break
-          fi
-      done <<< "$(echo "$emulator_section" | xmllint --xpath "//rule[@type='staticpath']/entry" - 2>/dev/null)"
-  fi
-  
-  if [ -z "$found_path" ]; then
-      log e "No valid path found for emulator: $emulator_name"
-      return 1
-  else
-      log d "Found emulator: \"$found_path\""
-      echo "$found_path"
-      return 0
-  fi
-}
-
-# Function to find the emulator name from the label in es_systems.xml
-find_emulator_name_from_label() {
-    local label="$1"
-    
-    # Search for the emulator matching the label in the es_systems.xml file
-    extracted_emulator_name=$(xmllint --recover --xpath "string(//system[name='$system']/command[@label='$label']/text())" "$es_systems" 2>/dev/null | sed 's/%//g' | sed 's/EMULATOR_//g' | cut -d' ' -f1)
-
-    if [[ -n "$extracted_emulator_name" ]]; then
-        log d "Found emulator by label: $extracted_emulator_name"
-        echo "$extracted_emulator_name"
-    else
-        log e "Emulator name not found for label: $label"
-        return 1
-    fi
-}
+}
\ No newline at end of file
diff --git a/functions/run_game.sh b/functions/run_game.sh
new file mode 100755
index 00000000..c6cee211
--- /dev/null
+++ b/functions/run_game.sh
@@ -0,0 +1,334 @@
+#!/bin/bash
+
+run_game() {
+  # Initialize variables
+  emulator=""
+  system=""
+  manual_mode=false
+
+  # Parse options
+  while getopts ":e:s:m" opt; do  # Use `m` for manual mode flag
+      case ${opt} in
+          e )
+              emulator=$OPTARG
+              ;;
+          s )
+              system=$OPTARG
+              ;;
+          m )
+              manual_mode=true
+              log i "Run game: manual mode enabled"
+              ;;
+          \? )
+              echo "Usage: $0 --run [-e emulator] [-s system] [-m manual] game"
+              exit 1
+              ;;
+      esac
+  done
+  shift $((OPTIND -1))
+
+  # Check for game argument
+  if [[ -z "$1" ]]; then
+      log e "Game path is required."
+      log i "Usage: $0 start [-e emulator] [-s system] [-m manual] game"
+      exit 1
+  fi
+
+  game=$1
+
+  # If no system is provided, extract it from the game path
+  if [[ -z "$system" ]]; then
+    system=$(echo "$game" | grep -oP '(?<=roms/)[^/]+')
+  fi
+
+  log d "Game: \"$game\""
+  log d "System: \"$system\""
+  
+  # Try finding the <altemulator> inside the specific game block
+  altemulator=$(xmllint --recover --xpath "string(//game[path='$game']/altemulator)" "$rdhome/ES-DE/gamelists/$system/gamelist.xml" 2>/dev/null)
+
+  if [[ -n "$altemulator" ]]; then
+      log d "Alternate emulator found in <altemulator>: $altemulator"
+      emulator_name=$(echo "$altemulator" | sed -e 's/ (Standalone)//')  # Strip " (Standalone)" from name
+      emulator=$(find_emulator "$emulator_name")
+
+      if [[ -n "$emulator" ]]; then
+          log d "Using alternate emulator: $emulator"
+      else
+          log e "No valid path found for emulator: $altemulator"
+          exit 1
+      fi
+  else
+    # Try to fetch <alternativeEmulator> from anywhere in the document
+    alternative_emulator=$(xmllint --recover --xpath 'string(//alternativeEmulator/label)' "$rdhome/ES-DE/gamelists/$system/gamelist.xml" 2>/dev/null)
+
+    if [[ -n "$alternative_emulator" ]]; then
+        log d "Alternate emulator found in <alternativeEmulator> header: $alternative_emulator"
+        
+        # Find the emulator name from the label in es_systems.xml
+        emulator_name=$(find_emulator_name_from_label "$alternative_emulator")
+        
+        if [[ -n "$emulator_name" ]]; then
+            # Pass the extracted emulator name to find_emulator function
+            emulator=$(find_emulator "$emulator_name")
+        fi
+        
+        if [[ -n "$emulator" ]]; then
+            log d "Using alternate emulator from <alternativeEmulator>: $emulator"
+        else
+            log e "No valid path found for emulator: $alternative_emulator"
+            exit 1
+        fi
+    else
+        log i "No alternate emulator found in game block or header, proceeding to auto mode."
+    fi
+  fi
+
+  # If an emulator is found, substitute placeholders in the command before running
+  if [[ -n "$emulator" ]]; then
+    # Ensure command substitution
+    find_system_commands "$emulator"
+    # TODO: almost there, we need just to start the emulator without Zenity: maybe we have to edit THAT function to pass the emulator to run
+    log d "Final command: $command"
+    eval "$command"
+  else
+    log e "No emulator found or selected. Exiting."
+    return 1
+  fi
+  
+  # If the emulator is not specified or manual mode is set, ask the user to select it via Zenity
+  if [[ -z "$emulator" && "$manual_mode" == true ]]; then
+    emulator=$(find_system_commands)
+  fi
+
+  # If emulator is still not set, fall back to the first available emulator
+  if [[ -z "$emulator" ]]; then
+    emulator=$(get_first_emulator)
+  fi
+
+}
+
+# Function to extract commands from es_systems.xml and present them in Zenity
+find_system_commands() {
+    local system_name=$system
+    # Use xmllint to extract the system commands from the XML
+    system_section=$(xmllint --xpath "//system[name='$system_name']" "$es_systems" 2>/dev/null)
+    
+    if [ -z "$system_section" ]; then
+        log e "System not found: $system_name"
+        exit 1
+    fi
+
+    # Extract commands and labels
+    commands=$(echo "$system_section" | xmllint --xpath "//command" - 2>/dev/null)
+
+    # Prepare Zenity command list
+    command_list=()
+    while IFS= read -r line; do
+        label=$(echo "$line" | sed -n 's/.*label="\([^"]*\)".*/\1/p')
+        command=$(echo "$line" | sed -n 's/.*<command[^>]*>\(.*\)<\/command>.*/\1/p')
+        
+        # Substitute placeholders in the command
+        command=$(substitute_placeholders "$command")
+        
+        # Add label and command to Zenity list (label first, command second)
+        command_list+=("$label" "$command")
+    done <<< "$commands"
+
+    # Check if there's only one command
+    if [ ${#command_list[@]} -eq 2 ]; then
+        log d "Only one command found for $system_name, running it directly: ${command_list[1]}"
+        selected_command="${command_list[1]}"
+    else
+        # Show the list with Zenity and return the **command** (second column) selected
+        selected_command=$(zenity --list \
+            --title="Select an emulator for $system_name" \
+            --column="Emulator" --column="Hidden Command" "${command_list[@]}" \
+            --width=800 --height=400 --print-column=2 --hide-column=2)
+    fi
+
+    echo "$selected_command"
+}
+
+# Function to substitute placeholders in the command
+substitute_placeholders() {
+    local cmd="$1"
+    local rom_path="$game"
+    local rom_dir=$(dirname "$rom_path")
+    
+    # Strip all file extensions from the base name
+    local base_name=$(basename "$rom_path")
+    base_name="${base_name%%.*}"
+
+    local file_name=$(basename "$rom_path")
+    local rom_raw="$rom_path"
+    local rom_dir_raw="$rom_dir"
+    local es_path=""
+    local emulator_path=""
+
+    # Manually replace %EMULATOR_*% placeholders
+    while [[ "$cmd" =~ (%EMULATOR_[A-Z0-9_]+%) ]]; do
+        placeholder="${BASH_REMATCH[1]}"
+        emulator_path=$(replace_emulator_placeholder "$placeholder")
+        cmd="${cmd//$placeholder/$emulator_path}"
+    done
+
+    # Substitute %BASENAME% and other placeholders
+    cmd="${cmd//"%BASENAME%"/"'$base_name'"}"
+    cmd="${cmd//"%FILENAME%"/"'$file_name'"}"
+    cmd="${cmd//"%ROMRAW%"/"'$rom_raw'"}"
+    cmd="${cmd//"%ROMPATH%"/"'$rom_dir'"}"
+    
+    # Ensure paths are quoted correctly
+    cmd="${cmd//"%ROM%"/"'$rom_path'"}"
+    cmd="${cmd//"%GAMEDIR%"/"'$rom_dir'"}"
+    cmd="${cmd//"%GAMEDIRRAW%"/"'$rom_dir_raw'"}"
+    cmd="${cmd//"%CORE_RETROARCH%"/"/var/config/retroarch/cores"}"
+
+    log d "Command after %BASENAME% and other substitutions: $cmd"
+
+    # Now handle %INJECT% after %BASENAME% has been substituted
+    cmd=$(handle_inject_placeholder "$cmd")
+
+    echo "$cmd"
+}
+
+# Function to replace %EMULATOR_SOMETHING% with the actual path of the emulator
+replace_emulator_placeholder() {
+    local placeholder=$1
+    # Extract emulator name from placeholder without changing case
+    local emulator_name="${placeholder//"%EMULATOR_"/}"  # Extract emulator name after %EMULATOR_
+    emulator_name="${emulator_name//"%"/}"  # Remove the trailing %
+
+    # Use the find_emulator function to get the emulator path using the correct casing
+    local emulator_exec=$(find_emulator "$emulator_name")
+    
+    if [[ -z "$emulator_exec" ]]; then
+        log e "Emulator '$emulator_name' not found."
+        exit 1
+    fi
+    echo "$emulator_exec"
+}
+
+# Function to handle the %INJECT% placeholder
+handle_inject_placeholder() {
+    local cmd="$1"
+    local rom_dir=$(dirname "$game") # Get the ROM directory based on the game path
+
+    # Find and process all occurrences of %INJECT%='something'.extension
+    while [[ "$cmd" =~ (%INJECT%=\'([^\']+)\')(.[^ ]+)? ]]; do
+        inject_file="${BASH_REMATCH[2]}"  # Extract the quoted file name
+        extension="${BASH_REMATCH[3]}"    # Extract the extension (if any)
+        inject_file_full_path="$rom_dir/$inject_file$extension"  # Form the full path
+
+        log d "Found inject part: %INJECT%='$inject_file'$extension"
+
+        # Check if the file exists
+        if [[ -f "$inject_file_full_path" ]]; then
+            # Read the content of the file and replace newlines with spaces
+            inject_content=$(cat "$inject_file_full_path" | tr '\n' ' ')
+            log i "File \"$inject_file_full_path\" found. Replacing %INJECT% with content."
+
+            # Escape special characters in the inject part for the replacement
+            escaped_inject_part=$(printf '%s' "%INJECT%='$inject_file'$extension" | sed 's/[]\/$*.^[]/\\&/g')
+
+            # Replace the entire %INJECT%=...'something'.extension part with the file content
+            cmd=$(echo "$cmd" | sed "s|$escaped_inject_part|$inject_content|g")
+
+            log d "Replaced cmd: $cmd"
+        else
+            log e "File \"$inject_file_full_path\" not found. Removing %INJECT% placeholder."
+
+            # Use sed to remove the entire %INJECT%=...'something'.extension
+            escaped_inject_part=$(printf '%s' "%INJECT%='$inject_file'$extension" | sed 's/[]\/$*.^[]/\\&/g')
+            cmd=$(echo "$cmd" | sed "s|$escaped_inject_part||g")
+
+            log d "sedded cmd: $cmd"
+        fi
+    done
+
+    log d "Returning the command with injected content: $cmd"
+    echo "$cmd"
+}
+
+# Function to get the first available emulator in the list
+get_first_emulator() {
+    local system_name=$system
+    system_section=$(xmllint --xpath "//system[name='$system_name']" "$es_systems" 2>/dev/null)
+
+    if [ -z "$system_section" ]; then
+        log e "System not found: $system_name"
+        exit 1
+    fi
+
+    # Extract the first command and use it as the selected emulator
+    first_command=$(echo "$system_section" | xmllint --xpath "string(//command[1])" - 2>/dev/null)
+
+    if [[ -n "$first_command" ]]; then
+        # Substitute placeholders in the command
+        first_command=$(substitute_placeholders "$first_command")
+        log d "Automatically selected the first emulator: $first_command"
+        echo "$first_command"
+    else
+        log e "No command found for the system: $system_name"
+        return 1
+    fi
+}
+
+find_emulator() {
+  local emulator_name=$1
+  local found_path=""
+
+  # Search the es_find_rules.xml file for the emulator
+  emulator_section=$(xmllint --xpath "//emulator[@name='$emulator_name']" "$es_find_rules" 2>/dev/null)
+  
+  if [ -z "$emulator_section" ]; then
+      log e "Emulator not found: $emulator_name"
+      return 1
+  fi
+  
+  # Search systempath entries
+  while IFS= read -r line; do
+      command_path=$(echo "$line" | sed -n 's/.*<entry>\(.*\)<\/entry>.*/\1/p')
+      if [ -x "$(command -v $command_path)" ]; then
+          found_path=$command_path
+          break
+      fi
+  done <<< "$(echo "$emulator_section" | xmllint --xpath "//rule[@type='systempath']/entry" - 2>/dev/null)"
+  
+  # If not found, search staticpath entries
+  if [ -z "$found_path" ]; then
+      while IFS= read -r line; do
+          command_path=$(eval echo "$line" | sed -n 's/.*<entry>\(.*\)<\/entry>.*/\1/p')
+          if [ -x "$command_path" ]; then
+              found_path=$command_path
+              break
+          fi
+      done <<< "$(echo "$emulator_section" | xmllint --xpath "//rule[@type='staticpath']/entry" - 2>/dev/null)"
+  fi
+  
+  if [ -z "$found_path" ]; then
+      log e "No valid path found for emulator: $emulator_name"
+      return 1
+  else
+      log d "Found emulator: \"$found_path\""
+      echo "$found_path"
+      return 0
+  fi
+}
+
+# Function to find the emulator name from the label in es_systems.xml
+find_emulator_name_from_label() {
+    local label="$1"
+    
+    # Search for the emulator matching the label in the es_systems.xml file
+    extracted_emulator_name=$(xmllint --recover --xpath "string(//system[name='$system']/command[@label='$label']/text())" "$es_systems" 2>/dev/null | sed 's/%//g' | sed 's/EMULATOR_//g' | cut -d' ' -f1)
+
+    if [[ -n "$extracted_emulator_name" ]]; then
+        log d "Found emulator by label: $extracted_emulator_name"
+        echo "$extracted_emulator_name"
+    else
+        log e "Emulator name not found for label: $label"
+        return 1
+    fi
+}

From ab0f3e033a2347eb7811cfcdbaaaada5bed84948 Mon Sep 17 00:00:00 2001
From: XargonWan <XargonWan@gmail.com>
Date: Thu, 12 Sep 2024 10:38:00 +0900
Subject: [PATCH 3/5] RUN_GAME: altemulator working

---
 functions/global.sh   |   3 +-
 functions/run_game.sh | 272 +++++++++++++++++++++---------------------
 2 files changed, 137 insertions(+), 138 deletions(-)

diff --git a/functions/global.sh b/functions/global.sh
index 5792a51a..0488a22b 100644
--- a/functions/global.sh
+++ b/functions/global.sh
@@ -54,7 +54,7 @@ cooker_repository_name="Cooker"
 main_repository_name="RetroDECK"                                                                         # The name of the main repository under RetroDECK organization
 features="$config/retrodeck/reference_lists/features.json"                                               # A file where all the RetroDECK and component capabilities are kept for querying
 es_systems="/app/share/es-de/resources/systems/linux/es_systems.xml"                                     # ES-DE supported system list   
-es_find_rules="/app/share/es-de/resources/systems/linux/es_find_rules.xml"                               # ES-DE emulator find rules
+es_find_rules="/app/share/es-de/resources/systems/linux/es_find_rules.xml"                               # ES-DE emulator find rules                                                  
 
 
 # Godot data transfer temp files
@@ -84,6 +84,7 @@ es_source_logs="/var/config/ES-DE/logs"
 raconf="/var/config/retroarch/retroarch.cfg"
 ra_core_conf="/var/config/retroarch/retroarch-core-options.cfg"
 ra_scummvm_conf="/var/config/retroarch/system/scummvm.ini"
+ra_cores_path="/var/config/retroarch/cores"  
 
 # CEMU config files
 
diff --git a/functions/run_game.sh b/functions/run_game.sh
index c6cee211..8cba7459 100755
--- a/functions/run_game.sh
+++ b/functions/run_game.sh
@@ -1,111 +1,106 @@
 #!/bin/bash
 
 run_game() {
-  # Initialize variables
-  emulator=""
-  system=""
-  manual_mode=false
+    # Initialize variables
+    emulator=""
+    system=""
+    manual_mode=false
 
-  # Parse options
-  while getopts ":e:s:m" opt; do  # Use `m` for manual mode flag
-      case ${opt} in
-          e )
-              emulator=$OPTARG
-              ;;
-          s )
-              system=$OPTARG
-              ;;
-          m )
-              manual_mode=true
-              log i "Run game: manual mode enabled"
-              ;;
-          \? )
-              echo "Usage: $0 --run [-e emulator] [-s system] [-m manual] game"
-              exit 1
-              ;;
-      esac
-  done
-  shift $((OPTIND -1))
+    # 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: $0 --run [-e emulator] [-s system] [-m manual] game"
+                exit 1
+                ;;
+        esac
+    done
+    shift $((OPTIND - 1))
 
-  # Check for game argument
-  if [[ -z "$1" ]]; then
-      log e "Game path is required."
-      log i "Usage: $0 start [-e emulator] [-s system] [-m manual] game"
-      exit 1
-  fi
+    # Check for game argument
+    if [[ -z "$1" ]]; then
+        log e "Game path is required."
+        log i "Usage: $0 start [-e emulator] [-s system] [-m manual] game"
+        exit 1
+    fi
 
-  game=$1
+    game=$1
+    game_basename="./$(basename "$game")"
 
-  # If no system is provided, extract it from the game path
-  if [[ -z "$system" ]]; then
-    system=$(echo "$game" | grep -oP '(?<=roms/)[^/]+')
-  fi
-
-  log d "Game: \"$game\""
-  log d "System: \"$system\""
-  
-  # Try finding the <altemulator> inside the specific game block
-  altemulator=$(xmllint --recover --xpath "string(//game[path='$game']/altemulator)" "$rdhome/ES-DE/gamelists/$system/gamelist.xml" 2>/dev/null)
-
-  if [[ -n "$altemulator" ]]; then
-      log d "Alternate emulator found in <altemulator>: $altemulator"
-      emulator_name=$(echo "$altemulator" | sed -e 's/ (Standalone)//')  # Strip " (Standalone)" from name
-      emulator=$(find_emulator "$emulator_name")
-
-      if [[ -n "$emulator" ]]; then
-          log d "Using alternate emulator: $emulator"
-      else
-          log e "No valid path found for emulator: $altemulator"
-          exit 1
-      fi
-  else
-    # Try to fetch <alternativeEmulator> from anywhere in the document
-    alternative_emulator=$(xmllint --recover --xpath 'string(//alternativeEmulator/label)' "$rdhome/ES-DE/gamelists/$system/gamelist.xml" 2>/dev/null)
-
-    if [[ -n "$alternative_emulator" ]]; then
-        log d "Alternate emulator found in <alternativeEmulator> header: $alternative_emulator"
-        
-        # Find the emulator name from the label in es_systems.xml
-        emulator_name=$(find_emulator_name_from_label "$alternative_emulator")
-        
-        if [[ -n "$emulator_name" ]]; then
-            # Pass the extracted emulator name to find_emulator function
-            emulator=$(find_emulator "$emulator_name")
+    # 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 e "Failed to detect system from game path."
+            exit 1
         fi
-        
-        if [[ -n "$emulator" ]]; then
-            log d "Using alternate emulator from <alternativeEmulator>: $emulator"
-        else
-            log e "No valid path found for emulator: $alternative_emulator"
+    fi
+    log d "System recognized: $system"
+
+    # 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=$(show_zenity_emulator_list "$system")
+        if [[ -z "$emulator" ]]; then
+            log e "No emulator selected in manual mode."
             exit 1
         fi
     else
-        log i "No alternate emulator found in game block or header, proceeding to auto mode."
+        log d "Automatically searching for an emulator for system: $system"
+
+        # Check for <altemulator> in the game block in gamelist.xml
+        altemulator=$(xmllint --recover --xpath "string(//game[path='$game_basename']/altemulator)" "$rdhome/ES-DE/gamelists/$system/gamelist.xml" 2>/dev/null)
+
+        if [[ -n "$altemulator" ]]; then
+            log d "Found <altemulator> for game: $altemulator"
+            emulator=$(xmllint --recover --xpath "string(//command[@label=\"$altemulator\"])" "$es_systems" 2>/dev/null)
+        fi
+
+        # Fallback to first available emulator in es_systems.xml if no <altemulator> found
+        if [[ -z "$emulator" ]]; then
+            log d "No alternate emulator found, using first available emulator in es_systems.xml"
+            emulator_command=$(xmllint --recover --xpath "string(//system[name='$system']/command[1])" "$es_systems" 2>/dev/null)
+            emulator=$(find_emulator_name_from_label "$emulator_command")
+        fi
+
+        if [[ -z "$emulator" ]]; then
+            log e "No valid emulator found for system: $system"
+            exit 1
+        fi
     fi
-  fi
 
-  # If an emulator is found, substitute placeholders in the command before running
-  if [[ -n "$emulator" ]]; then
-    # Ensure command substitution
-    find_system_commands "$emulator"
-    # TODO: almost there, we need just to start the emulator without Zenity: maybe we have to edit THAT function to pass the emulator to run
-    log d "Final command: $command"
-    eval "$command"
-  else
-    log e "No emulator found or selected. Exiting."
-    return 1
-  fi
-  
-  # If the emulator is not specified or manual mode is set, ask the user to select it via Zenity
-  if [[ -z "$emulator" && "$manual_mode" == true ]]; then
-    emulator=$(find_system_commands)
-  fi
+    # Step 3: Construct and Run the Command
+    log d "Preparing to launch with emulator: $emulator"
 
-  # If emulator is still not set, fall back to the first available emulator
-  if [[ -z "$emulator" ]]; then
-    emulator=$(get_first_emulator)
-  fi
+    # Now pass the final constructed command to substitute_placeholders function
+    final_command=$(substitute_placeholders "$emulator")
 
+    # Log and execute the command
+    log i "Launching game with command: $final_command"
+    eval "$final_command"
+}
+
+
+# Assume this function handles showing the Zenity list of emulators for manual mode
+show_zenity_emulator_list() {
+    local system="$1"
+    # Example logic to retrieve and show Zenity list of emulators for the system
+    # This would extract available emulators for the system from es_systems.xml and show a Zenity dialog
+    emulators=$(xmllint --xpath "//system[name='$system']/command/@label" "$es_systems" | sed 's/ label=/\n/g' | sed 's/\"//g' | grep -o '[^ ]*')
+    zenity --list --title="Select Emulator" --column="Emulators" $emulators
 }
 
 # Function to extract commands from es_systems.xml and present them in Zenity
@@ -153,6 +148,7 @@ find_system_commands() {
 # Function to substitute placeholders in the command
 substitute_placeholders() {
     local cmd="$1"
+    log d "Substitute placeholder: working on $cmd"
     local rom_path="$game"
     local rom_dir=$(dirname "$rom_path")
     
@@ -183,9 +179,9 @@ substitute_placeholders() {
     cmd="${cmd//"%ROM%"/"'$rom_path'"}"
     cmd="${cmd//"%GAMEDIR%"/"'$rom_dir'"}"
     cmd="${cmd//"%GAMEDIRRAW%"/"'$rom_dir_raw'"}"
-    cmd="${cmd//"%CORE_RETROARCH%"/"/var/config/retroarch/cores"}"
+    cmd="${cmd//"%CORE_RETROARCH%"/"$ra_cores_path"}"
 
-    log d "Command after %BASENAME% and other substitutions: $cmd"
+    log d "Command after placeholders substitutions: $cmd"
 
     # Now handle %INJECT% after %BASENAME% has been substituted
     cmd=$(handle_inject_placeholder "$cmd")
@@ -276,45 +272,45 @@ get_first_emulator() {
 }
 
 find_emulator() {
-  local emulator_name=$1
-  local found_path=""
+    local emulator_name="$1"
+    found_path=""
 
-  # Search the es_find_rules.xml file for the emulator
-  emulator_section=$(xmllint --xpath "//emulator[@name='$emulator_name']" "$es_find_rules" 2>/dev/null)
-  
-  if [ -z "$emulator_section" ]; then
-      log e "Emulator not found: $emulator_name"
-      return 1
-  fi
-  
-  # Search systempath entries
-  while IFS= read -r line; do
-      command_path=$(echo "$line" | sed -n 's/.*<entry>\(.*\)<\/entry>.*/\1/p')
-      if [ -x "$(command -v $command_path)" ]; then
-          found_path=$command_path
-          break
-      fi
-  done <<< "$(echo "$emulator_section" | xmllint --xpath "//rule[@type='systempath']/entry" - 2>/dev/null)"
-  
-  # If not found, search staticpath entries
-  if [ -z "$found_path" ]; then
-      while IFS= read -r line; do
-          command_path=$(eval echo "$line" | sed -n 's/.*<entry>\(.*\)<\/entry>.*/\1/p')
-          if [ -x "$command_path" ]; then
-              found_path=$command_path
-              break
-          fi
-      done <<< "$(echo "$emulator_section" | xmllint --xpath "//rule[@type='staticpath']/entry" - 2>/dev/null)"
-  fi
-  
-  if [ -z "$found_path" ]; then
-      log e "No valid path found for emulator: $emulator_name"
-      return 1
-  else
-      log d "Found emulator: \"$found_path\""
-      echo "$found_path"
-      return 0
-  fi
+    # Search the es_find_rules.xml file for the emulator
+    emulator_section=$(xmllint --xpath "//emulator[@name='$emulator_name']" "$es_find_rules" 2>/dev/null)
+
+    if [ -z "$emulator_section" ]; then
+        log e "Find emulator: emulator not found: $emulator_name"
+        return 1
+    fi
+
+    # Search systempath entries
+    while IFS= read -r line; do
+        command_path=$(echo "$line" | sed -n 's/.*<entry>\(.*\)<\/entry>.*/\1/p')
+        if [ -x "$(command -v $command_path)" ]; then
+            found_path=$command_path
+            break
+        fi
+    done <<< "$(echo "$emulator_section" | xmllint --xpath "//rule[@type='systempath']/entry" - 2>/dev/null)"
+
+    # If not found, search staticpath entries
+    if [ -z "$found_path" ]; then
+        while IFS= read -r line; do
+            command_path=$(eval echo "$line" | sed -n 's/.*<entry>\(.*\)<\/entry>.*/\1/p')
+            if [ -x "$command_path" ]; then
+                found_path=$command_path
+                break
+            fi
+        done <<< "$(echo "$emulator_section" | xmllint --xpath "//rule[@type='staticpath']/entry" - 2>/dev/null)"
+    fi
+
+    if [ -z "$found_path" ]; then
+        log e "Find emulator: no valid path found for emulator: $emulator_name"
+        return 1
+    else
+        log d "Find emulator: found emulator \"$found_path\""
+        echo "$found_path"
+        return 0
+    fi
 }
 
 # Function to find the emulator name from the label in es_systems.xml
@@ -323,12 +319,14 @@ find_emulator_name_from_label() {
     
     # Search for the emulator matching the label in the es_systems.xml file
     extracted_emulator_name=$(xmllint --recover --xpath "string(//system[name='$system']/command[@label='$label']/text())" "$es_systems" 2>/dev/null | sed 's/%//g' | sed 's/EMULATOR_//g' | cut -d' ' -f1)
+    log d "Found emulator from label: $extracted_emulator_name"
 
-    if [[ -n "$extracted_emulator_name" ]]; then
-        log d "Found emulator by label: $extracted_emulator_name"
-        echo "$extracted_emulator_name"
+    emulator_command=$(find_emulator "$extracted_emulator_name")
+
+    if [[ -n "$emulator_command" ]]; then
+        echo "$emulator_command"
     else
-        log e "Emulator name not found for label: $label"
+        log e "Found emulator from label: emulator name not found for label: $label"
         return 1
     fi
 }

From 3055ee5d94db22c1305bd6a4fb6adf4321f433a2 Mon Sep 17 00:00:00 2001
From: XargonWan <XargonWan@gmail.com>
Date: Thu, 12 Sep 2024 10:52:34 +0900
Subject: [PATCH 4/5] RUN_GAME: alternativeEmulator added but not working

---
 functions/run_game.sh | 16 ++++++++++++++++
 1 file changed, 16 insertions(+)

diff --git a/functions/run_game.sh b/functions/run_game.sh
index 8cba7459..2dbee57c 100755
--- a/functions/run_game.sh
+++ b/functions/run_game.sh
@@ -67,10 +67,26 @@ run_game() {
         if [[ -n "$altemulator" ]]; then
             log d "Found <altemulator> for game: $altemulator"
             emulator=$(xmllint --recover --xpath "string(//command[@label=\"$altemulator\"])" "$es_systems" 2>/dev/null)
+        else
+
+            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=$(xmllint --recover --xpath 'string(//alternativeEmulator/label)' "$rdhome/ES-DE/gamelists/$system/gamelist.xml" 2>/dev/null)
+
+            log d "Alternate emulator found in <alternativeEmulator> header: $alternative_emulator"
+
+            # TODO: fix this function
+            # [2024-09-12 10:51:11.232] [DEBUG] Alternate emulator found in <alternativeEmulator> header: PicoDrive
+            # then is broken
+            
+            # Find the emulator name from the label in es_systems.xml
+            emulator_name=$(find_emulator_name_from_label "$alternative_emulator")
+            emulator=$(find_emulator "$emulator_name")
         fi
 
         # Fallback to first available emulator in es_systems.xml if no <altemulator> found
         if [[ -z "$emulator" ]]; then
+            # TODO: the non-alt emulator is broken
             log d "No alternate emulator found, using first available emulator in es_systems.xml"
             emulator_command=$(xmllint --recover --xpath "string(//system[name='$system']/command[1])" "$es_systems" 2>/dev/null)
             emulator=$(find_emulator_name_from_label "$emulator_command")

From a1e55bb06b33133bf79638f4298e4b2e8f667341 Mon Sep 17 00:00:00 2001
From: XargonWan <XargonWan@gmail.com>
Date: Thu, 12 Sep 2024 11:30:39 +0900
Subject: [PATCH 5/5] RUN_GAME: fixed alternativeEmulator

---
 functions/run_game.sh | 14 ++++----------
 1 file changed, 4 insertions(+), 10 deletions(-)

diff --git a/functions/run_game.sh b/functions/run_game.sh
index 2dbee57c..4f3afcbd 100755
--- a/functions/run_game.sh
+++ b/functions/run_game.sh
@@ -65,23 +65,17 @@ run_game() {
         altemulator=$(xmllint --recover --xpath "string(//game[path='$game_basename']/altemulator)" "$rdhome/ES-DE/gamelists/$system/gamelist.xml" 2>/dev/null)
 
         if [[ -n "$altemulator" ]]; then
+
             log d "Found <altemulator> for game: $altemulator"
             emulator=$(xmllint --recover --xpath "string(//command[@label=\"$altemulator\"])" "$es_systems" 2>/dev/null)
-        else
+
+        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=$(xmllint --recover --xpath 'string(//alternativeEmulator/label)' "$rdhome/ES-DE/gamelists/$system/gamelist.xml" 2>/dev/null)
-
             log d "Alternate emulator found in <alternativeEmulator> header: $alternative_emulator"
+            emulator=$(xmllint --recover --xpath "string(//system[platform='$system']/command[@label=\"$alternative_emulator\"])" "$es_systems" 2>/dev/null)
 
-            # TODO: fix this function
-            # [2024-09-12 10:51:11.232] [DEBUG] Alternate emulator found in <alternativeEmulator> header: PicoDrive
-            # then is broken
-            
-            # Find the emulator name from the label in es_systems.xml
-            emulator_name=$(find_emulator_name_from_label "$alternative_emulator")
-            emulator=$(find_emulator "$emulator_name")
         fi
 
         # Fallback to first available emulator in es_systems.xml if no <altemulator> found