2017-10-02 23:24:00 +02:00
|
|
|
/*************************************************************************/
|
|
|
|
/* gd_mono_class.cpp */
|
|
|
|
/*************************************************************************/
|
|
|
|
/* This file is part of: */
|
|
|
|
/* GODOT ENGINE */
|
|
|
|
/* https://godotengine.org */
|
|
|
|
/*************************************************************************/
|
2020-01-01 11:16:22 +01:00
|
|
|
/* Copyright (c) 2007-2020 Juan Linietsky, Ariel Manzur. */
|
|
|
|
/* Copyright (c) 2014-2020 Godot Engine contributors (cf. AUTHORS.md). */
|
2017-10-02 23:24:00 +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. */
|
|
|
|
/*************************************************************************/
|
2018-01-05 00:50:27 +01:00
|
|
|
|
2017-10-02 23:24:00 +02:00
|
|
|
#include "gd_mono_class.h"
|
|
|
|
|
|
|
|
#include <mono/metadata/attrdefs.h>
|
2020-04-23 02:19:32 +02:00
|
|
|
#include <mono/metadata/debug-helpers.h>
|
2017-10-02 23:24:00 +02:00
|
|
|
|
|
|
|
#include "gd_mono_assembly.h"
|
2019-11-10 17:10:38 +01:00
|
|
|
#include "gd_mono_cache.h"
|
2018-07-18 23:07:57 +02:00
|
|
|
#include "gd_mono_marshal.h"
|
2017-10-02 23:24:00 +02:00
|
|
|
|
2018-07-18 23:07:57 +02:00
|
|
|
String GDMonoClass::get_full_name(MonoClass *p_mono_class) {
|
|
|
|
// mono_type_get_full_name is not exposed to embedders, but this seems to do the job
|
|
|
|
MonoReflectionType *type_obj = mono_type_get_object(mono_domain_get(), get_mono_type(p_mono_class));
|
2017-10-02 23:24:00 +02:00
|
|
|
|
2020-04-02 01:20:12 +02:00
|
|
|
MonoException *exc = nullptr;
|
2018-07-31 19:40:38 +02:00
|
|
|
MonoString *str = GDMonoUtils::object_to_string((MonoObject *)type_obj, &exc);
|
2019-07-03 09:44:53 +02:00
|
|
|
UNHANDLED_EXCEPTION(exc);
|
2017-10-02 23:24:00 +02:00
|
|
|
|
2018-07-18 23:07:57 +02:00
|
|
|
return GDMonoMarshal::mono_string_to_godot(str);
|
|
|
|
}
|
2017-10-02 23:24:00 +02:00
|
|
|
|
2018-07-18 23:07:57 +02:00
|
|
|
MonoType *GDMonoClass::get_mono_type(MonoClass *p_mono_class) {
|
|
|
|
return mono_class_get_type(p_mono_class);
|
2017-10-02 23:24:00 +02:00
|
|
|
}
|
|
|
|
|
2017-10-17 14:02:19 +02:00
|
|
|
String GDMonoClass::get_full_name() const {
|
2018-07-18 23:07:57 +02:00
|
|
|
return get_full_name(mono_class);
|
|
|
|
}
|
2017-10-17 14:02:19 +02:00
|
|
|
|
2020-04-23 02:19:32 +02:00
|
|
|
String GDMonoClass::get_type_desc() const {
|
|
|
|
return GDMonoUtils::get_type_desc(get_mono_type());
|
|
|
|
}
|
|
|
|
|
|
|
|
MonoType *GDMonoClass::get_mono_type() const {
|
2019-03-05 21:39:50 +01:00
|
|
|
// Careful, you cannot compare two MonoType*.
|
|
|
|
// There is mono_metadata_type_equal, how is this different from comparing two MonoClass*?
|
2018-07-18 23:07:57 +02:00
|
|
|
return get_mono_type(mono_class);
|
|
|
|
}
|
|
|
|
|
2019-02-19 22:36:27 +01:00
|
|
|
uint32_t GDMonoClass::get_flags() const {
|
|
|
|
return mono_class_get_flags(mono_class);
|
|
|
|
}
|
2018-07-18 23:07:57 +02:00
|
|
|
|
2019-02-19 22:36:27 +01:00
|
|
|
bool GDMonoClass::is_static() const {
|
|
|
|
uint32_t static_class_flags = MONO_TYPE_ATTR_ABSTRACT | MONO_TYPE_ATTR_SEALED;
|
|
|
|
return (get_flags() & static_class_flags) == static_class_flags;
|
|
|
|
}
|
|
|
|
|
|
|
|
bool GDMonoClass::is_assignable_from(GDMonoClass *p_from) const {
|
2018-07-18 23:07:57 +02:00
|
|
|
return mono_class_is_assignable_from(mono_class, p_from->mono_class);
|
2017-10-17 14:02:19 +02:00
|
|
|
}
|
|
|
|
|
2020-05-09 17:11:35 +02:00
|
|
|
StringName GDMonoClass::get_namespace() const {
|
|
|
|
GDMonoClass *nesting_class = get_nesting_class();
|
2020-07-05 19:19:36 +02:00
|
|
|
if (!nesting_class) {
|
2020-05-09 17:11:35 +02:00
|
|
|
return namespace_name;
|
2020-07-05 19:19:36 +02:00
|
|
|
}
|
2020-05-09 17:11:35 +02:00
|
|
|
return nesting_class->get_namespace();
|
|
|
|
}
|
|
|
|
|
|
|
|
String GDMonoClass::get_name_for_lookup() const {
|
|
|
|
GDMonoClass *nesting_class = get_nesting_class();
|
2020-07-05 19:19:36 +02:00
|
|
|
if (!nesting_class) {
|
2020-05-09 17:11:35 +02:00
|
|
|
return class_name;
|
2020-07-05 19:19:36 +02:00
|
|
|
}
|
2020-05-09 17:11:35 +02:00
|
|
|
return nesting_class->get_name_for_lookup() + "/" + class_name;
|
|
|
|
}
|
|
|
|
|
|
|
|
GDMonoClass *GDMonoClass::get_parent_class() const {
|
2019-07-03 09:44:53 +02:00
|
|
|
MonoClass *parent_mono_class = mono_class_get_parent(mono_class);
|
2020-04-02 01:20:12 +02:00
|
|
|
return parent_mono_class ? GDMono::get_singleton()->get_class(parent_mono_class) : nullptr;
|
2019-07-03 09:44:53 +02:00
|
|
|
}
|
2017-10-02 23:24:00 +02:00
|
|
|
|
2020-05-09 17:11:35 +02:00
|
|
|
GDMonoClass *GDMonoClass::get_nesting_class() const {
|
2019-07-03 09:44:53 +02:00
|
|
|
MonoClass *nesting_type = mono_class_get_nesting_type(mono_class);
|
2020-04-02 01:20:12 +02:00
|
|
|
return nesting_type ? GDMono::get_singleton()->get_class(nesting_type) : nullptr;
|
2017-10-02 23:24:00 +02:00
|
|
|
}
|
|
|
|
|
2017-10-17 14:02:19 +02:00
|
|
|
#ifdef TOOLS_ENABLED
|
|
|
|
Vector<MonoClassField *> GDMonoClass::get_enum_fields() {
|
|
|
|
bool class_is_enum = mono_class_is_enum(mono_class);
|
|
|
|
ERR_FAIL_COND_V(!class_is_enum, Vector<MonoClassField *>());
|
|
|
|
|
|
|
|
Vector<MonoClassField *> enum_fields;
|
|
|
|
|
2020-04-02 01:20:12 +02:00
|
|
|
void *iter = nullptr;
|
|
|
|
MonoClassField *raw_field = nullptr;
|
|
|
|
while ((raw_field = mono_class_get_fields(get_mono_ptr(), &iter)) != nullptr) {
|
2017-10-17 14:02:19 +02:00
|
|
|
uint32_t field_flags = mono_field_get_flags(raw_field);
|
|
|
|
|
|
|
|
// Enums have an instance field named value__ which holds the value of the enum.
|
|
|
|
// Enum constants are static, so we will use this to ignore the value__ field.
|
|
|
|
if (field_flags & MONO_FIELD_ATTR_PUBLIC && field_flags & MONO_FIELD_ATTR_STATIC) {
|
|
|
|
enum_fields.push_back(raw_field);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return enum_fields;
|
|
|
|
}
|
|
|
|
#endif
|
|
|
|
|
2017-10-02 23:24:00 +02:00
|
|
|
bool GDMonoClass::has_attribute(GDMonoClass *p_attr_class) {
|
|
|
|
#ifdef DEBUG_ENABLED
|
|
|
|
ERR_FAIL_NULL_V(p_attr_class, false);
|
|
|
|
#endif
|
|
|
|
|
2020-07-05 19:19:36 +02:00
|
|
|
if (!attrs_fetched) {
|
2017-10-02 23:24:00 +02:00
|
|
|
fetch_attributes();
|
2020-07-05 19:19:36 +02:00
|
|
|
}
|
2017-10-02 23:24:00 +02:00
|
|
|
|
2020-07-05 19:19:36 +02:00
|
|
|
if (!attributes) {
|
2017-10-02 23:24:00 +02:00
|
|
|
return false;
|
2020-07-05 19:19:36 +02:00
|
|
|
}
|
2017-10-02 23:24:00 +02:00
|
|
|
|
2018-01-04 21:05:46 +01:00
|
|
|
return mono_custom_attrs_has_attr(attributes, p_attr_class->get_mono_ptr());
|
2017-10-02 23:24:00 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
MonoObject *GDMonoClass::get_attribute(GDMonoClass *p_attr_class) {
|
|
|
|
#ifdef DEBUG_ENABLED
|
2020-04-02 01:20:12 +02:00
|
|
|
ERR_FAIL_NULL_V(p_attr_class, nullptr);
|
2017-10-02 23:24:00 +02:00
|
|
|
#endif
|
|
|
|
|
2020-07-05 19:19:36 +02:00
|
|
|
if (!attrs_fetched) {
|
2017-10-02 23:24:00 +02:00
|
|
|
fetch_attributes();
|
2020-07-05 19:19:36 +02:00
|
|
|
}
|
2017-10-02 23:24:00 +02:00
|
|
|
|
2020-07-05 19:19:36 +02:00
|
|
|
if (!attributes) {
|
2020-04-02 01:20:12 +02:00
|
|
|
return nullptr;
|
2020-07-05 19:19:36 +02:00
|
|
|
}
|
2017-10-02 23:24:00 +02:00
|
|
|
|
2018-01-04 21:05:46 +01:00
|
|
|
return mono_custom_attrs_get_attr(attributes, p_attr_class->get_mono_ptr());
|
2017-10-02 23:24:00 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
void GDMonoClass::fetch_attributes() {
|
2020-04-02 01:20:12 +02:00
|
|
|
ERR_FAIL_COND(attributes != nullptr);
|
2017-10-02 23:24:00 +02:00
|
|
|
|
2018-01-04 21:05:46 +01:00
|
|
|
attributes = mono_custom_attrs_from_class(get_mono_ptr());
|
2017-10-02 23:24:00 +02:00
|
|
|
attrs_fetched = true;
|
|
|
|
}
|
|
|
|
|
|
|
|
void GDMonoClass::fetch_methods_with_godot_api_checks(GDMonoClass *p_native_base) {
|
|
|
|
CRASH_COND(!CACHED_CLASS(GodotObject)->is_assignable_from(this));
|
|
|
|
|
2020-07-05 19:19:36 +02:00
|
|
|
if (methods_fetched) {
|
2017-10-02 23:24:00 +02:00
|
|
|
return;
|
2020-07-05 19:19:36 +02:00
|
|
|
}
|
2017-10-02 23:24:00 +02:00
|
|
|
|
2020-04-02 01:20:12 +02:00
|
|
|
void *iter = nullptr;
|
|
|
|
MonoMethod *raw_method = nullptr;
|
|
|
|
while ((raw_method = mono_class_get_methods(get_mono_ptr(), &iter)) != nullptr) {
|
2017-10-02 23:24:00 +02:00
|
|
|
StringName name = mono_method_get_name(raw_method);
|
|
|
|
|
2018-11-01 16:35:16 +01:00
|
|
|
// get_method implicitly fetches methods and adds them to this->methods
|
2017-10-02 23:24:00 +02:00
|
|
|
GDMonoMethod *method = get_method(raw_method, name);
|
|
|
|
ERR_CONTINUE(!method);
|
|
|
|
|
|
|
|
if (method->get_name() != name) {
|
|
|
|
#ifdef DEBUG_ENABLED
|
|
|
|
String fullname = method->get_ret_type_full_name() + " " + name + "(" + method->get_signature_desc(true) + ")";
|
2019-11-07 09:44:15 +01:00
|
|
|
WARN_PRINT("Method '" + fullname + "' is hidden by Godot API method. Should be '" +
|
|
|
|
method->get_full_name_no_class() + "'. In class '" + namespace_name + "." + class_name + "'.");
|
2017-10-02 23:24:00 +02:00
|
|
|
#endif
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
|
|
|
#ifdef DEBUG_ENABLED
|
|
|
|
// For debug builds, we also fetched from native base classes as well before if this is not a native base class.
|
|
|
|
// This allows us to warn the user here if he is using snake_case by mistake.
|
|
|
|
|
|
|
|
if (p_native_base != this) {
|
|
|
|
GDMonoClass *native_top = p_native_base;
|
|
|
|
while (native_top) {
|
|
|
|
GDMonoMethod *m = native_top->get_method(name, method->get_parameters_count());
|
|
|
|
|
|
|
|
if (m && m->get_name() != name) {
|
|
|
|
// found
|
|
|
|
String fullname = m->get_ret_type_full_name() + " " + name + "(" + m->get_signature_desc(true) + ")";
|
2019-11-07 09:44:15 +01:00
|
|
|
WARN_PRINT("Method '" + fullname + "' should be '" + m->get_full_name_no_class() +
|
|
|
|
"'. In class '" + namespace_name + "." + class_name + "'.");
|
2017-10-02 23:24:00 +02:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
2020-07-05 19:19:36 +02:00
|
|
|
if (native_top == CACHED_CLASS(GodotObject)) {
|
2017-10-02 23:24:00 +02:00
|
|
|
break;
|
2020-07-05 19:19:36 +02:00
|
|
|
}
|
2017-10-02 23:24:00 +02:00
|
|
|
|
|
|
|
native_top = native_top->get_parent_class();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
#endif
|
|
|
|
|
2020-04-02 01:20:12 +02:00
|
|
|
uint32_t flags = mono_method_get_flags(method->mono_method, nullptr);
|
2017-10-02 23:24:00 +02:00
|
|
|
|
2020-07-05 19:19:36 +02:00
|
|
|
if (!(flags & MONO_METHOD_ATTR_VIRTUAL)) {
|
2017-10-02 23:24:00 +02:00
|
|
|
continue;
|
2020-07-05 19:19:36 +02:00
|
|
|
}
|
2017-10-02 23:24:00 +02:00
|
|
|
|
|
|
|
// Virtual method of Godot Object derived type, let's try to find GodotMethod attribute
|
|
|
|
|
|
|
|
GDMonoClass *top = p_native_base;
|
|
|
|
|
|
|
|
while (top) {
|
|
|
|
GDMonoMethod *base_method = top->get_method(name, method->get_parameters_count());
|
|
|
|
|
|
|
|
if (base_method && base_method->has_attribute(CACHED_CLASS(GodotMethodAttribute))) {
|
|
|
|
// Found base method with GodotMethod attribute.
|
|
|
|
// We get the original API method name from this attribute.
|
|
|
|
// This name must point to the virtual method.
|
|
|
|
|
|
|
|
MonoObject *attr = base_method->get_attribute(CACHED_CLASS(GodotMethodAttribute));
|
|
|
|
|
|
|
|
StringName godot_method_name = CACHED_FIELD(GodotMethodAttribute, methodName)->get_string_value(attr);
|
|
|
|
#ifdef DEBUG_ENABLED
|
|
|
|
CRASH_COND(godot_method_name == StringName());
|
|
|
|
#endif
|
|
|
|
MethodKey key = MethodKey(godot_method_name, method->get_parameters_count());
|
|
|
|
GDMonoMethod **existing_method = methods.getptr(key);
|
2020-07-05 19:19:36 +02:00
|
|
|
if (existing_method) {
|
2017-10-02 23:24:00 +02:00
|
|
|
memdelete(*existing_method); // Must delete old one
|
2020-07-05 19:19:36 +02:00
|
|
|
}
|
2017-10-02 23:24:00 +02:00
|
|
|
methods.set(key, method);
|
|
|
|
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
2020-07-05 19:19:36 +02:00
|
|
|
if (top == CACHED_CLASS(GodotObject)) {
|
2017-10-02 23:24:00 +02:00
|
|
|
break;
|
2020-07-05 19:19:36 +02:00
|
|
|
}
|
2017-10-02 23:24:00 +02:00
|
|
|
|
|
|
|
top = top->get_parent_class();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
methods_fetched = true;
|
|
|
|
}
|
|
|
|
|
2018-01-09 17:19:03 +01:00
|
|
|
GDMonoMethod *GDMonoClass::get_fetched_method_unknown_params(const StringName &p_name) {
|
2020-04-02 01:20:12 +02:00
|
|
|
ERR_FAIL_COND_V(!methods_fetched, nullptr);
|
2017-10-02 23:24:00 +02:00
|
|
|
|
2020-04-02 01:20:12 +02:00
|
|
|
const MethodKey *k = nullptr;
|
2017-10-02 23:24:00 +02:00
|
|
|
|
|
|
|
while ((k = methods.next(k))) {
|
2020-07-05 19:19:36 +02:00
|
|
|
if (k->name == p_name) {
|
2017-10-02 23:24:00 +02:00
|
|
|
return methods.get(*k);
|
2020-07-05 19:19:36 +02:00
|
|
|
}
|
2017-10-02 23:24:00 +02:00
|
|
|
}
|
|
|
|
|
2020-04-02 01:20:12 +02:00
|
|
|
return nullptr;
|
2017-10-02 23:24:00 +02:00
|
|
|
}
|
|
|
|
|
2018-01-09 17:19:03 +01:00
|
|
|
bool GDMonoClass::has_fetched_method_unknown_params(const StringName &p_name) {
|
2020-04-02 01:20:12 +02:00
|
|
|
return get_fetched_method_unknown_params(p_name) != nullptr;
|
2018-01-09 17:19:03 +01:00
|
|
|
}
|
|
|
|
|
2019-03-05 18:52:19 +01:00
|
|
|
bool GDMonoClass::implements_interface(GDMonoClass *p_interface) {
|
|
|
|
return mono_class_implements_interface(mono_class, p_interface->get_mono_ptr());
|
|
|
|
}
|
|
|
|
|
2020-04-23 02:19:32 +02:00
|
|
|
bool GDMonoClass::has_public_parameterless_ctor() {
|
|
|
|
GDMonoMethod *ctor = get_method(".ctor", 0);
|
|
|
|
return ctor && ctor->get_visibility() == IMonoClassMember::PUBLIC;
|
|
|
|
}
|
|
|
|
|
2020-12-05 00:19:15 +01:00
|
|
|
GDMonoMethod *GDMonoClass::get_method(const StringName &p_name, uint16_t p_params_count) {
|
2017-10-02 23:24:00 +02:00
|
|
|
MethodKey key = MethodKey(p_name, p_params_count);
|
|
|
|
|
|
|
|
GDMonoMethod **match = methods.getptr(key);
|
|
|
|
|
2020-07-05 19:19:36 +02:00
|
|
|
if (match) {
|
2017-10-02 23:24:00 +02:00
|
|
|
return *match;
|
2020-07-05 19:19:36 +02:00
|
|
|
}
|
2017-10-02 23:24:00 +02:00
|
|
|
|
2020-07-05 19:19:36 +02:00
|
|
|
if (methods_fetched) {
|
2020-04-02 01:20:12 +02:00
|
|
|
return nullptr;
|
2020-07-05 19:19:36 +02:00
|
|
|
}
|
2017-10-02 23:24:00 +02:00
|
|
|
|
|
|
|
MonoMethod *raw_method = mono_class_get_method_from_name(mono_class, String(p_name).utf8().get_data(), p_params_count);
|
|
|
|
|
|
|
|
if (raw_method) {
|
|
|
|
GDMonoMethod *method = memnew(GDMonoMethod(p_name, raw_method));
|
|
|
|
methods.set(key, method);
|
|
|
|
|
|
|
|
return method;
|
|
|
|
}
|
|
|
|
|
2020-04-02 01:20:12 +02:00
|
|
|
return nullptr;
|
2017-10-02 23:24:00 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
GDMonoMethod *GDMonoClass::get_method(MonoMethod *p_raw_method) {
|
|
|
|
MonoMethodSignature *sig = mono_method_signature(p_raw_method);
|
|
|
|
|
|
|
|
int params_count = mono_signature_get_param_count(sig);
|
|
|
|
StringName method_name = mono_method_get_name(p_raw_method);
|
|
|
|
|
|
|
|
return get_method(p_raw_method, method_name, params_count);
|
|
|
|
}
|
|
|
|
|
|
|
|
GDMonoMethod *GDMonoClass::get_method(MonoMethod *p_raw_method, const StringName &p_name) {
|
|
|
|
MonoMethodSignature *sig = mono_method_signature(p_raw_method);
|
|
|
|
int params_count = mono_signature_get_param_count(sig);
|
|
|
|
return get_method(p_raw_method, p_name, params_count);
|
|
|
|
}
|
|
|
|
|
2020-12-05 00:19:15 +01:00
|
|
|
GDMonoMethod *GDMonoClass::get_method(MonoMethod *p_raw_method, const StringName &p_name, uint16_t p_params_count) {
|
2020-04-02 01:20:12 +02:00
|
|
|
ERR_FAIL_NULL_V(p_raw_method, nullptr);
|
2017-10-02 23:24:00 +02:00
|
|
|
|
|
|
|
MethodKey key = MethodKey(p_name, p_params_count);
|
|
|
|
|
|
|
|
GDMonoMethod **match = methods.getptr(key);
|
|
|
|
|
2020-07-05 19:19:36 +02:00
|
|
|
if (match) {
|
2017-10-02 23:24:00 +02:00
|
|
|
return *match;
|
2020-07-05 19:19:36 +02:00
|
|
|
}
|
2017-10-02 23:24:00 +02:00
|
|
|
|
|
|
|
GDMonoMethod *method = memnew(GDMonoMethod(p_name, p_raw_method));
|
|
|
|
methods.set(key, method);
|
|
|
|
|
|
|
|
return method;
|
|
|
|
}
|
|
|
|
|
|
|
|
GDMonoMethod *GDMonoClass::get_method_with_desc(const String &p_description, bool p_include_namespace) {
|
|
|
|
MonoMethodDesc *desc = mono_method_desc_new(p_description.utf8().get_data(), p_include_namespace);
|
|
|
|
MonoMethod *method = mono_method_desc_search_in_class(desc, mono_class);
|
|
|
|
mono_method_desc_free(desc);
|
|
|
|
|
2020-07-05 19:19:36 +02:00
|
|
|
if (!method) {
|
2020-04-23 02:19:32 +02:00
|
|
|
return nullptr;
|
2020-07-05 19:19:36 +02:00
|
|
|
}
|
2020-04-23 02:19:32 +02:00
|
|
|
|
2020-04-02 01:20:12 +02:00
|
|
|
ERR_FAIL_COND_V(mono_method_get_class(method) != mono_class, nullptr);
|
2018-01-09 17:19:03 +01:00
|
|
|
|
2017-10-02 23:24:00 +02:00
|
|
|
return get_method(method);
|
|
|
|
}
|
|
|
|
|
|
|
|
GDMonoField *GDMonoClass::get_field(const StringName &p_name) {
|
|
|
|
Map<StringName, GDMonoField *>::Element *result = fields.find(p_name);
|
|
|
|
|
2020-07-05 19:19:36 +02:00
|
|
|
if (result) {
|
2017-10-02 23:24:00 +02:00
|
|
|
return result->value();
|
2020-07-05 19:19:36 +02:00
|
|
|
}
|
2017-10-02 23:24:00 +02:00
|
|
|
|
2020-07-05 19:19:36 +02:00
|
|
|
if (fields_fetched) {
|
2020-04-02 01:20:12 +02:00
|
|
|
return nullptr;
|
2020-07-05 19:19:36 +02:00
|
|
|
}
|
2017-10-02 23:24:00 +02:00
|
|
|
|
|
|
|
MonoClassField *raw_field = mono_class_get_field_from_name(mono_class, String(p_name).utf8().get_data());
|
|
|
|
|
|
|
|
if (raw_field) {
|
|
|
|
GDMonoField *field = memnew(GDMonoField(raw_field, this));
|
|
|
|
fields.insert(p_name, field);
|
|
|
|
|
|
|
|
return field;
|
|
|
|
}
|
|
|
|
|
2020-04-02 01:20:12 +02:00
|
|
|
return nullptr;
|
2017-10-02 23:24:00 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
const Vector<GDMonoField *> &GDMonoClass::get_all_fields() {
|
2020-07-05 19:19:36 +02:00
|
|
|
if (fields_fetched) {
|
2017-10-02 23:24:00 +02:00
|
|
|
return fields_list;
|
2020-07-05 19:19:36 +02:00
|
|
|
}
|
2017-10-02 23:24:00 +02:00
|
|
|
|
2020-04-02 01:20:12 +02:00
|
|
|
void *iter = nullptr;
|
|
|
|
MonoClassField *raw_field = nullptr;
|
|
|
|
while ((raw_field = mono_class_get_fields(mono_class, &iter)) != nullptr) {
|
2017-10-02 23:24:00 +02:00
|
|
|
StringName name = mono_field_get_name(raw_field);
|
|
|
|
|
|
|
|
Map<StringName, GDMonoField *>::Element *match = fields.find(name);
|
|
|
|
|
|
|
|
if (match) {
|
|
|
|
fields_list.push_back(match->get());
|
|
|
|
} else {
|
|
|
|
GDMonoField *field = memnew(GDMonoField(raw_field, this));
|
|
|
|
fields.insert(name, field);
|
|
|
|
fields_list.push_back(field);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
fields_fetched = true;
|
|
|
|
|
|
|
|
return fields_list;
|
|
|
|
}
|
|
|
|
|
2018-01-04 21:05:46 +01:00
|
|
|
GDMonoProperty *GDMonoClass::get_property(const StringName &p_name) {
|
|
|
|
Map<StringName, GDMonoProperty *>::Element *result = properties.find(p_name);
|
|
|
|
|
2020-07-05 19:19:36 +02:00
|
|
|
if (result) {
|
2018-01-04 21:05:46 +01:00
|
|
|
return result->value();
|
2020-07-05 19:19:36 +02:00
|
|
|
}
|
2018-01-04 21:05:46 +01:00
|
|
|
|
2020-07-05 19:19:36 +02:00
|
|
|
if (properties_fetched) {
|
2020-04-02 01:20:12 +02:00
|
|
|
return nullptr;
|
2020-07-05 19:19:36 +02:00
|
|
|
}
|
2018-01-04 21:05:46 +01:00
|
|
|
|
|
|
|
MonoProperty *raw_property = mono_class_get_property_from_name(mono_class, String(p_name).utf8().get_data());
|
|
|
|
|
|
|
|
if (raw_property) {
|
|
|
|
GDMonoProperty *property = memnew(GDMonoProperty(raw_property, this));
|
|
|
|
properties.insert(p_name, property);
|
|
|
|
|
|
|
|
return property;
|
|
|
|
}
|
|
|
|
|
2020-04-02 01:20:12 +02:00
|
|
|
return nullptr;
|
2018-01-04 21:05:46 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
const Vector<GDMonoProperty *> &GDMonoClass::get_all_properties() {
|
2020-07-05 19:19:36 +02:00
|
|
|
if (properties_fetched) {
|
2018-01-04 21:05:46 +01:00
|
|
|
return properties_list;
|
2020-07-05 19:19:36 +02:00
|
|
|
}
|
2018-01-04 21:05:46 +01:00
|
|
|
|
2020-04-02 01:20:12 +02:00
|
|
|
void *iter = nullptr;
|
|
|
|
MonoProperty *raw_property = nullptr;
|
|
|
|
while ((raw_property = mono_class_get_properties(mono_class, &iter)) != nullptr) {
|
2018-01-04 21:05:46 +01:00
|
|
|
StringName name = mono_property_get_name(raw_property);
|
|
|
|
|
|
|
|
Map<StringName, GDMonoProperty *>::Element *match = properties.find(name);
|
|
|
|
|
|
|
|
if (match) {
|
|
|
|
properties_list.push_back(match->get());
|
|
|
|
} else {
|
|
|
|
GDMonoProperty *property = memnew(GDMonoProperty(raw_property, this));
|
|
|
|
properties.insert(name, property);
|
|
|
|
properties_list.push_back(property);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
properties_fetched = true;
|
|
|
|
|
|
|
|
return properties_list;
|
|
|
|
}
|
|
|
|
|
2018-01-18 18:17:29 +01:00
|
|
|
const Vector<GDMonoClass *> &GDMonoClass::get_all_delegates() {
|
2020-07-05 19:19:36 +02:00
|
|
|
if (delegates_fetched) {
|
2018-01-18 18:17:29 +01:00
|
|
|
return delegates_list;
|
2020-07-05 19:19:36 +02:00
|
|
|
}
|
2018-01-18 18:17:29 +01:00
|
|
|
|
2020-04-02 01:20:12 +02:00
|
|
|
void *iter = nullptr;
|
|
|
|
MonoClass *raw_class = nullptr;
|
|
|
|
while ((raw_class = mono_class_get_nested_types(mono_class, &iter)) != nullptr) {
|
2018-01-18 18:17:29 +01:00
|
|
|
if (mono_class_is_delegate(raw_class)) {
|
|
|
|
StringName name = mono_class_get_name(raw_class);
|
|
|
|
|
|
|
|
Map<StringName, GDMonoClass *>::Element *match = delegates.find(name);
|
|
|
|
|
|
|
|
if (match) {
|
|
|
|
delegates_list.push_back(match->get());
|
|
|
|
} else {
|
|
|
|
GDMonoClass *delegate = memnew(GDMonoClass(mono_class_get_namespace(raw_class), mono_class_get_name(raw_class), raw_class, assembly));
|
|
|
|
delegates.insert(name, delegate);
|
|
|
|
delegates_list.push_back(delegate);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
delegates_fetched = true;
|
|
|
|
|
|
|
|
return delegates_list;
|
|
|
|
}
|
|
|
|
|
2018-11-01 16:35:16 +01:00
|
|
|
const Vector<GDMonoMethod *> &GDMonoClass::get_all_methods() {
|
2018-12-08 09:54:12 +01:00
|
|
|
if (!method_list_fetched) {
|
2020-04-02 01:20:12 +02:00
|
|
|
void *iter = nullptr;
|
|
|
|
MonoMethod *raw_method = nullptr;
|
|
|
|
while ((raw_method = mono_class_get_methods(get_mono_ptr(), &iter)) != nullptr) {
|
2018-12-08 09:54:12 +01:00
|
|
|
method_list.push_back(memnew(GDMonoMethod(mono_method_get_name(raw_method), raw_method)));
|
|
|
|
}
|
|
|
|
|
|
|
|
method_list_fetched = true;
|
|
|
|
}
|
2018-11-01 16:35:16 +01:00
|
|
|
|
|
|
|
return method_list;
|
|
|
|
}
|
|
|
|
|
2017-10-02 23:24:00 +02:00
|
|
|
GDMonoClass::GDMonoClass(const StringName &p_namespace, const StringName &p_name, MonoClass *p_class, GDMonoAssembly *p_assembly) {
|
|
|
|
namespace_name = p_namespace;
|
|
|
|
class_name = p_name;
|
|
|
|
mono_class = p_class;
|
|
|
|
assembly = p_assembly;
|
|
|
|
|
|
|
|
attrs_fetched = false;
|
2020-04-02 01:20:12 +02:00
|
|
|
attributes = nullptr;
|
2017-10-02 23:24:00 +02:00
|
|
|
|
|
|
|
methods_fetched = false;
|
2018-11-01 16:35:16 +01:00
|
|
|
method_list_fetched = false;
|
2017-10-02 23:24:00 +02:00
|
|
|
fields_fetched = false;
|
2018-01-04 21:05:46 +01:00
|
|
|
properties_fetched = false;
|
2018-01-18 18:17:29 +01:00
|
|
|
delegates_fetched = false;
|
2017-10-02 23:24:00 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
GDMonoClass::~GDMonoClass() {
|
|
|
|
if (attributes) {
|
|
|
|
mono_custom_attrs_free(attributes);
|
|
|
|
}
|
|
|
|
|
|
|
|
for (Map<StringName, GDMonoField *>::Element *E = fields.front(); E; E = E->next()) {
|
|
|
|
memdelete(E->value());
|
|
|
|
}
|
|
|
|
|
2018-01-04 21:05:46 +01:00
|
|
|
for (Map<StringName, GDMonoProperty *>::Element *E = properties.front(); E; E = E->next()) {
|
|
|
|
memdelete(E->value());
|
|
|
|
}
|
|
|
|
|
2017-10-02 23:24:00 +02:00
|
|
|
{
|
|
|
|
// Ugly workaround...
|
|
|
|
// We may have duplicated values, because we redirect snake_case methods to PascalCasel (only Godot API methods).
|
|
|
|
// This way, we end with both the snake_case name and the PascalCasel name paired with the same method.
|
|
|
|
// Therefore, we must avoid deleting the same pointer twice.
|
|
|
|
|
|
|
|
int offset = 0;
|
|
|
|
Vector<GDMonoMethod *> deleted_methods;
|
|
|
|
deleted_methods.resize(methods.size());
|
|
|
|
|
2020-04-02 01:20:12 +02:00
|
|
|
const MethodKey *k = nullptr;
|
2017-10-02 23:24:00 +02:00
|
|
|
while ((k = methods.next(k))) {
|
|
|
|
GDMonoMethod *method = methods.get(*k);
|
|
|
|
|
|
|
|
if (method) {
|
|
|
|
for (int i = 0; i < offset; i++) {
|
|
|
|
if (deleted_methods[i] == method) {
|
|
|
|
// Already deleted
|
|
|
|
goto already_deleted;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-07-25 03:11:03 +02:00
|
|
|
deleted_methods.write[offset] = method;
|
2017-10-02 23:24:00 +02:00
|
|
|
++offset;
|
|
|
|
|
|
|
|
memdelete(method);
|
|
|
|
}
|
|
|
|
|
|
|
|
already_deleted:;
|
|
|
|
}
|
|
|
|
|
|
|
|
methods.clear();
|
|
|
|
}
|
2018-11-01 16:35:16 +01:00
|
|
|
|
|
|
|
for (int i = 0; i < method_list.size(); ++i) {
|
|
|
|
memdelete(method_list[i]);
|
|
|
|
}
|
2017-10-02 23:24:00 +02:00
|
|
|
}
|