gd_mono_class.h 5.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157
  1. /*************************************************************************/
  2. /* gd_mono_class.h */
  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. #ifndef GD_MONO_CLASS_H
  31. #define GD_MONO_CLASS_H
  32. #include <mono/metadata/debug-helpers.h>
  33. #include "core/map.h"
  34. #include "core/ustring.h"
  35. #include "gd_mono_field.h"
  36. #include "gd_mono_header.h"
  37. #include "gd_mono_method.h"
  38. #include "gd_mono_property.h"
  39. #include "gd_mono_utils.h"
  40. class GDMonoClass {
  41. struct MethodKey {
  42. struct Hasher {
  43. static _FORCE_INLINE_ uint32_t hash(const MethodKey &p_key) {
  44. uint32_t hash = 0;
  45. GDMonoUtils::hash_combine(hash, p_key.name.hash());
  46. GDMonoUtils::hash_combine(hash, HashMapHasherDefault::hash(p_key.params_count));
  47. return hash;
  48. }
  49. };
  50. _FORCE_INLINE_ bool operator==(const MethodKey &p_a) const {
  51. return p_a.params_count == params_count && p_a.name == name;
  52. }
  53. MethodKey() {}
  54. MethodKey(const StringName &p_name, int p_params_count) {
  55. name = p_name;
  56. params_count = p_params_count;
  57. }
  58. StringName name;
  59. int params_count;
  60. };
  61. StringName namespace_name;
  62. StringName class_name;
  63. MonoClass *mono_class;
  64. GDMonoAssembly *assembly;
  65. bool attrs_fetched;
  66. MonoCustomAttrInfo *attributes;
  67. // This contains both the original method names and remapped method names from the native Godot identifiers to the C# functions.
  68. // Most method-related functions refer to this and it's possible this is unintuitive for outside users; this may be a prime location for refactoring or renaming.
  69. bool methods_fetched;
  70. HashMap<MethodKey, GDMonoMethod *, MethodKey::Hasher> methods;
  71. bool method_list_fetched;
  72. Vector<GDMonoMethod *> method_list;
  73. bool fields_fetched;
  74. Map<StringName, GDMonoField *> fields;
  75. Vector<GDMonoField *> fields_list;
  76. bool properties_fetched;
  77. Map<StringName, GDMonoProperty *> properties;
  78. Vector<GDMonoProperty *> properties_list;
  79. bool delegates_fetched;
  80. Map<StringName, GDMonoClass *> delegates;
  81. Vector<GDMonoClass *> delegates_list;
  82. friend class GDMonoAssembly;
  83. GDMonoClass(const StringName &p_namespace, const StringName &p_name, MonoClass *p_class, GDMonoAssembly *p_assembly);
  84. public:
  85. static String get_full_name(MonoClass *p_mono_class);
  86. static MonoType *get_mono_type(MonoClass *p_mono_class);
  87. String get_full_name() const;
  88. MonoType *get_mono_type();
  89. bool is_assignable_from(GDMonoClass *p_from) const;
  90. _FORCE_INLINE_ StringName get_namespace() const { return namespace_name; }
  91. _FORCE_INLINE_ StringName get_name() const { return class_name; }
  92. _FORCE_INLINE_ MonoClass *get_mono_ptr() const { return mono_class; }
  93. _FORCE_INLINE_ const GDMonoAssembly *get_assembly() const { return assembly; }
  94. GDMonoClass *get_parent_class();
  95. #ifdef TOOLS_ENABLED
  96. Vector<MonoClassField *> get_enum_fields();
  97. #endif
  98. GDMonoMethod *get_fetched_method_unknown_params(const StringName &p_name);
  99. bool has_fetched_method_unknown_params(const StringName &p_name);
  100. bool has_attribute(GDMonoClass *p_attr_class);
  101. MonoObject *get_attribute(GDMonoClass *p_attr_class);
  102. void fetch_attributes();
  103. void fetch_methods_with_godot_api_checks(GDMonoClass *p_native_base);
  104. GDMonoMethod *get_method(const StringName &p_name, int p_params_count = 0);
  105. GDMonoMethod *get_method(MonoMethod *p_raw_method);
  106. GDMonoMethod *get_method(MonoMethod *p_raw_method, const StringName &p_name);
  107. GDMonoMethod *get_method(MonoMethod *p_raw_method, const StringName &p_name, int p_params_count);
  108. GDMonoMethod *get_method_with_desc(const String &p_description, bool p_include_namespace);
  109. void *get_method_thunk(const StringName &p_name, int p_params_count = 0);
  110. GDMonoField *get_field(const StringName &p_name);
  111. const Vector<GDMonoField *> &get_all_fields();
  112. GDMonoProperty *get_property(const StringName &p_name);
  113. const Vector<GDMonoProperty *> &get_all_properties();
  114. const Vector<GDMonoClass *> &get_all_delegates();
  115. const Vector<GDMonoMethod *> &get_all_methods();
  116. ~GDMonoClass();
  117. };
  118. #endif // GD_MONO_CLASS_H