From 9348e087b016acd87721fbb69ce0588f598d1bb4 Mon Sep 17 00:00:00 2001 From: Christopher F Date: Sat, 30 Jul 2016 18:23:12 -0400 Subject: [PATCH 1/2] Don't load modules that are already loaded Previously, if a user autoloaded commands more than once, commands that were already in the command map would be readded. If the module list already contains a module with the same type as the module being loaded, it will not load the new instance of this module. --- src/Discord.Net.Commands/CommandService.cs | 3 +++ 1 file changed, 3 insertions(+) diff --git a/src/Discord.Net.Commands/CommandService.cs b/src/Discord.Net.Commands/CommandService.cs index cd09c47b6..cc01ca2be 100644 --- a/src/Discord.Net.Commands/CommandService.cs +++ b/src/Discord.Net.Commands/CommandService.cs @@ -117,6 +117,9 @@ namespace Discord.Commands } private Module LoadInternal(object moduleInstance, ModuleAttribute moduleAttr, TypeInfo typeInfo) { + if (_modules.Any(m => m.Key.GetType().GetTypeInfo() == typeInfo)) + return _modules.FirstOrDefault(m => m.Key.GetType().GetTypeInfo() == typeInfo).Value; + var loadedModule = new Module(this, moduleInstance, moduleAttr, typeInfo); _modules[moduleInstance] = loadedModule; From 4c200c94ec38d04b7cdb839d77b6432e181bc8a0 Mon Sep 17 00:00:00 2001 From: Christopher F Date: Sat, 30 Jul 2016 21:44:14 -0400 Subject: [PATCH 2/2] Key the ModuleMap based on Type rather than instance --- src/Discord.Net.Commands/CommandService.cs | 14 +++++++------- 1 file changed, 7 insertions(+), 7 deletions(-) diff --git a/src/Discord.Net.Commands/CommandService.cs b/src/Discord.Net.Commands/CommandService.cs index cc01ca2be..2ce7c5517 100644 --- a/src/Discord.Net.Commands/CommandService.cs +++ b/src/Discord.Net.Commands/CommandService.cs @@ -12,7 +12,7 @@ namespace Discord.Commands public class CommandService { private readonly SemaphoreSlim _moduleLock; - private readonly ConcurrentDictionary _modules; + private readonly ConcurrentDictionary _modules; private readonly ConcurrentDictionary _typeReaders; private readonly CommandMap _map; @@ -22,7 +22,7 @@ namespace Discord.Commands public CommandService() { _moduleLock = new SemaphoreSlim(1, 1); - _modules = new ConcurrentDictionary(); + _modules = new ConcurrentDictionary(); _map = new CommandMap(); _typeReaders = new ConcurrentDictionary { @@ -100,7 +100,7 @@ namespace Discord.Commands await _moduleLock.WaitAsync().ConfigureAwait(false); try { - if (_modules.ContainsKey(moduleInstance)) + if (_modules.ContainsKey(moduleInstance.GetType())) throw new ArgumentException($"This module has already been loaded."); var typeInfo = moduleInstance.GetType().GetTypeInfo(); @@ -117,11 +117,11 @@ namespace Discord.Commands } private Module LoadInternal(object moduleInstance, ModuleAttribute moduleAttr, TypeInfo typeInfo) { - if (_modules.Any(m => m.Key.GetType().GetTypeInfo() == typeInfo)) - return _modules.FirstOrDefault(m => m.Key.GetType().GetTypeInfo() == typeInfo).Value; + if (_modules.ContainsKey(moduleInstance.GetType())) + return _modules[moduleInstance.GetType()]; var loadedModule = new Module(this, moduleInstance, moduleAttr, typeInfo); - _modules[moduleInstance] = loadedModule; + _modules[moduleInstance.GetType()] = loadedModule; foreach (var cmd in loadedModule.Commands) _map.AddCommand(cmd); @@ -179,7 +179,7 @@ namespace Discord.Commands private bool UnloadInternal(object module) { Module unloadedModule; - if (_modules.TryRemove(module, out unloadedModule)) + if (_modules.TryRemove(module.GetType(), out unloadedModule)) { foreach (var cmd in unloadedModule.Commands) _map.RemoveCommand(cmd);