-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain.sh
More file actions
146 lines (106 loc) · 4.28 KB
/
Copy pathmain.sh
File metadata and controls
146 lines (106 loc) · 4.28 KB
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
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
#!/bin/sh
if [ "$INPUT_VERSION" == "latest-snapshot" ]; then
echo "Fetching the latest snapshot version."
latest_version=$(curl -L $INPUT_MANIFEST_API_URL | jq -r '.latest.snapshot')
if [ -z "$latest_version" ]; then
echo "Error: Could not find the latest snapshot version in the manifest."
exit 1
fi
echo "Using latest snapshot version: $latest_version"
INPUT_VERSION="$latest_version"
elif [ "$INPUT_VERSION" == "latest-release" ]; then
echo "Fetching the latest release version."
latest_version=$(curl -L $INPUT_MANIFEST_API_URL | jq -r '.latest.release')
if [ -z "$latest_version" ]; then
echo "Error: Could not find the latest release version in the manifest."
exit 1
fi
echo "Using latest release version: $latest_version"
INPUT_VERSION="$latest_version"
else
echo "Using specified version: $INPUT_VERSION"
fi
echo "Make temp download directory."
TEMP_DOWNLOAD_DIR=$(mktemp -d)
echo "Fetch package URL from \"$INPUT_MANIFEST_API_URL\"."
package_url=$(curl -L $INPUT_MANIFEST_API_URL | jq -r ".versions[] | select(.id == \"$INPUT_VERSION\") | .url")
echo "Fetch client.jar URL from \"$package_url\"."
jar_url=$(curl -L $package_url | jq -r ".downloads.client.url")
echo "Fetching client.jar SHA1 hash from \"$package_url\"."
client_jar_sha1=$(curl -L $package_url | jq -r ".downloads.client.sha1")
echo "Downloading client.jar from \"$jar_url\"."
curl -L -o $TEMP_DOWNLOAD_DIR/client.jar $jar_url
echo "Verifying SHA1 hash of client.jar."
downloaded_sha1=$(sha1sum "$TEMP_DOWNLOAD_DIR/client.jar" | awk '{print $1}')
if [ "$downloaded_sha1" != "$client_jar_sha1" ]; then
echo "Error: SHA1 hash does not match. Expected $client_jar_sha1, but got $downloaded_sha1."
exit 1
else
echo "SHA1 hash verification passed for client.jar."
fi
echo "Saved \"client.jar\" to \"$TEMP_DOWNLOAD_DIR\"."
echo "::group:: Extract assets from client.jar"
unzip "$TEMP_DOWNLOAD_DIR/client.jar" "pack.png" -d "$INPUT_PATH"
unzip "$TEMP_DOWNLOAD_DIR/client.jar" "assets/*" -d "$INPUT_PATH"
echo "::endgroup::"
echo "Fetch asset index URL from \"$package_url\"."
asset_index_url=$(curl -L $package_url | jq -r ".assetIndex.url")
echo "::group:: Downloading additional assets from \"$asset_index_url\"."
export assets_path="$INPUT_PATH/assets"
echo "Saving additional assets to \"$assets_path\"."
asset_list=$(curl -L $asset_index_url | jq -r '.objects | to_entries[] | "\(.key) \(.value.hash)"')
FAILED_DOWNLOADS_FILE=$(mktemp)
echo "$asset_list" | while read -r path hash; do
echo "$path $hash"
done | xargs -n 2 -P "$INPUT_PARALLEL_DOWNLOADS" -I {} sh -c '
path=$(echo {} | awk "{print \$1}")
hash=$(echo {} | awk "{print \$2}")
first_hex="${hash:0:2}"
url="$INPUT_RESOURCES_API_URL/$first_hex/$hash"
destination="$assets_path/$path"
failed_file="$1"
mkdir -p "$(dirname "$destination")"
retries="$INPUT_DOWNLOAD_RETRIES"
attempt=1
while [ $attempt -le $retries ]; do
if curl -f -s -o "$destination" "$url"; then
echo -e "Saved \"$url\" to \"$destination\"."
exit 0
else
echo -e "\033[33mAttempt $attempt/$retries failed for \"$url\".\033[0m"
attempt=$((attempt + 1))
sleep 1
fi
done
echo -e "\033[31mFailed to download \"$url\" after $retries attempts.\033[0m"
echo "\"$url\"" >> "$failed_file"
' _ "$FAILED_DOWNLOADS_FILE"
echo "::endgroup::"
echo "::group:: Iterate over downloaded files to find and unzip ZIP files."
find "$INPUT_PATH" -type f -name "*.zip" | while read -r zipfile; do
unzip_dir="${zipfile%.zip}_unzipped"
echo "Unzipping \"$zipfile\" to \"$unzip_dir\"."
mkdir -p "$unzip_dir"
if unzip -o "$zipfile" -d "$unzip_dir"; then
echo "Successfully unzipped \"$zipfile\"."
else
echo "Failed to unzip \"$zipfile\"."
fi
echo "Removing \"$zipfile\"."
rm "$zipfile"
original_name="${zipfile%.zip}.zip"
echo "Renaming \"$unzip_dir\" to \"$original_name\"."
mv "$unzip_dir" "$original_name"
done
echo "::endgroup::"
if [ -s "$FAILED_DOWNLOADS_FILE" ]; then
echo "::group:: Some downloads failed."
cat "$FAILED_DOWNLOADS_FILE"
echo "::endgroup::"
failed_urls=$(jq -R -s -c 'split("\n")[:-1]' "$FAILED_DOWNLOADS_FILE")
echo "failed-downloads=$failed_urls" >> "$GITHUB_OUTPUT"
else
echo "failed-downloads=[]" >> "$GITHUB_OUTPUT"
fi
echo "All files downloaded and processed."
exit 0