Skip to content

Commit

Permalink
update 修改Adapter接口
Browse files Browse the repository at this point in the history
  • Loading branch information
chr233 committed Oct 17, 2023
1 parent 3df31f5 commit 5747a32
Show file tree
Hide file tree
Showing 5 changed files with 108 additions and 62 deletions.
44 changes: 20 additions & 24 deletions ASFEnhance/ASFEnhance.cs
Original file line number Diff line number Diff line change
@@ -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;
Expand Down Expand Up @@ -38,13 +37,13 @@ public Task OnASFInit(IReadOnlyDictionary<string, JToken>? 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
Expand Down Expand Up @@ -178,6 +177,7 @@ public Task OnLoaded()
/// </summary>
/// <param name="bot"></param>
/// <param name="access"></param>
/// <param name="cmd"></param>
/// <param name="message"></param>
/// <param name="args"></param>
/// <param name="steamId"></param>
Expand All @@ -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),
Expand Down Expand Up @@ -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 =>
Expand Down Expand Up @@ -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 //带参数
{
Expand Down Expand Up @@ -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,
}
};
}
Expand All @@ -886,6 +886,7 @@ public Task OnLoaded()
try
{
var cmd = args[0].ToUpperInvariant();

//跳过禁用命令
if (IsCmdDisabled(cmd) == true)
{
Expand All @@ -895,7 +896,7 @@ public Task OnLoaded()

var splits = cmd.Split('.', 2, StringSplitOptions.RemoveEmptyEntries);

Task<string?>? task;
Task<string?>? task = null;

if (splits.Length > 1) //指定插件名称
{
Expand All @@ -906,33 +907,28 @@ 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);
}
}

if (task != null)
{
return await task.ConfigureAwait(false);
}
else
else //显示命令帮助
{
return null;
return Other.Command.ShowUsageIfAvilable(cmd);
}
}
catch (Exception ex)
Expand Down
24 changes: 20 additions & 4 deletions ASFEnhance/Other/Command.cs
Original file line number Diff line number Diff line change
@@ -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;
Expand Down Expand Up @@ -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<string, SubModuleInfo>();
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("未加载外部模块");
}
}
}
28 changes: 20 additions & 8 deletions ASFEnhance/_Adapter_/Endpoint.cs
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
using System.Reflection;

namespace ASFEnhance._Adapter_;

/// <summary>
/// 子模块接入点
/// </summary>
Expand All @@ -10,20 +11,26 @@ public static class Endpoint
/// <summary>
/// 注册子模块
/// </summary>
/// <param name="pluginName"></param>
/// <param name="cmdPrefix"></param>
/// <param name="repoName"></param>
/// <param name="version"></param>
/// <param name="cmdHandler"></param>
/// <param name="pluginName">插件名称</param>
/// <param name="pluginId">插件唯一标识符</param>
/// <param name="cmdPrefix">命令前缀</param>
/// <param name="repoName">自动更新仓库</param>
/// <param name="version">版本</param>
/// <param name="cmdHandler">命令处理函数</param>
/// <returns></returns>
/// <exception cref="ArgumentNullException"></exception>
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));
Expand All @@ -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";
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,31 @@
using ASFEnhance.Data;

namespace ASFEnhance._Adapter_;
public static class Core

/// <summary>
/// 调用外部模块命令
/// </summary>
public static class ExtensionCore
{
/// <summary>
/// 子模块字典
/// </summary>
internal static Dictionary<string, SubModuleInfo> SubModules { get; } = new();

private static Task<string?>? CallCommand(this SubModuleInfo subModule, Bot bot, EAccess access, string cmd, string message, string[] args, ulong steamId)
internal static bool HasSubModule => SubModules.Any();

/// <summary>
/// 调用子模块命令处理函数
/// </summary>
/// <param name="subModule"></param>
/// <param name="bot"></param>
/// <param name="access"></param>
/// <param name="cmd"></param>
/// <param name="message"></param>
/// <param name="args"></param>
/// <param name="steamId"></param>
/// <returns></returns>
private static Task<string?>? Invoke(this SubModuleInfo subModule, Bot bot, EAccess access, string cmd, string message, string[] args, ulong steamId)
{
var objList = new List<object?>();
foreach (var paramName in subModule.ParamList)
Expand Down Expand Up @@ -51,17 +71,22 @@ public static class Core
/// <returns></returns>
internal static Task<string?>? 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<string?>(pluginInfo);
}
else
{
// 响应命令
var response = subModule.Invoke(bot, access, cmd, message, args, steamId);
if (response != null)
{
return response;
}
}
}

Expand All @@ -81,17 +106,18 @@ public static class Core
/// <returns></returns>
internal static Task<string?>? 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<string?>(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;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,12 +2,12 @@
using ArchiSteamFarm.Web.Responses;
using ASFEnhance.Data;

namespace ASFEnhance.ExpansionUtils;
namespace ASFEnhance._Adapter_;

/// <summary>
/// 插件更新相关
/// </summary>
public static class Update
internal static class Update
{
/// <summary>
/// 获取链接
Expand All @@ -34,9 +34,7 @@ public static Uri GetGitHubApiUri(string repo, bool useMirror)
var response = await ASF.WebBrowser!.UrlGetToJsonObject<GitHubReleaseResponse>(request).ConfigureAwait(false);

if (response == null && useMirror)
{
return await GetLatestRelease(repo, false).ConfigureAwait(false);
}

return response?.Content;
}
Expand All @@ -49,9 +47,7 @@ public static Uri GetGitHubApiUri(string repo, bool useMirror)
public static async Task<BinaryResponse?> DownloadRelease(string? downloadUrl)
{
if (string.IsNullOrEmpty(downloadUrl))
{
return null;
}

var request = new Uri(downloadUrl);
var response = await ASF.WebBrowser!.UrlGetToBinary(request).ConfigureAwait(false);
Expand Down

0 comments on commit 5747a32

Please sign in to comment.