diff options
Diffstat (limited to 'modules/mono/mono_gd/gd_mono.cpp')
-rw-r--r-- | modules/mono/mono_gd/gd_mono.cpp | 242 |
1 files changed, 177 insertions, 65 deletions
diff --git a/modules/mono/mono_gd/gd_mono.cpp b/modules/mono/mono_gd/gd_mono.cpp index a80155bd89..19e49d29f9 100644 --- a/modules/mono/mono_gd/gd_mono.cpp +++ b/modules/mono/mono_gd/gd_mono.cpp @@ -5,8 +5,8 @@ /* GODOT ENGINE */ /* https://godotengine.org */ /*************************************************************************/ -/* Copyright (c) 2007-2018 Juan Linietsky, Ariel Manzur. */ -/* Copyright (c) 2014-2018 Godot Engine contributors (cf. AUTHORS.md) */ +/* Copyright (c) 2007-2019 Juan Linietsky, Ariel Manzur. */ +/* Copyright (c) 2014-2019 Godot Engine contributors (cf. AUTHORS.md) */ /* */ /* Permission is hereby granted, free of charge, to any person obtaining */ /* a copy of this software and associated documentation files (the */ @@ -30,10 +30,12 @@ #include "gd_mono.h" +#include <mono/metadata/environment.h> #include <mono/metadata/exception.h> #include <mono/metadata/mono-config.h> #include <mono/metadata/mono-debug.h> #include <mono/metadata/mono-gc.h> +#include <mono/metadata/profiler.h> #include "core/os/dir_access.h" #include "core/os/file_access.h" @@ -54,16 +56,9 @@ #include "main/main.h" #endif -#ifdef MONO_PRINT_HANDLER_ENABLED -void gdmono_MonoPrintCallback(const char *string, mono_bool is_stdout) { - - if (is_stdout) { - OS::get_singleton()->print(string); - } else { - OS::get_singleton()->printerr(string); - } -} -#endif +#define OUT_OF_SYNC_ERR_MESSAGE(m_assembly_name) "The assembly '" m_assembly_name "' is out of sync. " \ + "This error is expected if you just upgraded to a newer Godot version. " \ + "Building the project will update the assembly to the correct version." GDMono *GDMono::singleton = NULL; @@ -90,6 +85,14 @@ void setup_runtime_main_args() { mono_runtime_set_main_args(main_args.size(), main_args.ptrw()); } +void gdmono_profiler_init() { + String profiler_args = GLOBAL_DEF("mono/profiler/args", "log:calls,alloc,sample,output=output.mlpd"); + bool profiler_enabled = GLOBAL_DEF("mono/profiler/enabled", false); + if (profiler_enabled) { + mono_profiler_load(profiler_args.utf8()); + } +} + #ifdef DEBUG_ENABLED static bool _wait_for_debugger_msecs(uint32_t p_msecs) { @@ -102,7 +105,7 @@ static bool _wait_for_debugger_msecs(uint32_t p_msecs) { OS::get_singleton()->delay_usec((p_msecs < 25 ? p_msecs : 25) * 1000); - int tdiff = OS::get_singleton()->get_ticks_msec() - last_tick; + uint32_t tdiff = OS::get_singleton()->get_ticks_msec() - last_tick; if (tdiff > p_msecs) { p_msecs = 0; @@ -130,9 +133,14 @@ void gdmono_debug_init() { } #endif - CharString da_args = String("--debugger-agent=transport=dt_socket,address=127.0.0.1:" + itos(da_port) + - ",embedding=1,server=y,suspend=" + (da_suspend ? "y,timeout=" + itos(da_timeout) : "n")) - .utf8(); + CharString da_args = OS::get_singleton()->get_environment("GODOT_MONO_DEBUGGER_AGENT").utf8(); + + if (da_args.length() == 0) { + da_args = String("--debugger-agent=transport=dt_socket,address=127.0.0.1:" + itos(da_port) + + ",embedding=1,server=y,suspend=" + (da_suspend ? "y,timeout=" + itos(da_timeout) : "n")) + .utf8(); + } + // --debugger-agent=help const char *options[] = { "--soft-breakpoints", @@ -145,6 +153,50 @@ void gdmono_debug_init() { } // namespace +void GDMono::add_mono_shared_libs_dir_to_path() { + // By default Mono seems to search shared libraries in the following directories: + // Current working directory, @executable_path@ and PATH + // The parent directory of the image file (assembly where the dllimport method is declared) + // @executable_path@/../lib + // @executable_path@/../Libraries (__MACH__ only) + + // This does not work when embedding Mono unless we use the same directory structure. + // To fix this we append the directory containing our shared libraries to PATH. + +#if defined(WINDOWS_ENABLED) || defined(UNIX_ENABLED) + String path_var("PATH"); + String path_value = OS::get_singleton()->get_environment(path_var); + +#ifdef WINDOWS_ENABLED + path_value += ';'; + + String bundled_bin_dir = GodotSharpDirs::get_data_mono_bin_dir(); +#ifdef TOOLS_ENABLED + if (DirAccess::exists(bundled_bin_dir)) { + path_value += bundled_bin_dir; + } else { + path_value += mono_reg_info.bin_dir; + } +#else + if (DirAccess::exists(bundled_bin_dir)) + path_value += bundled_bin_dir; +#endif // TOOLS_ENABLED + +#else + path_value += ':'; + + String bundled_lib_dir = GodotSharpDirs::get_data_mono_lib_dir(); + if (DirAccess::exists(bundled_lib_dir)) { + path_value += bundled_lib_dir; + } else { + // TODO: Do we need to add the lib dir when using the system installed Mono on Unix platforms? + } +#endif // WINDOWS_ENABLED + + OS::get_singleton()->set_environment(path_var, path_value); +#endif // WINDOWS_ENABLED || UNIX_ENABLED +} + void GDMono::initialize() { ERR_FAIL_NULL(Engine::get_singleton()); @@ -157,16 +209,11 @@ void GDMono::initialize() { GDMonoLog::get_singleton()->initialize(); -#ifdef MONO_PRINT_HANDLER_ENABLED - mono_trace_set_print_handler(gdmono_MonoPrintCallback); - mono_trace_set_printerr_handler(gdmono_MonoPrintCallback); -#endif - String assembly_rootdir; String config_dir; #ifdef TOOLS_ENABLED -#ifdef WINDOWS_ENABLED +#if defined(WINDOWS_ENABLED) mono_reg_info = MonoRegUtils::find_mono(); if (mono_reg_info.assembly_dir.length() && DirAccess::exists(mono_reg_info.assembly_dir)) { @@ -176,7 +223,7 @@ void GDMono::initialize() { if (mono_reg_info.config_dir.length() && DirAccess::exists(mono_reg_info.config_dir)) { config_dir = mono_reg_info.config_dir; } -#elif OSX_ENABLED +#elif defined(OSX_ENABLED) const char *c_assembly_rootdir = mono_assembly_getrootdir(); const char *c_config_dir = mono_get_config_dir(); @@ -208,18 +255,32 @@ void GDMono::initialize() { assembly_rootdir = bundled_assembly_rootdir; config_dir = bundled_config_dir; } + +#ifdef WINDOWS_ENABLED + if (assembly_rootdir.empty() || config_dir.empty()) { + // Assertion: if they are not set, then they weren't found in the registry + CRASH_COND(mono_reg_info.assembly_dir.length() > 0 || mono_reg_info.config_dir.length() > 0); + + ERR_PRINT("Cannot find Mono in the registry"); + } +#endif // WINDOWS_ENABLED + #else // These are always the directories in export templates assembly_rootdir = bundled_assembly_rootdir; config_dir = bundled_config_dir; -#endif +#endif // TOOLS_ENABLED // Leak if we call mono_set_dirs more than once mono_set_dirs(assembly_rootdir.length() ? assembly_rootdir.utf8().get_data() : NULL, config_dir.length() ? config_dir.utf8().get_data() : NULL); + add_mono_shared_libs_dir_to_path(); + GDMonoAssembly::initialize(); + gdmono_profiler_init(); + #ifdef DEBUG_ENABLED gdmono_debug_init(); #endif @@ -228,6 +289,29 @@ void GDMono::initialize() { mono_install_unhandled_exception_hook(&unhandled_exception_hook, NULL); +#ifndef TOOLS_ENABLED + if (!DirAccess::exists("res://.mono")) { + // 'res://.mono/' is missing so there is nothing to load. We don't need to initialize mono, but + // we still do so unless mscorlib is missing (which is the case for projects that don't use C#). + + String mscorlib_fname("mscorlib.dll"); + + Vector<String> search_dirs; + GDMonoAssembly::fill_search_dirs(search_dirs); + + bool found = false; + for (int i = 0; i < search_dirs.size(); i++) { + if (FileAccess::exists(search_dirs[i].plus_file(mscorlib_fname))) { + found = true; + break; + } + } + + if (!found) + return; // mscorlib is missing, do not initialize mono + } +#endif + root_domain = mono_jit_init_version("GodotEngine.RootDomain", "v4.0.30319"); ERR_EXPLAIN("Mono: Failed to initialize runtime"); @@ -285,15 +369,15 @@ void GDMono::initialize() { // metadata, so we invalidate the version in the metadata and unload the script domain. if (core_api_assembly_out_of_sync) { - ERR_PRINT("The loaded Core API assembly is out of sync"); + ERR_PRINT(OUT_OF_SYNC_ERR_MESSAGE(CORE_API_ASSEMBLY_NAME)); metadata_set_api_assembly_invalidated(APIAssembly::API_CORE, true); } else if (!GDMonoUtils::mono_cache.godot_api_cache_updated) { - ERR_PRINT("The loaded Core API assembly is in sync, but the cache update failed"); + ERR_PRINT("The loaded assembly '" CORE_API_ASSEMBLY_NAME "' is in sync, but the cache update failed"); metadata_set_api_assembly_invalidated(APIAssembly::API_CORE, true); } if (editor_api_assembly_out_of_sync) { - ERR_PRINT("The loaded Editor API assembly is out of sync"); + ERR_PRINT(OUT_OF_SYNC_ERR_MESSAGE(EDITOR_API_ASSEMBLY_NAME)); metadata_set_api_assembly_invalidated(APIAssembly::API_EDITOR, true); } @@ -322,7 +406,7 @@ namespace GodotSharpBindings { uint64_t get_core_api_hash(); #ifdef TOOLS_ENABLED uint64_t get_editor_api_hash(); -#endif // TOOLS_ENABLED +#endif uint32_t get_bindings_version(); void register_generated_icalls(); @@ -339,29 +423,20 @@ void GDMono::_register_internal_calls() { #endif } -#ifdef DEBUG_METHODS_ENABLED void GDMono::_initialize_and_check_api_hashes() { - api_core_hash = ClassDB::get_api_hash(ClassDB::API_CORE); - #ifdef MONO_GLUE_ENABLED - if (api_core_hash != GodotSharpBindings::get_core_api_hash()) { + if (get_api_core_hash() != GodotSharpBindings::get_core_api_hash()) { ERR_PRINT("Mono: Core API hash mismatch!"); } -#endif #ifdef TOOLS_ENABLED - api_editor_hash = ClassDB::get_api_hash(ClassDB::API_EDITOR); - -#ifdef MONO_GLUE_ENABLED - if (api_editor_hash != GodotSharpBindings::get_editor_api_hash()) { + if (get_api_editor_hash() != GodotSharpBindings::get_editor_api_hash()) { ERR_PRINT("Mono: Editor API hash mismatch!"); } -#endif - #endif // TOOLS_ENABLED +#endif // MONO_GLUE_ENABLED } -#endif // DEBUG_METHODS_ENABLED void GDMono::add_assembly(uint32_t p_domain_id, GDMonoAssembly *p_assembly) { @@ -513,6 +588,8 @@ bool GDMono::_load_core_api_assembly() { CS_GLUE_VERSION != api_assembly_ver.cs_glue_version; if (!core_api_assembly_out_of_sync) { GDMonoUtils::update_godot_api_cache(); + + _install_trace_listener(); } #else GDMonoUtils::update_godot_api_cache(); @@ -581,8 +658,6 @@ bool GDMono::_load_project_assembly() { if (success) { mono_assembly_set_main(project_assembly->get_assembly()); - - CSharpLanguage::get_singleton()->project_assembly_loaded(); } else { if (OS::get_singleton()->is_stdout_verbose()) print_error("Mono: Failed to load project assembly"); @@ -616,6 +691,23 @@ bool GDMono::_load_api_assemblies() { return true; } +void GDMono::_install_trace_listener() { + +#ifdef DEBUG_ENABLED + // Install the trace listener now before the project assembly is loaded + typedef void (*DebuggingUtils_InstallTraceListener)(MonoObject **); + MonoException *exc = NULL; + GDMonoClass *debug_utils = core_api_assembly->get_class(BINDINGS_NAMESPACE, "DebuggingUtils"); + DebuggingUtils_InstallTraceListener install_func = + (DebuggingUtils_InstallTraceListener)debug_utils->get_method_thunk("InstallTraceListener"); + install_func((MonoObject **)&exc); + if (exc) { + ERR_PRINT("Failed to install System.Diagnostics.Trace listener"); + GDMonoUtils::debug_print_unhandled_exception(exc); + } +#endif +} + #ifdef TOOLS_ENABLED String GDMono::_get_api_assembly_metadata_path() { @@ -705,8 +797,6 @@ Error GDMono::_unload_scripts_domain() { if (mono_domain_get() != root_domain) mono_domain_set(root_domain, true); - mono_gc_collect(mono_gc_max_generation()); - finalizing_scripts_domain = true; if (!mono_domain_finalize(scripts_domain, 2000)) { @@ -726,7 +816,9 @@ Error GDMono::_unload_scripts_domain() { #endif core_api_assembly_out_of_sync = false; +#ifdef TOOLS_ENABLED editor_api_assembly_out_of_sync = false; +#endif MonoDomain *domain = scripts_domain; scripts_domain = NULL; @@ -759,7 +851,7 @@ Error GDMono::_load_tools_domain() { } #endif -#ifdef TOOLS_ENABLED +#ifdef GD_MONO_HOT_RELOAD Error GDMono::reload_scripts_domain() { ERR_FAIL_COND_V(!runtime_initialized, ERR_BUG); @@ -772,6 +864,8 @@ Error GDMono::reload_scripts_domain() { } } + CSharpLanguage::get_singleton()->_on_scripts_domain_unloaded(); + Error err = _load_scripts_domain(); if (err != OK) { ERR_PRINT("Mono: Failed to load scripts domain"); @@ -780,14 +874,18 @@ Error GDMono::reload_scripts_domain() { #ifdef MONO_GLUE_ENABLED if (!_load_api_assemblies()) { - if ((core_api_assembly && (core_api_assembly_out_of_sync || !GDMonoUtils::mono_cache.godot_api_cache_updated)) || - (editor_api_assembly && editor_api_assembly_out_of_sync)) { + if ((core_api_assembly && (core_api_assembly_out_of_sync || !GDMonoUtils::mono_cache.godot_api_cache_updated)) +#ifdef TOOLS_ENABLED + || (editor_api_assembly && editor_api_assembly_out_of_sync) +#endif + ) { +#ifdef TOOLS_ENABLED // The assembly was successfully loaded, but the full api could not be cached. // This is most likely an outdated assembly loaded because of an invalid version in the // metadata, so we invalidate the version in the metadata and unload the script domain. if (core_api_assembly_out_of_sync) { - ERR_PRINT("The loaded Core API assembly is out of sync"); + ERR_PRINT(OUT_OF_SYNC_ERR_MESSAGE(CORE_API_ASSEMBLY_NAME)); metadata_set_api_assembly_invalidated(APIAssembly::API_CORE, true); } else if (!GDMonoUtils::mono_cache.godot_api_cache_updated) { ERR_PRINT("The loaded Core API assembly is in sync, but the cache update failed"); @@ -795,16 +893,20 @@ Error GDMono::reload_scripts_domain() { } if (editor_api_assembly_out_of_sync) { - ERR_PRINT("The loaded Editor API assembly is out of sync"); + ERR_PRINT(OUT_OF_SYNC_ERR_MESSAGE(EDITOR_API_ASSEMBLY_NAME)); metadata_set_api_assembly_invalidated(APIAssembly::API_EDITOR, true); } - Error err = _unload_scripts_domain(); + err = _unload_scripts_domain(); if (err != OK) { WARN_PRINT("Mono: Failed to unload scripts domain"); } return ERR_CANT_RESOLVE; +#else + ERR_PRINT("The loaded API assembly is invalid"); + CRASH_NOW(); +#endif } else { return ERR_CANT_OPEN; } @@ -832,14 +934,20 @@ Error GDMono::finalize_and_unload_domain(MonoDomain *p_domain) { if (mono_domain_get() == p_domain) mono_domain_set(root_domain, true); - mono_gc_collect(mono_gc_max_generation()); if (!mono_domain_finalize(p_domain, 2000)) { ERR_PRINT("Mono: Domain finalization timeout"); } + mono_gc_collect(mono_gc_max_generation()); _domain_assemblies_cleanup(mono_domain_get_id(p_domain)); +#ifdef TOOLS_ENABLED + if (p_domain == tools_domain) { + editor_tools_assembly = NULL; + } +#endif + MonoException *exc = NULL; mono_domain_try_unload(p_domain, (MonoObject **)&exc); @@ -899,8 +1007,10 @@ void GDMono::unhandled_exception_hook(MonoObject *p_exc, void *) { if (ScriptDebugger::get_singleton()) ScriptDebugger::get_singleton()->idle_poll(); #endif - abort(); - _UNREACHABLE_(); + + exit(mono_environment_exitcode_get()); + + GD_UNREACHABLE(); } GDMono::GDMono() { @@ -919,7 +1029,9 @@ GDMono::GDMono() { #endif core_api_assembly_out_of_sync = false; +#ifdef TOOLS_ENABLED editor_api_assembly_out_of_sync = false; +#endif corlib_assembly = NULL; core_api_assembly = NULL; @@ -929,23 +1041,29 @@ GDMono::GDMono() { editor_tools_assembly = NULL; #endif -#ifdef DEBUG_METHODS_ENABLED api_core_hash = 0; #ifdef TOOLS_ENABLED api_editor_hash = 0; #endif -#endif } GDMono::~GDMono() { if (is_runtime_initialized()) { - if (scripts_domain) { +#ifdef TOOLS_ENABLED + if (tools_domain) { + Error err = finalize_and_unload_domain(tools_domain); + if (err != OK) { + ERR_PRINT("Mono: Failed to unload tools domain"); + } + } +#endif + if (scripts_domain) { Error err = _unload_scripts_domain(); if (err != OK) { - WARN_PRINT("Mono: Failed to unload scripts domain"); + ERR_PRINT("Mono: Failed to unload scripts domain"); } } @@ -966,6 +1084,8 @@ GDMono::~GDMono() { mono_jit_cleanup(root_domain); + print_verbose("Mono: Finalized"); + runtime_initialized = false; } @@ -1057,17 +1177,9 @@ void _GodotSharp::_bind_methods() { _GodotSharp::_GodotSharp() { singleton = this; - queue_empty = true; -#ifndef NO_THREADS - queue_mutex = Mutex::create(); -#endif } _GodotSharp::~_GodotSharp() { singleton = NULL; - - if (queue_mutex) { - memdelete(queue_mutex); - } } |