public static ReleaseMetadataFile UpdateRelease(LauncherRemoteConfig config, string bin_pathname, string metadata_pathname) { //We're going to download the update now Console.Title = $"Delta Web Map Server - Updating..."; string platform = GetReleaseBinaryName(); using (MemoryStream ms = new MemoryStream()) { //Download our binary Program.WriteLineColor($"Downloading update {config.latest_release.version_major}.{config.latest_release.version_minor}...", ConsoleColor.DarkCyan); try { using (HttpClient hc = new HttpClient()) using (Stream ds = hc.GetStreamAsync(config.latest_release.binaries[platform].download_url).GetAwaiter().GetResult()) ds.CopyTo(ms); } catch (Exception ex) { //Failed Program.WriteLineColor("Sorry, could not download the update. Try again later.", ConsoleColor.Red); throw new Exception(); } //Open the ZIP file Program.WriteLineColor($"Extracting update {config.latest_release.version_major}.{config.latest_release.version_minor}...", ConsoleColor.DarkCyan); try { using (ZipArchive za = new ZipArchive(ms, ZipArchiveMode.Read, true)) { //Delete the existing binaries Directory.Delete(bin_pathname, true); //Create a new directory and extract Directory.CreateDirectory(bin_pathname); za.ExtractToDirectory(bin_pathname); } } catch (Exception ex) { //Failed Program.WriteLineColor("Sorry, could not extract update. Try again later.", ConsoleColor.Red); throw new Exception(); } } //Now, we'll create the metadata and save it ReleaseMetadataFile metadata = new ReleaseMetadataFile { release_channel = Program.LAUNCHER_CHANNEL, time = DateTime.UtcNow, version_major = config.latest_release.version_major, version_minor = config.latest_release.version_minor, app_exec = config.latest_release.binaries[platform].exec_name }; File.WriteAllText(metadata_pathname, JsonConvert.SerializeObject(metadata)); Program.WriteLineColor($"Updated Delta Web Map to version {config.latest_release.version_major}.{config.latest_release.version_minor}!", ConsoleColor.DarkCyan); return(metadata); }
static void Main(string[] args) { //Write launcher info WriteLineColor("Delta Web Map (C) RomanPort 2019", ConsoleColor.Cyan); WriteLineColor("Launcher Version " + LAUNCHER_VERSION.ToString("N1"), ConsoleColor.DarkCyan); Console.Title = $"Delta Web Map Server - Boot"; //Download launcher remote config file LauncherRemoteConfig config = DownloadConfigFile(); //Get our pathnames and create them if they don't exist string root = GetRootPathname(); string content = CombinePath(GetRootPathname(), "content", true); string saved = CombinePath(content, "saved", true); string app_saved = CombinePath(saved, "app", true); string app_bin = CombinePath(content, "bin", true); string release_metadata_path = CombinePath(saved, "release_metadata.json", false); MakePathIfNotExist(content); MakePathIfNotExist(saved); MakePathIfNotExist(app_saved); MakePathIfNotExist(app_bin); //Try and open the release data file ReleaseMetadataFile current_release = null; if (File.Exists(release_metadata_path)) { current_release = JsonConvert.DeserializeObject <ReleaseMetadataFile>(File.ReadAllText(release_metadata_path)); } //Install the app if we need to if (current_release == null) { current_release = ReleaseUpdater.UpdateRelease(config, app_bin, release_metadata_path); } //Update the app if we need to if (current_release.version_major < config.latest_release.version_major || current_release.version_minor < config.latest_release.version_minor) { current_release = ReleaseUpdater.UpdateRelease(config, app_bin, release_metadata_path); } //Write version WriteLineColor($"Client Version {current_release.version_major}.{current_release.version_minor}", ConsoleColor.DarkCyan); //Start the server server = StartServer(current_release, app_bin, app_saved); //Go into an updater loop while (true) { //Wait for the amount of time our policy states Thread.Sleep(config.launcher_config_sync_policy); //Redownload the config file try { config = DownloadConfigFile(); } catch { } //Update the app if we need to if (current_release.version_major < config.latest_release.version_major || current_release.version_minor < config.latest_release.version_minor) { //We must update. Shut down the service WriteLineColor($"There is an update. Upgrading from version {current_release.version_major}.{current_release.version_minor} to {config.latest_release.version_major}.{config.latest_release.version_minor}... Service will temporarily be interrupted.", ConsoleColor.Yellow); server.Kill(); //Now, update current_release = ReleaseUpdater.UpdateRelease(config, app_bin, release_metadata_path); //Restart the server server = StartServer(current_release, app_bin, app_saved); } //Check if the service crashed if (server.HasExited) { WriteLineColor($"The service crashed. Going to restart...", ConsoleColor.Yellow); server = StartServer(current_release, app_bin, app_saved); } } }