sendInfo( "CURRENT_VERSION: ".$current_version->getVersion() ); // Aktuelle Versionsnummern aus der Factorio-API abrufen. $live_versions = json_decode(file_get_contents("https://factorio.com/api/latest-releases"), true); // Anhand von Build und Edition die richtige Version auswählen. $latest_version = new \NAE\Factorio\FactorioVersion($live_versions[$target_build][$target_edition]); $cli->sendInfo( "LATEST_VERSION: ".$latest_version->getVersion() ); echo PHP_EOL; /** * Schreibt die neue Versionsnummer in die environment (env) datei des servers * @return bool Schreiben erfolgreich? */ function writeBackVersion() : bool { global $latest_version; $env_file = __DIR__."/factorio-settings.env"; $env = file_get_contents($env_file); $new_env = preg_replace("/FACTORIO_VERSION\=\"((\d+\.)+\d+)\"/su", "FACTORIO_VERSION=\"".$latest_version->getVersion()."\"", $env); return ( file_put_contents($env_file, $new_env) !== false ); } // Variablen zum speichern des Fortschrittes bzw. Erfolgs. $download_success = false; $extract_success = false; $cli->sendTitle( "PRÜFE AUF FACTORIO UPDATES" ); if($latest_version->isNewer($current_version)) { // neue Version verfügbar? $cli->sendTitle( "NEUE VERSION GEFUNDEN : UPDATE STARTET" ); if(file_exists($download_output)) { // liegt noch ein altes Server Archiv im Vertzeichnis? $cli->sendTitle( "ALTE SERVER FILES GEFUNDEN : STARTE LÖSCHVORGANG", 1 ); if( unlink($download_output) ) { // altes Archiv löschen $cli->sendSuccess( "ALTE SERVER FILES ERFOLGREICH GELÖSCHT!" ); } else { $cli->sendError( "ALTE SERVER FILES KONNTEN NICHT GELÖSCHT WERDEN!", true ); } } echo PHP_EOL; $cli->sendTitle( "STARTE DOWNLOAD DER SERVER FILES", 1 ); 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 ); } if($download_success) { echo PHP_EOL; $cli->sendTitle( "STARTE DAS ENTPACKEN DER NEUEN SERVER FILES", 1 ); $unxz_download_output = preg_replace("/.tar.xz$/", ".tar", $download_output); exec("unxz -vfd \"$download_output\" && tar -xvf \"$unxz_download_output\" > /dev/null", $output, $return); // Entpacken if($return === 0) { if( @rename(__DIR__."/factorio", __DIR__."/factorio-".$latest_version->getVersion()) ) { // Umbenennen mit Versionsnummer $cli->sendSuccess( "Neue Server Files wurden Erfolgreich entpackt!" ); $extract_success = true; } else { $cli->sendError( "Umbenennen des Factorio Ordners mit Versionsnummer Fehlgeschlagen!", true ); } } else { $cli->sendError( "Entpacken der Server Files ist Fehlgeschlagen!", true ); } } if($extract_success) { // RCON Kommunikation zum Server starten $rcon_data = (function() : ?array { global $current_version; $cfg = parse_ini_file(__DIR__."/factorio-".$current_version->getVersion()."/config/config.ini", true); if(!$cfg) return null; $cd = []; if(isset($cfg["other"])) { $parts = explode(":", $cfg["other"]["local-rcon-socket"]??"127.0.0.1:27015"); $cd["ip"] = $parts[0]; $cd["port"] = $parts[1]; if(isset($cfg["other"]["local-rcon-socket"])) { $cd["password"] = $cfg["other"]["local-rcon-password"]??""; } } return $cd; })(); $rcon = null; if($rcon_data) { try { $rcon = new \NAE\Valve\Rcon\ValveRcon($rcon_data["ip"], $rcon_data["port"], $rcon_data["password"]); $rcon->sendCommand("ACHTUNG: Server wird in kürze für ein Update gestoppt, bitte trennen Sie Ihre Verbindung zum Server. Der Server wird in kürze wieder verfügbar sein. Zeit bis zum Stopp: 60 Sekunden.", false); if($rcon->getError() !== null) { $cli->sendWarning( "RCON Verbindung konnte nicht aufgebaut werden!" ); $rcon = null; } } catch(\Exception $e) { $cli->sendWarning( "RCON Verbindung konnte nicht aufgebaut werden!" ); $rcon = null; } } if($rcon) sleep(60); // Falls die Meldung geklappt hat 60s warten, damit Spieler den Server verlassen können. // inititialize Systemctl Service interface $service = new \NAE\Service\Systemctl("factorio"); // Server Stoppen if($service->stop()) { $cli->sendInfo( "FACTORIO SERVER WURDE GESTOPPT!" ); } // neue Versionsnummer in .env Datei übernehmen if(writeBackVersion()) { $cli->sendInfo( "Version in Environment File wurde aktualisiert" ); } // Dateien aus alten Server Files kopieren.. if( is_dir(__DIR__."/factorio-".$current_version->getVersion()) && is_dir(__DIR__."/factorio-".$latest_version->getVersion()) ) { echo PHP_EOL; $cli->sendTitle( "Kopiere Konfigurations-Dateien in den neuen Server Pfad", 1 ); 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; if(file_exists($copy_source)) { if(is_dir($copy_source)) { $cli->sendTitle( "Kopierenvorgang Nr. ".($copy_id+1)." von ".count($copy_paths)." - $copy_name", 2 ); try { \NAE\Functions\Folder\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!" ); } else { $cli->sendError( "Kopiervorgang Fehlgeschlagen!" ); } } } else { $cli->sendWarning( "Quelle nicht vorhanden!" ); } } } // Fix file permissions.. (function(){ global $latest_version, $cli; $cli->sendTitle( "Rechte im neuen Server-Verzeichnis werden korrigiert", 1 ); exec("chown -R factorio:factorio \"".__DIR__."/factorio-".$latest_version->getVersion()."\"", $output, $return); if($return === 0) { $cli->sendSuccess( "Rechte erfolgreich korrigiert!" ); } else { $cli->sendError( "Rechte konnten nicht korrigiert werden!", true ); } })(); if($service->start()) { $cli->sendSuccess( "FACTORIO SERVER WURDE WIEDER GESTARTET!" ); } else { $cli->sendError( "FACTORIO SERVER KONNTE NICHT WIEDER GESTARTET WERDEN!", true ); } } } else { // Keine aktuellere Version verfügbar! $cli->sendInfo( "Kein Update verfügbar." ); }