gd_mono_class.cpp 16 KB

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