signal_awaiter_utils.cpp 5.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145
  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. Ref<SignalAwaiterHandle> sa_con = memnew(SignalAwaiterHandle(p_awaiter));
  40. #ifdef DEBUG_ENABLED
  41. sa_con->set_connection_target(p_target);
  42. #endif
  43. Vector<Variant> binds;
  44. binds.push_back(sa_con);
  45. Error err = p_source->connect(p_signal, sa_con.ptr(),
  46. CSharpLanguage::get_singleton()->get_string_names()._signal_callback,
  47. binds, Object::CONNECT_ONESHOT);
  48. if (err != OK) {
  49. // Set it as completed to prevent it from calling the failure callback when released.
  50. // The awaiter will be aware of the failure by checking the returned error.
  51. sa_con->set_completed(true);
  52. }
  53. return err;
  54. }
  55. } // namespace SignalAwaiterUtils
  56. Variant SignalAwaiterHandle::_signal_callback(const Variant **p_args, int p_argcount, Variant::CallError &r_error) {
  57. #ifdef DEBUG_ENABLED
  58. if (conn_target_id && !ObjectDB::get_instance(conn_target_id)) {
  59. ERR_EXPLAIN("Resumed after await, but class instance is gone");
  60. ERR_FAIL_V(Variant());
  61. }
  62. #endif
  63. if (p_argcount < 1) {
  64. r_error.error = Variant::CallError::CALL_ERROR_TOO_FEW_ARGUMENTS;
  65. r_error.argument = 1;
  66. return Variant();
  67. }
  68. Ref<SignalAwaiterHandle> self = *p_args[p_argcount - 1];
  69. if (self.is_null()) {
  70. r_error.error = Variant::CallError::CALL_ERROR_INVALID_ARGUMENT;
  71. r_error.argument = p_argcount - 1;
  72. r_error.expected = Variant::OBJECT;
  73. return Variant();
  74. }
  75. set_completed(true);
  76. int signal_argc = p_argcount - 1;
  77. MonoArray *signal_args = mono_array_new(SCRIPTS_DOMAIN, CACHED_CLASS_RAW(MonoObject), signal_argc);
  78. for (int i = 0; i < signal_argc; i++) {
  79. MonoObject *boxed = GDMonoMarshal::variant_to_mono_object(*p_args[i]);
  80. mono_array_set(signal_args, MonoObject *, i, boxed);
  81. }
  82. MonoException *exc = NULL;
  83. GD_MONO_BEGIN_RUNTIME_INVOKE;
  84. invoke_method_thunk(CACHED_METHOD_THUNK(SignalAwaiter, SignalCallback), get_target(), signal_args, (MonoObject **)&exc);
  85. GD_MONO_END_RUNTIME_INVOKE;
  86. if (exc) {
  87. GDMonoUtils::set_pending_exception(exc);
  88. ERR_FAIL_V(Variant());
  89. }
  90. return Variant();
  91. }
  92. void SignalAwaiterHandle::_bind_methods() {
  93. ClassDB::bind_vararg_method(METHOD_FLAGS_DEFAULT, "_signal_callback", &SignalAwaiterHandle::_signal_callback, MethodInfo("_signal_callback"));
  94. }
  95. SignalAwaiterHandle::SignalAwaiterHandle(MonoObject *p_managed) :
  96. MonoGCHandle(MonoGCHandle::new_strong_handle(p_managed), STRONG_HANDLE) {
  97. #ifdef DEBUG_ENABLED
  98. conn_target_id = 0;
  99. #endif
  100. }
  101. SignalAwaiterHandle::~SignalAwaiterHandle() {
  102. if (!completed) {
  103. MonoObject *awaiter = get_target();
  104. if (awaiter) {
  105. MonoException *exc = NULL;
  106. GD_MONO_BEGIN_RUNTIME_INVOKE;
  107. invoke_method_thunk(CACHED_METHOD_THUNK(SignalAwaiter, FailureCallback), awaiter, (MonoObject **)&exc);
  108. GD_MONO_END_RUNTIME_INVOKE;
  109. if (exc) {
  110. GDMonoUtils::set_pending_exception(exc);
  111. ERR_FAIL();
  112. }
  113. }
  114. }
  115. }