Fix broken presets.sh (wouldn't disable presets), upgrade for "Enable All" and "Disable All" buttons in change_preset_dialog and add exceptions for standalone PPSSPP cheevos token file

This commit is contained in:
icenine451 2025-04-01 13:54:05 -04:00
parent 197c0f1468
commit 52ce4249a8

View file

@ -13,10 +13,15 @@ change_preset_dialog() {
current_preset_settings=() current_preset_settings=()
local section_results local section_results
section_results=$(sed -n '/\['"$preset"'\]/, /\[/{ /\['"$preset"'\]/! { /\[/! p } }' "$rd_conf" | sed '/^$/d') section_results=$(sed -n '/\['"$preset"'\]/, /\[/{ /\['"$preset"'\]/! { /\[/! p } }' "$rd_conf" | sed '/^$/d')
all_emulators_in_preset=""
while IFS= read -r config_line; do while IFS= read -r config_line; do
system_name=$(get_setting_name "$config_line" "retrodeck") system_name=$(get_setting_name "$config_line" "retrodeck")
system_value=$(get_setting_value "$rd_conf" "$system_name" "retrodeck" "$preset") system_value=$(get_setting_value "$rd_conf" "$system_name" "retrodeck" "$preset")
if [[ -n $all_emulators_in_preset ]]; then
all_emulators_in_preset+=","
fi
all_emulators_in_preset+="$system_name" # Build a list of all emulators in case user selects "Enable All"
# Append three values: the current enabled state, a pretty name, and the internal system name. # Append three values: the current enabled state, a pretty name, and the internal system name.
current_preset_settings=("${current_preset_settings[@]}" "$system_value" "$(make_name_pretty "$system_name")" "$system_name") current_preset_settings=("${current_preset_settings[@]}" "$system_value" "$(make_name_pretty "$system_name")" "$system_name")
done < <(printf '%s\n' "$section_results") done < <(printf '%s\n' "$section_results")
@ -38,54 +43,37 @@ change_preset_dialog() {
--extra-button "Disable All") --extra-button "Disable All")
local rc=$? local rc=$?
local extra_action=""
log d "User made a choice: $choice with return code: $rc" log d "User made a choice: $choice with return code: $rc"
if [[ "$rc" == 0 || -n "$choice" ]]; then # If the user didn't hit Cancel
choice_made="true"
fi
# Handle extra button responses. # Handle extra button responses.
if [ "$choice" == "Enable All" ]; then if [ "$choice" == "Enable All" ]; then
log d "Enable All selected" log d "Enable All selected"
# Build a comma-separated list of all internal system names. # Assign the comma-separated list of all preset system names as the choice
all_systems="" choice="$all_emulators_in_preset"
for ((i=2; i<${#current_preset_settings[@]}; i+=3)); do
if [ -z "$all_systems" ]; then
all_systems="${current_preset_settings[$i]}"
else
all_systems="$all_systems,${current_preset_settings[$i]}"
fi
done
choice="$all_systems"
extra_action="extra"
force_state="true"
elif [ "$choice" == "Disable All" ]; then elif [ "$choice" == "Disable All" ]; then
log d "Disable All selected" log d "Disable All selected"
# Build a comma-separated list of all internal system names. # Assign empty string as choice, as all systems will be disabled
all_systems="" choice=""
for ((i=2; i<${#current_preset_settings[@]}; i+=3)); do
if [ -z "$all_systems" ]; then
all_systems="${current_preset_settings[$i]}"
else
all_systems="$all_systems,${current_preset_settings[$i]}"
fi
done
choice="$all_systems"
extra_action="extra"
force_state="false"
fi fi
# Call make_preset_changes if the user made a selection, # Call make_preset_changes if the user made a selection,
# or if an extra button was clicked (even if the resulting choice is empty). # or if an extra button was clicked (even if the resulting choice is empty, meaning all systems are to be disabled).
if [[ "$rc" == 0 || "$extra_action" == "extra" || -n "$choice" ]]; then if [[ "$choice_made" == "true" ]]; then
log d "Calling make_preset_changes with choice: $choice" log d "Calling make_preset_changes with choice: $choice"
( (
make_preset_changes "$preset" "$choice" "$force_state" make_preset_changes "$preset" "$choice"
) | rd_zenity --icon-name=net.retrodeck.retrodeck --progress --no-cancel --pulsate --auto-close \ ) | rd_zenity --icon-name=net.retrodeck.retrodeck --progress --no-cancel --pulsate --auto-close \
--window-icon="/app/share/icons/hicolor/scalable/apps/net.retrodeck.retrodeck.svg" \ --window-icon="/app/share/icons/hicolor/scalable/apps/net.retrodeck.retrodeck.svg" \
--title "RetroDECK Configurator Utility - Presets Configuration" \ --title "RetroDECK Configurator Utility - Presets Configuration" \
--text="Setting up your presets, please wait..." --text="Setting up your presets, please wait..."
else else
log i "No preset choices made" log i "No preset choices made"
fi fi
} }
@ -96,7 +84,7 @@ build_preset_list_options() {
# The function also builds several arrays (all_systems, changed_systems, etc.) that are used in the make_preset_changes() function. # The function also builds several arrays (all_systems, changed_systems, etc.) that are used in the make_preset_changes() function.
# This function needs to be called in the same memory space as make_preset_changes() at least once. # This function needs to be called in the same memory space as make_preset_changes() at least once.
# USAGE: build_preset_list_options "$preset" # USAGE: build_preset_list_options "$preset"
# INPUT: # INPUT:
# - $1: The name of the preset. # - $1: The name of the preset.
# OUTPUT: # OUTPUT:
# - $godot_current_preset_settings: A Godot temp file containing the system values, pretty system names, and system names. # - $godot_current_preset_settings: A Godot temp file containing the system values, pretty system names, and system names.
@ -138,25 +126,12 @@ build_preset_list_options() {
done < <(printf '%s\n' "$section_results") done < <(printf '%s\n' "$section_results")
} }
make_preset_changes() { make_preset_changes() {
# This function takes a preset name ($1) and a CSV list ($2) of system names. # This function will take a preset name $preset and a CSV list $choice, which contains the names of systems that have been enabled for this preset and enable them in the backend
# If a third parameter is provided (force_state), it forces the specified state (true/false) # Any systems which are currently enabled and not in the CSV list $choice will instead be disabled in the backend
# for only the systems in the CSV list. Otherwise, it toggles the current state. # USAGE: make_preset_changes $preset $choice
#
# USAGE: make_preset_changes $preset $choice [force_state]
#
# Examples:
# Force "borders" to be true for gba:
# make_preset_changes "borders" "gba" true
# Force "borders" to be true for all supported systems:
# make_preset_changes "borders" "all" true
# Toggle gba in preset "borders", this will disable the enabled and vice versa:
# make_preset_changes "borders" "gba" true
# Toggle all in preset "borders":
# make_preset_changes "borders" "all"
log d "Fetching incompatible presets from JSON file" # Fetch incompatible presets from JSON and create a lookup list
incompatible_presets=$(jq -r ' incompatible_presets=$(jq -r '
.incompatible_presets | to_entries[] | .incompatible_presets | to_entries[] |
[ [
@ -167,85 +142,47 @@ make_preset_changes() {
preset="$1" preset="$1"
choice="$2" choice="$2"
force_state="${3:-}"
if [[ "${force_state,,}" == "on" || "${force_state,,}" == "true" ]]; then
force_state="true"
elif [[ "${force_state,,}" == "off" || "${force_state,,}" == "false" ]]; then
force_state="false"
fi
log d "Building preset list options for preset: $preset"
build_preset_list_options "$preset" build_preset_list_options "$preset"
IFS="," read -ra choices <<< "$choice" IFS="," read -ra choices <<< "$choice"
if [[ " ${choices[*]} " == *" all "* ]]; then for emulator in "${all_systems[@]}"; do
log d "All systems selected for preset: $preset" if [[ " ${choices[*]} " =~ " ${emulator} " && ! " ${current_enabled_systems[*]} " =~ " ${emulator} " ]]; then
choices=("${all_systems[@]}") changed_systems=("${changed_systems[@]}" "$emulator")
fi if [[ ! " ${changed_presets[*]} " =~ " ${preset} " ]]; then
changed_presets=("${changed_presets[@]}" "$preset")
# Use an associative array to store the new state for each emulator.
declare -A emulator_state
# Iterate only over the specified systems.
for emulator in "${choices[@]}"; do
if [[ -n "$force_state" ]]; then
new_state="$force_state"
log i "Forcing $preset to state: $new_state for $emulator"
else
current_state=$(get_setting_value "$rd_conf" "$emulator" "retrodeck" "$preset")
if [[ "$current_state" == "true" ]]; then
new_state="false"
if [[ $emulator == "all" ]]; then
log i "Toggling off $preset for all systems"
else
log i "Toggling off $preset for system: $emulator"
fi fi
else set_setting_value "$rd_conf" "$emulator" "true" "retrodeck" "$preset"
if [[ $emulator == "all" ]]; then # Check for conflicting presets for this system
log i "Toggling on $preset for all systems" while IFS=: read -r preset_being_checked known_incompatible_preset || [[ -n "$preset_being_checked" ]];
else do
new_state="true" if [[ ! $preset_being_checked == "#"* ]] && [[ ! -z "$preset_being_checked" ]]; then
log i "Toggling on $preset for system: $emulator" if [[ "$preset" == "$preset_being_checked" ]]; then
fi if [[ $(get_setting_value "$rd_conf" "$emulator" "retrodeck" "$known_incompatible_preset") == "true" ]]; then
fi changed_presets=("${changed_presets[@]}" "$known_incompatible_preset")
fi set_setting_value "$rd_conf" "$emulator" "false" "retrodeck" "$known_incompatible_preset"
fi
emulator_state["$emulator"]="$new_state" fi
changed_systems=("${changed_systems[@]}" "$emulator")
[[ ! " ${changed_presets[*]} " =~ " ${preset} " ]] && changed_presets=("${changed_presets[@]}" "$preset")
set_setting_value "$rd_conf" "$emulator" "$new_state" "retrodeck" "$preset"
# If enabling the emulator, disable any conflicting presets.
if [[ "$new_state" == "true" ]]; then
while IFS=: read -r preset_being_checked known_incompatible_preset || [[ -n "$preset_being_checked" ]]; do
if [[ ! $preset_being_checked =~ ^# ]] && [[ -n "$preset_being_checked" ]]; then
if [[ "$preset" == "$preset_being_checked" ]] && [[ $(get_setting_value "$rd_conf" "$emulator" "retrodeck" "$known_incompatible_preset") == "true" ]]; then
log d "Disabling conflicting preset: $known_incompatible_preset for emulator: $emulator"
changed_presets=("${changed_presets[@]}" "$known_incompatible_preset")
set_setting_value "$rd_conf" "$emulator" "false" "retrodeck" "$known_incompatible_preset"
fi fi
done < <(echo "$incompatible_presets")
fi
if [[ ! " ${choices[*]} " =~ " ${emulator} " && ! " ${current_disabled_systems[*]} " =~ " ${emulator} " ]]; then
changed_systems=("${changed_systems[@]}" "$emulator")
if [[ ! " ${changed_presets[*]} " =~ " ${preset} " ]]; then
changed_presets=("${changed_presets[@]}" "$preset")
fi fi
done < <(echo "$incompatible_presets") set_setting_value "$rd_conf" "$emulator" "false" "retrodeck" "$preset"
fi fi
done done
for emulator in "${changed_systems[@]}"; do
# Rebuild config for all changed systems.
for emulator in "${changed_systems[@]}"; do
log d "Building preset config for changed emulator: $emulator"
if [[ "${emulator_state[$emulator]}" == "true" ]]; then
# When enabling, force a full config update (detailed settings applied).
build_preset_config "$emulator" "${changed_presets[*]}" true
else
build_preset_config "$emulator" "${changed_presets[*]}" build_preset_config "$emulator" "${changed_presets[*]}"
fi done
done
} }
build_preset_config() { build_preset_config() {
# This function will apply one or more presets for a given system, as listed in retrodeck.cfg # This function will apply one or more presets for a given system, as listed in retrodeck.cfg
# USAGE: build_preset_config "system name" "preset class 1" "preset class 2" "preset class 3" # USAGE: build_preset_config "system name" "preset class 1" "preset class 2" "preset class 3"
local system_being_changed="$1" local system_being_changed="$1"
shift shift
local presets_being_changed="$*" local presets_being_changed="$*"
@ -284,38 +221,52 @@ build_preset_config() {
eval defaults_file=$defaults_file eval defaults_file=$defaults_file
fi fi
local read_defaults_file="$defaults_file" local read_defaults_file="$defaults_file"
log d "Changing setting: $read_setting_name to $new_setting_value in $read_target_file"
if [[ "$read_system_enabled" == "true" ]]; then if [[ "$read_system_enabled" == "true" ]]; then
if [[ "$new_setting_value" = \$* ]]; then if [[ "$new_setting_value" = \$* ]]; then
eval new_setting_value=$new_setting_value eval new_setting_value=$new_setting_value
fi fi
if [[ "$read_config_format" == "retroarch" && ! "$retroarch_all" == "true" ]]; then # If this is a RetroArch core, generate the override file if [[ "$read_config_format" == "retroarch" && ! "$retroarch_all" == "true" ]]; then # Separate process if this is a per-system RetroArch override file
if [[ ! -f "$read_target_file" ]]; then if [[ ! -f "$read_target_file" ]]; then
log d "RetroArch per-system override file $read_target_file not found, creating and adding setting"
create_dir "$(realpath "$(dirname "$read_target_file")")" create_dir "$(realpath "$(dirname "$read_target_file")")"
echo "$read_setting_name = \""$new_setting_value"\"" > "$read_target_file" echo "$read_setting_name = \""$new_setting_value"\"" > "$read_target_file"
else else
if [[ -z $(grep -o -P "^$read_setting_name\b" "$read_target_file") ]]; then if [[ -z $(grep -o -P "^$read_setting_name\b" "$read_target_file") ]]; then
log d "RetroArch per-system override file $read_target_file does not contain setting $read_setting_name, adding and assigning value $new_setting_value"
add_setting "$read_target_file" "$read_setting_name" "$new_setting_value" "$read_config_format" "$section" add_setting "$read_target_file" "$read_setting_name" "$new_setting_value" "$read_config_format" "$section"
else else
log d "Changing setting: $read_setting_name to $new_setting_value in $read_target_file"
set_setting_value "$read_target_file" "$read_setting_name" "$new_setting_value" "$read_config_format" "$section" set_setting_value "$read_target_file" "$read_setting_name" "$new_setting_value" "$read_config_format" "$section"
fi fi
fi fi
elif [[ "$read_config_format" == "ppsspp" && "$read_target_file" == "$ppssppcheevosconf" ]]; then # Separate process if this is the standalone cheevos token file used by PPSSPP
log d "Creating PPSSPP cheevos token file $ppssppcheevosconf"
echo "$new_setting_value" > "$read_target_file"
else else
log d "Changing setting: $read_setting_name to $new_setting_value in $read_target_file"
set_setting_value "$read_target_file" "$read_setting_name" "$new_setting_value" "$read_config_format" "$section" set_setting_value "$read_target_file" "$read_setting_name" "$new_setting_value" "$read_config_format" "$section"
fi fi
else else
if [[ "$read_config_format" == "retroarch" && ! "$retroarch_all" == "true" ]]; then if [[ "$read_config_format" == "retroarch" && ! "$retroarch_all" == "true" ]]; then # Separate process if this is a per-system RetroArch override file
if [[ -f "$read_target_file" ]]; then if [[ -f "$read_target_file" ]]; then
log d "Removing setting $read_setting_name from RetroArch per-system override file $read_target_file"
delete_setting "$read_target_file" "$read_setting_name" "$read_config_format" "$section" delete_setting "$read_target_file" "$read_setting_name" "$read_config_format" "$section"
if [[ -z $(cat "$read_target_file") ]]; then # If the override file is empty if [[ -z $(cat "$read_target_file") ]]; then # If the override file is empty
log d "RetroArch per-system override file is empty, removing"
rm -f "$read_target_file" rm -f "$read_target_file"
fi fi
if [[ -z $(ls -1 "$(dirname "$read_target_file")") ]]; then # If the override folder is empty if [[ -z $(ls -1 "$(dirname "$read_target_file")") ]]; then # If the override folder is empty
log d "RetroArch per-system override folder is empty, removing"
rmdir "$(realpath "$(dirname "$read_target_file")")" rmdir "$(realpath "$(dirname "$read_target_file")")"
fi fi
fi fi
elif [[ "$read_config_format" == "ppsspp" && "$read_target_file" == "$ppssppcheevosconf" ]]; then # Separate process if this is the standalone cheevos token file used by PPSSPP
log d "Removing PPSSPP cheevos token file $ppssppcheevosconf"
rm "$read_target_file"
else else
local default_setting_value=$(get_setting_value "$read_defaults_file" "$read_setting_name" "$read_config_format" "$section") local default_setting_value=$(get_setting_value "$read_defaults_file" "$read_setting_name" "$read_config_format" "$section")
log d "Changing setting: $read_setting_name to $default_setting_value in $read_target_file"
set_setting_value "$read_target_file" "$read_setting_name" "$default_setting_value" "$read_config_format" "$section" set_setting_value "$read_target_file" "$read_setting_name" "$default_setting_value" "$read_config_format" "$section"
fi fi
fi fi