managed_callable.cpp 5.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135
  1. /**************************************************************************/
  2. /* managed_callable.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 "managed_callable.h"
  31. #include "csharp_script.h"
  32. #include "mono_gd/gd_mono_cache.h"
  33. #ifdef GD_MONO_HOT_RELOAD
  34. SelfList<ManagedCallable>::List ManagedCallable::instances;
  35. RBMap<ManagedCallable *, Array> ManagedCallable::instances_pending_reload;
  36. Mutex ManagedCallable::instances_mutex;
  37. #endif
  38. bool ManagedCallable::compare_equal(const CallableCustom *p_a, const CallableCustom *p_b) {
  39. const ManagedCallable *a = static_cast<const ManagedCallable *>(p_a);
  40. const ManagedCallable *b = static_cast<const ManagedCallable *>(p_b);
  41. if (a->delegate_handle.value == b->delegate_handle.value) {
  42. return true;
  43. }
  44. if (!a->delegate_handle.value || !b->delegate_handle.value) {
  45. return false;
  46. }
  47. // Call Delegate's 'Equals'
  48. return GDMonoCache::managed_callbacks.DelegateUtils_DelegateEquals(
  49. a->delegate_handle, b->delegate_handle);
  50. }
  51. bool ManagedCallable::compare_less(const CallableCustom *p_a, const CallableCustom *p_b) {
  52. if (compare_equal(p_a, p_b)) {
  53. return false;
  54. }
  55. return p_a < p_b;
  56. }
  57. uint32_t ManagedCallable::hash() const {
  58. return GDMonoCache::managed_callbacks.DelegateUtils_DelegateHash(delegate_handle);
  59. }
  60. String ManagedCallable::get_as_text() const {
  61. return "Delegate::Invoke";
  62. }
  63. CallableCustom::CompareEqualFunc ManagedCallable::get_compare_equal_func() const {
  64. return compare_equal_func_ptr;
  65. }
  66. CallableCustom::CompareLessFunc ManagedCallable::get_compare_less_func() const {
  67. return compare_less_func_ptr;
  68. }
  69. ObjectID ManagedCallable::get_object() const {
  70. if (object_id != ObjectID()) {
  71. return object_id;
  72. }
  73. return CSharpLanguage::get_singleton()->get_managed_callable_middleman()->get_instance_id();
  74. }
  75. int ManagedCallable::get_argument_count(bool &r_is_valid) const {
  76. return GDMonoCache::managed_callbacks.DelegateUtils_GetArgumentCount(delegate_handle, &r_is_valid);
  77. }
  78. void ManagedCallable::call(const Variant **p_arguments, int p_argcount, Variant &r_return_value, Callable::CallError &r_call_error) const {
  79. r_call_error.error = Callable::CallError::CALL_ERROR_INVALID_METHOD; // Can't find anything better
  80. r_return_value = Variant();
  81. ERR_FAIL_NULL(delegate_handle.value);
  82. GDMonoCache::managed_callbacks.DelegateUtils_InvokeWithVariantArgs(
  83. delegate_handle, trampoline, p_arguments, p_argcount, &r_return_value);
  84. r_call_error.error = Callable::CallError::CALL_OK;
  85. }
  86. void ManagedCallable::release_delegate_handle() {
  87. if (delegate_handle.value) {
  88. GDMonoCache::managed_callbacks.GCHandleBridge_FreeGCHandle(delegate_handle);
  89. delegate_handle = { nullptr };
  90. }
  91. }
  92. // Why you do this clang-format...
  93. /* clang-format off */
  94. ManagedCallable::ManagedCallable(GCHandleIntPtr p_delegate_handle, void *p_trampoline, ObjectID p_object_id) :
  95. delegate_handle(p_delegate_handle), trampoline(p_trampoline), object_id(p_object_id) {
  96. #ifdef GD_MONO_HOT_RELOAD
  97. {
  98. MutexLock lock(instances_mutex);
  99. instances.add(&self_instance);
  100. }
  101. #endif
  102. }
  103. /* clang-format on */
  104. ManagedCallable::~ManagedCallable() {
  105. #ifdef GD_MONO_HOT_RELOAD
  106. {
  107. MutexLock lock(instances_mutex);
  108. instances.remove(&self_instance);
  109. instances_pending_reload.erase(this);
  110. }
  111. #endif
  112. release_delegate_handle();
  113. }