Changeset - 875a40fa0362
[Not reviewed]
0 1 0
Branko Majic (branko) - 4 years ago 2020-07-05 01:13:25
branko@majic.rs
Noticket: Introduce mechanism for validating common paths:

- Add support for validating game installations directory and new
instance directory.
1 file changed with 65 insertions and 39 deletions:
0 comments (0 inline, 0 general) First comment
games/factorio_manager.sh
Show inline comments
 
@@ -821,12 +821,71 @@ function read_server_settings() {
 
            settings_value[$key]="[${value##, }]"
 
        fi
 
        debug "Processed value for $key: ${settings_value[$key]}"
 
    done
 
}
 

	
 
#
 
# Validates paths according to internal tool rules or terminates the
 
# program.
 
#
 
# This function helps to reduce boilerplate and centralise some common
 
# checks around handling of various paths.
 
#
 
# Arguments:
 
#
 
#   $1 (path_test)
 
#     Test to apply against the path. Supported tests are:
 
#
 
#       game_installations_directory
 
#         Checks if path contains Factorio installations in
 
#         sub-directories.
 
#
 
#       instance_directory_new
 
#         Checks if path is unused and can be used for new instance.
 
#
 
#   $2 (path)
 
#     Path that should be validated.
 
#
 
#   $3 (exit_code0
 
#     Exit code to use when terminating the program.
 
#
 
# Exits:
 
#
 
#   If the path type is unsupported, or if the specified path does not
 
#   conform to requirements for the specified path type.
 
#
 
function validate_path_or_terminate() {
 
    local path_test="$1"
 
    local path="$2"
 
    local exit_code="$3"
 

	
 
    if [[ $path_test == "game_installations_directory" ]]; then
 
        # Make sure user has set directory with game installations -
 
        # test both symlink and target destination.
 
        if [[ ! -L $game_installations_directory || ! -d $game_installations_directory ]]; then
 
            error "Game installations directory has not been properly set. Please run the set-game-dir command first."
 
            exit "$exit_code"
 
        fi
 

	
 
    elif [[ $path_test == "instance_directory_new" ]]; then
 
        if [[ -e $path/config.ini ]]; then
 
            error "Instance already exists."
 
            exit "$exit_code"
 
        fi
 

	
 
        if [[ -e $path ]]; then
 
            error "Instance directory already exists, but does not contain a valid Factorio instance: $path."
 
            exit "$exit_code"
 
        fi
 
    else
 
        error "Unable to validate path '$path', unsupported test requested: '$path_test'."
 
        exit "$ERROR_GENERAL"
 
    fi
 
}
 

	
 
# Define error codes.
 
SUCCESS=0
 
ERROR_ARGUMENTS=1
 
ERROR_CONFIGURATION=2
 
ERROR_GENERAL=3
 

	
 
@@ -915,23 +974,13 @@ if [[ $command == set-game-dir ]]; then
 

	
 
#==========#
 
# versions #
 
#==========#
 
elif [[ $command == versions ]]; then
 

	
 
    # Make sure user has set directory with game installations - test
 
    # both symlink and target destination.
 
    if [[ ! -L $game_installations_directory ]]; then
 
        error "Game installations directory has not been properly set. Please run the set-game-dir command first."
 
        exit "$ERROR_CONFIGURATION"
 
    fi
 

	
 
    if [[ ! -d $game_installations_directory ]]; then
 
        error "Game installations directory has not been properly set. Please run the set-game-dir command first."
 
        exit "$ERROR_CONFIGURATION"
 
    fi
 
    validate_path_or_terminate "game_installations_directory" "$game_installations_directory" "$ERROR_CONFIGURATION"
 

	
 
    echo "Locally available Factorio versions:"
 
    echo
 

	
 
    # Find all sub-directories that are Factorio installations.
 
    for candidate in "$game_installations_directory"/*; do
 
@@ -958,23 +1007,13 @@ elif [[ $command == list ]]; then
 
    echo
 

	
 
#========#
 
# create #
 
#========#
 
elif [[ $command == create ]]; then
 
    # Make sure user has set directory with game installations - test
 
    # both symlink and target destination.
 
    if [[ ! -L $game_installations_directory ]]; then
 
        error "Game installations directory has not been properly set. Please run the set-game-dir command first."
 
        exit "$ERROR_CONFIGURATION"
 
    fi
 

	
 
    if [[ ! -d $game_installations_directory ]]; then
 
        error "Game installations directory has not been properly set. Please run the set-game-dir command first."
 
        exit "$ERROR_CONFIGURATION"
 
    fi
 
    validate_path_or_terminate "game_installations_directory" "$game_installations_directory" "$ERROR_CONFIGURATION"
 

	
 
    # Read positional arguments.
 
    instance="${1-}"
 
    shift
 

	
 
    # Calculate derived variables.
 
@@ -985,16 +1024,13 @@ elif [[ $command == create ]]; then
 
    # Verify arguments.
 
    if [[ -z $instance ]]; then
 
        error "Missing argument: INSTANCE"
 
        exit "$ERROR_ARGUMENTS"
 
    fi
 

	
 
    if [[ -e $instance_directory ]]; then
 
        error "Instance already exists."
 
        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
 
@@ -1893,16 +1929,13 @@ elif [[ $command == copy ]]; then
 

	
 
    if [[ ! -f $source_game_config ]]; then
 
        error "Missing game configuration file: $source_game_config"
 
        exit "$ERROR_GENERAL"
 
    fi
 

	
 
    if [[ -e $destination_instance_directory ]]; then
 
        error "Instance already exists."
 
        exit "$ERROR_ARGUMENTS"
 
    fi
 
    validate_path_or_terminate "instance_directory_new" "$destination_instance_directory" "$ERROR_ARGUMENTS"
 

	
 
    (
 
        # Obtain lock - Factorio uses the same mechanism, so we should
 
        # be able to detect the game is running in this way.
 
        flock --exclusive --nonblock 200
 
        if [[ $? != 0 ]]; then
 
@@ -2043,17 +2076,13 @@ elif [[ $command == import ]]; then
 
    import_entries["factorio-current.log"]="contains logs from the currently running game"
 
    import_entries["factorio-previous.log"]="contains logs from the previously running game"
 
    import_entries["mods"]="contains mods and mod settings"
 
    import_entries["player-data.json"]="contains global information about the player, such as username, login token, chat history, etc."
 
    import_entries["saves"]="contains savegames"
 

	
 
    # Ensure we are working with valid directories.
 
    if [[ -e $instance_directory ]]; then
 
        error "Instance already exists."
 
        exit "$ERROR_ARGUMENTS"
 
    fi
 
    validate_path_or_terminate "instance_directory_new" "$instance_directory" "$ERROR_ARGUMENTS"
 

	
 
    if [[ ! -f $source_directory/bin/x64/factorio ]]; then
 
        error "Could not locate Factorio binary in source directory under: $source_directory/bin/x64/factorio"
 
        error "Factorio Manager natively supports instance imports only when all game data (savegames etc) are stored within Factorio installation directory."
 
        exit "$ERROR_ARGUMENTS"
 
    fi
 
@@ -2183,16 +2212,13 @@ elif [[ $command == create-server ]]; then
 
    # Verify arguments.
 
    if [[ -z $instance ]]; then
 
        error "Missing argument: INSTANCE"
 
        exit "$ERROR_ARGUMENTS"
 
    fi
 

	
 
    if [[ -e $instance_directory ]]; then
 
        error "Instance already exists."
 
        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
0 comments (0 inline, 0 general) First comment
You need to be logged in to comment. Login now