gd_mono_class.cpp 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497
  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-2018 Juan Linietsky, Ariel Manzur. */
  9. /* Copyright (c) 2014-2018 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 "gd_mono_assembly.h"
  33. MonoType *GDMonoClass::get_raw_type(GDMonoClass *p_class) {
  34. return mono_class_get_type(p_class->get_mono_ptr());
  35. }
  36. bool GDMonoClass::is_assignable_from(GDMonoClass *p_from) const {
  37. return mono_class_is_assignable_from(mono_class, p_from->mono_class);
  38. }
  39. String GDMonoClass::get_full_name() const {
  40. String res = namespace_name;
  41. if (res.length())
  42. res += ".";
  43. return res + class_name;
  44. }
  45. GDMonoClass *GDMonoClass::get_parent_class() {
  46. if (assembly) {
  47. MonoClass *parent_mono_class = mono_class_get_parent(mono_class);
  48. if (parent_mono_class) {
  49. return GDMono::get_singleton()->get_class(parent_mono_class);
  50. }
  51. }
  52. return NULL;
  53. }
  54. #ifdef TOOLS_ENABLED
  55. Vector<MonoClassField *> GDMonoClass::get_enum_fields() {
  56. bool class_is_enum = mono_class_is_enum(mono_class);
  57. ERR_FAIL_COND_V(!class_is_enum, Vector<MonoClassField *>());
  58. Vector<MonoClassField *> enum_fields;
  59. void *iter = NULL;
  60. MonoClassField *raw_field = NULL;
  61. while ((raw_field = mono_class_get_fields(get_mono_ptr(), &iter)) != NULL) {
  62. uint32_t field_flags = mono_field_get_flags(raw_field);
  63. // Enums have an instance field named value__ which holds the value of the enum.
  64. // Enum constants are static, so we will use this to ignore the value__ field.
  65. if (field_flags & MONO_FIELD_ATTR_PUBLIC && field_flags & MONO_FIELD_ATTR_STATIC) {
  66. enum_fields.push_back(raw_field);
  67. }
  68. }
  69. return enum_fields;
  70. }
  71. #endif
  72. bool GDMonoClass::has_attribute(GDMonoClass *p_attr_class) {
  73. #ifdef DEBUG_ENABLED
  74. ERR_FAIL_NULL_V(p_attr_class, false);
  75. #endif
  76. if (!attrs_fetched)
  77. fetch_attributes();
  78. if (!attributes)
  79. return false;
  80. return mono_custom_attrs_has_attr(attributes, p_attr_class->get_mono_ptr());
  81. }
  82. MonoObject *GDMonoClass::get_attribute(GDMonoClass *p_attr_class) {
  83. #ifdef DEBUG_ENABLED
  84. ERR_FAIL_NULL_V(p_attr_class, NULL);
  85. #endif
  86. if (!attrs_fetched)
  87. fetch_attributes();
  88. if (!attributes)
  89. return NULL;
  90. return mono_custom_attrs_get_attr(attributes, p_attr_class->get_mono_ptr());
  91. }
  92. void GDMonoClass::fetch_attributes() {
  93. ERR_FAIL_COND(attributes != NULL);
  94. attributes = mono_custom_attrs_from_class(get_mono_ptr());
  95. attrs_fetched = true;
  96. }
  97. void GDMonoClass::fetch_methods_with_godot_api_checks(GDMonoClass *p_native_base) {
  98. CRASH_COND(!CACHED_CLASS(GodotObject)->is_assignable_from(this));
  99. if (methods_fetched)
  100. return;
  101. void *iter = NULL;
  102. MonoMethod *raw_method = NULL;
  103. while ((raw_method = mono_class_get_methods(get_mono_ptr(), &iter)) != NULL) {
  104. StringName name = mono_method_get_name(raw_method);
  105. GDMonoMethod *method = get_method(raw_method, name);
  106. ERR_CONTINUE(!method);
  107. if (method->get_name() != name) {
  108. #ifdef DEBUG_ENABLED
  109. String fullname = method->get_ret_type_full_name() + " " + name + "(" + method->get_signature_desc(true) + ")";
  110. WARN_PRINTS("Method `" + fullname + "` is hidden by Godot API method. Should be `" +
  111. method->get_full_name_no_class() + "`. In class `" + namespace_name + "." + class_name + "`.");
  112. #endif
  113. continue;
  114. }
  115. #ifdef DEBUG_ENABLED
  116. // For debug builds, we also fetched from native base classes as well before if this is not a native base class.
  117. // This allows us to warn the user here if he is using snake_case by mistake.
  118. if (p_native_base != this) {
  119. GDMonoClass *native_top = p_native_base;
  120. while (native_top) {
  121. GDMonoMethod *m = native_top->get_method(name, method->get_parameters_count());
  122. if (m && m->get_name() != name) {
  123. // found
  124. String fullname = m->get_ret_type_full_name() + " " + name + "(" + m->get_signature_desc(true) + ")";
  125. WARN_PRINTS("Method `" + fullname + "` should be `" + m->get_full_name_no_class() +
  126. "`. In class `" + namespace_name + "." + class_name + "`.");
  127. break;
  128. }
  129. if (native_top == CACHED_CLASS(GodotObject))
  130. break;
  131. native_top = native_top->get_parent_class();
  132. }
  133. }
  134. #endif
  135. uint32_t flags = mono_method_get_flags(method->mono_method, NULL);
  136. if (!(flags & MONO_METHOD_ATTR_VIRTUAL))
  137. continue;
  138. // Virtual method of Godot Object derived type, let's try to find GodotMethod attribute
  139. GDMonoClass *top = p_native_base;
  140. while (top) {
  141. GDMonoMethod *base_method = top->get_method(name, method->get_parameters_count());
  142. if (base_method && base_method->has_attribute(CACHED_CLASS(GodotMethodAttribute))) {
  143. // Found base method with GodotMethod attribute.
  144. // We get the original API method name from this attribute.
  145. // This name must point to the virtual method.
  146. MonoObject *attr = base_method->get_attribute(CACHED_CLASS(GodotMethodAttribute));
  147. StringName godot_method_name = CACHED_FIELD(GodotMethodAttribute, methodName)->get_string_value(attr);
  148. #ifdef DEBUG_ENABLED
  149. CRASH_COND(godot_method_name == StringName());
  150. #endif
  151. MethodKey key = MethodKey(godot_method_name, method->get_parameters_count());
  152. GDMonoMethod **existing_method = methods.getptr(key);
  153. if (existing_method)
  154. memdelete(*existing_method); // Must delete old one
  155. methods.set(key, method);
  156. break;
  157. }
  158. if (top == CACHED_CLASS(GodotObject))
  159. break;
  160. top = top->get_parent_class();
  161. }
  162. }
  163. methods_fetched = true;
  164. }
  165. GDMonoMethod *GDMonoClass::get_fetched_method_unknown_params(const StringName &p_name) {
  166. ERR_FAIL_COND_V(!methods_fetched, NULL);
  167. const MethodKey *k = NULL;
  168. while ((k = methods.next(k))) {
  169. if (k->name == p_name)
  170. return methods.get(*k);
  171. }
  172. return NULL;
  173. }
  174. bool GDMonoClass::has_fetched_method_unknown_params(const StringName &p_name) {
  175. return get_fetched_method_unknown_params(p_name) != NULL;
  176. }
  177. GDMonoMethod *GDMonoClass::get_method(const StringName &p_name, int p_params_count) {
  178. MethodKey key = MethodKey(p_name, p_params_count);
  179. GDMonoMethod **match = methods.getptr(key);
  180. if (match)
  181. return *match;
  182. if (methods_fetched)
  183. return NULL;
  184. MonoMethod *raw_method = mono_class_get_method_from_name(mono_class, String(p_name).utf8().get_data(), p_params_count);
  185. if (raw_method) {
  186. GDMonoMethod *method = memnew(GDMonoMethod(p_name, raw_method));
  187. methods.set(key, method);
  188. return method;
  189. }
  190. return NULL;
  191. }
  192. GDMonoMethod *GDMonoClass::get_method(MonoMethod *p_raw_method) {
  193. MonoMethodSignature *sig = mono_method_signature(p_raw_method);
  194. int params_count = mono_signature_get_param_count(sig);
  195. StringName method_name = mono_method_get_name(p_raw_method);
  196. return get_method(p_raw_method, method_name, params_count);
  197. }
  198. GDMonoMethod *GDMonoClass::get_method(MonoMethod *p_raw_method, const StringName &p_name) {
  199. MonoMethodSignature *sig = mono_method_signature(p_raw_method);
  200. int params_count = mono_signature_get_param_count(sig);
  201. return get_method(p_raw_method, p_name, params_count);
  202. }
  203. GDMonoMethod *GDMonoClass::get_method(MonoMethod *p_raw_method, const StringName &p_name, int p_params_count) {
  204. ERR_FAIL_NULL_V(p_raw_method, NULL);
  205. MethodKey key = MethodKey(p_name, p_params_count);
  206. GDMonoMethod **match = methods.getptr(key);
  207. if (match)
  208. return *match;
  209. GDMonoMethod *method = memnew(GDMonoMethod(p_name, p_raw_method));
  210. methods.set(key, method);
  211. return method;
  212. }
  213. GDMonoMethod *GDMonoClass::get_method_with_desc(const String &p_description, bool p_include_namespace) {
  214. MonoMethodDesc *desc = mono_method_desc_new(p_description.utf8().get_data(), p_include_namespace);
  215. MonoMethod *method = mono_method_desc_search_in_class(desc, mono_class);
  216. mono_method_desc_free(desc);
  217. ERR_FAIL_COND_V(mono_method_get_class(method) != mono_class, NULL);
  218. return get_method(method);
  219. }
  220. GDMonoField *GDMonoClass::get_field(const StringName &p_name) {
  221. Map<StringName, GDMonoField *>::Element *result = fields.find(p_name);
  222. if (result)
  223. return result->value();
  224. if (fields_fetched)
  225. return NULL;
  226. MonoClassField *raw_field = mono_class_get_field_from_name(mono_class, String(p_name).utf8().get_data());
  227. if (raw_field) {
  228. GDMonoField *field = memnew(GDMonoField(raw_field, this));
  229. fields.insert(p_name, field);
  230. return field;
  231. }
  232. return NULL;
  233. }
  234. const Vector<GDMonoField *> &GDMonoClass::get_all_fields() {
  235. if (fields_fetched)
  236. return fields_list;
  237. void *iter = NULL;
  238. MonoClassField *raw_field = NULL;
  239. while ((raw_field = mono_class_get_fields(mono_class, &iter)) != NULL) {
  240. StringName name = mono_field_get_name(raw_field);
  241. Map<StringName, GDMonoField *>::Element *match = fields.find(name);
  242. if (match) {
  243. fields_list.push_back(match->get());
  244. } else {
  245. GDMonoField *field = memnew(GDMonoField(raw_field, this));
  246. fields.insert(name, field);
  247. fields_list.push_back(field);
  248. }
  249. }
  250. fields_fetched = true;
  251. return fields_list;
  252. }
  253. GDMonoProperty *GDMonoClass::get_property(const StringName &p_name) {
  254. Map<StringName, GDMonoProperty *>::Element *result = properties.find(p_name);
  255. if (result)
  256. return result->value();
  257. if (properties_fetched)
  258. return NULL;
  259. MonoProperty *raw_property = mono_class_get_property_from_name(mono_class, String(p_name).utf8().get_data());
  260. if (raw_property) {
  261. GDMonoProperty *property = memnew(GDMonoProperty(raw_property, this));
  262. properties.insert(p_name, property);
  263. return property;
  264. }
  265. return NULL;
  266. }
  267. const Vector<GDMonoProperty *> &GDMonoClass::get_all_properties() {
  268. if (properties_fetched)
  269. return properties_list;
  270. void *iter = NULL;
  271. MonoProperty *raw_property = NULL;
  272. while ((raw_property = mono_class_get_properties(mono_class, &iter)) != NULL) {
  273. StringName name = mono_property_get_name(raw_property);
  274. Map<StringName, GDMonoProperty *>::Element *match = properties.find(name);
  275. if (match) {
  276. properties_list.push_back(match->get());
  277. } else {
  278. GDMonoProperty *property = memnew(GDMonoProperty(raw_property, this));
  279. properties.insert(name, property);
  280. properties_list.push_back(property);
  281. }
  282. }
  283. properties_fetched = true;
  284. return properties_list;
  285. }
  286. const Vector<GDMonoClass *> &GDMonoClass::get_all_delegates() {
  287. if (delegates_fetched)
  288. return delegates_list;
  289. void *iter = NULL;
  290. MonoClass *raw_class = NULL;
  291. while ((raw_class = mono_class_get_nested_types(mono_class, &iter)) != NULL) {
  292. if (mono_class_is_delegate(raw_class)) {
  293. StringName name = mono_class_get_name(raw_class);
  294. Map<StringName, GDMonoClass *>::Element *match = delegates.find(name);
  295. if (match) {
  296. delegates_list.push_back(match->get());
  297. } else {
  298. GDMonoClass *delegate = memnew(GDMonoClass(mono_class_get_namespace(raw_class), mono_class_get_name(raw_class), raw_class, assembly));
  299. delegates.insert(name, delegate);
  300. delegates_list.push_back(delegate);
  301. }
  302. }
  303. }
  304. delegates_fetched = true;
  305. return delegates_list;
  306. }
  307. GDMonoClass::GDMonoClass(const StringName &p_namespace, const StringName &p_name, MonoClass *p_class, GDMonoAssembly *p_assembly) {
  308. namespace_name = p_namespace;
  309. class_name = p_name;
  310. mono_class = p_class;
  311. assembly = p_assembly;
  312. attrs_fetched = false;
  313. attributes = NULL;
  314. methods_fetched = false;
  315. fields_fetched = false;
  316. properties_fetched = false;
  317. delegates_fetched = false;
  318. }
  319. GDMonoClass::~GDMonoClass() {
  320. if (attributes) {
  321. mono_custom_attrs_free(attributes);
  322. }
  323. for (Map<StringName, GDMonoField *>::Element *E = fields.front(); E; E = E->next()) {
  324. memdelete(E->value());
  325. }
  326. for (Map<StringName, GDMonoProperty *>::Element *E = properties.front(); E; E = E->next()) {
  327. memdelete(E->value());
  328. }
  329. {
  330. // Ugly workaround...
  331. // We may have duplicated values, because we redirect snake_case methods to PascalCasel (only Godot API methods).
  332. // This way, we end with both the snake_case name and the PascalCasel name paired with the same method.
  333. // Therefore, we must avoid deleting the same pointer twice.
  334. int offset = 0;
  335. Vector<GDMonoMethod *> deleted_methods;
  336. deleted_methods.resize(methods.size());
  337. const MethodKey *k = NULL;
  338. while ((k = methods.next(k))) {
  339. GDMonoMethod *method = methods.get(*k);
  340. if (method) {
  341. for (int i = 0; i < offset; i++) {
  342. if (deleted_methods[i] == method) {
  343. // Already deleted
  344. goto already_deleted;
  345. }
  346. }
  347. deleted_methods[offset] = method;
  348. ++offset;
  349. memdelete(method);
  350. }
  351. already_deleted:;
  352. }
  353. methods.clear();
  354. }
  355. }