Skip to content

Commit

Permalink
Fix auto-update condition
Browse files Browse the repository at this point in the history
  • Loading branch information
Rudokhvist committed Sep 15, 2024
1 parent 0730ea1 commit d06c215
Show file tree
Hide file tree
Showing 2 changed files with 78 additions and 12 deletions.
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>ItemDispenser</PluginName>
<Version>3.0.0.0</Version>
<Version>3.0.0.1</Version>
</PropertyGroup>

<PropertyGroup>
Expand Down
88 changes: 77 additions & 11 deletions ItemDispenser/ItemDispenser.cs
Original file line number Diff line number Diff line change
Expand Up @@ -4,19 +4,25 @@
using System.Collections.Generic;
using System.Collections.ObjectModel;
using System.Composition;
using System.Diagnostics.Eventing.Reader;
using System.Globalization;
using System.Linq;
using System.Reflection;
using System.Text.Json;
using System.Threading.Tasks;
using ArchiSteamFarm;
using ArchiSteamFarm.Localization;
using ArchiSteamFarm.Core;
using ArchiSteamFarm.Helpers.Json;
using ArchiSteamFarm.IPC.Controllers.Api;
using ArchiSteamFarm.Plugins.Interfaces;
using ArchiSteamFarm.Steam;
using ArchiSteamFarm.Steam.Cards;
using ArchiSteamFarm.Steam.Data;
using ArchiSteamFarm.Steam.Exchange;
using ArchiSteamFarm.Steam.Storage;
using ArchiSteamFarm.Web.GitHub.Data;
using ArchiSteamFarm.Web.GitHub;

namespace ItemDispenser;

Expand All @@ -31,28 +37,88 @@ internal sealed class ItemDispenser : IBotTradeOffer2, IBotModules, IGitHubPlugi

public string RepositoryName => "Rudokhvist/ItemDispenser";

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

if ((releaseAssets == null) || (releaseAssets.Count == 0)) {
throw new ArgumentNullException(nameof(releaseAssets));
if (string.IsNullOrEmpty(RepositoryName)) {
ASF.ArchiLogger.LogGenericError(string.Format(CultureInfo.CurrentCulture, Strings.WarningFailedWithError, (nameof(RepositoryName))));

return null;
}

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

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

Version newVersion = new(releaseResponse.Tag);

if (!(Version.Major == newVersion.Major && Version.Minor == newVersion.Minor && Version.Build == newVersion.Build) && !(asfUpdate || forced)) {
ASF.ArchiLogger.LogGenericInfo(string.Format(CultureInfo.CurrentCulture, "New {0} plugin version {1} is only compatible with latest ASF version", Name, newVersion));
return null;
}


if (Version >= newVersion & !forced) {
ASF.ArchiLogger.LogGenericInfo(string.Format(CultureInfo.CurrentCulture, Strings.PluginUpdateNotFound, Name, Version, newVersion));

return null;
}

Collection<ReleaseAsset?> matches = [.. releaseAssets.Where(r => r.Name.Equals(Name + ".zip", StringComparison.OrdinalIgnoreCase))];
if (releaseResponse.Assets.Count == 0) {
ASF.ArchiLogger.LogGenericWarning(string.Format(CultureInfo.CurrentCulture, Strings.PluginUpdateNoAssetFound, Name, Version, newVersion));

if (matches.Count != 1) {
return Task.FromResult((ReleaseAsset?) null);
return null;
}

ReleaseAsset? release = matches[0];
ReleaseAsset? asset = await ((IGitHubPluginUpdates) this).GetTargetReleaseAsset(asfVersion, asfVariant, newVersion, releaseResponse.Assets).ConfigureAwait(false);

if ((asset == null) || !releaseResponse.Assets.Contains(asset)) {
ASF.ArchiLogger.LogGenericWarning(string.Format(CultureInfo.CurrentCulture, Strings.PluginUpdateNoAssetFound, Name, Version, newVersion));

return (Version.Major == newPluginVersion.Major && Version.Minor == newPluginVersion.Minor && Version.Build == newPluginVersion.Build) || asfVersion != Assembly.GetExecutingAssembly().GetName().Version
? Task.FromResult(release)
: Task.FromResult((ReleaseAsset?) null);
return null;
}

ASF.ArchiLogger.LogGenericInfo(string.Format(CultureInfo.CurrentCulture, Strings.PluginUpdateFound, Name, Version, newVersion));

return asset.DownloadURL;
}

//public Task<ReleaseAsset?> GetTargetReleaseAsset(Version asfVersion, string asfVariant, Version newPluginVersion, IReadOnlyCollection<ReleaseAsset> releaseAssets) {
// ArgumentNullException.ThrowIfNull(asfVersion);
// ArgumentException.ThrowIfNullOrEmpty(asfVariant);
// ArgumentNullException.ThrowIfNull(newPluginVersion);

// if ((releaseAssets == null) || (releaseAssets.Count == 0)) {
// throw new ArgumentNullException(nameof(releaseAssets));
// }

// Collection<ReleaseAsset?> matches = [.. releaseAssets.Where(r => r.Name.Equals(Name + ".zip", StringComparison.OrdinalIgnoreCase))];

// if (matches.Count != 1) {
// return Task.FromResult((ReleaseAsset?) null);
// }

// ReleaseAsset? release = matches[0];
// Version? currentASFVersion = Assembly.GetEntryAssembly()?.GetName().Version;

// ASF.ArchiLogger.LogGenericInfo(string.Format(CultureInfo.CurrentCulture, "Versions: {0}, {1}, {2}, {3}", JsonSerializer.Serialize(Version), JsonSerializer.Serialize(newPluginVersion), JsonSerializer.Serialize(asfVersion), JsonSerializer.Serialize(currentASFVersion)));

// if (currentASFVersion == null) {
// ASF.ArchiLogger.LogGenericWarning("Unable to check ASF version");
// return Task.FromResult((ReleaseAsset?) null);
// }

// if ((Version.Major == newPluginVersion.Major && Version.Minor == newPluginVersion.Minor && Version.Build == newPluginVersion.Build) || asfVersion != currentASFVersion) {
// return Task.FromResult(release);
// } else {
// ASF.ArchiLogger.LogGenericInfo(string.Format(CultureInfo.CurrentCulture, "New plugin version {0} is only compatible with latest ASF version", newPluginVersion));
// return Task.FromResult((ReleaseAsset?) null);
// }
//}

public Task OnBotInitModules(Bot bot, IReadOnlyDictionary<string, JsonElement>? additionalConfigProperties) {
ArgumentNullException.ThrowIfNull(bot);

Expand Down

0 comments on commit d06c215

Please sign in to comment.