Merge pull request #1404 from MidoriKami/CommandManagerPluginScoped

This commit is contained in:
goat 2023-09-19 19:30:33 +02:00 committed by GitHub
commit 8ef0f85155
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
3 changed files with 96 additions and 11 deletions

View file

@ -15,7 +15,6 @@ public sealed class CommandInfo
public CommandInfo(HandlerDelegate handler)
{
this.Handler = handler;
this.LoaderAssemblyName = Assembly.GetCallingAssembly()?.GetName()?.Name;
}
/// <summary>

View file

@ -1,4 +1,3 @@
using System;
using System.Collections.Concurrent;
using System.Collections.Generic;
using System.Collections.ObjectModel;
@ -9,22 +8,21 @@ using Dalamud.Game.Text;
using Dalamud.Game.Text.SeStringHandling;
using Dalamud.IoC;
using Dalamud.IoC.Internal;
using Dalamud.Logging.Internal;
using Dalamud.Plugin.Internal.Types;
using Dalamud.Plugin.Services;
using Serilog;
namespace Dalamud.Game.Command;
/// <summary>
/// This class manages registered in-game slash commands.
/// </summary>
[PluginInterface]
[InterfaceVersion("1.0")]
[ServiceManager.BlockingEarlyLoadedService]
#pragma warning disable SA1015
[ResolveVia<ICommandManager>]
#pragma warning restore SA1015
internal sealed class CommandManager : IServiceType, IDisposable, ICommandManager
{
private static readonly ModuleLog Log = new("Command");
private readonly ConcurrentDictionary<string, CommandInfo> commandMap = new();
private readonly Regex commandRegexEn = new(@"^The command (?<command>.+) does not exist\.$", 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)
// => argument: 13-17 (4 chars)
// => content.IndexOf(' ') == 12
command = content.Substring(0, separatorPosition);
command = content[..separatorPosition];
var argStart = separatorPosition + 1;
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;
}
}

View file

@ -15,7 +15,6 @@ using Dalamud.Game.Command;
using Dalamud.Interface.Colors;
using Dalamud.Interface.Components;
using Dalamud.Interface.Internal.Notifications;
using Dalamud.Interface.Style;
using Dalamud.Interface.Utility;
using Dalamud.Interface.Utility.Raii;
using Dalamud.Interface.Windowing;
@ -2227,8 +2226,7 @@ internal class PluginInstallerWindow : Window, IDisposable
{
var commands = commandManager.Commands
.Where(cInfo =>
cInfo.Value != null &&
cInfo.Value.ShowInHelp &&
cInfo.Value is { ShowInHelp: true } &&
cInfo.Value.LoaderAssemblyName == plugin.Manifest.InternalName)
.ToArray();