From 4362e88eabb7760cf5886259dff969bb1ec952c4 Mon Sep 17 00:00:00 2001 From: dnesov Date: Tue, 24 Jun 2025 22:25:26 +0200 Subject: [PATCH] Add more methods to retrieve resources in ResourceRef, and document existing ones. --- Voile/Source/Resources/Resource.cs | 32 ++++++++++++++++++++++++++---- 1 file changed, 28 insertions(+), 4 deletions(-) diff --git a/Voile/Source/Resources/Resource.cs b/Voile/Source/Resources/Resource.cs index b85b6c6..6b41c7b 100644 --- a/Voile/Source/Resources/Resource.cs +++ b/Voile/Source/Resources/Resource.cs @@ -13,11 +13,35 @@ namespace Voile /// public readonly Guid Guid = Guid.Empty; public bool HasValue => Guid != Guid.Empty; - /// - /// Retrieve a reference. - /// - public T Value => ResourceManager.GetResource(Guid); + /// + /// Retrieves a .
+ /// This will throw an if the resource wasn't loaded or is invalid.
+ /// You can check if resource was loaded with , or consider using . + ///
+ public T Value => ResourceManager.GetResource(Guid) + ?? throw new InvalidOperationException($"Resource with GUID {Guid} is not loaded or invalid."); + + /// + /// Retrieves a resource or null if the resource wasn't loaded or is invalid. + /// + public T? ValueOrNull => ResourceManager.GetResource(Guid); + + /// + /// Tries to retrieve a . + /// + /// An instance of a retrieved . + /// true if the resource was successfully retrieved, otherwise false. + public bool TryGetValue(out T? value) + { + value = ResourceManager.GetResource(Guid); + return value != null; + } + + /// + /// Create an empty . + /// + /// public static ResourceRef Empty() { return new ResourceRef(Guid.Empty);