godot/modules/mono/mono_gd/gd_mono_property.cpp

205 lines
6.9 KiB
C++
Raw Normal View History

/*************************************************************************/
/* gd_mono_property.cpp */
/*************************************************************************/
/* This file is part of: */
/* GODOT ENGINE */
/* https://godotengine.org */
/*************************************************************************/
/* Copyright (c) 2007-2021 Juan Linietsky, Ariel Manzur. */
/* Copyright (c) 2014-2021 Godot Engine contributors (cf. AUTHORS.md). */
/* */
/* 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
#include "gd_mono_property.h"
2019-11-10 16:10:38 +00:00
#include "gd_mono_cache.h"
#include "gd_mono_class.h"
#include "gd_mono_marshal.h"
2019-11-10 16:10:38 +00:00
#include "gd_mono_utils.h"
#include <mono/metadata/attrdefs.h>
GDMonoProperty::GDMonoProperty(MonoProperty *p_mono_property, GDMonoClass *p_owner) {
owner = p_owner;
mono_property = p_mono_property;
name = mono_property_get_name(mono_property);
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;
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);
}
attrs_fetched = false;
2020-04-01 23:20:12 +00:00
attributes = nullptr;
}
GDMonoProperty::~GDMonoProperty() {
if (attributes) {
mono_custom_attrs_free(attributes);
}
}
bool GDMonoProperty::is_static() {
MonoMethod *prop_method = mono_property_get_get_method(mono_property);
if (prop_method == nullptr) {
prop_method = mono_property_get_set_method(mono_property);
}
2020-04-01 23:20:12 +00:00
return mono_method_get_flags(prop_method, nullptr) & MONO_METHOD_ATTR_STATIC;
}
2019-01-28 23:02:35 +00:00
IMonoClassMember::Visibility GDMonoProperty::get_visibility() {
MonoMethod *prop_method = mono_property_get_get_method(mono_property);
if (prop_method == nullptr) {
prop_method = mono_property_get_set_method(mono_property);
}
2020-04-01 23:20:12 +00:00
switch (mono_method_get_flags(prop_method, nullptr) & MONO_METHOD_ATTR_ACCESS_MASK) {
case MONO_METHOD_ATTR_PRIVATE:
2019-01-28 23:02:35 +00:00
return IMonoClassMember::PRIVATE;
case MONO_METHOD_ATTR_FAM_AND_ASSEM:
2019-01-28 23:02:35 +00:00
return IMonoClassMember::PROTECTED_AND_INTERNAL;
case MONO_METHOD_ATTR_ASSEM:
2019-01-28 23:02:35 +00:00
return IMonoClassMember::INTERNAL;
case MONO_METHOD_ATTR_FAMILY:
2019-01-28 23:02:35 +00:00
return IMonoClassMember::PROTECTED;
case MONO_METHOD_ATTR_PUBLIC:
2019-01-28 23:02:35 +00:00
return IMonoClassMember::PUBLIC;
default:
2019-01-28 23:02:35 +00:00
ERR_FAIL_V(IMonoClassMember::PRIVATE);
}
}
bool GDMonoProperty::has_attribute(GDMonoClass *p_attr_class) {
ERR_FAIL_NULL_V(p_attr_class, false);
if (!attrs_fetched) {
fetch_attributes();
}
if (!attributes) {
return false;
}
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);
if (!attrs_fetched) {
fetch_attributes();
}
if (!attributes) {
2020-04-01 23:20:12 +00:00
return nullptr;
}
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);
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;
}
bool GDMonoProperty::has_setter() {
2020-04-01 23:20:12 +00:00
return mono_property_get_set_method(mono_property) != nullptr;
}
2018-06-26 19:03:42 +00:00
void GDMonoProperty::set_value(MonoObject *p_object, MonoObject *p_value, MonoException **r_exc) {
MonoMethod *prop_method = mono_property_get_set_method(mono_property);
void *params[1] = { p_value };
2020-04-01 23:20:12 +00:00
MonoException *exc = nullptr;
GDMonoUtils::runtime_invoke(prop_method, p_object, params, &exc);
if (exc) {
if (r_exc) {
*r_exc = exc;
} else {
GDMonoUtils::set_pending_exception(exc);
}
}
}
2018-06-26 19:03:42 +00:00
void GDMonoProperty::set_value(MonoObject *p_object, void **p_params, MonoException **r_exc) {
2020-04-01 23:20:12 +00:00
MonoException *exc = nullptr;
GDMonoUtils::property_set_value(mono_property, p_object, p_params, &exc);
if (exc) {
if (r_exc) {
*r_exc = exc;
} else {
2018-06-26 19:03:42 +00:00
GDMonoUtils::set_pending_exception(exc);
}
}
}
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);
if (exc) {
2020-04-01 23:20:12 +00:00
ret = nullptr;
if (r_exc) {
*r_exc = exc;
} else {
2018-06-26 19:03:42 +00:00
GDMonoUtils::set_pending_exception(exc);
}
}
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);
}