Skip to content

Commit

Permalink
Fixes #25
Browse files Browse the repository at this point in the history
Added exception support to the download class, then implimented a break after a failure.
  • Loading branch information
krisdb2009 committed Nov 15, 2019
1 parent 765dce1 commit 9f5a159
Show file tree
Hide file tree
Showing 2 changed files with 23 additions and 8 deletions.
20 changes: 15 additions & 5 deletions SuperGrate/Classes/Download.cs
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
using System;
using System.Net;
using System.ComponentModel;
using System.Threading.Tasks;

namespace SuperGrate
Expand All @@ -9,27 +10,36 @@ class Download
private WebClient Client = new WebClient();
private string URL;
private string DESTINATION;
private bool Downloaded = false;
private AsyncCompletedEventArgs Done = null;
public Download(string Url, string Destination)
{
URL = Url;
DESTINATION = Destination;
Client.DownloadProgressChanged += Client_DownloadProgressChanged;
Client.DownloadFileCompleted += Client_DownloadFileCompleted;
}
private void Client_DownloadFileCompleted(object sender, System.ComponentModel.AsyncCompletedEventArgs e)
private void Client_DownloadFileCompleted(object sender, AsyncCompletedEventArgs e)
{
Downloaded = true;
Done = e;
}
private void Client_DownloadProgressChanged(object sender, DownloadProgressChangedEventArgs e)
{
Logger.UpdateProgress(e.ProgressPercentage);
}
public Task Start()
public Task<bool> Start()
{
Client.DownloadFileAsync(new Uri(URL), DESTINATION);
return Task.Run(async () => {
while (!Downloaded) { await Task.Delay(100); }
while (Done == null) { await Task.Delay(100); }
if(Done.Error == null)
{
return true;
}
else
{
Logger.Exception(Done.Error, "Error downloading: " + URL + ".");
return false;
}
});
}
}
Expand Down
11 changes: 8 additions & 3 deletions SuperGrate/Classes/USMT.cs
Original file line number Diff line number Diff line change
Expand Up @@ -251,6 +251,7 @@ private static Task<bool> DownloadUSMTFromWeb()
return Task.Run(async () => {
try
{
bool success = true;
Logger.Information("Downloading USMT (" + DetectedArch + ") from the web...");
if (!Directory.Exists(USMTPath))
{
Expand All @@ -259,15 +260,17 @@ private static Task<bool> DownloadUSMTFromWeb()
string dlPath = Path.Combine(USMTPath, "USMT.zip");
if (DetectedArch == "64-bit")
{
await new Download("https://github.com/belowaverage-org/SuperGrate/raw/master/USMT/x64.zip", dlPath).Start();
success = await new Download("https://github.com/belowaverage-org/SuperGrate/raw/master/USMT/x64.zip", dlPath).Start();
if (!success) throw new Exception("Download failure.");
}
else if (DetectedArch == "32-bit")
{
await new Download("https://github.com/belowaverage-org/SuperGrate/raw/master/USMT/x86.zip", dlPath).Start();
success = await new Download("https://github.com/belowaverage-org/SuperGrate/raw/master/USMT/x86.zip", dlPath).Start();
if (!success) throw new Exception("Download failure.");
}
else
{
return false;
throw new Exception("Could not determine target architecture.");
}
Logger.Information("Decompressing USMT...");
ZipFile.ExtractToDirectory(dlPath, USMTPath);
Expand All @@ -277,6 +280,8 @@ private static Task<bool> DownloadUSMTFromWeb()
}
catch(Exception e)
{
Logger.Warning("Removing USMT folder due to failure...");
Directory.Delete(USMTPath, true);
Logger.Exception(e, "Failed to automatically download USMT from the web. Please download USMT and update the SuperGrate.xml accordingly.");
return false;
}
Expand Down

0 comments on commit 9f5a159

Please sign in to comment.