From 6e56046dcbb4e3465e81e915c1468863633fda48 2020-07-05 14:26:40 From: Branko Majic <branko@majic.rs> Date: 2020-07-05 14:26:40 Subject: [PATCH] Noticket: Introduce dedicated function for picking Factorio version in Factorio Manager: - Function is generic, and lets user pick the version more simply with pre-defined defaults. - Use the new function in "create" command. --- diff --git a/games/factorio_manager.sh b/games/factorio_manager.sh index 8db4f717cd3dc70254250b1ea40d1f5eda2f1b94..a0d0d9a264b3e5bb4821648da76b0c6188746bed 100755 --- a/games/factorio_manager.sh +++ b/games/factorio_manager.sh @@ -883,6 +883,88 @@ function validate_path_or_terminate() { fi } +# +# Prompts user to pick version of Factorio. +# +# Arguments: +# +# $1 (game_installations_directory) +# Path to directory containing Factorio installations. +# +# $2 (default_version, optional) +# Default version to fill-in for the user. If not set, last +# version in the directory (sorted alphabetically) will be used. +# +# $3 (default_marker, optional) +# Marker text to use for the default version. Default is "default". +# +# Returns: +# +# 0 if version was successfully selected, 1 otherwise. +# +# Sets: +# +# game_version_selected +# +function select_factorio_version() { + local game_installations_directory="$1" + local default_version="${2-}" + local default_marker="${3-default}" + + local game_versions_available=() + local candidate + local i default_option selected_option + + declare -g game_version_selected="" + + # Grab a list of available versions. + for candidate in "$game_installations_directory"/*; do + if [[ -f $candidate/bin/x64/factorio ]]; then + game_versions_available+=( "$(basename "$candidate")" ) + fi + done + + if [[ ${#game_versions_available[@]} == 0 ]]; then + error "Could not find any Factorio installations under directory '$game_installations_directory'." + error "Please unpack Factorio installations into the directory, or use set-game-dir command to specify directory with Factorio installations." + return 1 + fi + + echo "The following versions of Factorio are locally available:" + echo + + for i in "${!game_versions_available[@]}"; do + let i++ + echo -n " [$i] $(basename "${game_versions_available[$i-1]}")" + + # Highlight default version. + if [[ -z $default_version && $i == ${#game_versions_available[@]} ]] || \ + [[ -n $default_version && ${game_versions_available[i-1]} == $default_version ]]; then + colorecho boldgreen " [$default_marker]" + default_option="$i" + else + echo + fi + done + + echo + + while [[ -z $game_version_selected ]]; do + read -e -p "Please specify what version you would like to use (enter for $default_marker): " selected_option + [[ -z $selected_option ]] && selected_option="$default_option" + + if [[ $selected_option =~ ^[[:digit:]]+$ ]] && (( $selected_option >= 1 && $selected_option <= ${#game_versions_available[@]} )); then + game_version_selected="${game_versions_available[$selected_option-1]}" + else + error "Invalid option selected, please try again." + echo + fi + done + + return 0 +} + + # Define error codes. SUCCESS=0 ERROR_ARGUMENTS=1 @@ -1029,26 +1111,7 @@ elif [[ $command == create ]]; then validate_path_or_terminate "instance_directory_new" "$instance_directory" "$ERROR_ARGUMENTS" - # Display list of available Factorio versions and let user pick one. - echo "The following versions of Factorio are locally available:" - echo - - for candidate in "$game_installations_directory"/*; do - if [[ -f $candidate/bin/x64/factorio ]]; then - echo " - $(basename "$candidate")" - fi - done - - echo - - echo -n "Please specify what version you would like to use: " - read game_version_selected - - # Validate user input. - if [[ ! -f "$game_installations_directory/$game_version_selected/bin/x64/factorio" ]]; then - error "Requested version not locally available: $game_version_selected" - exit "$ERROR_ARGUMENTS" - fi + select_factorio_version "$game_installations_directory" || exit "$ERROR_GENERAL" # Set-up the instance. mkdir "$instance_directory" @@ -1095,9 +1158,6 @@ EOF warning "Since the generated configuration file is almost empty, Factorio will complain that the file seems corrupt." warning "Factorio will offer to fix the corrupted configuration file by filling-in the missing information during the first startup." warning "It should be safe to accept this. This warning will be shown by Factorio only the first time." - echo - echo "Please read the warning above, and press any key to continue." - read elif [[ $command == launch ]]; then