Changeset - 6e56046dcbb4
[Not reviewed]
0 1 0
Branko Majic (branko) - 4 years ago 2020-07-05 14:26:40
branko@majic.rs
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.
1 file changed with 83 insertions and 23 deletions:
0 comments (0 inline, 0 general) First comment
games/factorio_manager.sh
Show inline comments
 
@@ -880,12 +880,94 @@ function validate_path_or_terminate() {
 
    else
 
        error "Unable to validate path '$path', unsupported test requested: '$path_test'."
 
        exit "$ERROR_GENERAL"
 
    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
 
ERROR_CONFIGURATION=2
 
ERROR_GENERAL=3
 

	
 
@@ -1026,32 +1108,13 @@ elif [[ $command == create ]]; then
 
        error "Missing argument: INSTANCE"
 
        exit "$ERROR_ARGUMENTS"
 
    fi
 

	
 
    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"
 
    mkdir "$instance_directory/mods"
 
    echo "{}" > "$instance_directory/mods/mod-list.json"
 

	
 
@@ -1092,15 +1155,12 @@ EOF
 

	
 
    echo
 
    warning "Factorio Manager has created a minimal empty configuration file for Factorio under $game_config."
 
    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
 

	
 
    # Read positional arguments.
 
    instance="${1-}"
 
    shift
0 comments (0 inline, 0 general) First comment
You need to be logged in to comment. Login now