RUN_GAME: code prettyfication

This commit is contained in:
XargonWan 2024-09-11 13:55:04 +09:00
parent 24b35b7175
commit 51485497ae

View file

@ -895,9 +895,10 @@ start_retrodeck() {
find_emulator() { find_emulator() {
local emulator_name=$1 local emulator_name=$1
local found_path="" 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 # Search the es_find_rules.xml file for the emulator
emulator_section=$(xmllint --xpath "//emulator[@name='$emulator_name']" "/app/share/es-de/resources/systems/linux/es_find_rules.xml" 2>/dev/null) emulator_section=$(xmllint --xpath "//emulator[@name='$emulator_name']" "$es_find_rules" 2>/dev/null)
if [ -z "$emulator_section" ]; then if [ -z "$emulator_section" ]; then
log e "Emulator not found: $emulator_name" log e "Emulator not found: $emulator_name"
@ -938,13 +939,15 @@ find_emulator() {
# TODO: add the logic of alt emulator and default emulator # TODO: add the logic of alt emulator and default emulator
run_game() { 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 # Initialize variables
emulator="" emulator=""
system="" system=""
es_systems="/app/share/es-de/resources/systems/linux/es_systems.xml"
# Path to the es_systems.xml file
xml_file="/app/share/es-de/resources/systems/linux/es_systems.xml"
# Parse options # Parse options
while getopts ":e:s:" opt; do while getopts ":e:s:" opt; do
@ -965,8 +968,8 @@ run_game() {
# Check for game argument # Check for game argument
if [[ -z "$1" ]]; then if [[ -z "$1" ]]; then
echo "Error: Game file is required." log e "Game path is required."
echo "Usage: $0 --run [-e emulator] [-s system] game" log i "Usage: $0 start [-e emulator] [-s system] game"
exit 1 exit 1
fi fi
@ -980,64 +983,66 @@ run_game() {
log d "Game: \"$game\"" log d "Game: \"$game\""
log d "System: \"$system\"" log d "System: \"$system\""
# Function to handle the %INJECT% placeholder # Function to handle the %INJECT% placeholder
handle_inject_placeholder() { # When %INJECT%=filename is found in the game command it will check for the existence of the file
local cmd="$1" # If the file is found the "%INJECT%=file" will be replaced with the contents of the found file
local rom_dir=$(dirname "$game") # Get the ROM directory based on the game path 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 # Find and process all occurrences of %INJECT%='something'.extension
while [[ "$cmd" =~ (%INJECT%=\'([^\']+)\')(.[^ ]+)? ]]; do while [[ "$cmd" =~ (%INJECT%=\'([^\']+)\')(.[^ ]+)? ]]; do
inject_file="${BASH_REMATCH[2]}" # Extract the quoted file name inject_file="${BASH_REMATCH[2]}" # Extract the quoted file name
extension="${BASH_REMATCH[3]}" # Extract the extension (if any) extension="${BASH_REMATCH[3]}" # Extract the extension (if any)
inject_file_full_path="$rom_dir/$inject_file$extension" # Form the full path inject_file_full_path="$rom_dir/$inject_file$extension" # Form the full path
log d "Found inject part: %INJECT%='$inject_file'$extension" log d "Found inject part: %INJECT%='$inject_file'$extension"
# Check if the file exists # Check if the file exists
if [[ -f "$inject_file_full_path" ]]; then if [[ -f "$inject_file_full_path" ]]; then
# Read the content of the file and replace newlines with spaces # Read the content of the file and replace newlines with spaces
inject_content=$(cat "$inject_file_full_path" | tr '\n' ' ') inject_content=$(cat "$inject_file_full_path" | tr '\n' ' ')
log i "File \"$inject_file_full_path\" found. Replacing %INJECT% with content." log i "File \"$inject_file_full_path\" found. Replacing %INJECT% with content."
# Escape special characters in the inject part for the replacement # Escape special characters in the inject part for the replacement
escaped_inject_part=$(printf '%s' "%INJECT%='$inject_file'$extension" | sed 's/[]\/$*.^[]/\\&/g') escaped_inject_part=$(printf '%s' "%INJECT%='$inject_file'$extension" | sed 's/[]\/$*.^[]/\\&/g')
# Replace the entire %INJECT%=...'something'.extension part with the file content # Replace the entire %INJECT%=...'something'.extension part with the file content
cmd=$(echo "$cmd" | sed "s|$escaped_inject_part|$inject_content|g") cmd=$(echo "$cmd" | sed "s|$escaped_inject_part|$inject_content|g")
log d "Replaced cmd: $cmd" log d "Replaced cmd: $cmd"
else else
log e "File \"$inject_file_full_path\" not found. Removing %INJECT% placeholder." log e "File \"$inject_file_full_path\" not found. Removing %INJECT% placeholder."
# Use sed to remove the entire %INJECT%=...'something'.extension # Use sed to remove the entire %INJECT%=...'something'.extension
escaped_inject_part=$(printf '%s' "%INJECT%='$inject_file'$extension" | sed 's/[]\/$*.^[]/\\&/g') escaped_inject_part=$(printf '%s' "%INJECT%='$inject_file'$extension" | sed 's/[]\/$*.^[]/\\&/g')
cmd=$(echo "$cmd" | sed "s|$escaped_inject_part||g") cmd=$(echo "$cmd" | sed "s|$escaped_inject_part||g")
log d "sedded cmd: $cmd" log d "sedded cmd: $cmd"
fi fi
done done
log d "Returning the command with injected content: $cmd" log d "Returning the command with injected content: $cmd"
echo "$cmd" echo "$cmd"
} }
# Function to replace %EMULATOR_SOMETHING% with the actual path of the emulator # Function to replace %EMULATOR_SOMETHING% with the actual path of the emulator
replace_emulator_placeholder() { replace_emulator_placeholder() {
local placeholder=$1 local placeholder=$1
# Extract emulator name from placeholder without changing case # Extract emulator name from placeholder without changing case
local emulator_name="${placeholder//"%EMULATOR_"/}" # Extract emulator name after %EMULATOR_ local emulator_name="${placeholder//"%EMULATOR_"/}" # Extract emulator name after %EMULATOR_
emulator_name="${emulator_name//"%"/}" # Remove the trailing % emulator_name="${emulator_name//"%"/}" # Remove the trailing %
# Use the find_emulator function to get the emulator path using the correct casing # Use the find_emulator function to get the emulator path using the correct casing
local emulator_exec=$(find_emulator "$emulator_name") local emulator_exec=$(find_emulator "$emulator_name")
if [[ -z "$emulator_exec" ]]; then if [[ -z "$emulator_exec" ]]; then
log e "Emulator '$emulator_name' not found." log e "Emulator '$emulator_name' not found."
exit 1 exit 1
fi fi
echo "$emulator_exec" echo "$emulator_exec"
} }
# Function to substitute the placeholders # Function to substitute the placeholders
substitute_placeholders() { substitute_placeholders() {
@ -1086,7 +1091,7 @@ replace_emulator_placeholder() {
find_system_commands() { find_system_commands() {
local system_name=$system local system_name=$system
# Use xmllint to extract the system commands from the XML # Use xmllint to extract the system commands from the XML
system_section=$(xmllint --xpath "//system[name='$system_name']" "$xml_file" 2>/dev/null) system_section=$(xmllint --xpath "//system[name='$system_name']" "$es_systems" 2>/dev/null)
if [ -z "$system_section" ]; then if [ -z "$system_section" ]; then
log e "System not found: $system_name" log e "System not found: $system_name"