Skip to content

Commit

Permalink
Refactored WebClient usage.
Browse files Browse the repository at this point in the history
  • Loading branch information
Friendly0Fire committed Oct 1, 2021
1 parent ea81b46 commit 34a0e9b
Show file tree
Hide file tree
Showing 5 changed files with 46 additions and 30 deletions.
16 changes: 11 additions & 5 deletions application/GW2 Addon Manager/Backend/Addons/ApprovedList.cs
Original file line number Diff line number Diff line change
Expand Up @@ -28,21 +28,27 @@ public ApprovedList(IConfigurationManager configManager)
public void FetchListFromRepo()
{
const string tempFileName = "addonlist";
var client = UpdateHelpers.GetClient();

var raw = client.DownloadStringFromGithubAPI(RepoUrl + "/branches");
string raw = null;
using(var client = UpdateHelpers.GetClient())
{
raw = client.DownloadStringFromGithubAPI(RepoUrl + "/branches");
}
var result = JsonConvert.DeserializeObject<BranchInfo[]>(raw);
var master = result.Single(r => r.Name == "master").Commit.Sha;

if (master == _configManager.UserConfig.AddonsList.Hash) return;
if (master == _configManager.UserConfig.AddonsList.Hash || master is null) return;

if (Directory.Exists(AddonFolder))
Directory.Delete(AddonFolder, true);
if (File.Exists(tempFileName))
File.Delete(tempFileName);

//fetching new version
client.DownloadFileFromGithubAPI(RepoUrl + "/zipball", tempFileName);
using (var client = UpdateHelpers.GetClient())
{
client.DownloadFileFromGithubAPI(RepoUrl + "/zipball", tempFileName);
}

ZipFile.ExtractToDirectory(tempFileName, AddonFolder);
var downloaded = Directory.EnumerateDirectories(AddonFolder).First();
foreach (var entry in Directory.EnumerateFileSystemEntries(downloaded))
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -52,10 +52,12 @@ public async Task downloadLatestRelease()
viewModel.UpdateAvailable = $"{StaticText.Downloading} {latestInfo.tag_name}";

Directory.CreateDirectory(update_folder);
var client = UpdateHelpers.GetClient();
client.DownloadProgressChanged += new DownloadProgressChangedEventHandler(selfUpdate_DownloadProgress);
client.DownloadFileCompleted += new AsyncCompletedEventHandler(selfUpdate_DownloadCompleted);
await client.DownloadFileTaskAsync(new System.Uri(downloadUrl), Path.Combine(update_folder, update_name));
using (var client = UpdateHelpers.GetClient())
{
client.DownloadProgressChanged += new DownloadProgressChangedEventHandler(selfUpdate_DownloadProgress);
client.DownloadFileCompleted += new AsyncCompletedEventHandler(selfUpdate_DownloadCompleted);
await client.DownloadFileTaskAsync(new System.Uri(downloadUrl), Path.Combine(update_folder, update_name));
}
}

/* updating download status on UI */
Expand Down
10 changes: 6 additions & 4 deletions application/GW2 Addon Manager/Backend/LoaderSetup/LoaderSetup.cs
Original file line number Diff line number Diff line change
Expand Up @@ -61,17 +61,19 @@ public async Task HandleLoaderUpdate()
private async Task Download(string url)
{
viewModel.ProgBarLabel = "Downloading Addon Loader";
var client = UpdateHelpers.GetClient();

fileName = Path.Combine(Path.GetTempPath(), Path.GetFileName(url));

if (File.Exists(fileName))
File.Delete(fileName);

client.DownloadProgressChanged += new DownloadProgressChangedEventHandler(loader_DownloadProgressChanged);
client.DownloadFileCompleted += new AsyncCompletedEventHandler(loader_DownloadCompleted);
using (var client = UpdateHelpers.GetClient())
{
client.DownloadProgressChanged += new DownloadProgressChangedEventHandler(loader_DownloadProgressChanged);
client.DownloadFileCompleted += new AsyncCompletedEventHandler(loader_DownloadCompleted);

await client.DownloadFileTaskAsync(new System.Uri(url), fileName);
await client.DownloadFileTaskAsync(new System.Uri(url), fileName);
}
Install();
}
private void Install()
Expand Down
30 changes: 17 additions & 13 deletions application/GW2 Addon Manager/Backend/Updating/GenericUpdater.cs
Original file line number Diff line number Diff line change
Expand Up @@ -57,8 +57,6 @@ public async Task Update()
/// </summary>
private async Task GitCheckUpdate()
{
var client = UpdateHelpers.GetClient();

dynamic release_info = UpdateHelpers.GitReleaseInfo(addon_info.host_url);
latestVersion = release_info.tag_name;

Expand All @@ -68,21 +66,25 @@ private async Task GitCheckUpdate()

string download_link = release_info.assets[0].browser_download_url;
viewModel.ProgBarLabel = "Downloading " + addon_info.addon_name + " " + latestVersion;
await Download(download_link, client);
await Download(download_link);
}

private async Task StandaloneCheckUpdate()
{
var client = UpdateHelpers.GetClient();
string downloadURL = addon_info.host_url;

if (addon_info.version_url != null)
latestVersion = client.DownloadString(addon_info.version_url);
{
using(var client = UpdateHelpers.GetClient())
{
latestVersion = client.DownloadString(addon_info.version_url);
}
}
else
{
//for self-updating addons' first installation
viewModel.ProgBarLabel = "Downloading " + addon_info.addon_name;
await Download(downloadURL, client);
await Download(downloadURL);
return;
}

Expand All @@ -91,7 +93,7 @@ private async Task StandaloneCheckUpdate()
return;

viewModel.ProgBarLabel = "Downloading " + addon_info.addon_name + " " + latestVersion;
await Download(downloadURL, client);
await Download(downloadURL);
}


Expand All @@ -102,9 +104,8 @@ private async Task StandaloneCheckUpdate()
/// </summary>
/// <param name="url"></param>
/// <param name="client"></param>
private async Task Download(string url, WebClient client)
private async Task Download(string url)
{

//this calls helper method to fetch filename if it is not exposed in URL
fileName = Path.Combine(
Path.GetTempPath(),
Expand All @@ -114,10 +115,13 @@ private async Task Download(string url, WebClient client)
if (File.Exists(fileName))
File.Delete(fileName);

client.DownloadProgressChanged += new DownloadProgressChangedEventHandler(addon_DownloadProgressChanged);
client.DownloadFileCompleted += new AsyncCompletedEventHandler(addon_DownloadCompleted);

await client.DownloadFileTaskAsync(new System.Uri(url), fileName);
using (var client = UpdateHelpers.GetClient())
{
client.DownloadProgressChanged += new DownloadProgressChangedEventHandler(addon_DownloadProgressChanged);
client.DownloadFileCompleted += new AsyncCompletedEventHandler(addon_DownloadCompleted);

await client.DownloadFileTaskAsync(new System.Uri(url), fileName);
}
Install();
}

Expand Down
10 changes: 6 additions & 4 deletions application/GW2 Addon Manager/Backend/Updating/UpdateHelpers.cs
Original file line number Diff line number Diff line change
Expand Up @@ -43,10 +43,12 @@ public static void DownloadFileFromGithubAPI(this WebClient wc, string url, stri

public static dynamic GitReleaseInfo(string gitUrl)
{
var client = UpdateHelpers.GetClient();
string release_info_json = client.DownloadStringFromGithubAPI(gitUrl);
return JsonConvert.DeserializeObject(release_info_json);

using (var client = UpdateHelpers.GetClient())
{
var release_info_json = client.DownloadStringFromGithubAPI(gitUrl);
return JsonConvert.DeserializeObject(release_info_json);
}

}

public static async void UpdateAll()
Expand Down

0 comments on commit 34a0e9b

Please sign in to comment.