From 0e5490cc3adea2e684230a5600134b9d9119dee2 Mon Sep 17 00:00:00 2001 From: JustArchi Date: Fri, 8 Jul 2022 19:11:27 +0200 Subject: [PATCH] Allow plugin creators to initialize their own limiters --- ArchiSteamFarm/Plugins/PluginsCore.cs | 50 ++++++++++++++------------- 1 file changed, 26 insertions(+), 24 deletions(-) diff --git a/ArchiSteamFarm/Plugins/PluginsCore.cs b/ArchiSteamFarm/Plugins/PluginsCore.cs index 545a1726a..64d00013c 100644 --- a/ArchiSteamFarm/Plugins/PluginsCore.cs +++ b/ArchiSteamFarm/Plugins/PluginsCore.cs @@ -40,6 +40,7 @@ using ArchiSteamFarm.Steam; using ArchiSteamFarm.Steam.Data; using ArchiSteamFarm.Steam.Exchange; using ArchiSteamFarm.Steam.Integration.Callbacks; +using JetBrains.Annotations; using Newtonsoft.Json.Linq; using SteamKit2; @@ -51,6 +52,31 @@ internal static class PluginsCore { [ImportMany] internal static ImmutableHashSet? ActivePlugins { get; private set; } + [PublicAPI] + public static async Task GetCrossProcessSemaphore(string objectName) { + if (string.IsNullOrEmpty(objectName)) { + throw new ArgumentNullException(nameof(objectName)); + } + + string resourceName = OS.GetOsResourceName(objectName); + + if ((ActivePlugins == null) || (ActivePlugins.Count == 0)) { + return new CrossProcessFileBasedSemaphore(resourceName); + } + + IList responses; + + try { + responses = await Utilities.InParallel(ActivePlugins.OfType().Select(plugin => plugin.GetCrossProcessSemaphore(resourceName))).ConfigureAwait(false); + } catch (Exception e) { + ASF.ArchiLogger.LogGenericException(e); + + return new CrossProcessFileBasedSemaphore(resourceName); + } + + return responses.FirstOrDefault(static response => response != null) ?? new CrossProcessFileBasedSemaphore(resourceName); + } + internal static async Task GetBotsComparer() { if (ActivePlugins == null) { return StringComparer.Ordinal; @@ -95,30 +121,6 @@ internal static class PluginsCore { return lastChangeNumber; } - internal static async Task GetCrossProcessSemaphore(string objectName) { - if (string.IsNullOrEmpty(objectName)) { - throw new ArgumentNullException(nameof(objectName)); - } - - string resourceName = OS.GetOsResourceName(objectName); - - if ((ActivePlugins == null) || (ActivePlugins.Count == 0)) { - return new CrossProcessFileBasedSemaphore(resourceName); - } - - IList responses; - - try { - responses = await Utilities.InParallel(ActivePlugins.OfType().Select(plugin => plugin.GetCrossProcessSemaphore(resourceName))).ConfigureAwait(false); - } catch (Exception e) { - ASF.ArchiLogger.LogGenericException(e); - - return new CrossProcessFileBasedSemaphore(resourceName); - } - - return responses.FirstOrDefault(static response => response != null) ?? new CrossProcessFileBasedSemaphore(resourceName); - } - internal static async Task GetCustomMachineInfoProvider() { if (ActivePlugins == null) { return null;