gd_mono.h 9.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303
  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. #ifdef TOOLS_ENABLED
  83. bool editor_api_assembly_out_of_sync;
  84. #endif
  85. GDMonoAssembly *corlib_assembly;
  86. GDMonoAssembly *core_api_assembly;
  87. GDMonoAssembly *project_assembly;
  88. #ifdef TOOLS_ENABLED
  89. GDMonoAssembly *editor_api_assembly;
  90. GDMonoAssembly *editor_tools_assembly;
  91. #endif
  92. HashMap<uint32_t, HashMap<String, GDMonoAssembly *> > assemblies;
  93. void _domain_assemblies_cleanup(uint32_t p_domain_id);
  94. bool _load_corlib_assembly();
  95. bool _load_core_api_assembly();
  96. #ifdef TOOLS_ENABLED
  97. bool _load_editor_api_assembly();
  98. bool _load_editor_tools_assembly();
  99. #endif
  100. bool _load_project_assembly();
  101. bool _load_api_assemblies();
  102. #ifdef TOOLS_ENABLED
  103. String _get_api_assembly_metadata_path();
  104. #endif
  105. void _register_internal_calls();
  106. Error _load_scripts_domain();
  107. Error _unload_scripts_domain();
  108. #ifdef TOOLS_ENABLED
  109. Error _load_tools_domain();
  110. #endif
  111. uint64_t api_core_hash;
  112. #ifdef TOOLS_ENABLED
  113. uint64_t api_editor_hash;
  114. #endif
  115. void _initialize_and_check_api_hashes();
  116. GDMonoLog *gdmono_log;
  117. #if defined(WINDOWS_ENABLED) && defined(TOOLS_ENABLED)
  118. MonoRegInfo mono_reg_info;
  119. #endif
  120. void add_mono_shared_libs_dir_to_path();
  121. protected:
  122. static GDMono *singleton;
  123. public:
  124. uint64_t get_api_core_hash() {
  125. if (api_core_hash == 0)
  126. api_core_hash = ClassDB::get_api_hash(ClassDB::API_CORE);
  127. return api_core_hash;
  128. }
  129. #ifdef TOOLS_ENABLED
  130. uint64_t get_api_editor_hash() {
  131. if (api_editor_hash == 0)
  132. api_editor_hash = ClassDB::get_api_hash(ClassDB::API_EDITOR);
  133. return api_editor_hash;
  134. }
  135. #endif
  136. #ifdef TOOLS_ENABLED
  137. void metadata_set_api_assembly_invalidated(APIAssembly::Type p_api_type, bool p_invalidated);
  138. bool metadata_is_api_assembly_invalidated(APIAssembly::Type p_api_type);
  139. #endif
  140. static GDMono *get_singleton() { return singleton; }
  141. static void unhandled_exception_hook(MonoObject *p_exc, void *p_user_data);
  142. // Do not use these, unless you know what you're doing
  143. void add_assembly(uint32_t p_domain_id, GDMonoAssembly *p_assembly);
  144. GDMonoAssembly **get_loaded_assembly(const String &p_name);
  145. _FORCE_INLINE_ bool is_runtime_initialized() const { return runtime_initialized && !mono_runtime_is_shutting_down() /* stays true after shutdown finished */; }
  146. _FORCE_INLINE_ bool is_finalizing_scripts_domain() { return finalizing_scripts_domain; }
  147. _FORCE_INLINE_ MonoDomain *get_scripts_domain() { return scripts_domain; }
  148. #ifdef TOOLS_ENABLED
  149. _FORCE_INLINE_ MonoDomain *get_tools_domain() { return tools_domain; }
  150. #endif
  151. _FORCE_INLINE_ GDMonoAssembly *get_corlib_assembly() const { return corlib_assembly; }
  152. _FORCE_INLINE_ GDMonoAssembly *get_core_api_assembly() const { return core_api_assembly; }
  153. _FORCE_INLINE_ GDMonoAssembly *get_project_assembly() const { return project_assembly; }
  154. #ifdef TOOLS_ENABLED
  155. _FORCE_INLINE_ GDMonoAssembly *get_editor_api_assembly() const { return editor_api_assembly; }
  156. _FORCE_INLINE_ GDMonoAssembly *get_editor_tools_assembly() const { return editor_tools_assembly; }
  157. #endif
  158. #if defined(WINDOWS_ENABLED) && defined(TOOLS_ENABLED)
  159. const MonoRegInfo &get_mono_reg_info() { return mono_reg_info; }
  160. #endif
  161. GDMonoClass *get_class(MonoClass *p_raw_class);
  162. #ifdef GD_MONO_HOT_RELOAD
  163. Error reload_scripts_domain();
  164. #endif
  165. bool load_assembly(const String &p_name, GDMonoAssembly **r_assembly, bool p_refonly = false);
  166. bool load_assembly(const String &p_name, MonoAssemblyName *p_aname, GDMonoAssembly **r_assembly, bool p_refonly = false);
  167. bool load_assembly_from(const String &p_name, const String &p_path, GDMonoAssembly **r_assembly, bool p_refonly = false);
  168. Error finalize_and_unload_domain(MonoDomain *p_domain);
  169. void initialize();
  170. GDMono();
  171. ~GDMono();
  172. };
  173. namespace gdmono {
  174. class ScopeDomain {
  175. MonoDomain *prev_domain;
  176. public:
  177. ScopeDomain(MonoDomain *p_domain) {
  178. MonoDomain *prev_domain = mono_domain_get();
  179. if (prev_domain != p_domain) {
  180. this->prev_domain = prev_domain;
  181. mono_domain_set(p_domain, false);
  182. } else {
  183. this->prev_domain = NULL;
  184. }
  185. }
  186. ~ScopeDomain() {
  187. if (prev_domain)
  188. mono_domain_set(prev_domain, false);
  189. }
  190. };
  191. class ScopeExitDomainUnload {
  192. MonoDomain *domain;
  193. public:
  194. ScopeExitDomainUnload(MonoDomain *p_domain) :
  195. domain(p_domain) {
  196. }
  197. ~ScopeExitDomainUnload() {
  198. if (domain)
  199. GDMono::get_singleton()->finalize_and_unload_domain(domain);
  200. }
  201. };
  202. } // namespace gdmono
  203. #define _GDMONO_SCOPE_DOMAIN_(m_mono_domain) \
  204. gdmono::ScopeDomain __gdmono__scope__domain__(m_mono_domain); \
  205. (void)__gdmono__scope__domain__;
  206. #define _GDMONO_SCOPE_EXIT_DOMAIN_UNLOAD_(m_mono_domain) \
  207. gdmono::ScopeExitDomainUnload __gdmono__scope__exit__domain__unload__(m_mono_domain); \
  208. (void)__gdmono__scope__exit__domain__unload__;
  209. class _GodotSharp : public Object {
  210. GDCLASS(_GodotSharp, Object)
  211. friend class GDMono;
  212. bool _is_domain_finalizing_for_unload(int32_t p_domain_id);
  213. List<NodePath *> np_delete_queue;
  214. List<RID *> rid_delete_queue;
  215. protected:
  216. static _GodotSharp *singleton;
  217. static void _bind_methods();
  218. public:
  219. static _GodotSharp *get_singleton() { return singleton; }
  220. void attach_thread();
  221. void detach_thread();
  222. int32_t get_domain_id();
  223. int32_t get_scripts_domain_id();
  224. bool is_scripts_domain_loaded();
  225. bool is_domain_finalizing_for_unload();
  226. bool is_domain_finalizing_for_unload(int32_t p_domain_id);
  227. bool is_domain_finalizing_for_unload(MonoDomain *p_domain);
  228. bool is_runtime_shutting_down();
  229. bool is_runtime_initialized();
  230. _GodotSharp();
  231. ~_GodotSharp();
  232. };
  233. #endif // GD_MONO_H