Add more methods to retrieve resources in ResourceRef, and document existing ones.

This commit is contained in:
2025-06-24 22:25:26 +02:00
parent 58efd449a8
commit 4362e88eab

View File

@@ -13,11 +13,35 @@ namespace Voile
/// </summary> /// </summary>
public readonly Guid Guid = Guid.Empty; public readonly Guid Guid = Guid.Empty;
public bool HasValue => Guid != Guid.Empty; public bool HasValue => Guid != Guid.Empty;
/// <summary>
/// Retrieve a reference.
/// </summary>
public T Value => ResourceManager.GetResource<T>(Guid);
/// <summary>
/// Retrieves a <see cref="Resource"/>.<br />
/// This will throw an <see cref="InvalidOperationException"/> if the resource wasn't loaded or is invalid. <br />
/// You can check if resource was loaded with <see cref="HasValue"/>, or consider using <see cref="TryGetValue"/>.
/// </summary>
public T Value => ResourceManager.GetResource<T>(Guid)
?? throw new InvalidOperationException($"Resource with GUID {Guid} is not loaded or invalid.");
/// <summary>
/// Retrieves a resource or <c>null</c> if the resource wasn't loaded or is invalid.
/// </summary>
public T? ValueOrNull => ResourceManager.GetResource<T>(Guid);
/// <summary>
/// Tries to retrieve a <see cref="Resource"/>.
/// </summary>
/// <param name="value">An instance of a retrieved <see cref="Resource"/>.</param>
/// <returns><c>true</c> if the resource was successfully retrieved, otherwise <c>false</c>.</returns>
public bool TryGetValue(out T? value)
{
value = ResourceManager.GetResource<T>(Guid);
return value != null;
}
/// <summary>
/// Create an empty <see cref="ResourceRef"/>.
/// </summary>
/// <returns></returns>
public static ResourceRef<T> Empty() public static ResourceRef<T> Empty()
{ {
return new ResourceRef<T>(Guid.Empty); return new ResourceRef<T>(Guid.Empty);