Created
April 19, 2019 05:23
-
-
Save beeradmoore/1f0cf78e8b8dec624d65276bb9915be8 to your computer and use it in GitHub Desktop.
Downloads the latest version of Minecraft jars.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
echo "Downloading manifest.\n"; | |
$manifest = json_decode(file_get_contents('https://launchermeta.mojang.com/mc/game/version_manifest.json'), true); | |
$error = json_last_error(); | |
if ($error != JSON_ERROR_NONE) | |
{ | |
echo "ERROR: Could not parse manifest json, $error\n"; | |
exit; | |
} | |
$latest = $manifest['latest']['release']; | |
$download_dir = 'Versions'; | |
$client_filename = 'minecraft_client.'.$latest.'.jar'; | |
$server_filename = 'minecraft_server.'.$latest.'.jar'; | |
$client_out_file = $download_dir.'/'.$client_filename; | |
$server_out_file = $download_dir.'/'.$server_filename; | |
if (file_exists($download_dir) == false) | |
{ | |
mkdir($download_dir); | |
} | |
$latest_version = null; | |
foreach ($manifest['versions'] as $version) | |
{ | |
if ($version['id'] == $latest) | |
{ | |
$latest_version = $version; | |
break; | |
} | |
} | |
if ($latest_version == null) | |
{ | |
echo "ERROR: Unable to find latest version.\n"; | |
exit; | |
} | |
echo "Downloading version manifest.\n"; | |
$version_manifest = json_decode(file_get_contents($latest_version['url']), true); | |
if ($error != JSON_ERROR_NONE) | |
{ | |
echo "ERROR: Could not parse version manifest json, $error\n"; | |
exit; | |
} | |
$client_sha1 = $version_manifest['downloads']['client']['sha1']; | |
$server_sha1 = $version_manifest['downloads']['server']['sha1']; | |
$download_client = true; | |
$download_server = true; | |
if (file_exists($client_out_file)) | |
{ | |
if (sha1_file($client_out_file) == $client_sha1) | |
{ | |
$download_client = false; | |
echo "Client exists, skipping download.\n"; | |
} | |
else | |
{ | |
echo "Client exists but it doesn't match SHA1 hash, redownloading.\n"; | |
unlink($client_out_file); | |
} | |
} | |
if (file_exists($server_out_file)) | |
{ | |
if (sha1_file($server_out_file) == $server_sha1) | |
{ | |
$download_server = false; | |
echo "Server exists, skipping download.\n"; | |
} | |
else | |
{ | |
echo "Server exists but it doesn't match SHA1 hash, redownloading.\n"; | |
unlink($server_out_file); | |
} | |
} | |
if ($download_client == false && $download_server == false) | |
{ | |
echo "Nothing else to do.\n"; | |
exit; | |
} | |
$client_url = $version_manifest['downloads']['client']['url']; | |
$server_url = $version_manifest['downloads']['server']['url']; | |
if ($download_client) | |
{ | |
downloadFile($client_url, $client_out_file); | |
} | |
if ($download_server) | |
{ | |
downloadFile($server_url, $server_out_file); | |
} | |
function downloadFile($url, $save_as) | |
{ | |
echo "Downloading: $url\n"; | |
echo "To: $save_as\n"; | |
$fp = fopen($save_as, 'w+'); | |
if ($fp === false) | |
{ | |
echo "ERROR: Could not open $save_as\n"; | |
return; | |
} | |
$ch = curl_init($url); | |
curl_setopt($ch, CURLOPT_FILE, $fp); | |
curl_setopt($ch, CURLOPT_TIMEOUT, 300); | |
curl_exec($ch); | |
if (curl_errno($ch)) | |
{ | |
fclose($fp); | |
echo "ERROR: ".curl_error($ch)."\n"; | |
curl_close($ch); | |
return; | |
} | |
$status_code = curl_getinfo($ch, CURLINFO_HTTP_CODE); | |
curl_close($ch); | |
fclose($fp); | |
if ($status_code == 200) | |
{ | |
echo "Success\n"; | |
} | |
else | |
{ | |
echo "ERROR: Status code: $status_code\n"; | |
return; | |
} | |
} | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment