mirror of
https://github.com/goatcorp/Dalamud.git
synced 2025-12-12 18:27:23 +01:00
Add CommandManagerPluginScoped
This commit is contained in:
parent
7a0f58a106
commit
3e8be33a5c
1 changed files with 70 additions and 6 deletions
|
|
@ -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;
|
||||||
|
|
@ -17,12 +16,8 @@ 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 readonly ConcurrentDictionary<string, CommandInfo> commandMap = new();
|
private readonly ConcurrentDictionary<string, CommandInfo> commandMap = new();
|
||||||
|
|
@ -84,7 +79,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 +157,72 @@ 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
|
||||||
|
{
|
||||||
|
[ServiceManager.ServiceDependency]
|
||||||
|
private readonly CommandManager commandManagerService = Service<CommandManager>.Get();
|
||||||
|
|
||||||
|
private readonly List<string> pluginRegisteredCommands = new();
|
||||||
|
|
||||||
|
/// <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))
|
||||||
|
{
|
||||||
|
if (this.commandManagerService.AddHandler(command, info))
|
||||||
|
{
|
||||||
|
this.pluginRegisteredCommands.Add(command);
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
|
||||||
|
/// <inheritdoc/>
|
||||||
|
public bool RemoveHandler(string command)
|
||||||
|
{
|
||||||
|
if (this.pluginRegisteredCommands.Contains(command))
|
||||||
|
{
|
||||||
|
if (this.commandManagerService.RemoveHandler(command))
|
||||||
|
{
|
||||||
|
this.pluginRegisteredCommands.Remove(command);
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue