From 3f9ea9dbf183aaf4412e893edae670fa365aea59 Mon Sep 17 00:00:00 2001 From: Anna Clemens Date: Tue, 15 Feb 2022 16:57:15 -0500 Subject: [PATCH] refactor: rework the command system --- ChatTwo/Commands.cs | 76 ++++++++++++++++++++++++++++++++++++++++++ ChatTwo/Plugin.cs | 6 ++++ ChatTwo/Ui/ChatLog.cs | 39 +++++++++++++++++++--- ChatTwo/Ui/Settings.cs | 11 +++--- 4 files changed, 121 insertions(+), 11 deletions(-) create mode 100755 ChatTwo/Commands.cs diff --git a/ChatTwo/Commands.cs b/ChatTwo/Commands.cs new file mode 100755 index 0000000..363503f --- /dev/null +++ b/ChatTwo/Commands.cs @@ -0,0 +1,76 @@ +using Dalamud.Game.Command; +using Dalamud.Logging; + +namespace ChatTwo; + +internal sealed class Commands : IDisposable { + private Plugin Plugin { get; } + private Dictionary Registered { get; } = new(); + + internal Commands(Plugin plugin) { + this.Plugin = plugin; + } + + public void Dispose() { + foreach (var name in this.Registered.Keys) { + this.Plugin.CommandManager.RemoveHandler(name); + } + } + + internal void Initialise() { + foreach (var wrapper in this.Registered.Values) { + this.Plugin.CommandManager.AddHandler(wrapper.Name, new CommandInfo(this.Invoke) { + HelpMessage = wrapper.Description ?? string.Empty, + ShowInHelp = wrapper.ShowInHelp, + }); + } + } + + internal CommandWrapper Register(string name, string? description = null, bool? showInHelp = null) { + if (this.Registered.TryGetValue(name, out var wrapper)) { + if (description != null) { + wrapper.Description = description; + } + + if (showInHelp != null) { + wrapper.ShowInHelp = showInHelp.Value; + } + + return wrapper; + } + + this.Registered[name] = new CommandWrapper(name, description, showInHelp ?? true); + return this.Registered[name]; + } + + private void Invoke(string command, string arguments) { + if (!this.Registered.TryGetValue(command, out var wrapper)) { + PluginLog.Warning($"Missing registration for command {command}"); + return; + } + + try { + wrapper.Invoke(command, arguments); + } catch (Exception ex) { + PluginLog.Error(ex, $"Error while executing command {command}"); + } + } +} + +internal sealed class CommandWrapper { + internal string Name { get; } + internal string? Description { get; set; } + internal bool ShowInHelp { get; set; } + + internal event Action? Execute; + + internal CommandWrapper(string name, string? description, bool showInHelp) { + this.Name = name; + this.Description = description; + this.ShowInHelp = showInHelp; + } + + internal void Invoke(string command, string arguments) { + this.Execute?.Invoke(command, arguments); + } +} diff --git a/ChatTwo/Plugin.cs b/ChatTwo/Plugin.cs index 8daf3c2..1765526 100755 --- a/ChatTwo/Plugin.cs +++ b/ChatTwo/Plugin.cs @@ -59,6 +59,7 @@ public sealed class Plugin : IDalamudPlugin { internal TargetManager TargetManager { get; init; } internal Configuration Config { get; } + internal Commands Commands { get; } internal XivCommonBase Common { get; } internal TextureCache TextureCache { get; } internal GameFunctions.GameFunctions Functions { get; } @@ -78,12 +79,16 @@ public sealed class Plugin : IDalamudPlugin { this.LanguageChanged(this.Interface.UiLanguage); + this.Commands = new Commands(this); this.Common = new XivCommonBase(); this.TextureCache = new TextureCache(this.DataManager!); this.Functions = new GameFunctions.GameFunctions(this); this.Store = new Store(this); this.Ui = new PluginUi(this); + // let all the other components register, then initialise commands + this.Commands.Initialise(); + if (this.Interface.Reason is not PluginLoadReason.Boot) { this.Store.FilterAllTabs(false); } @@ -103,6 +108,7 @@ public sealed class Plugin : IDalamudPlugin { this.Functions.Dispose(); this.TextureCache.Dispose(); this.Common.Dispose(); + this.Commands.Dispose(); } internal void SaveConfig() { diff --git a/ChatTwo/Ui/ChatLog.cs b/ChatTwo/Ui/ChatLog.cs index fda7fc7..73cb7f8 100755 --- a/ChatTwo/Ui/ChatLog.cs +++ b/ChatTwo/Ui/ChatLog.cs @@ -6,7 +6,6 @@ using ChatTwo.Resources; using ChatTwo.Util; using Dalamud.Game.ClientState.Conditions; using Dalamud.Game.ClientState.Keys; -using Dalamud.Game.Command; using Dalamud.Game.Text.SeStringHandling; using Dalamud.Game.Text.SeStringHandling.Payloads; using Dalamud.Interface; @@ -46,9 +45,8 @@ internal sealed class ChatLog : IUiComponent { this.SetUpTextCommandChannels(); - this.Ui.Plugin.CommandManager.AddHandler("/clearlog2", new CommandInfo(this.ClearLog) { - HelpMessage = "Clears the Chat 2 chat log", - }); + this.Ui.Plugin.Commands.Register("/clearlog2", "Clear the Chat 2 chat log").Execute += this.ClearLog; + this.Ui.Plugin.Commands.Register("/chat2").Execute += this.ToggleChat; this._fontIcon = this.Ui.Plugin.DataManager.GetImGuiTexture("common/font/fonticon_ps5.tex"); @@ -62,7 +60,8 @@ internal sealed class ChatLog : IUiComponent { this.Ui.Plugin.ClientState.Login -= this.Login; this.Ui.Plugin.Functions.Chat.Activated -= this.Activated; this._fontIcon?.Dispose(); - this.Ui.Plugin.CommandManager.RemoveHandler("/clearlog2"); + this.Ui.Plugin.Commands.Register("/chat2").Execute -= this.ToggleChat; + this.Ui.Plugin.Commands.Register("/clearlog2").Execute -= this.ClearLog; } private void Logout(object? sender, EventArgs e) { @@ -160,6 +159,36 @@ internal sealed class ChatLog : IUiComponent { } } + private void ToggleChat(string command, string arguments) { + var parts = arguments.Split(' '); + if (parts.Length < 2 || parts[0] != "chat") { + return; + } + + switch (parts[1]) { + case "hide": + this._hideState = HideState.User; + break; + case "show": + this._hideState = HideState.None; + break; + case "toggle": + if (this._hideState is HideState.User or HideState.CutsceneOverride) { + this._hideState = HideState.None; + } + + if (this._hideState is HideState.Cutscene) { + this._hideState = HideState.CutsceneOverride; + } + + if (this._hideState is HideState.None) { + this._hideState = HideState.User; + } + + break; + } + } + private void SetUpTextCommandChannels() { this.TextCommandChannels.Clear(); diff --git a/ChatTwo/Ui/Settings.cs b/ChatTwo/Ui/Settings.cs index d6a0724..aadb82f 100755 --- a/ChatTwo/Ui/Settings.cs +++ b/ChatTwo/Ui/Settings.cs @@ -3,7 +3,6 @@ using System.Numerics; using ChatTwo.Resources; using ChatTwo.Ui.SettingsTabs; using ChatTwo.Util; -using Dalamud.Game.Command; using Dalamud.Interface; using ImGuiNET; @@ -31,17 +30,17 @@ internal sealed class Settings : IUiComponent { new About(), }; - this.Ui.Plugin.CommandManager.AddHandler("/chat2", new CommandInfo(this.Command) { - HelpMessage = "Toggle the Chat 2 settings", - }); + this.Ui.Plugin.Commands.Register("/chat2", "Perform various actions with Chat 2.").Execute += this.Command; } public void Dispose() { - this.Ui.Plugin.CommandManager.RemoveHandler("/chat2"); + this.Ui.Plugin.Commands.Register("/chat2").Execute -= this.Command; } private void Command(string command, string args) { - this.Ui.SettingsVisible ^= true; + if (string.IsNullOrWhiteSpace(args)) { + this.Ui.SettingsVisible ^= true; + } } private void Initialise() {