From a94c84840f8571091677f2c70dc43a7387d6bbbb Mon Sep 17 00:00:00 2001 From: EmoScape Date: Sun, 2 Mar 2025 11:13:44 -0500 Subject: [PATCH] improved the client update service to avoid blowing out client unnecessarily, and avoided conflicts with 2009Scape installs --- .../Services/ClientUpdateService.cs | 52 +++++++++---------- Saradomin/Utilities/CrossPlatform.cs | 10 ++-- 2 files changed, 31 insertions(+), 31 deletions(-) diff --git a/Saradomin/Infrastructure/Services/ClientUpdateService.cs b/Saradomin/Infrastructure/Services/ClientUpdateService.cs index cfa53bc..76ea166 100644 --- a/Saradomin/Infrastructure/Services/ClientUpdateService.cs +++ b/Saradomin/Infrastructure/Services/ClientUpdateService.cs @@ -16,8 +16,8 @@ namespace Saradomin.Infrastructure.Services private float CurrentDownloadProgress { get; set; } - public string ClientDownloadURL => "https://gitlab.com/2009scape/rt4-client/-/jobs/artifacts/master/raw/client/build/libs/rt4-client.jar?job=build"; - public string ClientHashURL => "https://gitlab.com/2009scape/rt4-client/-/jobs/artifacts/master/raw/client/build/libs/rt4-client.jar.sha256?job=build"; + public string ClientDownloadURL => "https://assets.emoscape.org/rt4-client.jar"; + public string ClientHashURL => "https://assets.emoscape.org/rt4-client.jar.sha256"; public string PreferredTargetFilePath => CrossPlatform.Get2009scapeExecutable(); @@ -38,45 +38,45 @@ namespace Saradomin.Infrastructure.Services } } - public async Task FetchRemoteClientExecutableAsync(CancellationToken cancellationToken, - string targetPath = null) + public async Task FetchRemoteClientExecutableAsync(CancellationToken cancellationToken, string targetPath = null) { CurrentDownloadProgress = 0; - targetPath ??= PreferredTargetFilePath; + + // Define a temporary file path to download to + string tempFilePath = targetPath + ".tmp"; - if (File.Exists(targetPath)) - { - File.Delete(targetPath); - } - + // Download the client to the temporary file regardless of whether it already exists. using (var httpClient = new HttpClient()) { var response = await httpClient.GetAsync(ClientDownloadURL, cancellationToken); var contentLength = response.Content.Headers.ContentLength ?? 12 * 1024 * 1024 * 1024f; using var responseStream = await response.Content.ReadAsStreamAsync(cancellationToken); - using var outFileStream = File.OpenWrite(targetPath); - - var data = new byte[1024]; - var totalRead = 0; - - while (responseStream.Position < contentLength) + using (var outFileStream = File.Create(tempFilePath)) { - var dataRead = await responseStream.ReadAsync(data, 0, data.Length, cancellationToken); - - if (dataRead <= 0) - throw new IOException("Unexpected 0-byte read in network stream."); - - await outFileStream.WriteAsync(data[0..dataRead], cancellationToken); - totalRead += dataRead; - - CurrentDownloadProgress = totalRead / contentLength; - DownloadProgressChanged?.Invoke(this, CurrentDownloadProgress); + var buffer = new byte[1024]; + var totalRead = 0; + int bytesRead; + while ((bytesRead = await responseStream.ReadAsync(buffer, 0, buffer.Length, cancellationToken)) > 0) + { + await outFileStream.WriteAsync(buffer, 0, bytesRead, cancellationToken); + totalRead += bytesRead; + CurrentDownloadProgress = totalRead / contentLength; + DownloadProgressChanged?.Invoke(this, CurrentDownloadProgress); + } } } + + // Replace the existing file only after a successful download. + if (File.Exists(targetPath)) + { + File.Delete(targetPath); + } + File.Move(tempFilePath, targetPath); } + public async Task ComputeLocalClientHashAsync(string filePath = null) { filePath ??= PreferredTargetFilePath; diff --git a/Saradomin/Utilities/CrossPlatform.cs b/Saradomin/Utilities/CrossPlatform.cs index 202f3aa..cd56e84 100644 --- a/Saradomin/Utilities/CrossPlatform.cs +++ b/Saradomin/Utilities/CrossPlatform.cs @@ -188,18 +188,18 @@ namespace Saradomin.Utilities { return Path.Combine( LocateUnixUserHome(), - "2009scape" + "EmoScape" ); } else { var userProfile = Path.Combine ( Environment.GetFolderPath(Environment.SpecialFolder.UserProfile), - "2009scape" + "EmoScape" ); var appData = Path.Combine( Environment.GetFolderPath(Environment.SpecialFolder.ApplicationData), - "2009scape" + "EmoScape" ); return Directory.Exists(userProfile) ? userProfile : appData; } @@ -213,14 +213,14 @@ namespace Saradomin.Utilities return Path.Combine( // Get the XDG_DATA_HOME environment variable, or if it doesn't exist, use the default ~/.local/share LocateUnixUserHome(), - "2009scape", + "EmoScape", "saradomin" ); } return Path.Combine( Environment.GetFolderPath(Environment.SpecialFolder.UserProfile), - "2009scape", + "EmoScape", "saradomin" ); }