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