Added ETA for download GCN/Wii Base

This commit is contained in:
ZestyTS 2023-10-03 20:09:21 -07:00
parent f3725826d5
commit 522c1cc338
2 changed files with 101 additions and 10 deletions

View file

@ -32,6 +32,7 @@ using System.Runtime.InteropServices.WindowsRuntime;
using System.Security.Cryptography;
using System.Drawing;
using static System.Net.WebRequestMethods;
using System.Windows.Media.Animation;
namespace UWUVCI_AIO_WPF
{
@ -2479,20 +2480,30 @@ namespace UWUVCI_AIO_WPF
}
return null;
}
public void ThreadDOwn()
{
}
public void Download()
{
ValidatePathsStillExist();
if (CheckForInternetConnection())
{
Task.Run(() => { Injection.Download(this); });
DownloadWait dw;
if (GameConfiguration.Console == GameConsoles.WII || GameConfiguration.Console == GameConsoles.GCN)
{
double speed = TestDownloadSpeed(); // in MB/s
TimeSpan estimatedTime = CalculateEstimatedTime(speed);
DownloadWait dw = new DownloadWait("Downloading Base - Please Wait", "", this);
// Start the actual download
Task.Run(() => { Injection.Download(this); });
// Display the waiting dialog with the estimated time
dw = new DownloadWait("Downloading Base - Please Wait", estimatedTime, this);
}
else
{
Task.Run(() => { Injection.Download(this); });
dw = new DownloadWait("Downloading Base - Please Wait", "", this);
}
try
{
dw.changeOwner(mw);
@ -2501,9 +2512,47 @@ namespace UWUVCI_AIO_WPF
dw.ShowDialog();
Progress = 0;
}
}
private double TestDownloadSpeed()
{
WebClient webClient = new WebClient();
Stopwatch sw = new Stopwatch();
//Using this file as a test file, it's about 16MB which should be small enough to not impact anything.
string url = "https://github.com/NicoAICP/UWUVCI-Tools/raw/master/gba2.zip";
byte[] data;
sw.Start();
try
{
data = webClient.DownloadData(url);
}
catch
{
return 0;
}
sw.Stop();
double timeTaken = sw.Elapsed.TotalSeconds; // time in seconds
double sizeOfData = data.Length / 1024.0 / 1024.0; // size in MB
return sizeOfData / timeTaken; // returns speed in MB/s
}
private TimeSpan CalculateEstimatedTime(double speedInMbps)
{
const double fileSize = 8.5 * 1024; // file size in MB
if (speedInMbps <= 0)
return TimeSpan.MaxValue;
double estimatedTimeInSec = fileSize / speedInMbps;
return TimeSpan.FromSeconds(estimatedTimeInSec);
}
public GameConsoles GetConsoleOfBase(GameBases gb)
{
GameConsoles ret = new GameConsoles();

View file

@ -12,6 +12,7 @@ namespace UWUVCI_AIO_WPF.UI.Windows
{
MainViewModel mvm;
DispatcherTimer timer = new DispatcherTimer();
private TimeSpan remainingTime;
public DownloadWait(string doing, string msg, MainViewModel mvm)
{
try
@ -54,6 +55,36 @@ namespace UWUVCI_AIO_WPF.UI.Windows
timer.Tick += timer_Tick;
}
public DownloadWait(string doing, TimeSpan estimatedTime, MainViewModel mvm)
{
// Initialization same as original constructor
try
{
if (Owner?.GetType() == typeof(MainWindow))
{
WindowStartupLocation = WindowStartupLocation.CenterOwner;
}
}
catch (Exception)
{
WindowStartupLocation = WindowStartupLocation.CenterScreen;
}
this.mvm = mvm;
InitializeComponent();
Key.Text = doing;
// Handle estimated time
if (estimatedTime != TimeSpan.MaxValue)
remainingTime = estimatedTime;
else
// Can't estimate, just starting with zero
remainingTime = TimeSpan.Zero;
timer.Interval = TimeSpan.FromSeconds(1);
timer.Tick += timer_Tick;
timer.Start();
}
private void Window_Minimize(object sender, RoutedEventArgs e)
{
WindowState = WindowState.Minimized;
@ -64,7 +95,18 @@ namespace UWUVCI_AIO_WPF.UI.Windows
pb.Value = mvm.Progress;
if(Key.Text.Contains("Downloading Base"))
{
if(mvm.Progress < 70)
// Check if remainingTime has been initialized (i.e., not zero)
if (remainingTime != TimeSpan.Zero)
{
msgT.Text += $"\nEstimated time remaining: {remainingTime.Minutes} minutes {remainingTime.Seconds} seconds";
if (remainingTime.TotalSeconds > 0)
{
remainingTime = remainingTime.Add(TimeSpan.FromSeconds(-1));
}
}
if (mvm.Progress < 70)
{
mvm.Progress += 1;
}