gd_mono.h 10 KB

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