refactor: rework the command system
This commit is contained in:
parent
cbaa3fef4f
commit
1409da708d
|
@ -0,0 +1,76 @@
|
|||
using Dalamud.Game.Command;
|
||||
using Dalamud.Logging;
|
||||
|
||||
namespace ChatTwo;
|
||||
|
||||
internal sealed class Commands : IDisposable {
|
||||
private Plugin Plugin { get; }
|
||||
private Dictionary<string, CommandWrapper> 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<string, string>? 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);
|
||||
}
|
||||
}
|
|
@ -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() {
|
||||
|
|
|
@ -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();
|
||||
|
||||
|
|
|
@ -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() {
|
||||
|
|
Loading…
Reference in New Issue