From 5747a321682b147db13200b69661337eb702f1dc Mon Sep 17 00:00:00 2001 From: Chr_ Date: Tue, 17 Oct 2023 17:40:11 +0800 Subject: [PATCH] =?UTF-8?q?update=20=E4=BF=AE=E6=94=B9Adapter=E6=8E=A5?= =?UTF-8?q?=E5=8F=A3?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- ASFEnhance/ASFEnhance.cs | 44 ++++++------- ASFEnhance/Other/Command.cs | 24 +++++-- ASFEnhance/_Adapter_/Endpoint.cs | 28 +++++--- .../_Adapter_/{Core.cs => ExtensionCore.cs} | 66 +++++++++++++------ .../{ExpansionUtils => _Adapter_}/Update.cs | 8 +-- 5 files changed, 108 insertions(+), 62 deletions(-) rename ASFEnhance/_Adapter_/{Core.cs => ExtensionCore.cs} (51%) rename ASFEnhance/{ExpansionUtils => _Adapter_}/Update.cs (93%) diff --git a/ASFEnhance/ASFEnhance.cs b/ASFEnhance/ASFEnhance.cs index 16dc6a25..49f470f1 100644 --- a/ASFEnhance/ASFEnhance.cs +++ b/ASFEnhance/ASFEnhance.cs @@ -1,7 +1,6 @@ using ArchiSteamFarm.Core; using ArchiSteamFarm.Plugins.Interfaces; using ArchiSteamFarm.Steam; -using ASFEnhance._Adapter_; using ASFEnhance.Data; using Newtonsoft.Json; using Newtonsoft.Json.Linq; @@ -38,13 +37,13 @@ public Task OnASFInit(IReadOnlyDictionary? additionalConfigPrope message.AppendLine(Langs.PluginInfo); message.AppendLine(Static.Line); - if (Core.SubModules.Any()) + if (_Adapter_.ExtensionCore.HasSubModule) { - message.AppendLineFormat("已加载 {0} 个外部模块", Core.SubModules.Count); + message.AppendLineFormat("已加载 {0} 个外部模块", _Adapter_.ExtensionCore.SubModules.Count); int index = 1; - foreach (var (_, subModule) in _Adapter_.Core.SubModules) + foreach (var (_, subModule) in _Adapter_.ExtensionCore.SubModules) { - message.AppendLineFormat("{0}: {1} {2}", index++, subModule.PluginName, subModule.PluginVersion); + message.AppendLineFormat("{0}: [{1,-4}] {2,-20} {3}", index++, subModule.CmdPrefix ?? "---", subModule.PluginName, subModule.PluginVersion); } } else @@ -178,6 +177,7 @@ public Task OnLoaded() /// /// /// + /// /// /// /// @@ -192,6 +192,11 @@ public Task OnLoaded() 0 => throw new InvalidOperationException(nameof(args)), 1 => cmd switch //不带参数 { + //Plugin Info + "ASFENHANCE" or + "ASFE" when access >= EAccess.FamilySharing => + Task.FromResult(PluginInfo), + //Event "SIM4" when access >= EAccess.Operator => Event.Command.ResponseSim4(bot), @@ -225,11 +230,6 @@ public Task OnLoaded() "CA" => bot.Commands.Response(access, "CART ASF", steamId), - //Plugin Info - "ASFENHANCE" or - "ASFE" when access >= EAccess.FamilySharing => - Task.FromResult(PluginInfo), - //Account "PURCHASEHISTORY" or "PH" when access >= EAccess.Operator => @@ -399,7 +399,7 @@ public Task OnLoaded() "ACCESSTOKEN" when access >= EAccess.Owner => Task.FromResult(Other.Command.ResponseDevFeatureUnavilable()), - _ => Task.FromResult(Other.Command.ShowUsageIfAvilable(args[0].ToUpperInvariant())), + _ => null, }, _ => cmd switch //带参数 { @@ -860,7 +860,7 @@ public Task OnLoaded() "ACCESSTOKEN" when Config.DevFeature && access >= EAccess.Owner => Task.FromResult(Other.Command.ResponseDevFeatureUnavilable()), - _ => Task.FromResult(Other.Command.ShowUsageIfAvilable(args[0].ToUpperInvariant())), + _ => null, } }; } @@ -886,6 +886,7 @@ public Task OnLoaded() try { var cmd = args[0].ToUpperInvariant(); + //跳过禁用命令 if (IsCmdDisabled(cmd) == true) { @@ -895,7 +896,7 @@ public Task OnLoaded() var splits = cmd.Split('.', 2, StringSplitOptions.RemoveEmptyEntries); - Task? task; + Task? task = null; if (splits.Length > 1) //指定插件名称 { @@ -906,23 +907,18 @@ public Task OnLoaded() { task = ResponseCommand(bot, access, cmd, message, args, steamId); } - else //调用外部模块命令 + else if (_Adapter_.ExtensionCore.HasSubModule) //调用外部模块命令 { - task = _Adapter_.Core.ExecuteCommand(pluginName, cmd, bot, access, message, args, steamId); + task = _Adapter_.ExtensionCore.ExecuteCommand(pluginName, cmd, bot, access, message, args, steamId); } } else //未指定插件名称 { task = ResponseCommand(bot, access, cmd, message, args, steamId); - //如果本插件未调用则调用外部插件命令 - if (task != null) + if (task == null && _Adapter_.ExtensionCore.HasSubModule) //如果本插件未调用则调用外部插件命令 { - var response = await task.ConfigureAwait(false); - if (string.IsNullOrEmpty(response)) - { - task = _Adapter_.Core.ExecuteCommand(cmd, bot, access, message, args, steamId); - } + task = _Adapter_.ExtensionCore.ExecuteCommand(cmd, bot, access, message, args, steamId); } } @@ -930,9 +926,9 @@ public Task OnLoaded() { return await task.ConfigureAwait(false); } - else + else //显示命令帮助 { - return null; + return Other.Command.ShowUsageIfAvilable(cmd); } } catch (Exception ex) diff --git a/ASFEnhance/Other/Command.cs b/ASFEnhance/Other/Command.cs index 027a8d9b..3c97fbd8 100644 --- a/ASFEnhance/Other/Command.cs +++ b/ASFEnhance/Other/Command.cs @@ -1,7 +1,9 @@ using ArchiSteamFarm.Plugins; using ArchiSteamFarm.Plugins.Interfaces; using ArchiSteamFarm.Steam; +using ASFEnhance.Data; using ASFEnhance.Explorer; +using System; using System.Collections.Immutable; using System.Text; using System.Text.RegularExpressions; @@ -253,18 +255,32 @@ internal static class Command if (activePlugins != null) { var sb = new StringBuilder(); - sb.AppendLine(FormatStaticResponse("已安装的插件")); + sb.AppendLine(FormatStaticResponse("已安装 {0} 个外部模块", activePlugins.Count)); - foreach (var x in activePlugins) + var subModules = new Dictionary(); + foreach (var subModule in _Adapter_.ExtensionCore.SubModules.Values) { - sb.AppendLineFormat("- {0} {1}", x.Name, x.Version); + subModules.TryAdd(subModule.PluginName, subModule); + } + + var index = 1; + foreach (var plugin in activePlugins) + { + if (subModules.TryGetValue(plugin.Name, out var subModule)) + { + sb.AppendLineFormat("{0}: [{1,-4}] {2,-20} {3} [ASFEnhance接入]", index++, subModule.CmdPrefix ?? "---", subModule.PluginName, subModule.PluginVersion); + } + else + { + sb.AppendLineFormat("{0}: {1,-20} {2}", index++, plugin.Name, plugin.Version); + } } return sb.ToString(); } else { - return FormatStaticResponse("未加载外部插件"); + return FormatStaticResponse("未加载外部模块"); } } } diff --git a/ASFEnhance/_Adapter_/Endpoint.cs b/ASFEnhance/_Adapter_/Endpoint.cs index 6bc456f2..36e6e333 100644 --- a/ASFEnhance/_Adapter_/Endpoint.cs +++ b/ASFEnhance/_Adapter_/Endpoint.cs @@ -2,6 +2,7 @@ using System.Reflection; namespace ASFEnhance._Adapter_; + /// /// 子模块接入点 /// @@ -10,20 +11,26 @@ public static class Endpoint /// /// 注册子模块 /// - /// - /// - /// - /// - /// + /// 插件名称 + /// 插件唯一标识符 + /// 命令前缀 + /// 自动更新仓库 + /// 版本 + /// 命令处理函数 /// /// - public static string RegisterModule(string pluginName, string? cmdPrefix, string? repoName, Version version, MethodInfo cmdHandler) + public static string RegisterModule(string pluginName, string pluginId, string? cmdPrefix, string? repoName, Version version, MethodInfo cmdHandler) { if (string.IsNullOrEmpty(pluginName)) { throw new ArgumentNullException(nameof(pluginName)); } + if (string.IsNullOrEmpty(pluginId)) + { + throw new ArgumentNullException(nameof(pluginId)); + } + if (version == null) { throw new ArgumentNullException(nameof(version)); @@ -50,8 +57,13 @@ public static string RegisterModule(string pluginName, string? cmdPrefix, string ParamList = paramList, }; - var pluginIdenty = pluginName.ToUpperInvariant(); - var success = Core.SubModules.TryAdd(pluginIdenty, subModule); + pluginId = pluginId.ToUpperInvariant(); + var success = ExtensionCore.SubModules.TryAdd(pluginId, subModule); + + if (!success) + { + ASFLogger.LogGenericWarning(string.Format("子模块 {0} 注册失败, 重复的 ID {1}", pluginName, pluginId)); + } return success ? pluginName : "注册失败, 重复的ID"; } diff --git a/ASFEnhance/_Adapter_/Core.cs b/ASFEnhance/_Adapter_/ExtensionCore.cs similarity index 51% rename from ASFEnhance/_Adapter_/Core.cs rename to ASFEnhance/_Adapter_/ExtensionCore.cs index 220eef69..0326d582 100644 --- a/ASFEnhance/_Adapter_/Core.cs +++ b/ASFEnhance/_Adapter_/ExtensionCore.cs @@ -2,11 +2,31 @@ using ASFEnhance.Data; namespace ASFEnhance._Adapter_; -public static class Core + +/// +/// 调用外部模块命令 +/// +public static class ExtensionCore { + /// + /// 子模块字典 + /// internal static Dictionary SubModules { get; } = new(); - private static Task? CallCommand(this SubModuleInfo subModule, Bot bot, EAccess access, string cmd, string message, string[] args, ulong steamId) + internal static bool HasSubModule => SubModules.Any(); + + /// + /// 调用子模块命令处理函数 + /// + /// + /// + /// + /// + /// + /// + /// + /// + private static Task? Invoke(this SubModuleInfo subModule, Bot bot, EAccess access, string cmd, string message, string[] args, ulong steamId) { var objList = new List(); foreach (var paramName in subModule.ParamList) @@ -51,17 +71,22 @@ public static class Core /// internal static Task? ExecuteCommand(string cmd, Bot bot, EAccess access, string message, string[] args, ulong steamId) { - if (SubModules.Count == 0) + foreach (var (pluginId, subModule) in SubModules) { - return null; - } - - foreach (var (_, subModule) in SubModules) - { - var response = subModule.CallCommand(bot, access, cmd, message, args, steamId); - if (response != null) + if (cmd == pluginId || (!string.IsNullOrEmpty(subModule.CmdPrefix) && subModule.CmdPrefix == cmd)) { - return response; + // 响应 Plugin Info 命令 + var pluginInfo = string.Format("{0} {1} [已接入 ASFEnhance]", subModule.PluginName, subModule.PluginVersion); + return Task.FromResult(pluginInfo); + } + else + { + // 响应命令 + var response = subModule.Invoke(bot, access, cmd, message, args, steamId); + if (response != null) + { + return response; + } } } @@ -81,17 +106,18 @@ public static class Core /// internal static Task? ExecuteCommand(string pluginName, string cmd, Bot bot, EAccess access, string message, string[] args, ulong steamId) { - if (SubModules.Count == 0) - { - return null; - } - - foreach (var (pluginIdenty, subModule) in SubModules) + foreach (var (pluginId, subModule) in SubModules) { - //PluginIdenty 和 CmdPrefix 匹配时响应命令 - if (pluginIdenty == pluginName || (!string.IsNullOrEmpty(subModule.CmdPrefix) && subModule.CmdPrefix == pluginName)) + if (cmd == pluginId || (!string.IsNullOrEmpty(subModule.CmdPrefix) && subModule.CmdPrefix == cmd)) + { + // 响应 Plugin Info 命令 + var pluginInfo = string.Format("{0} {1} [已接入 ASFEnhance]", subModule.PluginName, subModule.PluginVersion); + return Task.FromResult(pluginInfo); + } + else if (pluginId == pluginName || (!string.IsNullOrEmpty(subModule.CmdPrefix) && subModule.CmdPrefix == pluginName)) { - var response = subModule.CallCommand(bot, access, cmd, message, args, steamId); + //PluginIdenty 和 CmdPrefix 匹配时响应命令 + var response = subModule.Invoke(bot, access, cmd, message, args, steamId); if (response != null) { return response; diff --git a/ASFEnhance/ExpansionUtils/Update.cs b/ASFEnhance/_Adapter_/Update.cs similarity index 93% rename from ASFEnhance/ExpansionUtils/Update.cs rename to ASFEnhance/_Adapter_/Update.cs index 09fd491f..d4119ad7 100644 --- a/ASFEnhance/ExpansionUtils/Update.cs +++ b/ASFEnhance/_Adapter_/Update.cs @@ -2,12 +2,12 @@ using ArchiSteamFarm.Web.Responses; using ASFEnhance.Data; -namespace ASFEnhance.ExpansionUtils; +namespace ASFEnhance._Adapter_; /// /// 插件更新相关 /// -public static class Update +internal static class Update { /// /// 获取链接 @@ -34,9 +34,7 @@ public static Uri GetGitHubApiUri(string repo, bool useMirror) var response = await ASF.WebBrowser!.UrlGetToJsonObject(request).ConfigureAwait(false); if (response == null && useMirror) - { return await GetLatestRelease(repo, false).ConfigureAwait(false); - } return response?.Content; } @@ -49,9 +47,7 @@ public static Uri GetGitHubApiUri(string repo, bool useMirror) public static async Task DownloadRelease(string? downloadUrl) { if (string.IsNullOrEmpty(downloadUrl)) - { return null; - } var request = new Uri(downloadUrl); var response = await ASF.WebBrowser!.UrlGetToBinary(request).ConfigureAwait(false);