managed_callable.cpp 5.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148
  1. /*************************************************************************/
  2. /* managed_callable.cpp */
  3. /*************************************************************************/
  4. /* This file is part of: */
  5. /* GODOT ENGINE */
  6. /* https://godotengine.org */
  7. /*************************************************************************/
  8. /* Copyright (c) 2007-2021 Juan Linietsky, Ariel Manzur. */
  9. /* Copyright (c) 2014-2021 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 "managed_callable.h"
  31. #include "csharp_script.h"
  32. #include "mono_gd/gd_mono_marshal.h"
  33. #include "mono_gd/gd_mono_utils.h"
  34. #ifdef GD_MONO_HOT_RELOAD
  35. SelfList<ManagedCallable>::List ManagedCallable::instances;
  36. Map<ManagedCallable *, Array> ManagedCallable::instances_pending_reload;
  37. Mutex ManagedCallable::instances_mutex;
  38. #endif
  39. bool ManagedCallable::compare_equal(const CallableCustom *p_a, const CallableCustom *p_b) {
  40. const ManagedCallable *a = static_cast<const ManagedCallable *>(p_a);
  41. const ManagedCallable *b = static_cast<const ManagedCallable *>(p_b);
  42. MonoDelegate *delegate_a = (MonoDelegate *)a->delegate_handle.get_target();
  43. MonoDelegate *delegate_b = (MonoDelegate *)b->delegate_handle.get_target();
  44. if (!delegate_a || !delegate_b) {
  45. if (!delegate_a && !delegate_b) {
  46. return true;
  47. }
  48. return false;
  49. }
  50. // Call Delegate's 'Equals'
  51. return GDMonoUtils::mono_delegate_equal(delegate_a, delegate_b);
  52. }
  53. bool ManagedCallable::compare_less(const CallableCustom *p_a, const CallableCustom *p_b) {
  54. if (compare_equal(p_a, p_b)) {
  55. return false;
  56. }
  57. return p_a < p_b;
  58. }
  59. uint32_t ManagedCallable::hash() const {
  60. // hmm
  61. uint32_t hash = delegate_invoke->get_name().hash();
  62. return hash_djb2_one_64(delegate_handle.handle, hash);
  63. }
  64. String ManagedCallable::get_as_text() const {
  65. return "Delegate::Invoke";
  66. }
  67. CallableCustom::CompareEqualFunc ManagedCallable::get_compare_equal_func() const {
  68. return compare_equal_func_ptr;
  69. }
  70. CallableCustom::CompareLessFunc ManagedCallable::get_compare_less_func() const {
  71. return compare_less_func_ptr;
  72. }
  73. ObjectID ManagedCallable::get_object() const {
  74. // TODO: If the delegate target extends Godot.Object, use that instead!
  75. return CSharpLanguage::get_singleton()->get_managed_callable_middleman()->get_instance_id();
  76. }
  77. void ManagedCallable::call(const Variant **p_arguments, int p_argcount, Variant &r_return_value, Callable::CallError &r_call_error) const {
  78. r_call_error.error = Callable::CallError::CALL_ERROR_INVALID_METHOD; // Can't find anything better
  79. r_return_value = Variant();
  80. #ifdef GD_MONO_HOT_RELOAD
  81. // Lost during hot-reload
  82. ERR_FAIL_NULL(delegate_invoke);
  83. ERR_FAIL_COND(delegate_handle.is_released());
  84. #endif
  85. ERR_FAIL_COND(delegate_invoke->get_parameters_count() < p_argcount);
  86. MonoObject *delegate = delegate_handle.get_target();
  87. MonoException *exc = nullptr;
  88. MonoObject *ret = delegate_invoke->invoke(delegate, p_arguments, &exc);
  89. if (exc) {
  90. GDMonoUtils::set_pending_exception(exc);
  91. } else {
  92. r_return_value = GDMonoMarshal::mono_object_to_variant(ret);
  93. r_call_error.error = Callable::CallError::CALL_OK;
  94. }
  95. }
  96. void ManagedCallable::set_delegate(MonoDelegate *p_delegate) {
  97. delegate_handle = MonoGCHandleData::new_strong_handle((MonoObject *)p_delegate);
  98. MonoMethod *delegate_invoke_raw = mono_get_delegate_invoke(mono_object_get_class((MonoObject *)p_delegate));
  99. const StringName &delegate_invoke_name = CSharpLanguage::get_singleton()->get_string_names().delegate_invoke_method_name;
  100. delegate_invoke = memnew(GDMonoMethod(delegate_invoke_name, delegate_invoke_raw)); // TODO: Use pooling for this GDMonoMethod instances
  101. }
  102. ManagedCallable::ManagedCallable(MonoDelegate *p_delegate) {
  103. #ifdef DEBUG_ENABLED
  104. CRASH_COND(p_delegate == nullptr);
  105. #endif
  106. set_delegate(p_delegate);
  107. #ifdef GD_MONO_HOT_RELOAD
  108. {
  109. MutexLock lock(instances_mutex);
  110. instances.add(&self_instance);
  111. }
  112. #endif
  113. }
  114. ManagedCallable::~ManagedCallable() {
  115. #ifdef GD_MONO_HOT_RELOAD
  116. {
  117. MutexLock lock(instances_mutex);
  118. instances.remove(&self_instance);
  119. instances_pending_reload.erase(this);
  120. }
  121. #endif
  122. delegate_handle.release();
  123. }