gd_mono_class.h 6.0 KB

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