2017-10-02 21:24:00 +00:00
|
|
|
/*************************************************************************/
|
|
|
|
/* gd_mono.cpp */
|
|
|
|
/*************************************************************************/
|
|
|
|
/* This file is part of: */
|
|
|
|
/* GODOT ENGINE */
|
|
|
|
/* https://godotengine.org */
|
|
|
|
/*************************************************************************/
|
2020-01-01 10:16:22 +00:00
|
|
|
/* Copyright (c) 2007-2020 Juan Linietsky, Ariel Manzur. */
|
|
|
|
/* Copyright (c) 2014-2020 Godot Engine contributors (cf. AUTHORS.md). */
|
2017-10-02 21:24:00 +00:00
|
|
|
/* */
|
|
|
|
/* Permission is hereby granted, free of charge, to any person obtaining */
|
|
|
|
/* a copy of this software and associated documentation files (the */
|
|
|
|
/* "Software"), to deal in the Software without restriction, including */
|
|
|
|
/* without limitation the rights to use, copy, modify, merge, publish, */
|
|
|
|
/* distribute, sublicense, and/or sell copies of the Software, and to */
|
|
|
|
/* permit persons to whom the Software is furnished to do so, subject to */
|
|
|
|
/* the following conditions: */
|
|
|
|
/* */
|
|
|
|
/* The above copyright notice and this permission notice shall be */
|
|
|
|
/* included in all copies or substantial portions of the Software. */
|
|
|
|
/* */
|
|
|
|
/* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, */
|
|
|
|
/* EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF */
|
|
|
|
/* MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.*/
|
|
|
|
/* IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY */
|
|
|
|
/* CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, */
|
|
|
|
/* TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE */
|
|
|
|
/* SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. */
|
|
|
|
/*************************************************************************/
|
2018-01-04 23:50:27 +00:00
|
|
|
|
2017-10-02 21:24:00 +00:00
|
|
|
#include "gd_mono.h"
|
|
|
|
|
2019-04-18 11:58:55 +00:00
|
|
|
#include <mono/metadata/environment.h>
|
2017-10-22 22:35:04 +00:00
|
|
|
#include <mono/metadata/exception.h>
|
2017-10-02 21:24:00 +00:00
|
|
|
#include <mono/metadata/mono-config.h>
|
|
|
|
#include <mono/metadata/mono-debug.h>
|
2017-10-04 22:12:36 +00:00
|
|
|
#include <mono/metadata/mono-gc.h>
|
2019-03-03 00:57:35 +00:00
|
|
|
#include <mono/metadata/profiler.h>
|
2017-10-02 21:24:00 +00:00
|
|
|
|
2020-02-27 02:30:20 +00:00
|
|
|
#include "core/debugger/engine_debugger.h"
|
2018-09-11 16:13:45 +00:00
|
|
|
#include "core/os/dir_access.h"
|
|
|
|
#include "core/os/file_access.h"
|
|
|
|
#include "core/os/os.h"
|
|
|
|
#include "core/os/thread.h"
|
|
|
|
#include "core/project_settings.h"
|
2017-10-02 21:24:00 +00:00
|
|
|
|
|
|
|
#include "../csharp_script.h"
|
2018-02-22 12:13:51 +00:00
|
|
|
#include "../godotsharp_dirs.h"
|
2017-10-02 21:24:00 +00:00
|
|
|
#include "../utils/path_utils.h"
|
2019-11-10 16:10:38 +00:00
|
|
|
#include "gd_mono_cache.h"
|
2018-02-22 12:13:51 +00:00
|
|
|
#include "gd_mono_class.h"
|
|
|
|
#include "gd_mono_marshal.h"
|
2017-10-02 21:24:00 +00:00
|
|
|
#include "gd_mono_utils.h"
|
|
|
|
|
|
|
|
#ifdef TOOLS_ENABLED
|
2018-02-16 15:00:54 +00:00
|
|
|
#include "main/main.h"
|
2017-10-02 21:24:00 +00:00
|
|
|
#endif
|
|
|
|
|
2019-07-03 15:41:07 +00:00
|
|
|
#ifdef ANDROID_ENABLED
|
2019-08-26 15:46:57 +00:00
|
|
|
#include "android_mono_config.h"
|
2020-03-18 16:40:04 +00:00
|
|
|
#include "support/android_support.h"
|
|
|
|
#elif defined(IPHONE_ENABLED)
|
|
|
|
#include "support/ios_support.h"
|
|
|
|
#endif
|
|
|
|
|
|
|
|
#if defined(TOOL_ENABLED) && defined(GD_MONO_SINGLE_APPDOMAIN)
|
|
|
|
// This will no longer be the case if we replace appdomains with AssemblyLoadContext
|
|
|
|
#error "Editor build requires support for multiple appdomains"
|
|
|
|
#endif
|
|
|
|
|
|
|
|
#if defined(GD_MONO_HOT_RELOAD) && defined(GD_MONO_SINGLE_APPDOMAIN)
|
|
|
|
#error "Hot reloading requires multiple appdomains"
|
2019-07-03 15:41:07 +00:00
|
|
|
#endif
|
|
|
|
|
2019-11-10 16:10:38 +00:00
|
|
|
// TODO:
|
|
|
|
// This has turn into a gigantic mess. There's too much going on here. Too much #ifdef as well.
|
|
|
|
// It's just painful to read... It needs to be re-structured. Please, clean this up, future me.
|
|
|
|
|
2020-04-01 23:20:12 +00:00
|
|
|
GDMono *GDMono::singleton = nullptr;
|
2017-10-02 21:24:00 +00:00
|
|
|
|
2018-03-18 22:02:06 +00:00
|
|
|
namespace {
|
|
|
|
|
2019-11-10 16:10:38 +00:00
|
|
|
#if defined(JAVASCRIPT_ENABLED)
|
|
|
|
extern "C" {
|
|
|
|
void mono_wasm_load_runtime(const char *managed_path, int enable_debugging);
|
|
|
|
}
|
|
|
|
#endif
|
|
|
|
|
|
|
|
#if !defined(JAVASCRIPT_ENABLED)
|
|
|
|
|
|
|
|
void gd_mono_setup_runtime_main_args() {
|
2018-03-18 22:02:06 +00:00
|
|
|
CharString execpath = OS::get_singleton()->get_executable_path().utf8();
|
|
|
|
|
|
|
|
List<String> cmdline_args = OS::get_singleton()->get_cmdline_args();
|
|
|
|
|
|
|
|
List<CharString> cmdline_args_utf8;
|
|
|
|
Vector<char *> main_args;
|
|
|
|
main_args.resize(cmdline_args.size() + 1);
|
|
|
|
|
2018-07-25 01:11:03 +00:00
|
|
|
main_args.write[0] = execpath.ptrw();
|
2018-03-18 22:02:06 +00:00
|
|
|
|
|
|
|
int i = 1;
|
|
|
|
for (List<String>::Element *E = cmdline_args.front(); E; E = E->next()) {
|
|
|
|
CharString &stored = cmdline_args_utf8.push_back(E->get().utf8())->get();
|
2018-07-25 01:11:03 +00:00
|
|
|
main_args.write[i] = stored.ptrw();
|
2018-03-18 22:02:06 +00:00
|
|
|
i++;
|
|
|
|
}
|
|
|
|
|
|
|
|
mono_runtime_set_main_args(main_args.size(), main_args.ptrw());
|
|
|
|
}
|
|
|
|
|
2019-11-10 16:10:38 +00:00
|
|
|
void gd_mono_profiler_init() {
|
2019-03-03 00:57:35 +00:00
|
|
|
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());
|
2019-12-16 19:34:30 +00:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
const String env_var_name = "MONO_ENV_OPTIONS";
|
|
|
|
if (OS::get_singleton()->has_environment(env_var_name)) {
|
|
|
|
const auto mono_env_ops = OS::get_singleton()->get_environment(env_var_name);
|
|
|
|
// Usually MONO_ENV_OPTIONS looks like: --profile=jb:prof=timeline,ctl=remote,host=127.0.0.1:55467
|
|
|
|
const String prefix = "--profile=";
|
|
|
|
if (mono_env_ops.begins_with(prefix)) {
|
|
|
|
const auto ops = mono_env_ops.substr(prefix.length(), mono_env_ops.length());
|
|
|
|
mono_profiler_load(ops.utf8());
|
|
|
|
}
|
2019-03-03 00:57:35 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-11-10 16:10:38 +00:00
|
|
|
void gd_mono_debug_init() {
|
2019-09-25 20:02:24 +00:00
|
|
|
CharString da_args = OS::get_singleton()->get_environment("GODOT_MONO_DEBUGGER_AGENT").utf8();
|
|
|
|
|
2020-05-09 18:45:43 +00:00
|
|
|
if (da_args.length()) {
|
|
|
|
OS::get_singleton()->set_environment("GODOT_MONO_DEBUGGER_AGENT", String());
|
|
|
|
}
|
|
|
|
|
2019-09-25 20:02:24 +00:00
|
|
|
#ifdef TOOLS_ENABLED
|
2017-10-02 21:24:00 +00:00
|
|
|
int da_port = GLOBAL_DEF("mono/debugger_agent/port", 23685);
|
|
|
|
bool da_suspend = GLOBAL_DEF("mono/debugger_agent/wait_for_debugger", false);
|
|
|
|
int da_timeout = GLOBAL_DEF("mono/debugger_agent/wait_timeout", 3000);
|
|
|
|
|
|
|
|
if (Engine::get_singleton()->is_editor_hint() ||
|
|
|
|
ProjectSettings::get_singleton()->get_resource_path().empty() ||
|
2018-02-16 15:00:54 +00:00
|
|
|
Main::is_project_manager()) {
|
2019-07-03 07:44:53 +00:00
|
|
|
if (da_args.size() == 0)
|
|
|
|
return;
|
2017-10-02 21:24:00 +00:00
|
|
|
}
|
|
|
|
|
2019-02-03 05:51:07 +00:00
|
|
|
if (da_args.length() == 0) {
|
2019-01-21 21:44:05 +00:00
|
|
|
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();
|
|
|
|
}
|
2019-09-25 20:02:24 +00:00
|
|
|
#else
|
|
|
|
if (da_args.length() == 0)
|
|
|
|
return; // Exported games don't use the project settings to setup the debugger agent
|
|
|
|
#endif
|
2019-01-21 21:44:05 +00:00
|
|
|
|
2020-04-22 14:44:03 +00:00
|
|
|
// Debugging enabled
|
|
|
|
|
|
|
|
mono_debug_init(MONO_DEBUG_FORMAT_MONO);
|
|
|
|
|
2017-10-02 21:24:00 +00:00
|
|
|
// --debugger-agent=help
|
|
|
|
const char *options[] = {
|
|
|
|
"--soft-breakpoints",
|
|
|
|
da_args.get_data()
|
|
|
|
};
|
|
|
|
mono_jit_parse_options(2, (char **)options);
|
|
|
|
}
|
2018-03-18 22:02:06 +00:00
|
|
|
|
2019-11-10 16:10:38 +00:00
|
|
|
#endif // !defined(JAVASCRIPT_ENABLED)
|
|
|
|
|
|
|
|
#if defined(JAVASCRIPT_ENABLED)
|
|
|
|
MonoDomain *gd_initialize_mono_runtime() {
|
|
|
|
const char *vfs_prefix = "managed";
|
|
|
|
int enable_debugging = 0;
|
|
|
|
|
2020-04-22 14:44:03 +00:00
|
|
|
// TODO: Provide a way to enable debugging on WASM release builds.
|
2019-11-10 16:10:38 +00:00
|
|
|
#ifdef DEBUG_ENABLED
|
|
|
|
enable_debugging = 1;
|
|
|
|
#endif
|
|
|
|
|
|
|
|
mono_wasm_load_runtime(vfs_prefix, enable_debugging);
|
|
|
|
|
|
|
|
return mono_get_root_domain();
|
|
|
|
}
|
|
|
|
#else
|
|
|
|
MonoDomain *gd_initialize_mono_runtime() {
|
|
|
|
gd_mono_debug_init();
|
|
|
|
|
2020-03-18 16:40:04 +00:00
|
|
|
#if defined(IPHONE_ENABLED) || defined(ANDROID_ENABLED)
|
|
|
|
// I don't know whether this actually matters or not
|
|
|
|
const char *runtime_version = "mobile";
|
|
|
|
#else
|
|
|
|
const char *runtime_version = "v4.0.30319";
|
|
|
|
#endif
|
|
|
|
|
|
|
|
return mono_jit_init_version("GodotEngine.RootDomain", runtime_version);
|
2019-11-10 16:10:38 +00:00
|
|
|
}
|
2017-10-02 21:24:00 +00:00
|
|
|
#endif
|
|
|
|
|
2018-03-18 22:02:06 +00:00
|
|
|
} // namespace
|
|
|
|
|
2019-01-29 22:00:30 +00:00
|
|
|
void GDMono::add_mono_shared_libs_dir_to_path() {
|
2019-11-10 16:10:38 +00:00
|
|
|
// TODO: Replace this with a mono_dl_fallback
|
|
|
|
|
2019-01-29 22:00:30 +00:00
|
|
|
// 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();
|
2019-02-15 17:49:04 +00:00
|
|
|
#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
|
|
|
|
|
2019-01-29 22:00:30 +00:00
|
|
|
#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?
|
|
|
|
}
|
2019-02-15 17:49:04 +00:00
|
|
|
#endif // WINDOWS_ENABLED
|
2019-01-29 22:00:30 +00:00
|
|
|
|
|
|
|
OS::get_singleton()->set_environment(path_var, path_value);
|
2019-02-15 17:49:04 +00:00
|
|
|
#endif // WINDOWS_ENABLED || UNIX_ENABLED
|
2019-01-29 22:00:30 +00:00
|
|
|
}
|
|
|
|
|
2019-11-28 22:42:37 +00:00
|
|
|
void GDMono::determine_mono_dirs(String &r_assembly_rootdir, String &r_config_dir) {
|
|
|
|
String bundled_assembly_rootdir = GodotSharpDirs::get_data_mono_lib_dir();
|
|
|
|
String bundled_config_dir = GodotSharpDirs::get_data_mono_etc_dir();
|
2018-10-03 17:01:57 +00:00
|
|
|
|
|
|
|
#ifdef TOOLS_ENABLED
|
2019-11-28 22:42:37 +00:00
|
|
|
|
2019-04-05 21:20:20 +00:00
|
|
|
#if defined(WINDOWS_ENABLED)
|
2017-10-02 21:24:00 +00:00
|
|
|
mono_reg_info = MonoRegUtils::find_mono();
|
|
|
|
|
|
|
|
if (mono_reg_info.assembly_dir.length() && DirAccess::exists(mono_reg_info.assembly_dir)) {
|
2019-11-28 22:42:37 +00:00
|
|
|
r_assembly_rootdir = mono_reg_info.assembly_dir;
|
2017-10-02 21:24:00 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
if (mono_reg_info.config_dir.length() && DirAccess::exists(mono_reg_info.config_dir)) {
|
2019-11-28 22:42:37 +00:00
|
|
|
r_config_dir = mono_reg_info.config_dir;
|
2017-10-02 21:24:00 +00:00
|
|
|
}
|
2019-04-05 21:20:20 +00:00
|
|
|
#elif defined(OSX_ENABLED)
|
2018-10-03 17:01:57 +00:00
|
|
|
const char *c_assembly_rootdir = mono_assembly_getrootdir();
|
|
|
|
const char *c_config_dir = mono_get_config_dir();
|
|
|
|
|
|
|
|
if (!c_assembly_rootdir || !c_config_dir || !DirAccess::exists(c_assembly_rootdir) || !DirAccess::exists(c_config_dir)) {
|
|
|
|
Vector<const char *> locations;
|
|
|
|
locations.push_back("/Library/Frameworks/Mono.framework/Versions/Current/");
|
|
|
|
locations.push_back("/usr/local/var/homebrew/linked/mono/");
|
|
|
|
|
|
|
|
for (int i = 0; i < locations.size(); i++) {
|
2019-07-08 13:16:35 +00:00
|
|
|
String hint_assembly_rootdir = path::join(locations[i], "lib");
|
|
|
|
String hint_mscorlib_path = path::join(hint_assembly_rootdir, "mono", "4.5", "mscorlib.dll");
|
|
|
|
String hint_config_dir = path::join(locations[i], "etc");
|
2018-10-03 17:01:57 +00:00
|
|
|
|
|
|
|
if (FileAccess::exists(hint_mscorlib_path) && DirAccess::exists(hint_config_dir)) {
|
2019-11-28 22:42:37 +00:00
|
|
|
r_assembly_rootdir = hint_assembly_rootdir;
|
|
|
|
r_config_dir = hint_config_dir;
|
2018-10-03 17:01:57 +00:00
|
|
|
break;
|
2018-09-06 17:38:24 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2018-10-03 17:01:57 +00:00
|
|
|
#endif
|
|
|
|
|
2019-05-20 16:34:35 +00:00
|
|
|
if (DirAccess::exists(bundled_assembly_rootdir)) {
|
2019-11-28 22:42:37 +00:00
|
|
|
r_assembly_rootdir = bundled_assembly_rootdir;
|
2019-05-20 16:34:35 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
if (DirAccess::exists(bundled_config_dir)) {
|
2019-11-28 22:42:37 +00:00
|
|
|
r_config_dir = bundled_config_dir;
|
2018-10-03 17:01:57 +00:00
|
|
|
}
|
2019-02-15 18:04:45 +00:00
|
|
|
|
|
|
|
#ifdef WINDOWS_ENABLED
|
2019-11-28 22:42:37 +00:00
|
|
|
if (r_assembly_rootdir.empty() || r_config_dir.empty()) {
|
2019-08-09 01:39:45 +00:00
|
|
|
ERR_PRINT("Cannot find Mono in the registry.");
|
2019-02-15 18:04:45 +00:00
|
|
|
// 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);
|
|
|
|
}
|
|
|
|
#endif // WINDOWS_ENABLED
|
|
|
|
|
2017-10-02 21:24:00 +00:00
|
|
|
#else
|
2019-11-28 22:42:37 +00:00
|
|
|
// Export templates always use the bundled directories
|
|
|
|
r_assembly_rootdir = bundled_assembly_rootdir;
|
|
|
|
r_config_dir = bundled_config_dir;
|
|
|
|
#endif
|
|
|
|
}
|
|
|
|
|
|
|
|
void GDMono::initialize() {
|
|
|
|
ERR_FAIL_NULL(Engine::get_singleton());
|
|
|
|
|
|
|
|
print_verbose("Mono: Initializing module...");
|
|
|
|
|
|
|
|
char *runtime_build_info = mono_get_runtime_build_info();
|
|
|
|
print_verbose("Mono JIT compiler version " + String(runtime_build_info));
|
|
|
|
mono_free(runtime_build_info);
|
|
|
|
|
|
|
|
_init_godot_api_hashes();
|
|
|
|
_init_exception_policy();
|
|
|
|
|
|
|
|
GDMonoLog::get_singleton()->initialize();
|
2017-10-02 21:24:00 +00:00
|
|
|
|
2019-11-10 16:10:38 +00:00
|
|
|
#if !defined(JAVASCRIPT_ENABLED)
|
2019-11-28 22:42:37 +00:00
|
|
|
String assembly_rootdir;
|
|
|
|
String config_dir;
|
|
|
|
determine_mono_dirs(assembly_rootdir, config_dir);
|
|
|
|
|
2018-10-03 17:01:57 +00:00
|
|
|
// Leak if we call mono_set_dirs more than once
|
2020-04-01 23:20:12 +00:00
|
|
|
mono_set_dirs(assembly_rootdir.length() ? assembly_rootdir.utf8().get_data() : nullptr,
|
|
|
|
config_dir.length() ? config_dir.utf8().get_data() : nullptr);
|
2018-10-03 17:01:57 +00:00
|
|
|
|
2019-01-29 22:00:30 +00:00
|
|
|
add_mono_shared_libs_dir_to_path();
|
2019-11-10 16:10:38 +00:00
|
|
|
#endif
|
|
|
|
|
2020-03-18 16:40:04 +00:00
|
|
|
#ifdef ANDROID_ENABLED
|
|
|
|
mono_config_parse_memory(get_godot_android_mono_config().utf8().get_data());
|
|
|
|
#else
|
2020-04-01 23:20:12 +00:00
|
|
|
mono_config_parse(nullptr);
|
2020-03-18 16:40:04 +00:00
|
|
|
#endif
|
|
|
|
|
2019-11-10 16:10:38 +00:00
|
|
|
#if defined(ANDROID_ENABLED)
|
2020-03-18 16:40:04 +00:00
|
|
|
gdmono::android::support::initialize();
|
|
|
|
#elif defined(IPHONE_ENABLED)
|
|
|
|
gdmono::ios::support::initialize();
|
2019-11-10 16:10:38 +00:00
|
|
|
#endif
|
2019-01-29 22:00:30 +00:00
|
|
|
|
2017-10-02 21:24:00 +00:00
|
|
|
GDMonoAssembly::initialize();
|
|
|
|
|
2019-11-10 16:10:38 +00:00
|
|
|
#if !defined(JAVASCRIPT_ENABLED)
|
|
|
|
gd_mono_profiler_init();
|
2017-10-02 21:24:00 +00:00
|
|
|
#endif
|
|
|
|
|
2020-04-01 23:20:12 +00:00
|
|
|
mono_install_unhandled_exception_hook(&unhandled_exception_hook, nullptr);
|
2018-06-26 19:03:42 +00:00
|
|
|
|
2019-03-09 21:55:44 +00:00
|
|
|
#ifndef TOOLS_ENABLED
|
2019-11-28 22:42:37 +00:00
|
|
|
// Exported games that don't use C# must still work. They likely don't ship with mscorlib.
|
|
|
|
// We only initialize the Mono runtime if we can find mscorlib. Otherwise it would crash.
|
|
|
|
if (GDMonoAssembly::find_assembly("mscorlib.dll").empty()) {
|
|
|
|
print_verbose("Mono: Skipping runtime initialization because 'mscorlib.dll' could not be found");
|
2019-07-11 12:01:25 +00:00
|
|
|
return;
|
2019-11-28 22:42:37 +00:00
|
|
|
}
|
2019-02-10 17:34:34 +00:00
|
|
|
#endif
|
|
|
|
|
2019-12-16 14:40:26 +00:00
|
|
|
#if !defined(NO_MONO_THREADS_SUSPEND_WORKAROUND)
|
2019-08-29 23:33:50 +00:00
|
|
|
// FIXME: Temporary workaround. See: https://github.com/godotengine/godot/issues/29812
|
|
|
|
if (!OS::get_singleton()->has_environment("MONO_THREADS_SUSPEND")) {
|
|
|
|
OS::get_singleton()->set_environment("MONO_THREADS_SUSPEND", "preemptive");
|
|
|
|
}
|
|
|
|
#endif
|
|
|
|
|
2019-12-04 14:07:00 +00:00
|
|
|
// NOTE: Internal calls must be registered after the Mono runtime initialization.
|
2020-04-01 23:20:12 +00:00
|
|
|
// Otherwise registration fails with the error: 'assertion 'hash != nullptr' failed'.
|
2019-12-04 14:07:00 +00:00
|
|
|
|
2019-11-10 16:10:38 +00:00
|
|
|
root_domain = gd_initialize_mono_runtime();
|
2019-08-09 01:39:45 +00:00
|
|
|
ERR_FAIL_NULL_MSG(root_domain, "Mono: Failed to initialize runtime.");
|
2017-10-02 21:24:00 +00:00
|
|
|
|
|
|
|
GDMonoUtils::set_main_thread(GDMonoUtils::get_current_thread());
|
|
|
|
|
2019-11-10 16:10:38 +00:00
|
|
|
#if !defined(JAVASCRIPT_ENABLED)
|
|
|
|
gd_mono_setup_runtime_main_args(); // Required for System.Environment.GetCommandLineArgs
|
|
|
|
#endif
|
2018-03-18 22:02:06 +00:00
|
|
|
|
2017-10-02 21:24:00 +00:00
|
|
|
runtime_initialized = true;
|
|
|
|
|
2018-08-24 06:47:34 +00:00
|
|
|
print_verbose("Mono: Runtime initialized");
|
2017-10-02 21:24:00 +00:00
|
|
|
|
2019-12-04 14:07:00 +00:00
|
|
|
#if defined(ANDROID_ENABLED)
|
2020-03-18 16:40:04 +00:00
|
|
|
gdmono::android::support::register_internal_calls();
|
2019-12-04 14:07:00 +00:00
|
|
|
#endif
|
|
|
|
|
2017-10-02 21:24:00 +00:00
|
|
|
// mscorlib assembly MUST be present at initialization
|
2019-08-09 01:39:45 +00:00
|
|
|
bool corlib_loaded = _load_corlib_assembly();
|
|
|
|
ERR_FAIL_COND_MSG(!corlib_loaded, "Mono: Failed to load mscorlib assembly.");
|
2017-10-02 21:24:00 +00:00
|
|
|
|
2020-03-18 16:40:04 +00:00
|
|
|
#ifndef GD_MONO_SINGLE_APPDOMAIN
|
2019-08-09 01:39:45 +00:00
|
|
|
Error domain_load_err = _load_scripts_domain();
|
|
|
|
ERR_FAIL_COND_MSG(domain_load_err != OK, "Mono: Failed to load scripts domain.");
|
2020-03-18 16:40:04 +00:00
|
|
|
#else
|
|
|
|
scripts_domain = root_domain;
|
|
|
|
#endif
|
2017-10-02 21:24:00 +00:00
|
|
|
|
|
|
|
_register_internal_calls();
|
|
|
|
|
2019-07-11 12:01:25 +00:00
|
|
|
print_verbose("Mono: INITIALIZED");
|
|
|
|
}
|
2019-07-03 07:44:53 +00:00
|
|
|
|
2019-07-11 12:01:25 +00:00
|
|
|
void GDMono::initialize_load_assemblies() {
|
|
|
|
#ifndef MONO_GLUE_ENABLED
|
2019-08-09 01:39:45 +00:00
|
|
|
CRASH_NOW_MSG("Mono: This binary was built with 'mono_glue=no'; cannot load assemblies.");
|
2018-10-03 17:01:57 +00:00
|
|
|
#endif
|
2018-02-22 12:13:51 +00:00
|
|
|
|
2019-07-11 12:01:25 +00:00
|
|
|
// Load assemblies. The API and tools assemblies are required,
|
|
|
|
// the application is aborted if these assemblies cannot be loaded.
|
2018-02-22 12:13:51 +00:00
|
|
|
|
2019-07-11 12:01:25 +00:00
|
|
|
_load_api_assemblies();
|
2018-02-22 12:13:51 +00:00
|
|
|
|
2019-07-11 12:01:25 +00:00
|
|
|
#if defined(TOOLS_ENABLED)
|
2019-08-09 01:39:45 +00:00
|
|
|
bool tool_assemblies_loaded = _load_tools_assemblies();
|
|
|
|
CRASH_COND_MSG(!tool_assemblies_loaded, "Mono: Failed to load '" TOOLS_ASM_NAME "' assemblies.");
|
2019-07-11 12:01:25 +00:00
|
|
|
#endif
|
2017-10-02 21:24:00 +00:00
|
|
|
|
2020-05-22 00:17:06 +00:00
|
|
|
if (Main::is_project_manager())
|
|
|
|
return;
|
|
|
|
|
2019-07-11 12:01:25 +00:00
|
|
|
// Load the project's main assembly. This doesn't necessarily need to succeed.
|
|
|
|
// The game may not be using .NET at all, or if the project does use .NET and
|
|
|
|
// we're running in the editor, it may just happen to be it wasn't built yet.
|
|
|
|
if (!_load_project_assembly()) {
|
|
|
|
if (OS::get_singleton()->is_stdout_verbose())
|
|
|
|
print_error("Mono: Failed to load project assembly");
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
bool GDMono::_are_api_assemblies_out_of_sync() {
|
2019-11-10 16:10:38 +00:00
|
|
|
bool out_of_sync = core_api_assembly.assembly && (core_api_assembly.out_of_sync || !GDMonoCache::cached_data.godot_api_cache_updated);
|
2019-07-11 12:01:25 +00:00
|
|
|
#ifdef TOOLS_ENABLED
|
|
|
|
if (!out_of_sync)
|
2019-10-10 23:23:35 +00:00
|
|
|
out_of_sync = editor_api_assembly.assembly && editor_api_assembly.out_of_sync;
|
2019-07-11 12:01:25 +00:00
|
|
|
#endif
|
|
|
|
return out_of_sync;
|
2017-10-02 21:24:00 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
namespace GodotSharpBindings {
|
2019-07-11 12:01:25 +00:00
|
|
|
#ifdef MONO_GLUE_ENABLED
|
2017-10-02 21:24:00 +00:00
|
|
|
|
|
|
|
uint64_t get_core_api_hash();
|
2018-02-22 12:13:51 +00:00
|
|
|
#ifdef TOOLS_ENABLED
|
2017-10-02 21:24:00 +00:00
|
|
|
uint64_t get_editor_api_hash();
|
2019-01-28 23:02:35 +00:00
|
|
|
#endif
|
2018-02-22 12:13:51 +00:00
|
|
|
uint32_t get_bindings_version();
|
2019-09-20 15:34:44 +00:00
|
|
|
uint32_t get_cs_glue_version();
|
2017-10-02 21:24:00 +00:00
|
|
|
|
|
|
|
void register_generated_icalls();
|
2019-07-11 12:01:25 +00:00
|
|
|
|
|
|
|
#else
|
|
|
|
|
|
|
|
uint64_t get_core_api_hash() {
|
|
|
|
GD_UNREACHABLE();
|
|
|
|
}
|
|
|
|
#ifdef TOOLS_ENABLED
|
|
|
|
uint64_t get_editor_api_hash() {
|
|
|
|
GD_UNREACHABLE();
|
|
|
|
}
|
2017-10-02 21:24:00 +00:00
|
|
|
#endif
|
2019-07-11 12:01:25 +00:00
|
|
|
uint32_t get_bindings_version() {
|
2019-09-20 15:34:44 +00:00
|
|
|
GD_UNREACHABLE();
|
|
|
|
}
|
2020-05-14 12:29:06 +00:00
|
|
|
|
2019-09-20 15:34:44 +00:00
|
|
|
uint32_t get_cs_glue_version() {
|
2019-07-11 12:01:25 +00:00
|
|
|
GD_UNREACHABLE();
|
|
|
|
}
|
|
|
|
|
|
|
|
void register_generated_icalls() {
|
|
|
|
/* Fine, just do nothing */
|
|
|
|
}
|
|
|
|
|
|
|
|
#endif // MONO_GLUE_ENABLED
|
|
|
|
} // namespace GodotSharpBindings
|
2017-10-02 21:24:00 +00:00
|
|
|
|
|
|
|
void GDMono::_register_internal_calls() {
|
|
|
|
GodotSharpBindings::register_generated_icalls();
|
|
|
|
}
|
|
|
|
|
2019-11-28 22:42:37 +00:00
|
|
|
void GDMono::_init_godot_api_hashes() {
|
|
|
|
#if defined(MONO_GLUE_ENABLED) && defined(DEBUG_METHODS_ENABLED)
|
2019-01-28 23:02:35 +00:00
|
|
|
if (get_api_core_hash() != GodotSharpBindings::get_core_api_hash()) {
|
2019-08-09 01:39:45 +00:00
|
|
|
ERR_PRINT("Mono: Core API hash mismatch.");
|
2017-10-02 21:24:00 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
#ifdef TOOLS_ENABLED
|
2019-01-28 23:02:35 +00:00
|
|
|
if (get_api_editor_hash() != GodotSharpBindings::get_editor_api_hash()) {
|
2019-08-09 01:39:45 +00:00
|
|
|
ERR_PRINT("Mono: Editor API hash mismatch.");
|
2017-10-02 21:24:00 +00:00
|
|
|
}
|
|
|
|
#endif // TOOLS_ENABLED
|
2019-11-28 22:42:37 +00:00
|
|
|
#endif // MONO_GLUE_ENABLED && DEBUG_METHODS_ENABLED
|
|
|
|
}
|
|
|
|
|
|
|
|
void GDMono::_init_exception_policy() {
|
|
|
|
PropertyInfo exc_policy_prop = PropertyInfo(Variant::INT, "mono/unhandled_exception_policy", PROPERTY_HINT_ENUM,
|
|
|
|
vformat("Terminate Application:%s,Log Error:%s", (int)POLICY_TERMINATE_APP, (int)POLICY_LOG_ERROR));
|
|
|
|
unhandled_exception_policy = (UnhandledExceptionPolicy)(int)GLOBAL_DEF(exc_policy_prop.name, (int)POLICY_TERMINATE_APP);
|
|
|
|
ProjectSettings::get_singleton()->set_custom_property_info(exc_policy_prop.name, exc_policy_prop);
|
|
|
|
|
|
|
|
if (Engine::get_singleton()->is_editor_hint()) {
|
|
|
|
// Unhandled exceptions should not terminate the editor
|
|
|
|
unhandled_exception_policy = POLICY_LOG_ERROR;
|
|
|
|
}
|
2017-10-02 21:24:00 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void GDMono::add_assembly(uint32_t p_domain_id, GDMonoAssembly *p_assembly) {
|
|
|
|
assemblies[p_domain_id][p_assembly->get_name()] = p_assembly;
|
|
|
|
}
|
|
|
|
|
2020-03-18 16:40:04 +00:00
|
|
|
GDMonoAssembly *GDMono::get_loaded_assembly(const String &p_name) {
|
2020-05-09 18:54:07 +00:00
|
|
|
if (p_name == "mscorlib" && corlib_assembly)
|
|
|
|
return corlib_assembly;
|
2017-10-07 02:21:55 +00:00
|
|
|
|
|
|
|
MonoDomain *domain = mono_domain_get();
|
|
|
|
uint32_t domain_id = domain ? mono_domain_get_id(domain) : 0;
|
2020-03-18 16:40:04 +00:00
|
|
|
GDMonoAssembly **result = assemblies[domain_id].getptr(p_name);
|
2020-04-01 23:20:12 +00:00
|
|
|
return result ? *result : nullptr;
|
2017-10-07 02:21:55 +00:00
|
|
|
}
|
|
|
|
|
2018-02-22 12:39:41 +00:00
|
|
|
bool GDMono::load_assembly(const String &p_name, GDMonoAssembly **r_assembly, bool p_refonly) {
|
2020-05-09 18:54:07 +00:00
|
|
|
#ifdef DEBUG_ENABLED
|
2018-02-22 12:39:41 +00:00
|
|
|
CRASH_COND(!r_assembly);
|
2020-05-09 18:54:07 +00:00
|
|
|
#endif
|
2018-02-22 12:39:41 +00:00
|
|
|
|
|
|
|
MonoAssemblyName *aname = mono_assembly_name_new(p_name.utf8());
|
2018-10-19 17:02:52 +00:00
|
|
|
bool result = load_assembly(p_name, aname, r_assembly, p_refonly);
|
2018-02-22 12:39:41 +00:00
|
|
|
mono_assembly_name_free(aname);
|
|
|
|
mono_free(aname);
|
|
|
|
|
|
|
|
return result;
|
|
|
|
}
|
|
|
|
|
2018-10-19 17:02:52 +00:00
|
|
|
bool GDMono::load_assembly(const String &p_name, MonoAssemblyName *p_aname, GDMonoAssembly **r_assembly, bool p_refonly) {
|
2020-05-09 18:54:07 +00:00
|
|
|
#ifdef DEBUG_ENABLED
|
2017-10-02 21:24:00 +00:00
|
|
|
CRASH_COND(!r_assembly);
|
2020-05-09 18:54:07 +00:00
|
|
|
#endif
|
2017-10-02 21:24:00 +00:00
|
|
|
|
2020-05-09 18:54:07 +00:00
|
|
|
return load_assembly(p_name, p_aname, r_assembly, p_refonly, GDMonoAssembly::get_default_search_dirs());
|
|
|
|
}
|
2017-10-02 21:24:00 +00:00
|
|
|
|
2020-05-09 18:54:07 +00:00
|
|
|
bool GDMono::load_assembly(const String &p_name, MonoAssemblyName *p_aname, GDMonoAssembly **r_assembly, bool p_refonly, const Vector<String> &p_search_dirs) {
|
|
|
|
#ifdef DEBUG_ENABLED
|
|
|
|
CRASH_COND(!r_assembly);
|
|
|
|
#endif
|
2018-02-22 12:39:41 +00:00
|
|
|
|
2020-05-09 18:54:07 +00:00
|
|
|
print_verbose("Mono: Loading assembly " + p_name + (p_refonly ? " (refonly)" : "") + "...");
|
2017-10-02 21:24:00 +00:00
|
|
|
|
2020-05-09 18:54:07 +00:00
|
|
|
GDMonoAssembly *assembly = GDMonoAssembly::load(p_name, p_aname, p_refonly, p_search_dirs);
|
2017-10-02 21:24:00 +00:00
|
|
|
|
2020-05-09 18:54:07 +00:00
|
|
|
if (!assembly)
|
|
|
|
return false;
|
2018-02-22 12:39:41 +00:00
|
|
|
|
2020-05-09 18:54:07 +00:00
|
|
|
*r_assembly = assembly;
|
2017-10-02 21:24:00 +00:00
|
|
|
|
2018-08-24 06:47:34 +00:00
|
|
|
print_verbose("Mono: Assembly " + p_name + (p_refonly ? " (refonly)" : "") + " loaded from path: " + (*r_assembly)->get_path());
|
2017-10-02 21:24:00 +00:00
|
|
|
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2018-10-19 17:02:52 +00:00
|
|
|
bool GDMono::load_assembly_from(const String &p_name, const String &p_path, GDMonoAssembly **r_assembly, bool p_refonly) {
|
|
|
|
CRASH_COND(!r_assembly);
|
|
|
|
|
|
|
|
print_verbose("Mono: Loading assembly " + p_name + (p_refonly ? " (refonly)" : "") + "...");
|
|
|
|
|
|
|
|
GDMonoAssembly *assembly = GDMonoAssembly::load_from(p_name, p_path, p_refonly);
|
|
|
|
|
|
|
|
if (!assembly)
|
|
|
|
return false;
|
|
|
|
|
|
|
|
*r_assembly = assembly;
|
|
|
|
|
|
|
|
print_verbose("Mono: Assembly " + p_name + (p_refonly ? " (refonly)" : "") + " loaded from path: " + (*r_assembly)->get_path());
|
|
|
|
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2019-10-10 23:23:35 +00:00
|
|
|
ApiAssemblyInfo::Version ApiAssemblyInfo::Version::get_from_loaded_assembly(GDMonoAssembly *p_api_assembly, ApiAssemblyInfo::Type p_api_type) {
|
|
|
|
ApiAssemblyInfo::Version api_assembly_version;
|
2018-02-22 12:13:51 +00:00
|
|
|
|
2019-10-10 23:23:35 +00:00
|
|
|
const char *nativecalls_name = p_api_type == ApiAssemblyInfo::API_CORE ?
|
2018-02-22 12:13:51 +00:00
|
|
|
BINDINGS_CLASS_NATIVECALLS :
|
|
|
|
BINDINGS_CLASS_NATIVECALLS_EDITOR;
|
|
|
|
|
|
|
|
GDMonoClass *nativecalls_klass = p_api_assembly->get_class(BINDINGS_NAMESPACE, nativecalls_name);
|
|
|
|
|
|
|
|
if (nativecalls_klass) {
|
|
|
|
GDMonoField *api_hash_field = nativecalls_klass->get_field("godot_api_hash");
|
|
|
|
if (api_hash_field)
|
2020-04-01 23:20:12 +00:00
|
|
|
api_assembly_version.godot_api_hash = GDMonoMarshal::unbox<uint64_t>(api_hash_field->get_value(nullptr));
|
2018-02-22 12:13:51 +00:00
|
|
|
|
|
|
|
GDMonoField *binds_ver_field = nativecalls_klass->get_field("bindings_version");
|
|
|
|
if (binds_ver_field)
|
2020-04-01 23:20:12 +00:00
|
|
|
api_assembly_version.bindings_version = GDMonoMarshal::unbox<uint32_t>(binds_ver_field->get_value(nullptr));
|
2018-02-22 12:13:51 +00:00
|
|
|
|
|
|
|
GDMonoField *cs_glue_ver_field = nativecalls_klass->get_field("cs_glue_version");
|
|
|
|
if (cs_glue_ver_field)
|
2020-04-01 23:20:12 +00:00
|
|
|
api_assembly_version.cs_glue_version = GDMonoMarshal::unbox<uint32_t>(cs_glue_ver_field->get_value(nullptr));
|
2018-02-22 12:13:51 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
return api_assembly_version;
|
|
|
|
}
|
|
|
|
|
2019-10-10 23:23:35 +00:00
|
|
|
String ApiAssemblyInfo::to_string(ApiAssemblyInfo::Type p_type) {
|
|
|
|
return p_type == ApiAssemblyInfo::API_CORE ? "API_CORE" : "API_EDITOR";
|
2018-02-22 12:13:51 +00:00
|
|
|
}
|
|
|
|
|
2017-10-02 21:24:00 +00:00
|
|
|
bool GDMono::_load_corlib_assembly() {
|
|
|
|
if (corlib_assembly)
|
|
|
|
return true;
|
|
|
|
|
2018-02-22 12:39:41 +00:00
|
|
|
bool success = load_assembly("mscorlib", &corlib_assembly);
|
2017-10-02 21:24:00 +00:00
|
|
|
|
|
|
|
if (success)
|
2019-11-10 16:10:38 +00:00
|
|
|
GDMonoCache::update_corlib_cache();
|
2017-10-02 21:24:00 +00:00
|
|
|
|
|
|
|
return success;
|
|
|
|
}
|
|
|
|
|
2019-07-08 13:22:51 +00:00
|
|
|
#ifdef TOOLS_ENABLED
|
2019-10-10 23:23:35 +00:00
|
|
|
bool GDMono::copy_prebuilt_api_assembly(ApiAssemblyInfo::Type p_api_type, const String &p_config) {
|
2019-07-18 02:08:24 +00:00
|
|
|
String src_dir = GodotSharpDirs::get_data_editor_prebuilt_api_dir().plus_file(p_config);
|
|
|
|
String dst_dir = GodotSharpDirs::get_res_assemblies_base_dir().plus_file(p_config);
|
2019-07-11 12:01:25 +00:00
|
|
|
|
2019-10-10 23:23:35 +00:00
|
|
|
String assembly_name = p_api_type == ApiAssemblyInfo::API_CORE ? CORE_API_ASSEMBLY_NAME : EDITOR_API_ASSEMBLY_NAME;
|
2019-07-03 07:44:53 +00:00
|
|
|
|
|
|
|
// Create destination directory if needed
|
2019-07-11 12:01:25 +00:00
|
|
|
if (!DirAccess::exists(dst_dir)) {
|
|
|
|
DirAccess *da = DirAccess::create_for_path(dst_dir);
|
|
|
|
Error err = da->make_dir_recursive(dst_dir);
|
2019-07-03 07:44:53 +00:00
|
|
|
memdelete(da);
|
|
|
|
|
|
|
|
if (err != OK) {
|
2019-11-06 16:03:04 +00:00
|
|
|
ERR_PRINT("Failed to create destination directory for the API assemblies. Error: " + itos(err) + ".");
|
2019-07-03 07:44:53 +00:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-10-10 23:23:35 +00:00
|
|
|
DirAccessRef da = DirAccess::create(DirAccess::ACCESS_FILESYSTEM);
|
|
|
|
|
|
|
|
String xml_file = assembly_name + ".xml";
|
|
|
|
if (da->copy(src_dir.plus_file(xml_file), dst_dir.plus_file(xml_file)) != OK)
|
2019-11-07 08:44:15 +00:00
|
|
|
WARN_PRINT("Failed to copy '" + xml_file + "'.");
|
2019-10-10 23:23:35 +00:00
|
|
|
|
|
|
|
String pdb_file = assembly_name + ".pdb";
|
|
|
|
if (da->copy(src_dir.plus_file(pdb_file), dst_dir.plus_file(pdb_file)) != OK)
|
2019-11-07 08:44:15 +00:00
|
|
|
WARN_PRINT("Failed to copy '" + pdb_file + "'.");
|
2019-10-10 23:23:35 +00:00
|
|
|
|
2019-07-11 12:01:25 +00:00
|
|
|
String assembly_file = assembly_name + ".dll";
|
2019-10-10 23:23:35 +00:00
|
|
|
if (da->copy(src_dir.plus_file(assembly_file), dst_dir.plus_file(assembly_file)) != OK) {
|
2019-11-06 16:03:04 +00:00
|
|
|
ERR_PRINT("Failed to copy '" + assembly_file + "'.");
|
2019-10-10 23:23:35 +00:00
|
|
|
return false;
|
|
|
|
}
|
2019-07-03 07:44:53 +00:00
|
|
|
|
2019-10-10 23:23:35 +00:00
|
|
|
return true;
|
|
|
|
}
|
2019-07-03 07:44:53 +00:00
|
|
|
|
2019-10-10 23:23:35 +00:00
|
|
|
static bool try_get_cached_api_hash_for(const String &p_api_assemblies_dir, bool &r_out_of_sync) {
|
|
|
|
String core_api_assembly_path = p_api_assemblies_dir.plus_file(CORE_API_ASSEMBLY_NAME ".dll");
|
|
|
|
String editor_api_assembly_path = p_api_assemblies_dir.plus_file(EDITOR_API_ASSEMBLY_NAME ".dll");
|
2019-07-03 07:44:53 +00:00
|
|
|
|
2019-10-10 23:23:35 +00:00
|
|
|
if (!FileAccess::exists(core_api_assembly_path) || !FileAccess::exists(editor_api_assembly_path))
|
|
|
|
return false;
|
2019-07-03 07:44:53 +00:00
|
|
|
|
2019-10-10 23:23:35 +00:00
|
|
|
String cached_api_hash_path = p_api_assemblies_dir.plus_file("api_hash_cache.cfg");
|
2019-07-03 07:44:53 +00:00
|
|
|
|
2019-10-10 23:23:35 +00:00
|
|
|
if (!FileAccess::exists(cached_api_hash_path))
|
|
|
|
return false;
|
|
|
|
|
|
|
|
Ref<ConfigFile> cfg;
|
|
|
|
cfg.instance();
|
|
|
|
Error cfg_err = cfg->load(cached_api_hash_path);
|
|
|
|
ERR_FAIL_COND_V(cfg_err != OK, false);
|
2019-07-03 07:44:53 +00:00
|
|
|
|
2019-10-10 23:23:35 +00:00
|
|
|
// Checking the modified time is good enough
|
|
|
|
if (FileAccess::get_modified_time(core_api_assembly_path) != (uint64_t)cfg->get_value("core", "modified_time") ||
|
|
|
|
FileAccess::get_modified_time(editor_api_assembly_path) != (uint64_t)cfg->get_value("editor", "modified_time")) {
|
|
|
|
return false;
|
2019-07-03 07:44:53 +00:00
|
|
|
}
|
|
|
|
|
2019-10-10 23:23:35 +00:00
|
|
|
r_out_of_sync = GodotSharpBindings::get_bindings_version() != (uint32_t)cfg->get_value("core", "bindings_version") ||
|
|
|
|
GodotSharpBindings::get_cs_glue_version() != (uint32_t)cfg->get_value("core", "cs_glue_version") ||
|
|
|
|
GodotSharpBindings::get_bindings_version() != (uint32_t)cfg->get_value("editor", "bindings_version") ||
|
|
|
|
GodotSharpBindings::get_cs_glue_version() != (uint32_t)cfg->get_value("editor", "cs_glue_version") ||
|
|
|
|
GodotSharpBindings::get_core_api_hash() != (uint64_t)cfg->get_value("core", "api_hash") ||
|
|
|
|
GodotSharpBindings::get_editor_api_hash() != (uint64_t)cfg->get_value("editor", "api_hash");
|
|
|
|
|
2019-07-03 07:44:53 +00:00
|
|
|
return true;
|
|
|
|
}
|
2019-07-11 12:01:25 +00:00
|
|
|
|
2019-10-10 23:23:35 +00:00
|
|
|
static void create_cached_api_hash_for(const String &p_api_assemblies_dir) {
|
|
|
|
String core_api_assembly_path = p_api_assemblies_dir.plus_file(CORE_API_ASSEMBLY_NAME ".dll");
|
|
|
|
String editor_api_assembly_path = p_api_assemblies_dir.plus_file(EDITOR_API_ASSEMBLY_NAME ".dll");
|
|
|
|
String cached_api_hash_path = p_api_assemblies_dir.plus_file("api_hash_cache.cfg");
|
|
|
|
|
|
|
|
Ref<ConfigFile> cfg;
|
|
|
|
cfg.instance();
|
|
|
|
|
|
|
|
cfg->set_value("core", "modified_time", FileAccess::get_modified_time(core_api_assembly_path));
|
|
|
|
cfg->set_value("editor", "modified_time", FileAccess::get_modified_time(editor_api_assembly_path));
|
|
|
|
|
|
|
|
cfg->set_value("core", "bindings_version", GodotSharpBindings::get_bindings_version());
|
|
|
|
cfg->set_value("core", "cs_glue_version", GodotSharpBindings::get_cs_glue_version());
|
|
|
|
cfg->set_value("editor", "bindings_version", GodotSharpBindings::get_bindings_version());
|
|
|
|
cfg->set_value("editor", "cs_glue_version", GodotSharpBindings::get_cs_glue_version());
|
|
|
|
|
|
|
|
// This assumes the prebuilt api assemblies we copied to the project are not out of sync
|
|
|
|
cfg->set_value("core", "api_hash", GodotSharpBindings::get_core_api_hash());
|
|
|
|
cfg->set_value("editor", "api_hash", GodotSharpBindings::get_editor_api_hash());
|
|
|
|
|
|
|
|
Error err = cfg->save(cached_api_hash_path);
|
|
|
|
ERR_FAIL_COND(err != OK);
|
|
|
|
}
|
|
|
|
|
|
|
|
bool GDMono::_temp_domain_load_are_assemblies_out_of_sync(const String &p_config) {
|
|
|
|
MonoDomain *temp_domain = GDMonoUtils::create_domain("GodotEngine.Domain.CheckApiAssemblies");
|
|
|
|
ERR_FAIL_NULL_V(temp_domain, "Failed to create temporary domain to check API assemblies");
|
|
|
|
_GDMONO_SCOPE_EXIT_DOMAIN_UNLOAD_(temp_domain);
|
|
|
|
|
|
|
|
_GDMONO_SCOPE_DOMAIN_(temp_domain);
|
|
|
|
|
|
|
|
GDMono::LoadedApiAssembly temp_core_api_assembly;
|
|
|
|
GDMono::LoadedApiAssembly temp_editor_api_assembly;
|
|
|
|
|
|
|
|
if (!_try_load_api_assemblies(temp_core_api_assembly, temp_editor_api_assembly,
|
2020-04-01 23:20:12 +00:00
|
|
|
p_config, /* refonly: */ true, /* loaded_callback: */ nullptr)) {
|
2019-10-10 23:23:35 +00:00
|
|
|
return temp_core_api_assembly.out_of_sync || temp_editor_api_assembly.out_of_sync;
|
|
|
|
}
|
|
|
|
|
|
|
|
return true; // Failed to load, assume they're outdated assemblies
|
|
|
|
}
|
|
|
|
|
|
|
|
String GDMono::update_api_assemblies_from_prebuilt(const String &p_config, const bool *p_core_api_out_of_sync, const bool *p_editor_api_out_of_sync) {
|
2019-07-21 21:21:35 +00:00
|
|
|
#define FAIL_REASON(m_out_of_sync, m_prebuilt_exists) \
|
2019-07-11 12:01:25 +00:00
|
|
|
( \
|
|
|
|
(m_out_of_sync ? \
|
2019-08-09 01:39:45 +00:00
|
|
|
String("The assembly is invalidated ") : \
|
|
|
|
String("The assembly was not found ")) + \
|
2019-07-21 21:21:35 +00:00
|
|
|
(m_prebuilt_exists ? \
|
2019-08-09 01:39:45 +00:00
|
|
|
String("and the prebuilt assemblies are missing.") : \
|
|
|
|
String("and we failed to copy the prebuilt assemblies.")))
|
2019-07-11 12:01:25 +00:00
|
|
|
|
2019-10-10 23:23:35 +00:00
|
|
|
String dst_assemblies_dir = GodotSharpDirs::get_res_assemblies_base_dir().plus_file(p_config);
|
2019-07-11 12:01:25 +00:00
|
|
|
|
2019-10-10 23:23:35 +00:00
|
|
|
String core_assembly_path = dst_assemblies_dir.plus_file(CORE_API_ASSEMBLY_NAME ".dll");
|
|
|
|
String editor_assembly_path = dst_assemblies_dir.plus_file(EDITOR_API_ASSEMBLY_NAME ".dll");
|
2019-07-11 12:01:25 +00:00
|
|
|
|
2019-10-10 23:23:35 +00:00
|
|
|
bool api_assemblies_out_of_sync = false;
|
2019-07-11 12:01:25 +00:00
|
|
|
|
2019-10-10 23:23:35 +00:00
|
|
|
if (p_core_api_out_of_sync && p_editor_api_out_of_sync) {
|
|
|
|
api_assemblies_out_of_sync = p_core_api_out_of_sync || p_editor_api_out_of_sync;
|
|
|
|
} else if (FileAccess::exists(core_assembly_path) && FileAccess::exists(editor_assembly_path)) {
|
|
|
|
// Determine if they're out of sync
|
|
|
|
if (!try_get_cached_api_hash_for(dst_assemblies_dir, api_assemblies_out_of_sync)) {
|
|
|
|
api_assemblies_out_of_sync = _temp_domain_load_are_assemblies_out_of_sync(p_config);
|
|
|
|
}
|
|
|
|
}
|
2019-07-21 21:21:35 +00:00
|
|
|
|
2019-10-10 23:23:35 +00:00
|
|
|
// Note: Even if only one of the assemblies if missing or out of sync, we update both
|
2019-07-11 12:01:25 +00:00
|
|
|
|
2019-10-10 23:23:35 +00:00
|
|
|
if (!api_assemblies_out_of_sync && FileAccess::exists(core_assembly_path) && FileAccess::exists(editor_assembly_path))
|
|
|
|
return String(); // No update needed
|
2019-07-11 12:01:25 +00:00
|
|
|
|
2019-10-10 23:23:35 +00:00
|
|
|
print_verbose("Updating '" + p_config + "' API assemblies");
|
2019-07-18 02:08:24 +00:00
|
|
|
|
2019-10-10 23:23:35 +00:00
|
|
|
String prebuilt_api_dir = GodotSharpDirs::get_data_editor_prebuilt_api_dir().plus_file(p_config);
|
|
|
|
String prebuilt_core_dll_path = prebuilt_api_dir.plus_file(CORE_API_ASSEMBLY_NAME ".dll");
|
|
|
|
String prebuilt_editor_dll_path = prebuilt_api_dir.plus_file(EDITOR_API_ASSEMBLY_NAME ".dll");
|
2019-07-18 02:08:24 +00:00
|
|
|
|
2019-10-10 23:23:35 +00:00
|
|
|
if (!FileAccess::exists(prebuilt_core_dll_path) || !FileAccess::exists(prebuilt_editor_dll_path)) {
|
|
|
|
return FAIL_REASON(api_assemblies_out_of_sync, /* prebuilt_exists: */ false);
|
2019-07-18 02:08:24 +00:00
|
|
|
}
|
2019-07-11 12:01:25 +00:00
|
|
|
|
2019-10-10 23:23:35 +00:00
|
|
|
// Copy the prebuilt Api
|
|
|
|
if (!copy_prebuilt_api_assembly(ApiAssemblyInfo::API_CORE, p_config) ||
|
|
|
|
!copy_prebuilt_api_assembly(ApiAssemblyInfo::API_EDITOR, p_config)) {
|
|
|
|
return FAIL_REASON(api_assemblies_out_of_sync, /* prebuilt_exists: */ true);
|
|
|
|
}
|
|
|
|
|
|
|
|
// Cache the api hash of the assemblies we just copied
|
|
|
|
create_cached_api_hash_for(dst_assemblies_dir);
|
|
|
|
|
2019-07-11 12:01:25 +00:00
|
|
|
return String(); // Updated successfully
|
|
|
|
|
|
|
|
#undef FAIL_REASON
|
|
|
|
}
|
2019-07-08 13:22:51 +00:00
|
|
|
#endif
|
2019-07-03 07:44:53 +00:00
|
|
|
|
2019-10-10 23:23:35 +00:00
|
|
|
bool GDMono::_load_core_api_assembly(LoadedApiAssembly &r_loaded_api_assembly, const String &p_config, bool p_refonly) {
|
|
|
|
if (r_loaded_api_assembly.assembly)
|
2017-10-02 21:24:00 +00:00
|
|
|
return true;
|
|
|
|
|
2018-02-22 12:13:51 +00:00
|
|
|
#ifdef TOOLS_ENABLED
|
2019-07-11 12:01:25 +00:00
|
|
|
// For the editor and the editor player we want to load it from a specific path to make sure we can keep it up to date
|
2019-07-25 15:52:41 +00:00
|
|
|
|
|
|
|
// If running the project manager, load it from the prebuilt API directory
|
|
|
|
String assembly_dir = !Main::is_project_manager() ?
|
2019-10-10 23:23:35 +00:00
|
|
|
GodotSharpDirs::get_res_assemblies_base_dir().plus_file(p_config) :
|
|
|
|
GodotSharpDirs::get_data_editor_prebuilt_api_dir().plus_file(p_config);
|
2019-07-25 15:52:41 +00:00
|
|
|
|
|
|
|
String assembly_path = assembly_dir.plus_file(CORE_API_ASSEMBLY_NAME ".dll");
|
|
|
|
|
2019-07-11 12:01:25 +00:00
|
|
|
bool success = FileAccess::exists(assembly_path) &&
|
2019-10-10 23:23:35 +00:00
|
|
|
load_assembly_from(CORE_API_ASSEMBLY_NAME, assembly_path, &r_loaded_api_assembly.assembly, p_refonly);
|
2019-07-11 12:01:25 +00:00
|
|
|
#else
|
2019-10-11 13:17:36 +00:00
|
|
|
bool success = load_assembly(CORE_API_ASSEMBLY_NAME, &r_loaded_api_assembly.assembly, p_refonly);
|
2019-07-11 12:01:25 +00:00
|
|
|
#endif
|
2017-10-02 21:24:00 +00:00
|
|
|
|
2018-02-22 12:13:51 +00:00
|
|
|
if (success) {
|
2019-10-10 23:23:35 +00:00
|
|
|
ApiAssemblyInfo::Version api_assembly_ver = ApiAssemblyInfo::Version::get_from_loaded_assembly(r_loaded_api_assembly.assembly, ApiAssemblyInfo::API_CORE);
|
|
|
|
r_loaded_api_assembly.out_of_sync = GodotSharpBindings::get_core_api_hash() != api_assembly_ver.godot_api_hash ||
|
|
|
|
GodotSharpBindings::get_bindings_version() != api_assembly_ver.bindings_version ||
|
|
|
|
GodotSharpBindings::get_cs_glue_version() != api_assembly_ver.cs_glue_version;
|
2019-07-11 12:01:25 +00:00
|
|
|
} else {
|
2019-10-10 23:23:35 +00:00
|
|
|
r_loaded_api_assembly.out_of_sync = false;
|
2018-02-22 12:13:51 +00:00
|
|
|
}
|
2017-10-02 21:24:00 +00:00
|
|
|
|
|
|
|
return success;
|
|
|
|
}
|
|
|
|
|
|
|
|
#ifdef TOOLS_ENABLED
|
2019-10-10 23:23:35 +00:00
|
|
|
bool GDMono::_load_editor_api_assembly(LoadedApiAssembly &r_loaded_api_assembly, const String &p_config, bool p_refonly) {
|
|
|
|
if (r_loaded_api_assembly.assembly)
|
2017-10-02 21:24:00 +00:00
|
|
|
return true;
|
|
|
|
|
2019-07-11 12:01:25 +00:00
|
|
|
// For the editor and the editor player we want to load it from a specific path to make sure we can keep it up to date
|
2019-07-25 15:52:41 +00:00
|
|
|
|
|
|
|
// If running the project manager, load it from the prebuilt API directory
|
|
|
|
String assembly_dir = !Main::is_project_manager() ?
|
2019-10-10 23:23:35 +00:00
|
|
|
GodotSharpDirs::get_res_assemblies_base_dir().plus_file(p_config) :
|
|
|
|
GodotSharpDirs::get_data_editor_prebuilt_api_dir().plus_file(p_config);
|
2019-07-25 15:52:41 +00:00
|
|
|
|
|
|
|
String assembly_path = assembly_dir.plus_file(EDITOR_API_ASSEMBLY_NAME ".dll");
|
|
|
|
|
2019-07-11 12:01:25 +00:00
|
|
|
bool success = FileAccess::exists(assembly_path) &&
|
2019-10-10 23:23:35 +00:00
|
|
|
load_assembly_from(EDITOR_API_ASSEMBLY_NAME, assembly_path, &r_loaded_api_assembly.assembly, p_refonly);
|
2018-02-22 12:13:51 +00:00
|
|
|
|
|
|
|
if (success) {
|
2019-10-10 23:23:35 +00:00
|
|
|
ApiAssemblyInfo::Version api_assembly_ver = ApiAssemblyInfo::Version::get_from_loaded_assembly(r_loaded_api_assembly.assembly, ApiAssemblyInfo::API_EDITOR);
|
|
|
|
r_loaded_api_assembly.out_of_sync = GodotSharpBindings::get_editor_api_hash() != api_assembly_ver.godot_api_hash ||
|
|
|
|
GodotSharpBindings::get_bindings_version() != api_assembly_ver.bindings_version ||
|
|
|
|
GodotSharpBindings::get_cs_glue_version() != api_assembly_ver.cs_glue_version;
|
2019-07-11 12:01:25 +00:00
|
|
|
} else {
|
2019-10-10 23:23:35 +00:00
|
|
|
r_loaded_api_assembly.out_of_sync = false;
|
2018-02-22 12:13:51 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
return success;
|
2017-10-02 21:24:00 +00:00
|
|
|
}
|
|
|
|
#endif
|
|
|
|
|
2019-10-10 23:23:35 +00:00
|
|
|
bool GDMono::_try_load_api_assemblies(LoadedApiAssembly &r_core_api_assembly, LoadedApiAssembly &r_editor_api_assembly,
|
|
|
|
const String &p_config, bool p_refonly, CoreApiAssemblyLoadedCallback p_callback) {
|
|
|
|
if (!_load_core_api_assembly(r_core_api_assembly, p_config, p_refonly)) {
|
2019-07-11 12:01:25 +00:00
|
|
|
if (OS::get_singleton()->is_stdout_verbose())
|
|
|
|
print_error("Mono: Failed to load Core API assembly");
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
#ifdef TOOLS_ENABLED
|
2019-10-10 23:23:35 +00:00
|
|
|
if (!_load_editor_api_assembly(r_editor_api_assembly, p_config, p_refonly)) {
|
2019-07-11 12:01:25 +00:00
|
|
|
if (OS::get_singleton()->is_stdout_verbose())
|
|
|
|
print_error("Mono: Failed to load Editor API assembly");
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2019-10-10 23:23:35 +00:00
|
|
|
if (r_editor_api_assembly.out_of_sync)
|
2019-07-11 12:01:25 +00:00
|
|
|
return false;
|
|
|
|
#endif
|
|
|
|
|
2019-07-21 21:21:35 +00:00
|
|
|
// Check if the core API assembly is out of sync only after trying to load the
|
|
|
|
// editor API assembly. Otherwise, if both assemblies are out of sync, we would
|
|
|
|
// only update the former as we won't know the latter also needs to be updated.
|
2019-10-10 23:23:35 +00:00
|
|
|
if (r_core_api_assembly.out_of_sync)
|
2019-07-21 21:21:35 +00:00
|
|
|
return false;
|
|
|
|
|
2019-10-10 23:23:35 +00:00
|
|
|
if (p_callback)
|
|
|
|
return p_callback();
|
|
|
|
|
2019-07-11 12:01:25 +00:00
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2019-10-10 23:23:35 +00:00
|
|
|
bool GDMono::_on_core_api_assembly_loaded() {
|
2019-11-10 16:10:38 +00:00
|
|
|
GDMonoCache::update_godot_api_cache();
|
2019-10-10 23:23:35 +00:00
|
|
|
|
2019-11-10 16:10:38 +00:00
|
|
|
if (!GDMonoCache::cached_data.godot_api_cache_updated)
|
2019-10-10 23:23:35 +00:00
|
|
|
return false;
|
|
|
|
|
|
|
|
get_singleton()->_install_trace_listener();
|
|
|
|
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
bool GDMono::_try_load_api_assemblies_preset() {
|
|
|
|
return _try_load_api_assemblies(core_api_assembly, editor_api_assembly,
|
|
|
|
get_expected_api_build_config(), /* refonly: */ false, _on_core_api_assembly_loaded);
|
|
|
|
}
|
|
|
|
|
2019-07-11 12:01:25 +00:00
|
|
|
void GDMono::_load_api_assemblies() {
|
2019-10-10 23:23:35 +00:00
|
|
|
bool api_assemblies_loaded = _try_load_api_assemblies_preset();
|
2019-09-25 20:32:39 +00:00
|
|
|
|
2020-03-18 16:40:04 +00:00
|
|
|
#if defined(TOOLS_ENABLED) && !defined(GD_MONO_SINGLE_APPDOMAIN)
|
2019-09-25 20:32:39 +00:00
|
|
|
if (!api_assemblies_loaded) {
|
2019-10-10 23:23:35 +00:00
|
|
|
// The API assemblies are out of sync or some other error happened. Fine, try one more time, but
|
|
|
|
// this time update them from the prebuilt assemblies directory before trying to load them again.
|
2019-07-11 12:01:25 +00:00
|
|
|
|
2019-07-25 15:52:41 +00:00
|
|
|
// Shouldn't happen. The project manager loads the prebuilt API assemblies
|
2019-08-09 01:39:45 +00:00
|
|
|
CRASH_COND_MSG(Main::is_project_manager(), "Failed to load one of the prebuilt API assemblies.");
|
2019-07-25 15:52:41 +00:00
|
|
|
|
2019-07-11 12:01:25 +00:00
|
|
|
// 1. Unload the scripts domain
|
2019-08-09 01:39:45 +00:00
|
|
|
Error domain_unload_err = _unload_scripts_domain();
|
|
|
|
CRASH_COND_MSG(domain_unload_err != OK, "Mono: Failed to unload scripts domain.");
|
2019-07-11 12:01:25 +00:00
|
|
|
|
|
|
|
// 2. Update the API assemblies
|
2019-10-10 23:23:35 +00:00
|
|
|
String update_error = update_api_assemblies_from_prebuilt("Debug", &core_api_assembly.out_of_sync, &editor_api_assembly.out_of_sync);
|
2019-08-09 01:39:45 +00:00
|
|
|
CRASH_COND_MSG(!update_error.empty(), update_error);
|
2019-07-11 12:01:25 +00:00
|
|
|
|
|
|
|
// 3. Load the scripts domain again
|
2019-08-09 01:39:45 +00:00
|
|
|
Error domain_load_err = _load_scripts_domain();
|
|
|
|
CRASH_COND_MSG(domain_load_err != OK, "Mono: Failed to load scripts domain.");
|
2019-07-11 12:01:25 +00:00
|
|
|
|
|
|
|
// 4. Try loading the updated assemblies
|
2019-10-10 23:23:35 +00:00
|
|
|
api_assemblies_loaded = _try_load_api_assemblies_preset();
|
2019-09-25 20:32:39 +00:00
|
|
|
}
|
2020-03-18 16:40:04 +00:00
|
|
|
#endif
|
2019-09-25 20:32:39 +00:00
|
|
|
|
|
|
|
if (!api_assemblies_loaded) {
|
|
|
|
// welp... too bad
|
|
|
|
|
|
|
|
if (_are_api_assemblies_out_of_sync()) {
|
2019-10-10 23:23:35 +00:00
|
|
|
if (core_api_assembly.out_of_sync) {
|
2019-09-25 20:32:39 +00:00
|
|
|
ERR_PRINT("The assembly '" CORE_API_ASSEMBLY_NAME "' is out of sync.");
|
2019-11-10 16:10:38 +00:00
|
|
|
} else if (!GDMonoCache::cached_data.godot_api_cache_updated) {
|
2019-09-25 20:32:39 +00:00
|
|
|
ERR_PRINT("The loaded assembly '" CORE_API_ASSEMBLY_NAME "' is in sync, but the cache update failed.");
|
|
|
|
}
|
|
|
|
|
|
|
|
#ifdef TOOLS_ENABLED
|
2019-10-10 23:23:35 +00:00
|
|
|
if (editor_api_assembly.out_of_sync) {
|
2019-09-25 20:32:39 +00:00
|
|
|
ERR_PRINT("The assembly '" EDITOR_API_ASSEMBLY_NAME "' is out of sync.");
|
2019-07-11 12:01:25 +00:00
|
|
|
}
|
2019-07-20 12:26:46 +00:00
|
|
|
#endif
|
2019-09-25 20:32:39 +00:00
|
|
|
|
|
|
|
CRASH_NOW();
|
|
|
|
} else {
|
|
|
|
CRASH_NOW_MSG("Failed to load one of the API assemblies.");
|
|
|
|
}
|
2019-07-11 12:01:25 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-10-02 21:24:00 +00:00
|
|
|
#ifdef TOOLS_ENABLED
|
2019-07-03 07:44:53 +00:00
|
|
|
bool GDMono::_load_tools_assemblies() {
|
|
|
|
if (tools_assembly && tools_project_editor_assembly)
|
2017-10-02 21:24:00 +00:00
|
|
|
return true;
|
|
|
|
|
2019-08-09 01:39:45 +00:00
|
|
|
bool success = load_assembly(TOOLS_ASM_NAME, &tools_assembly) &&
|
|
|
|
load_assembly(TOOLS_PROJECT_EDITOR_ASM_NAME, &tools_project_editor_assembly);
|
2017-10-02 21:24:00 +00:00
|
|
|
|
2019-07-03 07:44:53 +00:00
|
|
|
return success;
|
2017-10-02 21:24:00 +00:00
|
|
|
}
|
|
|
|
#endif
|
|
|
|
|
|
|
|
bool GDMono::_load_project_assembly() {
|
|
|
|
if (project_assembly)
|
|
|
|
return true;
|
|
|
|
|
2019-07-03 15:41:07 +00:00
|
|
|
String appname = ProjectSettings::get_singleton()->get("application/config/name");
|
|
|
|
String appname_safe = OS::get_singleton()->get_safe_dir_name(appname);
|
|
|
|
if (appname_safe.empty()) {
|
|
|
|
appname_safe = "UnnamedProject";
|
2017-10-27 19:22:54 +00:00
|
|
|
}
|
2017-10-02 21:24:00 +00:00
|
|
|
|
2019-07-03 15:41:07 +00:00
|
|
|
bool success = load_assembly(appname_safe, &project_assembly);
|
2017-10-02 21:24:00 +00:00
|
|
|
|
2018-02-22 12:13:51 +00:00
|
|
|
if (success) {
|
2017-10-02 21:24:00 +00:00
|
|
|
mono_assembly_set_main(project_assembly->get_assembly());
|
2018-02-22 12:13:51 +00:00
|
|
|
}
|
2017-10-02 21:24:00 +00:00
|
|
|
|
|
|
|
return success;
|
|
|
|
}
|
|
|
|
|
2019-03-07 11:17:56 +00:00
|
|
|
void GDMono::_install_trace_listener() {
|
|
|
|
#ifdef DEBUG_ENABLED
|
|
|
|
// Install the trace listener now before the project assembly is loaded
|
2019-10-10 23:23:35 +00:00
|
|
|
GDMonoClass *debug_utils = get_core_api_assembly()->get_class(BINDINGS_NAMESPACE, "DebuggingUtils");
|
|
|
|
GDMonoMethod *install_func = debug_utils->get_method("InstallTraceListener");
|
|
|
|
|
2020-04-01 23:20:12 +00:00
|
|
|
MonoException *exc = nullptr;
|
|
|
|
install_func->invoke_raw(nullptr, nullptr, &exc);
|
2019-03-07 11:17:56 +00:00
|
|
|
if (exc) {
|
|
|
|
GDMonoUtils::debug_print_unhandled_exception(exc);
|
2019-10-10 23:23:35 +00:00
|
|
|
ERR_PRINT("Failed to install 'System.Diagnostics.Trace' listener.");
|
2019-03-07 11:17:56 +00:00
|
|
|
}
|
|
|
|
#endif
|
|
|
|
}
|
|
|
|
|
2020-03-18 16:40:04 +00:00
|
|
|
#ifndef GD_MONO_SINGLE_APPDOMAIN
|
2017-10-02 21:24:00 +00:00
|
|
|
Error GDMono::_load_scripts_domain() {
|
2020-04-01 23:20:12 +00:00
|
|
|
ERR_FAIL_COND_V(scripts_domain != nullptr, ERR_BUG);
|
2017-10-02 21:24:00 +00:00
|
|
|
|
2018-08-24 06:47:34 +00:00
|
|
|
print_verbose("Mono: Loading scripts domain...");
|
2017-10-02 21:24:00 +00:00
|
|
|
|
2019-10-10 23:23:35 +00:00
|
|
|
scripts_domain = GDMonoUtils::create_domain("GodotEngine.Domain.Scripts");
|
2017-10-02 21:24:00 +00:00
|
|
|
|
2019-08-09 01:39:45 +00:00
|
|
|
ERR_FAIL_NULL_V_MSG(scripts_domain, ERR_CANT_CREATE, "Mono: Could not create scripts app domain.");
|
2017-10-02 21:24:00 +00:00
|
|
|
|
|
|
|
mono_domain_set(scripts_domain, true);
|
|
|
|
|
|
|
|
return OK;
|
|
|
|
}
|
|
|
|
|
|
|
|
Error GDMono::_unload_scripts_domain() {
|
|
|
|
ERR_FAIL_NULL_V(scripts_domain, ERR_BUG);
|
|
|
|
|
2020-03-18 16:40:04 +00:00
|
|
|
print_verbose("Mono: Finalizing scripts domain...");
|
2017-10-02 21:24:00 +00:00
|
|
|
|
|
|
|
if (mono_domain_get() != root_domain)
|
|
|
|
mono_domain_set(root_domain, true);
|
|
|
|
|
2018-10-06 21:00:18 +00:00
|
|
|
finalizing_scripts_domain = true;
|
|
|
|
|
|
|
|
if (!mono_domain_finalize(scripts_domain, 2000)) {
|
2019-08-09 01:39:45 +00:00
|
|
|
ERR_PRINT("Mono: Domain finalization timeout.");
|
2018-10-06 21:00:18 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
finalizing_scripts_domain = false;
|
2017-10-02 21:24:00 +00:00
|
|
|
|
2017-10-04 22:12:36 +00:00
|
|
|
mono_gc_collect(mono_gc_max_generation());
|
|
|
|
|
2019-11-10 16:10:38 +00:00
|
|
|
GDMonoCache::clear_godot_api_cache();
|
2019-05-20 16:34:35 +00:00
|
|
|
|
2017-10-02 21:24:00 +00:00
|
|
|
_domain_assemblies_cleanup(mono_domain_get_id(scripts_domain));
|
|
|
|
|
2020-04-01 23:20:12 +00:00
|
|
|
core_api_assembly.assembly = nullptr;
|
2019-10-12 19:27:56 +00:00
|
|
|
#ifdef TOOLS_ENABLED
|
2020-04-01 23:20:12 +00:00
|
|
|
editor_api_assembly.assembly = nullptr;
|
2019-10-12 19:27:56 +00:00
|
|
|
#endif
|
|
|
|
|
2020-04-01 23:20:12 +00:00
|
|
|
project_assembly = nullptr;
|
2017-10-02 21:24:00 +00:00
|
|
|
#ifdef TOOLS_ENABLED
|
2020-04-01 23:20:12 +00:00
|
|
|
tools_assembly = nullptr;
|
|
|
|
tools_project_editor_assembly = nullptr;
|
2017-10-02 21:24:00 +00:00
|
|
|
#endif
|
|
|
|
|
|
|
|
MonoDomain *domain = scripts_domain;
|
2020-04-01 23:20:12 +00:00
|
|
|
scripts_domain = nullptr;
|
2017-10-02 21:24:00 +00:00
|
|
|
|
2020-03-18 16:40:04 +00:00
|
|
|
print_verbose("Mono: Unloading scripts domain...");
|
|
|
|
|
2020-04-01 23:20:12 +00:00
|
|
|
MonoException *exc = nullptr;
|
2018-06-26 19:03:42 +00:00
|
|
|
mono_domain_try_unload(domain, (MonoObject **)&exc);
|
2017-10-02 21:24:00 +00:00
|
|
|
|
2018-06-26 19:03:42 +00:00
|
|
|
if (exc) {
|
2019-08-09 01:39:45 +00:00
|
|
|
ERR_PRINT("Exception thrown when unloading scripts domain.");
|
2018-06-26 19:03:42 +00:00
|
|
|
GDMonoUtils::debug_unhandled_exception(exc);
|
2017-10-02 21:24:00 +00:00
|
|
|
return FAILED;
|
|
|
|
}
|
|
|
|
|
|
|
|
return OK;
|
|
|
|
}
|
2020-03-18 16:40:04 +00:00
|
|
|
#endif
|
2017-10-02 21:24:00 +00:00
|
|
|
|
2019-01-22 17:33:36 +00:00
|
|
|
#ifdef GD_MONO_HOT_RELOAD
|
2017-10-02 21:24:00 +00:00
|
|
|
Error GDMono::reload_scripts_domain() {
|
|
|
|
ERR_FAIL_COND_V(!runtime_initialized, ERR_BUG);
|
|
|
|
|
|
|
|
if (scripts_domain) {
|
2019-08-09 01:39:45 +00:00
|
|
|
Error domain_unload_err = _unload_scripts_domain();
|
|
|
|
ERR_FAIL_COND_V_MSG(domain_unload_err != OK, domain_unload_err, "Mono: Failed to unload scripts domain.");
|
2017-10-02 21:24:00 +00:00
|
|
|
}
|
|
|
|
|
2019-04-25 23:19:54 +00:00
|
|
|
CSharpLanguage::get_singleton()->_on_scripts_domain_unloaded();
|
2019-03-07 18:55:40 +00:00
|
|
|
|
2019-08-09 01:39:45 +00:00
|
|
|
Error domain_load_err = _load_scripts_domain();
|
|
|
|
ERR_FAIL_COND_V_MSG(domain_load_err != OK, domain_load_err, "Mono: Failed to load scripts domain.");
|
2017-10-02 21:24:00 +00:00
|
|
|
|
2019-07-11 12:01:25 +00:00
|
|
|
// Load assemblies. The API and tools assemblies are required,
|
|
|
|
// the application is aborted if these assemblies cannot be loaded.
|
2018-02-22 12:13:51 +00:00
|
|
|
|
2019-07-11 12:01:25 +00:00
|
|
|
_load_api_assemblies();
|
2018-02-22 12:13:51 +00:00
|
|
|
|
2019-07-11 12:01:25 +00:00
|
|
|
#if defined(TOOLS_ENABLED)
|
2019-08-09 01:39:45 +00:00
|
|
|
bool tools_assemblies_loaded = _load_tools_assemblies();
|
|
|
|
CRASH_COND_MSG(!tools_assemblies_loaded, "Mono: Failed to load '" TOOLS_ASM_NAME "' assemblies.");
|
2019-07-03 07:44:53 +00:00
|
|
|
#endif
|
|
|
|
|
2019-07-11 12:01:25 +00:00
|
|
|
// Load the project's main assembly. Here, during hot-reloading, we do
|
|
|
|
// consider failing to load the project's main assembly to be an error.
|
|
|
|
// However, unlike the API and tools assemblies, the application can continue working.
|
2018-09-26 21:59:38 +00:00
|
|
|
if (!_load_project_assembly()) {
|
2019-07-11 12:01:25 +00:00
|
|
|
print_error("Mono: Failed to load project assembly");
|
2018-02-22 12:13:51 +00:00
|
|
|
return ERR_CANT_OPEN;
|
2018-09-26 21:59:38 +00:00
|
|
|
}
|
2018-02-22 12:13:51 +00:00
|
|
|
|
2017-10-02 21:24:00 +00:00
|
|
|
return OK;
|
|
|
|
}
|
|
|
|
#endif
|
|
|
|
|
2020-03-18 16:40:04 +00:00
|
|
|
#ifndef GD_MONO_SINGLE_APPDOMAIN
|
2018-02-22 12:39:41 +00:00
|
|
|
Error GDMono::finalize_and_unload_domain(MonoDomain *p_domain) {
|
2020-04-01 23:20:12 +00:00
|
|
|
CRASH_COND(p_domain == nullptr);
|
2019-07-03 07:44:53 +00:00
|
|
|
CRASH_COND(p_domain == GDMono::get_singleton()->get_scripts_domain()); // Should use _unload_scripts_domain() instead
|
2018-02-22 12:39:41 +00:00
|
|
|
|
|
|
|
String domain_name = mono_domain_get_friendly_name(p_domain);
|
|
|
|
|
2019-08-09 01:39:45 +00:00
|
|
|
print_verbose("Mono: Unloading domain '" + domain_name + "'...");
|
2018-02-22 12:39:41 +00:00
|
|
|
|
2018-10-03 17:01:57 +00:00
|
|
|
if (mono_domain_get() == p_domain)
|
2018-02-22 12:39:41 +00:00
|
|
|
mono_domain_set(root_domain, true);
|
|
|
|
|
2018-10-06 21:00:18 +00:00
|
|
|
if (!mono_domain_finalize(p_domain, 2000)) {
|
2019-08-09 01:39:45 +00:00
|
|
|
ERR_PRINT("Mono: Domain finalization timeout.");
|
2018-10-06 21:00:18 +00:00
|
|
|
}
|
2019-03-10 17:54:07 +00:00
|
|
|
|
2018-02-22 12:39:41 +00:00
|
|
|
mono_gc_collect(mono_gc_max_generation());
|
|
|
|
|
|
|
|
_domain_assemblies_cleanup(mono_domain_get_id(p_domain));
|
|
|
|
|
2020-04-01 23:20:12 +00:00
|
|
|
MonoException *exc = nullptr;
|
2018-06-26 19:03:42 +00:00
|
|
|
mono_domain_try_unload(p_domain, (MonoObject **)&exc);
|
2018-02-22 12:39:41 +00:00
|
|
|
|
2018-06-26 19:03:42 +00:00
|
|
|
if (exc) {
|
2019-11-06 16:03:04 +00:00
|
|
|
ERR_PRINT("Exception thrown when unloading domain '" + domain_name + "'.");
|
2019-07-03 07:44:53 +00:00
|
|
|
GDMonoUtils::debug_print_unhandled_exception(exc);
|
2018-02-22 12:39:41 +00:00
|
|
|
return FAILED;
|
|
|
|
}
|
|
|
|
|
|
|
|
return OK;
|
|
|
|
}
|
2020-03-18 16:40:04 +00:00
|
|
|
#endif
|
2018-02-22 12:39:41 +00:00
|
|
|
|
2017-10-02 21:24:00 +00:00
|
|
|
GDMonoClass *GDMono::get_class(MonoClass *p_raw_class) {
|
|
|
|
MonoImage *image = mono_class_get_image(p_raw_class);
|
|
|
|
|
|
|
|
if (image == corlib_assembly->get_image())
|
|
|
|
return corlib_assembly->get_class(p_raw_class);
|
|
|
|
|
|
|
|
uint32_t domain_id = mono_domain_get_id(mono_domain_get());
|
|
|
|
HashMap<String, GDMonoAssembly *> &domain_assemblies = assemblies[domain_id];
|
|
|
|
|
2020-04-01 23:20:12 +00:00
|
|
|
const String *k = nullptr;
|
2017-10-02 21:24:00 +00:00
|
|
|
while ((k = domain_assemblies.next(k))) {
|
|
|
|
GDMonoAssembly *assembly = domain_assemblies.get(*k);
|
|
|
|
if (assembly->get_image() == image) {
|
|
|
|
GDMonoClass *klass = assembly->get_class(p_raw_class);
|
|
|
|
|
|
|
|
if (klass)
|
|
|
|
return klass;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-04-01 23:20:12 +00:00
|
|
|
return nullptr;
|
2017-10-02 21:24:00 +00:00
|
|
|
}
|
|
|
|
|
2019-07-03 07:44:53 +00:00
|
|
|
GDMonoClass *GDMono::get_class(const StringName &p_namespace, const StringName &p_name) {
|
2020-03-18 16:40:04 +00:00
|
|
|
GDMonoClass *klass = corlib_assembly->get_class(p_namespace, p_name);
|
|
|
|
if (klass)
|
|
|
|
return klass;
|
|
|
|
|
2019-07-03 07:44:53 +00:00
|
|
|
uint32_t domain_id = mono_domain_get_id(mono_domain_get());
|
|
|
|
HashMap<String, GDMonoAssembly *> &domain_assemblies = assemblies[domain_id];
|
|
|
|
|
2020-04-01 23:20:12 +00:00
|
|
|
const String *k = nullptr;
|
2019-07-03 07:44:53 +00:00
|
|
|
while ((k = domain_assemblies.next(k))) {
|
|
|
|
GDMonoAssembly *assembly = domain_assemblies.get(*k);
|
2020-03-18 16:40:04 +00:00
|
|
|
klass = assembly->get_class(p_namespace, p_name);
|
2019-07-03 07:44:53 +00:00
|
|
|
if (klass)
|
|
|
|
return klass;
|
|
|
|
}
|
|
|
|
|
2020-04-01 23:20:12 +00:00
|
|
|
return nullptr;
|
2019-07-03 07:44:53 +00:00
|
|
|
}
|
|
|
|
|
2017-10-02 21:24:00 +00:00
|
|
|
void GDMono::_domain_assemblies_cleanup(uint32_t p_domain_id) {
|
|
|
|
HashMap<String, GDMonoAssembly *> &domain_assemblies = assemblies[p_domain_id];
|
|
|
|
|
2020-04-01 23:20:12 +00:00
|
|
|
const String *k = nullptr;
|
2017-10-02 21:24:00 +00:00
|
|
|
while ((k = domain_assemblies.next(k))) {
|
|
|
|
memdelete(domain_assemblies.get(*k));
|
|
|
|
}
|
|
|
|
|
|
|
|
assemblies.erase(p_domain_id);
|
|
|
|
}
|
|
|
|
|
2018-06-26 19:03:42 +00:00
|
|
|
void GDMono::unhandled_exception_hook(MonoObject *p_exc, void *) {
|
2018-07-18 14:24:33 +00:00
|
|
|
// This method will be called by the runtime when a thrown exception is not handled.
|
|
|
|
// It won't be called when we manually treat a thrown exception as unhandled.
|
|
|
|
// We assume the exception was already printed before calling this hook.
|
2018-06-26 19:03:42 +00:00
|
|
|
|
|
|
|
#ifdef DEBUG_ENABLED
|
|
|
|
GDMonoUtils::debug_send_unhandled_exception_error((MonoException *)p_exc);
|
2020-02-27 02:30:20 +00:00
|
|
|
if (EngineDebugger::is_active())
|
|
|
|
EngineDebugger::get_singleton()->poll_events(false);
|
2018-06-26 19:03:42 +00:00
|
|
|
#endif
|
2019-04-18 11:58:55 +00:00
|
|
|
|
|
|
|
exit(mono_environment_exitcode_get());
|
|
|
|
|
2019-01-28 23:02:35 +00:00
|
|
|
GD_UNREACHABLE();
|
2018-06-26 19:03:42 +00:00
|
|
|
}
|
|
|
|
|
2017-10-02 21:24:00 +00:00
|
|
|
GDMono::GDMono() {
|
|
|
|
singleton = this;
|
|
|
|
|
|
|
|
gdmono_log = memnew(GDMonoLog);
|
|
|
|
|
|
|
|
runtime_initialized = false;
|
2018-10-06 21:00:18 +00:00
|
|
|
finalizing_scripts_domain = false;
|
2017-10-02 21:24:00 +00:00
|
|
|
|
2020-04-01 23:20:12 +00:00
|
|
|
root_domain = nullptr;
|
|
|
|
scripts_domain = nullptr;
|
2017-10-02 21:24:00 +00:00
|
|
|
|
2020-04-01 23:20:12 +00:00
|
|
|
corlib_assembly = nullptr;
|
|
|
|
project_assembly = nullptr;
|
2017-10-02 21:24:00 +00:00
|
|
|
#ifdef TOOLS_ENABLED
|
2020-04-01 23:20:12 +00:00
|
|
|
tools_assembly = nullptr;
|
|
|
|
tools_project_editor_assembly = nullptr;
|
2017-10-02 21:24:00 +00:00
|
|
|
#endif
|
|
|
|
|
|
|
|
api_core_hash = 0;
|
|
|
|
#ifdef TOOLS_ENABLED
|
|
|
|
api_editor_hash = 0;
|
|
|
|
#endif
|
2019-07-24 21:13:24 +00:00
|
|
|
|
|
|
|
unhandled_exception_policy = POLICY_TERMINATE_APP;
|
2017-10-02 21:24:00 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
GDMono::~GDMono() {
|
2018-09-12 00:41:54 +00:00
|
|
|
if (is_runtime_initialized()) {
|
2020-03-18 16:40:04 +00:00
|
|
|
#ifndef GD_MONO_SINGLE_APPDOMAIN
|
2019-03-10 17:54:07 +00:00
|
|
|
if (scripts_domain) {
|
2017-10-02 21:24:00 +00:00
|
|
|
Error err = _unload_scripts_domain();
|
|
|
|
if (err != OK) {
|
2019-08-09 01:39:45 +00:00
|
|
|
ERR_PRINT("Mono: Failed to unload scripts domain.");
|
2017-10-02 21:24:00 +00:00
|
|
|
}
|
|
|
|
}
|
2020-03-18 16:40:04 +00:00
|
|
|
#else
|
|
|
|
CRASH_COND(scripts_domain != root_domain);
|
|
|
|
|
|
|
|
print_verbose("Mono: Finalizing scripts domain...");
|
|
|
|
|
|
|
|
if (mono_domain_get() != root_domain)
|
|
|
|
mono_domain_set(root_domain, true);
|
|
|
|
|
|
|
|
finalizing_scripts_domain = true;
|
|
|
|
|
|
|
|
if (!mono_domain_finalize(root_domain, 2000)) {
|
|
|
|
ERR_PRINT("Mono: Domain finalization timeout.");
|
|
|
|
}
|
|
|
|
|
|
|
|
finalizing_scripts_domain = false;
|
|
|
|
|
|
|
|
mono_gc_collect(mono_gc_max_generation());
|
|
|
|
|
|
|
|
GDMonoCache::clear_godot_api_cache();
|
|
|
|
|
|
|
|
_domain_assemblies_cleanup(mono_domain_get_id(root_domain));
|
|
|
|
|
2020-04-01 23:20:12 +00:00
|
|
|
core_api_assembly.assembly = nullptr;
|
2020-03-18 16:40:04 +00:00
|
|
|
|
2020-04-01 23:20:12 +00:00
|
|
|
project_assembly = nullptr;
|
2020-03-18 16:40:04 +00:00
|
|
|
|
2020-04-01 23:20:12 +00:00
|
|
|
root_domain = nullptr;
|
|
|
|
scripts_domain = nullptr;
|
2020-03-18 16:40:04 +00:00
|
|
|
|
|
|
|
// Leave the rest to 'mono_jit_cleanup'
|
|
|
|
#endif
|
2017-10-02 21:24:00 +00:00
|
|
|
|
2020-04-01 23:20:12 +00:00
|
|
|
const uint32_t *k = nullptr;
|
2017-10-02 21:24:00 +00:00
|
|
|
while ((k = assemblies.next(k))) {
|
|
|
|
HashMap<String, GDMonoAssembly *> &domain_assemblies = assemblies.get(*k);
|
|
|
|
|
2020-04-01 23:20:12 +00:00
|
|
|
const String *kk = nullptr;
|
2017-10-02 21:24:00 +00:00
|
|
|
while ((kk = domain_assemblies.next(kk))) {
|
|
|
|
memdelete(domain_assemblies.get(*kk));
|
|
|
|
}
|
|
|
|
}
|
|
|
|
assemblies.clear();
|
|
|
|
|
2018-08-24 06:47:34 +00:00
|
|
|
print_verbose("Mono: Runtime cleanup...");
|
2017-10-02 21:24:00 +00:00
|
|
|
|
|
|
|
mono_jit_cleanup(root_domain);
|
2018-09-12 00:41:54 +00:00
|
|
|
|
2019-03-10 17:54:07 +00:00
|
|
|
print_verbose("Mono: Finalized");
|
|
|
|
|
2018-09-12 00:41:54 +00:00
|
|
|
runtime_initialized = false;
|
2017-10-02 21:24:00 +00:00
|
|
|
}
|
|
|
|
|
2020-03-18 16:40:04 +00:00
|
|
|
#if defined(ANDROID_ENABLED)
|
|
|
|
gdmono::android::support::cleanup();
|
|
|
|
#endif
|
|
|
|
|
2017-10-02 21:24:00 +00:00
|
|
|
if (gdmono_log)
|
|
|
|
memdelete(gdmono_log);
|
2017-10-29 04:57:20 +00:00
|
|
|
|
2020-04-01 23:20:12 +00:00
|
|
|
singleton = nullptr;
|
2017-10-02 21:24:00 +00:00
|
|
|
}
|
|
|
|
|
2020-04-01 23:20:12 +00:00
|
|
|
_GodotSharp *_GodotSharp::singleton = nullptr;
|
2017-10-02 21:24:00 +00:00
|
|
|
|
|
|
|
void _GodotSharp::attach_thread() {
|
|
|
|
GDMonoUtils::attach_current_thread();
|
|
|
|
}
|
|
|
|
|
|
|
|
void _GodotSharp::detach_thread() {
|
|
|
|
GDMonoUtils::detach_current_thread();
|
|
|
|
}
|
|
|
|
|
2018-09-12 00:41:54 +00:00
|
|
|
int32_t _GodotSharp::get_domain_id() {
|
|
|
|
MonoDomain *domain = mono_domain_get();
|
|
|
|
CRASH_COND(!domain); // User must check if runtime is initialized before calling this method
|
|
|
|
return mono_domain_get_id(domain);
|
2017-10-02 21:24:00 +00:00
|
|
|
}
|
|
|
|
|
2018-09-12 00:41:54 +00:00
|
|
|
int32_t _GodotSharp::get_scripts_domain_id() {
|
2019-07-03 07:44:53 +00:00
|
|
|
MonoDomain *domain = GDMono::get_singleton()->get_scripts_domain();
|
2018-09-12 00:41:54 +00:00
|
|
|
CRASH_COND(!domain); // User must check if scripts domain is loaded before calling this method
|
|
|
|
return mono_domain_get_id(domain);
|
2017-10-02 21:24:00 +00:00
|
|
|
}
|
|
|
|
|
2018-09-12 00:41:54 +00:00
|
|
|
bool _GodotSharp::is_scripts_domain_loaded() {
|
2020-04-01 23:20:12 +00:00
|
|
|
return GDMono::get_singleton()->is_runtime_initialized() && GDMono::get_singleton()->get_scripts_domain() != nullptr;
|
2018-09-12 00:41:54 +00:00
|
|
|
}
|
2017-10-02 21:24:00 +00:00
|
|
|
|
2018-09-12 00:41:54 +00:00
|
|
|
bool _GodotSharp::_is_domain_finalizing_for_unload(int32_t p_domain_id) {
|
|
|
|
return is_domain_finalizing_for_unload(p_domain_id);
|
|
|
|
}
|
2017-12-31 21:37:57 +00:00
|
|
|
|
2018-09-12 00:41:54 +00:00
|
|
|
bool _GodotSharp::is_domain_finalizing_for_unload() {
|
|
|
|
return is_domain_finalizing_for_unload(mono_domain_get());
|
|
|
|
}
|
|
|
|
|
|
|
|
bool _GodotSharp::is_domain_finalizing_for_unload(int32_t p_domain_id) {
|
|
|
|
return is_domain_finalizing_for_unload(mono_domain_get_by_id(p_domain_id));
|
|
|
|
}
|
|
|
|
|
|
|
|
bool _GodotSharp::is_domain_finalizing_for_unload(MonoDomain *p_domain) {
|
|
|
|
if (!p_domain)
|
|
|
|
return true;
|
2019-07-03 07:44:53 +00:00
|
|
|
if (p_domain == GDMono::get_singleton()->get_scripts_domain() && GDMono::get_singleton()->is_finalizing_scripts_domain())
|
2018-10-06 21:00:18 +00:00
|
|
|
return true;
|
2018-09-12 00:41:54 +00:00
|
|
|
return mono_domain_is_unloading(p_domain);
|
|
|
|
}
|
|
|
|
|
|
|
|
bool _GodotSharp::is_runtime_shutting_down() {
|
|
|
|
return mono_runtime_is_shutting_down();
|
|
|
|
}
|
|
|
|
|
|
|
|
bool _GodotSharp::is_runtime_initialized() {
|
|
|
|
return GDMono::get_singleton()->is_runtime_initialized();
|
2017-10-02 21:24:00 +00:00
|
|
|
}
|
|
|
|
|
2019-07-03 07:44:53 +00:00
|
|
|
void _GodotSharp::_reload_assemblies(bool p_soft_reload) {
|
|
|
|
#ifdef GD_MONO_HOT_RELOAD
|
2020-04-23 00:23:25 +00:00
|
|
|
// This method may be called more than once with `call_deferred`, so we need to check
|
|
|
|
// again if reloading is needed to avoid reloading multiple times unnecessarily.
|
|
|
|
if (CSharpLanguage::get_singleton()->is_assembly_reloading_needed())
|
|
|
|
CSharpLanguage::get_singleton()->reload_assemblies(p_soft_reload);
|
2019-07-03 07:44:53 +00:00
|
|
|
#endif
|
|
|
|
}
|
|
|
|
|
2017-10-02 21:24:00 +00:00
|
|
|
void _GodotSharp::_bind_methods() {
|
|
|
|
ClassDB::bind_method(D_METHOD("attach_thread"), &_GodotSharp::attach_thread);
|
|
|
|
ClassDB::bind_method(D_METHOD("detach_thread"), &_GodotSharp::detach_thread);
|
|
|
|
|
2018-09-12 00:41:54 +00:00
|
|
|
ClassDB::bind_method(D_METHOD("get_domain_id"), &_GodotSharp::get_domain_id);
|
|
|
|
ClassDB::bind_method(D_METHOD("get_scripts_domain_id"), &_GodotSharp::get_scripts_domain_id);
|
|
|
|
ClassDB::bind_method(D_METHOD("is_scripts_domain_loaded"), &_GodotSharp::is_scripts_domain_loaded);
|
|
|
|
ClassDB::bind_method(D_METHOD("is_domain_finalizing_for_unload", "domain_id"), &_GodotSharp::_is_domain_finalizing_for_unload);
|
|
|
|
|
|
|
|
ClassDB::bind_method(D_METHOD("is_runtime_shutting_down"), &_GodotSharp::is_runtime_shutting_down);
|
|
|
|
ClassDB::bind_method(D_METHOD("is_runtime_initialized"), &_GodotSharp::is_runtime_initialized);
|
2019-07-03 07:44:53 +00:00
|
|
|
ClassDB::bind_method(D_METHOD("_reload_assemblies"), &_GodotSharp::_reload_assemblies);
|
2017-10-02 21:24:00 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
_GodotSharp::_GodotSharp() {
|
|
|
|
singleton = this;
|
|
|
|
}
|
|
|
|
|
|
|
|
_GodotSharp::~_GodotSharp() {
|
2020-04-01 23:20:12 +00:00
|
|
|
singleton = nullptr;
|
2017-10-02 21:24:00 +00:00
|
|
|
}
|