gd_mono_utils.h 7.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186
  1. /*************************************************************************/
  2. /* gd_mono_utils.h */
  3. /*************************************************************************/
  4. /* This file is part of: */
  5. /* GODOT ENGINE */
  6. /* https://godotengine.org */
  7. /*************************************************************************/
  8. /* Copyright (c) 2007-2017 Juan Linietsky, Ariel Manzur. */
  9. /* Copyright (c) 2014-2017 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_MONOUTILS_H
  31. #define GD_MONOUTILS_H
  32. #include <mono/metadata/threads.h>
  33. #include "../mono_gc_handle.h"
  34. #include "gd_mono_header.h"
  35. #include "object.h"
  36. #include "reference.h"
  37. namespace GDMonoUtils {
  38. typedef MonoObject *(*MarshalUtils_DictToArrays)(MonoObject *, MonoArray **, MonoArray **, MonoObject **);
  39. typedef MonoObject *(*MarshalUtils_ArraysToDict)(MonoArray *, MonoArray *, MonoObject **);
  40. typedef MonoObject *(*SignalAwaiter_SignalCallback)(MonoObject *, MonoArray **, MonoObject **);
  41. typedef MonoObject *(*SignalAwaiter_FailureCallback)(MonoObject *, MonoObject **);
  42. typedef MonoObject *(*GodotTaskScheduler_Activate)(MonoObject *, MonoObject **);
  43. struct MonoCache {
  44. // Format for cached classes in the Godot namespace: class_<Class>
  45. // Macro: CACHED_CLASS(<Class>)
  46. // Format for cached classes in a different namespace: class_<Namespace>_<Class>
  47. // Macro: CACHED_NS_CLASS(<Namespace>, <Class>)
  48. // -----------------------------------------------
  49. // corlib classes
  50. // Let's use the no-namespace format for these too
  51. GDMonoClass *class_MonoObject;
  52. GDMonoClass *class_bool;
  53. GDMonoClass *class_int8_t;
  54. GDMonoClass *class_int16_t;
  55. GDMonoClass *class_int32_t;
  56. GDMonoClass *class_int64_t;
  57. GDMonoClass *class_uint8_t;
  58. GDMonoClass *class_uint16_t;
  59. GDMonoClass *class_uint32_t;
  60. GDMonoClass *class_uint64_t;
  61. GDMonoClass *class_float;
  62. GDMonoClass *class_double;
  63. GDMonoClass *class_String;
  64. GDMonoClass *class_IntPtr;
  65. MonoClass *rawclass_Dictionary;
  66. // -----------------------------------------------
  67. GDMonoClass *class_Vector2;
  68. GDMonoClass *class_Rect2;
  69. GDMonoClass *class_Transform2D;
  70. GDMonoClass *class_Vector3;
  71. GDMonoClass *class_Basis;
  72. GDMonoClass *class_Quat;
  73. GDMonoClass *class_Transform;
  74. GDMonoClass *class_AABB;
  75. GDMonoClass *class_Color;
  76. GDMonoClass *class_Plane;
  77. GDMonoClass *class_NodePath;
  78. GDMonoClass *class_RID;
  79. GDMonoClass *class_GodotObject;
  80. GDMonoClass *class_GodotReference;
  81. GDMonoClass *class_Node;
  82. GDMonoClass *class_Control;
  83. GDMonoClass *class_Spatial;
  84. GDMonoClass *class_WeakRef;
  85. GDMonoClass *class_MarshalUtils;
  86. GDMonoClass *class_ExportAttribute;
  87. GDMonoField *field_ExportAttribute_hint;
  88. GDMonoField *field_ExportAttribute_hint_string;
  89. GDMonoClass *class_ToolAttribute;
  90. GDMonoClass *class_RemoteAttribute;
  91. GDMonoClass *class_SyncAttribute;
  92. GDMonoClass *class_MasterAttribute;
  93. GDMonoClass *class_SlaveAttribute;
  94. GDMonoClass *class_GodotMethodAttribute;
  95. GDMonoField *field_GodotMethodAttribute_methodName;
  96. GDMonoField *field_GodotObject_ptr;
  97. GDMonoField *field_NodePath_ptr;
  98. GDMonoField *field_Image_ptr;
  99. GDMonoField *field_RID_ptr;
  100. MarshalUtils_DictToArrays methodthunk_MarshalUtils_DictionaryToArrays;
  101. MarshalUtils_ArraysToDict methodthunk_MarshalUtils_ArraysToDictionary;
  102. SignalAwaiter_SignalCallback methodthunk_SignalAwaiter_SignalCallback;
  103. SignalAwaiter_FailureCallback methodthunk_SignalAwaiter_FailureCallback;
  104. GodotTaskScheduler_Activate methodthunk_GodotTaskScheduler_Activate;
  105. Ref<MonoGCHandle> task_scheduler_handle;
  106. void clear_members();
  107. void cleanup() {}
  108. MonoCache() {
  109. clear_members();
  110. }
  111. };
  112. extern MonoCache mono_cache;
  113. void update_corlib_cache();
  114. void update_godot_api_cache();
  115. void clear_cache();
  116. _FORCE_INLINE_ void hash_combine(uint32_t &p_hash, const uint32_t &p_with_hash) {
  117. p_hash ^= p_with_hash + 0x9e3779b9 + (p_hash << 6) + (p_hash >> 2);
  118. }
  119. /**
  120. * If the object has a csharp script, returns the target of the gchandle stored in the script instance
  121. * Otherwise returns a newly constructed MonoObject* which is attached to the object
  122. * Returns NULL on error
  123. */
  124. MonoObject *unmanaged_get_managed(Object *unmanaged);
  125. void set_main_thread(MonoThread *p_thread);
  126. void attach_current_thread();
  127. void detach_current_thread();
  128. MonoThread *get_current_thread();
  129. _FORCE_INLINE_ bool is_main_thread() {
  130. return mono_domain_get() != NULL && mono_thread_get_main() == mono_thread_current();
  131. }
  132. GDMonoClass *get_object_class(MonoObject *p_object);
  133. GDMonoClass *type_get_proxy_class(const StringName &p_type);
  134. GDMonoClass *get_class_native_base(GDMonoClass *p_class);
  135. MonoObject *create_managed_for_godot_object(GDMonoClass *p_class, const StringName &p_native, Object *p_object);
  136. MonoObject *create_managed_from(const NodePath &p_from);
  137. MonoObject *create_managed_from(const RID &p_from);
  138. MonoDomain *create_domain(const String &p_friendly_name);
  139. String get_exception_name_and_message(MonoObject *p_ex);
  140. } // namespace GDMonoUtils
  141. #define NATIVE_GDMONOCLASS_NAME(m_class) (GDMonoMarshal::mono_string_to_godot((MonoString *)m_class->get_field(BINDINGS_NATIVE_NAME_FIELD)->get_value(NULL)))
  142. #define CACHED_CLASS(m_class) (GDMonoUtils::mono_cache.class_##m_class)
  143. #define CACHED_CLASS_RAW(m_class) (GDMonoUtils::mono_cache.class_##m_class->get_raw())
  144. #define CACHED_NS_CLASS(m_ns, m_class) (GDMonoUtils::mono_cache.class_##m_ns##_##m_class)
  145. #define CACHED_RAW_MONO_CLASS(m_class) (GDMonoUtils::mono_cache.rawclass_##m_class)
  146. #define CACHED_FIELD(m_class, m_field) (GDMonoUtils::mono_cache.field_##m_class##_##m_field)
  147. #define CACHED_METHOD_THUNK(m_class, m_method) (GDMonoUtils::mono_cache.methodthunk_##m_class##_##m_method)
  148. #ifdef REAL_T_IS_DOUBLE
  149. #define REAL_T_MONOCLASS CACHED_CLASS_RAW(double)
  150. #else
  151. #define REAL_T_MONOCLASS CACHED_CLASS_RAW(float)
  152. #endif
  153. #endif // GD_MONOUTILS_H