From 1f0526b2ef6f4fd1cd8330bd7ed6293e5ca2621c Mon Sep 17 00:00:00 2001 From: goat Date: Sat, 15 Jul 2023 13:29:16 +0200 Subject: [PATCH] fix: make CommandManager thread-safe --- Dalamud/Game/Command/CommandManager.cs | 14 ++++++-------- 1 file changed, 6 insertions(+), 8 deletions(-) diff --git a/Dalamud/Game/Command/CommandManager.cs b/Dalamud/Game/Command/CommandManager.cs index 11cbbffbd..63a1a3d09 100644 --- a/Dalamud/Game/Command/CommandManager.cs +++ b/Dalamud/Game/Command/CommandManager.cs @@ -1,4 +1,5 @@ using System; +using System.Collections.Concurrent; using System.Collections.Generic; using System.Collections.ObjectModel; using System.Text.RegularExpressions; @@ -24,7 +25,7 @@ namespace Dalamud.Game.Command; #pragma warning restore SA1015 public sealed class CommandManager : IServiceType, IDisposable, ICommandManager { - private readonly Dictionary commandMap = new(); + private readonly ConcurrentDictionary commandMap = new(); private readonly Regex commandRegexEn = new(@"^The command (?.+) does not exist\.$", RegexOptions.Compiled); private readonly Regex commandRegexJp = new(@"^そのコマンドはありません。: (?.+)$", RegexOptions.Compiled); private readonly Regex commandRegexDe = new(@"^„(?.+)“ existiert nicht als Textkommando\.$", RegexOptions.Compiled); @@ -115,22 +116,19 @@ public sealed class CommandManager : IServiceType, IDisposable, ICommandManager if (info == null) throw new ArgumentNullException(nameof(info), "Command handler is null."); - try - { - this.commandMap.Add(command, info); - return true; - } - catch (ArgumentException) + if (!this.commandMap.TryAdd(command, info)) { Log.Error("Command {CommandName} is already registered.", command); return false; } + + return true; } /// public bool RemoveHandler(string command) { - return this.commandMap.Remove(command); + return this.commandMap.Remove(command, out _); } ///