summaryrefslogtreecommitdiff
path: root/get_version.sh
blob: cae7101f41b3369dbb3156e126f6c1d8b47b79d4 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
#!/usr/bin/env sh

# Only accepts 1 argument: the Minecraft version to download
LOOOKING_FOR=
if [ -z "${1}" ]; then
	echo "usage: ${0} <version>"
	exit 1
else
	LOOKING_FOR="${1}"
fi

PLATFORM=$(uname -s | tr '[:upper:]' '[:lower:]')

MANIFEST_LIST_URL='https://launchermeta.mojang.com/mc/game/version_manifest.json'

mkdir -p assets/indexes

echo 'Retrieving version manifest...'
VERSION_MANIFEST_URL=$(curl "${MANIFEST_LIST_URL}" 2>/dev/null | jq -r "(.versions.[] | select(.id==\"${LOOKING_FOR}\")).url")

if [ -z "${VERSION_MANIFEST_URL}" ]; then
	echo "ERROR: Unable to find requested version"
	exit 1
fi

mkdir -p manifests
VERSION_MANIFEST=$(curl ${VERSION_MANIFEST_URL} 2>/dev/null | sed -e 's/\\/\\\\/g' | tee "manifests/${LOOKING_FOR}.json") # Some versions contain illegal escape sequences which will crash some shells such as Ksh; double-escape it just to be safe

# 1.12 and earlier, natives and regular libraries are contained in different elements. For 1.13 and later, an element may contain both.
echo 'Downloading libraries...'
for I in $(echo "${VERSION_MANIFEST}" | jq -c .libraries.[]); do
	if [[ $(echo "${I}" | jq .downloads.artifact) != 'null' ]]; then
		echo "Library '$(echo ${I} | jq -r .name )' found - downloading..."
		mkdir -p libraries/$(dirname $(echo ${I} | jq -r .downloads.artifact.path))
		curl $(echo ${I} | jq -r .downloads.artifact.url) > libraries/$(echo "${I}" | jq -r .downloads.artifact.path) 2>/dev/null
	fi
	if [[ $(echo "${I}" | jq .natives) != 'null' ]]; then
		echo "Downloading native for platform '${PLATFORM}'..."
		if [[ $(echo ${I} | jq -r ".downloads.classifiers.\"natives-${PLATFORM}\"") == 'null' ]]; then
			echo "WARNING: Mojang does not distribute a '$(echo ${I} | jq -r .name)' native for this platform. This is probably not an issue unless you are using an obscure platform."
		else
			mkdir -p libraries/$(dirname $(echo ${I} | jq -r ".downloads.classifiers.\"natives-${PLATFORM}\".path"))
			curl $(echo ${I} | jq -r ".downloads.classifiers.\"natives-${PLATFORM}\".url") > libraries/$(echo "${I}" | jq -r ".downloads.classifiers.\"natives-${PLATFORM}\".path") 2>/dev/null
		fi
	fi
done

echo 'Retrieving asset index...'
ASSET_INDEX=$(echo "${VERSION_MANIFEST}" | jq .assetIndex)

echo 'Retrieving object list...'
OBJECT_LIST=$(curl $(echo ${ASSET_INDEX} | jq -r .url) 2>/dev/null | tee assets/indexes/$(echo ${ASSET_INDEX} | jq -r .id).json)

HASH_COUNT=$(echo ${OBJECT_LIST} | jq '.objects | length')

echo 'Downloading required assets. This may take a few minutes.'
MCRESOURCES='https://resources.download.minecraft.net/'
if [[ "$(echo "${ASSET_INDEX}" | jq -r .id)" == 'pre-1.6' || "$(echo "${ASSET_INDEX}" | jq -r .id)" == 'legacy' ]]; then # Release 1.6 and prior use a different asset layout
	while read -r LINE; do
		H=$(echo ${OBJECT_LIST} | jq -r ".objects.[\"${LINE}\"].hash")
 		DIR="${H[0]}${H[1]}/"

		mkdir -p "$(dirname "assets/virtual/legacy/${LINE}")"
		curl "${MCRESOURCES}${DIR}${H}" > "assets/virtual/legacy/${LINE}" 2>/dev/null
		echo "Downloaded: assets/virtual/legacy/${LINE}"
	done <<< "$(echo "${OBJECT_LIST}" | jq -r '.objects | keys[]')"
else
	IND=0
	for I in $(echo "${OBJECT_LIST}" | jq -r .objects.[].hash); do
		IND=$(echo "${IND}" + 1 | nickle)
		DIR="$(echo "${I}" | cut -c1-2)/"

		mkdir -p "assets/objects/${DIR}"

		# If file exists, check its integrity with hash and skip if it's good
		if [[ -f assets/objects/${DIR}${I} && $(cat "assets/objects/${DIR}${I}" | sha1sum | sed -e 's/  (stdin)//') == "${I}" ]]; then # Removing (stdin) is not needed for most sha1sum implementations
				echo "${IND}/${HASH_COUNT} (${DIR}${I}) is already downloaded; skipping. ($(echo "printf(\"%5l\", ${IND} / ${HASH_COUNT} * 100)" | nickle)%)"
			continue;
		fi

		curl "${MCRESOURCES}${DIR}${I}" > "assets/objects/${DIR}${I}" 2>/dev/null
		echo "Downloading ${IND}/${HASH_COUNT}: ${MCRESOURCES}${DIR}${I} ($(echo "printf(\"%5l\", ${IND} / ${HASH_COUNT} * 100)" | nickle)%)"
	done
fi

echo 'Downloading minecraft jar...'
CLIENT_URL=$(echo "${VERSION_MANIFEST}" | jq -r .downloads.client.url)
mkdir -p "libraries/com/mojang/minecraft/${LOOKING_FOR}"
curl "${CLIENT_URL}" > "libraries/com/mojang/minecraft/${LOOKING_FOR}/minecraft-${LOOKING_FOR}-client.jar" 2>/dev/null

echo 'Finished!'