DP-PatMe2Mqtt/PatMe2Mqtt/Cmd.cs

54 lines
1.2 KiB
C#
Raw Normal View History

2023-05-26 15:26:16 +02:00
using Dalamud.Game.Command;
2023-11-14 00:07:11 +01:00
using Dalamud.Plugin.Services;
2023-05-26 15:26:16 +02:00
using System;
namespace PatMe2Mqtt
{
public abstract class Cmd : IDisposable
{
2023-11-14 00:07:11 +01:00
private ICommandManager? _commandManager;
2023-05-26 15:26:16 +02:00
private string? _commandString;
public abstract string Name { get; }
public string FullName => _commandString ?? $"/{Name}";
public abstract string HelpMessage { get; }
2023-11-14 00:07:11 +01:00
public void Register(ICommandManager commandManager)
2023-05-26 15:26:16 +02:00
{
if (_commandManager is not null)
{
Dispose();
}
_commandManager = commandManager;
_commandString = FullName;
commandManager.AddHandler(FullName, new CommandInfo(Handle)
{
HelpMessage = HelpMessage
});
}
public abstract void Run(string arguments);
public void Dispose()
{
_commandManager?.RemoveHandler(FullName);
_commandManager = null;
GC.SuppressFinalize(this);
}
private void Handle(string command, string arguments)
{
if (command != _commandString)
{
return;
}
Run(arguments);
}
}
}