Created
January 8, 2024 06:40
-
-
Save iso2022jp/761d3a79eb37a349e091c4a192dd0c06 to your computer and use it in GitHub Desktop.
Factorio Headless Updater
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/bin/bash | |
set -euo pipefail | |
DRYRUN='' | |
DOWNLOAD_ONLY='' | |
usage() { | |
echo "Usage: $0 [-n|-d]" 1>&2 | |
exit 1 | |
} | |
while getopts nd OPT; do | |
case $OPT in | |
n) DRYRUN=1;; | |
d) DOWNLOAD_ONLY=1;; | |
*) usage;; | |
esac | |
done | |
shift $((OPTIND - 1)) | |
# System | |
readonly FACTORIO=/opt/factorio/bin/x64/factorio | |
readonly SERVICE=factorio | |
readonly SERVICE_USER=factorio | |
readonly PLATFORM=core-linux_headless64 | |
# patch mapping JSON | |
readonly PATCH_MAP='https://updater.factorio.com/get-available-versions' | |
# package=core-linux_headless64 | |
# isTarget=false | |
# apiVersion=2 | |
# from=1.1.70 | |
# to=1.1.71 | |
readonly PATCH_BASE="https://updater.factorio.com/updater/get-download?package=$PLATFORM&isTarget=false&apiVersion=2" | |
version() { $FACTORIO --version | grep -oP '(?<=Version: )\d+\.\d+\.\d+'; } | |
filter_stable() { jq -r '.["core-linux_headless64"] | .[] | select(has("stable")).stable'; } | |
filter_next() { jq -r '.["core-linux_headless64"] | .[] | select(.from == "'"${1?Missing version from.}"'").to'; } | |
stop_daemon() { | |
if [[ ! $DRYRUN && ! $DOWNLOAD_ONLY ]]; then | |
sudo systemctl stop "$SERVICE" | |
fi | |
} | |
start_daemon() { | |
if [[ ! $DRYRUN && ! $DOWNLOAD_ONLY ]]; then | |
sudo systemctl start "$SERVICE" | |
fi | |
} | |
download_update() { | |
local from="${1?Missing version from.}" | |
local to="${2?Missing version to.}" | |
local patch="/opt/factorio-patch-$from-to-$to.zip" | |
if [[ ! $DRYRUN ]]; then | |
if [[ ! -f $patch ]]; then | |
sudo curl -#Lo "$patch" "$PATCH_BASE&from=$from&to=$to" | |
fi | |
fi | |
echo "$patch" | |
} | |
apply_update() { | |
local patch="${1?Missing file.}" | |
if [[ ! $DRYRUN && ! $DOWNLOAD_ONLY ]]; then | |
sudo -u "$SERVICE_USER" $FACTORIO --apply-update "$patch" | |
fi | |
} | |
main() { | |
# Detect current version | |
version=$(version) | |
echo "Local version: $version" | |
# Check published versions | |
echo "Cheking published versions..." | |
sudo curl -#Lo '/opt/factorio-available-versions' "$PATCH_MAP" | |
stable=$(cat /opt/factorio-available-versions | filter_stable) | |
echo "Stable version: $stable." | |
if [[ $version = $stable ]]; then | |
echo 'No update required.' && exit | |
fi | |
# collect updates | |
updates=() | |
from=$version | |
until [[ $from = $stable ]]; do | |
to=$(cat /opt/factorio-available-versions | filter_next "$from") | |
echo "Update available: from $from to $to." | |
echo "Downloading update: from $from to $to..." | |
updates+=($(download_update "$from" "$to")) | |
from="$to" | |
done | |
echo "Ready for update." | |
stop_daemon | |
trap 'start_daemon' EXIT | |
if [[ ! $DRYRUN && ! $DOWNLOAD_ONLY ]]; then | |
for update in ${updates[@]}; do | |
echo "Applying update...: $update" | |
apply_update "$update" | |
done | |
if [[ $(version) != $stable ]]; then | |
echo "Update failed!" && exit 1 | |
fi | |
fi | |
if [[ $DRYRUN ]]; then | |
echo "Dry run succeeded." | |
elif [[ $DOWNLOAD_ONLY ]]; then | |
echo "Download succeeded." | |
else | |
echo "Update succeeded." | |
fi | |
} | |
main |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment