gd_mono.h 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340
  1. /*************************************************************************/
  2. /* gd_mono.h */
  3. /*************************************************************************/
  4. /* This file is part of: */
  5. /* GODOT ENGINE */
  6. /* https://godotengine.org */
  7. /*************************************************************************/
  8. /* Copyright (c) 2007-2020 Juan Linietsky, Ariel Manzur. */
  9. /* Copyright (c) 2014-2020 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 ApiAssemblyInfo {
  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 ApiAssemblyInfo
  69. class GDMono {
  70. public:
  71. enum UnhandledExceptionPolicy {
  72. POLICY_TERMINATE_APP,
  73. POLICY_LOG_ERROR
  74. };
  75. struct LoadedApiAssembly {
  76. GDMonoAssembly *assembly;
  77. bool out_of_sync;
  78. LoadedApiAssembly() :
  79. assembly(nullptr),
  80. out_of_sync(false) {
  81. }
  82. };
  83. private:
  84. bool runtime_initialized;
  85. bool finalizing_scripts_domain;
  86. UnhandledExceptionPolicy unhandled_exception_policy;
  87. MonoDomain *root_domain;
  88. MonoDomain *scripts_domain;
  89. HashMap<uint32_t, HashMap<String, GDMonoAssembly *>> assemblies;
  90. GDMonoAssembly *corlib_assembly;
  91. GDMonoAssembly *project_assembly;
  92. #ifdef TOOLS_ENABLED
  93. GDMonoAssembly *tools_assembly;
  94. GDMonoAssembly *tools_project_editor_assembly;
  95. #endif
  96. LoadedApiAssembly core_api_assembly;
  97. LoadedApiAssembly editor_api_assembly;
  98. typedef bool (*CoreApiAssemblyLoadedCallback)();
  99. bool _are_api_assemblies_out_of_sync();
  100. bool _temp_domain_load_are_assemblies_out_of_sync(const String &p_config);
  101. bool _load_core_api_assembly(LoadedApiAssembly &r_loaded_api_assembly, const String &p_config, bool p_refonly);
  102. #ifdef TOOLS_ENABLED
  103. bool _load_editor_api_assembly(LoadedApiAssembly &r_loaded_api_assembly, const String &p_config, bool p_refonly);
  104. #endif
  105. static bool _on_core_api_assembly_loaded();
  106. bool _load_corlib_assembly();
  107. #ifdef TOOLS_ENABLED
  108. bool _load_tools_assemblies();
  109. #endif
  110. bool _load_project_assembly();
  111. bool _try_load_api_assemblies(LoadedApiAssembly &r_core_api_assembly, LoadedApiAssembly &r_editor_api_assembly,
  112. const String &p_config, bool p_refonly, CoreApiAssemblyLoadedCallback p_callback);
  113. bool _try_load_api_assemblies_preset();
  114. void _load_api_assemblies();
  115. void _install_trace_listener();
  116. void _register_internal_calls();
  117. #ifndef GD_MONO_SINGLE_APPDOMAIN
  118. Error _load_scripts_domain();
  119. Error _unload_scripts_domain();
  120. #endif
  121. void _domain_assemblies_cleanup(uint32_t p_domain_id);
  122. uint64_t api_core_hash;
  123. #ifdef TOOLS_ENABLED
  124. uint64_t api_editor_hash;
  125. #endif
  126. void _init_godot_api_hashes();
  127. void _init_exception_policy();
  128. GDMonoLog *gdmono_log;
  129. #if defined(WINDOWS_ENABLED) && defined(TOOLS_ENABLED)
  130. MonoRegInfo mono_reg_info;
  131. #endif
  132. void add_mono_shared_libs_dir_to_path();
  133. void determine_mono_dirs(String &r_assembly_rootdir, String &r_config_dir);
  134. protected:
  135. static GDMono *singleton;
  136. public:
  137. #ifdef DEBUG_METHODS_ENABLED
  138. uint64_t get_api_core_hash() {
  139. if (api_core_hash == 0)
  140. api_core_hash = ClassDB::get_api_hash(ClassDB::API_CORE);
  141. return api_core_hash;
  142. }
  143. #ifdef TOOLS_ENABLED
  144. uint64_t get_api_editor_hash() {
  145. if (api_editor_hash == 0)
  146. api_editor_hash = ClassDB::get_api_hash(ClassDB::API_EDITOR);
  147. return api_editor_hash;
  148. }
  149. #endif // TOOLS_ENABLED
  150. #endif // DEBUG_METHODS_ENABLED
  151. _FORCE_INLINE_ static String get_expected_api_build_config() {
  152. #ifdef TOOLS_ENABLED
  153. return "Debug";
  154. #else
  155. #ifdef DEBUG_ENABLED
  156. return "Debug";
  157. #else
  158. return "Release";
  159. #endif
  160. #endif
  161. }
  162. #ifdef TOOLS_ENABLED
  163. bool copy_prebuilt_api_assembly(ApiAssemblyInfo::Type p_api_type, const String &p_config);
  164. String update_api_assemblies_from_prebuilt(const String &p_config, const bool *p_core_api_out_of_sync = nullptr, const bool *p_editor_api_out_of_sync = nullptr);
  165. #endif
  166. static GDMono *get_singleton() { return singleton; }
  167. [[noreturn]] static void unhandled_exception_hook(MonoObject *p_exc, void *p_user_data);
  168. UnhandledExceptionPolicy get_unhandled_exception_policy() const { return unhandled_exception_policy; }
  169. // Do not use these, unless you know what you're doing
  170. void add_assembly(uint32_t p_domain_id, GDMonoAssembly *p_assembly);
  171. GDMonoAssembly *get_loaded_assembly(const String &p_name);
  172. _FORCE_INLINE_ bool is_runtime_initialized() const { return runtime_initialized && !mono_runtime_is_shutting_down() /* stays true after shutdown finished */; }
  173. _FORCE_INLINE_ bool is_finalizing_scripts_domain() { return finalizing_scripts_domain; }
  174. _FORCE_INLINE_ MonoDomain *get_scripts_domain() { return scripts_domain; }
  175. _FORCE_INLINE_ GDMonoAssembly *get_corlib_assembly() const { return corlib_assembly; }
  176. _FORCE_INLINE_ GDMonoAssembly *get_core_api_assembly() const { return core_api_assembly.assembly; }
  177. _FORCE_INLINE_ GDMonoAssembly *get_project_assembly() const { return project_assembly; }
  178. #ifdef TOOLS_ENABLED
  179. _FORCE_INLINE_ GDMonoAssembly *get_editor_api_assembly() const { return editor_api_assembly.assembly; }
  180. _FORCE_INLINE_ GDMonoAssembly *get_tools_assembly() const { return tools_assembly; }
  181. _FORCE_INLINE_ GDMonoAssembly *get_tools_project_editor_assembly() const { return tools_project_editor_assembly; }
  182. #endif
  183. #if defined(WINDOWS_ENABLED) && defined(TOOLS_ENABLED)
  184. const MonoRegInfo &get_mono_reg_info() { return mono_reg_info; }
  185. #endif
  186. GDMonoClass *get_class(MonoClass *p_raw_class);
  187. GDMonoClass *get_class(const StringName &p_namespace, const StringName &p_name);
  188. #ifdef GD_MONO_HOT_RELOAD
  189. Error reload_scripts_domain();
  190. #endif
  191. bool load_assembly(const String &p_name, GDMonoAssembly **r_assembly, bool p_refonly = false);
  192. bool load_assembly(const String &p_name, MonoAssemblyName *p_aname, GDMonoAssembly **r_assembly, bool p_refonly = false);
  193. bool load_assembly_from(const String &p_name, const String &p_path, GDMonoAssembly **r_assembly, bool p_refonly = false);
  194. Error finalize_and_unload_domain(MonoDomain *p_domain);
  195. void initialize();
  196. void initialize_load_assemblies();
  197. GDMono();
  198. ~GDMono();
  199. };
  200. namespace gdmono {
  201. class ScopeDomain {
  202. MonoDomain *prev_domain;
  203. public:
  204. ScopeDomain(MonoDomain *p_domain) {
  205. MonoDomain *prev_domain = mono_domain_get();
  206. if (prev_domain != p_domain) {
  207. this->prev_domain = prev_domain;
  208. mono_domain_set(p_domain, false);
  209. } else {
  210. this->prev_domain = nullptr;
  211. }
  212. }
  213. ~ScopeDomain() {
  214. if (prev_domain)
  215. mono_domain_set(prev_domain, false);
  216. }
  217. };
  218. class ScopeExitDomainUnload {
  219. MonoDomain *domain;
  220. public:
  221. ScopeExitDomainUnload(MonoDomain *p_domain) :
  222. domain(p_domain) {
  223. }
  224. ~ScopeExitDomainUnload() {
  225. if (domain)
  226. GDMono::get_singleton()->finalize_and_unload_domain(domain);
  227. }
  228. };
  229. } // namespace gdmono
  230. #define _GDMONO_SCOPE_DOMAIN_(m_mono_domain) \
  231. gdmono::ScopeDomain __gdmono__scope__domain__(m_mono_domain); \
  232. (void)__gdmono__scope__domain__;
  233. #define _GDMONO_SCOPE_EXIT_DOMAIN_UNLOAD_(m_mono_domain) \
  234. gdmono::ScopeExitDomainUnload __gdmono__scope__exit__domain__unload__(m_mono_domain); \
  235. (void)__gdmono__scope__exit__domain__unload__;
  236. class _GodotSharp : public Object {
  237. GDCLASS(_GodotSharp, Object);
  238. friend class GDMono;
  239. bool _is_domain_finalizing_for_unload(int32_t p_domain_id);
  240. List<NodePath *> np_delete_queue;
  241. List<RID *> rid_delete_queue;
  242. void _reload_assemblies(bool p_soft_reload);
  243. protected:
  244. static _GodotSharp *singleton;
  245. static void _bind_methods();
  246. public:
  247. static _GodotSharp *get_singleton() { return singleton; }
  248. void attach_thread();
  249. void detach_thread();
  250. int32_t get_domain_id();
  251. int32_t get_scripts_domain_id();
  252. bool is_scripts_domain_loaded();
  253. bool is_domain_finalizing_for_unload();
  254. bool is_domain_finalizing_for_unload(int32_t p_domain_id);
  255. bool is_domain_finalizing_for_unload(MonoDomain *p_domain);
  256. bool is_runtime_shutting_down();
  257. bool is_runtime_initialized();
  258. _GodotSharp();
  259. ~_GodotSharp();
  260. };
  261. #endif // GD_MONO_H