Skip to content

Instantly share code, notes, and snippets.

@thislooksfun
Last active May 1, 2017 05:39
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save thislooksfun/d747e931d806a45ac2fd47bf57268297 to your computer and use it in GitHub Desktop.
Save thislooksfun/d747e931d806a45ac2fd47bf57268297 to your computer and use it in GitHub Desktop.
A simple mod migration script for Factorio 0.14 -> 0.15
#!/bin/bash
VERSION="1.1.1"
# ██ ██ ███████ ██████ ██ ███████ ██ ██ █████ ██████ ██████ ███████
# ██ ██ ██ ██ ██ ██ ██ ██ ██ ██ ██ ██ ██ ██ ██
# ██ ██ █████ ██████ ██ █████ ████ ███████ ██████ ██ ███ ███████
# ██ ██ ██ ██ ██ ██ ██ ██ ██ ██ ██ ██ ██ ██ ██
# ████ ███████ ██ ██ ██ ██ ██ ██ ██ ██ ██ ██████ ███████
VERBOSE=false
if [ $# -eq 0 ]; then
echo "Must pass a mod path to the migration tool"
exit 1
elif [ $# -eq 1 ]; then
MOD=$1
elif [ $# -eq 2 ]; then
if [ $1 == '-v' ]; then
VERBOSE=true
MOD=$2
elif [ $2 == '-v' ]; then
VERBOSE=true
MOD=$1
else
echo "Invalid perameters"
exit 1
fi
elif [ $# -gt 2 ]; then
echo "Expected 1-2 arguments, got '$#'"
exit 1
fi
MOD=${MOD%/}
if ! [ -e "$MOD" ]; then
echo "'$MOD' isn't a valid filesystem path"
exit 1
fi
if [ -d "$MOD" ]; then
MODNAME=$MOD
elif [[ $MOD == *.zip ]]; then
MODNAME=${MOD%.zip}
else
echo "Mod path must be a folder or a .zip file"
exit 1
fi
MODNAME=${MODNAME##*/}
# ███████ ████████ █████ ██████ ████████
# ██ ██ ██ ██ ██ ██ ██
# ███████ ██ ███████ ██████ ██
# ██ ██ ██ ██ ██ ██ ██
# ███████ ██ ██ ██ ██ ██ ██
cd $(dirname "$MOD")
LOG="./migration_log_$MODNAME.txt"
touch "$LOG"
echo -e "mod_migrator v$VERSION\n"
echo "Migrating mod '$MODNAME' to version 0.15" | tee "$LOG"
echo -e "\n-- Backing up..." | tee -a "$LOG"
cp "$MOD" "./${MOD##*/}.backup" | tee -a "$LOG"
WASZIPPED=false
if [[ $MOD == *.zip ]]; then
WASZIPPED=true
echo -e "\n-- Unzipping mod..." | tee -a "$LOG"
if $VERBOSE; then
unzip -o "$MOD" | sed "s/^/ - /" | tee -a "$LOG"
else
unzip -o "$MOD" >> /dev/null
fi
if [ -d ./__MACOSX ]; then
rm -r ./__MACOSX | sed "s/^/ - /" | tee -a "$LOG"
fi
rm "$MOD"
MOD=${MOD%.zip}
fi
FV="factorio_version\": \"0.15"
if grep -q "$FV" "$MOD/info.json"; then
echo -e "\nMod already claims to work on 0.15! ('factorio_version' is set to '0.15')" | tee -a "$LOG"
echo "Skipping to cleanup" | tee -a "$LOG"
else
# ██ ██████ ██████ █████ ██ ███████
# ██ ██ ██ ██ ██ ██ ██ ██
# ██ ██ ██ ██ ███████ ██ █████
# ██ ██ ██ ██ ██ ██ ██ ██
# ███████ ██████ ██████ ██ ██ ███████ ███████
# ' = ' is not allowed anymore, must be '=' (no spaces)
LOCALE="$MOD/locale"
if [ -d "$LOCALE" ]; then
echo -e "\n-- Correcting locale files... " | tee -a "$LOG"
for d in "$LOCALE"/*; do
echo ${d##*/} | sed "s/^/ - /" | tee -a "$LOG"
for f in "$d"/*; do
echo -n "$(echo ${f##*/}... | sed "s/^/ - /")" | tee -a "$LOG"
sed -i '' -e 's/ ?= ?/=/' "$f" >> /dev/null
echo "done" | tee -a "$LOG"
done
done
fi
# ██████ ███████ ███ ██ █████ ███ ███ ███████
# ██ ██ ██ ████ ██ ██ ██ ████ ████ ██
# ██████ █████ ██ ██ ██ ███████ ██ ████ ██ █████
# ██ ██ ██ ██ ██ ██ ██ ██ ██ ██ ██ ██
# ██ ██ ███████ ██ ████ ██ ██ ██ ██ ███████
# Some script calls have been renamed/moved, update them accordingly
declare -a FIND
declare -a REPLACE
FIND[0]="less_then"; REPLACE[0]="less_than"
FIND[1]="railpictures()"; REPLACE[1]="rail_pictures()"
FIND[2]="underground_belt_distance"; REPLACE[2]="max_underground_distance"
FIND[3]="game.get_event_handler"; REPLACE[3]="script.get_event_handler"
FIND[4]="game.raise_event"; REPLACE[4]="script.raise_event"
FIND[5]="alien-science-pack"; REPLACE[5]="production-science-pack"
declare -a REMOVED
REMOVED[0]="coroutine"
REMOVED[1]="evolution_factor"
REMOVED[2]="build_from_cursor"
REMOVED[3]="rotate_for_build"
REMOVED[4]="get_tileproperties"
REMOVED[5]="fluid_resource_statistics"
echo -e "\n-- Updating renamed APIs..." | tee -a "$LOG"
find "$MOD" -name "*.lua" | while read f; do
python -c "import os.path; print \" - \" + os.path.relpath('$f', '$MOD')" | tee -a "$LOG"
L=1
while IFS= read -r line; do
for ((i = 0; i < ${#FIND[@]}; ++i)); do
line2=$(echo "$line" | sed -e "s/${FIND[$i]}/${REPLACE[$i]}/g")
if ! [ "$line" == "$line2" ]; then
printf " - L%s: %-30s -> %s\n" $L ${FIND[$i]} ${REPLACE[$i]} | tee -a "$LOG"
sed -i '' -e "${L}s/^.*$/$line2/" "$f" | tee -a "$LOG"
fi
done
for r in ${REMOVED[@]}; do
if [[ "$line" == *"$r"* ]]; then
printf " - L%s: Warning: '%-30s' has been removed\n" $L $r | tee -a "$LOG"
fi
done
L=$(( L+1 ))
done < "$f"
done
# ███████ ██ ██
# ██ ██ ██
# █████ ██ ██
# ██ ██ ██
# ██ ███████ ████
echo -e "\n-- Setting factorio_version to 0.15..." | tee -a "$LOG"
sed -i '' -e 's/"factorio_version": "0\.14"/"factorio_version": "0.15"/' "$MOD/info.json"
fi
# ██████ ██ ███████ █████ ███ ██ ██ ██ ██████
# ██ ██ ██ ██ ██ ████ ██ ██ ██ ██ ██
# ██ ██ █████ ███████ ██ ██ ██ ██ ██ ██████
# ██ ██ ██ ██ ██ ██ ██ ██ ██ ██ ██
# ██████ ███████ ███████ ██ ██ ██ ████ ██████ ██
if $WASZIPPED; then
echo -e "\n-- Re-zipping mod..." | tee -a "$LOG"
if $VERBOSE; then
zip -r "$MODNAME.zip" "${MOD##*/}" | sed "s/^/ - /" | tee -a "$LOG"
zip -d "$MODNAME.zip" __MACOSX/\* | sed "s/^/ - /" | tee -a "$LOG"
zip -d "$MODNAME.zip" \*/.DS_Store | sed "s/^/ - /" | tee -a "$LOG"
else
zip -r "$MODNAME.zip" "${MOD##*/}" >> /dev/null
zip -d "$MODNAME.zip" __MACOSX/\* >> /dev/null
zip -d "$MODNAME.zip" \*/.DS_Store >> /dev/null
fi
rm -r "$MOD"
fi
echo -e "\nMigration Done!\n" | tee -a "$LOG"
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment