Skip to content

Commit

Permalink
fixes
Browse files Browse the repository at this point in the history
  • Loading branch information
Bod9001 committed Dec 13, 2024
1 parent 06df8b7 commit 2f67e99
Showing 1 changed file with 41 additions and 12 deletions.
53 changes: 41 additions & 12 deletions UnitystationLauncher/Services/TTSService.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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))

Check failure on line 126 in UnitystationLauncher/Services/TTSService.cs

View check run for this annotation

Codacy Production / Codacy Static Code Analysis

UnitystationLauncher/Services/TTSService.cs#L126

Refactor this code to not nest more than 3 control flow statements.
{
System.IO.File.Delete(file);
}

foreach (var directory in System.IO.Directory.GetDirectories(LocalVersion))

Check failure on line 131 in UnitystationLauncher/Services/TTSService.cs

View check run for this annotation

Codacy Production / Codacy Static Code Analysis

UnitystationLauncher/Services/TTSService.cs#L131

Refactor this code to not nest more than 3 control flow statements.
{
System.IO.Directory.Delete(directory, true);
}
}


var zip = _environmentService.GetCurrentEnvironment() switch
{
CurrentEnvironment.WindowsStandalone => "win.zip",
Expand All @@ -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);
Expand Down Expand Up @@ -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;
}
Expand All @@ -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)
Expand Down Expand Up @@ -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)
Expand Down Expand Up @@ -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
Expand All @@ -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());
}

}
}
}

0 comments on commit 2f67e99

Please sign in to comment.