Skip to content

Commit

Permalink
Even more updater optimisations + refactors
Browse files Browse the repository at this point in the history
- Revert GetLatestReleaseAsync back to returning single release instead of most recent releases

- Refactor update download/extract/install process to use async methods instead of BackgroundWorker

- Remove unneccesary exception type checks, left over from when we were still using Octokit

- Query latest release directly if we don't need the latest pre-release

- Use HttpClient instead of deprecated WebClient for update downloads

- Add API error handling for authentication and rate limit errors

- Other minor optimisations, mostly relating to variable names
  • Loading branch information
Sparronator9999 committed Jan 13, 2025
1 parent 7a83b3a commit 6b5fac7
Show file tree
Hide file tree
Showing 9 changed files with 267 additions and 207 deletions.
35 changes: 35 additions & 0 deletions YAMDCC.Updater/GitHubApi/ApiError.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
// This file is part of YAMDCC (Yet Another MSI Dragon Center Clone).
// Copyright © Sparronator9999 and Contributors 2025.
//
// YAMDCC is free software: you can redistribute it and/or modify it
// under the terms of the GNU General Public License as published by the Free
// Software Foundation, either version 3 of the License, or (at your option)
// any later version.
//
// YAMDCC is distributed in the hope that it will be useful, but
// WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
// or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for
// more details.
//
// You should have received a copy of the GNU General Public License along with
// YAMDCC. If not, see <https://www.gnu.org/licenses/>.

using Newtonsoft.Json;

namespace YAMDCC.Updater.GitHubApi;

// suppress warning about default values never getting overwritten
// since they get populated when deserialising JSON to these classes
#pragma warning disable CS0649
internal class ApiError
{
[JsonProperty("message")]
public string Message;

[JsonProperty("documentation_url")]
public string DocsUrl;

[JsonProperty("status")]
public string Status;
}
#pragma warning restore CS0649
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@

using Newtonsoft.Json;

namespace YAMDCC.Updater;
namespace YAMDCC.Updater.GitHubApi;

// suppress warning about default values never getting overwritten
// since they get populated when deserialising JSON to these classes
Expand Down Expand Up @@ -57,7 +57,7 @@ internal class Author
public string StarredUrl;

[JsonProperty("subscriptions_url")]
public string SubscriptionsUrl;
public string SubsUrl;

[JsonProperty("organizations_url")]
public string OrganizationsUrl;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@
using System;
using Newtonsoft.Json;

namespace YAMDCC.Updater;
namespace YAMDCC.Updater.GitHubApi;

// suppress warning about default values never getting overwritten
// since they get populated when deserialising JSON to these classes
Expand Down Expand Up @@ -64,13 +64,13 @@ internal class Release
public bool Draft;

[JsonProperty("prerelease")]
public bool Prerelease;
public bool PreRelease;

[JsonProperty("created_at")]
public DateTime CreatedAt;
public DateTimeOffset CreatedAt;

[JsonProperty("published_at")]
public DateTime PublishedAt;
public DateTimeOffset PublishedAt;

[JsonProperty("author")]
public Author Author;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@
using System;
using Newtonsoft.Json;

namespace YAMDCC.Updater;
namespace YAMDCC.Updater.GitHubApi;

// suppress warning about default values never getting overwritten
// since they get populated when deserialising JSON to these classes
Expand Down Expand Up @@ -55,10 +55,10 @@ internal class ReleaseAsset
public int DownloadCount;

[JsonProperty("created_at")]
public DateTime CreatedAt;
public DateTimeOffset CreatedAt;

[JsonProperty("updated_at")]
public DateTime UpdatedAt;
public DateTimeOffset UpdatedAt;

[JsonProperty("uploader")]
public Author Uploader;
Expand Down
44 changes: 20 additions & 24 deletions YAMDCC.Updater/Program.cs
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@
using System.Windows.Forms;
using YAMDCC.Common;
using YAMDCC.Common.Dialogs;
using YAMDCC.Updater.GitHubApi;

namespace YAMDCC.Updater;

Expand All @@ -49,15 +50,15 @@ private static int Main(string[] args)
return BGUpdate(false) ? 0 : 1;
case "--setautoupdate":
if (Utils.IsAdmin() && args.Length >= 2 &&
bool.TryParse(args[1], out bool enable))
bool.TryParse(args[1], out bool enabled))
{
Updater.SetAutoUpdateEnabled(enable);
Updater.SetAutoUpdateEnabled(enabled);
return 0;
}
Utils.ShowError("Could not set auto-update state!");
return 1;
case "--install":
// args: oldPath, updatePath, destPath
// args: --install <oldPath> <updatePath> <destPath>
if (args.Length >= 4)
{
InstallUpdate(args[1], args[2], args[3]);
Expand All @@ -77,6 +78,7 @@ private static void InstallUpdate(
ProgressDialog dlg = new("Installing YAMDCC update...", (e) =>
{
// uninstall the old YAMDCC service
// TODO: detect if YAMDCC service is already uninstalled
if (Utils.StopService("yamdccsvc"))
{
if (!Utils.UninstallService($"{destPath}\\yamdccsvc"))
Expand Down Expand Up @@ -122,6 +124,7 @@ private static void InstallUpdate(
}

// cleanup :)
// (note: does not delete "Old" folder that we should be running from)
Directory.Delete(updatePath);
});
dlg.ShowDialog();
Expand All @@ -139,38 +142,34 @@ private static bool BGUpdate(bool autoUpdate)
{
try
{
Release[] releases = Updater.GetReleasesAsync(Utils.GetCurrentVerSuffix() != "release").GetAwaiter().GetResult();
Release release = Updater.GetLatestReleaseAsync(Utils.GetCurrentVerSuffix() != "release").GetAwaiter().GetResult();

if (Utils.GetCurrentVersion() < Utils.GetVersion(releases[0].TagName.Remove(0, 1)))
if (Utils.GetCurrentVersion() < Utils.GetVersion(release.TagName.Remove(0, 1)))
{
Application.Run(new UpdateForm(releases, autoUpdate));
Application.Run(new UpdateForm(release, autoUpdate));
}
else if (!autoUpdate)
{
Utils.ShowInfo("YAMDCC is up to date.", "Up to date");
}
return true;
}
catch (Exception ex)
catch (HttpRequestException ex)
{
if (ex is HttpRequestException)
{
Utils.ShowError("Failed to check for YAMDCC update!\n" +
$"Details:\n{GetExceptionMsgs(ex)}");
return false;
}
else
{
throw;
}
Utils.ShowError("Failed to check for YAMDCC update!\n" +
$"Details:\n{GetExceptionMsgs(ex)}");
return false;
}
}

private static string GetExceptionMsgs(Exception ex)
{
return ex.InnerException is null
? $"{ex.GetType()}: {ex.Message}"
: $"{ex.GetType()}: {ex.Message} ---> {GetExceptionMsgs(ex.InnerException)}";
string str = $"{ex.GetType()}: {ex.Message}";
if (ex.InnerException is not null)
{
str += $" ---> {GetExceptionMsgs(ex.InnerException)}";
}
return str;
}

private static Assembly AssemblyResolve(object sender, ResolveEventArgs e)
Expand All @@ -187,9 +186,6 @@ private static Assembly AssemblyResolve(object sender, ResolveEventArgs e)

return Assembly.Load(output.ToArray());
}
else
{
return null;
}
return null;
}
}
4 changes: 2 additions & 2 deletions YAMDCC.Updater/UpdateForm.Designer.cs

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Loading

0 comments on commit 6b5fac7

Please sign in to comment.