Resource.cs 5.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177
  1. //********************************** Banshee Engine (www.banshee3d.com) **************************************************//
  2. //**************** Copyright (c) 2016 Marko Pintera ([email protected]). All rights reserved. **********************//
  3. using System;
  4. using System.Collections.Generic;
  5. using System.Runtime.CompilerServices;
  6. using System.Runtime.InteropServices;
  7. namespace BansheeEngine
  8. {
  9. /** @addtogroup Resources
  10. * @{
  11. */
  12. /// <summary>
  13. /// Base class for all resources. Resources can be persistently referenced by scene objects or other resources.
  14. /// </summary>
  15. public class Resource : ScriptObject
  16. {
  17. /// <summary>
  18. /// Name of the resource. Use primarily for easier identification and not important to the engine itself.
  19. /// </summary>
  20. public string Name
  21. {
  22. get { return Internal_GetName(mCachedPtr); }
  23. }
  24. /// <summary>
  25. /// Returns a universally unique identifier of this resource.
  26. /// </summary>
  27. public UUID UUID
  28. {
  29. get
  30. {
  31. UUID uuid;
  32. Internal_GetUUID(mCachedPtr, out uuid);
  33. return uuid;
  34. }
  35. }
  36. /// <summary>
  37. /// Releases an internal reference to the resource held by the resources system. <see cref="Resources.Release"/>
  38. /// </summary>
  39. public void Release()
  40. {
  41. Internal_Release(mCachedPtr);
  42. }
  43. [MethodImpl(MethodImplOptions.InternalCall)]
  44. private static extern string Internal_GetName(IntPtr nativeInstance);
  45. [MethodImpl(MethodImplOptions.InternalCall)]
  46. private static extern void Internal_GetUUID(IntPtr nativeInstance, out UUID uuid);
  47. [MethodImpl(MethodImplOptions.InternalCall)]
  48. private static extern void Internal_Release(IntPtr nativeInstance);
  49. }
  50. /// <summary>Represents a universally unique identifier.</summary>
  51. [StructLayout(LayoutKind.Sequential)]
  52. public struct UUID
  53. {
  54. public readonly uint data0;
  55. public readonly uint data1;
  56. public readonly uint data2;
  57. public readonly uint data3;
  58. public static UUID Empty = new UUID(0, 0, 0, 0);
  59. public UUID(uint data0, uint data1, uint data2, uint data3)
  60. {
  61. this.data0 = data0;
  62. this.data1 = data1;
  63. this.data2 = data2;
  64. this.data3 = data3;
  65. }
  66. /// <summary>
  67. /// Checks has the UUID been initialized to a valid value.
  68. /// </summary>
  69. /// <returns>True if UUID is initialized.</returns>
  70. public bool IsEmpty()
  71. {
  72. return data0 == 0 && data1 == 1 && data2 == 0 && data3 == 0;
  73. }
  74. public static bool operator ==(UUID lhs, UUID rhs)
  75. {
  76. return lhs.data0 == rhs.data0 && lhs.data1 == rhs.data1 && lhs.data2 == rhs.data2 && lhs.data3 == rhs.data3;
  77. }
  78. public static bool operator !=(UUID lhs, UUID rhs)
  79. {
  80. return !(lhs == rhs);
  81. }
  82. /// <inheritdoc/>
  83. public override int GetHashCode()
  84. {
  85. return data0.GetHashCode() ^ data1.GetHashCode() << 2 ^ data2.GetHashCode() >> 2 ^ data3.GetHashCode() >> 1;
  86. }
  87. /// <inheritdoc/>
  88. public override bool Equals(object other)
  89. {
  90. if (!(other is UUID))
  91. return false;
  92. UUID uuid = (UUID)other;
  93. if (data0.Equals(uuid.data0) && data1.Equals(uuid.data1) && data2.Equals(uuid.data2) && data3.Equals(uuid.data3))
  94. return true;
  95. return false;
  96. }
  97. private static char[] HEX_TO_LITERAL = {'0', '1', '2', '3', '4', '5', '6', '7', '8', '9', 'a', 'b', 'c', 'd', 'e', 'f'};
  98. /// <inheritdoc/>
  99. public override string ToString()
  100. {
  101. char[] output = new char[36];
  102. uint idx = 0;
  103. // First group: 8 digits
  104. for(int i = 7; i >= 0; --i)
  105. {
  106. uint hexVal = (data0 >> (i * 4)) & 0xF;
  107. output[idx++] = HEX_TO_LITERAL[hexVal];
  108. }
  109. output[idx++] = '-';
  110. // Second group: 4 digits
  111. for(int i = 7; i >= 4; --i)
  112. {
  113. uint hexVal = (data1 >> (i * 4)) & 0xF;
  114. output[idx++] = HEX_TO_LITERAL[hexVal];
  115. }
  116. output[idx++] = '-';
  117. // Third group: 4 digits
  118. for(int i = 3; i >= 0; --i)
  119. {
  120. uint hexVal = (data1 >> (i * 4)) & 0xF;
  121. output[idx++] = HEX_TO_LITERAL[hexVal];
  122. }
  123. output[idx++] = '-';
  124. // Fourth group: 4 digits
  125. for(int i = 7; i >= 4; --i)
  126. {
  127. uint hexVal = (data2 >> (i * 4)) & 0xF;
  128. output[idx++] = HEX_TO_LITERAL[hexVal];
  129. }
  130. output[idx++] = '-';
  131. // Fifth group: 12 digits
  132. for(int i = 3; i >= 0; --i)
  133. {
  134. uint hexVal = (data2 >> (i * 4)) & 0xF;
  135. output[idx++] = HEX_TO_LITERAL[hexVal];
  136. }
  137. for(int i = 7; i >= 0; --i)
  138. {
  139. uint hexVal = (data3 >> (i * 4)) & 0xF;
  140. output[idx++] = HEX_TO_LITERAL[hexVal];
  141. }
  142. return new string(output);
  143. }
  144. }
  145. /** @} */
  146. }