gd_mono.cpp 21 KB

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