gd_mono.cpp 33 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149
  1. /*************************************************************************/
  2. /* gd_mono.cpp */
  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. #include "gd_mono.h"
  31. #include <mono/metadata/exception.h>
  32. #include <mono/metadata/mono-config.h>
  33. #include <mono/metadata/mono-debug.h>
  34. #include <mono/metadata/mono-gc.h>
  35. #include <mono/metadata/profiler.h>
  36. #include "core/os/dir_access.h"
  37. #include "core/os/file_access.h"
  38. #include "core/os/os.h"
  39. #include "core/os/thread.h"
  40. #include "core/project_settings.h"
  41. #include "../csharp_script.h"
  42. #include "../glue/cs_glue_version.gen.h"
  43. #include "../godotsharp_dirs.h"
  44. #include "../utils/path_utils.h"
  45. #include "gd_mono_class.h"
  46. #include "gd_mono_marshal.h"
  47. #include "gd_mono_utils.h"
  48. #ifdef TOOLS_ENABLED
  49. #include "../editor/godotsharp_editor.h"
  50. #include "main/main.h"
  51. #endif
  52. #define OUT_OF_SYNC_ERR_MESSAGE(m_assembly_name) "The assembly '" m_assembly_name "' is out of sync. " \
  53. "This error is expected if you just upgraded to a newer Godot version. " \
  54. "Building the project will update the assembly to the correct version."
  55. GDMono *GDMono::singleton = NULL;
  56. namespace {
  57. void setup_runtime_main_args() {
  58. CharString execpath = OS::get_singleton()->get_executable_path().utf8();
  59. List<String> cmdline_args = OS::get_singleton()->get_cmdline_args();
  60. List<CharString> cmdline_args_utf8;
  61. Vector<char *> main_args;
  62. main_args.resize(cmdline_args.size() + 1);
  63. main_args.write[0] = execpath.ptrw();
  64. int i = 1;
  65. for (List<String>::Element *E = cmdline_args.front(); E; E = E->next()) {
  66. CharString &stored = cmdline_args_utf8.push_back(E->get().utf8())->get();
  67. main_args.write[i] = stored.ptrw();
  68. i++;
  69. }
  70. mono_runtime_set_main_args(main_args.size(), main_args.ptrw());
  71. }
  72. void gdmono_profiler_init() {
  73. String profiler_args = GLOBAL_DEF("mono/profiler/args", "log:calls,alloc,sample,output=output.mlpd");
  74. bool profiler_enabled = GLOBAL_DEF("mono/profiler/enabled", false);
  75. if (profiler_enabled) {
  76. mono_profiler_load(profiler_args.utf8());
  77. }
  78. }
  79. #ifdef DEBUG_ENABLED
  80. static bool _wait_for_debugger_msecs(uint32_t p_msecs) {
  81. do {
  82. if (mono_is_debugger_attached())
  83. return true;
  84. int last_tick = OS::get_singleton()->get_ticks_msec();
  85. OS::get_singleton()->delay_usec((p_msecs < 25 ? p_msecs : 25) * 1000);
  86. uint32_t tdiff = OS::get_singleton()->get_ticks_msec() - last_tick;
  87. if (tdiff > p_msecs) {
  88. p_msecs = 0;
  89. } else {
  90. p_msecs -= tdiff;
  91. }
  92. } while (p_msecs > 0);
  93. return mono_is_debugger_attached();
  94. }
  95. void gdmono_debug_init() {
  96. mono_debug_init(MONO_DEBUG_FORMAT_MONO);
  97. int da_port = GLOBAL_DEF("mono/debugger_agent/port", 23685);
  98. bool da_suspend = GLOBAL_DEF("mono/debugger_agent/wait_for_debugger", false);
  99. int da_timeout = GLOBAL_DEF("mono/debugger_agent/wait_timeout", 3000);
  100. #ifdef TOOLS_ENABLED
  101. if (Engine::get_singleton()->is_editor_hint() ||
  102. ProjectSettings::get_singleton()->get_resource_path().empty() ||
  103. Main::is_project_manager()) {
  104. return;
  105. }
  106. #endif
  107. CharString da_args = OS::get_singleton()->get_environment("GODOT_MONO_DEBUGGER_AGENT").utf8();
  108. if (da_args.length() == 0) {
  109. da_args = String("--debugger-agent=transport=dt_socket,address=127.0.0.1:" + itos(da_port) +
  110. ",embedding=1,server=y,suspend=" + (da_suspend ? "y,timeout=" + itos(da_timeout) : "n"))
  111. .utf8();
  112. }
  113. // --debugger-agent=help
  114. const char *options[] = {
  115. "--soft-breakpoints",
  116. da_args.get_data()
  117. };
  118. mono_jit_parse_options(2, (char **)options);
  119. }
  120. #endif
  121. } // namespace
  122. void GDMono::add_mono_shared_libs_dir_to_path() {
  123. // By default Mono seems to search shared libraries in the following directories:
  124. // Current working directory, @executable_path@ and PATH
  125. // The parent directory of the image file (assembly where the dllimport method is declared)
  126. // @executable_path@/../lib
  127. // @executable_path@/../Libraries (__MACH__ only)
  128. // This does not work when embedding Mono unless we use the same directory structure.
  129. // To fix this we append the directory containing our shared libraries to PATH.
  130. #if defined(WINDOWS_ENABLED) || defined(UNIX_ENABLED)
  131. String path_var("PATH");
  132. String path_value = OS::get_singleton()->get_environment(path_var);
  133. #ifdef WINDOWS_ENABLED
  134. path_value += ';';
  135. String bundled_bin_dir = GodotSharpDirs::get_data_mono_bin_dir();
  136. #ifdef TOOLS_ENABLED
  137. if (DirAccess::exists(bundled_bin_dir)) {
  138. path_value += bundled_bin_dir;
  139. } else {
  140. path_value += mono_reg_info.bin_dir;
  141. }
  142. #else
  143. if (DirAccess::exists(bundled_bin_dir))
  144. path_value += bundled_bin_dir;
  145. #endif // TOOLS_ENABLED
  146. #else
  147. path_value += ':';
  148. String bundled_lib_dir = GodotSharpDirs::get_data_mono_lib_dir();
  149. if (DirAccess::exists(bundled_lib_dir)) {
  150. path_value += bundled_lib_dir;
  151. } else {
  152. // TODO: Do we need to add the lib dir when using the system installed Mono on Unix platforms?
  153. }
  154. #endif // WINDOWS_ENABLED
  155. OS::get_singleton()->set_environment(path_var, path_value);
  156. #endif // WINDOWS_ENABLED || UNIX_ENABLED
  157. }
  158. void GDMono::initialize() {
  159. ERR_FAIL_NULL(Engine::get_singleton());
  160. print_verbose("Mono: Initializing module...");
  161. #ifdef DEBUG_METHODS_ENABLED
  162. _initialize_and_check_api_hashes();
  163. #endif
  164. GDMonoLog::get_singleton()->initialize();
  165. String assembly_rootdir;
  166. String config_dir;
  167. #ifdef TOOLS_ENABLED
  168. #ifdef WINDOWS_ENABLED
  169. mono_reg_info = MonoRegUtils::find_mono();
  170. if (mono_reg_info.assembly_dir.length() && DirAccess::exists(mono_reg_info.assembly_dir)) {
  171. assembly_rootdir = mono_reg_info.assembly_dir;
  172. }
  173. if (mono_reg_info.config_dir.length() && DirAccess::exists(mono_reg_info.config_dir)) {
  174. config_dir = mono_reg_info.config_dir;
  175. }
  176. #elif OSX_ENABLED
  177. const char *c_assembly_rootdir = mono_assembly_getrootdir();
  178. const char *c_config_dir = mono_get_config_dir();
  179. if (!c_assembly_rootdir || !c_config_dir || !DirAccess::exists(c_assembly_rootdir) || !DirAccess::exists(c_config_dir)) {
  180. Vector<const char *> locations;
  181. locations.push_back("/Library/Frameworks/Mono.framework/Versions/Current/");
  182. locations.push_back("/usr/local/var/homebrew/linked/mono/");
  183. for (int i = 0; i < locations.size(); i++) {
  184. String hint_assembly_rootdir = path_join(locations[i], "lib");
  185. String hint_mscorlib_path = path_join(hint_assembly_rootdir, "mono", "4.5", "mscorlib.dll");
  186. String hint_config_dir = path_join(locations[i], "etc");
  187. if (FileAccess::exists(hint_mscorlib_path) && DirAccess::exists(hint_config_dir)) {
  188. assembly_rootdir = hint_assembly_rootdir;
  189. config_dir = hint_config_dir;
  190. break;
  191. }
  192. }
  193. }
  194. #endif
  195. #endif // TOOLS_ENABLED
  196. String bundled_assembly_rootdir = GodotSharpDirs::get_data_mono_lib_dir();
  197. String bundled_config_dir = GodotSharpDirs::get_data_mono_etc_dir();
  198. #ifdef TOOLS_ENABLED
  199. if (DirAccess::exists(bundled_assembly_rootdir) && DirAccess::exists(bundled_config_dir)) {
  200. assembly_rootdir = bundled_assembly_rootdir;
  201. config_dir = bundled_config_dir;
  202. }
  203. #ifdef WINDOWS_ENABLED
  204. if (assembly_rootdir.empty() || config_dir.empty()) {
  205. // Assertion: if they are not set, then they weren't found in the registry
  206. CRASH_COND(mono_reg_info.assembly_dir.length() > 0 || mono_reg_info.config_dir.length() > 0);
  207. ERR_PRINT("Cannot find Mono in the registry");
  208. }
  209. #endif // WINDOWS_ENABLED
  210. #else
  211. // These are always the directories in export templates
  212. assembly_rootdir = bundled_assembly_rootdir;
  213. config_dir = bundled_config_dir;
  214. #endif // TOOLS_ENABLED
  215. // Leak if we call mono_set_dirs more than once
  216. mono_set_dirs(assembly_rootdir.length() ? assembly_rootdir.utf8().get_data() : NULL,
  217. config_dir.length() ? config_dir.utf8().get_data() : NULL);
  218. add_mono_shared_libs_dir_to_path();
  219. GDMonoAssembly::initialize();
  220. gdmono_profiler_init();
  221. #ifdef DEBUG_ENABLED
  222. gdmono_debug_init();
  223. #endif
  224. mono_config_parse(NULL);
  225. mono_install_unhandled_exception_hook(&unhandled_exception_hook, NULL);
  226. #ifdef TOOLS_ENABLED
  227. if (!DirAccess::exists("res://.mono")) {
  228. // 'res://.mono/' is missing so there is nothing to load. We don't need to initialize mono, but
  229. // we still do so unless mscorlib is missing (which is the case for projects that don't use C#).
  230. String mscorlib_fname("mscorlib.dll");
  231. Vector<String> search_dirs;
  232. GDMonoAssembly::fill_search_dirs(search_dirs);
  233. bool found = false;
  234. for (int i = 0; i < search_dirs.size(); i++) {
  235. if (FileAccess::exists(search_dirs[i].plus_file(mscorlib_fname))) {
  236. found = true;
  237. break;
  238. }
  239. }
  240. if (!found)
  241. return; // mscorlib is missing, do not initialize mono
  242. }
  243. #endif
  244. root_domain = mono_jit_init_version("GodotEngine.RootDomain", "v4.0.30319");
  245. ERR_EXPLAIN("Mono: Failed to initialize runtime");
  246. ERR_FAIL_NULL(root_domain);
  247. GDMonoUtils::set_main_thread(GDMonoUtils::get_current_thread());
  248. setup_runtime_main_args(); // Required for System.Environment.GetCommandLineArgs
  249. runtime_initialized = true;
  250. print_verbose("Mono: Runtime initialized");
  251. // mscorlib assembly MUST be present at initialization
  252. ERR_EXPLAIN("Mono: Failed to load mscorlib assembly");
  253. ERR_FAIL_COND(!_load_corlib_assembly());
  254. #ifdef TOOLS_ENABLED
  255. // The tools domain must be loaded here, before the scripts domain.
  256. // Otherwise domain unload on the scripts domain will hang indefinitely.
  257. ERR_EXPLAIN("Mono: Failed to load tools domain");
  258. ERR_FAIL_COND(_load_tools_domain() != OK);
  259. // TODO move to editor init callback, and do it lazily when required before editor init (e.g.: bindings generation)
  260. ERR_EXPLAIN("Mono: Failed to load Editor Tools assembly");
  261. ERR_FAIL_COND(!_load_editor_tools_assembly());
  262. #endif
  263. ERR_EXPLAIN("Mono: Failed to load scripts domain");
  264. ERR_FAIL_COND(_load_scripts_domain() != OK);
  265. #ifdef DEBUG_ENABLED
  266. bool debugger_attached = _wait_for_debugger_msecs(500);
  267. if (!debugger_attached && OS::get_singleton()->is_stdout_verbose())
  268. print_error("Mono: Debugger wait timeout");
  269. #endif
  270. _register_internal_calls();
  271. // The following assemblies are not required at initialization
  272. #ifdef MONO_GLUE_ENABLED
  273. if (_load_api_assemblies()) {
  274. // Everything is fine with the api assemblies, load the project assembly
  275. _load_project_assembly();
  276. } else {
  277. if ((core_api_assembly && (core_api_assembly_out_of_sync || !GDMonoUtils::mono_cache.godot_api_cache_updated))
  278. #ifdef TOOLS_ENABLED
  279. || (editor_api_assembly && editor_api_assembly_out_of_sync)
  280. #endif
  281. ) {
  282. #ifdef TOOLS_ENABLED
  283. // The assembly was successfully loaded, but the full api could not be cached.
  284. // This is most likely an outdated assembly loaded because of an invalid version in the
  285. // metadata, so we invalidate the version in the metadata and unload the script domain.
  286. if (core_api_assembly_out_of_sync) {
  287. ERR_PRINT(OUT_OF_SYNC_ERR_MESSAGE(CORE_API_ASSEMBLY_NAME));
  288. metadata_set_api_assembly_invalidated(APIAssembly::API_CORE, true);
  289. } else if (!GDMonoUtils::mono_cache.godot_api_cache_updated) {
  290. ERR_PRINT("The loaded assembly '" CORE_API_ASSEMBLY_NAME "' is in sync, but the cache update failed");
  291. metadata_set_api_assembly_invalidated(APIAssembly::API_CORE, true);
  292. }
  293. if (editor_api_assembly_out_of_sync) {
  294. ERR_PRINT(OUT_OF_SYNC_ERR_MESSAGE(EDITOR_API_ASSEMBLY_NAME));
  295. metadata_set_api_assembly_invalidated(APIAssembly::API_EDITOR, true);
  296. }
  297. print_line("Mono: Proceeding to unload scripts domain because of invalid API assemblies.");
  298. Error err = _unload_scripts_domain();
  299. if (err != OK) {
  300. WARN_PRINT("Mono: Failed to unload scripts domain");
  301. }
  302. #else
  303. ERR_PRINT("The loaded API assembly is invalid");
  304. CRASH_NOW();
  305. #endif // TOOLS_ENABLED
  306. }
  307. }
  308. #else
  309. print_verbose("Mono: Glue disabled, ignoring script assemblies.");
  310. #endif // MONO_GLUE_ENABLED
  311. print_verbose("Mono: INITIALIZED");
  312. }
  313. #ifdef MONO_GLUE_ENABLED
  314. namespace GodotSharpBindings {
  315. uint64_t get_core_api_hash();
  316. #ifdef TOOLS_ENABLED
  317. uint64_t get_editor_api_hash();
  318. #endif
  319. uint32_t get_bindings_version();
  320. void register_generated_icalls();
  321. } // namespace GodotSharpBindings
  322. #endif
  323. void GDMono::_register_internal_calls() {
  324. #ifdef MONO_GLUE_ENABLED
  325. GodotSharpBindings::register_generated_icalls();
  326. #endif
  327. #ifdef TOOLS_ENABLED
  328. GodotSharpEditor::register_internal_calls();
  329. #endif
  330. }
  331. void GDMono::_initialize_and_check_api_hashes() {
  332. #ifdef MONO_GLUE_ENABLED
  333. if (get_api_core_hash() != GodotSharpBindings::get_core_api_hash()) {
  334. ERR_PRINT("Mono: Core API hash mismatch!");
  335. }
  336. #ifdef TOOLS_ENABLED
  337. if (get_api_editor_hash() != GodotSharpBindings::get_editor_api_hash()) {
  338. ERR_PRINT("Mono: Editor API hash mismatch!");
  339. }
  340. #endif // TOOLS_ENABLED
  341. #endif // MONO_GLUE_ENABLED
  342. }
  343. void GDMono::add_assembly(uint32_t p_domain_id, GDMonoAssembly *p_assembly) {
  344. assemblies[p_domain_id][p_assembly->get_name()] = p_assembly;
  345. }
  346. GDMonoAssembly **GDMono::get_loaded_assembly(const String &p_name) {
  347. MonoDomain *domain = mono_domain_get();
  348. uint32_t domain_id = domain ? mono_domain_get_id(domain) : 0;
  349. return assemblies[domain_id].getptr(p_name);
  350. }
  351. bool GDMono::load_assembly(const String &p_name, GDMonoAssembly **r_assembly, bool p_refonly) {
  352. CRASH_COND(!r_assembly);
  353. MonoAssemblyName *aname = mono_assembly_name_new(p_name.utf8());
  354. bool result = load_assembly(p_name, aname, r_assembly, p_refonly);
  355. mono_assembly_name_free(aname);
  356. mono_free(aname);
  357. return result;
  358. }
  359. bool GDMono::load_assembly(const String &p_name, MonoAssemblyName *p_aname, GDMonoAssembly **r_assembly, bool p_refonly) {
  360. CRASH_COND(!r_assembly);
  361. print_verbose("Mono: Loading assembly " + p_name + (p_refonly ? " (refonly)" : "") + "...");
  362. MonoImageOpenStatus status = MONO_IMAGE_OK;
  363. MonoAssembly *assembly = mono_assembly_load_full(p_aname, NULL, &status, p_refonly);
  364. if (!assembly)
  365. return false;
  366. ERR_FAIL_COND_V(status != MONO_IMAGE_OK, false);
  367. uint32_t domain_id = mono_domain_get_id(mono_domain_get());
  368. GDMonoAssembly **stored_assembly = assemblies[domain_id].getptr(p_name);
  369. ERR_FAIL_COND_V(stored_assembly == NULL, false);
  370. ERR_FAIL_COND_V((*stored_assembly)->get_assembly() != assembly, false);
  371. *r_assembly = *stored_assembly;
  372. print_verbose("Mono: Assembly " + p_name + (p_refonly ? " (refonly)" : "") + " loaded from path: " + (*r_assembly)->get_path());
  373. return true;
  374. }
  375. bool GDMono::load_assembly_from(const String &p_name, const String &p_path, GDMonoAssembly **r_assembly, bool p_refonly) {
  376. CRASH_COND(!r_assembly);
  377. print_verbose("Mono: Loading assembly " + p_name + (p_refonly ? " (refonly)" : "") + "...");
  378. GDMonoAssembly *assembly = GDMonoAssembly::load_from(p_name, p_path, p_refonly);
  379. if (!assembly)
  380. return false;
  381. #ifdef DEBUG_ENABLED
  382. uint32_t domain_id = mono_domain_get_id(mono_domain_get());
  383. GDMonoAssembly **stored_assembly = assemblies[domain_id].getptr(p_name);
  384. ERR_FAIL_COND_V(stored_assembly == NULL, false);
  385. ERR_FAIL_COND_V(*stored_assembly != assembly, false);
  386. #endif
  387. *r_assembly = assembly;
  388. print_verbose("Mono: Assembly " + p_name + (p_refonly ? " (refonly)" : "") + " loaded from path: " + (*r_assembly)->get_path());
  389. return true;
  390. }
  391. APIAssembly::Version APIAssembly::Version::get_from_loaded_assembly(GDMonoAssembly *p_api_assembly, APIAssembly::Type p_api_type) {
  392. APIAssembly::Version api_assembly_version;
  393. const char *nativecalls_name = p_api_type == APIAssembly::API_CORE ?
  394. BINDINGS_CLASS_NATIVECALLS :
  395. BINDINGS_CLASS_NATIVECALLS_EDITOR;
  396. GDMonoClass *nativecalls_klass = p_api_assembly->get_class(BINDINGS_NAMESPACE, nativecalls_name);
  397. if (nativecalls_klass) {
  398. GDMonoField *api_hash_field = nativecalls_klass->get_field("godot_api_hash");
  399. if (api_hash_field)
  400. api_assembly_version.godot_api_hash = GDMonoMarshal::unbox<uint64_t>(api_hash_field->get_value(NULL));
  401. GDMonoField *binds_ver_field = nativecalls_klass->get_field("bindings_version");
  402. if (binds_ver_field)
  403. api_assembly_version.bindings_version = GDMonoMarshal::unbox<uint32_t>(binds_ver_field->get_value(NULL));
  404. GDMonoField *cs_glue_ver_field = nativecalls_klass->get_field("cs_glue_version");
  405. if (cs_glue_ver_field)
  406. api_assembly_version.cs_glue_version = GDMonoMarshal::unbox<uint32_t>(cs_glue_ver_field->get_value(NULL));
  407. }
  408. return api_assembly_version;
  409. }
  410. String APIAssembly::to_string(APIAssembly::Type p_type) {
  411. return p_type == APIAssembly::API_CORE ? "API_CORE" : "API_EDITOR";
  412. }
  413. bool GDMono::_load_corlib_assembly() {
  414. if (corlib_assembly)
  415. return true;
  416. bool success = load_assembly("mscorlib", &corlib_assembly);
  417. if (success)
  418. GDMonoUtils::update_corlib_cache();
  419. return success;
  420. }
  421. bool GDMono::_load_core_api_assembly() {
  422. if (core_api_assembly)
  423. return true;
  424. #ifdef TOOLS_ENABLED
  425. if (metadata_is_api_assembly_invalidated(APIAssembly::API_CORE)) {
  426. print_verbose("Mono: Skipping loading of Core API assembly because it was invalidated");
  427. return false;
  428. }
  429. #endif
  430. String assembly_path = GodotSharpDirs::get_res_assemblies_dir().plus_file(CORE_API_ASSEMBLY_NAME ".dll");
  431. if (!FileAccess::exists(assembly_path))
  432. return false;
  433. bool success = load_assembly_from(CORE_API_ASSEMBLY_NAME,
  434. assembly_path,
  435. &core_api_assembly);
  436. if (success) {
  437. #ifdef MONO_GLUE_ENABLED
  438. APIAssembly::Version api_assembly_ver = APIAssembly::Version::get_from_loaded_assembly(core_api_assembly, APIAssembly::API_CORE);
  439. core_api_assembly_out_of_sync = GodotSharpBindings::get_core_api_hash() != api_assembly_ver.godot_api_hash ||
  440. GodotSharpBindings::get_bindings_version() != api_assembly_ver.bindings_version ||
  441. CS_GLUE_VERSION != api_assembly_ver.cs_glue_version;
  442. if (!core_api_assembly_out_of_sync) {
  443. GDMonoUtils::update_godot_api_cache();
  444. }
  445. #else
  446. GDMonoUtils::update_godot_api_cache();
  447. #endif
  448. }
  449. return success;
  450. }
  451. #ifdef TOOLS_ENABLED
  452. bool GDMono::_load_editor_api_assembly() {
  453. if (editor_api_assembly)
  454. return true;
  455. if (metadata_is_api_assembly_invalidated(APIAssembly::API_EDITOR)) {
  456. print_verbose("Mono: Skipping loading of Editor API assembly because it was invalidated");
  457. return false;
  458. }
  459. String assembly_path = GodotSharpDirs::get_res_assemblies_dir().plus_file(EDITOR_API_ASSEMBLY_NAME ".dll");
  460. if (!FileAccess::exists(assembly_path))
  461. return false;
  462. bool success = load_assembly_from(EDITOR_API_ASSEMBLY_NAME,
  463. assembly_path,
  464. &editor_api_assembly);
  465. if (success) {
  466. #ifdef MONO_GLUE_ENABLED
  467. APIAssembly::Version api_assembly_ver = APIAssembly::Version::get_from_loaded_assembly(editor_api_assembly, APIAssembly::API_EDITOR);
  468. editor_api_assembly_out_of_sync = GodotSharpBindings::get_editor_api_hash() != api_assembly_ver.godot_api_hash ||
  469. GodotSharpBindings::get_bindings_version() != api_assembly_ver.bindings_version ||
  470. CS_GLUE_VERSION != api_assembly_ver.cs_glue_version;
  471. #endif
  472. }
  473. return success;
  474. }
  475. #endif
  476. #ifdef TOOLS_ENABLED
  477. bool GDMono::_load_editor_tools_assembly() {
  478. if (editor_tools_assembly)
  479. return true;
  480. _GDMONO_SCOPE_DOMAIN_(tools_domain)
  481. return load_assembly(EDITOR_TOOLS_ASSEMBLY_NAME, &editor_tools_assembly);
  482. }
  483. #endif
  484. bool GDMono::_load_project_assembly() {
  485. if (project_assembly)
  486. return true;
  487. String name = ProjectSettings::get_singleton()->get("application/config/name");
  488. if (name.empty()) {
  489. name = "UnnamedProject";
  490. }
  491. bool success = load_assembly(name, &project_assembly);
  492. if (success) {
  493. mono_assembly_set_main(project_assembly->get_assembly());
  494. CSharpLanguage::get_singleton()->project_assembly_loaded();
  495. } else {
  496. if (OS::get_singleton()->is_stdout_verbose())
  497. print_error("Mono: Failed to load project assembly");
  498. }
  499. return success;
  500. }
  501. bool GDMono::_load_api_assemblies() {
  502. if (!_load_core_api_assembly()) {
  503. if (OS::get_singleton()->is_stdout_verbose())
  504. print_error("Mono: Failed to load Core API assembly");
  505. return false;
  506. }
  507. if (core_api_assembly_out_of_sync || !GDMonoUtils::mono_cache.godot_api_cache_updated)
  508. return false;
  509. #ifdef TOOLS_ENABLED
  510. if (!_load_editor_api_assembly()) {
  511. if (OS::get_singleton()->is_stdout_verbose())
  512. print_error("Mono: Failed to load Editor API assembly");
  513. return false;
  514. }
  515. if (editor_api_assembly_out_of_sync)
  516. return false;
  517. #endif
  518. return true;
  519. }
  520. #ifdef TOOLS_ENABLED
  521. String GDMono::_get_api_assembly_metadata_path() {
  522. return GodotSharpDirs::get_res_metadata_dir().plus_file("api_assemblies.cfg");
  523. }
  524. void GDMono::metadata_set_api_assembly_invalidated(APIAssembly::Type p_api_type, bool p_invalidated) {
  525. String section = APIAssembly::to_string(p_api_type);
  526. String path = _get_api_assembly_metadata_path();
  527. Ref<ConfigFile> metadata;
  528. metadata.instance();
  529. metadata->load(path);
  530. metadata->set_value(section, "invalidated", p_invalidated);
  531. String assembly_path = GodotSharpDirs::get_res_assemblies_dir()
  532. .plus_file(p_api_type == APIAssembly::API_CORE ?
  533. CORE_API_ASSEMBLY_NAME ".dll" :
  534. EDITOR_API_ASSEMBLY_NAME ".dll");
  535. ERR_FAIL_COND(!FileAccess::exists(assembly_path));
  536. uint64_t modified_time = FileAccess::get_modified_time(assembly_path);
  537. metadata->set_value(section, "invalidated_asm_modified_time", String::num_uint64(modified_time));
  538. String dir = path.get_base_dir();
  539. if (!DirAccess::exists(dir)) {
  540. DirAccessRef da = DirAccess::create(DirAccess::ACCESS_FILESYSTEM);
  541. ERR_FAIL_COND(!da);
  542. Error err = da->make_dir_recursive(ProjectSettings::get_singleton()->globalize_path(dir));
  543. ERR_FAIL_COND(err != OK);
  544. }
  545. Error save_err = metadata->save(path);
  546. ERR_FAIL_COND(save_err != OK);
  547. }
  548. bool GDMono::metadata_is_api_assembly_invalidated(APIAssembly::Type p_api_type) {
  549. String section = APIAssembly::to_string(p_api_type);
  550. Ref<ConfigFile> metadata;
  551. metadata.instance();
  552. metadata->load(_get_api_assembly_metadata_path());
  553. String assembly_path = GodotSharpDirs::get_res_assemblies_dir()
  554. .plus_file(p_api_type == APIAssembly::API_CORE ?
  555. CORE_API_ASSEMBLY_NAME ".dll" :
  556. EDITOR_API_ASSEMBLY_NAME ".dll");
  557. if (!FileAccess::exists(assembly_path))
  558. return false;
  559. uint64_t modified_time = FileAccess::get_modified_time(assembly_path);
  560. uint64_t stored_modified_time = metadata->get_value(section, "invalidated_asm_modified_time", 0);
  561. return metadata->get_value(section, "invalidated", false) && modified_time <= stored_modified_time;
  562. }
  563. #endif
  564. Error GDMono::_load_scripts_domain() {
  565. ERR_FAIL_COND_V(scripts_domain != NULL, ERR_BUG);
  566. print_verbose("Mono: Loading scripts domain...");
  567. scripts_domain = GDMonoUtils::create_domain("GodotEngine.ScriptsDomain");
  568. ERR_EXPLAIN("Mono: Could not create scripts app domain");
  569. ERR_FAIL_NULL_V(scripts_domain, ERR_CANT_CREATE);
  570. mono_domain_set(scripts_domain, true);
  571. return OK;
  572. }
  573. Error GDMono::_unload_scripts_domain() {
  574. ERR_FAIL_NULL_V(scripts_domain, ERR_BUG);
  575. print_verbose("Mono: Unloading scripts domain...");
  576. if (mono_domain_get() != root_domain)
  577. mono_domain_set(root_domain, true);
  578. mono_gc_collect(mono_gc_max_generation());
  579. finalizing_scripts_domain = true;
  580. if (!mono_domain_finalize(scripts_domain, 2000)) {
  581. ERR_PRINT("Mono: Domain finalization timeout");
  582. }
  583. finalizing_scripts_domain = false;
  584. mono_gc_collect(mono_gc_max_generation());
  585. _domain_assemblies_cleanup(mono_domain_get_id(scripts_domain));
  586. core_api_assembly = NULL;
  587. project_assembly = NULL;
  588. #ifdef TOOLS_ENABLED
  589. editor_api_assembly = NULL;
  590. #endif
  591. core_api_assembly_out_of_sync = false;
  592. #ifdef TOOLS_ENABLED
  593. editor_api_assembly_out_of_sync = false;
  594. #endif
  595. MonoDomain *domain = scripts_domain;
  596. scripts_domain = NULL;
  597. MonoException *exc = NULL;
  598. mono_domain_try_unload(domain, (MonoObject **)&exc);
  599. if (exc) {
  600. ERR_PRINT("Exception thrown when unloading scripts domain");
  601. GDMonoUtils::debug_unhandled_exception(exc);
  602. return FAILED;
  603. }
  604. return OK;
  605. }
  606. #ifdef TOOLS_ENABLED
  607. Error GDMono::_load_tools_domain() {
  608. ERR_FAIL_COND_V(tools_domain != NULL, ERR_BUG);
  609. print_verbose("Mono: Loading tools domain...");
  610. tools_domain = GDMonoUtils::create_domain("GodotEngine.ToolsDomain");
  611. ERR_EXPLAIN("Mono: Could not create tools app domain");
  612. ERR_FAIL_NULL_V(tools_domain, ERR_CANT_CREATE);
  613. return OK;
  614. }
  615. #endif
  616. #ifdef GD_MONO_HOT_RELOAD
  617. Error GDMono::reload_scripts_domain() {
  618. ERR_FAIL_COND_V(!runtime_initialized, ERR_BUG);
  619. if (scripts_domain) {
  620. Error err = _unload_scripts_domain();
  621. if (err != OK) {
  622. ERR_PRINT("Mono: Failed to unload scripts domain");
  623. return err;
  624. }
  625. }
  626. Error err = _load_scripts_domain();
  627. if (err != OK) {
  628. ERR_PRINT("Mono: Failed to load scripts domain");
  629. return err;
  630. }
  631. #ifdef MONO_GLUE_ENABLED
  632. if (!_load_api_assemblies()) {
  633. if ((core_api_assembly && (core_api_assembly_out_of_sync || !GDMonoUtils::mono_cache.godot_api_cache_updated))
  634. #ifdef TOOLS_ENABLED
  635. || (editor_api_assembly && editor_api_assembly_out_of_sync)
  636. #endif
  637. ) {
  638. #ifdef TOOLS_ENABLED
  639. // The assembly was successfully loaded, but the full api could not be cached.
  640. // This is most likely an outdated assembly loaded because of an invalid version in the
  641. // metadata, so we invalidate the version in the metadata and unload the script domain.
  642. if (core_api_assembly_out_of_sync) {
  643. ERR_PRINT(OUT_OF_SYNC_ERR_MESSAGE(CORE_API_ASSEMBLY_NAME));
  644. metadata_set_api_assembly_invalidated(APIAssembly::API_CORE, true);
  645. } else if (!GDMonoUtils::mono_cache.godot_api_cache_updated) {
  646. ERR_PRINT("The loaded Core API assembly is in sync, but the cache update failed");
  647. metadata_set_api_assembly_invalidated(APIAssembly::API_CORE, true);
  648. }
  649. if (editor_api_assembly_out_of_sync) {
  650. ERR_PRINT(OUT_OF_SYNC_ERR_MESSAGE(EDITOR_API_ASSEMBLY_NAME));
  651. metadata_set_api_assembly_invalidated(APIAssembly::API_EDITOR, true);
  652. }
  653. err = _unload_scripts_domain();
  654. if (err != OK) {
  655. WARN_PRINT("Mono: Failed to unload scripts domain");
  656. }
  657. return ERR_CANT_RESOLVE;
  658. #else
  659. ERR_PRINT("The loaded API assembly is invalid");
  660. CRASH_NOW();
  661. #endif
  662. } else {
  663. return ERR_CANT_OPEN;
  664. }
  665. }
  666. if (!_load_project_assembly()) {
  667. return ERR_CANT_OPEN;
  668. }
  669. #else
  670. print_verbose("Mono: Glue disabled, ignoring script assemblies.");
  671. #endif // MONO_GLUE_ENABLED
  672. return OK;
  673. }
  674. #endif
  675. Error GDMono::finalize_and_unload_domain(MonoDomain *p_domain) {
  676. CRASH_COND(p_domain == NULL);
  677. String domain_name = mono_domain_get_friendly_name(p_domain);
  678. print_verbose("Mono: Unloading domain `" + domain_name + "`...");
  679. if (mono_domain_get() == p_domain)
  680. mono_domain_set(root_domain, true);
  681. mono_gc_collect(mono_gc_max_generation());
  682. if (!mono_domain_finalize(p_domain, 2000)) {
  683. ERR_PRINT("Mono: Domain finalization timeout");
  684. }
  685. mono_gc_collect(mono_gc_max_generation());
  686. _domain_assemblies_cleanup(mono_domain_get_id(p_domain));
  687. MonoException *exc = NULL;
  688. mono_domain_try_unload(p_domain, (MonoObject **)&exc);
  689. if (exc) {
  690. ERR_PRINTS("Exception thrown when unloading domain `" + domain_name + "`");
  691. GDMonoUtils::debug_unhandled_exception(exc);
  692. return FAILED;
  693. }
  694. return OK;
  695. }
  696. GDMonoClass *GDMono::get_class(MonoClass *p_raw_class) {
  697. MonoImage *image = mono_class_get_image(p_raw_class);
  698. if (image == corlib_assembly->get_image())
  699. return corlib_assembly->get_class(p_raw_class);
  700. uint32_t domain_id = mono_domain_get_id(mono_domain_get());
  701. HashMap<String, GDMonoAssembly *> &domain_assemblies = assemblies[domain_id];
  702. const String *k = NULL;
  703. while ((k = domain_assemblies.next(k))) {
  704. GDMonoAssembly *assembly = domain_assemblies.get(*k);
  705. if (assembly->get_image() == image) {
  706. GDMonoClass *klass = assembly->get_class(p_raw_class);
  707. if (klass)
  708. return klass;
  709. }
  710. }
  711. return NULL;
  712. }
  713. void GDMono::_domain_assemblies_cleanup(uint32_t p_domain_id) {
  714. HashMap<String, GDMonoAssembly *> &domain_assemblies = assemblies[p_domain_id];
  715. const String *k = NULL;
  716. while ((k = domain_assemblies.next(k))) {
  717. memdelete(domain_assemblies.get(*k));
  718. }
  719. assemblies.erase(p_domain_id);
  720. }
  721. void GDMono::unhandled_exception_hook(MonoObject *p_exc, void *) {
  722. // This method will be called by the runtime when a thrown exception is not handled.
  723. // It won't be called when we manually treat a thrown exception as unhandled.
  724. // We assume the exception was already printed before calling this hook.
  725. #ifdef DEBUG_ENABLED
  726. GDMonoUtils::debug_send_unhandled_exception_error((MonoException *)p_exc);
  727. if (ScriptDebugger::get_singleton())
  728. ScriptDebugger::get_singleton()->idle_poll();
  729. #endif
  730. abort();
  731. GD_UNREACHABLE();
  732. }
  733. GDMono::GDMono() {
  734. singleton = this;
  735. gdmono_log = memnew(GDMonoLog);
  736. runtime_initialized = false;
  737. finalizing_scripts_domain = false;
  738. root_domain = NULL;
  739. scripts_domain = NULL;
  740. #ifdef TOOLS_ENABLED
  741. tools_domain = NULL;
  742. #endif
  743. core_api_assembly_out_of_sync = false;
  744. #ifdef TOOLS_ENABLED
  745. editor_api_assembly_out_of_sync = false;
  746. #endif
  747. corlib_assembly = NULL;
  748. core_api_assembly = NULL;
  749. project_assembly = NULL;
  750. #ifdef TOOLS_ENABLED
  751. editor_api_assembly = NULL;
  752. editor_tools_assembly = NULL;
  753. #endif
  754. api_core_hash = 0;
  755. #ifdef TOOLS_ENABLED
  756. api_editor_hash = 0;
  757. #endif
  758. }
  759. GDMono::~GDMono() {
  760. if (is_runtime_initialized()) {
  761. if (scripts_domain) {
  762. Error err = _unload_scripts_domain();
  763. if (err != OK) {
  764. WARN_PRINT("Mono: Failed to unload scripts domain");
  765. }
  766. }
  767. const uint32_t *k = NULL;
  768. while ((k = assemblies.next(k))) {
  769. HashMap<String, GDMonoAssembly *> &domain_assemblies = assemblies.get(*k);
  770. const String *kk = NULL;
  771. while ((kk = domain_assemblies.next(kk))) {
  772. memdelete(domain_assemblies.get(*kk));
  773. }
  774. }
  775. assemblies.clear();
  776. GDMonoUtils::clear_cache();
  777. print_verbose("Mono: Runtime cleanup...");
  778. mono_jit_cleanup(root_domain);
  779. runtime_initialized = false;
  780. }
  781. if (gdmono_log)
  782. memdelete(gdmono_log);
  783. singleton = NULL;
  784. }
  785. _GodotSharp *_GodotSharp::singleton = NULL;
  786. void _GodotSharp::attach_thread() {
  787. GDMonoUtils::attach_current_thread();
  788. }
  789. void _GodotSharp::detach_thread() {
  790. GDMonoUtils::detach_current_thread();
  791. }
  792. int32_t _GodotSharp::get_domain_id() {
  793. MonoDomain *domain = mono_domain_get();
  794. CRASH_COND(!domain); // User must check if runtime is initialized before calling this method
  795. return mono_domain_get_id(domain);
  796. }
  797. int32_t _GodotSharp::get_scripts_domain_id() {
  798. MonoDomain *domain = SCRIPTS_DOMAIN;
  799. CRASH_COND(!domain); // User must check if scripts domain is loaded before calling this method
  800. return mono_domain_get_id(domain);
  801. }
  802. bool _GodotSharp::is_scripts_domain_loaded() {
  803. return GDMono::get_singleton()->is_runtime_initialized() && SCRIPTS_DOMAIN != NULL;
  804. }
  805. bool _GodotSharp::_is_domain_finalizing_for_unload(int32_t p_domain_id) {
  806. return is_domain_finalizing_for_unload(p_domain_id);
  807. }
  808. bool _GodotSharp::is_domain_finalizing_for_unload() {
  809. return is_domain_finalizing_for_unload(mono_domain_get());
  810. }
  811. bool _GodotSharp::is_domain_finalizing_for_unload(int32_t p_domain_id) {
  812. return is_domain_finalizing_for_unload(mono_domain_get_by_id(p_domain_id));
  813. }
  814. bool _GodotSharp::is_domain_finalizing_for_unload(MonoDomain *p_domain) {
  815. if (!p_domain)
  816. return true;
  817. if (p_domain == SCRIPTS_DOMAIN && GDMono::get_singleton()->is_finalizing_scripts_domain())
  818. return true;
  819. return mono_domain_is_unloading(p_domain);
  820. }
  821. bool _GodotSharp::is_runtime_shutting_down() {
  822. return mono_runtime_is_shutting_down();
  823. }
  824. bool _GodotSharp::is_runtime_initialized() {
  825. return GDMono::get_singleton()->is_runtime_initialized();
  826. }
  827. void _GodotSharp::_bind_methods() {
  828. ClassDB::bind_method(D_METHOD("attach_thread"), &_GodotSharp::attach_thread);
  829. ClassDB::bind_method(D_METHOD("detach_thread"), &_GodotSharp::detach_thread);
  830. ClassDB::bind_method(D_METHOD("get_domain_id"), &_GodotSharp::get_domain_id);
  831. ClassDB::bind_method(D_METHOD("get_scripts_domain_id"), &_GodotSharp::get_scripts_domain_id);
  832. ClassDB::bind_method(D_METHOD("is_scripts_domain_loaded"), &_GodotSharp::is_scripts_domain_loaded);
  833. ClassDB::bind_method(D_METHOD("is_domain_finalizing_for_unload", "domain_id"), &_GodotSharp::_is_domain_finalizing_for_unload);
  834. ClassDB::bind_method(D_METHOD("is_runtime_shutting_down"), &_GodotSharp::is_runtime_shutting_down);
  835. ClassDB::bind_method(D_METHOD("is_runtime_initialized"), &_GodotSharp::is_runtime_initialized);
  836. }
  837. _GodotSharp::_GodotSharp() {
  838. singleton = this;
  839. }
  840. _GodotSharp::~_GodotSharp() {
  841. singleton = NULL;
  842. }