RUN_GAME: fixed %INJECT% placeholder in both of the cases

This commit is contained in:
XargonWan 2024-09-11 10:18:03 +09:00
parent d0644c82e6
commit 6496e1f051

View file

@ -984,29 +984,37 @@ run_game() {
# Function to handle the %INJECT% placeholder # Function to handle the %INJECT% placeholder
handle_inject_placeholder() { handle_inject_placeholder() {
local cmd="$1" 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 # Find and process all occurrences of %INJECT%='something'.extension
while [[ "$cmd" =~ %INJECT%=(.*) ]]; do while [[ "$cmd" =~ (%INJECT%=\'([^\']+)\')(.[^ ]+)? ]]; do
inject_file="${BASH_REMATCH[1]}" # Extract the file name 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 log d "Found inject part: %INJECT%='$inject_file'$extension"
inject_file_full_path="$rom_dir/$inject_file"
log d "Found %INJECT% pointing to file \"$inject_file_full_path\"" # Check if the file exists
# Check if the file exists (no escaping needed, just quotes)
if [[ -f "$inject_file_full_path" ]]; then if [[ -f "$inject_file_full_path" ]]; then
# Read the content of the file # Read the content of the file and replace newlines with spaces
inject_content=$(cat "$inject_file_full_path") inject_content=$(cat "$inject_file_full_path" | tr '\n' ' ')
log i "File \"$inject_file_full_path\" found, injecting content \"$inject_content\"" log i "File \"$inject_file_full_path\" found. Replacing %INJECT% with content."
# Replace the %INJECT% placeholder with the content of the file # Escape special characters in the inject part for the replacement
cmd="${cmd//%INJECT%=$inject_file/$inject_content}" 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 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."
# 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 fi
done done
@ -1015,9 +1023,6 @@ handle_inject_placeholder() {
} }
# 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
@ -1059,15 +1064,15 @@ replace_emulator_placeholder() {
done done
# Substitute %BASENAME% and other placeholders # Substitute %BASENAME% and other placeholders
cmd="${cmd//"%BASENAME%"/"$base_name"}" cmd="${cmd//"%BASENAME%"/"'$base_name'"}"
cmd="${cmd//"%FILENAME%"/"$file_name"}" cmd="${cmd//"%FILENAME%"/"'$file_name'"}"
cmd="${cmd//"%ROMRAW%"/"$rom_raw"}" cmd="${cmd//"%ROMRAW%"/"'$rom_raw'"}"
cmd="${cmd//"%ROMPATH%"/"$rom_dir"}" cmd="${cmd//"%ROMPATH%"/"'$rom_dir'"}"
# Ensure paths are quoted correctly # Ensure paths are quoted correctly
cmd="${cmd//"%ROM%"/"\"$rom_path\""}" cmd="${cmd//"%ROM%"/"'$rom_path'"}"
cmd="${cmd//"%GAMEDIR%"/"\"$rom_dir\""}" cmd="${cmd//"%GAMEDIR%"/"'$rom_dir'"}"
cmd="${cmd//"%GAMEDIRRAW%"/"\"$rom_dir_raw\""}" cmd="${cmd//"%GAMEDIRRAW%"/"'$rom_dir_raw'"}"
cmd="${cmd//"%CORE_RETROARCH%"/"/var/config/retroarch/cores"}" cmd="${cmd//"%CORE_RETROARCH%"/"/var/config/retroarch/cores"}"
log d "Command after %BASENAME% and other substitutions: $cmd" log d "Command after %BASENAME% and other substitutions: $cmd"