2018-09-04 03:40:41 +00:00
|
|
|
/*************************************************************************/
|
|
|
|
/* base_object_glue.cpp */
|
|
|
|
/*************************************************************************/
|
|
|
|
/* This file is part of: */
|
|
|
|
/* GODOT ENGINE */
|
|
|
|
/* https://godotengine.org */
|
|
|
|
/*************************************************************************/
|
2019-01-01 11:53:14 +00:00
|
|
|
/* Copyright (c) 2007-2019 Juan Linietsky, Ariel Manzur. */
|
|
|
|
/* Copyright (c) 2014-2019 Godot Engine contributors (cf. AUTHORS.md) */
|
2018-09-04 03:40:41 +00: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. */
|
|
|
|
/*************************************************************************/
|
|
|
|
|
|
|
|
#include "base_object_glue.h"
|
|
|
|
|
|
|
|
#ifdef MONO_GLUE_ENABLED
|
|
|
|
|
|
|
|
#include "core/reference.h"
|
2019-02-12 12:30:56 +00:00
|
|
|
#include "core/string_name.h"
|
2018-09-04 03:40:41 +00:00
|
|
|
|
2018-09-12 00:41:54 +00:00
|
|
|
#include "../csharp_script.h"
|
2019-03-28 19:01:54 +00:00
|
|
|
#include "../mono_gd/gd_mono_class.h"
|
2018-09-04 03:40:41 +00:00
|
|
|
#include "../mono_gd/gd_mono_internals.h"
|
|
|
|
#include "../mono_gd/gd_mono_utils.h"
|
|
|
|
#include "../signal_awaiter_utils.h"
|
2019-03-28 19:01:54 +00:00
|
|
|
#include "arguments_vector.h"
|
2018-09-04 03:40:41 +00:00
|
|
|
|
2018-09-12 00:41:54 +00:00
|
|
|
Object *godot_icall_Object_Ctor(MonoObject *p_obj) {
|
2018-09-04 03:40:41 +00:00
|
|
|
Object *instance = memnew(Object);
|
2018-09-12 00:41:54 +00:00
|
|
|
GDMonoInternals::tie_managed_to_unmanaged(p_obj, instance);
|
2018-09-04 03:40:41 +00:00
|
|
|
return instance;
|
|
|
|
}
|
|
|
|
|
2018-09-12 00:41:54 +00:00
|
|
|
void godot_icall_Object_Disposed(MonoObject *p_obj, Object *p_ptr) {
|
2018-09-04 03:40:41 +00:00
|
|
|
#ifdef DEBUG_ENABLED
|
2018-09-12 00:41:54 +00:00
|
|
|
CRASH_COND(p_ptr == NULL);
|
2018-09-04 03:40:41 +00:00
|
|
|
#endif
|
2018-09-12 00:41:54 +00:00
|
|
|
|
|
|
|
if (p_ptr->get_script_instance()) {
|
|
|
|
CSharpInstance *cs_instance = CAST_CSHARP_INSTANCE(p_ptr->get_script_instance());
|
|
|
|
if (cs_instance) {
|
2018-12-01 01:23:55 +00:00
|
|
|
if (!cs_instance->is_destructing_script_instance()) {
|
|
|
|
cs_instance->mono_object_disposed(p_obj);
|
|
|
|
p_ptr->set_script_instance(NULL);
|
|
|
|
}
|
2018-09-12 00:41:54 +00:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void *data = p_ptr->get_script_instance_binding(CSharpLanguage::get_singleton()->get_language_index());
|
|
|
|
|
|
|
|
if (data) {
|
2019-02-03 05:35:22 +00:00
|
|
|
CSharpScriptBinding &script_binding = ((Map<Object *, CSharpScriptBinding>::Element *)data)->get();
|
|
|
|
if (script_binding.inited) {
|
|
|
|
Ref<MonoGCHandle> &gchandle = script_binding.gchandle;
|
|
|
|
if (gchandle.is_valid()) {
|
|
|
|
CSharpLanguage::release_script_gchandle(p_obj, gchandle);
|
|
|
|
}
|
2018-09-12 00:41:54 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-03-28 19:01:54 +00:00
|
|
|
void godot_icall_Reference_Disposed(MonoObject *p_obj, Object *p_ptr, MonoBoolean p_is_finalizer) {
|
2018-09-12 00:41:54 +00:00
|
|
|
#ifdef DEBUG_ENABLED
|
|
|
|
CRASH_COND(p_ptr == NULL);
|
|
|
|
// This is only called with Reference derived classes
|
|
|
|
CRASH_COND(!Object::cast_to<Reference>(p_ptr));
|
|
|
|
#endif
|
|
|
|
|
|
|
|
Reference *ref = static_cast<Reference *>(p_ptr);
|
|
|
|
|
|
|
|
if (ref->get_script_instance()) {
|
|
|
|
CSharpInstance *cs_instance = CAST_CSHARP_INSTANCE(ref->get_script_instance());
|
|
|
|
if (cs_instance) {
|
2018-12-01 01:23:55 +00:00
|
|
|
if (!cs_instance->is_destructing_script_instance()) {
|
2019-02-03 05:35:22 +00: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) {
|
|
|
|
memdelete(ref);
|
|
|
|
} else if (remove_script_instance) {
|
2018-12-01 01:23:55 +00:00
|
|
|
ref->set_script_instance(NULL);
|
|
|
|
}
|
2018-09-12 00:41:54 +00:00
|
|
|
}
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// Unsafe refcount decrement. The managed instance also counts as a reference.
|
|
|
|
// See: CSharpLanguage::alloc_instance_binding_data(Object *p_object)
|
|
|
|
if (ref->unreference()) {
|
|
|
|
memdelete(ref);
|
|
|
|
} else {
|
|
|
|
void *data = ref->get_script_instance_binding(CSharpLanguage::get_singleton()->get_language_index());
|
|
|
|
|
|
|
|
if (data) {
|
2019-02-03 05:35:22 +00:00
|
|
|
CSharpScriptBinding &script_binding = ((Map<Object *, CSharpScriptBinding>::Element *)data)->get();
|
|
|
|
if (script_binding.inited) {
|
|
|
|
Ref<MonoGCHandle> &gchandle = script_binding.gchandle;
|
|
|
|
if (gchandle.is_valid()) {
|
|
|
|
CSharpLanguage::release_script_gchandle(p_obj, gchandle);
|
|
|
|
}
|
2018-09-12 00:41:54 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2018-09-04 03:40:41 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
MethodBind *godot_icall_Object_ClassDB_get_method(MonoString *p_type, MonoString *p_method) {
|
|
|
|
StringName type(GDMonoMarshal::mono_string_to_godot(p_type));
|
|
|
|
StringName method(GDMonoMarshal::mono_string_to_godot(p_method));
|
|
|
|
return ClassDB::get_method(type, method);
|
|
|
|
}
|
|
|
|
|
|
|
|
MonoObject *godot_icall_Object_weakref(Object *p_obj) {
|
|
|
|
if (!p_obj)
|
|
|
|
return NULL;
|
|
|
|
|
|
|
|
Ref<WeakRef> wref;
|
|
|
|
Reference *ref = Object::cast_to<Reference>(p_obj);
|
|
|
|
|
|
|
|
if (ref) {
|
|
|
|
REF r = ref;
|
|
|
|
if (!r.is_valid())
|
|
|
|
return NULL;
|
|
|
|
|
|
|
|
wref.instance();
|
|
|
|
wref->set_ref(r);
|
|
|
|
} else {
|
|
|
|
wref.instance();
|
|
|
|
wref->set_obj(p_obj);
|
|
|
|
}
|
|
|
|
|
2019-02-03 05:35:22 +00:00
|
|
|
return GDMonoUtils::unmanaged_get_managed(wref.ptr());
|
2018-09-04 03:40:41 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
Error godot_icall_SignalAwaiter_connect(Object *p_source, MonoString *p_signal, Object *p_target, MonoObject *p_awaiter) {
|
|
|
|
String signal = GDMonoMarshal::mono_string_to_godot(p_signal);
|
|
|
|
return SignalAwaiterUtils::connect_signal_awaiter(p_source, signal, p_target, p_awaiter);
|
|
|
|
}
|
|
|
|
|
2019-03-28 19:01:54 +00: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;
|
|
|
|
for (List<PropertyInfo>::Element *E = property_list.front(); E; E = E->next()) {
|
|
|
|
MonoString *boxed = GDMonoMarshal::mono_string_from_godot(E->get().name);
|
|
|
|
mono_array_set(result, MonoString *, i, boxed);
|
|
|
|
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));
|
|
|
|
}
|
|
|
|
|
|
|
|
Variant::CallError error;
|
|
|
|
Variant result = p_ptr->call(StringName(name), args.ptr(), argc, error);
|
|
|
|
|
|
|
|
*r_result = GDMonoMarshal::variant_to_mono_object(result);
|
|
|
|
|
|
|
|
return error.error == OK;
|
|
|
|
}
|
|
|
|
|
|
|
|
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;
|
|
|
|
}
|
|
|
|
|
2018-09-04 03:40:41 +00:00
|
|
|
void godot_register_object_icalls() {
|
|
|
|
mono_add_internal_call("Godot.Object::godot_icall_Object_Ctor", (void *)godot_icall_Object_Ctor);
|
2018-09-12 00:41:54 +00:00
|
|
|
mono_add_internal_call("Godot.Object::godot_icall_Object_Disposed", (void *)godot_icall_Object_Disposed);
|
|
|
|
mono_add_internal_call("Godot.Object::godot_icall_Reference_Disposed", (void *)godot_icall_Reference_Disposed);
|
2018-09-04 03:40:41 +00:00
|
|
|
mono_add_internal_call("Godot.Object::godot_icall_Object_ClassDB_get_method", (void *)godot_icall_Object_ClassDB_get_method);
|
|
|
|
mono_add_internal_call("Godot.Object::godot_icall_Object_weakref", (void *)godot_icall_Object_weakref);
|
|
|
|
mono_add_internal_call("Godot.SignalAwaiter::godot_icall_SignalAwaiter_connect", (void *)godot_icall_SignalAwaiter_connect);
|
2019-03-28 19:01:54 +00:00
|
|
|
mono_add_internal_call("Godot.DynamicGodotObject::godot_icall_DynamicGodotObject_SetMemberList", (void *)godot_icall_DynamicGodotObject_SetMemberList);
|
|
|
|
mono_add_internal_call("Godot.DynamicGodotObject::godot_icall_DynamicGodotObject_InvokeMember", (void *)godot_icall_DynamicGodotObject_InvokeMember);
|
|
|
|
mono_add_internal_call("Godot.DynamicGodotObject::godot_icall_DynamicGodotObject_GetMember", (void *)godot_icall_DynamicGodotObject_GetMember);
|
|
|
|
mono_add_internal_call("Godot.DynamicGodotObject::godot_icall_DynamicGodotObject_SetMember", (void *)godot_icall_DynamicGodotObject_SetMember);
|
2018-09-04 03:40:41 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
#endif // MONO_GLUE_ENABLED
|