ResourceRef.cs 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138
  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 the resource hasn't been loaded it will be loaded as if calling
  25. /// <see cref="Resources.Load{T}(string,bool)"/> using default settings.
  26. /// </summary>
  27. public Resource GenericValue => Internal_GetResource(mCachedPtr);
  28. /// <summary>
  29. /// Returns the UUID of the resource this handle is referencing.
  30. /// </summary>
  31. public UUID UUID
  32. {
  33. get
  34. {
  35. Internal_GetUUID(mCachedPtr, out var uuid);
  36. return uuid;
  37. }
  38. }
  39. /// <summary>
  40. /// Casts the generic resource reference type to a specific resource reference type.
  41. /// </summary>
  42. /// <typeparam name="T">Type of resource to cast the reference to.</typeparam>
  43. /// <returns>New reference type.</returns>
  44. public RRef<T> As<T>() where T : Resource
  45. {
  46. return (RRef<T>)Internal_CastAs(mCachedPtr, typeof(T));
  47. }
  48. /// <inheritdoc/>
  49. public override bool Equals(object other)
  50. {
  51. if (ReferenceEquals(other, null))
  52. return false;
  53. if (!(other is RRefBase))
  54. return false;
  55. RRefBase otherRef = (RRefBase)other;
  56. Internal_GetUUID(mCachedPtr, out var lhs);
  57. Internal_GetUUID(otherRef.mCachedPtr, out var rhs);
  58. return lhs.Equals(rhs);
  59. }
  60. public bool Equals(RRefBase other)
  61. {
  62. if (ReferenceEquals(other, null))
  63. return false;
  64. Internal_GetUUID(mCachedPtr, out var lhs);
  65. Internal_GetUUID(other.mCachedPtr, out var rhs);
  66. return lhs.Equals(rhs);
  67. }
  68. public static bool operator==(RRefBase lhs, RRefBase rhs)
  69. {
  70. if (ReferenceEquals(lhs, rhs))
  71. return true;
  72. if (ReferenceEquals(lhs, null))
  73. return false;
  74. if (ReferenceEquals(rhs, null))
  75. return false;
  76. Internal_GetUUID(lhs.mCachedPtr, out var lhsUUID);
  77. Internal_GetUUID(rhs.mCachedPtr, out var rhsUUID);
  78. return lhsUUID.Equals(rhsUUID);
  79. }
  80. public static bool operator!=(RRefBase lhs, RRefBase rhs)
  81. {
  82. return !(lhs == rhs);
  83. }
  84. /// <inheritdoc/>
  85. public override int GetHashCode()
  86. {
  87. return UUID.GetHashCode();
  88. }
  89. [MethodImpl(MethodImplOptions.InternalCall)]
  90. private static extern bool Internal_IsLoaded(IntPtr thisPtr);
  91. [MethodImpl(MethodImplOptions.InternalCall)]
  92. protected static extern Resource Internal_GetResource(IntPtr thisPtr);
  93. [MethodImpl(MethodImplOptions.InternalCall)]
  94. private static extern void Internal_GetUUID(IntPtr thisPtr, out UUID uuid);
  95. [MethodImpl(MethodImplOptions.InternalCall)]
  96. private static extern RRefBase Internal_CastAs(IntPtr thisPtr, Type type);
  97. }
  98. /// <summary>
  99. /// Provides a handle to a <see cref="Resource"/>. The underlying resource might or might not be loaded.
  100. /// </summary>
  101. /// <typeparam name="T">Resource type that the handle references</typeparam>
  102. public class RRef<T> : RRefBase where T : Resource
  103. {
  104. /// <summary>
  105. /// Returns the referenced resource. If the resource hasn't been loaded it will be loaded as if calling
  106. /// <see cref="Resources.Load{T}(string,bool)"/> using default settings.
  107. /// </summary>
  108. public T Value => (T) GenericValue;
  109. }
  110. /** @} */
  111. }