Skip to content

Instantly share code, notes, and snippets.

@RomainL972
Created February 27, 2019 21:15
Show Gist options
  • Save RomainL972/0c60bda054378d28cda29c5df3f6a64d to your computer and use it in GitHub Desktop.
Save RomainL972/0c60bda054378d28cda29c5df3f6a64d to your computer and use it in GitHub Desktop.
<?php
if($argc < 2)
die("Give modpack url as argument");
$url = $argv[1] . "/files/latest";
$modpack_headers = get_headers($url, 1);
if(!in_array("HTTP/1.1 200 OK", $modpack_headers))
die("URL isn't valid\n");
echo "Downloading modpack\n";
$modpack = download(end($modpack_headers["Location"]))[0];
echo "\nExtracting modpack\n";
$tmp = sys_get_temp_dir();
$filename = tempnam($tmp, 'modpack');
file_put_contents($filename, $modpack);
$extract = $filename . ".dir";
// echo $extract;
if(!mkdir($extract, 0700))
die("Couldn't create directory\n");
$zip = new ZipArchive;
$res = $zip->open($filename);
if ($res === TRUE) {
$zip->extractTo($extract);
$zip->close();
} else {
die("Couldn't open zipfile\n");
}
// $extract = "/tmp/modpack8SD0u1.dir";
echo "Reading manifest\n";
$manifest = json_decode(file_get_contents($extract . "/manifest.json"), true);
if($manifest["manifestType"] != "minecraftModpack" || $manifest["manifestVersion"] != 1)
die("Unsupported manifest\n");
$overrides = $extract . "/" . $manifest["overrides"];
$total = count($manifest["files"]);
foreach ($manifest["files"] as $number => $file) {
if(!$file["required"]){
echo "File " . $number+1 . "/" . $total . " not required\n";
continue;
}
echo "Downloading mod ";
echo $number+1 . "/" . $total . "\n";
$jar = download("https://minecraft.curseforge.com/projects/" .
$file["projectID"] . "/files/" . $file["fileID"] . "/download");
file_put_contents($overrides . "/mods/" . $jar[1], $jar[0]);
echo "\nSuccessfully downloaded " . $jar[1] . "\n\n";
}
echo "Finished! go check " . $extract . "\n";
unlink($filename);
// rmdir($extract);
function download($url) {
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $url);
//curl_setopt($ch, CURLOPT_BUFFERSIZE,128);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_PROGRESSFUNCTION, 'progress');
curl_setopt($ch, CURLOPT_NOPROGRESS, false); // needed to make progress function work
curl_setopt($ch, CURLOPT_HEADER, 0);
curl_setopt($ch, CURLOPT_FOLLOWLOCATION, true);
$file = curl_exec($ch);
$last_url = explode('/',curl_getinfo($ch, CURLINFO_EFFECTIVE_URL));
curl_close($ch);
return [$file, end($last_url)];
}
function progress($resource,$download_size, $downloaded, $upload_size, $uploaded)
{
// static $previous_time = 0;
if ( !$download_size == 0) {
$progress = round( $downloaded * 100 / $download_size );
echo "\033[5D"; // Move 5 characters backward
echo str_pad($progress, 3, ' ', STR_PAD_LEFT) . " %";
$previous_time = time();
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment