gd_mono.h 8.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302
  1. /*************************************************************************/
  2. /* gd_mono.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_H
  31. #define GD_MONO_H
  32. #include "core/io/config_file.h"
  33. #include "../godotsharp_defs.h"
  34. #include "gd_mono_assembly.h"
  35. #include "gd_mono_log.h"
  36. #ifdef WINDOWS_ENABLED
  37. #include "../utils/mono_reg_utils.h"
  38. #endif
  39. namespace APIAssembly {
  40. enum Type {
  41. API_CORE,
  42. API_EDITOR
  43. };
  44. struct Version {
  45. uint64_t godot_api_hash;
  46. uint32_t bindings_version;
  47. uint32_t cs_glue_version;
  48. bool operator==(const Version &p_other) const {
  49. return godot_api_hash == p_other.godot_api_hash &&
  50. bindings_version == p_other.bindings_version &&
  51. cs_glue_version == p_other.cs_glue_version;
  52. }
  53. Version() :
  54. godot_api_hash(0),
  55. bindings_version(0),
  56. cs_glue_version(0) {
  57. }
  58. Version(uint64_t p_godot_api_hash,
  59. uint32_t p_bindings_version,
  60. uint32_t p_cs_glue_version) :
  61. godot_api_hash(p_godot_api_hash),
  62. bindings_version(p_bindings_version),
  63. cs_glue_version(p_cs_glue_version) {
  64. }
  65. static Version get_from_loaded_assembly(GDMonoAssembly *p_api_assembly, Type p_api_type);
  66. };
  67. String to_string(Type p_type);
  68. } // namespace APIAssembly
  69. #define SCRIPTS_DOMAIN GDMono::get_singleton()->get_scripts_domain()
  70. #ifdef TOOLS_ENABLED
  71. #define TOOLS_DOMAIN GDMono::get_singleton()->get_tools_domain()
  72. #endif
  73. class GDMono {
  74. bool runtime_initialized;
  75. bool finalizing_scripts_domain;
  76. MonoDomain *root_domain;
  77. MonoDomain *scripts_domain;
  78. #ifdef TOOLS_ENABLED
  79. MonoDomain *tools_domain;
  80. #endif
  81. bool core_api_assembly_out_of_sync;
  82. bool editor_api_assembly_out_of_sync;
  83. GDMonoAssembly *corlib_assembly;
  84. GDMonoAssembly *core_api_assembly;
  85. GDMonoAssembly *project_assembly;
  86. #ifdef TOOLS_ENABLED
  87. GDMonoAssembly *editor_api_assembly;
  88. GDMonoAssembly *editor_tools_assembly;
  89. #endif
  90. HashMap<uint32_t, HashMap<String, GDMonoAssembly *> > assemblies;
  91. void _domain_assemblies_cleanup(uint32_t p_domain_id);
  92. bool _load_corlib_assembly();
  93. bool _load_core_api_assembly();
  94. #ifdef TOOLS_ENABLED
  95. bool _load_editor_api_assembly();
  96. bool _load_editor_tools_assembly();
  97. #endif
  98. bool _load_project_assembly();
  99. bool _load_api_assemblies();
  100. #ifdef TOOLS_ENABLED
  101. String _get_api_assembly_metadata_path();
  102. #endif
  103. void _register_internal_calls();
  104. Error _load_scripts_domain();
  105. Error _unload_scripts_domain();
  106. #ifdef TOOLS_ENABLED
  107. Error _load_tools_domain();
  108. #endif
  109. #ifdef DEBUG_METHODS_ENABLED
  110. uint64_t api_core_hash;
  111. #ifdef TOOLS_ENABLED
  112. uint64_t api_editor_hash;
  113. #endif
  114. void _initialize_and_check_api_hashes();
  115. #endif
  116. GDMonoLog *gdmono_log;
  117. #if defined(WINDOWS_ENABLED) && defined(TOOLS_ENABLED)
  118. MonoRegInfo mono_reg_info;
  119. #endif
  120. protected:
  121. static GDMono *singleton;
  122. public:
  123. #ifdef DEBUG_METHODS_ENABLED
  124. uint64_t get_api_core_hash() { return api_core_hash; }
  125. #ifdef TOOLS_ENABLED
  126. uint64_t get_api_editor_hash() { return api_editor_hash; }
  127. #endif
  128. #endif
  129. #ifdef TOOLS_ENABLED
  130. void metadata_set_api_assembly_invalidated(APIAssembly::Type p_api_type, bool p_invalidated);
  131. bool metadata_is_api_assembly_invalidated(APIAssembly::Type p_api_type);
  132. #endif
  133. static GDMono *get_singleton() { return singleton; }
  134. static void unhandled_exception_hook(MonoObject *p_exc, void *p_user_data);
  135. // Do not use these, unless you know what you're doing
  136. void add_assembly(uint32_t p_domain_id, GDMonoAssembly *p_assembly);
  137. GDMonoAssembly **get_loaded_assembly(const String &p_name);
  138. _FORCE_INLINE_ bool is_runtime_initialized() const { return runtime_initialized && !mono_runtime_is_shutting_down() /* stays true after shutdown finished */; }
  139. _FORCE_INLINE_ bool is_finalizing_scripts_domain() { return finalizing_scripts_domain; }
  140. _FORCE_INLINE_ MonoDomain *get_scripts_domain() { return scripts_domain; }
  141. #ifdef TOOLS_ENABLED
  142. _FORCE_INLINE_ MonoDomain *get_tools_domain() { return tools_domain; }
  143. #endif
  144. _FORCE_INLINE_ GDMonoAssembly *get_corlib_assembly() const { return corlib_assembly; }
  145. _FORCE_INLINE_ GDMonoAssembly *get_core_api_assembly() const { return core_api_assembly; }
  146. _FORCE_INLINE_ GDMonoAssembly *get_project_assembly() const { return project_assembly; }
  147. #ifdef TOOLS_ENABLED
  148. _FORCE_INLINE_ GDMonoAssembly *get_editor_api_assembly() const { return editor_api_assembly; }
  149. _FORCE_INLINE_ GDMonoAssembly *get_editor_tools_assembly() const { return editor_tools_assembly; }
  150. #endif
  151. #if defined(WINDOWS_ENABLED) && defined(TOOLS_ENABLED)
  152. const MonoRegInfo &get_mono_reg_info() { return mono_reg_info; }
  153. #endif
  154. GDMonoClass *get_class(MonoClass *p_raw_class);
  155. #ifdef TOOLS_ENABLED
  156. Error reload_scripts_domain();
  157. #endif
  158. bool load_assembly(const String &p_name, GDMonoAssembly **r_assembly, bool p_refonly = false);
  159. bool load_assembly(const String &p_name, MonoAssemblyName *p_aname, GDMonoAssembly **r_assembly, bool p_refonly = false);
  160. bool load_assembly_from(const String &p_name, const String &p_path, GDMonoAssembly **r_assembly, bool p_refonly = false);
  161. Error finalize_and_unload_domain(MonoDomain *p_domain);
  162. void initialize();
  163. GDMono();
  164. ~GDMono();
  165. };
  166. namespace gdmono {
  167. class ScopeDomain {
  168. MonoDomain *prev_domain;
  169. public:
  170. ScopeDomain(MonoDomain *p_domain) {
  171. MonoDomain *prev_domain = mono_domain_get();
  172. if (prev_domain != p_domain) {
  173. this->prev_domain = prev_domain;
  174. mono_domain_set(p_domain, false);
  175. } else {
  176. this->prev_domain = NULL;
  177. }
  178. }
  179. ~ScopeDomain() {
  180. if (prev_domain)
  181. mono_domain_set(prev_domain, false);
  182. }
  183. };
  184. class ScopeExitDomainUnload {
  185. MonoDomain *domain;
  186. public:
  187. ScopeExitDomainUnload(MonoDomain *p_domain) :
  188. domain(p_domain) {
  189. }
  190. ~ScopeExitDomainUnload() {
  191. if (domain)
  192. GDMono::get_singleton()->finalize_and_unload_domain(domain);
  193. }
  194. };
  195. } // namespace gdmono
  196. #define _GDMONO_SCOPE_DOMAIN_(m_mono_domain) \
  197. gdmono::ScopeDomain __gdmono__scope__domain__(m_mono_domain); \
  198. (void)__gdmono__scope__domain__;
  199. #define _GDMONO_SCOPE_EXIT_DOMAIN_UNLOAD_(m_mono_domain) \
  200. gdmono::ScopeExitDomainUnload __gdmono__scope__exit__domain__unload__(m_mono_domain); \
  201. (void)__gdmono__scope__exit__domain__unload__;
  202. class _GodotSharp : public Object {
  203. GDCLASS(_GodotSharp, Object)
  204. friend class GDMono;
  205. bool _is_domain_finalizing_for_unload(int32_t p_domain_id);
  206. List<NodePath *> np_delete_queue;
  207. List<RID *> rid_delete_queue;
  208. bool queue_empty;
  209. #ifndef NO_THREADS
  210. Mutex *queue_mutex;
  211. #endif
  212. protected:
  213. static _GodotSharp *singleton;
  214. static void _bind_methods();
  215. public:
  216. static _GodotSharp *get_singleton() { return singleton; }
  217. void attach_thread();
  218. void detach_thread();
  219. int32_t get_domain_id();
  220. int32_t get_scripts_domain_id();
  221. bool is_scripts_domain_loaded();
  222. bool is_domain_finalizing_for_unload();
  223. bool is_domain_finalizing_for_unload(int32_t p_domain_id);
  224. bool is_domain_finalizing_for_unload(MonoDomain *p_domain);
  225. bool is_runtime_shutting_down();
  226. bool is_runtime_initialized();
  227. _GodotSharp();
  228. ~_GodotSharp();
  229. };
  230. #endif // GD_MONO_H