|
| 1 | +#!/usr/bin/env bash |
| 2 | +### Run this script on GitHub after zipping the mod to upload the mod on mods.factorio.com |
| 3 | +### This a modified version of https://github.com/shanemadden/factorio-mod-portal-publish/blob/1930e44ecd86e3f2d90921da2a1f40b050fd12c3/entrypoint.sh |
| 4 | + |
| 5 | + |
| 6 | +### Check commands |
| 7 | +has_errors=false |
| 8 | +if ! command -v jq &> /dev/null; then |
| 9 | + echo "Please install jq https://stedolan.github.io/jq/" |
| 10 | + has_errors=true |
| 11 | +fi |
| 12 | +if [ $has_errors = true ] ; then |
| 13 | + exit 1 |
| 14 | +fi |
| 15 | + |
| 16 | + |
| 17 | +### Get mod name and version from info.json |
| 18 | +### https://stedolan.github.io/jq/ |
| 19 | +MOD_VERSION=$(jq -r '.version' info.json) |
| 20 | +MOD_NAME=$(jq -r '.name' info.json) |
| 21 | + |
| 22 | + |
| 23 | +# Validate the version string we're building |
| 24 | +if ! echo "${MOD_VERSION}" | grep -P --quiet '^(v)?\d+\.\d+\.\d+$'; then |
| 25 | + echo "Bad version, needs to be (v)%u.%u.%u" |
| 26 | + exit 1 |
| 27 | +fi |
| 28 | + |
| 29 | + |
| 30 | +# Get a CSRF token by loading the login form |
| 31 | +CSRF=$(curl -b cookiejar.txt -c cookiejar.txt -s https://factorio.com/login?mods=1 | grep csrf_token | sed -r -e 's/.*value="(.*)".*/\1/') |
| 32 | + |
| 33 | + |
| 34 | +# Authenticate with the credential secrets and the CSRF token, getting a session cookie for the authorized user |
| 35 | +curl -b cookiejar.txt -c cookiejar.txt -s -e https://factorio.com/login?mods=1 -F "csrf_token=${CSRF}" -F "username_or_email=${FACTORIO_USER}" -F "password=${FACTORIO_PASSWORD}" -o /dev/null https://factorio.com/login |
| 36 | + |
| 37 | + |
| 38 | +# Query the mod info, verify the version number we're trying to push doesn't already exist |
| 39 | +curl -b cookiejar.txt -c cookiejar.txt -s "https://mods.factorio.com/api/mods/${MOD_NAME}/full" | jq -e ".releases[] | select(.version == \"${MOD_VERSION}\")" |
| 40 | +# store the return code before running anything else |
| 41 | +STATUS_CODE=$? |
| 42 | + |
| 43 | +if [[ $STATUS_CODE -ne 4 ]]; then |
| 44 | + echo "Release already exists, skipping" |
| 45 | + exit 0 |
| 46 | +fi |
| 47 | +echo "Release doesn't exist for ${MOD_VERSION}, uploading" |
| 48 | + |
| 49 | + |
| 50 | +# Load the upload form, getting an upload token |
| 51 | +UPLOAD_TOKEN=$(curl -b cookiejar.txt -c cookiejar.txt -s "https://mods.factorio.com/mod/${MOD_NAME}/downloads/edit" | grep token | sed -r -e "s/.*token: '(.*)'.*/\1/") |
| 52 | +if [[ -z "${UPLOAD_TOKEN}" ]]; then |
| 53 | + echo "Couldn't get an upload token, failed" |
| 54 | + exit 1 |
| 55 | +fi |
| 56 | + |
| 57 | +# Upload the file, getting back a response with details to send in the final form submission to complete the upload |
| 58 | +UPLOAD_RESULT=$(curl -b cookiejar.txt -c cookiejar.txt -s -F "file=@${MOD_NAME}_${MOD_VERSION}.zip;type=application/x-zip-compressed" "https://direct.mods-data.factorio.com/upload/mod/${UPLOAD_TOKEN}") |
| 59 | + |
| 60 | +# Parse 'em and stat the file for the form fields |
| 61 | +CHANGELOG=$(echo "${UPLOAD_RESULT}" | jq -r '@uri "\(.changelog)"') |
| 62 | +INFO=$(echo "${UPLOAD_RESULT}" | jq -r '@uri "\(.info)"') |
| 63 | +FILENAME=$(echo "${UPLOAD_RESULT}" | jq -r '.filename') |
| 64 | +THUMBNAIL=$(echo "${UPLOAD_RESULT}" | jq -r '.thumbnail // empty') |
| 65 | + |
| 66 | +if [[ "${FILENAME}" == "null" ]] || [[ -z "${FILENAME}" ]]; then |
| 67 | + echo "Upload failed" |
| 68 | + exit 1 |
| 69 | +fi |
| 70 | +echo "Uploaded ${MOD_NAME}_${MOD_VERSION}.zip to ${FILENAME}, submitting as new version" |
| 71 | + |
| 72 | +# Post the form, completing the release |
| 73 | +curl -b cookiejar.txt -c cookiejar.txt -s -X POST -d "file=&info_json=${INFO}&changelog=${CHANGELOG}&filename=${FILENAME}&file_size=${FILESIZE}&thumbnail=${THUMBNAIL}" -H "Content-Type: application/x-www-form-urlencoded" -o /dev/null "https://mods.factorio.com/mod/${MOD_NAME}/downloads/edit" |
| 74 | +# TODO if that had a failure exit code then report failure, exit 1 |
| 75 | + |
| 76 | +echo "Completed" |
0 commit comments