From 73399459eacbc15187edf4dc9e26fd934b8a7fad Mon Sep 17 00:00:00 2001 From: Quin Lynch <49576606+quinchs@users.noreply.github.com> Date: Sat, 26 Mar 2022 16:21:26 -0300 Subject: [PATCH] feature: add a way to remove type readers from the interaction/command service. (#2210) * Add remove methods * add inline docs Co-authored-by: Cenngo --- src/Discord.Net.Commands/CommandService.cs | 35 +++++++++++++ .../InteractionService.cs | 52 +++++++++++++++++++ src/Discord.Net.Interactions/Map/TypeMap.cs | 12 +++++ 3 files changed, 99 insertions(+) diff --git a/src/Discord.Net.Commands/CommandService.cs b/src/Discord.Net.Commands/CommandService.cs index d6dfc2fb7..57e0e430e 100644 --- a/src/Discord.Net.Commands/CommandService.cs +++ b/src/Discord.Net.Commands/CommandService.cs @@ -403,6 +403,41 @@ namespace Discord.Commands AddNullableTypeReader(type, reader); } } + + /// + /// Removes a type reader from the list of type readers. + /// + /// + /// Removing a from the will not dereference the from the loaded module/command instances. + /// You need to reload the modules for the changes to take effect. + /// + /// The type to remove the readers from. + /// if the default readers for should be removed; otherwise . + /// The removed collection of type readers. + /// if the remove operation was successful; otherwise . + public bool TryRemoveTypeReader(Type type, bool isDefaultTypeReader, out IDictionary readers) + { + readers = new Dictionary(); + + if (isDefaultTypeReader) + { + var isSuccess = _defaultTypeReaders.TryRemove(type, out var result); + if (isSuccess) + readers.Add(result?.GetType(), result); + + return isSuccess; + } + else + { + var isSuccess = _typeReaders.TryRemove(type, out var result); + + if (isSuccess) + readers = result; + + return isSuccess; + } + } + internal bool HasDefaultTypeReader(Type type) { if (_defaultTypeReaders.ContainsKey(type)) diff --git a/src/Discord.Net.Interactions/InteractionService.cs b/src/Discord.Net.Interactions/InteractionService.cs index 927e39735..deb6fa931 100644 --- a/src/Discord.Net.Interactions/InteractionService.cs +++ b/src/Discord.Net.Interactions/InteractionService.cs @@ -905,9 +905,61 @@ namespace Discord.Interactions public void AddGenericTypeReader(Type targetType, Type readerType) => _typeReaderMap.AddGeneric(targetType, readerType); + /// + /// Removes a type reader for the type . + /// + /// The type to remove the readers from. + /// The reader if the resulting remove operation was successful. + /// if the remove operation was successful; otherwise . + public bool TryRemoveTypeReader(out TypeReader reader) + => TryRemoveTypeReader(typeof(T), out reader); + + /// + /// Removes a type reader for the given type. + /// + /// + /// Removing a from the will not dereference the from the loaded module/command instances. + /// You need to reload the modules for the changes to take effect. + /// + /// The type to remove the reader from. + /// The reader if the resulting remove operation was successful. + /// if the remove operation was successful; otherwise . + public bool TryRemoveTypeReader(Type type, out TypeReader reader) + => _typeReaderMap.TryRemoveConcrete(type, out reader); + + /// + /// Removes a generic type reader from the type . + /// + /// + /// Removing a from the will not dereference the from the loaded module/command instances. + /// You need to reload the modules for the changes to take effect. + /// + /// The type to remove the readers from. + /// The removed readers type. + /// if the remove operation was successful; otherwise . + public bool TryRemoveGenericTypeReader(out Type readerType) + => TryRemoveGenericTypeReader(typeof(T), out readerType); + + /// + /// Removes a generic type reader from the given type. + /// + /// + /// Removing a from the will not dereference the from the loaded module/command instances. + /// You need to reload the modules for the changes to take effect. + /// + /// The type to remove the reader from. + /// The readers type if the remove operation was successful. + /// if the remove operation was successful; otherwise . + public bool TryRemoveGenericTypeReader(Type type, out Type readerType) + => _typeReaderMap.TryRemoveGeneric(type, out readerType); + /// /// Serialize an object using a into a to be placed in a Component CustomId. /// + /// + /// Removing a from the will not dereference the from the loaded module/command instances. + /// You need to reload the modules for the changes to take effect. + /// /// Type of the object to be serialized. /// Object to be serialized. /// Services that will be passed on to the . diff --git a/src/Discord.Net.Interactions/Map/TypeMap.cs b/src/Discord.Net.Interactions/Map/TypeMap.cs index ef1ef4a53..520ed7231 100644 --- a/src/Discord.Net.Interactions/Map/TypeMap.cs +++ b/src/Discord.Net.Interactions/Map/TypeMap.cs @@ -74,6 +74,18 @@ namespace Discord.Interactions _generics[targetType] = converterType; } + public bool TryRemoveConcrete(out TConverter converter) + => TryRemoveConcrete(typeof(TTarget), out converter); + + public bool TryRemoveConcrete(Type type, out TConverter converter) + => _concretes.TryRemove(type, out converter); + + public bool TryRemoveGeneric(out Type converterType) + => TryRemoveGeneric(typeof(TTarget), out converterType); + + public bool TryRemoveGeneric(Type targetType, out Type converterType) + => _generics.TryRemove(targetType, out converterType); + private Type GetMostSpecific(Type type) { if (_generics.TryGetValue(type, out var matching))