feat: final touches and bug fixes

This commit is contained in:
rjindael 2023-10-01 03:42:31 -07:00
parent c6867e2243
commit c1a2035866
No known key found for this signature in database
GPG Key ID: D069369C906CCF31
4 changed files with 54 additions and 30 deletions

View File

@ -26,13 +26,18 @@ public class Bootstrapper : Interfaces.IBootstrapper
public Bootstrapper(string payload) public Bootstrapper(string payload)
{ {
Payload = payload; // TODO: Do this in a better way?
Payload = payload.Replace($"{Constants.PROTOCOL_KEY}://", "");
if (Payload[^1] == '/')
Payload = Payload[..^1];
} }
public bool Initialize() public bool Initialize()
{ {
if (!Base64.IsBase64String(Payload)) if (!Base64.IsBase64String(Payload))
{ {
MessageBox.Show(Payload);
return false; return false;
} }
@ -40,6 +45,7 @@ public class Bootstrapper : Interfaces.IBootstrapper
string[] pieces = Base64.ConvertBase64ToString(Payload).Split("|"); string[] pieces = Base64.ConvertBase64ToString(Payload).Split("|");
if (pieces.Length != 4) if (pieces.Length != 4)
{ {
MessageBox.Show(pieces.Length.ToString());
return false; return false;
} }
@ -155,6 +161,8 @@ public class Bootstrapper : Interfaces.IBootstrapper
} }
} }
bool deleteArchive = false;
if (!clientUpToDate) if (!clientUpToDate)
{ {
// Download the required binaries // Download the required binaries
@ -164,33 +172,39 @@ public class Bootstrapper : Interfaces.IBootstrapper
Directory.Delete(Path.Combine(Paths.Versions, Arguments["Version"]), true); Directory.Delete(Path.Combine(Paths.Versions, Arguments["Version"]), true);
Directory.CreateDirectory(Path.Combine(Paths.Versions, Arguments["Version"])); Directory.CreateDirectory(Path.Combine(Paths.Versions, Arguments["Version"]));
// Download archive // Download archive in a synchronous way so that checksum doesn't get tangled
Task.WaitAny(Task.Factory.StartNew(async () => { // Create a new HttpClient
using WebClient client = new(); using (HttpClient client = new())
bool finished = false; {
// Send a GET request to the URL specified in clientRelease.Asset.Url
client.DownloadProgressChanged += (_, e) => { using (HttpResponseMessage response = await client.GetAsync(clientRelease.Asset.Url))
double bytesIn = double.Parse(e.BytesReceived.ToString()); {
double totalBytes = double.Parse(e.TotalBytesToReceive.ToString()); // Get the response content as a stream
double percentage = bytesIn / totalBytes * 100; using (Stream responseStream = await response.Content.ReadAsStreamAsync())
{
ProgressBarSet(int.Parse(Math.Truncate(percentage).ToString())); // Create a new file stream to save the downloaded file
}; using (FileStream archiveStream = new FileStream(Path.Combine(Paths.Versions, Arguments["Version"], "archive.zip"), FileMode.Create))
{
client.DownloadFileCompleted += (_, _) => finished = true; // Read the response stream and write it to the file stream
byte[] buffer = new byte[4096];
client.DownloadFileAsync(new Uri(clientRelease.Asset.Url), Path.Combine(Paths.Versions, Arguments["Version"], "archive.zip")); int bytesRead;
while ((bytesRead = await responseStream.ReadAsync(buffer, 0, buffer.Length)) > 0)
while (!finished) await Task.Delay(100); {
})); await archiveStream.WriteAsync(buffer, 0, bytesRead);
}
}
}
}
}
// Compare archive checksum // Compare archive checksum
using SHA256 SHA256 = SHA256.Create(); using SHA256 SHA256 = SHA256.Create();
using FileStream fileStream = File.OpenRead(Path.Combine(Paths.Versions, Arguments["Version"], "archive.zip")); using FileStream fileStream = File.OpenRead(Path.Combine(Paths.Versions, Arguments["Version"], "archive.zip"));
string computedChecksum = Convert.ToBase64String(SHA256.ComputeHash(fileStream)); byte[] hashBytes = SHA256.ComputeHash(fileStream);
string computedChecksum = BitConverter.ToString(hashBytes).Replace("-", "");
if (clientRelease.Asset.Checksum != computedChecksum) if (clientRelease.Asset.Checksum.ToLower() != computedChecksum.ToLower())
{ {
Error($"Failed to update {Constants.PROJECT_NAME} {Arguments["Version"]}", $"Failed to update {Constants.PROJECT_NAME}. Please try again later."); Error($"Failed to update {Constants.PROJECT_NAME} {Arguments["Version"]}", $"Failed to update {Constants.PROJECT_NAME}. Please try again later.");
return; return;
@ -201,7 +215,7 @@ public class Bootstrapper : Interfaces.IBootstrapper
ProgressBarStateChange(Enums.ProgressBarState.Marquee); ProgressBarStateChange(Enums.ProgressBarState.Marquee);
ZipFile.ExtractToDirectory(Path.Combine(Paths.Versions, Arguments["Version"], "archive.zip"), Path.Combine(Paths.Versions, Arguments["Version"])); ZipFile.ExtractToDirectory(Path.Combine(Paths.Versions, Arguments["Version"], "archive.zip"), Path.Combine(Paths.Versions, Arguments["Version"]));
File.Delete(Path.Combine(Paths.Versions, Arguments["Version"], "archive.zip")); deleteArchive = true;
} }
if (createStudioShortcut) if (createStudioShortcut)
@ -226,7 +240,7 @@ public class Bootstrapper : Interfaces.IBootstrapper
StartInfo = new() StartInfo = new()
{ {
FileName = Path.Combine(Paths.Versions, Arguments["Version"], $"{Constants.PROJECT_NAME}.Player.exe"), FileName = Path.Combine(Paths.Versions, Arguments["Version"], $"{Constants.PROJECT_NAME}.Player.exe"),
Arguments = $"-a \"{Web.FormatUrl("/Login/Negotiate.ashx")}\" -t \"{Arguments["Ticket"]}\" -j \"{Arguments["JoinScript"]}\"", Arguments = $"-a \"{Web.FormatUrl("/Login/Negotiate.ashx")}\" -t \"{Arguments["Ticket"]}\" -j \"{Arguments["JoinScript"]}\" ",
UseShellExecute = true, UseShellExecute = true,
} }
}; };
@ -240,6 +254,9 @@ public class Bootstrapper : Interfaces.IBootstrapper
launched = Win32.IsWindowVisible(player.MainWindowHandle); launched = Win32.IsWindowVisible(player.MainWindowHandle);
} }
if (deleteArchive)
File.Delete(Path.Combine(Paths.Versions, Arguments["Version"], "archive.zip"));
Environment.Exit((int)Win32.ErrorCode.ERROR_SUCCESS); Environment.Exit((int)Win32.ErrorCode.ERROR_SUCCESS);
}); });
@ -276,7 +293,7 @@ public class Bootstrapper : Interfaces.IBootstrapper
#region Installation #region Installation
public static void Install() public static void Install()
{ {
// Cleanup our registry entries beforehand (if they even exist) // Cleanup our registry entries beforehand (if they even exist)
Protocol.Unregister(); Protocol.Unregister();
Register(); Register();

View File

@ -19,6 +19,8 @@ internal static class Program
Paths.Initialize(Path.Combine(Paths.LocalAppData, Constants.PROJECT_NAME)); Paths.Initialize(Path.Combine(Paths.LocalAppData, Constants.PROJECT_NAME));
} }
Web.Initialize();
if (!Web.IsConnected && Web.IsInMaintenance) if (!Web.IsConnected && Web.IsInMaintenance)
{ {
// Try licensing this launcher and attempt to connect again // Try licensing this launcher and attempt to connect again

View File

@ -37,7 +37,7 @@ public class Protocol : Interfaces.IProtocol
catch catch
{ {
#if DEBUG #if DEBUG
throw; // throw;
#endif #endif
} }
} }

View File

@ -8,11 +8,16 @@ public static class Web
public static readonly HttpClient HttpClient = new(); public static readonly HttpClient HttpClient = new();
public static async void Initialize() public static void Initialize()
{ {
CurrentUrl = IsInMaintenance ? $"{Constants.MAINTENANCE_DOMAIN}.{Constants.BASE_URL}" : Constants.BASE_URL; CurrentUrl = IsInMaintenance ? $"{Constants.MAINTENANCE_DOMAIN}.{Constants.BASE_URL}" : Constants.BASE_URL;
HealthCheckStatus status = await GetHealthStatus(); // Synchronous block is intentional
Task<HealthCheckStatus> task = GetHealthStatus();
task.Wait();
HealthCheckStatus status = task.Result;
if (status != HealthCheckStatus.Success) if (status != HealthCheckStatus.Success)
{ {
if (status == HealthCheckStatus.Maintenance) if (status == HealthCheckStatus.Maintenance)
@ -49,7 +54,7 @@ public static class Web
public static async Task<HealthCheckStatus> GetHealthStatus() public static async Task<HealthCheckStatus> GetHealthStatus()
{ {
var response = await Http.GetJson<HealthCheck>(FormatUrl("/health-check")); var response = await Http.GetJson<HealthCheck>(FormatUrl("/api/health"));
return response?.Status ?? HealthCheckStatus.Failure; return response?.Status ?? HealthCheckStatus.Failure;
} }