mirror of
https://github.com/goatcorp/Dalamud.git
synced 2026-02-19 14:27:45 +01:00
fix: make CommandManager thread-safe
This commit is contained in:
parent
b683c5ba43
commit
1f0526b2ef
1 changed files with 6 additions and 8 deletions
|
|
@ -1,4 +1,5 @@
|
||||||
using System;
|
using System;
|
||||||
|
using System.Collections.Concurrent;
|
||||||
using System.Collections.Generic;
|
using System.Collections.Generic;
|
||||||
using System.Collections.ObjectModel;
|
using System.Collections.ObjectModel;
|
||||||
using System.Text.RegularExpressions;
|
using System.Text.RegularExpressions;
|
||||||
|
|
@ -24,7 +25,7 @@ namespace Dalamud.Game.Command;
|
||||||
#pragma warning restore SA1015
|
#pragma warning restore SA1015
|
||||||
public sealed class CommandManager : IServiceType, IDisposable, ICommandManager
|
public sealed class CommandManager : IServiceType, IDisposable, ICommandManager
|
||||||
{
|
{
|
||||||
private readonly Dictionary<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);
|
||||||
private readonly Regex commandRegexDe = new(@"^„(?<command>.+)“ existiert nicht als Textkommando\.$", RegexOptions.Compiled);
|
private readonly Regex commandRegexDe = new(@"^„(?<command>.+)“ existiert nicht als Textkommando\.$", RegexOptions.Compiled);
|
||||||
|
|
@ -115,22 +116,19 @@ public sealed class CommandManager : IServiceType, IDisposable, ICommandManager
|
||||||
if (info == null)
|
if (info == null)
|
||||||
throw new ArgumentNullException(nameof(info), "Command handler is null.");
|
throw new ArgumentNullException(nameof(info), "Command handler is null.");
|
||||||
|
|
||||||
try
|
if (!this.commandMap.TryAdd(command, info))
|
||||||
{
|
|
||||||
this.commandMap.Add(command, info);
|
|
||||||
return true;
|
|
||||||
}
|
|
||||||
catch (ArgumentException)
|
|
||||||
{
|
{
|
||||||
Log.Error("Command {CommandName} is already registered.", command);
|
Log.Error("Command {CommandName} is already registered.", command);
|
||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
return true;
|
||||||
}
|
}
|
||||||
|
|
||||||
/// <inheritdoc/>
|
/// <inheritdoc/>
|
||||||
public bool RemoveHandler(string command)
|
public bool RemoveHandler(string command)
|
||||||
{
|
{
|
||||||
return this.commandMap.Remove(command);
|
return this.commandMap.Remove(command, out _);
|
||||||
}
|
}
|
||||||
|
|
||||||
/// <inheritdoc/>
|
/// <inheritdoc/>
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue