RUN_GAME: working but looping

This commit is contained in:
XargonWan 2024-09-10 22:13:26 +09:00
parent c4a80119af
commit 38806dabc1

View file

@ -936,6 +936,7 @@ find_emulator() {
# TODO: add the logic of alt emulator and default emulator
# TODO: if the emulator is only one just skip the zenity and run it
run_game() {
@ -984,71 +985,91 @@ run_game() {
handle_inject_placeholder() {
local cmd="$1"
# Check if .esprefix file exists
local inject_file="${game}.esprefix"
# Find all occurrences of %INJECT%=something
while [[ "$cmd" =~ %INJECT%=(.*) ]]; do
inject_file="${BASH_REMATCH[1]}" # Extract the file name
log d "Checking if \"${game}.esprefix\" exists"
# Prepend the directory path to ensure we have the full path to the file
inject_file_full_path="$rom_dir/$inject_file"
if [[ ! -f "$inject_file" ]]; then
# If the inject file does not exist, strip all forms of %INJECT% from the command
log d "Found %INJECT% pointing to file \"$inject_file_full_path\""
log d "\"${game}.esprefix\" not existing"
# 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\""
# This handles cases where %INJECT% might be in different forms:
# Strip %INJECT%="value", %INJECT%="", or just %INJECT%=
cmd="${cmd//%INJECT%=\"*\"/}"
cmd="${cmd//%INJECT%=/}"
cmd="${cmd//%INJECT%/}"
cmd="${cmd//%BASENAME%.esprefix=\"*\"/}"
cmd="${cmd//%BASENAME%.esprefix=/}"
cmd="${cmd//%BASENAME%.esprefix/}"
log d "Returning the following command:$cmd"
echo "$cmd"
return
# Replace the %INJECT% placeholder with the content of the file
cmd="${cmd//%INJECT%=$inject_file/$inject_content}"
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/}"
fi
done
# If the .esprefix file exists, process its content
inject_content=$(head -c 4096 "$inject_file")
log i ".esprefix file found, injecting \"$inject_content\""
cmd="${cmd//%INJECT%=\"$inject_file\"/$inject_content}"
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")
local base_name=$(basename "$rom_path" .psvita) # Ensure the .psvita extension is stripped
# 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=""
# Handle %INJECT% placeholder if present
cmd=$(handle_inject_placeholder "$cmd")
# 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 emulator path
cmd=$(echo "$cmd" | sed -r 's/%EMULATOR_[A-Z0-9_]+%/$(replace_emulator_placeholder "&")/g')
# 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 that paths with spaces are properly quoted and substitute other placeholders
# Ensure paths are quoted correctly
cmd="${cmd//"%ROM%"/"\"$rom_path\""}"
cmd="${cmd//"%BASENAME%"/"\"$base_name\""}" # Proper base name without extra .psvita
cmd="${cmd//"%FILENAME%"/"\"$file_name\""}"
cmd="${cmd//"%ROMRAW%"/"\"$rom_raw\""}"
cmd="${cmd//"%ROMPATH%"/"\"$rom_dir\""}"
cmd="${cmd//"%ESPATH%"/"\"$es_path\""}"
cmd="${cmd//"%EMUDIR%"/"\"$emulator_path\""}"
cmd="${cmd//"%GAMEDIR%"/"\"$rom_dir\""}"
cmd="${cmd//"%GAMEDIRRAW%"/"\"$rom_dir_raw\""}"
cmd="${cmd//"%CORE_RETROARCH%"/"/var/config/retroarch/cores"}"
log d "Final command: $cmd" # Log the final command for debugging
log d "Command after %BASENAME% and other substitutions: $cmd"
# Now handle %INJECT% after %BASENAME% has been substituted
cmd=$(handle_inject_placeholder "$cmd")
echo "$cmd"
}