Object.base.cs 5.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164
  1. using System;
  2. using System.Runtime.CompilerServices;
  3. namespace Godot
  4. {
  5. public partial class Object : IDisposable
  6. {
  7. private bool _disposed = false;
  8. private static StringName nativeName = "Object";
  9. internal IntPtr ptr;
  10. internal bool memoryOwn;
  11. /// <summary>
  12. /// Constructs a new <see cref="Object"/>.
  13. /// </summary>
  14. public Object() : this(false)
  15. {
  16. if (ptr == IntPtr.Zero)
  17. ptr = godot_icall_Object_Ctor(this);
  18. _InitializeGodotScriptInstanceInternals();
  19. }
  20. internal void _InitializeGodotScriptInstanceInternals()
  21. {
  22. godot_icall_Object_ConnectEventSignals(ptr);
  23. }
  24. internal Object(bool memoryOwn)
  25. {
  26. this.memoryOwn = memoryOwn;
  27. }
  28. /// <summary>
  29. /// The pointer to the native instance of this <see cref="Object"/>.
  30. /// </summary>
  31. public IntPtr NativeInstance
  32. {
  33. get { return ptr; }
  34. }
  35. internal static IntPtr GetPtr(Object instance)
  36. {
  37. if (instance == null)
  38. return IntPtr.Zero;
  39. if (instance._disposed)
  40. throw new ObjectDisposedException(instance.GetType().FullName);
  41. return instance.ptr;
  42. }
  43. ~Object()
  44. {
  45. Dispose(false);
  46. }
  47. /// <summary>
  48. /// Disposes of this <see cref="Object"/>.
  49. /// </summary>
  50. public void Dispose()
  51. {
  52. Dispose(true);
  53. GC.SuppressFinalize(this);
  54. }
  55. /// <summary>
  56. /// Disposes implementation of this <see cref="Object"/>.
  57. /// </summary>
  58. protected virtual void Dispose(bool disposing)
  59. {
  60. if (_disposed)
  61. return;
  62. if (ptr != IntPtr.Zero)
  63. {
  64. if (memoryOwn)
  65. {
  66. memoryOwn = false;
  67. godot_icall_RefCounted_Disposed(this, ptr, !disposing);
  68. }
  69. else
  70. {
  71. godot_icall_Object_Disposed(this, ptr);
  72. }
  73. ptr = IntPtr.Zero;
  74. }
  75. _disposed = true;
  76. }
  77. /// <summary>
  78. /// Converts this <see cref="Object"/> to a string.
  79. /// </summary>
  80. /// <returns>A string representation of this object.</returns>
  81. public override string ToString()
  82. {
  83. return godot_icall_Object_ToString(GetPtr(this));
  84. }
  85. /// <summary>
  86. /// Returns a new <see cref="SignalAwaiter"/> awaiter configured to complete when the instance
  87. /// <paramref name="source"/> emits the signal specified by the <paramref name="signal"/> parameter.
  88. /// </summary>
  89. /// <param name="source">
  90. /// The instance the awaiter will be listening to.
  91. /// </param>
  92. /// <param name="signal">
  93. /// The signal the awaiter will be waiting for.
  94. /// </param>
  95. /// <example>
  96. /// This sample prints a message once every frame up to 100 times.
  97. /// <code>
  98. /// public override void _Ready()
  99. /// {
  100. /// for (int i = 0; i &lt; 100; i++)
  101. /// {
  102. /// await ToSignal(GetTree(), "process_frame");
  103. /// GD.Print($"Frame {i}");
  104. /// }
  105. /// }
  106. /// </code>
  107. /// </example>
  108. /// <returns>
  109. /// A <see cref="SignalAwaiter"/> that completes when
  110. /// <paramref name="source"/> emits the <paramref name="signal"/>.
  111. /// </returns>
  112. public SignalAwaiter ToSignal(Object source, StringName signal)
  113. {
  114. return new SignalAwaiter(source, signal, this);
  115. }
  116. /// <summary>
  117. /// Gets a new <see cref="DynamicGodotObject"/> associated with this instance.
  118. /// </summary>
  119. public dynamic DynamicObject => new DynamicGodotObject(this);
  120. internal static IntPtr __ClassDB_get_method(StringName type, string method)
  121. {
  122. return godot_icall_Object_ClassDB_get_method(StringName.GetPtr(type), method);
  123. }
  124. [MethodImpl(MethodImplOptions.InternalCall)]
  125. internal static extern IntPtr godot_icall_Object_Ctor(Object obj);
  126. [MethodImpl(MethodImplOptions.InternalCall)]
  127. internal static extern void godot_icall_Object_Disposed(Object obj, IntPtr ptr);
  128. [MethodImpl(MethodImplOptions.InternalCall)]
  129. internal static extern void godot_icall_RefCounted_Disposed(Object obj, IntPtr ptr, bool isFinalizer);
  130. [MethodImpl(MethodImplOptions.InternalCall)]
  131. internal static extern void godot_icall_Object_ConnectEventSignals(IntPtr obj);
  132. [MethodImpl(MethodImplOptions.InternalCall)]
  133. internal static extern string godot_icall_Object_ToString(IntPtr ptr);
  134. // Used by the generated API
  135. [MethodImpl(MethodImplOptions.InternalCall)]
  136. internal static extern IntPtr godot_icall_Object_ClassDB_get_method(IntPtr type, string method);
  137. }
  138. }