This commit is contained in:
Archi
2024-01-24 12:43:36 +01:00
parent 2a2d4f09f1
commit 05c5a7fc30
6 changed files with 70 additions and 50 deletions

View File

@@ -105,13 +105,13 @@ public static class ASF {
};
}
internal static async Task Init() {
internal static async Task<bool> 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) {

View File

@@ -103,7 +103,7 @@ internal sealed class Startup {
Dictionary<string, string> pluginPaths = new(StringComparer.Ordinal);
if (PluginsCore.ActivePlugins?.Count > 0) {
if (PluginsCore.ActivePlugins.Count > 0) {
foreach (IWebInterface plugin in PluginsCore.ActivePlugins.OfType<IWebInterface>()) {
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<Assembly>? assemblies = PluginsCore.LoadAssemblies();
if (assemblies != null) {

View File

@@ -1220,5 +1220,11 @@ namespace ArchiSteamFarm.Localization {
return ResourceManager.GetString("WarningRegionRestrictedPackage", resourceCulture);
}
}
public static string WarningUnsupportedOfficialPlugins {
get {
return ResourceManager.GetString("WarningUnsupportedOfficialPlugins", resourceCulture);
}
}
}
}

View File

@@ -752,4 +752,8 @@ Process uptime: {1}</value>
<value>ASF is unable to play app {0} as it has region-related restriction for {1} country that lasts until {2}.</value>
<comment>{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).</comment>
</data>
<data name="WarningUnsupportedOfficialPlugins" xml:space="preserve">
<value>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.</value>
<comment>{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.</comment>
</data>
</root>

View File

@@ -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<IPlugin>? ActivePlugins { get; private set; }
internal static FrozenSet<IPlugin> ActivePlugins { get; private set; } = FrozenSet<IPlugin>.Empty;
[PublicAPI]
public static async Task<ICrossProcessSemaphore> 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<StringComparer> GetBotsComparer() {
if (ActivePlugins == null) {
if (ActivePlugins.Count == 0) {
return StringComparer.Ordinal;
}
@@ -113,7 +113,7 @@ public static class PluginsCore {
internal static async Task<uint> 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<IMachineInfoProvider?> 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<bool> InitPlugins() {
if (ActivePlugins.Count > 0) {
throw new InvalidOperationException(nameof(ActivePlugins));
}
HashSet<Assembly>? 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<Assembly>? LoadAssemblies() {
@@ -273,7 +287,7 @@ public static class PluginsCore {
}
internal static async Task OnASFInitModules(IReadOnlyDictionary<string, JToken>? 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<string, JToken>? 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<HashSet<ClientMsgHandler>?> 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;
}

View File

@@ -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<byte> ShutdownResetEvent = new();
private static readonly FrozenSet<PosixSignal> SupportedPosixSignals = new HashSet<PosixSignal>(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<bool> InitCore(IReadOnlyCollection<string>? args) {