2018-09-04 05:40:41 +02:00
|
|
|
/*************************************************************************/
|
|
|
|
/* base_object_glue.cpp */
|
|
|
|
/*************************************************************************/
|
|
|
|
/* This file is part of: */
|
|
|
|
/* GODOT ENGINE */
|
|
|
|
/* https://godotengine.org */
|
|
|
|
/*************************************************************************/
|
2022-01-03 21:27:34 +01:00
|
|
|
/* Copyright (c) 2007-2022 Juan Linietsky, Ariel Manzur. */
|
|
|
|
/* Copyright (c) 2014-2022 Godot Engine contributors (cf. AUTHORS.md). */
|
2018-09-04 05:40:41 +02:00
|
|
|
/* */
|
|
|
|
/* Permission is hereby granted, free of charge, to any person obtaining */
|
|
|
|
/* a copy of this software and associated documentation files (the */
|
|
|
|
/* "Software"), to deal in the Software without restriction, including */
|
|
|
|
/* without limitation the rights to use, copy, modify, merge, publish, */
|
|
|
|
/* distribute, sublicense, and/or sell copies of the Software, and to */
|
|
|
|
/* permit persons to whom the Software is furnished to do so, subject to */
|
|
|
|
/* the following conditions: */
|
|
|
|
/* */
|
|
|
|
/* The above copyright notice and this permission notice shall be */
|
|
|
|
/* included in all copies or substantial portions of the Software. */
|
|
|
|
/* */
|
|
|
|
/* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, */
|
|
|
|
/* EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF */
|
|
|
|
/* MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.*/
|
|
|
|
/* IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY */
|
|
|
|
/* CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, */
|
|
|
|
/* TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE */
|
|
|
|
/* SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. */
|
|
|
|
/*************************************************************************/
|
|
|
|
|
2020-11-07 19:33:38 -03:00
|
|
|
#include "core/object/class_db.h"
|
2021-06-04 18:03:15 +02:00
|
|
|
#include "core/object/ref_counted.h"
|
2020-11-07 19:33:38 -03:00
|
|
|
#include "core/string/string_name.h"
|
2018-09-04 05:40:41 +02:00
|
|
|
|
2018-09-12 02:41:54 +02:00
|
|
|
#include "../csharp_script.h"
|
2019-11-10 17:10:38 +01:00
|
|
|
#include "../mono_gd/gd_mono_cache.h"
|
2018-09-04 05:40:41 +02:00
|
|
|
#include "../mono_gd/gd_mono_internals.h"
|
|
|
|
#include "../mono_gd/gd_mono_utils.h"
|
|
|
|
#include "../signal_awaiter_utils.h"
|
|
|
|
|
2021-09-12 20:21:15 +02:00
|
|
|
void godot_icall_Object_Disposed(Object *p_ptr) {
|
2018-09-04 05:40:41 +02:00
|
|
|
#ifdef DEBUG_ENABLED
|
2020-04-02 01:20:12 +02:00
|
|
|
CRASH_COND(p_ptr == nullptr);
|
2018-09-04 05:40:41 +02:00
|
|
|
#endif
|
2018-09-12 02:41:54 +02:00
|
|
|
|
|
|
|
if (p_ptr->get_script_instance()) {
|
|
|
|
CSharpInstance *cs_instance = CAST_CSHARP_INSTANCE(p_ptr->get_script_instance());
|
|
|
|
if (cs_instance) {
|
2018-12-01 02:23:55 +01:00
|
|
|
if (!cs_instance->is_destructing_script_instance()) {
|
2021-09-12 20:21:15 +02:00
|
|
|
cs_instance->mono_object_disposed();
|
2020-04-02 01:20:12 +02:00
|
|
|
p_ptr->set_script_instance(nullptr);
|
2018-12-01 02:23:55 +01:00
|
|
|
}
|
2018-09-12 02:41:54 +02:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
}
|
2021-08-16 17:16:36 +02:00
|
|
|
|
|
|
|
void *data = CSharpLanguage::get_existing_instance_binding(p_ptr);
|
2018-09-12 02:41:54 +02:00
|
|
|
|
|
|
|
if (data) {
|
2022-05-13 15:04:37 +02:00
|
|
|
CSharpScriptBinding &script_binding = ((RBMap<Object *, CSharpScriptBinding>::Element *)data)->get();
|
2019-02-03 06:35:22 +01:00
|
|
|
if (script_binding.inited) {
|
2019-12-11 17:08:40 +01:00
|
|
|
MonoGCHandleData &gchandle = script_binding.gchandle;
|
|
|
|
if (!gchandle.is_released()) {
|
2021-09-12 20:21:15 +02:00
|
|
|
CSharpLanguage::release_script_gchandle(nullptr, gchandle);
|
|
|
|
script_binding.inited = false;
|
2019-02-03 06:35:22 +01:00
|
|
|
}
|
2018-09-12 02:41:54 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-09-12 20:21:15 +02:00
|
|
|
void godot_icall_RefCounted_Disposed(Object *p_ptr, MonoBoolean p_is_finalizer) {
|
2018-09-12 02:41:54 +02:00
|
|
|
#ifdef DEBUG_ENABLED
|
2020-04-02 01:20:12 +02:00
|
|
|
CRASH_COND(p_ptr == nullptr);
|
2021-06-04 18:03:15 +02:00
|
|
|
// This is only called with RefCounted derived classes
|
|
|
|
CRASH_COND(!Object::cast_to<RefCounted>(p_ptr));
|
2018-09-12 02:41:54 +02:00
|
|
|
#endif
|
2021-08-16 17:16:36 +02:00
|
|
|
|
2021-06-04 18:03:15 +02:00
|
|
|
RefCounted *rc = static_cast<RefCounted *>(p_ptr);
|
2018-09-12 02:41:54 +02:00
|
|
|
|
2021-06-04 18:03:15 +02:00
|
|
|
if (rc->get_script_instance()) {
|
2021-06-13 11:15:44 +02:00
|
|
|
CSharpInstance *cs_instance = CAST_CSHARP_INSTANCE(rc->get_script_instance());
|
2018-09-12 02:41:54 +02:00
|
|
|
if (cs_instance) {
|
2018-12-01 02:23:55 +01:00
|
|
|
if (!cs_instance->is_destructing_script_instance()) {
|
2019-02-03 06:35:22 +01:00
|
|
|
bool delete_owner;
|
|
|
|
bool remove_script_instance;
|
|
|
|
|
2021-09-12 20:21:15 +02:00
|
|
|
cs_instance->mono_object_disposed_baseref(p_is_finalizer, delete_owner, remove_script_instance);
|
2019-02-03 06:35:22 +01:00
|
|
|
|
|
|
|
if (delete_owner) {
|
2021-06-04 18:03:15 +02:00
|
|
|
memdelete(rc);
|
2019-02-03 06:35:22 +01:00
|
|
|
} else if (remove_script_instance) {
|
2021-06-04 18:03:15 +02:00
|
|
|
rc->set_script_instance(nullptr);
|
2018-12-01 02:23:55 +01:00
|
|
|
}
|
2018-09-12 02:41:54 +02:00
|
|
|
}
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// Unsafe refcount decrement. The managed instance also counts as a reference.
|
|
|
|
// See: CSharpLanguage::alloc_instance_binding_data(Object *p_object)
|
2021-06-13 11:15:44 +02:00
|
|
|
CSharpLanguage::get_singleton()->pre_unsafe_unreference(rc);
|
|
|
|
if (rc->unreference()) {
|
|
|
|
memdelete(rc);
|
2018-09-12 02:41:54 +02:00
|
|
|
} else {
|
2021-08-16 17:16:36 +02:00
|
|
|
void *data = CSharpLanguage::get_existing_instance_binding(rc);
|
2018-09-12 02:41:54 +02:00
|
|
|
|
|
|
|
if (data) {
|
2022-05-13 15:04:37 +02:00
|
|
|
CSharpScriptBinding &script_binding = ((RBMap<Object *, CSharpScriptBinding>::Element *)data)->get();
|
2019-02-03 06:35:22 +01:00
|
|
|
if (script_binding.inited) {
|
2019-12-11 17:08:40 +01:00
|
|
|
MonoGCHandleData &gchandle = script_binding.gchandle;
|
|
|
|
if (!gchandle.is_released()) {
|
2021-09-12 20:21:15 +02:00
|
|
|
CSharpLanguage::release_script_gchandle(nullptr, gchandle);
|
|
|
|
script_binding.inited = false;
|
2019-02-03 06:35:22 +01:00
|
|
|
}
|
2018-09-12 02:41:54 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2018-09-04 05:40:41 +02:00
|
|
|
}
|
|
|
|
|
2021-09-12 20:21:15 +02:00
|
|
|
void godot_icall_Object_ConnectEventSignal(Object *p_ptr, const StringName *p_event_signal) {
|
2020-03-14 19:20:17 +01:00
|
|
|
CSharpInstance *csharp_instance = CAST_CSHARP_INSTANCE(p_ptr->get_script_instance());
|
|
|
|
if (csharp_instance) {
|
2021-09-12 20:21:15 +02:00
|
|
|
csharp_instance->connect_event_signal(*p_event_signal);
|
2020-03-14 19:20:17 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-09-12 20:21:15 +02:00
|
|
|
int32_t godot_icall_SignalAwaiter_connect(Object *p_source, StringName *p_signal, Object *p_target, GCHandleIntPtr p_awaiter_handle_ptr) {
|
2020-03-14 19:20:17 +01:00
|
|
|
StringName signal = p_signal ? *p_signal : StringName();
|
2021-09-12 20:21:15 +02:00
|
|
|
return (int32_t)gd_mono_connect_signal_awaiter(p_source, signal, p_target, p_awaiter_handle_ptr);
|
2018-09-04 05:40:41 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
void godot_register_object_icalls() {
|
2020-12-05 00:05:01 +01:00
|
|
|
GDMonoUtils::add_internal_call("Godot.Object::godot_icall_Object_Disposed", godot_icall_Object_Disposed);
|
2021-06-13 11:15:44 +02:00
|
|
|
GDMonoUtils::add_internal_call("Godot.Object::godot_icall_RefCounted_Disposed", godot_icall_RefCounted_Disposed);
|
2021-09-12 20:21:15 +02:00
|
|
|
GDMonoUtils::add_internal_call("Godot.Object::godot_icall_Object_ConnectEventSignal", godot_icall_Object_ConnectEventSignal);
|
2020-12-05 00:05:01 +01:00
|
|
|
GDMonoUtils::add_internal_call("Godot.SignalAwaiter::godot_icall_SignalAwaiter_connect", godot_icall_SignalAwaiter_connect);
|
2018-09-04 05:40:41 +02:00
|
|
|
}
|