Skip to content

Commit

Permalink
Fix #974
Browse files Browse the repository at this point in the history
  • Loading branch information
SirSparkles committed Jun 30, 2023
1 parent 42c4494 commit befac50
Show file tree
Hide file tree
Showing 2 changed files with 51 additions and 9 deletions.
4 changes: 4 additions & 0 deletions TVRename/Utility/Release.cs
Original file line number Diff line number Diff line change
Expand Up @@ -105,6 +105,10 @@ private int CompareTo(object? obj)

public bool NewerThan(Release? compare) => CompareTo(compare) > 0;

public bool OlderThan(Release? compare) => CompareTo(compare) < 0;

public bool SameVersionAs(Release? compare) => CompareTo(compare) == 0;

public override string ToString()
{
StringBuilder sb = new();
Expand Down
56 changes: 47 additions & 9 deletions TVRename/Utility/VersionUpdater.cs
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ public static class VersionUpdater

public static async Task<ServerRelease?> CheckForUpdatesAsync()
{
Release currentVersion;
Release? currentVersion = null;

try
{
Expand All @@ -31,19 +31,57 @@ public static class VersionUpdater
catch (ArgumentException e)
{
Logger.Error(e, "Failed to establish if there are any new releases as could not parse internal version: " + Helpers.DisplayVersion);
return null;
}

(ServerRelease? latestVersion, ServerRelease? latestBetaVersion) = await GetLatestReleasesAsync().ConfigureAwait(false);

return TVSettings.Instance.mode switch
bool production = TVSettings.Instance.mode == TVSettings.BetaMode.ProductionOnly;

if (production)
{
TVSettings.BetaMode.ProductionOnly when latestVersion?.NewerThan(currentVersion) ?? false =>
latestVersion,
TVSettings.BetaMode.BetaToo when latestBetaVersion?.NewerThan(currentVersion) ?? false =>
latestBetaVersion,
_ => null
};
if (currentVersion is null)
{
Logger.Warn("Internal version is missing, assuming we need to upgrade.");
return latestVersion ?? latestBetaVersion;
}
if (currentVersion.SameVersionAs(latestVersion))
{
return null;
}
if (latestVersion is null)
{
Logger.Warn("Cannot obtain the latest version from GitHub");
return null;
}

if (latestVersion.NewerThan(currentVersion))
{
return latestVersion;
}
}

if (!production)
{
if (currentVersion is null)
{
Logger.Warn("Internal version is missing, assuming we need to upgrade to latest beta.");
return latestBetaVersion ?? latestVersion;
}
if (currentVersion.SameVersionAs(latestBetaVersion))
{
return null;
}
if (latestBetaVersion is null)
{
Logger.Warn("Cannot obtain the latest beta version from GitHub");
return null;
}
if (latestBetaVersion.NewerThan(currentVersion))
{
return latestBetaVersion;
}
}
return null;
}

private static async Task<(ServerRelease? latestVersion, ServerRelease? latestBetaVersion)> GetLatestReleasesAsync()
Expand Down

0 comments on commit befac50

Please sign in to comment.