signal_awaiter_utils.cpp 5.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150
  1. /*************************************************************************/
  2. /* signal_awaiter_utils.cpp */
  3. /*************************************************************************/
  4. /* This file is part of: */
  5. /* GODOT ENGINE */
  6. /* https://godotengine.org */
  7. /*************************************************************************/
  8. /* Copyright (c) 2007-2019 Juan Linietsky, Ariel Manzur. */
  9. /* Copyright (c) 2014-2019 Godot Engine contributors (cf. AUTHORS.md) */
  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_class.h"
  33. #include "mono_gd/gd_mono_marshal.h"
  34. #include "mono_gd/gd_mono_utils.h"
  35. namespace SignalAwaiterUtils {
  36. Error connect_signal_awaiter(Object *p_source, const String &p_signal, Object *p_target, MonoObject *p_awaiter) {
  37. ERR_FAIL_NULL_V(p_source, ERR_INVALID_DATA);
  38. ERR_FAIL_NULL_V(p_target, ERR_INVALID_DATA);
  39. uint32_t awaiter_handle = MonoGCHandle::make_strong_handle(p_awaiter);
  40. Ref<SignalAwaiterHandle> sa_con = memnew(SignalAwaiterHandle(awaiter_handle));
  41. #ifdef DEBUG_ENABLED
  42. sa_con->set_connection_target(p_target);
  43. #endif
  44. Vector<Variant> binds;
  45. binds.push_back(sa_con);
  46. Error err = p_source->connect(p_signal, sa_con.ptr(),
  47. CSharpLanguage::get_singleton()->get_string_names()._signal_callback,
  48. binds, Object::CONNECT_ONESHOT);
  49. if (err != OK) {
  50. // Set it as completed to prevent it from calling the failure callback when released.
  51. // The awaiter will be aware of the failure by checking the returned error.
  52. sa_con->set_completed(true);
  53. }
  54. return err;
  55. }
  56. } // namespace SignalAwaiterUtils
  57. Variant SignalAwaiterHandle::_signal_callback(const Variant **p_args, int p_argcount, Variant::CallError &r_error) {
  58. #ifdef DEBUG_ENABLED
  59. if (conn_target_id && !ObjectDB::get_instance(conn_target_id)) {
  60. ERR_EXPLAIN("Resumed after await, but class instance is gone");
  61. ERR_FAIL_V(Variant());
  62. }
  63. #endif
  64. if (p_argcount < 1) {
  65. r_error.error = Variant::CallError::CALL_ERROR_TOO_FEW_ARGUMENTS;
  66. r_error.argument = 1;
  67. return Variant();
  68. }
  69. Ref<SignalAwaiterHandle> self = *p_args[p_argcount - 1];
  70. if (self.is_null()) {
  71. r_error.error = Variant::CallError::CALL_ERROR_INVALID_ARGUMENT;
  72. r_error.argument = p_argcount - 1;
  73. r_error.expected = Variant::OBJECT;
  74. return Variant();
  75. }
  76. set_completed(true);
  77. int signal_argc = p_argcount - 1;
  78. MonoArray *signal_args = mono_array_new(SCRIPTS_DOMAIN, CACHED_CLASS_RAW(MonoObject), signal_argc);
  79. for (int i = 0; i < signal_argc; i++) {
  80. MonoObject *boxed = GDMonoMarshal::variant_to_mono_object(*p_args[i]);
  81. mono_array_set(signal_args, MonoObject *, i, boxed);
  82. }
  83. GDMonoUtils::SignalAwaiter_SignalCallback thunk = CACHED_METHOD_THUNK(SignalAwaiter, SignalCallback);
  84. MonoException *exc = NULL;
  85. GD_MONO_BEGIN_RUNTIME_INVOKE;
  86. thunk(get_target(), signal_args, (MonoObject **)&exc);
  87. GD_MONO_END_RUNTIME_INVOKE;
  88. if (exc) {
  89. GDMonoUtils::set_pending_exception(exc);
  90. ERR_FAIL_V(Variant());
  91. }
  92. return Variant();
  93. }
  94. void SignalAwaiterHandle::_bind_methods() {
  95. ClassDB::bind_vararg_method(METHOD_FLAGS_DEFAULT, "_signal_callback", &SignalAwaiterHandle::_signal_callback, MethodInfo("_signal_callback"));
  96. }
  97. SignalAwaiterHandle::SignalAwaiterHandle(uint32_t p_managed_handle) :
  98. MonoGCHandle(p_managed_handle) {
  99. #ifdef DEBUG_ENABLED
  100. conn_target_id = 0;
  101. #endif
  102. }
  103. SignalAwaiterHandle::~SignalAwaiterHandle() {
  104. if (!completed) {
  105. GDMonoUtils::SignalAwaiter_FailureCallback thunk = CACHED_METHOD_THUNK(SignalAwaiter, FailureCallback);
  106. MonoObject *awaiter = get_target();
  107. if (awaiter) {
  108. MonoException *exc = NULL;
  109. GD_MONO_BEGIN_RUNTIME_INVOKE;
  110. thunk(awaiter, (MonoObject **)&exc);
  111. GD_MONO_END_RUNTIME_INVOKE;
  112. if (exc) {
  113. GDMonoUtils::set_pending_exception(exc);
  114. ERR_FAIL_V();
  115. }
  116. }
  117. }
  118. }