2018-09-04 11:40:41 +08:00
|
|
|
/*************************************************************************/
|
|
|
|
/* base_object_glue.cpp */
|
|
|
|
/*************************************************************************/
|
|
|
|
/* This file is part of: */
|
|
|
|
/* GODOT ENGINE */
|
|
|
|
/* https://godotengine.org */
|
|
|
|
/*************************************************************************/
|
2021-01-02 03:13:46 +08:00
|
|
|
/* Copyright (c) 2007-2021 Juan Linietsky, Ariel Manzur. */
|
|
|
|
/* Copyright (c) 2014-2021 Godot Engine contributors (cf. AUTHORS.md). */
|
2018-09-04 11:40:41 +08: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. */
|
|
|
|
/*************************************************************************/
|
|
|
|
|
|
|
|
#ifdef MONO_GLUE_ENABLED
|
|
|
|
|
2020-11-08 06:33:38 +08:00
|
|
|
#include "core/object/class_db.h"
|
2021-06-05 00:03:15 +08:00
|
|
|
#include "core/object/ref_counted.h"
|
2020-11-08 06:33:38 +08:00
|
|
|
#include "core/string/string_name.h"
|
2018-09-04 11:40:41 +08:00
|
|
|
|
2018-09-12 08:41:54 +08:00
|
|
|
#include "../csharp_script.h"
|
2019-11-11 00:10:38 +08:00
|
|
|
#include "../mono_gd/gd_mono_cache.h"
|
2019-03-29 03:01:54 +08:00
|
|
|
#include "../mono_gd/gd_mono_class.h"
|
2018-09-04 11:40:41 +08:00
|
|
|
#include "../mono_gd/gd_mono_internals.h"
|
2020-07-06 01:19:36 +08:00
|
|
|
#include "../mono_gd/gd_mono_marshal.h"
|
2018-09-04 11:40:41 +08:00
|
|
|
#include "../mono_gd/gd_mono_utils.h"
|
|
|
|
#include "../signal_awaiter_utils.h"
|
2019-03-29 03:01:54 +08:00
|
|
|
#include "arguments_vector.h"
|
2018-09-04 11:40:41 +08:00
|
|
|
|
2018-09-12 08:41:54 +08:00
|
|
|
Object *godot_icall_Object_Ctor(MonoObject *p_obj) {
|
2018-09-04 11:40:41 +08:00
|
|
|
Object *instance = memnew(Object);
|
2018-09-12 08:41:54 +08:00
|
|
|
GDMonoInternals::tie_managed_to_unmanaged(p_obj, instance);
|
2018-09-04 11:40:41 +08:00
|
|
|
return instance;
|
|
|
|
}
|
|
|
|
|
2018-09-12 08:41:54 +08:00
|
|
|
void godot_icall_Object_Disposed(MonoObject *p_obj, Object *p_ptr) {
|
2018-09-04 11:40:41 +08:00
|
|
|
#ifdef DEBUG_ENABLED
|
2020-04-02 07:20:12 +08:00
|
|
|
CRASH_COND(p_ptr == nullptr);
|
2018-09-04 11:40:41 +08:00
|
|
|
#endif
|
2018-09-12 08:41:54 +08:00
|
|
|
|
|
|
|
if (p_ptr->get_script_instance()) {
|
|
|
|
CSharpInstance *cs_instance = CAST_CSHARP_INSTANCE(p_ptr->get_script_instance());
|
|
|
|
if (cs_instance) {
|
2018-12-01 09:23:55 +08:00
|
|
|
if (!cs_instance->is_destructing_script_instance()) {
|
|
|
|
cs_instance->mono_object_disposed(p_obj);
|
2020-04-02 07:20:12 +08:00
|
|
|
p_ptr->set_script_instance(nullptr);
|
2018-12-01 09:23:55 +08:00
|
|
|
}
|
2018-09-12 08:41:54 +08:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
}
|
2021-08-16 23:16:36 +08:00
|
|
|
|
|
|
|
void *data = CSharpLanguage::get_existing_instance_binding(p_ptr);
|
2018-09-12 08:41:54 +08:00
|
|
|
|
|
|
|
if (data) {
|
2019-02-03 13:35:22 +08:00
|
|
|
CSharpScriptBinding &script_binding = ((Map<Object *, CSharpScriptBinding>::Element *)data)->get();
|
|
|
|
if (script_binding.inited) {
|
2019-12-12 00:08:40 +08:00
|
|
|
MonoGCHandleData &gchandle = script_binding.gchandle;
|
|
|
|
if (!gchandle.is_released()) {
|
2019-02-03 13:35:22 +08:00
|
|
|
CSharpLanguage::release_script_gchandle(p_obj, gchandle);
|
|
|
|
}
|
2018-09-12 08:41:54 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-06-13 17:15:44 +08:00
|
|
|
void godot_icall_RefCounted_Disposed(MonoObject *p_obj, Object *p_ptr, MonoBoolean p_is_finalizer) {
|
2018-09-12 08:41:54 +08:00
|
|
|
#ifdef DEBUG_ENABLED
|
2020-04-02 07:20:12 +08:00
|
|
|
CRASH_COND(p_ptr == nullptr);
|
2021-06-05 00:03:15 +08:00
|
|
|
// This is only called with RefCounted derived classes
|
|
|
|
CRASH_COND(!Object::cast_to<RefCounted>(p_ptr));
|
2018-09-12 08:41:54 +08:00
|
|
|
#endif
|
2021-08-16 23:16:36 +08:00
|
|
|
|
2021-06-05 00:03:15 +08:00
|
|
|
RefCounted *rc = static_cast<RefCounted *>(p_ptr);
|
2018-09-12 08:41:54 +08:00
|
|
|
|
2021-06-05 00:03:15 +08:00
|
|
|
if (rc->get_script_instance()) {
|
2021-06-13 17:15:44 +08:00
|
|
|
CSharpInstance *cs_instance = CAST_CSHARP_INSTANCE(rc->get_script_instance());
|
2018-09-12 08:41:54 +08:00
|
|
|
if (cs_instance) {
|
2018-12-01 09:23:55 +08:00
|
|
|
if (!cs_instance->is_destructing_script_instance()) {
|
2019-02-03 13:35:22 +08:00
|
|
|
bool delete_owner;
|
|
|
|
bool remove_script_instance;
|
|
|
|
|
|
|
|
cs_instance->mono_object_disposed_baseref(p_obj, p_is_finalizer, delete_owner, remove_script_instance);
|
|
|
|
|
|
|
|
if (delete_owner) {
|
2021-06-05 00:03:15 +08:00
|
|
|
memdelete(rc);
|
2019-02-03 13:35:22 +08:00
|
|
|
} else if (remove_script_instance) {
|
2021-06-05 00:03:15 +08:00
|
|
|
rc->set_script_instance(nullptr);
|
2018-12-01 09:23:55 +08:00
|
|
|
}
|
2018-09-12 08:41:54 +08: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 17:15:44 +08:00
|
|
|
CSharpLanguage::get_singleton()->pre_unsafe_unreference(rc);
|
|
|
|
if (rc->unreference()) {
|
|
|
|
memdelete(rc);
|
2018-09-12 08:41:54 +08:00
|
|
|
} else {
|
2021-08-16 23:16:36 +08:00
|
|
|
void *data = CSharpLanguage::get_existing_instance_binding(rc);
|
2018-09-12 08:41:54 +08:00
|
|
|
|
|
|
|
if (data) {
|
2019-02-03 13:35:22 +08:00
|
|
|
CSharpScriptBinding &script_binding = ((Map<Object *, CSharpScriptBinding>::Element *)data)->get();
|
|
|
|
if (script_binding.inited) {
|
2019-12-12 00:08:40 +08:00
|
|
|
MonoGCHandleData &gchandle = script_binding.gchandle;
|
|
|
|
if (!gchandle.is_released()) {
|
2019-02-03 13:35:22 +08:00
|
|
|
CSharpLanguage::release_script_gchandle(p_obj, gchandle);
|
|
|
|
}
|
2018-09-12 08:41:54 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2018-09-04 11:40:41 +08:00
|
|
|
}
|
|
|
|
|
2020-03-15 02:20:17 +08:00
|
|
|
void godot_icall_Object_ConnectEventSignals(Object *p_ptr) {
|
|
|
|
CSharpInstance *csharp_instance = CAST_CSHARP_INSTANCE(p_ptr->get_script_instance());
|
|
|
|
if (csharp_instance) {
|
|
|
|
csharp_instance->connect_event_signals();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
MethodBind *godot_icall_Object_ClassDB_get_method(StringName *p_type, MonoString *p_method) {
|
|
|
|
StringName type = p_type ? *p_type : StringName();
|
2018-09-04 11:40:41 +08:00
|
|
|
StringName method(GDMonoMarshal::mono_string_to_godot(p_method));
|
|
|
|
return ClassDB::get_method(type, method);
|
|
|
|
}
|
|
|
|
|
2020-03-15 02:20:17 +08:00
|
|
|
MonoObject *godot_icall_Object_weakref(Object *p_ptr) {
|
2020-07-06 01:19:36 +08:00
|
|
|
if (!p_ptr) {
|
2020-04-02 07:20:12 +08:00
|
|
|
return nullptr;
|
2020-07-06 01:19:36 +08:00
|
|
|
}
|
2018-09-04 11:40:41 +08:00
|
|
|
|
|
|
|
Ref<WeakRef> wref;
|
2021-06-05 00:03:15 +08:00
|
|
|
RefCounted *rc = Object::cast_to<RefCounted>(p_ptr);
|
2018-09-04 11:40:41 +08:00
|
|
|
|
2021-06-05 00:03:15 +08:00
|
|
|
if (rc) {
|
|
|
|
REF r = rc;
|
2020-07-06 01:19:36 +08:00
|
|
|
if (!r.is_valid()) {
|
2020-04-02 07:20:12 +08:00
|
|
|
return nullptr;
|
2020-07-06 01:19:36 +08:00
|
|
|
}
|
2018-09-04 11:40:41 +08:00
|
|
|
|
2021-06-18 06:03:09 +08:00
|
|
|
wref.instantiate();
|
2018-09-04 11:40:41 +08:00
|
|
|
wref->set_ref(r);
|
|
|
|
} else {
|
2021-06-18 06:03:09 +08:00
|
|
|
wref.instantiate();
|
2020-03-15 02:20:17 +08:00
|
|
|
wref->set_obj(p_ptr);
|
2018-09-04 11:40:41 +08:00
|
|
|
}
|
|
|
|
|
2019-02-03 13:35:22 +08:00
|
|
|
return GDMonoUtils::unmanaged_get_managed(wref.ptr());
|
2018-09-04 11:40:41 +08:00
|
|
|
}
|
|
|
|
|
2020-12-05 07:05:01 +08:00
|
|
|
int32_t godot_icall_SignalAwaiter_connect(Object *p_source, StringName *p_signal, Object *p_target, MonoObject *p_awaiter) {
|
2020-03-15 02:20:17 +08:00
|
|
|
StringName signal = p_signal ? *p_signal : StringName();
|
2020-12-05 07:05:01 +08:00
|
|
|
return (int32_t)gd_mono_connect_signal_awaiter(p_source, signal, p_target, p_awaiter);
|
2018-09-04 11:40:41 +08:00
|
|
|
}
|
|
|
|
|
2019-03-29 03:01:54 +08:00
|
|
|
MonoArray *godot_icall_DynamicGodotObject_SetMemberList(Object *p_ptr) {
|
|
|
|
List<PropertyInfo> property_list;
|
|
|
|
p_ptr->get_property_list(&property_list);
|
|
|
|
|
|
|
|
MonoArray *result = mono_array_new(mono_domain_get(), CACHED_CLASS_RAW(String), property_list.size());
|
|
|
|
|
|
|
|
int i = 0;
|
2021-07-24 21:46:25 +08:00
|
|
|
for (const PropertyInfo &E : property_list) {
|
2021-07-16 11:45:57 +08:00
|
|
|
MonoString *boxed = GDMonoMarshal::mono_string_from_godot(E.name);
|
2019-05-21 00:34:35 +08:00
|
|
|
mono_array_setref(result, i, boxed);
|
2019-03-29 03:01:54 +08:00
|
|
|
i++;
|
|
|
|
}
|
|
|
|
|
|
|
|
return result;
|
|
|
|
}
|
|
|
|
|
|
|
|
MonoBoolean godot_icall_DynamicGodotObject_InvokeMember(Object *p_ptr, MonoString *p_name, MonoArray *p_args, MonoObject **r_result) {
|
|
|
|
String name = GDMonoMarshal::mono_string_to_godot(p_name);
|
|
|
|
|
|
|
|
int argc = mono_array_length(p_args);
|
|
|
|
|
|
|
|
ArgumentsVector<Variant> arg_store(argc);
|
|
|
|
ArgumentsVector<const Variant *> args(argc);
|
|
|
|
|
|
|
|
for (int i = 0; i < argc; i++) {
|
|
|
|
MonoObject *elem = mono_array_get(p_args, MonoObject *, i);
|
|
|
|
arg_store.set(i, GDMonoMarshal::mono_object_to_variant(elem));
|
|
|
|
args.set(i, &arg_store.get(i));
|
|
|
|
}
|
|
|
|
|
2020-02-20 03:27:19 +08:00
|
|
|
Callable::CallError error;
|
2019-03-29 03:01:54 +08:00
|
|
|
Variant result = p_ptr->call(StringName(name), args.ptr(), argc, error);
|
|
|
|
|
|
|
|
*r_result = GDMonoMarshal::variant_to_mono_object(result);
|
|
|
|
|
2020-02-20 03:27:19 +08:00
|
|
|
return error.error == Callable::CallError::CALL_OK;
|
2019-03-29 03:01:54 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
MonoBoolean godot_icall_DynamicGodotObject_GetMember(Object *p_ptr, MonoString *p_name, MonoObject **r_result) {
|
|
|
|
String name = GDMonoMarshal::mono_string_to_godot(p_name);
|
|
|
|
|
|
|
|
bool valid;
|
|
|
|
Variant value = p_ptr->get(StringName(name), &valid);
|
|
|
|
|
|
|
|
if (valid) {
|
|
|
|
*r_result = GDMonoMarshal::variant_to_mono_object(value);
|
|
|
|
}
|
|
|
|
|
|
|
|
return valid;
|
|
|
|
}
|
|
|
|
|
|
|
|
MonoBoolean godot_icall_DynamicGodotObject_SetMember(Object *p_ptr, MonoString *p_name, MonoObject *p_value) {
|
|
|
|
String name = GDMonoMarshal::mono_string_to_godot(p_name);
|
|
|
|
Variant value = GDMonoMarshal::mono_object_to_variant(p_value);
|
|
|
|
|
|
|
|
bool valid;
|
|
|
|
p_ptr->set(StringName(name), value, &valid);
|
|
|
|
|
|
|
|
return valid;
|
|
|
|
}
|
|
|
|
|
2019-04-18 20:48:10 +08:00
|
|
|
MonoString *godot_icall_Object_ToString(Object *p_ptr) {
|
2019-07-08 21:17:43 +08:00
|
|
|
#ifdef DEBUG_ENABLED
|
|
|
|
// Cannot happen in C#; would get an ObjectDisposedException instead.
|
2020-04-02 07:20:12 +08:00
|
|
|
CRASH_COND(p_ptr == nullptr);
|
2019-07-08 21:17:43 +08:00
|
|
|
#endif
|
2020-03-15 02:20:17 +08:00
|
|
|
// Can't call 'Object::to_string()' here, as that can end up calling 'ToString' again resulting in an endless circular loop.
|
|
|
|
String result = "[" + p_ptr->get_class() + ":" + itos(p_ptr->get_instance_id()) + "]";
|
2019-07-08 21:17:43 +08:00
|
|
|
return GDMonoMarshal::mono_string_from_godot(result);
|
2019-04-18 20:48:10 +08:00
|
|
|
}
|
|
|
|
|
2018-09-04 11:40:41 +08:00
|
|
|
void godot_register_object_icalls() {
|
2020-12-05 07:05:01 +08:00
|
|
|
GDMonoUtils::add_internal_call("Godot.Object::godot_icall_Object_Ctor", godot_icall_Object_Ctor);
|
|
|
|
GDMonoUtils::add_internal_call("Godot.Object::godot_icall_Object_Disposed", godot_icall_Object_Disposed);
|
2021-06-13 17:15:44 +08:00
|
|
|
GDMonoUtils::add_internal_call("Godot.Object::godot_icall_RefCounted_Disposed", godot_icall_RefCounted_Disposed);
|
2020-12-05 07:05:01 +08:00
|
|
|
GDMonoUtils::add_internal_call("Godot.Object::godot_icall_Object_ConnectEventSignals", godot_icall_Object_ConnectEventSignals);
|
|
|
|
GDMonoUtils::add_internal_call("Godot.Object::godot_icall_Object_ClassDB_get_method", godot_icall_Object_ClassDB_get_method);
|
|
|
|
GDMonoUtils::add_internal_call("Godot.Object::godot_icall_Object_ToString", godot_icall_Object_ToString);
|
|
|
|
GDMonoUtils::add_internal_call("Godot.Object::godot_icall_Object_weakref", godot_icall_Object_weakref);
|
|
|
|
GDMonoUtils::add_internal_call("Godot.SignalAwaiter::godot_icall_SignalAwaiter_connect", godot_icall_SignalAwaiter_connect);
|
|
|
|
GDMonoUtils::add_internal_call("Godot.DynamicGodotObject::godot_icall_DynamicGodotObject_SetMemberList", godot_icall_DynamicGodotObject_SetMemberList);
|
|
|
|
GDMonoUtils::add_internal_call("Godot.DynamicGodotObject::godot_icall_DynamicGodotObject_InvokeMember", godot_icall_DynamicGodotObject_InvokeMember);
|
|
|
|
GDMonoUtils::add_internal_call("Godot.DynamicGodotObject::godot_icall_DynamicGodotObject_GetMember", godot_icall_DynamicGodotObject_GetMember);
|
|
|
|
GDMonoUtils::add_internal_call("Godot.DynamicGodotObject::godot_icall_DynamicGodotObject_SetMember", godot_icall_DynamicGodotObject_SetMember);
|
2018-09-04 11:40:41 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
#endif // MONO_GLUE_ENABLED
|