Gamer-Server-Utility-Scripts/Factorio/screenstart.sh

202 lines
6.6 KiB
Bash
Raw Normal View History

#!/bin/bash
#ClayGarth.com
game=factorio
############################## SAVE AND STOP FUNCTIONS ######################
stop() {
echo "Stopping $server server."
# Try to save before forcing the screen closed.
screen -p 0 -S $server -X eval "stuff /quit\015" >> /dev/null 2>&1
LOG_FILE="/home/$game/servers/$server/factorio-current.log"
MONITOR_STRING="Goodbye"
echo "Monitoring the $server server's log file for save confirmation."
# Check if the monitor string is already present in the log file
if grep -q "$MONITOR_STRING" "$LOG_FILE"; then
echo "The $server server was already stopped!"
else
# Tail the log file and monitor for the specified string
# Wait for the server to fully save before starting the server instead of force quitting the screen
if timeout 30 tail -n 0 -F "$LOG_FILE" | grep -q "$MONITOR_STRING"; then
echo "The $server server has saved successfully."
else
echo "Timeout reached. The $server server did not save within 30 seconds."
screen -S $server -p 0 -X stuff "^C" >> /dev/null 2>&1
sleep 2
screen -X -S $server quit >> /dev/null 2>&1
sleep 1
fi
fi
# Removes the .temp save game to fix loading issues.
# Technically a fix, but it needs to go through each server's files.
cd /home/$game/servers/$server/saves || exit 1
rm ./*.tmp.zip >> /dev/null 2>&1
echo "=============================="
}
stopall(){
server="test"
stop
server="ribbon"
stop
server="family"
stop
server="private"
stop
}
################################ UPDATE FUNCTION ##################################
update(){
# URL for the Factorio API
FACTORIO_API="https://factorio.com/api/latest-releases"
# Use curl to get the JSON response from the Factorio API
FACTORIO_RESPONSE=$(curl -s "$FACTORIO_API")
# Use jq to parse the JSON and extract the stable headless version
FACTORIO_VERSION=$(echo "$FACTORIO_RESPONSE" | jq -r '.stable.headless')
# Remove periods from the Factorio version number
FACTORIO_VERSION_NO_PERIODS=$(echo "$FACTORIO_VERSION" | tr -d '.')
# Path to the file storing the current Factorio version
VERSION_FILE="/home/$game/factorio_version.txt"
# Check if version.txt exists and if the latest Factorio version is newer
if [ -f "$VERSION_FILE" ]; then
CURRENT_VERSION=$(cat "$VERSION_FILE")
if [ "$FACTORIO_VERSION_NO_PERIODS" -gt "$CURRENT_VERSION" ]; then
echo "Newer Factorio version available. Updating $VERSION_FILE and downloading..."
# Update version.txt with the new Factorio version
echo "$FACTORIO_VERSION_NO_PERIODS" > "$VERSION_FILE"
# Update Factorio
cd /home/$game || exit 1
wget https://www.factorio.com/get-download/stable/headless/linux64 -O factorio.tar.xz
tar -xf factorio.tar.xz
rm -f factorio.tar.xz
#mod updater
#default mods, most servers will use this mod dir
#servers w/ special mods will need a new line under here and a new dir created for them.
/usr/bin/python3 /home/factorio/mod_updater.py -s /home/factorio/servers/Master/server-settings.json -m /home/factorio/mods/default --token 2674c79ebc4fb9e04c29 --username clash --fact-path /home/factorio/factorio/bin/x64/factorio --update
#/usr/bin/python3 /home/factorio/mod_updater.py -s /home/factorio/servers/Master/server-settings.json -m /home/factorio/mods/dad --token 2674fb9e04c29 --username clash --fact-path /home/factorio/factorio/bin/x64/factorio --update
else
echo "Factorio is already up-to-date. No need to download an update."
echo "=============================="
fi
else
# Create version.txt if it doesn't exist
echo "$FACTORIO_VERSION_NO_PERIODS" > "$VERSION_FILE"
echo "Factorio version file created."
fi
}
################################ BASH ARGUMENTS ################################
if [ "$1" = "--update" ];
then
update
echo "updated"
fi
if [ "$1" = "--help" ];
then
echo "--update or --stop"
exit
fi
if [ "$1" = "--stop" ];
then
stopall
exit
fi
################################ ISSUE FIXES ################################
#fixes perms issues
chown -R $game:$game /home/$game/ > /dev/null 2>&1
chmod -R 755 /home/factorio/mods > /dev/null 2>&1
chmod +x /home/$game/factorio/bin/x64/factorio > /dev/null 2>&1
#Copy mod settings, they sometimes get overwritten
cp /home/factorio/mods/mod-settings.dat /home/factorio/mods/default/ > /dev/null 2>&1
######################################### UPDATE &RESTART Servers ###########################################
#Starts a screen session for each server.
#Change --server-settings , --config and --port for each server.
#Do NOT put $start in quotes when starting the screen. Even if ShellCheck tells you to.
cd /home/$game/factorio/bin/x64/ || exit 1
#Stop all servers
stopall
#Update Factorio if needed
update
#Start all servers
#TEST SERVER
server='test'
port='34198'
start="/home/$game/factorio/bin/x64/factorio --start-server-load-latest --server-settings /home/factorio/servers/$server/server-settings.json --config /home/factorio/servers/$server/config.ini --executable-path /home/factorio/factorio/bin/x64/ --mod-directory /home/factorio/mods/default/ --port $port"
echo "Starting test Server"
/usr/bin/screen -dmS $server $start
#RIBBON SERVER
server='ribbon'
port='34196'
start="/home/$game/factorio/bin/x64/factorio --start-server-load-latest --server-settings /home/factorio/servers/$server/server-settings.json --config /home/factorio/servers/$server/config.ini --executable-path /home/factorio/factorio/bin/x64/ --mod-directory /home/factorio/mods/default/ --port $port"
#echo "Starting ribbon Server"
#/usr/bin/screen -dmS $server $start
#FAMILY SERVER
server='family'
port='34197'
start="/home/$game/factorio/bin/x64/factorio --start-server-load-latest --server-settings /home/factorio/servers/$server/server-settings.json --config /home/factorio/servers/$server/config.ini --executable-path /home/factorio/factorio/bin/x64/ --mod-directory /home/factorio/mods/default/ --port $port"
echo "Starting family Server"
/usr/bin/screen -dmS $server $start
#PRIVATE SERVER
server='private'
port='34199'
start="/home/$game/factorio/bin/x64/factorio --start-server-load-latest --server-settings /home/factorio/servers/$server/server-settings.json --config /home/factorio/servers/$server/config.ini --executable-path /home/factorio/factorio/bin/x64/ --mod-directory /home/factorio/mods/default/ --port $port"
echo "Starting private Server"
/usr/bin/screen -dmS $server $start
echo "=============================="
screen -ls
echo "=============================="
exit 0