From 82d48ed222bfc9b3fa40651e80747b2c9ab92e34 Mon Sep 17 00:00:00 2001 From: Citrinate Date: Sun, 3 Mar 2024 10:56:04 -0500 Subject: [PATCH] Added support for ASFEnhance features --- FreePackages/AdapterBridge.cs | 57 +++++++++++++++++++++++++++++++++++ FreePackages/Commands.cs | 3 ++ FreePackages/FreePackages.cs | 20 +++++++++++- 3 files changed, 79 insertions(+), 1 deletion(-) create mode 100644 FreePackages/AdapterBridge.cs diff --git a/FreePackages/AdapterBridge.cs b/FreePackages/AdapterBridge.cs new file mode 100644 index 0000000..09950d4 --- /dev/null +++ b/FreePackages/AdapterBridge.cs @@ -0,0 +1,57 @@ +using System; +using System.Reflection; +using ArchiSteamFarm.Core; + +// ASFEnhanced Adapter https://github.com/chr233/ASFEnhanceAdapterDemoPlugin + +namespace FreePackages; +internal static class AdapterBridge +{ + /// + /// 注册子模块 + /// + /// 插件名称 + /// 插件唯一标识符 + /// 命令前缀 + /// 自动更新仓库 + /// 命令处理函数 + /// + public static bool InitAdapter(string pluginName, string pluginId, string? cmdPrefix, string? repoName, MethodInfo? cmdHandler) + { + try + { + var adapterEndpoint = Assembly.Load("ASFEnhance").GetType("ASFEnhance._Adapter_.Endpoint"); + var registerModule = adapterEndpoint?.GetMethod("RegisterModule", BindingFlags.Static | BindingFlags.Public); + var pluinVersion = Assembly.GetExecutingAssembly().GetName().Version; + + if (registerModule != null && adapterEndpoint != null) + { + var result = registerModule?.Invoke(null, new object?[] { pluginName, pluginId, cmdPrefix, repoName, pluinVersion, cmdHandler }); + + if (result is string str) + { + if (str == pluginName) + { + return true; + } + else + { + ASF.ArchiLogger.LogGenericWarning(str); + } + } + } + } +#if DEBUG + catch (Exception ex) + { + ASF.ArchiLogger.LogGenericException(ex, "Community with ASFEnhance failed"); + } +#else + catch (Exception) + { + ASF.ArchiLogger.LogGenericDebug("Community with ASFEnhance failed"); + } +#endif + return false; + } +} diff --git a/FreePackages/Commands.cs b/FreePackages/Commands.cs index b4ef86d..52d32ec 100644 --- a/FreePackages/Commands.cs +++ b/FreePackages/Commands.cs @@ -22,6 +22,9 @@ internal static class Commands { switch (args.Length) { case 1: switch (args[0].ToUpperInvariant()) { + case "FREEPACKAGES" when access >= EAccess.FamilySharing: + return String.Format("{0} {1}", nameof(FreePackages), (typeof(FreePackages).Assembly.GetName().Version ?? new Version("0")).ToString()); + case "CLEARFREEPACKAGESQUEUE": return ResponseClearQueue(bot, access); diff --git a/FreePackages/FreePackages.cs b/FreePackages/FreePackages.cs index 9a11bb4..30756d0 100644 --- a/FreePackages/FreePackages.cs +++ b/FreePackages/FreePackages.cs @@ -8,6 +8,7 @@ using SteamKit2; using System.Text.Json; using ArchiSteamFarm.Helpers.Json; +using System.Reflection; namespace FreePackages { [Export(typeof(IPlugin))] @@ -15,13 +16,30 @@ public sealed class FreePackages : IASF, IBotModules, ISteamPICSChanges, IBotSte public string Name => nameof(FreePackages); public Version Version => typeof(FreePackages).Assembly.GetName().Version ?? new Version("0"); internal static GlobalCache? GlobalCache; + private bool ASFEnhanceEnabled = false; public Task OnLoaded() { ASF.ArchiLogger.LogGenericInfo("Free Packages ASF Plugin by Citrinate"); + + // ASFEnhanced Adapter https://github.com/chr233/ASFEnhanceAdapterDemoPlugin + var flag = BindingFlags.Static | BindingFlags.Public | BindingFlags.NonPublic; + var handler = typeof(Commands).GetMethod(nameof(Commands.Response), flag); + const string pluginId = nameof(FreePackages); + const string cmdPrefix = "FREEPACKAGES"; + const string repoName = "Citrinate/FreePackages"; + var registered = AdapterBridge.InitAdapter(Name, pluginId, cmdPrefix, repoName, handler); + ASFEnhanceEnabled = registered; + return Task.CompletedTask; } - public Task OnBotCommand(Bot bot, EAccess access, string message, string[] args, ulong steamID = 0) => Task.FromResult(Commands.Response(bot, access, steamID, message, args)); + public Task OnBotCommand(Bot bot, EAccess access, string message, string[] args, ulong steamID = 0) { + if (ASFEnhanceEnabled) { + return Task.FromResult(null); + } + + return Task.FromResult(Commands.Response(bot, access, steamID, message, args)); + } public async Task OnASFInit(IReadOnlyDictionary? additionalConfigProperties = null) { if (GlobalCache == null) {