Skip to content

Commit

Permalink
Implement GitHub update checks for ASFFreeGamesPlugin (v1.8.0.0)
Browse files Browse the repository at this point in the history
* **Implemented automatic update check via GitHub:** The `ASFFreeGamesPlugin` now supports checking for updates through GitHub. This feature is currently disabled by default (`CanUpdate` is initially set to `true`).
* **Added `GithubPluginUpdater` class:** This class handles communication with GitHub to retrieve the latest release information.
* **Updated version number:** The plugin version is bumped to `1.8.0.0`.
* **Removed unnecessary dependencies:** The build process now excludes unnecessary DLLs from the final package.

**Fixes issue #101.**
  • Loading branch information
maxisoft committed Oct 13, 2024
1 parent bdcdf3d commit 155352c
Show file tree
Hide file tree
Showing 4 changed files with 85 additions and 3 deletions.
3 changes: 3 additions & 0 deletions .github/workflows/publish.yml
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,9 @@ jobs:
seven_zip_args="-mx=1"
zip_args="-1"
# Remove useless dlls
rm -rf out/${1}/${PLUGIN_NAME}/System.IO.Hashing.dll out/${1}/${PLUGIN_NAME}/NLog.dll out/${1}/${PLUGIN_NAME}/SteamKit2.dll out/${1}/${PLUGIN_NAME}/System.IO.Hashing.dll out/${1}/${PLUGIN_NAME}/protobuf-net.Core.dll out/${1}/${PLUGIN_NAME}/protobuf-net.dll
# Include extra logic for builds marked for release
case "$GITHUB_REF" in
"refs/tags/"*)
Expand Down
13 changes: 11 additions & 2 deletions ASFFreeGames/ASFFreeGamesPlugin.cs
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@
using JetBrains.Annotations;
using Maxisoft.ASF.ASFExtentions;
using Maxisoft.ASF.Configurations;
using Maxisoft.ASF.Github;
using Maxisoft.ASF.Utils;
using SteamKit2;
using static ArchiSteamFarm.Core.ASF;
Expand All @@ -29,7 +30,7 @@ internal interface IASFFreeGamesPlugin {

#pragma warning disable CA1812 // ASF uses this class during runtime
[SuppressMessage("Design", "CA1001:Disposable fields")]
internal sealed class ASFFreeGamesPlugin : IASF, IBot, IBotConnection, IBotCommand2, IUpdateAware, IASFFreeGamesPlugin {
internal sealed class ASFFreeGamesPlugin : IASF, IBot, IBotConnection, IBotCommand2, IUpdateAware, IASFFreeGamesPlugin, IGitHubPluginUpdates {
internal const string StaticName = nameof(ASFFreeGamesPlugin);
private const int CollectGamesTimeout = 3 * 60 * 1000;

Expand All @@ -43,7 +44,9 @@ internal static PluginContext Context {
private static CancellationToken CancellationToken => Context.CancellationToken;

public string Name => StaticName;
public Version Version => typeof(ASFFreeGamesPlugin).Assembly.GetName().Version ?? throw new InvalidOperationException(nameof(Version));
public Version Version => GetVersion();

private static Version GetVersion() => typeof(ASFFreeGamesPlugin).Assembly.GetName().Version ?? throw new InvalidOperationException(nameof(Version));

private readonly ConcurrentHashSet<Bot> Bots = new(new BotEqualityComparer());
private readonly Lazy<CancellationTokenSource> CancellationTokenSourceLazy = new(static () => new CancellationTokenSource());
Expand Down Expand Up @@ -209,6 +212,12 @@ private async Task RemoveBot(Bot bot) {
private void StartTimerIfNeeded() => CollectIntervalManager.StartTimerIfNeeded();

~ASFFreeGamesPlugin() => CollectIntervalManager.Dispose();
public readonly GithubPluginUpdater Updater = new(new Lazy<Version>(GetVersion));
string IGitHubPluginUpdates.RepositoryName => GithubPluginUpdater.RepositoryName;

bool IGitHubPluginUpdates.CanUpdate => Updater.CanUpdate;

Task<Uri?> IGitHubPluginUpdates.GetTargetReleaseURL(Version asfVersion, string asfVariant, bool asfUpdate, bool stable, bool forced) => Updater.GetTargetReleaseURL(asfVersion, asfVariant, asfUpdate, stable, forced);
}

#pragma warning restore CA1812 // ASF uses this class during runtime
70 changes: 70 additions & 0 deletions ASFFreeGames/Github/GithubPluginUpdater.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,70 @@
using System;
using System.Linq;
using System.Threading.Tasks;
using ArchiSteamFarm;
using ArchiSteamFarm.Localization;
using ArchiSteamFarm.Web.GitHub;
using ArchiSteamFarm.Web.GitHub.Data;

namespace Maxisoft.ASF.Github;

public class GithubPluginUpdater(Lazy<Version> version) {
public const string RepositoryName = "maxisoft/ASFFreeGames";
public bool CanUpdate { get; internal set; } = true;

private Version CurrentVersion => version.Value;

public async Task<Uri?> GetTargetReleaseURL(Version asfVersion, string asfVariant, bool asfUpdate, bool stable, bool forced) {
ArgumentNullException.ThrowIfNull(asfVersion);
ArgumentException.ThrowIfNullOrEmpty(asfVariant);

if (!CanUpdate) {
return null;
}

if (string.IsNullOrEmpty(RepositoryName)) {
//ArchiSteamFarm.Core.ASF.ArchiLogger.LogGenericError(Strings.FormatWarningFailedWithError(nameof(RepositoryName)));

return null;
}

ReleaseResponse? releaseResponse = await GitHubService.GetLatestRelease(RepositoryName).ConfigureAwait(false);

if (releaseResponse == null) {
return null;
}

if (releaseResponse.IsPreRelease) {
return null;
}

Version newVersion = new(releaseResponse.Tag.ToUpperInvariant().TrimStart('V'));

if (!forced && (CurrentVersion >= newVersion)) {
// Allow same version to be re-updated when we're updating ASF release and more than one asset is found - potential compatibility difference
if ((CurrentVersion > newVersion) || !asfUpdate || (releaseResponse.Assets.Count(static asset => asset.Name.EndsWith(".zip", StringComparison.OrdinalIgnoreCase)) < 2)) {
//ASF.ArchiLogger.LogGenericInfo(Strings.FormatPluginUpdateNotFound(Name, Version, newVersion));

return null;
}
}

if (releaseResponse.Assets.Count == 0) {
//ASF.ArchiLogger.LogGenericWarning(Strings.FormatPluginUpdateNoAssetFound(Name, Version, newVersion));

return null;
}

ReleaseAsset? asset = releaseResponse.Assets.FirstOrDefault(static asset => asset.Name.EndsWith(".zip", StringComparison.OrdinalIgnoreCase) && (asset.Size > (1 << 18)));

if ((asset == null) || !releaseResponse.Assets.Contains(asset)) {
//ASF.ArchiLogger.LogGenericWarning(Strings.FormatPluginUpdateNoAssetFound(Name, Version, newVersion));

return null;
}

//.ArchiLogger.LogGenericInfo(Strings.FormatPluginUpdateFound(Name, Version, newVersion));

return asset.DownloadURL;
}
}
2 changes: 1 addition & 1 deletion Directory.Build.props
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@

<PropertyGroup>
<PluginName>ASFFreeGames</PluginName>
<Version>1.7.1.0</Version>
<Version>1.8.0.0</Version>
<TargetFramework>net8.0</TargetFramework>
</PropertyGroup>

Expand Down

0 comments on commit 155352c

Please sign in to comment.