bugfixes and implementation of naming the file correctly after download.

This commit is contained in:
Marcel Naeve 2024-11-20 04:21:28 +01:00
parent 20cd7d51c6
commit 85a1beee12
Signed by: manae
GPG Key ID: CBB7B21A2EBDC6A9
2 changed files with 106 additions and 22 deletions

View File

@ -19,16 +19,19 @@ $target_edition = "headless";
// Wo wird der Server dieses Builds heruntergeladen?
$download_source = "https://factorio.com/get-download/stable/headless/linux64";
// sha256 hash Source
$download_hash_source = "https://www.factorio.com/download/sha256sums/";
// In welcher Datei soll der Download gespeichert werden?
// Wichtig da Factorio eine .tar.gz datei als linux64 Datei anbietet.
$download_output = __DIR__."/factorio.tar";
// Welche Dateien oder Ordner sollen vom alten Server übernommen werden?
$copy_paths = [
"server-adminlist.json",
"player-data.json",
"config",
"config-path.cfg"
"/server-adminlist.json",
"/player-data.json",
"/config",
"/config-path.cfg"
];
@ -80,6 +83,38 @@ function writeBackVersion() : bool {
// removes files and non-empty directories
function rrmdir($dir) {
if (is_dir($dir)) {
$files = scandir($dir);
foreach ($files as $file)
if ($file != "." && $file != "..") rrmdir("$dir/$file");
rmdir($dir);
}
else if (file_exists($dir)) unlink($dir);
}
// copies files and non-empty directories
function rcopy($src, $dst) {
if (file_exists($dst)) rrmdir($dst);
if (is_dir($src)) {
mkdir($dst);
$files = scandir($src);
foreach ($files as $file)
if ($file != "." && $file != "..") rcopy("$src/$file", "$dst/$file");
}
else if (file_exists($src)) copy($src, $dst);
}
// Variablen zum speichern des Fortschrittes bzw. Erfolgs.
$download_success = false;
$extract_success = false;
@ -112,9 +147,40 @@ if($latest_version->isNewer($current_version)) { // neue Version verfügbar?
if(\NAE\Functions\Curl\downloadFile($download_output, $download_source)) { // Download der neuen Server Files
$dl_real_out = $download_output;
$sha256file = file_get_contents($download_hash_source); // sha256 hash liste von factorio
$fileHash = hash_file("sha256", $download_output);
$cli->sendInfo("Umbenennen des Heruntergeladenen Archivs..");
$realFilename = (function(array $lst, string $hash) : ?string {
$hashLen = strlen($hash);
foreach($lst as $file) {
if(substr($file, 0, $hashLen) == $hash && substr($file, $hashLen, 1) == " ") {
return trim(substr($file, $hashLen+1));
}
}
return null;
})(explode("\n", $sha256file), $fileHash);
if($realFilename === null) {
$cli->sendError( "FEHLGESCHLAGEN: Kann den richtigen Dateinamen aus der sha256 hash liste nicht ermitteln!", true );
} else {
$orig_download_output = $download_output;
$download_output = __DIR__."/$realFilename";
if(@rename( $orig_download_output, $download_output )) {
$cli->sendSuccess( "DOWNLOAD SUCCESS!" );
$download_success = true;
}
}
} else {
$cli->sendError( "DOWNLOAD FEHLGESCHLAGEN!", true );
@ -131,7 +197,7 @@ if($latest_version->isNewer($current_version)) { // neue Version verfügbar?
if($return === 0) {
if( rename(__DIR__."/factorio", __DIR__."/factorio-".$latest_version->getVersion()) ) { // Umbenennen mit Versionsnummer
if( @rename(__DIR__."/factorio", __DIR__."/factorio-".$latest_version->getVersion()) ) { // Umbenennen mit Versionsnummer
$cli->sendSuccess( "Neue Server Files wurden Erfolgreich entpackt!" );
$extract_success = true;
@ -222,13 +288,29 @@ if($latest_version->isNewer($current_version)) { // neue Version verfügbar?
foreach($copy_paths as $copy_id => $copy_name) {
$copy_source = __DIR__."factorio-/".$current_version->getVersion().$copy_name;
$copy_dest = __DIR__."factorio-/".$latest_version->getVersion().$copy_name;
$copy_source = __DIR__."/factorio-".$current_version->getVersion().$copy_name;
$copy_dest = __DIR__."/factorio-".$latest_version->getVersion().$copy_name;
if(file_exists($copy_source)) {
if(is_dir($copy_source)) {
$cli->sendTitle( "Kopierenvorgang Nr. ".($copy_id+1)." von ".count($copy_paths)." - $copy_name", 2 );
if(copy($copy_source, $copy_dest)) {
try {
rcopy($copy_source, $copy_dest);
$cli->sendSuccess( "Kopiervorgang Erfolgreich!" );
} catch (\Exception $e) {
$cli->sendError( "Kopiervorgang Fehlgeschlagen!" );
}
} else {
$cli->sendTitle( "Kopierenvorgang Nr. ".($copy_id+1)." von ".count($copy_paths)." - $copy_name", 2 );
if(@copy($copy_source, $copy_dest)) {
$cli->sendSuccess( "Kopiervorgang Erfolgreich!" );
@ -238,6 +320,8 @@ if($latest_version->isNewer($current_version)) { // neue Version verfügbar?
}
}
} else {
$cli->sendWarning( "Quelle nicht vorhanden!" );

View File

@ -14,19 +14,19 @@ namespace NAE\Functions\Curl;
*/
function downloadFile(string $fileDestination, string $fileSource) : bool {
$ch = curl_init($fileSource);
$ch = \curl_init($fileSource);
$fp = fopen($fileDestination, 'wb');
$fp = \fopen($fileDestination, 'wb');
curl_setopt($ch, CURLOPT_FILE, $fp);
curl_setopt($ch, CURLOPT_HEADER, 0);
curl_setopt($ch, CURLOPT_FOLLOWLOCATION, true);
\curl_setopt($ch, CURLOPT_FILE, $fp);
\curl_setopt($ch, CURLOPT_HEADER, 0);
\curl_setopt($ch, CURLOPT_FOLLOWLOCATION, true);
curl_exec($ch);
$info = curl_getinfo($ch);
\curl_exec($ch);
$info = \curl_getinfo($ch);
curl_close($ch);
fclose($fp);
\curl_close($ch);
\fclose($fp);
return ( $info["download_content_length"]==$info["size_download"] ); // only works when the file size is known upfront from headers