diff --git a/VpSharp/src/VirtualParadiseClient.Extensions.cs b/VpSharp/src/VirtualParadiseClient.Extensions.cs index d3d9401..918cf55 100644 --- a/VpSharp/src/VirtualParadiseClient.Extensions.cs +++ b/VpSharp/src/VirtualParadiseClient.Extensions.cs @@ -1,3 +1,4 @@ +using System.Diagnostics.CodeAnalysis; using System.Globalization; using System.Reflection; using VpSharp.ClientExtensions; @@ -8,6 +9,15 @@ public sealed partial class VirtualParadiseClient { private readonly List _extensions = new(); + /// + /// Gets a read-only view of the extensions currently added to this client. + /// + /// A read-only view of the extensions. + public IReadOnlyList Extensions + { + get => _extensions.AsReadOnly(); + } + /// /// Adds an extension to this client. /// @@ -65,4 +75,74 @@ public sealed partial class VirtualParadiseClient _extensions.Add(extension); return extension; } + + /// + /// Gets the extension whose type matches a specified type. + /// + /// The type of the extension to get. + /// The extension instance whose type matches . + /// No extension with the specified type is added to this client. + public VirtualParadiseClientExtension GetExtension(Type type) + { + ArgumentNullException.ThrowIfNull(type); + + VirtualParadiseClientExtension? result = _extensions.Find(e => e.GetType() == type); + if (result is null) + { + throw new InvalidOperationException($"No extension with the type {type} is added to this client."); + } + + return result; + } + + /// + /// Gets the extension whose type matches a specified type. + /// + /// The type of the extension to get. + /// The extension instance whose type matches . + /// No extension with the specified type is added to this client. + public T GetExtension() where T : VirtualParadiseClientExtension + { + var result = _extensions.Find(e => e.GetType() == typeof(T)) as T; + if (result is null) + { + throw new InvalidOperationException($"No extension with the type {typeof(T)} is added to this client."); + } + + return result; + } + + /// + /// Attempts to get the extension whose type matches a specified type. + /// + /// The type of the extension to get. + /// + /// When this method returns, contains the extension instance whose type matches , or + /// if no such extension exists. + /// + /// if a matching extension was found; otherwise, . + /// No extension with the specified type is added to this client. + public bool TryGetExtension(Type type, [NotNullWhen(true)] out VirtualParadiseClientExtension? extension) + { + ArgumentNullException.ThrowIfNull(type); + + extension = _extensions.Find(e => e.GetType() == type); + return extension is not null; + } + + /// + /// Attempts to get the extension whose type matches a specified type. + /// + /// The type of the extension to get. + /// + /// When this method returns, contains the extension instance whose type matches , or + /// if no such extension exists. + /// + /// if a matching extension was found; otherwise, . + /// No extension with the specified type is added to this client. + public bool TryGetExtension([NotNullWhen(true)] out T? extension) where T : VirtualParadiseClientExtension + { + extension = _extensions.Find(e => e.GetType() == typeof(T)) as T; + return extension is not null; + } }