From e79e8de6dc1bee62a441e1537be459c22d081fb5 Mon Sep 17 00:00:00 2001 From: Liza Carvelli Date: Sat, 25 Feb 2023 00:55:48 +0100 Subject: [PATCH] Use ISubCommand for subcommands --- Pal.Client/Certificate.pfx | Bin 0 -> 1750 bytes Pal.Client/Commands/ISubCommand.cs | 10 ++++ Pal.Client/Commands/PalConfigCommand.cs | 15 ++++-- Pal.Client/Commands/PalNearCommand.cs | 28 ++++------ Pal.Client/Commands/PalStatsCommand.cs | 11 +++- .../Commands/PalTestConnectionCommand.cs | 15 ++++-- Pal.Client/DependencyContextInitializer.cs | 5 +- Pal.Client/DependencyInjectionContext.cs | 8 +-- Pal.Client/Plugin.cs | 50 ++++++++---------- Pal.Client/Secrets.cs | Bin 0 -> 204 bytes codesigning.pfx | Bin 0 -> 1676 bytes 11 files changed, 79 insertions(+), 63 deletions(-) create mode 100644 Pal.Client/Certificate.pfx create mode 100644 Pal.Client/Commands/ISubCommand.cs create mode 100644 Pal.Client/Secrets.cs create mode 100644 codesigning.pfx diff --git a/Pal.Client/Certificate.pfx b/Pal.Client/Certificate.pfx new file mode 100644 index 0000000000000000000000000000000000000000..8fbc22dc9cb1305721c922acfb82f7580837757b GIT binary patch literal 1750 zcmV;{1}XUfM@dveQdv+`0MMfd=-|bB2>uOM?g5R+g1cUVJRF&V_~Ips{_WVa^veg~ zktZd|K;M^YA3q`E5}A@gS{*j7pz6hMBx0c={ANCiv)*9BM&47vL}80>;{<0IQ2E1G zA1zfJF~b;MmT7!BNdZw*(Aq05quXz#--7%9q%U6!($TpJ`2!t#g1{GPeWJ3E3JhX3 z2YpnJ+O<+Wu6lmL;T2dP6lm~zRRE+$b+W&mYP{&Ubksa;YJECB=nGm_KR8N1W0C^l zw&hX>d+&-%qIz3)Y3*IBwGT$|GvD4;dJS)vJG)(bv~>jgXZLHrN4wuU2>G`*2xF0aftUAW>;I|bgj1P%ULJBXf-SG8vomAh>HX3eYt{r zC5G}mm8zaTN6HNEUSJjvvH*q%hX;&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8MrFJh-_19r~p3cs|Ko3 za;x38R|aLoDo0OmL=R39p}S9y&hq={m@ssL&hy+9x-eu`O>_K(ZCG0P}pfOaMextaaJkeAk_yte#oS4U%ZW z!b+fJ!-zRBy>p7FN68vZRX> GetHandlers(); + } +} diff --git a/Pal.Client/Commands/PalConfigCommand.cs b/Pal.Client/Commands/PalConfigCommand.cs index 296392f..74218e1 100644 --- a/Pal.Client/Commands/PalConfigCommand.cs +++ b/Pal.Client/Commands/PalConfigCommand.cs @@ -1,10 +1,11 @@ -using Dalamud.Interface.Windowing; +using System; +using System.Collections.Generic; using Pal.Client.Configuration; using Pal.Client.Windows; namespace Pal.Client.Commands { - internal class PalConfigCommand + internal class PalConfigCommand : ISubCommand { private readonly IPalacePalConfiguration _configuration; private readonly AgreementWindow _agreementWindow; @@ -20,7 +21,15 @@ namespace Pal.Client.Commands _configWindow = configWindow; } - public void Execute() + + public IReadOnlyDictionary> GetHandlers() + => new Dictionary> + { + { "config", _ => Execute() }, + { "", _ => Execute() } + }; + + private void Execute() { if (_configuration.FirstUse) _agreementWindow.IsOpen = true; diff --git a/Pal.Client/Commands/PalNearCommand.cs b/Pal.Client/Commands/PalNearCommand.cs index 53e9b8a..4649bb5 100644 --- a/Pal.Client/Commands/PalNearCommand.cs +++ b/Pal.Client/Commands/PalNearCommand.cs @@ -1,4 +1,5 @@ using System; +using System.Collections.Generic; using System.Linq; using Dalamud.Game.ClientState; using Pal.Client.DependencyInjection; @@ -8,7 +9,7 @@ using Pal.Client.Rendering; namespace Pal.Client.Commands { - internal sealed class PalNearCommand + internal sealed class PalNearCommand : ISubCommand { private readonly Chat _chat; private readonly ClientState _clientState; @@ -24,23 +25,14 @@ namespace Pal.Client.Commands _floorService = floorService; } - public void Execute(string arguments) - { - switch (arguments) + + public IReadOnlyDictionary> GetHandlers() + => new Dictionary> { - default: - DebugNearest(_ => true); - break; - - case "tnear": - DebugNearest(m => m.Type == MemoryLocation.EType.Trap); - break; - - case "hnear": - DebugNearest(m => m.Type == MemoryLocation.EType.Hoard); - break; - } - } + { "near", _ => DebugNearest(_ => true) }, + { "tnear", _ => DebugNearest(m => m.Type == MemoryLocation.EType.Trap) }, + { "hnear", _ => DebugNearest(m => m.Type == MemoryLocation.EType.Hoard) }, + }; private void DebugNearest(Predicate predicate) { @@ -54,7 +46,7 @@ namespace Pal.Client.Commands var playerPosition = _clientState.LocalPlayer?.Position; if (playerPosition == null) return; - _chat.Message($"{playerPosition}"); + _chat.Message($"Your position: {playerPosition}"); var nearbyMarkers = state.Locations .Where(m => predicate(m)) diff --git a/Pal.Client/Commands/PalStatsCommand.cs b/Pal.Client/Commands/PalStatsCommand.cs index 917efcc..366f97f 100644 --- a/Pal.Client/Commands/PalStatsCommand.cs +++ b/Pal.Client/Commands/PalStatsCommand.cs @@ -1,9 +1,10 @@ using System; +using System.Collections.Generic; using Pal.Client.DependencyInjection; namespace Pal.Client.Commands { - internal sealed class PalStatsCommand + internal sealed class PalStatsCommand : ISubCommand { private readonly StatisticsService _statisticsService; @@ -12,7 +13,13 @@ namespace Pal.Client.Commands _statisticsService = statisticsService; } - public void Execute() + public IReadOnlyDictionary> GetHandlers() + => new Dictionary> + { + { "stats", _ => Execute() }, + }; + + private void Execute() => _statisticsService.ShowGlobalStatistics(); } } diff --git a/Pal.Client/Commands/PalTestConnectionCommand.cs b/Pal.Client/Commands/PalTestConnectionCommand.cs index 174330c..0ad6e41 100644 --- a/Pal.Client/Commands/PalTestConnectionCommand.cs +++ b/Pal.Client/Commands/PalTestConnectionCommand.cs @@ -1,9 +1,11 @@ -using ECommons.Schedulers; +using System; +using System.Collections.Generic; +using ECommons.Schedulers; using Pal.Client.Windows; namespace Pal.Client.Commands { - internal sealed class PalTestConnectionCommand + internal sealed class PalTestConnectionCommand : ISubCommand { private readonly ConfigWindow _configWindow; @@ -12,7 +14,14 @@ namespace Pal.Client.Commands _configWindow = configWindow; } - public void Execute() + public IReadOnlyDictionary> GetHandlers() + => new Dictionary> + { + { "test-connection", _ => Execute() }, + { "tc", _ => Execute() }, + }; + + private void Execute() { var _ = new TickScheduler(() => _configWindow.TestConnection()); } diff --git a/Pal.Client/DependencyContextInitializer.cs b/Pal.Client/DependencyContextInitializer.cs index 8308f3f..58b6f52 100644 --- a/Pal.Client/DependencyContextInitializer.cs +++ b/Pal.Client/DependencyContextInitializer.cs @@ -69,10 +69,7 @@ namespace Pal.Client _serviceProvider.GetRequiredService(); // eager load any commands to find errors now, not when running them - _serviceProvider.GetRequiredService(); - _serviceProvider.GetRequiredService(); - _serviceProvider.GetRequiredService(); - _serviceProvider.GetRequiredService(); + _serviceProvider.GetRequiredService>(); cancellationToken.ThrowIfCancellationRequested(); diff --git a/Pal.Client/DependencyInjectionContext.cs b/Pal.Client/DependencyInjectionContext.cs index 918dbe2..9632c3b 100644 --- a/Pal.Client/DependencyInjectionContext.cs +++ b/Pal.Client/DependencyInjectionContext.cs @@ -113,10 +113,10 @@ namespace Pal.Client _serviceCollection.AddTransient(); // commands - _serviceCollection.AddScoped(); - _serviceCollection.AddScoped(); - _serviceCollection.AddScoped(); - _serviceCollection.AddScoped(); + _serviceCollection.AddScoped(); + _serviceCollection.AddScoped(); + _serviceCollection.AddScoped(); + _serviceCollection.AddScoped(); // territory & marker related services _serviceCollection.AddScoped(); diff --git a/Pal.Client/Plugin.cs b/Pal.Client/Plugin.cs index e1409dd..7a064b7 100644 --- a/Pal.Client/Plugin.cs +++ b/Pal.Client/Plugin.cs @@ -2,6 +2,7 @@ using Dalamud.Plugin; using Pal.Client.Rendering; using System; +using System.Collections.Generic; using System.Globalization; using System.Linq; using System.Threading; @@ -165,45 +166,36 @@ namespace Pal.Client return; } - var sp = rootScope.ServiceProvider; - - switch (arguments) - { - case "": - case "config": - sp.GetRequiredService().Execute(); - break; - - case "stats": - sp.GetRequiredService().Execute(); - break; - - case "tc": - case "test-connection": - sp.GetRequiredService().Execute(); - break; - - case "near": - case "tnear": - case "hnear": - sp.GetRequiredService().Execute(arguments); - break; - - default: - chat.Error(string.Format(Localization.Command_pal_UnknownSubcommand, arguments, + Action commandHandler = rootScope.ServiceProvider + .GetRequiredService>() + .SelectMany(cmd => cmd.GetHandlers()) + .Where(cmd => cmd.Key == arguments.ToLowerInvariant()) + .Select(cmd => cmd.Value) + .SingleOrDefault(missingCommand => + { + chat.Error(string.Format(Localization.Command_pal_UnknownSubcommand, missingCommand, command)); - break; - } + }); + commandHandler.Invoke(arguments); } catch (Exception e) { + _logger.LogError(e, "Could not execute command '{Command}' with arguments '{Arguments}'", command, + arguments); chat.Error(e.ToString()); } }); } private void OpenConfigUi() - => _rootScope!.ServiceProvider.GetRequiredService().Execute(); + { + _rootScope!.ServiceProvider.GetRequiredService>() + .SelectMany(cmd => cmd.GetHandlers()) + .Where(cmd => cmd.Key == "config") + .Select(cmd => cmd.Value) + .Single() + .Invoke("config"); + } private void LanguageChanged(string languageCode) { diff --git a/Pal.Client/Secrets.cs b/Pal.Client/Secrets.cs new file mode 100644 index 0000000000000000000000000000000000000000..4f294d837ef3aeb7735fd32e42fbbd648b1eeb80 GIT binary patch literal 204 zcmV;-05kspM@dveQdv+`0F@(MkP#Pl18u5l(2&QD$Ced=xjZ<@PZ`-uRaE%RXtqrG zWYv?8Mi#ORtpOFpD37EbpRJil)5&^RdcO(?JNSCOI(N3siJ0okjivt4hsfn1)4P!Gj-j)I4c(0yc#hr%S2?X(W3oH?rVVHu&J* zCe|%RI<|=VJVb)H{Ecbaw8(`G(1t+SxmLP(a-8q8=3u`TEDV-Pq!sWo7#olw*S`Rb ztJmyz{D|a5gBc)R6js|I5^JX|=Jw43Z5{!L82`1G=fA`FAzoX3RPPz$_LekdlVu!P z9Hg+!1)$u*K3-Zk)2}${ipTI2nCtzOdwAi?P`8$2KW||9p=e8;Bs1pMX}8w9D%OGw)OQHr?x-aURU2>WLNiMA?U+QT81@nFR+MB zlWf8h0D5)QdnP3x#{PNDu)}(Ub;OtuUdr)NCpZgvUA=eo)t=!CvS|!s{&UeS#pAeh zv7?%%s{DIVR*3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