From 2f67e9921472001129577337aff25a087e1cedac Mon Sep 17 00:00:00 2001 From: Bod9001 Date: Fri, 13 Dec 2024 19:21:20 +0000 Subject: [PATCH] fixes --- UnitystationLauncher/Services/TTSService.cs | 53 ++++++++++++++++----- 1 file changed, 41 insertions(+), 12 deletions(-) diff --git a/UnitystationLauncher/Services/TTSService.cs b/UnitystationLauncher/Services/TTSService.cs index ac07999..ab4b6f4 100644 --- a/UnitystationLauncher/Services/TTSService.cs +++ b/UnitystationLauncher/Services/TTSService.cs @@ -115,14 +115,25 @@ public async Task CheckAndDownloadLatestVersion(Download Download) { if (localVersionModel == null || localVersionModel.Version != CurrentVersion.Version) { + Download.Active = true; + Download.DownloadState = DownloadState.InProgress; StopTTS(); + await Task.Delay(2 * 1000); //to give it some grace period to shutdown + var LocalVersion = System.IO.Path.Combine(installationBasePath, "tts"); if (System.IO.Directory.Exists(LocalVersion)) { - System.IO.Directory.Delete(LocalVersion, true); + foreach (var file in System.IO.Directory.GetFiles(LocalVersion)) + { + System.IO.File.Delete(file); + } + + foreach (var directory in System.IO.Directory.GetDirectories(LocalVersion)) + { + System.IO.Directory.Delete(directory, true); + } } - var zip = _environmentService.GetCurrentEnvironment() switch { CurrentEnvironment.WindowsStandalone => "win.zip", @@ -131,8 +142,7 @@ public async Task CheckAndDownloadLatestVersion(Download Download) _ => null }; - Download.Active = true; - Download.DownloadState = DownloadState.InProgress; + HttpResponseMessage request = await _httpClient.GetAsync(ApiUrls.TTSFiles + "/" + zip, HttpCompletionOption.ResponseHeadersRead); @@ -167,14 +177,12 @@ private void ExtractTo(Stream progressStream, string LocalVersion, Download Down case CurrentEnvironment.WindowsStandalone: { ZipArchive archive = new(progressStream); - Download.DownloadState = DownloadState.Extracting; archive.ExtractToDirectory(LocalVersion, true); break; } case CurrentEnvironment.LinuxStandalone or CurrentEnvironment.LinuxFlatpak: { using var decompressedStream = DecompressXz(progressStream); // Decompress XZ stream to get .tar - ExtractTar(decompressedStream, LocalVersion); break; } @@ -184,6 +192,7 @@ private void ExtractTo(Stream progressStream, string LocalVersion, Download Down Download.Active = false; Download.DownloadState = DownloadState.InProgress; + StartTTS(); } private static Stream DecompressXz(Stream compressedStream) @@ -251,11 +260,19 @@ public void StartTTS() var Preference = _preferencesService.GetPreferences(); if ((Preference.TTSEnabled is true) == false) return; - if (process != null && process.HasExited == false) + try { - return; + if (process != null && process.HasExited == false) + { + return; + } + } + catch (Exception e) + { + Log.Error(e.ToString()); } + string installationBasePath = _preferencesService.GetPreferences().InstallationPath; var LocalVersion = System.IO.Path.Combine(installationBasePath, "tts"); if (System.IO.Directory.Exists(LocalVersion) == false) @@ -304,10 +321,14 @@ public void StartTTS() // Ensure subprocess ends when the main application exits AppDomain.CurrentDomain.ProcessExit += (sender, e) => { - if (process.HasExited == false) + if (process != null) { - process.Kill(); + if (process.HasExited == false) + { + process.Kill(); + } } + }; try @@ -325,10 +346,18 @@ public void StopTTS() { if (process != null) { - if (process.HasExited == false) + try + { + if (process.HasExited == false) + { + process.Kill(); + } + } + catch (Exception e) { - process.Kill(); + Log.Error(e.ToString()); } + } } } \ No newline at end of file