From 05c5a7fc30e1161200fa224143cebdecdd53c601 Mon Sep 17 00:00:00 2001 From: Archi Date: Wed, 24 Jan 2024 12:43:36 +0100 Subject: [PATCH] Closes #3128 --- ArchiSteamFarm/Core/ASF.cs | 8 +- ArchiSteamFarm/IPC/Startup.cs | 4 +- .../Localization/Strings.Designer.cs | 6 ++ ArchiSteamFarm/Localization/Strings.resx | 4 + ArchiSteamFarm/Plugins/PluginsCore.cs | 88 +++++++++++-------- ArchiSteamFarm/Program.cs | 10 +-- 6 files changed, 70 insertions(+), 50 deletions(-) diff --git a/ArchiSteamFarm/Core/ASF.cs b/ArchiSteamFarm/Core/ASF.cs index dd55552a5..27dc33a91 100644 --- a/ArchiSteamFarm/Core/ASF.cs +++ b/ArchiSteamFarm/Core/ASF.cs @@ -105,13 +105,13 @@ public static class ASF { }; } - internal static async Task Init() { + internal static async Task Init() { if (GlobalConfig == null) { throw new InvalidOperationException(nameof(GlobalConfig)); } - if (!PluginsCore.InitPlugins()) { - await Task.Delay(SharedInfo.InformationDelay).ConfigureAwait(false); + if (!await PluginsCore.InitPlugins().ConfigureAwait(false)) { + return false; } WebBrowser = new WebBrowser(ArchiLogger, GlobalConfig.WebProxy, true); @@ -142,6 +142,8 @@ public static class ASF { if (Program.ConfigWatch) { InitConfigWatchEvents(); } + + return true; } internal static bool IsValidBotName(string botName) { diff --git a/ArchiSteamFarm/IPC/Startup.cs b/ArchiSteamFarm/IPC/Startup.cs index fbabebdef..9ca530820 100644 --- a/ArchiSteamFarm/IPC/Startup.cs +++ b/ArchiSteamFarm/IPC/Startup.cs @@ -103,7 +103,7 @@ internal sealed class Startup { Dictionary pluginPaths = new(StringComparer.Ordinal); - if (PluginsCore.ActivePlugins?.Count > 0) { + if (PluginsCore.ActivePlugins.Count > 0) { foreach (IWebInterface plugin in PluginsCore.ActivePlugins.OfType()) { if (string.IsNullOrEmpty(plugin.PhysicalPath) || string.IsNullOrEmpty(plugin.WebPath)) { // Invalid path provided @@ -317,7 +317,7 @@ internal sealed class Startup { IMvcBuilder mvc = services.AddControllers(); // Add support for controllers declared in custom plugins - if (PluginsCore.ActivePlugins?.Count > 0) { + if (PluginsCore.ActivePlugins.Count > 0) { HashSet? assemblies = PluginsCore.LoadAssemblies(); if (assemblies != null) { diff --git a/ArchiSteamFarm/Localization/Strings.Designer.cs b/ArchiSteamFarm/Localization/Strings.Designer.cs index bb479a250..84baf24b9 100644 --- a/ArchiSteamFarm/Localization/Strings.Designer.cs +++ b/ArchiSteamFarm/Localization/Strings.Designer.cs @@ -1220,5 +1220,11 @@ namespace ArchiSteamFarm.Localization { return ResourceManager.GetString("WarningRegionRestrictedPackage", resourceCulture); } } + + public static string WarningUnsupportedOfficialPlugins { + get { + return ResourceManager.GetString("WarningUnsupportedOfficialPlugins", resourceCulture); + } + } } } diff --git a/ArchiSteamFarm/Localization/Strings.resx b/ArchiSteamFarm/Localization/Strings.resx index 474e407c9..739be404c 100644 --- a/ArchiSteamFarm/Localization/Strings.resx +++ b/ArchiSteamFarm/Localization/Strings.resx @@ -752,4 +752,8 @@ Process uptime: {1} ASF is unable to play app {0} as it has region-related restriction for {1} country that lasts until {2}. {0} will be replaced by app ID (number), {1} will be replaced by short country code (string, such as "PL"), {2} will be replaced by human-readable date (string). + + You're attempting to run official {0} plugin in mismatched with ASF version: {1} (expected {2}). This suggests you're doing something horribly wrong, either fix your setup or supply --ignore-unsupported-environment argument if you really know what you're doing. + {0} will be replaced by plugin name, {1} will be replaced by plugin's version number, {2} will be replaced by ASF's version number. + diff --git a/ArchiSteamFarm/Plugins/PluginsCore.cs b/ArchiSteamFarm/Plugins/PluginsCore.cs index a6c7211bd..b99e6170d 100644 --- a/ArchiSteamFarm/Plugins/PluginsCore.cs +++ b/ArchiSteamFarm/Plugins/PluginsCore.cs @@ -49,10 +49,10 @@ using SteamKit2; namespace ArchiSteamFarm.Plugins; public static class PluginsCore { - internal static bool HasCustomPluginsLoaded => ActivePlugins?.Any(static plugin => plugin is not OfficialPlugin officialPlugin || !officialPlugin.HasSameVersion()) == true; + internal static bool HasCustomPluginsLoaded => ActivePlugins.Any(static plugin => plugin is not OfficialPlugin officialPlugin || !officialPlugin.HasSameVersion()); [ImportMany] - internal static FrozenSet? ActivePlugins { get; private set; } + internal static FrozenSet ActivePlugins { get; private set; } = FrozenSet.Empty; [PublicAPI] public static async Task GetCrossProcessSemaphore(string objectName) { @@ -73,7 +73,7 @@ public static class PluginsCore { string resourceName = OS.GetOsResourceName(objectName); - if ((ActivePlugins == null) || (ActivePlugins.Count == 0)) { + if (ActivePlugins.Count == 0) { return new CrossProcessFileBasedSemaphore(resourceName); } @@ -91,7 +91,7 @@ public static class PluginsCore { } internal static async Task GetBotsComparer() { - if (ActivePlugins == null) { + if (ActivePlugins.Count == 0) { return StringComparer.Ordinal; } @@ -113,7 +113,7 @@ public static class PluginsCore { internal static async Task GetChangeNumberToStartFrom() { uint lastChangeNumber = ASF.GlobalDatabase?.LastChangeNumber ?? 0; - if ((lastChangeNumber == 0) || (ActivePlugins == null)) { + if ((lastChangeNumber == 0) || (ActivePlugins.Count == 0)) { return lastChangeNumber; } @@ -137,7 +137,7 @@ public static class PluginsCore { internal static async Task GetCustomMachineInfoProvider(Bot bot) { ArgumentNullException.ThrowIfNull(bot); - if (ActivePlugins == null) { + if (ActivePlugins.Count == 0) { return null; } @@ -155,9 +155,9 @@ public static class PluginsCore { } [UnconditionalSuppressMessage("AssemblyLoadTrimming", "IL2026:RequiresUnreferencedCode", Justification = "We don't care about trimmed assemblies, as we need it to work only with the known (used) ones")] - internal static bool InitPlugins() { - if (ActivePlugins != null) { - return false; + internal static async Task InitPlugins() { + if (ActivePlugins.Count > 0) { + throw new InvalidOperationException(nameof(ActivePlugins)); } HashSet? assemblies = LoadAssemblies(); @@ -182,6 +182,8 @@ public static class PluginsCore { ASF.ArchiLogger.LogGenericError(string.Format(CultureInfo.CurrentCulture, Strings.WarningFailedWithError, assembly.FullName)); ASF.ArchiLogger.LogGenericException(e); + await Task.Delay(SharedInfo.InformationDelay).ConfigureAwait(false); + return false; } } @@ -200,6 +202,8 @@ public static class PluginsCore { } catch (Exception e) { ASF.ArchiLogger.LogGenericException(e); + await Task.Delay(SharedInfo.InformationDelay).ConfigureAwait(false); + return false; } @@ -211,11 +215,19 @@ public static class PluginsCore { foreach (IPlugin plugin in activePlugins) { try { - string pluginName = plugin.Name; + ASF.ArchiLogger.LogGenericInfo(string.Format(CultureInfo.CurrentCulture, Strings.PluginLoading, plugin.Name, plugin.Version)); - ASF.ArchiLogger.LogGenericInfo(string.Format(CultureInfo.CurrentCulture, Strings.PluginLoading, pluginName, plugin.Version)); - plugin.OnLoaded(); - ASF.ArchiLogger.LogGenericInfo(string.Format(CultureInfo.CurrentCulture, Strings.PluginLoaded, pluginName)); + if (plugin is OfficialPlugin officialPlugin && !officialPlugin.HasSameVersion()) { + ASF.ArchiLogger.LogGenericError(string.Format(CultureInfo.CurrentCulture, Strings.WarningUnsupportedOfficialPlugins, plugin.Name, plugin.Version, SharedInfo.Version)); + + await Task.Delay(SharedInfo.InformationDelay).ConfigureAwait(false); + + return false; + } + + await plugin.OnLoaded().ConfigureAwait(false); + + ASF.ArchiLogger.LogGenericInfo(string.Format(CultureInfo.CurrentCulture, Strings.PluginLoaded, plugin.Name)); } catch (Exception e) { ASF.ArchiLogger.LogGenericException(e); invalidPlugins.Add(plugin); @@ -223,10 +235,12 @@ public static class PluginsCore { } if (invalidPlugins.Count > 0) { + await Task.Delay(SharedInfo.InformationDelay).ConfigureAwait(false); + activePlugins.ExceptWith(invalidPlugins); if (activePlugins.Count == 0) { - return false; + return true; } } @@ -239,7 +253,7 @@ public static class PluginsCore { Console.Title = SharedInfo.ProgramIdentifier; } - return invalidPlugins.Count == 0; + return true; } internal static HashSet? LoadAssemblies() { @@ -273,7 +287,7 @@ public static class PluginsCore { } internal static async Task OnASFInitModules(IReadOnlyDictionary? additionalConfigProperties = null) { - if ((ActivePlugins == null) || (ActivePlugins.Count == 0)) { + if (ActivePlugins.Count == 0) { return; } @@ -297,7 +311,7 @@ public static class PluginsCore { throw new ArgumentNullException(nameof(args)); } - if ((ActivePlugins == null) || (ActivePlugins.Count == 0)) { + if (ActivePlugins.Count == 0) { return null; } @@ -317,7 +331,7 @@ public static class PluginsCore { internal static async Task OnBotDestroy(Bot bot) { ArgumentNullException.ThrowIfNull(bot); - if ((ActivePlugins == null) || (ActivePlugins.Count == 0)) { + if (ActivePlugins.Count == 0) { return; } @@ -331,7 +345,7 @@ public static class PluginsCore { internal static async Task OnBotDisconnected(Bot bot, EResult reason) { ArgumentNullException.ThrowIfNull(bot); - if ((ActivePlugins == null) || (ActivePlugins.Count == 0)) { + if (ActivePlugins.Count == 0) { return; } @@ -345,7 +359,7 @@ public static class PluginsCore { internal static async Task OnBotFarmingFinished(Bot bot, bool farmedSomething) { ArgumentNullException.ThrowIfNull(bot); - if ((ActivePlugins == null) || (ActivePlugins.Count == 0)) { + if (ActivePlugins.Count == 0) { return; } @@ -359,7 +373,7 @@ public static class PluginsCore { internal static async Task OnBotFarmingStarted(Bot bot) { ArgumentNullException.ThrowIfNull(bot); - if ((ActivePlugins == null) || (ActivePlugins.Count == 0)) { + if (ActivePlugins.Count == 0) { return; } @@ -373,7 +387,7 @@ public static class PluginsCore { internal static async Task OnBotFarmingStopped(Bot bot) { ArgumentNullException.ThrowIfNull(bot); - if ((ActivePlugins == null) || (ActivePlugins.Count == 0)) { + if (ActivePlugins.Count == 0) { return; } @@ -391,7 +405,7 @@ public static class PluginsCore { throw new ArgumentOutOfRangeException(nameof(steamID)); } - if ((ActivePlugins == null) || (ActivePlugins.Count == 0)) { + if (ActivePlugins.Count == 0) { return false; } @@ -411,7 +425,7 @@ public static class PluginsCore { internal static async Task OnBotInit(Bot bot) { ArgumentNullException.ThrowIfNull(bot); - if ((ActivePlugins == null) || (ActivePlugins.Count == 0)) { + if (ActivePlugins.Count == 0) { return; } @@ -425,7 +439,7 @@ public static class PluginsCore { internal static async Task OnBotInitModules(Bot bot, IReadOnlyDictionary? additionalConfigProperties = null) { ArgumentNullException.ThrowIfNull(bot); - if ((ActivePlugins == null) || (ActivePlugins.Count == 0)) { + if (ActivePlugins.Count == 0) { return; } @@ -439,7 +453,7 @@ public static class PluginsCore { internal static async Task OnBotLoggedOn(Bot bot) { ArgumentNullException.ThrowIfNull(bot); - if ((ActivePlugins == null) || (ActivePlugins.Count == 0)) { + if (ActivePlugins.Count == 0) { return; } @@ -459,7 +473,7 @@ public static class PluginsCore { ArgumentException.ThrowIfNullOrEmpty(message); - if ((ActivePlugins == null) || (ActivePlugins.Count == 0)) { + if (ActivePlugins.Count == 0) { return null; } @@ -480,7 +494,7 @@ public static class PluginsCore { ArgumentNullException.ThrowIfNull(bot); ArgumentNullException.ThrowIfNull(callbackManager); - if ((ActivePlugins == null) || (ActivePlugins.Count == 0)) { + if (ActivePlugins.Count == 0) { return; } @@ -494,7 +508,7 @@ public static class PluginsCore { internal static async Task?> OnBotSteamHandlersInit(Bot bot) { ArgumentNullException.ThrowIfNull(bot); - if ((ActivePlugins == null) || (ActivePlugins.Count == 0)) { + if (ActivePlugins.Count == 0) { return null; } @@ -515,7 +529,7 @@ public static class PluginsCore { ArgumentNullException.ThrowIfNull(bot); ArgumentNullException.ThrowIfNull(tradeOffer); - if ((ActivePlugins == null) || (ActivePlugins.Count == 0)) { + if (ActivePlugins.Count == 0) { return false; } @@ -539,7 +553,7 @@ public static class PluginsCore { throw new ArgumentNullException(nameof(tradeResults)); } - if ((ActivePlugins == null) || (ActivePlugins.Count == 0)) { + if (ActivePlugins.Count == 0) { return; } @@ -557,7 +571,7 @@ public static class PluginsCore { throw new ArgumentNullException(nameof(newNotifications)); } - if ((ActivePlugins == null) || (ActivePlugins.Count == 0)) { + if (ActivePlugins.Count == 0) { return; } @@ -573,7 +587,7 @@ public static class PluginsCore { ArgumentNullException.ThrowIfNull(appChanges); ArgumentNullException.ThrowIfNull(packageChanges); - if ((ActivePlugins == null) || (ActivePlugins.Count == 0)) { + if (ActivePlugins.Count == 0) { return; } @@ -587,7 +601,7 @@ public static class PluginsCore { internal static async Task OnPICSChangesRestart(uint currentChangeNumber) { ArgumentOutOfRangeException.ThrowIfZero(currentChangeNumber); - if ((ActivePlugins == null) || (ActivePlugins.Count == 0)) { + if (ActivePlugins.Count == 0) { return; } @@ -602,7 +616,7 @@ public static class PluginsCore { ArgumentNullException.ThrowIfNull(bot); ArgumentNullException.ThrowIfNull(data); - if ((ActivePlugins == null) || (ActivePlugins.Count == 0)) { + if (ActivePlugins.Count == 0) { return; } @@ -616,7 +630,7 @@ public static class PluginsCore { internal static async Task OnUpdateFinished(Version newVersion) { ArgumentNullException.ThrowIfNull(newVersion); - if ((ActivePlugins == null) || (ActivePlugins.Count == 0)) { + if (ActivePlugins.Count == 0) { return; } @@ -630,7 +644,7 @@ public static class PluginsCore { internal static async Task OnUpdateProceeding(Version newVersion) { ArgumentNullException.ThrowIfNull(newVersion); - if ((ActivePlugins == null) || (ActivePlugins.Count == 0)) { + if (ActivePlugins.Count == 0) { return; } diff --git a/ArchiSteamFarm/Program.cs b/ArchiSteamFarm/Program.cs index 4887f4b3c..5c484abc2 100644 --- a/ArchiSteamFarm/Program.cs +++ b/ArchiSteamFarm/Program.cs @@ -49,6 +49,7 @@ namespace ArchiSteamFarm; internal static class Program { internal static bool ConfigMigrate { get; private set; } = true; internal static bool ConfigWatch { get; private set; } = true; + internal static bool IgnoreUnsupportedEnvironment { get; private set; } internal static string? NetworkGroup { get; private set; } internal static bool ProcessRequired { get; private set; } internal static bool RestartAllowed { get; private set; } = true; @@ -60,7 +61,6 @@ internal static class Program { private static readonly TaskCompletionSource ShutdownResetEvent = new(); private static readonly FrozenSet SupportedPosixSignals = new HashSet(2) { PosixSignal.SIGINT, PosixSignal.SIGTERM }.ToFrozenSet(); - private static bool IgnoreUnsupportedEnvironment; private static bool InputCryptkeyManually; private static bool Minimized; private static bool SystemRequired; @@ -205,13 +205,7 @@ internal static class Program { OS.Init(ASF.GlobalConfig?.OptimizationMode ?? GlobalConfig.DefaultOptimizationMode); - if (!await InitGlobalDatabaseAndServices().ConfigureAwait(false)) { - return false; - } - - await ASF.Init().ConfigureAwait(false); - - return true; + return await InitGlobalDatabaseAndServices().ConfigureAwait(false) && await ASF.Init().ConfigureAwait(false); } private static async Task InitCore(IReadOnlyCollection? args) {