improved the client update service to avoid blowing out client unnecessarily, and avoided conflicts with 2009Scape installs

This commit is contained in:
Greg Tseng 2025-03-02 11:13:44 -05:00
parent fa3d5ebee0
commit dd7e229db3
2 changed files with 31 additions and 31 deletions

View File

@ -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<string> ComputeLocalClientHashAsync(string filePath = null)
{
filePath ??= PreferredTargetFilePath;

View File

@ -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"
);
}