mirror of
https://github.com/goatcorp/Dalamud.git
synced 2025-12-15 05:04:15 +01:00
Merge pull request #1404 from MidoriKami/CommandManagerPluginScoped
This commit is contained in:
commit
8ef0f85155
3 changed files with 96 additions and 11 deletions
|
|
@ -15,7 +15,6 @@ public sealed class CommandInfo
|
||||||
public CommandInfo(HandlerDelegate handler)
|
public CommandInfo(HandlerDelegate handler)
|
||||||
{
|
{
|
||||||
this.Handler = handler;
|
this.Handler = handler;
|
||||||
this.LoaderAssemblyName = Assembly.GetCallingAssembly()?.GetName()?.Name;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
/// <summary>
|
/// <summary>
|
||||||
|
|
|
||||||
|
|
@ -1,4 +1,3 @@
|
||||||
using System;
|
|
||||||
using System.Collections.Concurrent;
|
using System.Collections.Concurrent;
|
||||||
using System.Collections.Generic;
|
using System.Collections.Generic;
|
||||||
using System.Collections.ObjectModel;
|
using System.Collections.ObjectModel;
|
||||||
|
|
@ -9,22 +8,21 @@ using Dalamud.Game.Text;
|
||||||
using Dalamud.Game.Text.SeStringHandling;
|
using Dalamud.Game.Text.SeStringHandling;
|
||||||
using Dalamud.IoC;
|
using Dalamud.IoC;
|
||||||
using Dalamud.IoC.Internal;
|
using Dalamud.IoC.Internal;
|
||||||
|
using Dalamud.Logging.Internal;
|
||||||
|
using Dalamud.Plugin.Internal.Types;
|
||||||
using Dalamud.Plugin.Services;
|
using Dalamud.Plugin.Services;
|
||||||
using Serilog;
|
|
||||||
|
|
||||||
namespace Dalamud.Game.Command;
|
namespace Dalamud.Game.Command;
|
||||||
|
|
||||||
/// <summary>
|
/// <summary>
|
||||||
/// This class manages registered in-game slash commands.
|
/// This class manages registered in-game slash commands.
|
||||||
/// </summary>
|
/// </summary>
|
||||||
[PluginInterface]
|
|
||||||
[InterfaceVersion("1.0")]
|
[InterfaceVersion("1.0")]
|
||||||
[ServiceManager.BlockingEarlyLoadedService]
|
[ServiceManager.BlockingEarlyLoadedService]
|
||||||
#pragma warning disable SA1015
|
|
||||||
[ResolveVia<ICommandManager>]
|
|
||||||
#pragma warning restore SA1015
|
|
||||||
internal sealed class CommandManager : IServiceType, IDisposable, ICommandManager
|
internal sealed class CommandManager : IServiceType, IDisposable, ICommandManager
|
||||||
{
|
{
|
||||||
|
private static readonly ModuleLog Log = new("Command");
|
||||||
|
|
||||||
private readonly ConcurrentDictionary<string, CommandInfo> commandMap = new();
|
private readonly ConcurrentDictionary<string, CommandInfo> commandMap = new();
|
||||||
private readonly Regex commandRegexEn = new(@"^The command (?<command>.+) does not exist\.$", RegexOptions.Compiled);
|
private readonly Regex commandRegexEn = new(@"^The command (?<command>.+) does not exist\.$", RegexOptions.Compiled);
|
||||||
private readonly Regex commandRegexJp = new(@"^そのコマンドはありません。: (?<command>.+)$", RegexOptions.Compiled);
|
private readonly Regex commandRegexJp = new(@"^そのコマンドはありません。: (?<command>.+)$", RegexOptions.Compiled);
|
||||||
|
|
@ -84,7 +82,7 @@ internal sealed class CommandManager : IServiceType, IDisposable, ICommandManage
|
||||||
// => command: 0-12 (12 chars)
|
// => command: 0-12 (12 chars)
|
||||||
// => argument: 13-17 (4 chars)
|
// => argument: 13-17 (4 chars)
|
||||||
// => content.IndexOf(' ') == 12
|
// => content.IndexOf(' ') == 12
|
||||||
command = content.Substring(0, separatorPosition);
|
command = content[..separatorPosition];
|
||||||
|
|
||||||
var argStart = separatorPosition + 1;
|
var argStart = separatorPosition + 1;
|
||||||
argument = content[argStart..];
|
argument = content[argStart..];
|
||||||
|
|
@ -162,3 +160,93 @@ internal sealed class CommandManager : IServiceType, IDisposable, ICommandManage
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// Plugin-scoped version of a AddonLifecycle service.
|
||||||
|
/// </summary>
|
||||||
|
[PluginInterface]
|
||||||
|
[InterfaceVersion("1.0")]
|
||||||
|
[ServiceManager.ScopedService]
|
||||||
|
#pragma warning disable SA1015
|
||||||
|
[ResolveVia<ICommandManager>]
|
||||||
|
#pragma warning restore SA1015
|
||||||
|
internal class CommandManagerPluginScoped : IDisposable, IServiceType, ICommandManager
|
||||||
|
{
|
||||||
|
private static readonly ModuleLog Log = new("Command");
|
||||||
|
|
||||||
|
[ServiceManager.ServiceDependency]
|
||||||
|
private readonly CommandManager commandManagerService = Service<CommandManager>.Get();
|
||||||
|
|
||||||
|
private readonly List<string> pluginRegisteredCommands = new();
|
||||||
|
private readonly LocalPlugin pluginInfo;
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// Initializes a new instance of the <see cref="CommandManagerPluginScoped"/> class.
|
||||||
|
/// </summary>
|
||||||
|
/// <param name="localPlugin">Info for the plugin that requests this service.</param>
|
||||||
|
public CommandManagerPluginScoped(LocalPlugin localPlugin)
|
||||||
|
{
|
||||||
|
this.pluginInfo = localPlugin;
|
||||||
|
}
|
||||||
|
|
||||||
|
/// <inheritdoc/>
|
||||||
|
public ReadOnlyDictionary<string, CommandInfo> Commands => this.commandManagerService.Commands;
|
||||||
|
|
||||||
|
/// <inheritdoc/>
|
||||||
|
public void Dispose()
|
||||||
|
{
|
||||||
|
foreach (var command in this.pluginRegisteredCommands)
|
||||||
|
{
|
||||||
|
this.commandManagerService.RemoveHandler(command);
|
||||||
|
}
|
||||||
|
|
||||||
|
this.pluginRegisteredCommands.Clear();
|
||||||
|
}
|
||||||
|
|
||||||
|
/// <inheritdoc/>
|
||||||
|
public bool ProcessCommand(string content)
|
||||||
|
=> this.commandManagerService.ProcessCommand(content);
|
||||||
|
|
||||||
|
/// <inheritdoc/>
|
||||||
|
public void DispatchCommand(string command, string argument, CommandInfo info)
|
||||||
|
=> this.commandManagerService.DispatchCommand(command, argument, info);
|
||||||
|
|
||||||
|
/// <inheritdoc/>
|
||||||
|
public bool AddHandler(string command, CommandInfo info)
|
||||||
|
{
|
||||||
|
if (!this.pluginRegisteredCommands.Contains(command))
|
||||||
|
{
|
||||||
|
info.LoaderAssemblyName = this.pluginInfo.InternalName;
|
||||||
|
if (this.commandManagerService.AddHandler(command, info))
|
||||||
|
{
|
||||||
|
this.pluginRegisteredCommands.Add(command);
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
else
|
||||||
|
{
|
||||||
|
Log.Error($"Command {command} is already registered.");
|
||||||
|
}
|
||||||
|
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
|
||||||
|
/// <inheritdoc/>
|
||||||
|
public bool RemoveHandler(string command)
|
||||||
|
{
|
||||||
|
if (this.pluginRegisteredCommands.Contains(command))
|
||||||
|
{
|
||||||
|
if (this.commandManagerService.RemoveHandler(command))
|
||||||
|
{
|
||||||
|
this.pluginRegisteredCommands.Remove(command);
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
else
|
||||||
|
{
|
||||||
|
Log.Error($"Command {command} not found.");
|
||||||
|
}
|
||||||
|
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
|
||||||
|
|
@ -15,7 +15,6 @@ using Dalamud.Game.Command;
|
||||||
using Dalamud.Interface.Colors;
|
using Dalamud.Interface.Colors;
|
||||||
using Dalamud.Interface.Components;
|
using Dalamud.Interface.Components;
|
||||||
using Dalamud.Interface.Internal.Notifications;
|
using Dalamud.Interface.Internal.Notifications;
|
||||||
using Dalamud.Interface.Style;
|
|
||||||
using Dalamud.Interface.Utility;
|
using Dalamud.Interface.Utility;
|
||||||
using Dalamud.Interface.Utility.Raii;
|
using Dalamud.Interface.Utility.Raii;
|
||||||
using Dalamud.Interface.Windowing;
|
using Dalamud.Interface.Windowing;
|
||||||
|
|
@ -2227,8 +2226,7 @@ internal class PluginInstallerWindow : Window, IDisposable
|
||||||
{
|
{
|
||||||
var commands = commandManager.Commands
|
var commands = commandManager.Commands
|
||||||
.Where(cInfo =>
|
.Where(cInfo =>
|
||||||
cInfo.Value != null &&
|
cInfo.Value is { ShowInHelp: true } &&
|
||||||
cInfo.Value.ShowInHelp &&
|
|
||||||
cInfo.Value.LoaderAssemblyName == plugin.Manifest.InternalName)
|
cInfo.Value.LoaderAssemblyName == plugin.Manifest.InternalName)
|
||||||
.ToArray();
|
.ToArray();
|
||||||
|
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue