From 85a1beee121d76e26015138bcf66942e198d3602 Mon Sep 17 00:00:00 2001 From: Marcel Naeve Date: Wed, 20 Nov 2024 04:21:28 +0100 Subject: [PATCH] bugfixes and implementation of naming the file correctly after download. --- factorio_update.php | 110 ++++++++++++++++++++++++++++++---- scripts/func.downloadFile.php | 18 +++--- 2 files changed, 106 insertions(+), 22 deletions(-) diff --git a/factorio_update.php b/factorio_update.php index 59c2771..8f8b663 100644 --- a/factorio_update.php +++ b/factorio_update.php @@ -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,8 +147,39 @@ if($latest_version->isNewer($current_version)) { // neue Version verfügbar? if(\NAE\Functions\Curl\downloadFile($download_output, $download_source)) { // Download der neuen Server Files - $cli->sendSuccess( "DOWNLOAD SUCCESS!" ); - $download_success = true; + $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 { @@ -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,19 +288,37 @@ 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)) { - $cli->sendTitle( "Kopierenvorgang Nr. ".($copy_id+1)." von ".count($copy_paths)." - $copy_name", 2 ); - if(copy($copy_source, $copy_dest)) { + if(is_dir($copy_source)) { - $cli->sendSuccess( "Kopiervorgang Erfolgreich!" ); + $cli->sendTitle( "Kopierenvorgang Nr. ".($copy_id+1)." von ".count($copy_paths)." - $copy_name", 2 ); + try { + + rcopy($copy_source, $copy_dest); + $cli->sendSuccess( "Kopiervorgang Erfolgreich!" ); + + } catch (\Exception $e) { + + $cli->sendError( "Kopiervorgang Fehlgeschlagen!" ); + + } } else { - $cli->sendError( "Kopiervorgang Fehlgeschlagen!" ); + $cli->sendTitle( "Kopierenvorgang Nr. ".($copy_id+1)." von ".count($copy_paths)." - $copy_name", 2 ); + if(@copy($copy_source, $copy_dest)) { + + $cli->sendSuccess( "Kopiervorgang Erfolgreich!" ); + + } else { + + $cli->sendError( "Kopiervorgang Fehlgeschlagen!" ); + + } } diff --git a/scripts/func.downloadFile.php b/scripts/func.downloadFile.php index 1249a55..0fff4a0 100644 --- a/scripts/func.downloadFile.php +++ b/scripts/func.downloadFile.php @@ -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