2018-01-04 20:05:46 +00:00
|
|
|
/*************************************************************************/
|
|
|
|
/* gd_mono_property.cpp */
|
|
|
|
/*************************************************************************/
|
|
|
|
/* This file is part of: */
|
|
|
|
/* GODOT ENGINE */
|
|
|
|
/* https://godotengine.org */
|
|
|
|
/*************************************************************************/
|
2021-01-01 19:13:46 +00:00
|
|
|
/* Copyright (c) 2007-2021 Juan Linietsky, Ariel Manzur. */
|
|
|
|
/* Copyright (c) 2014-2021 Godot Engine contributors (cf. AUTHORS.md). */
|
2018-01-04 20:05:46 +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. */
|
|
|
|
/*************************************************************************/
|
2019-01-01 11:46:36 +00:00
|
|
|
|
2018-01-04 20:05:46 +00:00
|
|
|
#include "gd_mono_property.h"
|
|
|
|
|
2019-11-10 16:10:38 +00:00
|
|
|
#include "gd_mono_cache.h"
|
2018-01-04 20:05:46 +00:00
|
|
|
#include "gd_mono_class.h"
|
|
|
|
#include "gd_mono_marshal.h"
|
2019-11-10 16:10:38 +00:00
|
|
|
#include "gd_mono_utils.h"
|
2018-01-04 20:05:46 +00:00
|
|
|
|
|
|
|
#include <mono/metadata/attrdefs.h>
|
|
|
|
|
|
|
|
GDMonoProperty::GDMonoProperty(MonoProperty *p_mono_property, GDMonoClass *p_owner) {
|
|
|
|
owner = p_owner;
|
|
|
|
mono_property = p_mono_property;
|
2021-01-18 08:34:10 +00:00
|
|
|
name = String::utf8(mono_property_get_name(mono_property));
|
2018-01-04 20:05:46 +00:00
|
|
|
|
|
|
|
MonoMethod *prop_method = mono_property_get_get_method(mono_property);
|
|
|
|
|
|
|
|
if (prop_method) {
|
|
|
|
MonoMethodSignature *getter_sig = mono_method_signature(prop_method);
|
|
|
|
|
|
|
|
MonoType *ret_type = mono_signature_get_return_type(getter_sig);
|
|
|
|
|
|
|
|
type.type_encoding = mono_type_get_type(ret_type);
|
|
|
|
MonoClass *ret_type_class = mono_class_from_mono_type(ret_type);
|
|
|
|
type.type_class = GDMono::get_singleton()->get_class(ret_type_class);
|
|
|
|
} else {
|
|
|
|
prop_method = mono_property_get_set_method(mono_property);
|
|
|
|
|
|
|
|
MonoMethodSignature *setter_sig = mono_method_signature(prop_method);
|
|
|
|
|
2020-04-01 23:20:12 +00:00
|
|
|
void *iter = nullptr;
|
2018-01-04 20:05:46 +00:00
|
|
|
MonoType *param_raw_type = mono_signature_get_params(setter_sig, &iter);
|
|
|
|
|
|
|
|
type.type_encoding = mono_type_get_type(param_raw_type);
|
|
|
|
MonoClass *param_type_class = mono_class_from_mono_type(param_raw_type);
|
|
|
|
type.type_class = GDMono::get_singleton()->get_class(param_type_class);
|
|
|
|
}
|
|
|
|
|
2021-10-18 22:26:56 +00:00
|
|
|
param_buffer_size = GDMonoMarshal::variant_get_managed_unboxed_size(type);
|
|
|
|
|
2018-01-04 20:05:46 +00:00
|
|
|
attrs_fetched = false;
|
2020-04-01 23:20:12 +00:00
|
|
|
attributes = nullptr;
|
2018-01-04 20:05:46 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
GDMonoProperty::~GDMonoProperty() {
|
|
|
|
if (attributes) {
|
|
|
|
mono_custom_attrs_free(attributes);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
bool GDMonoProperty::is_static() {
|
|
|
|
MonoMethod *prop_method = mono_property_get_get_method(mono_property);
|
2020-07-05 17:19:36 +00:00
|
|
|
if (prop_method == nullptr) {
|
2018-01-04 20:05:46 +00:00
|
|
|
prop_method = mono_property_get_set_method(mono_property);
|
2020-07-05 17:19:36 +00:00
|
|
|
}
|
2020-04-01 23:20:12 +00:00
|
|
|
return mono_method_get_flags(prop_method, nullptr) & MONO_METHOD_ATTR_STATIC;
|
2018-01-04 20:05:46 +00:00
|
|
|
}
|
|
|
|
|
2019-01-28 23:02:35 +00:00
|
|
|
IMonoClassMember::Visibility GDMonoProperty::get_visibility() {
|
2018-01-04 20:05:46 +00:00
|
|
|
MonoMethod *prop_method = mono_property_get_get_method(mono_property);
|
2020-07-05 17:19:36 +00:00
|
|
|
if (prop_method == nullptr) {
|
2018-01-04 20:05:46 +00:00
|
|
|
prop_method = mono_property_get_set_method(mono_property);
|
2020-07-05 17:19:36 +00:00
|
|
|
}
|
2018-01-04 20:05:46 +00:00
|
|
|
|
2020-04-01 23:20:12 +00:00
|
|
|
switch (mono_method_get_flags(prop_method, nullptr) & MONO_METHOD_ATTR_ACCESS_MASK) {
|
2018-01-04 20:05:46 +00:00
|
|
|
case MONO_METHOD_ATTR_PRIVATE:
|
2019-01-28 23:02:35 +00:00
|
|
|
return IMonoClassMember::PRIVATE;
|
2018-01-04 20:05:46 +00:00
|
|
|
case MONO_METHOD_ATTR_FAM_AND_ASSEM:
|
2019-01-28 23:02:35 +00:00
|
|
|
return IMonoClassMember::PROTECTED_AND_INTERNAL;
|
2018-01-04 20:05:46 +00:00
|
|
|
case MONO_METHOD_ATTR_ASSEM:
|
2019-01-28 23:02:35 +00:00
|
|
|
return IMonoClassMember::INTERNAL;
|
2018-01-04 20:05:46 +00:00
|
|
|
case MONO_METHOD_ATTR_FAMILY:
|
2019-01-28 23:02:35 +00:00
|
|
|
return IMonoClassMember::PROTECTED;
|
2018-01-04 20:05:46 +00:00
|
|
|
case MONO_METHOD_ATTR_PUBLIC:
|
2019-01-28 23:02:35 +00:00
|
|
|
return IMonoClassMember::PUBLIC;
|
2018-01-04 20:05:46 +00:00
|
|
|
default:
|
2019-01-28 23:02:35 +00:00
|
|
|
ERR_FAIL_V(IMonoClassMember::PRIVATE);
|
2018-01-04 20:05:46 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
bool GDMonoProperty::has_attribute(GDMonoClass *p_attr_class) {
|
|
|
|
ERR_FAIL_NULL_V(p_attr_class, false);
|
|
|
|
|
2020-07-05 17:19:36 +00:00
|
|
|
if (!attrs_fetched) {
|
2018-01-04 20:05:46 +00:00
|
|
|
fetch_attributes();
|
2020-07-05 17:19:36 +00:00
|
|
|
}
|
2018-01-04 20:05:46 +00:00
|
|
|
|
2020-07-05 17:19:36 +00:00
|
|
|
if (!attributes) {
|
2018-01-04 20:05:46 +00:00
|
|
|
return false;
|
2020-07-05 17:19:36 +00:00
|
|
|
}
|
2018-01-04 20:05:46 +00:00
|
|
|
|
|
|
|
return mono_custom_attrs_has_attr(attributes, p_attr_class->get_mono_ptr());
|
|
|
|
}
|
|
|
|
|
|
|
|
MonoObject *GDMonoProperty::get_attribute(GDMonoClass *p_attr_class) {
|
2020-04-01 23:20:12 +00:00
|
|
|
ERR_FAIL_NULL_V(p_attr_class, nullptr);
|
2018-01-04 20:05:46 +00:00
|
|
|
|
2020-07-05 17:19:36 +00:00
|
|
|
if (!attrs_fetched) {
|
2018-01-04 20:05:46 +00:00
|
|
|
fetch_attributes();
|
2020-07-05 17:19:36 +00:00
|
|
|
}
|
2018-01-04 20:05:46 +00:00
|
|
|
|
2020-07-05 17:19:36 +00:00
|
|
|
if (!attributes) {
|
2020-04-01 23:20:12 +00:00
|
|
|
return nullptr;
|
2020-07-05 17:19:36 +00:00
|
|
|
}
|
2018-01-04 20:05:46 +00:00
|
|
|
|
|
|
|
return mono_custom_attrs_get_attr(attributes, p_attr_class->get_mono_ptr());
|
|
|
|
}
|
|
|
|
|
|
|
|
void GDMonoProperty::fetch_attributes() {
|
2020-04-01 23:20:12 +00:00
|
|
|
ERR_FAIL_COND(attributes != nullptr);
|
2018-01-04 20:05:46 +00:00
|
|
|
attributes = mono_custom_attrs_from_property(owner->get_mono_ptr(), mono_property);
|
|
|
|
attrs_fetched = true;
|
|
|
|
}
|
|
|
|
|
|
|
|
bool GDMonoProperty::has_getter() {
|
2020-04-01 23:20:12 +00:00
|
|
|
return mono_property_get_get_method(mono_property) != nullptr;
|
2018-01-04 20:05:46 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
bool GDMonoProperty::has_setter() {
|
2020-04-01 23:20:12 +00:00
|
|
|
return mono_property_get_set_method(mono_property) != nullptr;
|
2018-01-04 20:05:46 +00:00
|
|
|
}
|
|
|
|
|
2021-10-18 22:26:56 +00:00
|
|
|
void GDMonoProperty::set_value_from_variant(MonoObject *p_object, const Variant &p_value, MonoException **r_exc) {
|
|
|
|
uint8_t *buffer = (uint8_t *)alloca(param_buffer_size);
|
|
|
|
unsigned int offset = 0;
|
2018-01-04 20:05:46 +00:00
|
|
|
|
2021-10-18 22:26:56 +00:00
|
|
|
void *params[1] = {
|
|
|
|
GDMonoMarshal::variant_to_managed_unboxed(p_value, type, buffer, offset)
|
|
|
|
};
|
|
|
|
|
|
|
|
#ifdef DEBUG_ENABLED
|
|
|
|
CRASH_COND(offset != param_buffer_size);
|
|
|
|
#endif
|
2018-01-04 20:05:46 +00:00
|
|
|
|
2021-10-18 22:26:56 +00:00
|
|
|
MonoException *exc = nullptr;
|
|
|
|
GDMonoUtils::property_set_value(mono_property, p_object, params, &exc);
|
2018-01-04 20:05:46 +00:00
|
|
|
if (exc) {
|
|
|
|
if (r_exc) {
|
|
|
|
*r_exc = exc;
|
|
|
|
} else {
|
2018-06-26 19:03:42 +00:00
|
|
|
GDMonoUtils::set_pending_exception(exc);
|
2018-01-04 20:05:46 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-06-26 19:03:42 +00:00
|
|
|
MonoObject *GDMonoProperty::get_value(MonoObject *p_object, MonoException **r_exc) {
|
2020-04-01 23:20:12 +00:00
|
|
|
MonoException *exc = nullptr;
|
|
|
|
MonoObject *ret = GDMonoUtils::property_get_value(mono_property, p_object, nullptr, &exc);
|
2018-01-04 20:05:46 +00:00
|
|
|
|
|
|
|
if (exc) {
|
2020-04-01 23:20:12 +00:00
|
|
|
ret = nullptr;
|
2018-01-04 20:05:46 +00:00
|
|
|
if (r_exc) {
|
|
|
|
*r_exc = exc;
|
|
|
|
} else {
|
2018-06-26 19:03:42 +00:00
|
|
|
GDMonoUtils::set_pending_exception(exc);
|
2018-01-04 20:05:46 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
|
|
|
bool GDMonoProperty::get_bool_value(MonoObject *p_object) {
|
|
|
|
return (bool)GDMonoMarshal::unbox<MonoBoolean>(get_value(p_object));
|
|
|
|
}
|
|
|
|
|
|
|
|
int GDMonoProperty::get_int_value(MonoObject *p_object) {
|
|
|
|
return GDMonoMarshal::unbox<int32_t>(get_value(p_object));
|
|
|
|
}
|
|
|
|
|
|
|
|
String GDMonoProperty::get_string_value(MonoObject *p_object) {
|
|
|
|
MonoObject *val = get_value(p_object);
|
|
|
|
return GDMonoMarshal::mono_string_to_godot((MonoString *)val);
|
|
|
|
}
|