gd_mono_class.cpp 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570
  1. /*************************************************************************/
  2. /* gd_mono_class.cpp */
  3. /*************************************************************************/
  4. /* This file is part of: */
  5. /* GODOT ENGINE */
  6. /* https://godotengine.org */
  7. /*************************************************************************/
  8. /* Copyright (c) 2007-2021 Juan Linietsky, Ariel Manzur. */
  9. /* Copyright (c) 2014-2021 Godot Engine contributors (cf. AUTHORS.md). */
  10. /* */
  11. /* Permission is hereby granted, free of charge, to any person obtaining */
  12. /* a copy of this software and associated documentation files (the */
  13. /* "Software"), to deal in the Software without restriction, including */
  14. /* without limitation the rights to use, copy, modify, merge, publish, */
  15. /* distribute, sublicense, and/or sell copies of the Software, and to */
  16. /* permit persons to whom the Software is furnished to do so, subject to */
  17. /* the following conditions: */
  18. /* */
  19. /* The above copyright notice and this permission notice shall be */
  20. /* included in all copies or substantial portions of the Software. */
  21. /* */
  22. /* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, */
  23. /* EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF */
  24. /* MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.*/
  25. /* IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY */
  26. /* CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, */
  27. /* TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE */
  28. /* SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. */
  29. /*************************************************************************/
  30. #include "gd_mono_class.h"
  31. #include <mono/metadata/attrdefs.h>
  32. #include <mono/metadata/debug-helpers.h>
  33. #include "gd_mono_assembly.h"
  34. #include "gd_mono_cache.h"
  35. #include "gd_mono_marshal.h"
  36. String GDMonoClass::get_full_name(MonoClass *p_mono_class) {
  37. // mono_type_get_full_name is not exposed to embedders, but this seems to do the job
  38. MonoReflectionType *type_obj = mono_type_get_object(mono_domain_get(), get_mono_type(p_mono_class));
  39. MonoException *exc = nullptr;
  40. MonoString *str = GDMonoUtils::object_to_string((MonoObject *)type_obj, &exc);
  41. UNHANDLED_EXCEPTION(exc);
  42. return GDMonoMarshal::mono_string_to_godot(str);
  43. }
  44. MonoType *GDMonoClass::get_mono_type(MonoClass *p_mono_class) {
  45. return mono_class_get_type(p_mono_class);
  46. }
  47. String GDMonoClass::get_full_name() const {
  48. return get_full_name(mono_class);
  49. }
  50. String GDMonoClass::get_type_desc() const {
  51. return GDMonoUtils::get_type_desc(get_mono_type());
  52. }
  53. MonoType *GDMonoClass::get_mono_type() const {
  54. // Careful, you cannot compare two MonoType*.
  55. // There is mono_metadata_type_equal, how is this different from comparing two MonoClass*?
  56. return get_mono_type(mono_class);
  57. }
  58. uint32_t GDMonoClass::get_flags() const {
  59. return mono_class_get_flags(mono_class);
  60. }
  61. bool GDMonoClass::is_static() const {
  62. uint32_t static_class_flags = MONO_TYPE_ATTR_ABSTRACT | MONO_TYPE_ATTR_SEALED;
  63. return (get_flags() & static_class_flags) == static_class_flags;
  64. }
  65. bool GDMonoClass::is_assignable_from(GDMonoClass *p_from) const {
  66. return mono_class_is_assignable_from(mono_class, p_from->mono_class);
  67. }
  68. StringName GDMonoClass::get_namespace() const {
  69. GDMonoClass *nesting_class = get_nesting_class();
  70. if (!nesting_class) {
  71. return namespace_name;
  72. }
  73. return nesting_class->get_namespace();
  74. }
  75. String GDMonoClass::get_name_for_lookup() const {
  76. GDMonoClass *nesting_class = get_nesting_class();
  77. if (!nesting_class) {
  78. return class_name;
  79. }
  80. return nesting_class->get_name_for_lookup() + "/" + class_name;
  81. }
  82. GDMonoClass *GDMonoClass::get_parent_class() const {
  83. MonoClass *parent_mono_class = mono_class_get_parent(mono_class);
  84. return parent_mono_class ? GDMono::get_singleton()->get_class(parent_mono_class) : nullptr;
  85. }
  86. GDMonoClass *GDMonoClass::get_nesting_class() const {
  87. MonoClass *nesting_type = mono_class_get_nesting_type(mono_class);
  88. return nesting_type ? GDMono::get_singleton()->get_class(nesting_type) : nullptr;
  89. }
  90. #ifdef TOOLS_ENABLED
  91. Vector<MonoClassField *> GDMonoClass::get_enum_fields() {
  92. bool class_is_enum = mono_class_is_enum(mono_class);
  93. ERR_FAIL_COND_V(!class_is_enum, Vector<MonoClassField *>());
  94. Vector<MonoClassField *> enum_fields;
  95. void *iter = nullptr;
  96. MonoClassField *raw_field = nullptr;
  97. while ((raw_field = mono_class_get_fields(get_mono_ptr(), &iter)) != nullptr) {
  98. uint32_t field_flags = mono_field_get_flags(raw_field);
  99. // Enums have an instance field named value__ which holds the value of the enum.
  100. // Enum constants are static, so we will use this to ignore the value__ field.
  101. if (field_flags & MONO_FIELD_ATTR_PUBLIC && field_flags & MONO_FIELD_ATTR_STATIC) {
  102. enum_fields.push_back(raw_field);
  103. }
  104. }
  105. return enum_fields;
  106. }
  107. #endif
  108. bool GDMonoClass::has_attribute(GDMonoClass *p_attr_class) {
  109. #ifdef DEBUG_ENABLED
  110. ERR_FAIL_NULL_V(p_attr_class, false);
  111. #endif
  112. if (!attrs_fetched) {
  113. fetch_attributes();
  114. }
  115. if (!attributes) {
  116. return false;
  117. }
  118. return mono_custom_attrs_has_attr(attributes, p_attr_class->get_mono_ptr());
  119. }
  120. MonoObject *GDMonoClass::get_attribute(GDMonoClass *p_attr_class) {
  121. #ifdef DEBUG_ENABLED
  122. ERR_FAIL_NULL_V(p_attr_class, nullptr);
  123. #endif
  124. if (!attrs_fetched) {
  125. fetch_attributes();
  126. }
  127. if (!attributes) {
  128. return nullptr;
  129. }
  130. return mono_custom_attrs_get_attr(attributes, p_attr_class->get_mono_ptr());
  131. }
  132. void GDMonoClass::fetch_attributes() {
  133. ERR_FAIL_COND(attributes != nullptr);
  134. attributes = mono_custom_attrs_from_class(get_mono_ptr());
  135. attrs_fetched = true;
  136. }
  137. void GDMonoClass::fetch_methods_with_godot_api_checks(GDMonoClass *p_native_base) {
  138. CRASH_COND(!CACHED_CLASS(GodotObject)->is_assignable_from(this));
  139. if (methods_fetched) {
  140. return;
  141. }
  142. void *iter = nullptr;
  143. MonoMethod *raw_method = nullptr;
  144. while ((raw_method = mono_class_get_methods(get_mono_ptr(), &iter)) != nullptr) {
  145. StringName name = String::utf8(mono_method_get_name(raw_method));
  146. // get_method implicitly fetches methods and adds them to this->methods
  147. GDMonoMethod *method = get_method(raw_method, name);
  148. ERR_CONTINUE(!method);
  149. if (method->get_name() != name) {
  150. #ifdef DEBUG_ENABLED
  151. String fullname = method->get_ret_type_full_name() + " " + name + "(" + method->get_signature_desc(true) + ")";
  152. WARN_PRINT("Method '" + fullname + "' is hidden by Godot API method. Should be '" +
  153. method->get_full_name_no_class() + "'. In class '" + namespace_name + "." + class_name + "'.");
  154. #endif
  155. continue;
  156. }
  157. #ifdef DEBUG_ENABLED
  158. // For debug builds, we also fetched from native base classes as well before if this is not a native base class.
  159. // This allows us to warn the user here if he is using snake_case by mistake.
  160. if (p_native_base != this) {
  161. GDMonoClass *native_top = p_native_base;
  162. while (native_top) {
  163. GDMonoMethod *m = native_top->get_method(name, method->get_parameters_count());
  164. if (m && m->get_name() != name) {
  165. // found
  166. String fullname = m->get_ret_type_full_name() + " " + name + "(" + m->get_signature_desc(true) + ")";
  167. WARN_PRINT("Method '" + fullname + "' should be '" + m->get_full_name_no_class() +
  168. "'. In class '" + namespace_name + "." + class_name + "'.");
  169. break;
  170. }
  171. if (native_top == CACHED_CLASS(GodotObject)) {
  172. break;
  173. }
  174. native_top = native_top->get_parent_class();
  175. }
  176. }
  177. #endif
  178. uint32_t flags = mono_method_get_flags(method->mono_method, nullptr);
  179. if (!(flags & MONO_METHOD_ATTR_VIRTUAL)) {
  180. continue;
  181. }
  182. // Virtual method of Godot Object derived type, let's try to find GodotMethod attribute
  183. GDMonoClass *top = p_native_base;
  184. while (top) {
  185. GDMonoMethod *base_method = top->get_method(name, method->get_parameters_count());
  186. if (base_method && base_method->has_attribute(CACHED_CLASS(GodotMethodAttribute))) {
  187. // Found base method with GodotMethod attribute.
  188. // We get the original API method name from this attribute.
  189. // This name must point to the virtual method.
  190. MonoObject *attr = base_method->get_attribute(CACHED_CLASS(GodotMethodAttribute));
  191. StringName godot_method_name = CACHED_FIELD(GodotMethodAttribute, methodName)->get_string_value(attr);
  192. #ifdef DEBUG_ENABLED
  193. CRASH_COND(godot_method_name == StringName());
  194. #endif
  195. MethodKey key = MethodKey(godot_method_name, method->get_parameters_count());
  196. GDMonoMethod **existing_method = methods.getptr(key);
  197. if (existing_method) {
  198. memdelete(*existing_method); // Must delete old one
  199. }
  200. methods.set(key, method);
  201. break;
  202. }
  203. if (top == CACHED_CLASS(GodotObject)) {
  204. break;
  205. }
  206. top = top->get_parent_class();
  207. }
  208. }
  209. methods_fetched = true;
  210. }
  211. GDMonoMethod *GDMonoClass::get_fetched_method_unknown_params(const StringName &p_name) {
  212. ERR_FAIL_COND_V(!methods_fetched, nullptr);
  213. const MethodKey *k = nullptr;
  214. while ((k = methods.next(k))) {
  215. if (k->name == p_name) {
  216. return methods.get(*k);
  217. }
  218. }
  219. return nullptr;
  220. }
  221. bool GDMonoClass::has_fetched_method_unknown_params(const StringName &p_name) {
  222. return get_fetched_method_unknown_params(p_name) != nullptr;
  223. }
  224. bool GDMonoClass::implements_interface(GDMonoClass *p_interface) {
  225. return mono_class_implements_interface(mono_class, p_interface->get_mono_ptr());
  226. }
  227. bool GDMonoClass::has_public_parameterless_ctor() {
  228. GDMonoMethod *ctor = get_method(".ctor", 0);
  229. return ctor && ctor->get_visibility() == IMonoClassMember::PUBLIC;
  230. }
  231. GDMonoMethod *GDMonoClass::get_method(const StringName &p_name, uint16_t p_params_count) {
  232. MethodKey key = MethodKey(p_name, p_params_count);
  233. GDMonoMethod **match = methods.getptr(key);
  234. if (match) {
  235. return *match;
  236. }
  237. if (methods_fetched) {
  238. return nullptr;
  239. }
  240. MonoMethod *raw_method = mono_class_get_method_from_name(mono_class, String(p_name).utf8().get_data(), p_params_count);
  241. if (raw_method) {
  242. GDMonoMethod *method = memnew(GDMonoMethod(p_name, raw_method));
  243. methods.set(key, method);
  244. return method;
  245. }
  246. return nullptr;
  247. }
  248. GDMonoMethod *GDMonoClass::get_method(MonoMethod *p_raw_method) {
  249. MonoMethodSignature *sig = mono_method_signature(p_raw_method);
  250. int params_count = mono_signature_get_param_count(sig);
  251. StringName method_name = String::utf8(mono_method_get_name(p_raw_method));
  252. return get_method(p_raw_method, method_name, params_count);
  253. }
  254. GDMonoMethod *GDMonoClass::get_method(MonoMethod *p_raw_method, const StringName &p_name) {
  255. MonoMethodSignature *sig = mono_method_signature(p_raw_method);
  256. int params_count = mono_signature_get_param_count(sig);
  257. return get_method(p_raw_method, p_name, params_count);
  258. }
  259. GDMonoMethod *GDMonoClass::get_method(MonoMethod *p_raw_method, const StringName &p_name, uint16_t p_params_count) {
  260. ERR_FAIL_NULL_V(p_raw_method, nullptr);
  261. MethodKey key = MethodKey(p_name, p_params_count);
  262. GDMonoMethod **match = methods.getptr(key);
  263. if (match) {
  264. return *match;
  265. }
  266. GDMonoMethod *method = memnew(GDMonoMethod(p_name, p_raw_method));
  267. methods.set(key, method);
  268. return method;
  269. }
  270. GDMonoMethod *GDMonoClass::get_method_with_desc(const String &p_description, bool p_include_namespace) {
  271. MonoMethodDesc *desc = mono_method_desc_new(p_description.utf8().get_data(), p_include_namespace);
  272. MonoMethod *method = mono_method_desc_search_in_class(desc, mono_class);
  273. mono_method_desc_free(desc);
  274. if (!method) {
  275. return nullptr;
  276. }
  277. ERR_FAIL_COND_V(mono_method_get_class(method) != mono_class, nullptr);
  278. return get_method(method);
  279. }
  280. GDMonoField *GDMonoClass::get_field(const StringName &p_name) {
  281. Map<StringName, GDMonoField *>::Element *result = fields.find(p_name);
  282. if (result) {
  283. return result->value();
  284. }
  285. if (fields_fetched) {
  286. return nullptr;
  287. }
  288. MonoClassField *raw_field = mono_class_get_field_from_name(mono_class, String(p_name).utf8().get_data());
  289. if (raw_field) {
  290. GDMonoField *field = memnew(GDMonoField(raw_field, this));
  291. fields.insert(p_name, field);
  292. return field;
  293. }
  294. return nullptr;
  295. }
  296. const Vector<GDMonoField *> &GDMonoClass::get_all_fields() {
  297. if (fields_fetched) {
  298. return fields_list;
  299. }
  300. void *iter = nullptr;
  301. MonoClassField *raw_field = nullptr;
  302. while ((raw_field = mono_class_get_fields(mono_class, &iter)) != nullptr) {
  303. StringName name = String::utf8(mono_field_get_name(raw_field));
  304. Map<StringName, GDMonoField *>::Element *match = fields.find(name);
  305. if (match) {
  306. fields_list.push_back(match->get());
  307. } else {
  308. GDMonoField *field = memnew(GDMonoField(raw_field, this));
  309. fields.insert(name, field);
  310. fields_list.push_back(field);
  311. }
  312. }
  313. fields_fetched = true;
  314. return fields_list;
  315. }
  316. GDMonoProperty *GDMonoClass::get_property(const StringName &p_name) {
  317. Map<StringName, GDMonoProperty *>::Element *result = properties.find(p_name);
  318. if (result) {
  319. return result->value();
  320. }
  321. if (properties_fetched) {
  322. return nullptr;
  323. }
  324. MonoProperty *raw_property = mono_class_get_property_from_name(mono_class, String(p_name).utf8().get_data());
  325. if (raw_property) {
  326. GDMonoProperty *property = memnew(GDMonoProperty(raw_property, this));
  327. properties.insert(p_name, property);
  328. return property;
  329. }
  330. return nullptr;
  331. }
  332. const Vector<GDMonoProperty *> &GDMonoClass::get_all_properties() {
  333. if (properties_fetched) {
  334. return properties_list;
  335. }
  336. void *iter = nullptr;
  337. MonoProperty *raw_property = nullptr;
  338. while ((raw_property = mono_class_get_properties(mono_class, &iter)) != nullptr) {
  339. StringName name = String::utf8(mono_property_get_name(raw_property));
  340. Map<StringName, GDMonoProperty *>::Element *match = properties.find(name);
  341. if (match) {
  342. properties_list.push_back(match->get());
  343. } else {
  344. GDMonoProperty *property = memnew(GDMonoProperty(raw_property, this));
  345. properties.insert(name, property);
  346. properties_list.push_back(property);
  347. }
  348. }
  349. properties_fetched = true;
  350. return properties_list;
  351. }
  352. const Vector<GDMonoClass *> &GDMonoClass::get_all_delegates() {
  353. if (delegates_fetched) {
  354. return delegates_list;
  355. }
  356. void *iter = nullptr;
  357. MonoClass *raw_class = nullptr;
  358. while ((raw_class = mono_class_get_nested_types(mono_class, &iter)) != nullptr) {
  359. if (mono_class_is_delegate(raw_class)) {
  360. StringName name = String::utf8(mono_class_get_name(raw_class));
  361. Map<StringName, GDMonoClass *>::Element *match = delegates.find(name);
  362. if (match) {
  363. delegates_list.push_back(match->get());
  364. } else {
  365. GDMonoClass *delegate = memnew(GDMonoClass(String::utf8(mono_class_get_namespace(raw_class)), String::utf8(mono_class_get_name(raw_class)), raw_class, assembly));
  366. delegates.insert(name, delegate);
  367. delegates_list.push_back(delegate);
  368. }
  369. }
  370. }
  371. delegates_fetched = true;
  372. return delegates_list;
  373. }
  374. const Vector<GDMonoMethod *> &GDMonoClass::get_all_methods() {
  375. if (!method_list_fetched) {
  376. void *iter = nullptr;
  377. MonoMethod *raw_method = nullptr;
  378. while ((raw_method = mono_class_get_methods(get_mono_ptr(), &iter)) != nullptr) {
  379. method_list.push_back(memnew(GDMonoMethod(String::utf8(mono_method_get_name(raw_method)), raw_method)));
  380. }
  381. method_list_fetched = true;
  382. }
  383. return method_list;
  384. }
  385. GDMonoClass::GDMonoClass(const StringName &p_namespace, const StringName &p_name, MonoClass *p_class, GDMonoAssembly *p_assembly) {
  386. namespace_name = p_namespace;
  387. class_name = p_name;
  388. mono_class = p_class;
  389. assembly = p_assembly;
  390. attrs_fetched = false;
  391. attributes = nullptr;
  392. methods_fetched = false;
  393. method_list_fetched = false;
  394. fields_fetched = false;
  395. properties_fetched = false;
  396. delegates_fetched = false;
  397. }
  398. GDMonoClass::~GDMonoClass() {
  399. if (attributes) {
  400. mono_custom_attrs_free(attributes);
  401. }
  402. for (Map<StringName, GDMonoField *>::Element *E = fields.front(); E; E = E->next()) {
  403. memdelete(E->value());
  404. }
  405. for (Map<StringName, GDMonoProperty *>::Element *E = properties.front(); E; E = E->next()) {
  406. memdelete(E->value());
  407. }
  408. {
  409. // Ugly workaround...
  410. // We may have duplicated values, because we redirect snake_case methods to PascalCasel (only Godot API methods).
  411. // This way, we end with both the snake_case name and the PascalCasel name paired with the same method.
  412. // Therefore, we must avoid deleting the same pointer twice.
  413. int offset = 0;
  414. Vector<GDMonoMethod *> deleted_methods;
  415. deleted_methods.resize(methods.size());
  416. const MethodKey *k = nullptr;
  417. while ((k = methods.next(k))) {
  418. GDMonoMethod *method = methods.get(*k);
  419. if (method) {
  420. for (int i = 0; i < offset; i++) {
  421. if (deleted_methods[i] == method) {
  422. // Already deleted
  423. goto already_deleted;
  424. }
  425. }
  426. deleted_methods.write[offset] = method;
  427. ++offset;
  428. memdelete(method);
  429. }
  430. already_deleted:;
  431. }
  432. methods.clear();
  433. }
  434. for (int i = 0; i < method_list.size(); ++i) {
  435. memdelete(method_list[i]);
  436. }
  437. }