Skip to content

Instantly share code, notes, and snippets.

@a-gu
Last active October 22, 2022 20:14
Show Gist options
  • Save a-gu/42d3336bf5eac7da48f8315538b786c2 to your computer and use it in GitHub Desktop.
Save a-gu/42d3336bf5eac7da48f8315538b786c2 to your computer and use it in GitHub Desktop.
Downloads the latest VanillaMC jarfile (requires jq)
#!/usr/bin/env bash
# -h or --help CLI helptext
if [[ "$1" =~ ^-{1,2}h(elp)?$ ]] ; then
echo "Usage:"
echo " ./latest_vanilla.sh [branch] [asset] [output]"
echo
echo "Positional Arguments:"
echo " [branch] is \"release\" or \"snapshot\""
echo " [asset] is \"client\", \"client_mappings\", \"server\", or \"server_mappings\""
echo " [output] is a file name"
exit
fi
# entrypoint URL
VERSION_MANIFEST="https://launchermeta.mojang.com/mc/game/version_manifest.json"
# naming CLI inputs
branch=$1
: ${branch:=release}
if ! [[ "$branch" =~ ^(release|snapshot)$ ]] ; then
echo "Unrecognized branch \"$branch\". Expected \"release\" or \"snapshot\"."
exit 1
fi
asset=$2
: ${asset:=server}
if ! [[ "$asset" =~ ^(client|client_mappings|server|server_mappings)$ ]] ; then
echo "Unrecognized asset \"$asset\". Expected \"client\", \"client_mappings\", \"server\", or \"server_mappings\"."
exit 1
fi
output=$3
# base version manifest JSON
json_versions=$(curl -s "$VERSION_MANIFEST")
latest_version=$(echo $json_versions | jq -r ".latest.$branch")
latest_url=$(echo $json_versions | jq -r ".versions | map(select(.id = "\"$latest_version\"")) | .[0].url")
# version-specific JSON
json_branch=$(curl -s "$latest_url")
latest_asset=$(echo "$json_branch" | jq -r ".downloads.$asset.url")
latest_hash=$(echo "$json_branch" | jq -r ".downloads.$asset.sha1")
latest_filename=$(echo "$latest_asset" | sed -r 's/^.*\/([^\/]+)$/\1/')
: ${output:=$latest_filename}
# download output file
if [ -f "$output" ] ; then
echo "Checking existing file \"$output\"."
existing_hash=$(sha1sum "$output" | sed -r 's/^([0-9a-f]{40})\b.*$/\1/')
if [ "$latest_hash" == "$existing_hash" ] ; then
echo "Existing file hash matches, skipping download."
exit
fi
fi
echo "Downloading latest $asset on the $branch branch to \"$output\"."
curl -s --output "$output" "$latest_asset"
# verify output file
output_hash=$(sha1sum "$output" | sed -r 's/^([0-9a-f]{40})\b.*$/\1/')
if [ "$latest_hash" != "$output_hash" ] ; then
echo "Downloaded file hash $output_hash did not match expected hash $latest_hash."
exit 1
else
echo "Downloaded file hash $output_hash was verified correctly."
fi
@ProjectInitiative
Copy link

Very well documented and useful script. Will definitely try to incorporate in my project! Nice job!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment