Skip to content

Commit dd8976b

Browse files
authored
TTS Download (#230)
* TTS Download * White spaces changes * quality * fix * Good files * updatws * formaatting * format
1 parent 17d545c commit dd8976b

File tree

19 files changed

+564
-32
lines changed

19 files changed

+564
-32
lines changed

UnitystationLauncher.Tests/ViewModels/ServersPanelViewModelTests.cs

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
using UnitystationLauncher.Tests.MocksRepository.PingService;
44
using UnitystationLauncher.Tests.MocksRepository.ServerService;
55
using UnitystationLauncher.ViewModels;
6+
using Xunit.Sdk;
67

78
namespace UnitystationLauncher.Tests.ViewModels;
89

@@ -16,7 +17,7 @@ public static void ServersPanelViewModel_ShouldFetchServers()
1617
IPingService mockPingService = new MockPingStaticPingTime(5);
1718
IServerService mockServerService = new MockRandomServers(1, 20);
1819

19-
ServersPanelViewModel serversPanelViewModel = new(mockInstallationService, mockPingService, mockServerService);
20+
ServersPanelViewModel serversPanelViewModel = new(mockInstallationService, mockPingService, mockServerService, null, null);
2021
serversPanelViewModel.ServerViews.Should().NotBeEmpty();
2122
}
2223

@@ -27,7 +28,7 @@ public static void ServersPanelViewModel_ShouldHandleExceptionInServerService()
2728
IPingService mockPingService = new MockPingStaticPingTime(5);
2829
IServerService mockServerService = new MockServersThrowsException();
2930

30-
Func<ServersPanelViewModel> act = () => new(mockInstallationService, mockPingService, mockServerService);
31+
Func<ServersPanelViewModel> act = () => new(mockInstallationService, mockPingService, mockServerService, null, null);
3132
act.Should().NotThrow();
3233
act.Invoke().ServerViews.Should().NotBeNull().And.BeEmpty();
3334
}

UnitystationLauncher/Constants/ApiUrls.cs

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,4 +18,7 @@ public static class ApiUrls
1818

1919
private static string RawGitHubFileBaseUrl => "https://raw.githubusercontent.com/unitystation/unitystation/develop";
2020
public static string CodeScanListUrl => $"{RawGitHubFileBaseUrl}/CodeScanList.json";
21+
public static string TTSFiles => $"{CdnBaseUrl}/STTBundleTTS/TTS";
22+
public static string TTSVersionFile => $"{TTSFiles}/version.txt";
23+
2124
}

UnitystationLauncher/Infrastructure/MessageBoxBuilder.cs

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,6 @@
11
using System.Collections.Generic;
2+
using Avalonia;
3+
using Avalonia.Controls.ApplicationLifetimes;
24
using MsBox.Avalonia;
35
using MsBox.Avalonia.Base;
46
using MsBox.Avalonia.Dto;
@@ -14,6 +16,7 @@ public static IMsBox<string> CreateMessageBox(MessageBoxButtons buttonLayout, st
1416
{
1517
IMsBox<string> msgBox = MessageBoxManager.GetMessageBoxCustom(new MessageBoxCustomParams
1618
{
19+
Topmost = true,
1720
SystemDecorations = Avalonia.Controls.SystemDecorations.BorderOnly,
1821
WindowStartupLocation = Avalonia.Controls.WindowStartupLocation.CenterScreen,
1922
ContentHeader = header,

UnitystationLauncher/Models/Api/Server.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,7 @@ public Server(string forkName, int buildVersion, string serverIp, int serverPort
2929
public string? OsxDownload { get; set; }
3030
public string? LinuxDownload { get; set; }
3131

32-
public string ServerGoodFileVersion { get; set; } = string.Empty;
32+
public string GoodFileVersion { get; set; } = string.Empty;
3333

3434
public (string, int) ForkAndVersion => (ForkName, BuildVersion);
3535

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
using System.Text.Json.Serialization;
2+
3+
namespace UnitystationLauncher.Models.Api;
4+
5+
public class VersionModel
6+
{
7+
[JsonPropertyName("version")]
8+
public required string Version { get; set; }
9+
}

UnitystationLauncher/Models/ConfigFile/Preferences.cs

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@ public class Preferences : ReactiveObject
77
private bool _autoRemove = true;
88
private int _ignoreVersionUpdate;
99
private string _installationPath = string.Empty;
10+
private bool? _TTSEnabled = null;
1011

1112
public bool AutoRemove
1213
{
@@ -25,4 +26,10 @@ public string InstallationPath
2526
get => _installationPath;
2627
set => this.RaiseAndSetIfChanged(ref _installationPath, value);
2728
}
29+
30+
public bool? TTSEnabled
31+
{
32+
get => _TTSEnabled;
33+
set => this.RaiseAndSetIfChanged(ref _TTSEnabled, value);
34+
}
2835
}

UnitystationLauncher/Models/Enums/DownloadState.cs

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,5 +10,6 @@ public enum DownloadState
1010
InProgress,
1111
Scanning,
1212
Installed,
13-
Failed
13+
Failed,
14+
Extracting
1415
}

UnitystationLauncher/Services/CodeScanConfigService.cs

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -47,11 +47,11 @@ public CodeScanConfigService(HttpClient httpClient, IPreferencesService preferen
4747

4848
string pathBase = _preferencesService.GetPreferences().InstallationPath;
4949
string folderName = GetFolderName(version);
50-
string versionPath = Path.Combine(pathBase, version, folderName);
50+
string versionPath = Path.Combine(pathBase, "nonbuild", version, folderName);
5151

5252
if (Directory.Exists(versionPath) == false)
5353
{
54-
string zipExtractPath = Path.Combine(pathBase, version);
54+
string zipExtractPath = Path.Combine(pathBase, "nonbuild", version);
5555
HttpResponseMessage request = await _httpClient.GetAsync($"{ApiUrls.GoodFilesBaseUrl}/{version}/{folderName}.zip", HttpCompletionOption.ResponseHeadersRead);
5656
await using Stream responseStream = await request.Content.ReadAsStreamAsync();
5757
ZipArchive archive = new(responseStream);
@@ -183,12 +183,12 @@ private string GetZipFolderName()
183183
switch (os)
184184
{
185185
case CurrentEnvironment.WindowsStandalone:
186-
return "Windows";
186+
return "StandaloneWindows64";
187187
case CurrentEnvironment.LinuxFlatpak:
188188
case CurrentEnvironment.LinuxStandalone:
189-
return "Linux";
189+
return "StandaloneLinux64";
190190
case CurrentEnvironment.MacOsStandalone:
191-
return "Mac";
191+
return "StandaloneOSX";
192192
default:
193193
throw new UnsupportedPlatformException($"Unable to determine OS Version {os}");
194194
}

UnitystationLauncher/Services/CodeScanService.cs

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -36,8 +36,10 @@ public async Task<bool> OnScanAsync(ZipArchive archive, string targetDirectory,
3636
// TODO: Enable extraction cancelling
3737
DirectoryInfo root = new(_preferencesService.GetPreferences().InstallationPath);
3838

39-
DirectoryInfo stagingDirectory = root.CreateSubdirectory("UnsafeBuildZipDirectory");
40-
DirectoryInfo processingDirectory = root.CreateSubdirectory("UnsafeBuildProcessing");
39+
var nonbuild = root.CreateSubdirectory("nonbuild");
40+
41+
DirectoryInfo stagingDirectory = nonbuild.CreateSubdirectory("UnsafeBuildZipDirectory");
42+
DirectoryInfo processingDirectory = nonbuild.CreateSubdirectory("UnsafeBuildProcessing");
4143
DirectoryInfo? dataPath = null;
4244
archive.ExtractToDirectory(stagingDirectory.ToString(), true);
4345
try

UnitystationLauncher/Services/InstallationService.cs

Lines changed: 29 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -41,17 +41,19 @@ public class InstallationService : IInstallationService
4141
private readonly List<Download> _downloads;
4242
private List<Installation> _installations = new();
4343
private readonly string _installationsJsonFilePath;
44+
private readonly ITTSService _TTSVersionService;
4445

4546
public InstallationService(HttpClient httpClient, IPreferencesService preferencesService,
4647
IEnvironmentService environmentService, IServerService serverService, ICodeScanService codeScanService,
47-
ICodeScanConfigService codeScanConfigService)
48+
ICodeScanConfigService codeScanConfigService, ITTSService ITTSVersionService)
4849
{
4950
_httpClient = httpClient;
5051
_preferencesService = preferencesService;
5152
_environmentService = environmentService;
5253
_serverService = serverService;
5354
_codeScanService = codeScanService;
5455
_codeScanConfigService = codeScanConfigService;
56+
_TTSVersionService = ITTSVersionService;
5557

5658
_downloads = new();
5759
_installationsJsonFilePath = Path.Combine(_environmentService.GetUserdataDirectory(), "installations.json");
@@ -93,14 +95,12 @@ public List<Installation> GetInstallations()
9395
return (null!, failureReason);
9496
}
9597

96-
server.ServerGoodFileVersion = "1.0.0"; //TODO
97-
98-
bool result = await _codeScanConfigService.ValidGoodFilesVersionAsync(server.ServerGoodFileVersion);
98+
bool result = await _codeScanConfigService.ValidGoodFilesVersionAsync(server.GoodFileVersion);
9999

100100
if (result == false)
101101
{
102102
const string failureReason = "server does not have a valid ServerGoodFileVersion ";
103-
Log.Warning(failureReason + $" ServerName: {server.ServerName} ServerGoodFileVersion : {server.ServerGoodFileVersion}");
103+
Log.Warning(failureReason + $" ServerName: {server.ServerName} ServerGoodFileVersion : {server.GoodFileVersion}");
104104
return (null!, failureReason);
105105
}
106106

@@ -115,9 +115,20 @@ public List<Installation> GetInstallations()
115115

116116
string installationBasePath = _preferencesService.GetPreferences().InstallationPath;
117117
// should be something like {basePath}/{forkName}/{version}
118-
string installationPath = Path.Combine(installationBasePath, server.ForkName.SanitiseStringPath(), server.ServerGoodFileVersion.SanitiseStringPath(), server.BuildVersion.ToString());
118+
if (server.ForkName.SanitiseStringPath() == "nonbuild")
119+
{
120+
throw new Exception($" bad server ForkName {server.ForkName.SanitiseStringPath()} Not allowed as save location (nonbuild)");
121+
}
122+
123+
if (server.ForkName.SanitiseStringPath() == "tts")
124+
{
125+
throw new Exception($" bad server ForkName {server.ForkName.SanitiseStringPath()} Not allowed as save location (tts) ");
126+
}
127+
119128

120-
download = new(downloadUrl, installationPath, server.ForkName, server.BuildVersion, server.ServerGoodFileVersion);
129+
string installationPath = Path.Combine(installationBasePath, server.ForkName.SanitiseStringPath(), server.GoodFileVersion.SanitiseStringPath(), server.BuildVersion.ToString());
130+
131+
download = new(downloadUrl, installationPath, server.ForkName, server.BuildVersion, server.GoodFileVersion);
121132

122133
(bool canStartDownload, string cantDownloadReason) = CanStartDownload(download);
123134

@@ -133,12 +144,17 @@ public List<Installation> GetInstallations()
133144
}
134145

135146
_downloads.Add(download);
147+
148+
149+
136150
RxApp.MainThreadScheduler.ScheduleAsync((_, _) => StartDownloadAsync(download));
137151
return (download, string.Empty);
138152
}
139153

140154
public (bool, string) StartInstallation(Guid installationId, string? server = null, short? port = null)
141155
{
156+
_TTSVersionService.StartTTS();
157+
142158
Installation? installation = GetInstallationById(installationId);
143159
if (installation == null)
144160
{
@@ -422,6 +438,9 @@ private static string GetArguments(string? server, long? port)
422438

423439
private async Task StartDownloadAsync(Download download)
424440
{
441+
//Update TTS if it's needed
442+
await _TTSVersionService.CheckAndDownloadLatestVersion(download);
443+
425444
Log.Information("Download requested, Installation Path '{Path}', Url '{Url}'", download.InstallPath, download.DownloadUrl);
426445
try
427446
{
@@ -433,13 +452,11 @@ private async Task StartDownloadAsync(Download download)
433452
await using Stream responseStream = await request.Content.ReadAsStreamAsync();
434453
Log.Information("Download connection established");
435454
await using ProgressStream progressStream = new(responseStream);
436-
download.Size = request.Content.Headers.ContentLength ??
437-
throw new ContentLengthNullException(download.DownloadUrl);
455+
download.Size = request.Content.Headers.ContentLength ?? throw new ContentLengthNullException(download.DownloadUrl);
438456

439457
using IDisposable logProgressDisposable = LogProgress(progressStream, download);
440458

441-
using IDisposable progressDisposable = progressStream.Progress
442-
.Subscribe(p => { download.Downloaded = p; });
459+
using IDisposable progressDisposable = progressStream.Progress.Subscribe(p => { download.Downloaded = p; });
443460

444461
// ExtractAndScan() must be run in a separate thread, but we want this one to wait for that one to finish
445462
// Without this download progress will not work properly
@@ -558,7 +575,7 @@ private static async Task ShowScanFailPopUp(string message, string logFolder)
558575
}
559576
}
560577

561-
private static IDisposable LogProgress(ProgressStream progressStream, Download download)
578+
public static IDisposable LogProgress(ProgressStream progressStream, Download download)
562579
{
563580
long lastPosition = 0L;
564581
DateTime lastTime = DateTime.Now;

0 commit comments

Comments
 (0)