Create CLI preset change function and update direct CLI options to both

This commit is contained in:
icenine451 2025-04-01 17:29:24 -04:00
parent ab8d281696
commit a38786df62
2 changed files with 179 additions and 30 deletions

View file

@ -5,8 +5,6 @@ change_preset_dialog() {
# show their current enable/disabled state and allow the user to change one or more.
# USAGE: change_preset_dialog "$preset"
log d "Starting change_preset_dialog for preset: $preset"
preset="$1"
pretty_preset_name=${preset//_/ } # Preset name prettification
pretty_preset_name=$(echo "$pretty_preset_name" | awk '{for(i=1;i<=NF;i++){$i=toupper(substr($i,1,1))substr($i,2)}}1')
@ -15,6 +13,8 @@ change_preset_dialog() {
section_results=$(sed -n '/\['"$preset"'\]/, /\[/{ /\['"$preset"'\]/! { /\[/! p } }' "$rd_conf" | sed '/^$/d')
all_emulators_in_preset=""
log d "Starting change_preset_dialog for preset: $preset"
while IFS= read -r config_line; do
system_name=$(get_setting_name "$config_line" "retrodeck")
system_value=$(get_setting_value "$rd_conf" "$system_name" "retrodeck" "$preset")
@ -76,7 +76,6 @@ change_preset_dialog() {
fi
}
build_preset_list_options() {
# FUNCTION: build_preset_list_options
# DESCRIPTION: This function builds a list of all the systems available for a given preset.
@ -377,3 +376,64 @@ fetch_all_presets() {
echo "${presets[@]}"
fi
}
change_presets_cli () {
# This function will allow a user to change presets either individually or all for a preset class from the CLI.
# USAGE: change_presets_cli "$preset" "$system/all" "$on/off"
local preset="$1"
local system="$2"
local value="$3"
local section_results
section_results=$(sed -n '/\['"$preset"'\]/, /\[/{ /\['"$preset"'\]/! { /\[/! p } }' "$rd_conf" | sed '/^$/d')
local all_emulators_in_preset="" # A CSV string containing all emulators in a preset block
local all_other_emulators_in_preset="" # A CSV string containing every emulator except the one provided by the user in a preset block
log d "Changing settings for preset: $preset"
while IFS= read -r config_line; do
# Build a list of all emulators in the preset block
system_name=$(get_setting_name "$config_line" "retrodeck")
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 provides "all" as the system name
if [[ "$value" =~ (false|off) && ! "$system" == "all" ]]; then # If the user is disabling a specific emulator, check for any other already enabled and keep them enabled
system_value=$(get_setting_value "$rd_conf" "$system_name" "retrodeck" "$preset")
if [[ ! "$system_name" == "$system" && "$system_value" == "true" ]]; then
log d "Other system $system_name is enabled for preset $preset, retaining setting."
if [[ -n $all_other_emulators_in_preset ]]; then
all_other_emulators_in_preset+=","
fi
all_other_emulators_in_preset+="$system_name" # Build a list of all emulators that are currently enabled that aren't the one being disabled
fi
fi
done < <(printf '%s\n' "$section_results")
if [[ "$value" =~ (true|on) ]]; then # If user is enabling one or more systems in a preset
if [[ "$system" == "all" ]]; then
log d "Enabling all emualtors for preset $preset"
choice="$all_emulators_in_preset" # All emulators in the preset will be enabled
else
if [[ "$all_emulators_in_preset" =~ "$system" ]]; then
log d "Enabling preset $preset for $system"
choice="$system"
else
log i "Emulator $system does not support preset $preset, please check the command options and try again."
fi
fi
else # If user is disabling one or more systems in a preset
if [[ "$system" == "all" ]]; then
choice="" # Empty string means all systems in preset should be disabled
else
choice="$all_other_emulators_in_preset"
fi
fi
# Call make_preset_changes if the user made a selection,
# or if an extra button was clicked (even if the resulting choice is empty, meaning all systems are to be disabled).
log d "Calling make_preset_changes with choice: $choice"
make_preset_changes "$preset" "$choice"
}

View file

@ -19,7 +19,8 @@ Arguments:
--reset <component> \t Reset RetroDECK or one or more component/emulator configurations to default values. WARNING: no confirmation prompt
--factory-reset \t Factory Reset, triggers the initial setup WARNING: no confirmation prompt
--test-upgrade <version> \t Test upgrading RetroDECK to a specific version, developer use only
--set <preset> <system/all> [value] \t Configure or toggle a preset. Examples: --set borders, --set borders all true,\n\t\t\t\t\t\t --set borders gba false. Use --set-help for more information
--get <preset> [system/all] \t Show the current status of all systems in a given preset type. Use --get-help for more information.
--set <preset> <system/all> <value> \t Configure or toggle a preset. Examples: --set borders all true,\n\t\t\t\t\t\t --set borders gba false. Use --set-help for more information
--open <component/emulator> \t Open a specific component or emulator\n\t\t\t\t\t\t --open --list for a list of available components
Game Launch:
@ -61,22 +62,33 @@ case "$1" in
fi
exit 0
;;
--get-help)
LOG_SILENT=true
source /app/libexec/global.sh
echo -e "\nUsed to check the state of all systems for a given preset.\n\nAvailable presets are:"
fetch_all_presets | tr ' ' ',' | sed 's/,/, /g'
echo -e "\nUsage: --get <preset> [system/all]"
echo -e "\nExamples:"
echo -e " Get the list of all emulators that support the \"borders\" preset:"
echo -e " --get borders"
echo -e " Get the current state of the borders preset for the snes system:"
echo -e " --get borders snes"
echo -e " Get the current state of the borders preset for all the systems that support it:"
echo -e " --get borders all"
exit 0
;;
--set-help)
LOG_SILENT=true
source /app/libexec/global.sh
echo -e "\nUsed to toggle or set a preset.\n\nAvailable presets are:"
fetch_all_presets | tr ' ' ',' | sed 's/,/, /g'
echo -e "\nUsage: --set <preset> <system/all> [value]"
echo -e "\nUsage: --set <preset> <system/all> <value>"
echo -e "\nExamples:"
echo -e " Force borders to be ON for GBA:"
echo -e " make_preset_changes borders gba on"
echo -e " Force borders to be OFF for all supported systems:"
echo -e " make_preset_changes borders all off"
echo -e " Toggle GBA in preset borders (this will disable if enabled and vice versa):"
echo -e " make_preset_changes borders gba"
echo -e " Toggle all in preset borders:"
echo -e " make_preset_changes borders all"
echo -e "\nYou can also use 'true' or 'false' instead of 'on' and 'off'.\nThe forced status is case insensitive."
echo -e " Enable borders for GBA:"
echo -e " --set borders gba on"
echo -e " Disable borders for all supported systems:"
echo -e " --set borders all off"
echo -e "\nYou can also use 'true' or 'false' instead of 'on' and 'off'."
exit 0
;;
esac
@ -96,17 +108,17 @@ if [ -f "$lockfile" ]; then
set_setting_value "$rd_conf" "update_check" "true" retrodeck "options"
set_setting_value "$rd_conf" "developer_options" "true" retrodeck "options"
set_setting_value "$rd_conf" "logging_level" "debug" retrodeck "options"
cooker_base_version=$(echo $version | cut -d'-' -f2)
cooker_base_version=$(echo "$version" | cut -d'-' -f2)
choice=$(rd_zenity --icon-name=net.retrodeck.retrodeck --info --no-wrap --ok-label="Upgrade" --extra-button="Don't Upgrade" --extra-button="Full Wipe and Fresh Install" \
--window-icon="/app/share/icons/hicolor/scalable/apps/net.retrodeck.retrodeck.svg" \
--title "RetroDECK Cooker Upgrade" \
--text="You appear to be upgrading to a \"cooker\" build of RetroDECK.\n\nWould you like to perform the standard post-update process, skip the post-update process or remove ALL existing RetroDECK folders and data (including ROMs and saves) to start from a fresh install?\n\nPerforming the normal post-update process multiple times may lead to unexpected results.")
rc=$? # Capture return code, as "Yes" button has no text value
if [[ $rc == "1" ]]; then # If any button other than "Yes" was clicked
if [[ $choice == "Don't Upgrade" ]]; then # If user wants to bypass the post_update.sh process this time.
if [[ "$choice" == "Don't Upgrade" ]]; then # If user wants to bypass the post_update.sh process this time.
log i "Skipping upgrade process for cooker build, updating stored version in retrodeck.cfg"
set_setting_value "$rd_conf" "version" "$hard_version" retrodeck # Set version of currently running RetroDECK to updated retrodeck.cfg
elif [[ $choice == "Full Wipe and Fresh Install" ]]; then # Remove all RetroDECK data and start a fresh install
elif [[ "$choice" == "Full Wipe and Fresh Install" ]]; then # Remove all RetroDECK data and start a fresh install
if [[ $(configurator_generic_question_dialog "RetroDECK Cooker Reset" "This is going to remove all of the data in all locations used by RetroDECK!\n\n(INCLUDING BIOS FILES, BORDERS, DOWNLOADED MEDIA, GAMELISTS, MODS, ROMS, SAVES, STATES, SCREENSHOTS, TEXTURE PACKS AND THEMES)\n\nAre you sure you want to contine?") == "true" ]]; then
if [[ $(configurator_generic_question_dialog "RetroDECK Cooker Reset" "Are you super sure?\n\nThere is no going back from this process, everything is gonzo.\nDust in the wind.\n\nYesterdays omelette.") == "true" ]]; then
if [[ $(configurator_generic_question_dialog "RetroDECK Cooker Reset" "But are you super DUPER sure? We REAAAALLLLLYY want to make sure you know what is happening here.\n\nThe ~/retrodeck and ~/.var/app/net.retrodeck.retrodeck folders and ALL of their contents\nare about to be PERMANENTLY removed.\n\nStill sure you want to proceed?") == "true" ]]; then
@ -125,17 +137,17 @@ if [ -f "$lockfile" ]; then
fi
else
log i "Performing normal upgrade process for version $cooker_base_version"
version=$cooker_base_version # Temporarily assign cooker base version to $version so update script can read it properly.
version="$cooker_base_version" # Temporarily assign cooker base version to $version so update script can read it properly.
post_update
fi
else # If newly-installed version is a normal build.
if grep -qF "cooker" <<< "$version"; then # If previously installed version was a cooker build
cooker_base_version=$(echo "$version" | cut -d'-' -f2)
version=$cooker_base_version # Temporarily assign cooker base version to $version so update script can read it properly.
set_setting_value "$rd_conf" "update_repo" "RetroDECK" retrodeck "options"
set_setting_value "$rd_conf" "update_check" "false" retrodeck "options"
set_setting_value "$rd_conf" "update_ignore" "" retrodeck "options"
set_setting_value "$rd_conf" "developer_options" "false" retrodeck "options"
version="$cooker_base_version" # Temporarily assign cooker base version to $version so update script can read it properly.
set_setting_value $rd_conf "update_repo" "RetroDECK" retrodeck "options"
set_setting_value $rd_conf "update_check" "false" retrodeck "options"
set_setting_value $rd_conf "update_ignore" "" retrodeck "options"
set_setting_value $rd_conf "developer_options" "false" retrodeck "options"
set_setting_value "$rd_conf" "logging_level" "info" retrodeck "options"
fi
post_update # Executing post update script
@ -220,19 +232,96 @@ while [[ $# -gt 0 ]]; do
exit 1
fi
;;
--get)
preset="$2"
system="$3"
if [[ -z "$preset" ]]; then
echo "Error: No preset specified. Usage: --get <preset> [system] (use --get-help for more information)"
exit 1
elif [[ $(fetch_all_presets | tr ' ' ',' | sed 's/,/, /g') =~ "$preset" ]]; then
preset_compatible_systems=$(sed -n '/\['"$preset"'\]/, /\[/{ /\['"$preset"'\]/! { /\[/! p } }' "$rd_conf" | sed '/^$/d')
if [[ -z "$system" ]]; then # User provided a preset but no system argument
preset_compatible_systems=$(echo "$preset_compatible_systems" | cut -d= -f1 | sed ':a;N;$!ba;s/\n/, /g')
echo "The systems that support the preset $preset are $preset_compatible_systems"
elif [[ "$system" == "all" ]]; then
while IFS= read -r config_line; do
current_system_name=$(get_setting_name "$config_line" "retrodeck")
current_system_value=$(get_setting_value "$rd_conf" "$current_system_name" "retrodeck" "$preset")
if [[ "$current_system_value" == "true" ]]; then
current_system_value="enabled"
else
current_system_value="disabled"
fi
echo "The preset $preset for the system $current_system_name is $current_system_value"
done < <(printf '%s\n' "$preset_compatible_systems")
elif [[ "$preset_compatible_systems" =~ "$system" ]]; then
preset_state=$(get_setting_value "$rd_conf" "$system" "retrodeck" "$preset")
if [[ "$preset_state" == "true" ]]; then
preset_state="enabled"
else
preset_state="disabled"
fi
echo "The preset $preset for the system $system is $preset_state."
else
echo "The system $system is not compatible with the preset $preset. (use --get-help for more information)"
exit 1
fi
else
echo "Preset $preset not recognized. Use --get-help for a list of valid options."
exit 1
fi
exit 0
;;
--set)
preset="$2"
system="$3"
value="$4"
if [ "$preset" == "cheevos" ]; then
echo "Error: The 'cheevos' preset is not yet supported via CLI. Please use the RetroDECK Configurator."
if [[ -z "$preset" ]]; then
echo "Error: No preset specified. Usage: --set <preset> <system/all> <value> (use --set-help for more information)"
exit 1
elif [[ -z "$system" ]]; then
echo "Error: No system specified. Usage: --set <preset> <system/all> <value> (use --set-help for more information)"
exit 1
elif [[ -z "$value" ]]; then
echo "Error: No value specified. Usage: --set <preset> <system/all> <value> (use --set-help for more information)"
exit 1
elif [[ $(fetch_all_presets | tr ' ' ',' | sed 's/,/, /g') =~ "$preset" ]]; then
if [[ "$preset" == "cheevos" && "$value" =~ (true|on) ]]; then # Get cheevos login information
read -p "Please enter your RetroAchievements username: " cheevos_username
read -s -p "Please enter your RetroAchievements password: " cheevos_password
if cheevos_info=$(get_cheevos_token "$cheevos_username" "$cheevos_password"); then
echo "RetroAchievements login succeeded, proceeding..."
else # login failed
echo "RetroAchievements login failed, please try again."
exit 1
fi
fi
if [[ ! "$system" == "all" ]]; then # Check if emulator is already set as requested
current_system_value=$(get_setting_value "$rd_conf" "$system" "retrodeck" "$preset")
if [[ "$value" =~ (true|on) && "$current_system_value" == "true" ]]; then
echo "The preset $preset is already enabled for the system $system"
elif [[ "$value" =~ (false|off) && "$current_system_value" == "false" ]]; then
echo "The preset $preset is already disabled for the system $system"
else # Otherwise needs to be changed
if change_presets_cli "$preset" "$system" "$value"; then
echo "$preset preset changes for $system are complete"
else
echo "Something went wrong during the preset change, please check the logs for details."
exit 1
fi
fi
else
if change_presets_cli "$preset" "$system" "$value"; then
echo "$preset preset changes for all compatible systems are complete"
else
echo "Something went wrong during the preset change, please check the logs for details."
exit 1
fi
fi
else
echo "Preset $preset not recognized. Use --set-help for a list of valid options."
exit 1
fi
if [ -z "$preset" ]; then
echo "Error: No preset specified. Usage: --set <preset> <system/all> [value] (use --set-help for more information)"
exit 1
fi
make_preset_changes "$preset" "$system" "$value"
exit 0
;;
--open)