diff --git a/functions/other_functions.sh b/functions/other_functions.sh index 9826868a..42fe9701 100644 --- a/functions/other_functions.sh +++ b/functions/other_functions.sh @@ -984,29 +984,37 @@ run_game() { # Function to handle the %INJECT% placeholder handle_inject_placeholder() { local cmd="$1" - local rom_dir=$(dirname "$game") # Define rom_dir based on the game path + local rom_dir=$(dirname "$game") # Get the ROM directory based on the game path - # Find all occurrences of %INJECT%=something - while [[ "$cmd" =~ %INJECT%=(.*) ]]; do - inject_file="${BASH_REMATCH[1]}" # Extract the file name + # 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 - # Prepend the directory path to ensure we have the full path to the file - inject_file_full_path="$rom_dir/$inject_file" + log d "Found inject part: %INJECT%='$inject_file'$extension" - log d "Found %INJECT% pointing to file \"$inject_file_full_path\"" - - # Check if the file exists (no escaping needed, just quotes) + # Check if the file exists if [[ -f "$inject_file_full_path" ]]; then - # Read the content of the file - inject_content=$(cat "$inject_file_full_path") - log i "File \"$inject_file_full_path\" found, injecting content \"$inject_content\"" - - # Replace the %INJECT% placeholder with the content of the file - cmd="${cmd//%INJECT%=$inject_file/$inject_content}" + # 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." - # If the file does not exist, just remove the placeholder - cmd="${cmd//%INJECT%=$inject_file/}" + + # 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 @@ -1015,9 +1023,6 @@ handle_inject_placeholder() { } - - - # Function to replace %EMULATOR_SOMETHING% with the actual path of the emulator replace_emulator_placeholder() { local placeholder=$1 @@ -1059,15 +1064,15 @@ replace_emulator_placeholder() { 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"}" + 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//"%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"