make progress bar use bytes downloaded

This commit is contained in:
bluepilledgreat 2023-01-08 13:47:13 +00:00
parent 83fa7f3b13
commit ff9b6a0016

View File

@ -82,7 +82,9 @@ namespace Bloxstrap
private readonly bool FreshInstall; private readonly bool FreshInstall;
private int ProgressIncrement; private double ProgressIncrement;
private long TotalBytes = 0;
private long TotalDownloadedBytes = 0;
private bool CancelFired = false; private bool CancelFired = false;
public IBootstrapperDialog Dialog = null!; public IBootstrapperDialog Dialog = null!;
@ -111,7 +113,9 @@ namespace Bloxstrap
// if bloxstrap is installing for the first time but is running, prompt to close roblox // if bloxstrap is installing for the first time but is running, prompt to close roblox
// if roblox needs updating but is running, ignore update for now // if roblox needs updating but is running, ignore update for now
#if !DEBUG
if (!Directory.Exists(VersionFolder) && CheckIfRunning(true) || Program.Settings.VersionGuid != VersionGuid && !CheckIfRunning(false)) if (!Directory.Exists(VersionFolder) && CheckIfRunning(true) || Program.Settings.VersionGuid != VersionGuid && !CheckIfRunning(false))
#endif
await InstallLatestVersion(); await InstallLatestVersion();
ApplyModifications(); ApplyModifications();
@ -394,6 +398,12 @@ namespace Bloxstrap
#endregion #endregion
#region Roblox Install #region Roblox Install
private void UpdateProgressbar()
{
int newProgress = (int)Math.Floor(ProgressIncrement * TotalDownloadedBytes);
Dialog.ProgressValue = newProgress;
}
private async Task InstallLatestVersion() private async Task InstallLatestVersion()
{ {
if (FreshInstall) if (FreshInstall)
@ -405,34 +415,24 @@ namespace Bloxstrap
Dialog.CancelEnabled = true; Dialog.CancelEnabled = true;
// i believe the bootstrapper bases the progress bar off
// bytes downloaded / bytes total according to rbxPkgManifest?
// i'm too lazy for that, so here it's just based off how many packages
// have finished downloading
Dialog.ProgressStyle = ProgressBarStyle.Continuous; Dialog.ProgressStyle = ProgressBarStyle.Continuous;
ProgressIncrement = (int)Math.Floor((decimal)1 / VersionPackageManifest.Count * 100); // compute total bytes to download
foreach (var package in VersionPackageManifest)
{
TotalBytes += package.PackedSize;
}
ProgressIncrement = (double)1 / TotalBytes * 100;
Directory.CreateDirectory(Directories.Downloads); Directory.CreateDirectory(Directories.Downloads);
foreach (Package package in VersionPackageManifest) foreach (Package package in VersionPackageManifest)
{ {
// download all the packages at once // download all the packages at once
DownloadPackage(package); await DownloadPackage(package);
} }
do
{
// wait for download to finish (and also round off the progress bar if needed)
if (Dialog.ProgressValue == ProgressIncrement * VersionPackageManifest.Count)
Dialog.ProgressValue = 100;
await Task.Delay(1000);
}
while (Dialog.ProgressValue != 100);
Dialog.ProgressStyle = ProgressBarStyle.Marquee; Dialog.ProgressStyle = ProgressBarStyle.Marquee;
Debug.WriteLine("Finished downloading"); Debug.WriteLine("Finished downloading");
@ -592,7 +592,7 @@ namespace Bloxstrap
} }
} }
private async void DownloadPackage(Package package) private async Task DownloadPackage(Package package)
{ {
string packageUrl = $"{DeployManager.BaseUrl}/{VersionGuid}-{package.Name}"; string packageUrl = $"{DeployManager.BaseUrl}/{VersionGuid}-{package.Name}";
string packageLocation = Path.Combine(Directories.Downloads, package.Signature); string packageLocation = Path.Combine(Directories.Downloads, package.Signature);
@ -602,18 +602,23 @@ namespace Bloxstrap
{ {
FileInfo file = new(packageLocation); FileInfo file = new(packageLocation);
#if !DEBUG
string calculatedMD5 = Utilities.MD5File(packageLocation); string calculatedMD5 = Utilities.MD5File(packageLocation);
if (calculatedMD5 != package.Signature) if (calculatedMD5 != package.Signature)
{ {
Debug.WriteLine($"{package.Name} is corrupted ({calculatedMD5} != {package.Signature})! Deleting and re-downloading..."); Debug.WriteLine($"{package.Name} is corrupted ({calculatedMD5} != {package.Signature})! Deleting and re-downloading...");
#endif
file.Delete(); file.Delete();
#if !DEBUG
} }
else else
{ {
Debug.WriteLine($"{package.Name} is already downloaded, skipping..."); Debug.WriteLine($"{package.Name} is already downloaded, skipping...");
Dialog.ProgressValue += ProgressIncrement; TotalDownloadedBytes += package.PackedSize;
UpdateProgressbar();
return; return;
} }
#endif
} }
else if (File.Exists(robloxPackageLocation)) else if (File.Exists(robloxPackageLocation))
{ {
@ -622,7 +627,8 @@ namespace Bloxstrap
Debug.WriteLine($"Found existing version of {package.Name} ({robloxPackageLocation})! Copying to Downloads folder..."); Debug.WriteLine($"Found existing version of {package.Name} ({robloxPackageLocation})! Copying to Downloads folder...");
File.Copy(robloxPackageLocation, packageLocation); File.Copy(robloxPackageLocation, packageLocation);
Dialog.ProgressValue += ProgressIncrement; TotalDownloadedBytes += package.PackedSize;
UpdateProgressbar();
return; return;
} }
@ -630,18 +636,30 @@ namespace Bloxstrap
{ {
Debug.WriteLine($"Downloading {package.Name}..."); Debug.WriteLine($"Downloading {package.Name}...");
var response = await Program.HttpClient.GetAsync(packageUrl);
if (CancelFired) if (CancelFired)
return; return;
var response = await Program.HttpClient.GetAsync(packageUrl, HttpCompletionOption.ResponseHeadersRead);
var buffer = new byte[8192];
using (var stream = await response.Content.ReadAsStreamAsync())
using (var fileStream = new FileStream(packageLocation, FileMode.CreateNew)) using (var fileStream = new FileStream(packageLocation, FileMode.CreateNew))
{ {
await response.Content.CopyToAsync(fileStream); while (true)
{
var bytesRead = await stream.ReadAsync(buffer, 0, buffer.Length);
if (bytesRead == 0)
break; // we're done
await fileStream.WriteAsync(buffer, 0, bytesRead);
TotalDownloadedBytes += bytesRead;
UpdateProgressbar();
}
} }
Debug.WriteLine($"Finished downloading {package.Name}!"); Debug.WriteLine($"Finished downloading {package.Name}!");
Dialog.ProgressValue += ProgressIncrement;
} }
} }
@ -693,7 +711,7 @@ namespace Bloxstrap
if (package is null) if (package is null)
return; return;
DownloadPackage(package); DownloadPackage(package).GetAwaiter().GetResult();
string packageLocation = Path.Combine(Directories.Downloads, package.Signature); string packageLocation = Path.Combine(Directories.Downloads, package.Signature);
string packageFolder = Path.Combine(VersionFolder, PackageDirectories[package.Name]); string packageFolder = Path.Combine(VersionFolder, PackageDirectories[package.Name]);