2
0

gd_mono.cpp 20 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595
  1. /**************************************************************************/
  2. /* gd_mono.cpp */
  3. /**************************************************************************/
  4. /* This file is part of: */
  5. /* GODOT ENGINE */
  6. /* https://godotengine.org */
  7. /**************************************************************************/
  8. /* Copyright (c) 2014-present Godot Engine contributors (see AUTHORS.md). */
  9. /* Copyright (c) 2007-2014 Juan Linietsky, Ariel Manzur. */
  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. #include "gd_mono.h"
  31. #include "core/config/project_settings.h"
  32. #include "core/debugger/engine_debugger.h"
  33. #include "core/io/dir_access.h"
  34. #include "core/io/file_access.h"
  35. #include "core/os/os.h"
  36. #include "core/os/thread.h"
  37. #include "../csharp_script.h"
  38. #include "../glue/runtime_interop.h"
  39. #include "../godotsharp_dirs.h"
  40. #include "../utils/path_utils.h"
  41. #include "gd_mono_cache.h"
  42. #include "../thirdparty/coreclr_delegates.h"
  43. #include "../thirdparty/hostfxr.h"
  44. #ifdef TOOLS_ENABLED
  45. #include "../editor/hostfxr_resolver.h"
  46. #endif
  47. #ifdef UNIX_ENABLED
  48. #include <dlfcn.h>
  49. #endif
  50. // TODO mobile
  51. #if 0
  52. #ifdef ANDROID_ENABLED
  53. #include "support/android_support.h"
  54. #elif defined(IOS_ENABLED)
  55. #include "support/ios_support.h"
  56. #endif
  57. #endif
  58. GDMono *GDMono::singleton = nullptr;
  59. namespace {
  60. hostfxr_initialize_for_dotnet_command_line_fn hostfxr_initialize_for_dotnet_command_line = nullptr;
  61. hostfxr_initialize_for_runtime_config_fn hostfxr_initialize_for_runtime_config = nullptr;
  62. hostfxr_get_runtime_delegate_fn hostfxr_get_runtime_delegate = nullptr;
  63. hostfxr_close_fn hostfxr_close = nullptr;
  64. #ifdef _WIN32
  65. static_assert(sizeof(char_t) == sizeof(char16_t));
  66. using HostFxrCharString = Char16String;
  67. #define HOSTFXR_STR(m_str) L##m_str
  68. #else
  69. static_assert(sizeof(char_t) == sizeof(char));
  70. using HostFxrCharString = CharString;
  71. #define HOSTFXR_STR(m_str) m_str
  72. #endif
  73. HostFxrCharString str_to_hostfxr(const String &p_str) {
  74. #ifdef _WIN32
  75. return p_str.utf16();
  76. #else
  77. return p_str.utf8();
  78. #endif
  79. }
  80. const char_t *get_data(const HostFxrCharString &p_char_str) {
  81. return (const char_t *)p_char_str.get_data();
  82. }
  83. String find_hostfxr() {
  84. #ifdef TOOLS_ENABLED
  85. String dotnet_root;
  86. String fxr_path;
  87. if (godotsharp::hostfxr_resolver::try_get_path(dotnet_root, fxr_path)) {
  88. return fxr_path;
  89. }
  90. // hostfxr_resolver doesn't look for dotnet in `PATH`. If it fails, we try to find the dotnet
  91. // executable in `PATH` here and pass its location as `dotnet_root` to `get_hostfxr_path`.
  92. String dotnet_exe = path::find_executable("dotnet");
  93. if (!dotnet_exe.is_empty()) {
  94. // The file found in PATH may be a symlink
  95. dotnet_exe = path::abspath(path::realpath(dotnet_exe));
  96. // TODO:
  97. // Sometimes, the symlink may not point to the dotnet executable in the dotnet root.
  98. // That's the case with snaps. The snap install should have been found with the
  99. // previous `get_hostfxr_path`, but it would still be better to do this properly
  100. // and use something like `dotnet --list-sdks/runtimes` to find the actual location.
  101. // This way we could also check if the proper sdk or runtime is installed. This would
  102. // allow us to fail gracefully and show some helpful information in the editor.
  103. dotnet_root = dotnet_exe.get_base_dir();
  104. if (godotsharp::hostfxr_resolver::try_get_path_from_dotnet_root(dotnet_root, fxr_path)) {
  105. return fxr_path;
  106. }
  107. }
  108. ERR_PRINT(String() + ".NET: One of the dependent libraries is missing. " +
  109. "Typically when the `hostfxr`, `hostpolicy` or `coreclr` dynamic " +
  110. "libraries are not present in the expected locations.");
  111. return String();
  112. #else
  113. #if defined(WINDOWS_ENABLED)
  114. String probe_path = GodotSharpDirs::get_api_assemblies_dir()
  115. .path_join("hostfxr.dll");
  116. #elif defined(MACOS_ENABLED)
  117. String probe_path = GodotSharpDirs::get_api_assemblies_dir()
  118. .path_join("libhostfxr.dylib");
  119. #elif defined(UNIX_ENABLED)
  120. String probe_path = GodotSharpDirs::get_api_assemblies_dir()
  121. .path_join("libhostfxr.so");
  122. #else
  123. #error "Platform not supported (yet?)"
  124. #endif
  125. if (FileAccess::exists(probe_path)) {
  126. return probe_path;
  127. }
  128. return String();
  129. #endif
  130. }
  131. bool load_hostfxr(void *&r_hostfxr_dll_handle) {
  132. String hostfxr_path = find_hostfxr();
  133. if (hostfxr_path.is_empty()) {
  134. return false;
  135. }
  136. print_verbose("Found hostfxr: " + hostfxr_path);
  137. Error err = OS::get_singleton()->open_dynamic_library(hostfxr_path, r_hostfxr_dll_handle);
  138. if (err != OK) {
  139. return false;
  140. }
  141. void *lib = r_hostfxr_dll_handle;
  142. void *symbol = nullptr;
  143. err = OS::get_singleton()->get_dynamic_library_symbol_handle(lib, "hostfxr_initialize_for_dotnet_command_line", symbol);
  144. ERR_FAIL_COND_V(err != OK, false);
  145. hostfxr_initialize_for_dotnet_command_line = (hostfxr_initialize_for_dotnet_command_line_fn)symbol;
  146. err = OS::get_singleton()->get_dynamic_library_symbol_handle(lib, "hostfxr_initialize_for_runtime_config", symbol);
  147. ERR_FAIL_COND_V(err != OK, false);
  148. hostfxr_initialize_for_runtime_config = (hostfxr_initialize_for_runtime_config_fn)symbol;
  149. err = OS::get_singleton()->get_dynamic_library_symbol_handle(lib, "hostfxr_get_runtime_delegate", symbol);
  150. ERR_FAIL_COND_V(err != OK, false);
  151. hostfxr_get_runtime_delegate = (hostfxr_get_runtime_delegate_fn)symbol;
  152. err = OS::get_singleton()->get_dynamic_library_symbol_handle(lib, "hostfxr_close", symbol);
  153. ERR_FAIL_COND_V(err != OK, false);
  154. hostfxr_close = (hostfxr_close_fn)symbol;
  155. return (hostfxr_initialize_for_runtime_config &&
  156. hostfxr_get_runtime_delegate &&
  157. hostfxr_close);
  158. }
  159. #ifdef TOOLS_ENABLED
  160. load_assembly_and_get_function_pointer_fn initialize_hostfxr_for_config(const char_t *p_config_path) {
  161. hostfxr_handle cxt = nullptr;
  162. int rc = hostfxr_initialize_for_runtime_config(p_config_path, nullptr, &cxt);
  163. if (rc != 0 || cxt == nullptr) {
  164. hostfxr_close(cxt);
  165. ERR_FAIL_V_MSG(nullptr, "hostfxr_initialize_for_runtime_config failed with code: " + itos(rc));
  166. }
  167. void *load_assembly_and_get_function_pointer = nullptr;
  168. rc = hostfxr_get_runtime_delegate(cxt,
  169. hdt_load_assembly_and_get_function_pointer, &load_assembly_and_get_function_pointer);
  170. if (rc != 0 || load_assembly_and_get_function_pointer == nullptr) {
  171. ERR_FAIL_V_MSG(nullptr, "hostfxr_get_runtime_delegate failed with code: " + itos(rc));
  172. }
  173. hostfxr_close(cxt);
  174. return (load_assembly_and_get_function_pointer_fn)load_assembly_and_get_function_pointer;
  175. }
  176. #else
  177. load_assembly_and_get_function_pointer_fn initialize_hostfxr_self_contained(
  178. const char_t *p_main_assembly_path) {
  179. hostfxr_handle cxt = nullptr;
  180. List<String> cmdline_args = OS::get_singleton()->get_cmdline_args();
  181. List<HostFxrCharString> argv_store;
  182. Vector<const char_t *> argv;
  183. argv.resize(cmdline_args.size() + 1);
  184. argv.write[0] = p_main_assembly_path;
  185. int i = 1;
  186. for (const String &E : cmdline_args) {
  187. HostFxrCharString &stored = argv_store.push_back(str_to_hostfxr(E))->get();
  188. argv.write[i] = get_data(stored);
  189. i++;
  190. }
  191. int rc = hostfxr_initialize_for_dotnet_command_line(argv.size(), argv.ptrw(), nullptr, &cxt);
  192. if (rc != 0 || cxt == nullptr) {
  193. hostfxr_close(cxt);
  194. ERR_FAIL_V_MSG(nullptr, "hostfxr_initialize_for_dotnet_command_line failed with code: " + itos(rc));
  195. }
  196. void *load_assembly_and_get_function_pointer = nullptr;
  197. rc = hostfxr_get_runtime_delegate(cxt,
  198. hdt_load_assembly_and_get_function_pointer, &load_assembly_and_get_function_pointer);
  199. if (rc != 0 || load_assembly_and_get_function_pointer == nullptr) {
  200. ERR_FAIL_V_MSG(nullptr, "hostfxr_get_runtime_delegate failed with code: " + itos(rc));
  201. }
  202. hostfxr_close(cxt);
  203. return (load_assembly_and_get_function_pointer_fn)load_assembly_and_get_function_pointer;
  204. }
  205. #endif
  206. #ifdef TOOLS_ENABLED
  207. using godot_plugins_initialize_fn = bool (*)(void *, bool, gdmono::PluginCallbacks *, GDMonoCache::ManagedCallbacks *, const void **, int32_t);
  208. #else
  209. using godot_plugins_initialize_fn = bool (*)(void *, GDMonoCache::ManagedCallbacks *, const void **, int32_t);
  210. #endif
  211. #ifdef TOOLS_ENABLED
  212. godot_plugins_initialize_fn initialize_hostfxr_and_godot_plugins(bool &r_runtime_initialized) {
  213. godot_plugins_initialize_fn godot_plugins_initialize = nullptr;
  214. HostFxrCharString godot_plugins_path = str_to_hostfxr(
  215. GodotSharpDirs::get_api_assemblies_dir().path_join("GodotPlugins.dll"));
  216. HostFxrCharString config_path = str_to_hostfxr(
  217. GodotSharpDirs::get_api_assemblies_dir().path_join("GodotPlugins.runtimeconfig.json"));
  218. load_assembly_and_get_function_pointer_fn load_assembly_and_get_function_pointer =
  219. initialize_hostfxr_for_config(get_data(config_path));
  220. if (load_assembly_and_get_function_pointer == nullptr) {
  221. // Show a message box to the user to make the problem explicit (and explain a potential crash).
  222. OS::get_singleton()->alert(TTR("Unable to load .NET runtime, no compatible version was found.\nAttempting to create/edit a project will lead to a crash.\n\nPlease install the .NET SDK 6.0 or later from https://dotnet.microsoft.com/en-us/download and restart Godot."), TTR("Failed to load .NET runtime"));
  223. ERR_FAIL_V_MSG(nullptr, ".NET: Failed to load compatible .NET runtime");
  224. }
  225. r_runtime_initialized = true;
  226. print_verbose(".NET: hostfxr initialized");
  227. int rc = load_assembly_and_get_function_pointer(get_data(godot_plugins_path),
  228. HOSTFXR_STR("GodotPlugins.Main, GodotPlugins"),
  229. HOSTFXR_STR("InitializeFromEngine"),
  230. UNMANAGEDCALLERSONLY_METHOD,
  231. nullptr,
  232. (void **)&godot_plugins_initialize);
  233. ERR_FAIL_COND_V_MSG(rc != 0, nullptr, ".NET: Failed to get GodotPlugins initialization function pointer");
  234. return godot_plugins_initialize;
  235. }
  236. #else
  237. static String get_assembly_name() {
  238. String assembly_name = GLOBAL_GET("dotnet/project/assembly_name");
  239. if (assembly_name.is_empty()) {
  240. assembly_name = ProjectSettings::get_singleton()->get_safe_project_name();
  241. }
  242. return assembly_name;
  243. }
  244. godot_plugins_initialize_fn initialize_hostfxr_and_godot_plugins(bool &r_runtime_initialized) {
  245. godot_plugins_initialize_fn godot_plugins_initialize = nullptr;
  246. String assembly_name = get_assembly_name();
  247. HostFxrCharString assembly_path = str_to_hostfxr(GodotSharpDirs::get_api_assemblies_dir()
  248. .path_join(assembly_name + ".dll"));
  249. load_assembly_and_get_function_pointer_fn load_assembly_and_get_function_pointer =
  250. initialize_hostfxr_self_contained(get_data(assembly_path));
  251. ERR_FAIL_NULL_V(load_assembly_and_get_function_pointer, nullptr);
  252. r_runtime_initialized = true;
  253. print_verbose(".NET: hostfxr initialized");
  254. int rc = load_assembly_and_get_function_pointer(get_data(assembly_path),
  255. get_data(str_to_hostfxr("GodotPlugins.Game.Main, " + assembly_name)),
  256. HOSTFXR_STR("InitializeFromGameProject"),
  257. UNMANAGEDCALLERSONLY_METHOD,
  258. nullptr,
  259. (void **)&godot_plugins_initialize);
  260. ERR_FAIL_COND_V_MSG(rc != 0, nullptr, ".NET: Failed to get GodotPlugins initialization function pointer");
  261. return godot_plugins_initialize;
  262. }
  263. godot_plugins_initialize_fn try_load_native_aot_library(void *&r_aot_dll_handle) {
  264. String assembly_name = get_assembly_name();
  265. #if defined(WINDOWS_ENABLED)
  266. String native_aot_so_path = GodotSharpDirs::get_api_assemblies_dir().path_join(assembly_name + ".dll");
  267. #elif defined(MACOS_ENABLED)
  268. String native_aot_so_path = GodotSharpDirs::get_api_assemblies_dir().path_join(assembly_name + ".dylib");
  269. #elif defined(UNIX_ENABLED)
  270. String native_aot_so_path = GodotSharpDirs::get_api_assemblies_dir().path_join(assembly_name + ".so");
  271. #else
  272. #error "Platform not supported (yet?)"
  273. #endif
  274. if (FileAccess::exists(native_aot_so_path)) {
  275. Error err = OS::get_singleton()->open_dynamic_library(native_aot_so_path, r_aot_dll_handle);
  276. if (err != OK) {
  277. return nullptr;
  278. }
  279. void *lib = r_aot_dll_handle;
  280. void *symbol = nullptr;
  281. err = OS::get_singleton()->get_dynamic_library_symbol_handle(lib, "godotsharp_game_main_init", symbol);
  282. ERR_FAIL_COND_V(err != OK, nullptr);
  283. return (godot_plugins_initialize_fn)symbol;
  284. }
  285. return nullptr;
  286. }
  287. #endif
  288. } // namespace
  289. static bool _on_core_api_assembly_loaded() {
  290. if (!GDMonoCache::godot_api_cache_updated) {
  291. return false;
  292. }
  293. bool debug;
  294. #ifdef DEBUG_ENABLED
  295. debug = true;
  296. #else
  297. debug = false;
  298. #endif
  299. GDMonoCache::managed_callbacks.GD_OnCoreApiAssemblyLoaded(debug);
  300. return true;
  301. }
  302. void GDMono::initialize() {
  303. print_verbose(".NET: Initializing module...");
  304. _init_godot_api_hashes();
  305. godot_plugins_initialize_fn godot_plugins_initialize = nullptr;
  306. if (!load_hostfxr(hostfxr_dll_handle)) {
  307. #if !defined(TOOLS_ENABLED)
  308. godot_plugins_initialize = try_load_native_aot_library(hostfxr_dll_handle);
  309. if (godot_plugins_initialize != nullptr) {
  310. is_native_aot = true;
  311. } else {
  312. ERR_FAIL_MSG(".NET: Failed to load hostfxr");
  313. }
  314. #else
  315. // Show a message box to the user to make the problem explicit (and explain a potential crash).
  316. OS::get_singleton()->alert(TTR("Unable to load .NET runtime, specifically hostfxr.\nAttempting to create/edit a project will lead to a crash.\n\nPlease install the .NET SDK 6.0 or later from https://dotnet.microsoft.com/en-us/download and restart Godot."), TTR("Failed to load .NET runtime"));
  317. ERR_FAIL_MSG(".NET: Failed to load hostfxr");
  318. #endif
  319. }
  320. if (!is_native_aot) {
  321. godot_plugins_initialize = initialize_hostfxr_and_godot_plugins(runtime_initialized);
  322. ERR_FAIL_NULL(godot_plugins_initialize);
  323. }
  324. int32_t interop_funcs_size = 0;
  325. const void **interop_funcs = godotsharp::get_runtime_interop_funcs(interop_funcs_size);
  326. GDMonoCache::ManagedCallbacks managed_callbacks{};
  327. void *godot_dll_handle = nullptr;
  328. #if defined(UNIX_ENABLED) && !defined(MACOS_ENABLED) && !defined(IOS_ENABLED)
  329. // Managed code can access it on its own on other platforms
  330. godot_dll_handle = dlopen(nullptr, RTLD_NOW);
  331. #endif
  332. #ifdef TOOLS_ENABLED
  333. gdmono::PluginCallbacks plugin_callbacks_res;
  334. bool init_ok = godot_plugins_initialize(godot_dll_handle,
  335. Engine::get_singleton()->is_editor_hint(),
  336. &plugin_callbacks_res, &managed_callbacks,
  337. interop_funcs, interop_funcs_size);
  338. ERR_FAIL_COND_MSG(!init_ok, ".NET: GodotPlugins initialization failed");
  339. plugin_callbacks = plugin_callbacks_res;
  340. #else
  341. bool init_ok = godot_plugins_initialize(godot_dll_handle, &managed_callbacks,
  342. interop_funcs, interop_funcs_size);
  343. ERR_FAIL_COND_MSG(!init_ok, ".NET: GodotPlugins initialization failed");
  344. #endif
  345. GDMonoCache::update_godot_api_cache(managed_callbacks);
  346. print_verbose(".NET: GodotPlugins initialized");
  347. _on_core_api_assembly_loaded();
  348. }
  349. #ifdef TOOLS_ENABLED
  350. void GDMono::initialize_load_assemblies() {
  351. if (Engine::get_singleton()->is_project_manager_hint()) {
  352. return;
  353. }
  354. // Load the project's main assembly. This doesn't necessarily need to succeed.
  355. // The game may not be using .NET at all, or if the project does use .NET and
  356. // we're running in the editor, it may just happen to be it wasn't built yet.
  357. if (!_load_project_assembly()) {
  358. if (OS::get_singleton()->is_stdout_verbose()) {
  359. print_error(".NET: Failed to load project assembly");
  360. }
  361. }
  362. }
  363. #endif
  364. void GDMono::_init_godot_api_hashes() {
  365. #ifdef DEBUG_METHODS_ENABLED
  366. get_api_core_hash();
  367. #ifdef TOOLS_ENABLED
  368. get_api_editor_hash();
  369. #endif // TOOLS_ENABLED
  370. #endif // DEBUG_METHODS_ENABLED
  371. }
  372. #ifdef TOOLS_ENABLED
  373. bool GDMono::_load_project_assembly() {
  374. String assembly_name = GLOBAL_GET("dotnet/project/assembly_name");
  375. if (assembly_name.is_empty()) {
  376. assembly_name = ProjectSettings::get_singleton()->get_safe_project_name();
  377. }
  378. String assembly_path = GodotSharpDirs::get_res_temp_assemblies_dir()
  379. .path_join(assembly_name + ".dll");
  380. assembly_path = ProjectSettings::get_singleton()->globalize_path(assembly_path);
  381. if (!FileAccess::exists(assembly_path)) {
  382. return false;
  383. }
  384. String loaded_assembly_path;
  385. bool success = plugin_callbacks.LoadProjectAssemblyCallback(assembly_path.utf16(), &loaded_assembly_path);
  386. if (success) {
  387. project_assembly_path = loaded_assembly_path.simplify_path();
  388. project_assembly_modified_time = FileAccess::get_modified_time(loaded_assembly_path);
  389. }
  390. return success;
  391. }
  392. #endif
  393. #ifdef GD_MONO_HOT_RELOAD
  394. Error GDMono::reload_project_assemblies() {
  395. ERR_FAIL_COND_V(!runtime_initialized, ERR_BUG);
  396. finalizing_scripts_domain = true;
  397. CSharpLanguage::get_singleton()->_on_scripts_domain_about_to_unload();
  398. if (!get_plugin_callbacks().UnloadProjectPluginCallback()) {
  399. ERR_FAIL_V_MSG(Error::FAILED, ".NET: Failed to unload assemblies.");
  400. }
  401. finalizing_scripts_domain = false;
  402. // Load the project's main assembly. Here, during hot-reloading, we do
  403. // consider failing to load the project's main assembly to be an error.
  404. if (!_load_project_assembly()) {
  405. print_error(".NET: Failed to load project assembly.");
  406. return ERR_CANT_OPEN;
  407. }
  408. return OK;
  409. }
  410. #endif
  411. GDMono::GDMono() {
  412. singleton = this;
  413. runtime_initialized = false;
  414. finalizing_scripts_domain = false;
  415. api_core_hash = 0;
  416. #ifdef TOOLS_ENABLED
  417. api_editor_hash = 0;
  418. #endif
  419. }
  420. GDMono::~GDMono() {
  421. finalizing_scripts_domain = true;
  422. if (is_runtime_initialized()) {
  423. if (GDMonoCache::godot_api_cache_updated) {
  424. GDMonoCache::managed_callbacks.DisposablesTracker_OnGodotShuttingDown();
  425. }
  426. }
  427. if (hostfxr_dll_handle) {
  428. OS::get_singleton()->close_dynamic_library(hostfxr_dll_handle);
  429. }
  430. finalizing_scripts_domain = false;
  431. runtime_initialized = false;
  432. #if defined(ANDROID_ENABLED)
  433. gdmono::android::support::cleanup();
  434. #endif
  435. singleton = nullptr;
  436. }
  437. namespace mono_bind {
  438. GodotSharp *GodotSharp::singleton = nullptr;
  439. bool GodotSharp::_is_runtime_initialized() {
  440. return GDMono::get_singleton() != nullptr && GDMono::get_singleton()->is_runtime_initialized();
  441. }
  442. void GodotSharp::_reload_assemblies(bool p_soft_reload) {
  443. #ifdef GD_MONO_HOT_RELOAD
  444. CRASH_COND(CSharpLanguage::get_singleton() == nullptr);
  445. // This method may be called more than once with `call_deferred`, so we need to check
  446. // again if reloading is needed to avoid reloading multiple times unnecessarily.
  447. if (CSharpLanguage::get_singleton()->is_assembly_reloading_needed()) {
  448. CSharpLanguage::get_singleton()->reload_assemblies(p_soft_reload);
  449. }
  450. #endif
  451. }
  452. void GodotSharp::_bind_methods() {
  453. ClassDB::bind_method(D_METHOD("is_runtime_initialized"), &GodotSharp::_is_runtime_initialized);
  454. ClassDB::bind_method(D_METHOD("_reload_assemblies"), &GodotSharp::_reload_assemblies);
  455. }
  456. GodotSharp::GodotSharp() {
  457. singleton = this;
  458. }
  459. GodotSharp::~GodotSharp() {
  460. singleton = nullptr;
  461. }
  462. } // namespace mono_bind