signal_awaiter_utils.cpp 7.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205
  1. /**************************************************************************/
  2. /* signal_awaiter_utils.cpp */
  3. /**************************************************************************/
  4. /* This file is part of: */
  5. /* GODOT ENGINE */
  6. /* https://godotengine.org */
  7. /**************************************************************************/
  8. /* Copyright (c) 2014-present Godot Engine contributors (see AUTHORS.md). */
  9. /* Copyright (c) 2007-2014 Juan Linietsky, Ariel Manzur. */
  10. /* */
  11. /* Permission is hereby granted, free of charge, to any person obtaining */
  12. /* a copy of this software and associated documentation files (the */
  13. /* "Software"), to deal in the Software without restriction, including */
  14. /* without limitation the rights to use, copy, modify, merge, publish, */
  15. /* distribute, sublicense, and/or sell copies of the Software, and to */
  16. /* permit persons to whom the Software is furnished to do so, subject to */
  17. /* the following conditions: */
  18. /* */
  19. /* The above copyright notice and this permission notice shall be */
  20. /* included in all copies or substantial portions of the Software. */
  21. /* */
  22. /* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, */
  23. /* EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF */
  24. /* MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. */
  25. /* IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY */
  26. /* CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, */
  27. /* TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE */
  28. /* SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. */
  29. /**************************************************************************/
  30. #include "signal_awaiter_utils.h"
  31. #include "csharp_script.h"
  32. #include "mono_gd/gd_mono_cache.h"
  33. Error gd_mono_connect_signal_awaiter(Object *p_source, const StringName &p_signal, Object *p_target, GCHandleIntPtr p_awaiter_handle_ptr) {
  34. ERR_FAIL_NULL_V(p_source, ERR_INVALID_DATA);
  35. ERR_FAIL_NULL_V(p_target, ERR_INVALID_DATA);
  36. // TODO: Use pooling for ManagedCallable instances.
  37. MonoGCHandleData awaiter_handle(p_awaiter_handle_ptr, gdmono::GCHandleType::STRONG_HANDLE);
  38. SignalAwaiterCallable *awaiter_callable = memnew(SignalAwaiterCallable(p_target, awaiter_handle, p_signal));
  39. Callable callable = Callable(awaiter_callable);
  40. return p_source->connect(p_signal, callable, Object::CONNECT_ONE_SHOT);
  41. }
  42. bool SignalAwaiterCallable::compare_equal(const CallableCustom *p_a, const CallableCustom *p_b) {
  43. // Only called if both instances are of type SignalAwaiterCallable. Static cast is safe.
  44. const SignalAwaiterCallable *a = static_cast<const SignalAwaiterCallable *>(p_a);
  45. const SignalAwaiterCallable *b = static_cast<const SignalAwaiterCallable *>(p_b);
  46. return a->awaiter_handle.handle.value == b->awaiter_handle.handle.value;
  47. }
  48. bool SignalAwaiterCallable::compare_less(const CallableCustom *p_a, const CallableCustom *p_b) {
  49. if (compare_equal(p_a, p_b)) {
  50. return false;
  51. }
  52. return p_a < p_b;
  53. }
  54. uint32_t SignalAwaiterCallable::hash() const {
  55. uint32_t hash = signal.hash();
  56. return hash_murmur3_one_64(target_id, hash);
  57. }
  58. String SignalAwaiterCallable::get_as_text() const {
  59. Object *base = ObjectDB::get_instance(target_id);
  60. if (base) {
  61. String class_name = base->get_class();
  62. Ref<Script> script = base->get_script();
  63. if (script.is_valid() && script->get_path().is_resource_file()) {
  64. class_name += "(" + script->get_path().get_file() + ")";
  65. }
  66. return class_name + "::SignalAwaiterMiddleman::" + String(signal);
  67. } else {
  68. return "null::SignalAwaiterMiddleman::" + String(signal);
  69. }
  70. }
  71. CallableCustom::CompareEqualFunc SignalAwaiterCallable::get_compare_equal_func() const {
  72. return compare_equal_func_ptr;
  73. }
  74. CallableCustom::CompareLessFunc SignalAwaiterCallable::get_compare_less_func() const {
  75. return compare_less_func_ptr;
  76. }
  77. ObjectID SignalAwaiterCallable::get_object() const {
  78. return target_id;
  79. }
  80. StringName SignalAwaiterCallable::get_signal() const {
  81. return signal;
  82. }
  83. void SignalAwaiterCallable::call(const Variant **p_arguments, int p_argcount, Variant &r_return_value, Callable::CallError &r_call_error) const {
  84. r_call_error.error = Callable::CallError::CALL_ERROR_INVALID_METHOD; // Can't find anything better
  85. r_return_value = Variant();
  86. #ifdef DEBUG_ENABLED
  87. ERR_FAIL_COND_MSG(target_id.is_valid() && !ObjectDB::get_instance(target_id),
  88. "Resumed after await, but class instance is gone.");
  89. #endif
  90. bool awaiter_is_null = false;
  91. GDMonoCache::managed_callbacks.SignalAwaiter_SignalCallback(awaiter_handle.get_intptr(), p_arguments, p_argcount, &awaiter_is_null);
  92. if (awaiter_is_null) {
  93. r_call_error.error = Callable::CallError::CALL_ERROR_INSTANCE_IS_NULL;
  94. return;
  95. }
  96. r_call_error.error = Callable::CallError::CALL_OK;
  97. }
  98. SignalAwaiterCallable::SignalAwaiterCallable(Object *p_target, MonoGCHandleData p_awaiter_handle, const StringName &p_signal) :
  99. target_id(p_target->get_instance_id()),
  100. awaiter_handle(p_awaiter_handle),
  101. signal(p_signal) {
  102. }
  103. SignalAwaiterCallable::~SignalAwaiterCallable() {
  104. awaiter_handle.release();
  105. }
  106. bool EventSignalCallable::compare_equal(const CallableCustom *p_a, const CallableCustom *p_b) {
  107. const EventSignalCallable *a = static_cast<const EventSignalCallable *>(p_a);
  108. const EventSignalCallable *b = static_cast<const EventSignalCallable *>(p_b);
  109. if (a->owner != b->owner) {
  110. return false;
  111. }
  112. if (a->event_signal_name != b->event_signal_name) {
  113. return false;
  114. }
  115. return true;
  116. }
  117. bool EventSignalCallable::compare_less(const CallableCustom *p_a, const CallableCustom *p_b) {
  118. if (compare_equal(p_a, p_b)) {
  119. return false;
  120. }
  121. return p_a < p_b;
  122. }
  123. uint32_t EventSignalCallable::hash() const {
  124. uint32_t hash = event_signal_name.hash();
  125. return hash_murmur3_one_64(owner->get_instance_id(), hash);
  126. }
  127. String EventSignalCallable::get_as_text() const {
  128. String class_name = owner->get_class();
  129. Ref<Script> script = owner->get_script();
  130. if (script.is_valid() && script->get_path().is_resource_file()) {
  131. class_name += "(" + script->get_path().get_file() + ")";
  132. }
  133. return class_name + "::EventSignalMiddleman::" + String(event_signal_name);
  134. }
  135. CallableCustom::CompareEqualFunc EventSignalCallable::get_compare_equal_func() const {
  136. return compare_equal_func_ptr;
  137. }
  138. CallableCustom::CompareLessFunc EventSignalCallable::get_compare_less_func() const {
  139. return compare_less_func_ptr;
  140. }
  141. ObjectID EventSignalCallable::get_object() const {
  142. return owner->get_instance_id();
  143. }
  144. StringName EventSignalCallable::get_signal() const {
  145. return event_signal_name;
  146. }
  147. void EventSignalCallable::call(const Variant **p_arguments, int p_argcount, Variant &r_return_value, Callable::CallError &r_call_error) const {
  148. r_call_error.error = Callable::CallError::CALL_ERROR_INVALID_METHOD; // Can't find anything better
  149. r_return_value = Variant();
  150. CSharpInstance *csharp_instance = CAST_CSHARP_INSTANCE(owner->get_script_instance());
  151. ERR_FAIL_NULL(csharp_instance);
  152. GCHandleIntPtr owner_gchandle_intptr = csharp_instance->get_gchandle_intptr();
  153. bool awaiter_is_null = false;
  154. GDMonoCache::managed_callbacks.ScriptManagerBridge_RaiseEventSignal(
  155. owner_gchandle_intptr, &event_signal_name,
  156. p_arguments, p_argcount, &awaiter_is_null);
  157. if (awaiter_is_null) {
  158. r_call_error.error = Callable::CallError::CALL_ERROR_INSTANCE_IS_NULL;
  159. return;
  160. }
  161. r_call_error.error = Callable::CallError::CALL_OK;
  162. }
  163. EventSignalCallable::EventSignalCallable(Object *p_owner, const StringName &p_event_signal_name) :
  164. owner(p_owner),
  165. event_signal_name(p_event_signal_name) {
  166. }