ResourceRef.cs 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100
  1. //********************************** Banshee Engine (www.banshee3d.com) **************************************************//
  2. //**************** Copyright (c) 2016 Marko Pintera ([email protected]). All rights reserved. **********************//
  3. using System;
  4. using System.Runtime.CompilerServices;
  5. namespace BansheeEngine
  6. {
  7. /** @addtogroup Resources
  8. * @{
  9. */
  10. /// <summary>
  11. /// Provides a handle to a <see cref="Resource"/>. The underlying resource might or might not be loaded.
  12. /// </summary>
  13. public class RRefBase : ScriptObject
  14. {
  15. /// <summary>
  16. /// Constructor for internal use only.
  17. /// </summary>
  18. protected RRefBase() { }
  19. /// <summary>
  20. /// Checks is the referenced resource loaded.
  21. /// </summary>
  22. public bool IsLoaded => Internal_IsLoaded(mCachedPtr);
  23. /// <summary>
  24. /// Returns the referenced resource if loaded, or null otherwise.
  25. /// </summary>
  26. public Resource GenericValue => Internal_GetResource(mCachedPtr);
  27. /// <summary>
  28. /// Returns the UUID of the resource this handle is referencing.
  29. /// </summary>
  30. public UUID UUID
  31. {
  32. get
  33. {
  34. Internal_GetUUID(mCachedPtr, out var uuid);
  35. return uuid;
  36. }
  37. }
  38. /// <summary>
  39. /// Casts the generic resource reference type to a specific resource reference type.
  40. /// </summary>
  41. /// <typeparam name="T">Type of resource to cast the reference to.</typeparam>
  42. /// <returns>New reference type.</returns>
  43. public RRef<T> As<T>() where T : Resource
  44. {
  45. return (RRef<T>)Internal_CastAs(mCachedPtr, typeof(T));
  46. }
  47. /// <inheritdoc/>
  48. public override bool Equals(object other)
  49. {
  50. if (!(other is RRefBase))
  51. return false;
  52. RRefBase otherRef = (RRefBase)other;
  53. Internal_GetUUID(mCachedPtr, out var lhs);
  54. Internal_GetUUID(otherRef.mCachedPtr, out var rhs);
  55. return lhs.Equals(rhs);
  56. }
  57. /// <inheritdoc/>
  58. public override int GetHashCode()
  59. {
  60. return UUID.GetHashCode();
  61. }
  62. [MethodImpl(MethodImplOptions.InternalCall)]
  63. private static extern bool Internal_IsLoaded(IntPtr thisPtr);
  64. [MethodImpl(MethodImplOptions.InternalCall)]
  65. protected static extern Resource Internal_GetResource(IntPtr thisPtr);
  66. [MethodImpl(MethodImplOptions.InternalCall)]
  67. private static extern void Internal_GetUUID(IntPtr thisPtr, out UUID uuid);
  68. [MethodImpl(MethodImplOptions.InternalCall)]
  69. private static extern RRefBase Internal_CastAs(IntPtr thisPtr, Type type);
  70. }
  71. /// <summary>
  72. /// Provides a handle to a <see cref="Resource"/>. The underlying resource might or might not be loaded.
  73. /// </summary>
  74. /// <typeparam name="T">Resource type that the handle references</typeparam>
  75. public class RRef<T> : RRefBase where T : Resource
  76. {
  77. /// <summary>
  78. /// Returns the referenced resource if loaded, or null otherwise.
  79. /// </summary>
  80. public T Value => (T) GenericValue;
  81. }
  82. /** @} */
  83. }