Skip to content

Commit

Permalink
Prevent official plugins from updating to unmatched versions
Browse files Browse the repository at this point in the history
  • Loading branch information
JustArchi committed Apr 21, 2024
1 parent 1a732bb commit efb7262
Show file tree
Hide file tree
Showing 3 changed files with 60 additions and 4 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,7 @@ namespace ArchiSteamFarm.OfficialPlugins.Monitoring;

[Export(typeof(IPlugin))]
[SuppressMessage("ReSharper", "MemberCanBeFileLocal")]
internal sealed class MonitoringPlugin : OfficialPlugin, IDisposable, IGitHubPluginUpdates, IWebInterface, IWebServiceProvider {
internal sealed class MonitoringPlugin : OfficialPlugin, IDisposable, IOfficialGitHubPluginUpdates, IWebInterface, IWebServiceProvider {
private const string MeterName = SharedInfo.AssemblyName;

private const string MetricNamePrefix = "asf";
Expand Down
17 changes: 14 additions & 3 deletions ArchiSteamFarm/Plugins/Interfaces/IGitHubPluginUpdates.cs
Original file line number Diff line number Diff line change
Expand Up @@ -114,24 +114,35 @@ public interface IGitHubPluginUpdates : IPluginUpdates {
throw new ArgumentNullException(nameof(releaseAssets));
}

return Task.FromResult(FindPossibleMatch(asfVersion, newPluginVersion, releaseAssets));
}

protected ReleaseAsset? FindPossibleMatch(Version asfVersion, Version newPluginVersion, IReadOnlyCollection<ReleaseAsset> releaseAssets) {
ArgumentNullException.ThrowIfNull(asfVersion);
ArgumentNullException.ThrowIfNull(newPluginVersion);

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

Dictionary<string, ReleaseAsset> assetsByName = releaseAssets.ToDictionary(static asset => asset.Name, StringComparer.OrdinalIgnoreCase);

foreach (string possibleMatch in GetPossibleMatches(asfVersion)) {
if (assetsByName.TryGetValue(possibleMatch, out ReleaseAsset? targetAsset)) {
return Task.FromResult<ReleaseAsset?>(targetAsset);
return targetAsset;
}
}

// The very last fallback in case user uses different naming scheme
HashSet<ReleaseAsset> zipAssets = releaseAssets.Where(static asset => asset.Name.EndsWith(".zip", StringComparison.OrdinalIgnoreCase)).ToHashSet();

if (zipAssets.Count == 1) {
return Task.FromResult<ReleaseAsset?>(zipAssets.First());
return zipAssets.First();
}

ASF.ArchiLogger.LogGenericWarning(string.Format(CultureInfo.CurrentCulture, Strings.PluginUpdateConflictingAssetsFound, Name, Version, newPluginVersion));

return Task.FromResult<ReleaseAsset?>(null);
return null;
}

protected async Task<Uri?> GetTargetReleaseURL(Version asfVersion, string asfVariant, bool asfUpdate, bool stable, bool forced) {
Expand Down
45 changes: 45 additions & 0 deletions ArchiSteamFarm/Plugins/Interfaces/IOfficialGitHubPluginUpdates.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
// ----------------------------------------------------------------------------------------------
// _ _ _ ____ _ _____
// / \ _ __ ___ | |__ (_)/ ___| | |_ ___ __ _ _ __ ___ | ___|__ _ _ __ _ __ ___
// / _ \ | '__|/ __|| '_ \ | |\___ \ | __|/ _ \ / _` || '_ ` _ \ | |_ / _` || '__|| '_ ` _ \
// / ___ \ | | | (__ | | | || | ___) || |_| __/| (_| || | | | | || _|| (_| || | | | | | | |
// /_/ \_\|_| \___||_| |_||_||____/ \__|\___| \__,_||_| |_| |_||_| \__,_||_| |_| |_| |_|
// ----------------------------------------------------------------------------------------------
// |
// Copyright 2015-2024 Łukasz "JustArchi" Domeradzki
// Contact: [email protected]
// |
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
// |
// http://www.apache.org/licenses/LICENSE-2.0
// |
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.

using System;
using System.Collections.Generic;
using System.Threading.Tasks;
using ArchiSteamFarm.Web.GitHub.Data;

namespace ArchiSteamFarm.Plugins.Interfaces;

internal interface IOfficialGitHubPluginUpdates : IGitHubPluginUpdates {
Task<ReleaseAsset?> IGitHubPluginUpdates.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));
}

// For official plugins, the ASF version must match the plugin version
// Refuse to find the match if that's not the case, otherwise fallback to default implementation
return Task.FromResult(asfVersion == newPluginVersion ? FindPossibleMatch(asfVersion, newPluginVersion, releaseAssets) : null);
}
}

0 comments on commit efb7262

Please sign in to comment.