Changeset - 88411ad1540e
[Not reviewed]
0 1 0
Branko Majic (branko) - 22 months ago 2022-07-14 23:16:38
branko@majic.rs
[factorio_manager.sh] Added command for repeatedly launching an instance:

- Very helpful when developing mods that modify prototypes and require
instance restarts to make the changes visible.
1 file changed with 87 insertions and 0 deletions:
0 comments (0 inline, 0 general)
games/factorio_manager.sh
Show inline comments
 
@@ -30,6 +30,7 @@ $program $version, helper tool for managing Factorio instances
 

	
 
Usage:
 
  $program [OPTIONS] launch INSTANCE
 
  $program [OPTIONS] launch-loop INSTANCE
 
  $program [OPTIONS] info INSTANCE
 
  $program [OPTIONS] list
 

	
 
@@ -161,6 +162,13 @@ launch INSTANCE
 
    Launches the instance with specified name. See the note for the
 
    "create" command.
 

	
 
launch-loop INSTANCE
 

	
 
    Launches the instance with specified name in a loop. Instance will
 
    be relaunched every time it exits. Useful for mod development. To
 
    stop the loop, send SIGTERM to Factorio instance (or just press
 
    CTRL-C). See the note for the "create" command.
 

	
 
list
 

	
 
    Lists available Factorio instances.
 
@@ -1526,6 +1534,85 @@ elif [[ $command == launch ]]; then
 
    fi
 

	
 

	
 
#=============#
 
# launch-loop #
 
#=============#
 
elif [[ $command == launch-loop ]]; then
 

	
 
    # Make sure game installations directory has been set.
 
    validate_path_or_terminate "game_installations_directory" "$game_installations_directory" "$ERROR_CONFIGURATION"
 

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

	
 
    # Set-up derived variables.
 
    instance_directory="$manager_directory/$instance"
 
    instance_config="$instance_directory/instance.conf"
 
    game_config="$instance_directory/config.ini"
 
    server_config="$instance_directory/server-settings.json"
 

	
 
    # Verify arguments.
 
    if [[ -z $instance ]]; then
 
        error "Missing argument: INSTANCE"
 
        exit "$ERROR_ARGUMENTS"
 
    fi
 

	
 
    # Validate that instance directory contains valid instance.
 
    validate_path_or_terminate "instance_directory" "$instance_directory" "$ERROR_ARGUMENTS"
 

	
 
    # Update instance write directory prior to launching - this is a
 
    # failsafe in case it got changed by hand or perhaps the value
 
    # comes from backups of a copied instance.
 
    current_write_data=$(grep "^write-data=" "$game_config")
 
    expected_write_data="write-data=${instance_directory}"
 

	
 
    if [[ $current_write_data != "$expected_write_data" ]]; then
 
        warning "Incorrect path specified for write-data in game configuration file: $game_config"
 
        warning "Current configuration is: $current_write_data"
 
        warning "Configuration will be replaced with: $expected_write_data"
 
        sed -i -e "s#^write-data=.*#$expected_write_data#" "$game_config"
 
    fi
 

	
 
    # Read launcher configuration for the instance.
 
    # shellcheck source=/dev/null
 
    source "$instance_config"
 

	
 
    if [[ -z $game_version ]]; then
 
        error "Missing game version information in $game_config."
 
        exit "$ERROR_CONFIGURATION"
 
    fi
 

	
 
    # Set-up paths for launching the game, and ensure they still exist
 
    # (versions can be removed by user).
 
    game_directory="${game_installations_directory}/${game_version}"
 
    factorio_bin="$game_directory/bin/x64/factorio"
 

	
 
    if [[ ! -e $factorio_bin ]]; then
 
        error "Could not locate Factorio binary under: $factorio_bin"
 
        error "Factorio $game_version installation may have been removed from game installations directory:"
 
        error "   $(readlink -f "$game_installations_directory")"
 
        exit "$ERROR_CONFIGURATION"
 
    fi
 

	
 
    # Launch instance
 
    if [[ -e $server_config ]]; then
 
        while "$factorio_bin" --config "$game_config" --start-server "$instance_directory/saves/default.zip"; do
 
            info "Relaunching in 2 seconds (press CTRL-C to abort)..."
 
            if ! sleep 2; then
 
                exit
 
            fi
 
        done
 
    else
 
        while "$factorio_bin" --config "$game_config"; do
 
            info "Relaunching in 2 seconds (press CTRL-C to abort)..."
 
            if ! sleep 2; then
 
                exit
 
            fi
 
        done
 
    fi
 

	
 

	
 
#========#
 
# backup #
 
#========#
0 comments (0 inline, 0 general)