virtualx-engine/modules/mono/mono_gd/gd_mono.cpp

1265 lines
39 KiB
C++
Raw Normal View History

2017-10-02 23:24:00 +02:00
/*************************************************************************/
/* gd_mono.cpp */
/*************************************************************************/
/* This file is part of: */
/* GODOT ENGINE */
/* https://godotengine.org */
/*************************************************************************/
/* Copyright (c) 2007-2019 Juan Linietsky, Ariel Manzur. */
/* Copyright (c) 2014-2019 Godot Engine contributors (cf. AUTHORS.md) */
2017-10-02 23:24:00 +02: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. */
/*************************************************************************/
2017-10-02 23:24:00 +02:00
#include "gd_mono.h"
#include <mono/metadata/environment.h>
#include <mono/metadata/exception.h>
2017-10-02 23:24:00 +02:00
#include <mono/metadata/mono-config.h>
#include <mono/metadata/mono-debug.h>
#include <mono/metadata/mono-gc.h>
2019-03-03 01:57:35 +01:00
#include <mono/metadata/profiler.h>
2017-10-02 23:24:00 +02: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 23:24:00 +02:00
#include "../csharp_script.h"
2018-09-12 02:50:16 +02:00
#include "../glue/cs_glue_version.gen.h"
#include "../godotsharp_dirs.h"
2017-10-02 23:24:00 +02:00
#include "../utils/path_utils.h"
#include "gd_mono_class.h"
#include "gd_mono_marshal.h"
2017-10-02 23:24:00 +02:00
#include "gd_mono_utils.h"
#ifdef TOOLS_ENABLED
#include "main/main.h"
2017-10-02 23:24:00 +02:00
#endif
#ifdef ANDROID_ENABLED
#include "android_mono_config.gen.h"
#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."
2017-10-02 23:24:00 +02:00
GDMono *GDMono::singleton = NULL;
namespace {
void setup_runtime_main_args() {
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);
main_args.write[0] = execpath.ptrw();
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();
main_args.write[i] = stored.ptrw();
i++;
}
mono_runtime_set_main_args(main_args.size(), main_args.ptrw());
}
2019-03-03 01:57:35 +01:00
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());
}
}
2017-10-02 23:24:00 +02:00
#ifdef DEBUG_ENABLED
bool _wait_for_debugger_msecs(uint32_t p_msecs) {
2017-10-02 23:24:00 +02:00
do {
if (mono_is_debugger_attached())
return true;
int last_tick = OS::get_singleton()->get_ticks_msec();
OS::get_singleton()->delay_usec((p_msecs < 25 ? p_msecs : 25) * 1000);
uint32_t tdiff = OS::get_singleton()->get_ticks_msec() - last_tick;
2017-10-02 23:24:00 +02:00
if (tdiff > p_msecs) {
p_msecs = 0;
} else {
p_msecs -= tdiff;
}
} while (p_msecs > 0);
return mono_is_debugger_attached();
}
void gdmono_debug_init() {
mono_debug_init(MONO_DEBUG_FORMAT_MONO);
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);
CharString da_args = OS::get_singleton()->get_environment("GODOT_MONO_DEBUGGER_AGENT").utf8();
2017-10-02 23:24:00 +02:00
#ifdef TOOLS_ENABLED
if (Engine::get_singleton()->is_editor_hint() ||
ProjectSettings::get_singleton()->get_resource_path().empty() ||
Main::is_project_manager()) {
if (da_args.size() == 0)
return;
2017-10-02 23:24:00 +02:00
}
#endif
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();
}
2017-10-02 23:24:00 +02:00
// --debugger-agent=help
const char *options[] = {
"--soft-breakpoints",
da_args.get_data()
};
mono_jit_parse_options(2, (char **)options);
}
2017-10-02 23:24:00 +02:00
#endif
} // 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
}
2017-10-02 23:24:00 +02:00
void GDMono::initialize() {
ERR_FAIL_NULL(Engine::get_singleton());
print_verbose("Mono: Initializing module...");
2017-10-02 23:24:00 +02:00
char *runtime_build_info = mono_get_runtime_build_info();
print_verbose("Mono JIT compiler version " + String(runtime_build_info));
mono_free(runtime_build_info);
2017-10-02 23:24:00 +02:00
#ifdef DEBUG_METHODS_ENABLED
_initialize_and_check_api_hashes();
#endif
GDMonoLog::get_singleton()->initialize();
Mono: Editor and export template dependencies and fixes - Bundle editor dependencies: - 'GodotSharp': Root data directory for the editor - 'Tools': Editor dependencies. Only GodotSharp.dll for now. - 'Api': Prebuilt GodotSharp and GodotSharpEditor API assemblies. - 'Mono': Mono files to bundle with the editor. - 'bin': (Optional, not used for now) Mono bin directory. - 'etc': Mono configuration files. - 'lib': Mono dependency shared libraries. - 'lib/mono/4.5': Framework assemblies. - Added build option to copy the required files from the mono installation to 'GodotSharp/Mono'. Enable with 'copy_mono_root=yes'. Disabled by default. - Export template dependencies: - 'data_AppName'/'data_Godot': - 'Mono': Mono files to bundle with the game. - 'etc': Mono configuration files. - 'lib': Mono dependency shared libraries. - The data directory is generated when compiling and must be bundled with the export templates. In the case of OSX, the data directory must be placed inside the 'osx.zip' export template. - In OSX, alternative location for directories (needed for app bundles) are: - 'data_AppName/Mono/etc' --> '../Resources/GodotSharp/Mono/etc' - 'data_AppName/Mono/lib' --> '../Frameworks/GodotSharp/Mono/lib' - The editor can bundle prebuilt API assemblies. - Generate them with a tools build by running: `--generate-cs-core-api <GodotSharp_OutputDir> --generate-cs-editor-api <GodotSharpEditor_OutputDir> <GodotSharp_OutputDir>/bin/Release/GodotSharp.dll` (This command will be simplified in the future and both projects will be in the same solution) - Build the solutions and copy the output files to '#bin/GodotSharp/Api'. - Fixed API assembly being added twice during the export process.
2018-10-03 19:01:57 +02:00
String assembly_rootdir;
String config_dir;
#ifdef TOOLS_ENABLED
#if defined(WINDOWS_ENABLED)
2017-10-02 23:24:00 +02:00
mono_reg_info = MonoRegUtils::find_mono();
if (mono_reg_info.assembly_dir.length() && DirAccess::exists(mono_reg_info.assembly_dir)) {
Mono: Editor and export template dependencies and fixes - Bundle editor dependencies: - 'GodotSharp': Root data directory for the editor - 'Tools': Editor dependencies. Only GodotSharp.dll for now. - 'Api': Prebuilt GodotSharp and GodotSharpEditor API assemblies. - 'Mono': Mono files to bundle with the editor. - 'bin': (Optional, not used for now) Mono bin directory. - 'etc': Mono configuration files. - 'lib': Mono dependency shared libraries. - 'lib/mono/4.5': Framework assemblies. - Added build option to copy the required files from the mono installation to 'GodotSharp/Mono'. Enable with 'copy_mono_root=yes'. Disabled by default. - Export template dependencies: - 'data_AppName'/'data_Godot': - 'Mono': Mono files to bundle with the game. - 'etc': Mono configuration files. - 'lib': Mono dependency shared libraries. - The data directory is generated when compiling and must be bundled with the export templates. In the case of OSX, the data directory must be placed inside the 'osx.zip' export template. - In OSX, alternative location for directories (needed for app bundles) are: - 'data_AppName/Mono/etc' --> '../Resources/GodotSharp/Mono/etc' - 'data_AppName/Mono/lib' --> '../Frameworks/GodotSharp/Mono/lib' - The editor can bundle prebuilt API assemblies. - Generate them with a tools build by running: `--generate-cs-core-api <GodotSharp_OutputDir> --generate-cs-editor-api <GodotSharpEditor_OutputDir> <GodotSharp_OutputDir>/bin/Release/GodotSharp.dll` (This command will be simplified in the future and both projects will be in the same solution) - Build the solutions and copy the output files to '#bin/GodotSharp/Api'. - Fixed API assembly being added twice during the export process.
2018-10-03 19:01:57 +02:00
assembly_rootdir = mono_reg_info.assembly_dir;
2017-10-02 23:24:00 +02:00
}
if (mono_reg_info.config_dir.length() && DirAccess::exists(mono_reg_info.config_dir)) {
Mono: Editor and export template dependencies and fixes - Bundle editor dependencies: - 'GodotSharp': Root data directory for the editor - 'Tools': Editor dependencies. Only GodotSharp.dll for now. - 'Api': Prebuilt GodotSharp and GodotSharpEditor API assemblies. - 'Mono': Mono files to bundle with the editor. - 'bin': (Optional, not used for now) Mono bin directory. - 'etc': Mono configuration files. - 'lib': Mono dependency shared libraries. - 'lib/mono/4.5': Framework assemblies. - Added build option to copy the required files from the mono installation to 'GodotSharp/Mono'. Enable with 'copy_mono_root=yes'. Disabled by default. - Export template dependencies: - 'data_AppName'/'data_Godot': - 'Mono': Mono files to bundle with the game. - 'etc': Mono configuration files. - 'lib': Mono dependency shared libraries. - The data directory is generated when compiling and must be bundled with the export templates. In the case of OSX, the data directory must be placed inside the 'osx.zip' export template. - In OSX, alternative location for directories (needed for app bundles) are: - 'data_AppName/Mono/etc' --> '../Resources/GodotSharp/Mono/etc' - 'data_AppName/Mono/lib' --> '../Frameworks/GodotSharp/Mono/lib' - The editor can bundle prebuilt API assemblies. - Generate them with a tools build by running: `--generate-cs-core-api <GodotSharp_OutputDir> --generate-cs-editor-api <GodotSharpEditor_OutputDir> <GodotSharp_OutputDir>/bin/Release/GodotSharp.dll` (This command will be simplified in the future and both projects will be in the same solution) - Build the solutions and copy the output files to '#bin/GodotSharp/Api'. - Fixed API assembly being added twice during the export process.
2018-10-03 19:01:57 +02:00
config_dir = mono_reg_info.config_dir;
2017-10-02 23:24:00 +02:00
}
#elif defined(OSX_ENABLED)
Mono: Editor and export template dependencies and fixes - Bundle editor dependencies: - 'GodotSharp': Root data directory for the editor - 'Tools': Editor dependencies. Only GodotSharp.dll for now. - 'Api': Prebuilt GodotSharp and GodotSharpEditor API assemblies. - 'Mono': Mono files to bundle with the editor. - 'bin': (Optional, not used for now) Mono bin directory. - 'etc': Mono configuration files. - 'lib': Mono dependency shared libraries. - 'lib/mono/4.5': Framework assemblies. - Added build option to copy the required files from the mono installation to 'GodotSharp/Mono'. Enable with 'copy_mono_root=yes'. Disabled by default. - Export template dependencies: - 'data_AppName'/'data_Godot': - 'Mono': Mono files to bundle with the game. - 'etc': Mono configuration files. - 'lib': Mono dependency shared libraries. - The data directory is generated when compiling and must be bundled with the export templates. In the case of OSX, the data directory must be placed inside the 'osx.zip' export template. - In OSX, alternative location for directories (needed for app bundles) are: - 'data_AppName/Mono/etc' --> '../Resources/GodotSharp/Mono/etc' - 'data_AppName/Mono/lib' --> '../Frameworks/GodotSharp/Mono/lib' - The editor can bundle prebuilt API assemblies. - Generate them with a tools build by running: `--generate-cs-core-api <GodotSharp_OutputDir> --generate-cs-editor-api <GodotSharpEditor_OutputDir> <GodotSharp_OutputDir>/bin/Release/GodotSharp.dll` (This command will be simplified in the future and both projects will be in the same solution) - Build the solutions and copy the output files to '#bin/GodotSharp/Api'. - Fixed API assembly being added twice during the export process.
2018-10-03 19:01:57 +02: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++) {
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");
if (FileAccess::exists(hint_mscorlib_path) && DirAccess::exists(hint_config_dir)) {
assembly_rootdir = hint_assembly_rootdir;
config_dir = hint_config_dir;
break;
}
}
}
Mono: Editor and export template dependencies and fixes - Bundle editor dependencies: - 'GodotSharp': Root data directory for the editor - 'Tools': Editor dependencies. Only GodotSharp.dll for now. - 'Api': Prebuilt GodotSharp and GodotSharpEditor API assemblies. - 'Mono': Mono files to bundle with the editor. - 'bin': (Optional, not used for now) Mono bin directory. - 'etc': Mono configuration files. - 'lib': Mono dependency shared libraries. - 'lib/mono/4.5': Framework assemblies. - Added build option to copy the required files from the mono installation to 'GodotSharp/Mono'. Enable with 'copy_mono_root=yes'. Disabled by default. - Export template dependencies: - 'data_AppName'/'data_Godot': - 'Mono': Mono files to bundle with the game. - 'etc': Mono configuration files. - 'lib': Mono dependency shared libraries. - The data directory is generated when compiling and must be bundled with the export templates. In the case of OSX, the data directory must be placed inside the 'osx.zip' export template. - In OSX, alternative location for directories (needed for app bundles) are: - 'data_AppName/Mono/etc' --> '../Resources/GodotSharp/Mono/etc' - 'data_AppName/Mono/lib' --> '../Frameworks/GodotSharp/Mono/lib' - The editor can bundle prebuilt API assemblies. - Generate them with a tools build by running: `--generate-cs-core-api <GodotSharp_OutputDir> --generate-cs-editor-api <GodotSharpEditor_OutputDir> <GodotSharp_OutputDir>/bin/Release/GodotSharp.dll` (This command will be simplified in the future and both projects will be in the same solution) - Build the solutions and copy the output files to '#bin/GodotSharp/Api'. - Fixed API assembly being added twice during the export process.
2018-10-03 19:01:57 +02:00
#endif
#endif // TOOLS_ENABLED
String bundled_assembly_rootdir = GodotSharpDirs::get_data_mono_lib_dir();
String bundled_config_dir = GodotSharpDirs::get_data_mono_etc_dir();
#ifdef TOOLS_ENABLED
if (DirAccess::exists(bundled_assembly_rootdir)) {
Mono: Editor and export template dependencies and fixes - Bundle editor dependencies: - 'GodotSharp': Root data directory for the editor - 'Tools': Editor dependencies. Only GodotSharp.dll for now. - 'Api': Prebuilt GodotSharp and GodotSharpEditor API assemblies. - 'Mono': Mono files to bundle with the editor. - 'bin': (Optional, not used for now) Mono bin directory. - 'etc': Mono configuration files. - 'lib': Mono dependency shared libraries. - 'lib/mono/4.5': Framework assemblies. - Added build option to copy the required files from the mono installation to 'GodotSharp/Mono'. Enable with 'copy_mono_root=yes'. Disabled by default. - Export template dependencies: - 'data_AppName'/'data_Godot': - 'Mono': Mono files to bundle with the game. - 'etc': Mono configuration files. - 'lib': Mono dependency shared libraries. - The data directory is generated when compiling and must be bundled with the export templates. In the case of OSX, the data directory must be placed inside the 'osx.zip' export template. - In OSX, alternative location for directories (needed for app bundles) are: - 'data_AppName/Mono/etc' --> '../Resources/GodotSharp/Mono/etc' - 'data_AppName/Mono/lib' --> '../Frameworks/GodotSharp/Mono/lib' - The editor can bundle prebuilt API assemblies. - Generate them with a tools build by running: `--generate-cs-core-api <GodotSharp_OutputDir> --generate-cs-editor-api <GodotSharpEditor_OutputDir> <GodotSharp_OutputDir>/bin/Release/GodotSharp.dll` (This command will be simplified in the future and both projects will be in the same solution) - Build the solutions and copy the output files to '#bin/GodotSharp/Api'. - Fixed API assembly being added twice during the export process.
2018-10-03 19:01:57 +02:00
assembly_rootdir = bundled_assembly_rootdir;
}
if (DirAccess::exists(bundled_config_dir)) {
Mono: Editor and export template dependencies and fixes - Bundle editor dependencies: - 'GodotSharp': Root data directory for the editor - 'Tools': Editor dependencies. Only GodotSharp.dll for now. - 'Api': Prebuilt GodotSharp and GodotSharpEditor API assemblies. - 'Mono': Mono files to bundle with the editor. - 'bin': (Optional, not used for now) Mono bin directory. - 'etc': Mono configuration files. - 'lib': Mono dependency shared libraries. - 'lib/mono/4.5': Framework assemblies. - Added build option to copy the required files from the mono installation to 'GodotSharp/Mono'. Enable with 'copy_mono_root=yes'. Disabled by default. - Export template dependencies: - 'data_AppName'/'data_Godot': - 'Mono': Mono files to bundle with the game. - 'etc': Mono configuration files. - 'lib': Mono dependency shared libraries. - The data directory is generated when compiling and must be bundled with the export templates. In the case of OSX, the data directory must be placed inside the 'osx.zip' export template. - In OSX, alternative location for directories (needed for app bundles) are: - 'data_AppName/Mono/etc' --> '../Resources/GodotSharp/Mono/etc' - 'data_AppName/Mono/lib' --> '../Frameworks/GodotSharp/Mono/lib' - The editor can bundle prebuilt API assemblies. - Generate them with a tools build by running: `--generate-cs-core-api <GodotSharp_OutputDir> --generate-cs-editor-api <GodotSharpEditor_OutputDir> <GodotSharp_OutputDir>/bin/Release/GodotSharp.dll` (This command will be simplified in the future and both projects will be in the same solution) - Build the solutions and copy the output files to '#bin/GodotSharp/Api'. - Fixed API assembly being added twice during the export process.
2018-10-03 19:01:57 +02:00
config_dir = bundled_config_dir;
}
#ifdef WINDOWS_ENABLED
if (assembly_rootdir.empty() || config_dir.empty()) {
ERR_PRINT("Cannot find Mono in the registry");
// 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 23:24:00 +02:00
#else
Mono: Editor and export template dependencies and fixes - Bundle editor dependencies: - 'GodotSharp': Root data directory for the editor - 'Tools': Editor dependencies. Only GodotSharp.dll for now. - 'Api': Prebuilt GodotSharp and GodotSharpEditor API assemblies. - 'Mono': Mono files to bundle with the editor. - 'bin': (Optional, not used for now) Mono bin directory. - 'etc': Mono configuration files. - 'lib': Mono dependency shared libraries. - 'lib/mono/4.5': Framework assemblies. - Added build option to copy the required files from the mono installation to 'GodotSharp/Mono'. Enable with 'copy_mono_root=yes'. Disabled by default. - Export template dependencies: - 'data_AppName'/'data_Godot': - 'Mono': Mono files to bundle with the game. - 'etc': Mono configuration files. - 'lib': Mono dependency shared libraries. - The data directory is generated when compiling and must be bundled with the export templates. In the case of OSX, the data directory must be placed inside the 'osx.zip' export template. - In OSX, alternative location for directories (needed for app bundles) are: - 'data_AppName/Mono/etc' --> '../Resources/GodotSharp/Mono/etc' - 'data_AppName/Mono/lib' --> '../Frameworks/GodotSharp/Mono/lib' - The editor can bundle prebuilt API assemblies. - Generate them with a tools build by running: `--generate-cs-core-api <GodotSharp_OutputDir> --generate-cs-editor-api <GodotSharpEditor_OutputDir> <GodotSharp_OutputDir>/bin/Release/GodotSharp.dll` (This command will be simplified in the future and both projects will be in the same solution) - Build the solutions and copy the output files to '#bin/GodotSharp/Api'. - Fixed API assembly being added twice during the export process.
2018-10-03 19:01:57 +02:00
// These are always the directories in export templates
assembly_rootdir = bundled_assembly_rootdir;
config_dir = bundled_config_dir;
#endif // TOOLS_ENABLED
2017-10-02 23:24:00 +02:00
Mono: Editor and export template dependencies and fixes - Bundle editor dependencies: - 'GodotSharp': Root data directory for the editor - 'Tools': Editor dependencies. Only GodotSharp.dll for now. - 'Api': Prebuilt GodotSharp and GodotSharpEditor API assemblies. - 'Mono': Mono files to bundle with the editor. - 'bin': (Optional, not used for now) Mono bin directory. - 'etc': Mono configuration files. - 'lib': Mono dependency shared libraries. - 'lib/mono/4.5': Framework assemblies. - Added build option to copy the required files from the mono installation to 'GodotSharp/Mono'. Enable with 'copy_mono_root=yes'. Disabled by default. - Export template dependencies: - 'data_AppName'/'data_Godot': - 'Mono': Mono files to bundle with the game. - 'etc': Mono configuration files. - 'lib': Mono dependency shared libraries. - The data directory is generated when compiling and must be bundled with the export templates. In the case of OSX, the data directory must be placed inside the 'osx.zip' export template. - In OSX, alternative location for directories (needed for app bundles) are: - 'data_AppName/Mono/etc' --> '../Resources/GodotSharp/Mono/etc' - 'data_AppName/Mono/lib' --> '../Frameworks/GodotSharp/Mono/lib' - The editor can bundle prebuilt API assemblies. - Generate them with a tools build by running: `--generate-cs-core-api <GodotSharp_OutputDir> --generate-cs-editor-api <GodotSharpEditor_OutputDir> <GodotSharp_OutputDir>/bin/Release/GodotSharp.dll` (This command will be simplified in the future and both projects will be in the same solution) - Build the solutions and copy the output files to '#bin/GodotSharp/Api'. - Fixed API assembly being added twice during the export process.
2018-10-03 19:01:57 +02:00
// 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();
2017-10-02 23:24:00 +02:00
GDMonoAssembly::initialize();
2019-03-03 01:57:35 +01:00
gdmono_profiler_init();
2017-10-02 23:24:00 +02:00
#ifdef DEBUG_ENABLED
gdmono_debug_init();
#endif
#ifdef ANDROID_ENABLED
mono_config_parse_memory(get_godot_android_mono_config().utf8().get_data());
#else
2017-10-02 23:24:00 +02:00
mono_config_parse(NULL);
#endif
2017-10-02 23:24:00 +02:00
2018-06-26 21:03:42 +02:00
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
2017-10-02 23:24:00 +02:00
root_domain = mono_jit_init_version("GodotEngine.RootDomain", "v4.0.30319");
ERR_EXPLAIN("Mono: Failed to initialize runtime");
ERR_FAIL_NULL(root_domain);
GDMonoUtils::set_main_thread(GDMonoUtils::get_current_thread());
setup_runtime_main_args(); // Required for System.Environment.GetCommandLineArgs
2017-10-02 23:24:00 +02:00
runtime_initialized = true;
print_verbose("Mono: Runtime initialized");
2017-10-02 23:24:00 +02:00
// mscorlib assembly MUST be present at initialization
ERR_EXPLAIN("Mono: Failed to load mscorlib assembly");
ERR_FAIL_COND(!_load_corlib_assembly());
ERR_EXPLAIN("Mono: Failed to load scripts domain");
ERR_FAIL_COND(_load_scripts_domain() != OK);
#ifdef DEBUG_ENABLED
bool debugger_attached = _wait_for_debugger_msecs(500);
if (!debugger_attached && OS::get_singleton()->is_stdout_verbose())
print_error("Mono: Debugger wait timeout");
2017-10-02 23:24:00 +02:00
#endif
_register_internal_calls();
// The following assemblies are not required at initialization
#ifdef MONO_GLUE_ENABLED
if (_load_api_assemblies()) {
// Everything is fine with the api assemblies, load the tools and project assemblies
#if defined(TOOLS_ENABLED)
ERR_EXPLAIN("Mono: Failed to load GodotTools assemblies");
ERR_FAIL_COND(!_load_tools_assemblies());
#endif
_load_project_assembly();
} else {
Mono: Editor and export template dependencies and fixes - Bundle editor dependencies: - 'GodotSharp': Root data directory for the editor - 'Tools': Editor dependencies. Only GodotSharp.dll for now. - 'Api': Prebuilt GodotSharp and GodotSharpEditor API assemblies. - 'Mono': Mono files to bundle with the editor. - 'bin': (Optional, not used for now) Mono bin directory. - 'etc': Mono configuration files. - 'lib': Mono dependency shared libraries. - 'lib/mono/4.5': Framework assemblies. - Added build option to copy the required files from the mono installation to 'GodotSharp/Mono'. Enable with 'copy_mono_root=yes'. Disabled by default. - Export template dependencies: - 'data_AppName'/'data_Godot': - 'Mono': Mono files to bundle with the game. - 'etc': Mono configuration files. - 'lib': Mono dependency shared libraries. - The data directory is generated when compiling and must be bundled with the export templates. In the case of OSX, the data directory must be placed inside the 'osx.zip' export template. - In OSX, alternative location for directories (needed for app bundles) are: - 'data_AppName/Mono/etc' --> '../Resources/GodotSharp/Mono/etc' - 'data_AppName/Mono/lib' --> '../Frameworks/GodotSharp/Mono/lib' - The editor can bundle prebuilt API assemblies. - Generate them with a tools build by running: `--generate-cs-core-api <GodotSharp_OutputDir> --generate-cs-editor-api <GodotSharpEditor_OutputDir> <GodotSharp_OutputDir>/bin/Release/GodotSharp.dll` (This command will be simplified in the future and both projects will be in the same solution) - Build the solutions and copy the output files to '#bin/GodotSharp/Api'. - Fixed API assembly being added twice during the export process.
2018-10-03 19:01:57 +02:00
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(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 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(OUT_OF_SYNC_ERR_MESSAGE(EDITOR_API_ASSEMBLY_NAME));
metadata_set_api_assembly_invalidated(APIAssembly::API_EDITOR, true);
}
print_line("Mono: Proceeding to unload scripts domain because of invalid API assemblies.");
Error err = _unload_scripts_domain();
if (err != OK) {
WARN_PRINT("Mono: Failed to unload scripts domain");
}
#else
ERR_PRINT("The loaded API assembly is invalid");
CRASH_NOW();
#endif // TOOLS_ENABLED
}
}
#else
print_verbose("Mono: Glue disabled, ignoring script assemblies.");
#endif // MONO_GLUE_ENABLED
2017-10-02 23:24:00 +02:00
print_verbose("Mono: INITIALIZED");
2017-10-02 23:24:00 +02:00
}
#ifdef MONO_GLUE_ENABLED
2017-10-02 23:24:00 +02:00
namespace GodotSharpBindings {
uint64_t get_core_api_hash();
#ifdef TOOLS_ENABLED
2017-10-02 23:24:00 +02:00
uint64_t get_editor_api_hash();
2019-01-29 00:02:35 +01:00
#endif
uint32_t get_bindings_version();
2017-10-02 23:24:00 +02:00
void register_generated_icalls();
} // namespace GodotSharpBindings
#endif
void GDMono::_register_internal_calls() {
#ifdef MONO_GLUE_ENABLED
2017-10-02 23:24:00 +02:00
GodotSharpBindings::register_generated_icalls();
#endif
}
void GDMono::_initialize_and_check_api_hashes() {
#ifdef MONO_GLUE_ENABLED
2019-01-29 00:02:35 +01:00
if (get_api_core_hash() != GodotSharpBindings::get_core_api_hash()) {
2017-10-02 23:24:00 +02:00
ERR_PRINT("Mono: Core API hash mismatch!");
}
#ifdef TOOLS_ENABLED
2019-01-29 00:02:35 +01:00
if (get_api_editor_hash() != GodotSharpBindings::get_editor_api_hash()) {
2017-10-02 23:24:00 +02:00
ERR_PRINT("Mono: Editor API hash mismatch!");
}
#endif // TOOLS_ENABLED
2019-01-29 00:02:35 +01:00
#endif // MONO_GLUE_ENABLED
2017-10-02 23:24:00 +02:00
}
void GDMono::add_assembly(uint32_t p_domain_id, GDMonoAssembly *p_assembly) {
assemblies[p_domain_id][p_assembly->get_name()] = p_assembly;
}
2017-10-07 04:21:55 +02:00
GDMonoAssembly **GDMono::get_loaded_assembly(const String &p_name) {
MonoDomain *domain = mono_domain_get();
uint32_t domain_id = domain ? mono_domain_get_id(domain) : 0;
return assemblies[domain_id].getptr(p_name);
}
2018-02-22 13:39:41 +01:00
bool GDMono::load_assembly(const String &p_name, GDMonoAssembly **r_assembly, bool p_refonly) {
CRASH_COND(!r_assembly);
MonoAssemblyName *aname = mono_assembly_name_new(p_name.utf8());
bool result = load_assembly(p_name, aname, r_assembly, p_refonly);
2018-02-22 13:39:41 +01:00
mono_assembly_name_free(aname);
mono_free(aname);
return result;
}
bool GDMono::load_assembly(const String &p_name, MonoAssemblyName *p_aname, GDMonoAssembly **r_assembly, bool p_refonly) {
2017-10-02 23:24:00 +02:00
CRASH_COND(!r_assembly);
print_verbose("Mono: Loading assembly " + p_name + (p_refonly ? " (refonly)" : "") + "...");
2017-10-02 23:24:00 +02:00
MonoImageOpenStatus status = MONO_IMAGE_OK;
MonoAssembly *assembly = mono_assembly_load_full(p_aname, NULL, &status, p_refonly);
2017-10-02 23:24:00 +02:00
if (!assembly)
return false;
2017-10-02 23:24:00 +02:00
2018-02-22 13:39:41 +01:00
ERR_FAIL_COND_V(status != MONO_IMAGE_OK, false);
2017-10-02 23:24:00 +02:00
uint32_t domain_id = mono_domain_get_id(mono_domain_get());
GDMonoAssembly **stored_assembly = assemblies[domain_id].getptr(p_name);
2017-10-07 04:21:55 +02:00
ERR_FAIL_COND_V(stored_assembly == NULL, false);
ERR_FAIL_COND_V((*stored_assembly)->get_assembly() != assembly, false);
2018-02-22 13:39:41 +01:00
2017-10-07 04:21:55 +02:00
*r_assembly = *stored_assembly;
2017-10-02 23:24:00 +02:00
print_verbose("Mono: Assembly " + p_name + (p_refonly ? " (refonly)" : "") + " loaded from path: " + (*r_assembly)->get_path());
2017-10-02 23:24:00 +02:00
return true;
}
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;
#ifdef DEBUG_ENABLED
uint32_t domain_id = mono_domain_get_id(mono_domain_get());
GDMonoAssembly **stored_assembly = assemblies[domain_id].getptr(p_name);
ERR_FAIL_COND_V(stored_assembly == NULL, false);
ERR_FAIL_COND_V(*stored_assembly != assembly, false);
#endif
*r_assembly = assembly;
print_verbose("Mono: Assembly " + p_name + (p_refonly ? " (refonly)" : "") + " loaded from path: " + (*r_assembly)->get_path());
return true;
}
APIAssembly::Version APIAssembly::Version::get_from_loaded_assembly(GDMonoAssembly *p_api_assembly, APIAssembly::Type p_api_type) {
APIAssembly::Version api_assembly_version;
const char *nativecalls_name = p_api_type == APIAssembly::API_CORE ?
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)
api_assembly_version.godot_api_hash = GDMonoMarshal::unbox<uint64_t>(api_hash_field->get_value(NULL));
GDMonoField *binds_ver_field = nativecalls_klass->get_field("bindings_version");
if (binds_ver_field)
api_assembly_version.bindings_version = GDMonoMarshal::unbox<uint32_t>(binds_ver_field->get_value(NULL));
GDMonoField *cs_glue_ver_field = nativecalls_klass->get_field("cs_glue_version");
if (cs_glue_ver_field)
api_assembly_version.cs_glue_version = GDMonoMarshal::unbox<uint32_t>(cs_glue_ver_field->get_value(NULL));
}
return api_assembly_version;
}
String APIAssembly::to_string(APIAssembly::Type p_type) {
return p_type == APIAssembly::API_CORE ? "API_CORE" : "API_EDITOR";
}
2017-10-02 23:24:00 +02:00
bool GDMono::_load_corlib_assembly() {
if (corlib_assembly)
return true;
2018-02-22 13:39:41 +01:00
bool success = load_assembly("mscorlib", &corlib_assembly);
2017-10-02 23:24:00 +02:00
if (success)
GDMonoUtils::update_corlib_cache();
return success;
}
static bool copy_api_assembly(const String &p_src_dir, const String &p_dst_dir, const String &p_assembly_name, APIAssembly::Type p_api_type) {
// Create destination directory if needed
if (!DirAccess::exists(p_dst_dir)) {
DirAccess *da = DirAccess::create_for_path(p_dst_dir);
Error err = da->make_dir_recursive(p_dst_dir);
memdelete(da);
if (err != OK) {
ERR_PRINTS("Failed to create destination directory for the API assemblies. Error: " + itos(err));
return false;
}
}
String assembly_file = p_assembly_name + ".dll";
String assembly_src = p_src_dir.plus_file(assembly_file);
String assembly_dst = p_dst_dir.plus_file(assembly_file);
if (!FileAccess::exists(assembly_dst) ||
FileAccess::get_modified_time(assembly_src) > FileAccess::get_modified_time(assembly_dst) ||
GDMono::get_singleton()->metadata_is_api_assembly_invalidated(p_api_type)) {
DirAccessRef da = DirAccess::create(DirAccess::ACCESS_FILESYSTEM);
String xml_file = p_assembly_name + ".xml";
if (da->copy(p_src_dir.plus_file(xml_file), p_dst_dir.plus_file(xml_file)) != OK)
WARN_PRINTS("Failed to copy " + xml_file);
String pdb_file = p_assembly_name + ".pdb";
if (da->copy(p_src_dir.plus_file(pdb_file), p_dst_dir.plus_file(pdb_file)) != OK)
WARN_PRINTS("Failed to copy " + pdb_file);
Error err = da->copy(assembly_src, assembly_dst);
if (err != OK) {
ERR_PRINTS("Failed to copy " + assembly_file);
return false;
}
GDMono::get_singleton()->metadata_set_api_assembly_invalidated(p_api_type, false);
}
return true;
}
2017-10-02 23:24:00 +02:00
bool GDMono::_load_core_api_assembly() {
if (core_api_assembly)
2017-10-02 23:24:00 +02:00
return true;
#ifdef TOOLS_ENABLED
if (metadata_is_api_assembly_invalidated(APIAssembly::API_CORE)) {
String prebuilt_api_dir = GodotSharpDirs::get_data_editor_prebuilt_api_dir().plus_file("Debug");
String prebuilt_dll_path = prebuilt_api_dir.plus_file(CORE_API_ASSEMBLY_NAME ".dll");
String invalidated_dll_path = get_invalidated_api_assembly_path(APIAssembly::API_CORE);
if (!FileAccess::exists(prebuilt_dll_path) ||
FileAccess::get_modified_time(invalidated_dll_path) == FileAccess::get_modified_time(prebuilt_dll_path)) {
print_verbose("Mono: Skipping loading of Core API assembly because it was invalidated");
return false;
} else {
// Copy the prebuilt Api
String res_assemblies_dir = GodotSharpDirs::get_res_assemblies_dir();
if (!copy_api_assembly(prebuilt_api_dir, res_assemblies_dir, CORE_API_ASSEMBLY_NAME, APIAssembly::API_CORE) ||
!copy_api_assembly(prebuilt_api_dir, res_assemblies_dir, EDITOR_API_ASSEMBLY_NAME, APIAssembly::API_EDITOR)) {
print_verbose("Mono: Failed to copy prebuilt API. Skipping loading of Core API assembly because it was invalidated");
return false;
}
}
}
#endif
String assembly_path = GodotSharpDirs::get_res_assemblies_dir().plus_file(CORE_API_ASSEMBLY_NAME ".dll");
bool success = (FileAccess::exists(assembly_path) &&
load_assembly_from(CORE_API_ASSEMBLY_NAME, assembly_path, &core_api_assembly)) ||
load_assembly(CORE_API_ASSEMBLY_NAME, &core_api_assembly);
2017-10-02 23:24:00 +02:00
if (success) {
#ifdef MONO_GLUE_ENABLED
APIAssembly::Version api_assembly_ver = APIAssembly::Version::get_from_loaded_assembly(core_api_assembly, APIAssembly::API_CORE);
core_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 ||
2018-09-12 02:50:16 +02:00
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
2017-10-02 23:24:00 +02:00
GDMonoUtils::update_godot_api_cache();
#endif
}
2017-10-02 23:24:00 +02:00
return success;
}
#ifdef TOOLS_ENABLED
bool GDMono::_load_editor_api_assembly() {
if (editor_api_assembly)
return true;
if (metadata_is_api_assembly_invalidated(APIAssembly::API_EDITOR)) {
String prebuilt_api_dir = GodotSharpDirs::get_data_editor_prebuilt_api_dir().plus_file("Debug");
String prebuilt_dll_path = prebuilt_api_dir.plus_file(EDITOR_API_ASSEMBLY_NAME ".dll");
String invalidated_dll_path = get_invalidated_api_assembly_path(APIAssembly::API_EDITOR);
if (!FileAccess::exists(prebuilt_dll_path) ||
FileAccess::get_modified_time(invalidated_dll_path) == FileAccess::get_modified_time(prebuilt_dll_path)) {
print_verbose("Mono: Skipping loading of Editor API assembly because it was invalidated");
return false;
} else {
// Copy the prebuilt editor Api (no need to copy the core api if we got to this point)
String res_assemblies_dir = GodotSharpDirs::get_res_assemblies_dir();
if (!copy_api_assembly(prebuilt_api_dir, res_assemblies_dir, EDITOR_API_ASSEMBLY_NAME, APIAssembly::API_EDITOR)) {
print_verbose("Mono: Failed to copy prebuilt API. Skipping loading of Editor API assembly because it was invalidated");
return false;
}
}
}
String assembly_path = GodotSharpDirs::get_res_assemblies_dir().plus_file(EDITOR_API_ASSEMBLY_NAME ".dll");
bool success = (FileAccess::exists(assembly_path) &&
load_assembly_from(EDITOR_API_ASSEMBLY_NAME, assembly_path, &editor_api_assembly)) ||
load_assembly(EDITOR_API_ASSEMBLY_NAME, &editor_api_assembly);
if (success) {
#ifdef MONO_GLUE_ENABLED
APIAssembly::Version api_assembly_ver = APIAssembly::Version::get_from_loaded_assembly(editor_api_assembly, APIAssembly::API_EDITOR);
editor_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 ||
2018-09-12 02:50:16 +02:00
CS_GLUE_VERSION != api_assembly_ver.cs_glue_version;
#endif
}
return success;
2017-10-02 23:24:00 +02:00
}
#endif
#ifdef TOOLS_ENABLED
bool GDMono::_load_tools_assemblies() {
2017-10-02 23:24:00 +02:00
if (tools_assembly && tools_project_editor_assembly)
2017-10-02 23:24:00 +02:00
return true;
bool success = load_assembly(TOOLS_ASSEMBLY_NAME, &tools_assembly) &&
load_assembly(TOOLS_PROJECT_EDITOR_ASSEMBLY_NAME, &tools_project_editor_assembly);
2017-10-02 23:24:00 +02:00
return success;
2017-10-02 23:24:00 +02:00
}
#endif
bool GDMono::_load_project_assembly() {
if (project_assembly)
return true;
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-02 23:24:00 +02:00
bool success = load_assembly(appname_safe, &project_assembly);
2017-10-02 23:24:00 +02:00
if (success) {
2017-10-02 23:24:00 +02:00
mono_assembly_set_main(project_assembly->get_assembly());
} else {
if (OS::get_singleton()->is_stdout_verbose())
print_error("Mono: Failed to load project assembly");
}
2017-10-02 23:24:00 +02:00
return success;
}
bool GDMono::_load_api_assemblies() {
2017-10-02 23:24:00 +02:00
if (!_load_core_api_assembly()) {
if (OS::get_singleton()->is_stdout_verbose())
print_error("Mono: Failed to load Core API assembly");
2017-10-02 23:24:00 +02:00
return false;
}
if (core_api_assembly_out_of_sync || !GDMonoUtils::mono_cache.godot_api_cache_updated)
return false;
2017-10-02 23:24:00 +02:00
#ifdef TOOLS_ENABLED
if (!_load_editor_api_assembly()) {
if (OS::get_singleton()->is_stdout_verbose())
print_error("Mono: Failed to load Editor API assembly");
return false;
2017-10-02 23:24:00 +02:00
}
if (editor_api_assembly_out_of_sync)
return false;
#endif
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() {
return GodotSharpDirs::get_res_metadata_dir().plus_file("api_assemblies.cfg");
}
void GDMono::metadata_set_api_assembly_invalidated(APIAssembly::Type p_api_type, bool p_invalidated) {
String section = APIAssembly::to_string(p_api_type);
String path = _get_api_assembly_metadata_path();
Ref<ConfigFile> metadata;
metadata.instance();
metadata->load(path);
metadata->set_value(section, "invalidated", p_invalidated);
String assembly_path = GodotSharpDirs::get_res_assemblies_dir()
.plus_file(p_api_type == APIAssembly::API_CORE ?
CORE_API_ASSEMBLY_NAME ".dll" :
EDITOR_API_ASSEMBLY_NAME ".dll");
ERR_FAIL_COND(!FileAccess::exists(assembly_path));
uint64_t modified_time = FileAccess::get_modified_time(assembly_path);
metadata->set_value(section, "invalidated_asm_modified_time", String::num_uint64(modified_time));
String dir = path.get_base_dir();
if (!DirAccess::exists(dir)) {
DirAccessRef da = DirAccess::create(DirAccess::ACCESS_FILESYSTEM);
ERR_FAIL_COND(!da);
Error err = da->make_dir_recursive(ProjectSettings::get_singleton()->globalize_path(dir));
ERR_FAIL_COND(err != OK);
2017-10-02 23:24:00 +02:00
}
Error save_err = metadata->save(path);
ERR_FAIL_COND(save_err != OK);
}
2017-10-02 23:24:00 +02:00
bool GDMono::metadata_is_api_assembly_invalidated(APIAssembly::Type p_api_type) {
String section = APIAssembly::to_string(p_api_type);
Ref<ConfigFile> metadata;
metadata.instance();
metadata->load(_get_api_assembly_metadata_path());
String assembly_path = GodotSharpDirs::get_res_assemblies_dir()
.plus_file(p_api_type == APIAssembly::API_CORE ?
CORE_API_ASSEMBLY_NAME ".dll" :
EDITOR_API_ASSEMBLY_NAME ".dll");
if (!FileAccess::exists(assembly_path))
return false;
uint64_t modified_time = FileAccess::get_modified_time(assembly_path);
uint64_t stored_modified_time = metadata->get_value(section, "invalidated_asm_modified_time", 0);
return metadata->get_value(section, "invalidated", false) && modified_time <= stored_modified_time;
2017-10-02 23:24:00 +02:00
}
String GDMono::get_invalidated_api_assembly_path(APIAssembly::Type p_api_type) {
return GodotSharpDirs::get_res_assemblies_dir()
.plus_file(p_api_type == APIAssembly::API_CORE ?
CORE_API_ASSEMBLY_NAME ".dll" :
EDITOR_API_ASSEMBLY_NAME ".dll");
}
#endif
2017-10-02 23:24:00 +02:00
Error GDMono::_load_scripts_domain() {
ERR_FAIL_COND_V(scripts_domain != NULL, ERR_BUG);
print_verbose("Mono: Loading scripts domain...");
2017-10-02 23:24:00 +02:00
scripts_domain = GDMonoUtils::create_domain("GodotEngine.ScriptsDomain");
ERR_EXPLAIN("Mono: Could not create scripts app domain");
ERR_FAIL_NULL_V(scripts_domain, ERR_CANT_CREATE);
mono_domain_set(scripts_domain, true);
return OK;
}
Error GDMono::_unload_scripts_domain() {
ERR_FAIL_NULL_V(scripts_domain, ERR_BUG);
print_verbose("Mono: Unloading scripts domain...");
2017-10-02 23:24:00 +02:00
if (mono_domain_get() != root_domain)
mono_domain_set(root_domain, true);
finalizing_scripts_domain = true;
if (!mono_domain_finalize(scripts_domain, 2000)) {
ERR_PRINT("Mono: Domain finalization timeout");
}
finalizing_scripts_domain = false;
2017-10-02 23:24:00 +02:00
mono_gc_collect(mono_gc_max_generation());
GDMonoUtils::clear_godot_api_cache();
2017-10-02 23:24:00 +02:00
_domain_assemblies_cleanup(mono_domain_get_id(scripts_domain));
core_api_assembly = NULL;
2017-10-02 23:24:00 +02:00
project_assembly = NULL;
#ifdef TOOLS_ENABLED
editor_api_assembly = NULL;
tools_assembly = NULL;
tools_project_editor_assembly = NULL;
2017-10-02 23:24:00 +02:00
#endif
core_api_assembly_out_of_sync = false;
#ifdef TOOLS_ENABLED
editor_api_assembly_out_of_sync = false;
#endif
2017-10-02 23:24:00 +02:00
MonoDomain *domain = scripts_domain;
scripts_domain = NULL;
2018-06-26 21:03:42 +02:00
MonoException *exc = NULL;
mono_domain_try_unload(domain, (MonoObject **)&exc);
2017-10-02 23:24:00 +02:00
2018-06-26 21:03:42 +02:00
if (exc) {
ERR_PRINT("Exception thrown when unloading scripts domain");
GDMonoUtils::debug_unhandled_exception(exc);
2017-10-02 23:24:00 +02:00
return FAILED;
}
return OK;
}
#ifdef GD_MONO_HOT_RELOAD
2017-10-02 23:24:00 +02:00
Error GDMono::reload_scripts_domain() {
ERR_FAIL_COND_V(!runtime_initialized, ERR_BUG);
if (scripts_domain) {
Error err = _unload_scripts_domain();
if (err != OK) {
ERR_PRINT("Mono: Failed to unload scripts domain");
return err;
}
}
CSharpLanguage::get_singleton()->_on_scripts_domain_unloaded();
2017-10-02 23:24:00 +02:00
Error err = _load_scripts_domain();
if (err != OK) {
ERR_PRINT("Mono: Failed to load scripts domain");
return err;
}
#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))
#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.
2017-10-02 23:24:00 +02:00
if (core_api_assembly_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");
metadata_set_api_assembly_invalidated(APIAssembly::API_CORE, true);
}
if (editor_api_assembly_out_of_sync) {
ERR_PRINT(OUT_OF_SYNC_ERR_MESSAGE(EDITOR_API_ASSEMBLY_NAME));
metadata_set_api_assembly_invalidated(APIAssembly::API_EDITOR, true);
}
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;
}
}
#ifdef TOOLS_ENABLED
ERR_EXPLAIN("Mono: Failed to load GodotTools assemblies");
ERR_FAIL_COND_V(!_load_tools_assemblies(), ERR_CANT_OPEN);
#endif
if (!_load_project_assembly()) {
return ERR_CANT_OPEN;
}
#else
print_verbose("Mono: Glue disabled, ignoring script assemblies.");
#endif // MONO_GLUE_ENABLED
2017-10-02 23:24:00 +02:00
return OK;
}
#endif
2018-02-22 13:39:41 +01:00
Error GDMono::finalize_and_unload_domain(MonoDomain *p_domain) {
CRASH_COND(p_domain == NULL);
CRASH_COND(p_domain == GDMono::get_singleton()->get_scripts_domain()); // Should use _unload_scripts_domain() instead
2018-02-22 13:39:41 +01:00
String domain_name = mono_domain_get_friendly_name(p_domain);
print_verbose("Mono: Unloading domain `" + domain_name + "`...");
2018-02-22 13:39:41 +01:00
Mono: Editor and export template dependencies and fixes - Bundle editor dependencies: - 'GodotSharp': Root data directory for the editor - 'Tools': Editor dependencies. Only GodotSharp.dll for now. - 'Api': Prebuilt GodotSharp and GodotSharpEditor API assemblies. - 'Mono': Mono files to bundle with the editor. - 'bin': (Optional, not used for now) Mono bin directory. - 'etc': Mono configuration files. - 'lib': Mono dependency shared libraries. - 'lib/mono/4.5': Framework assemblies. - Added build option to copy the required files from the mono installation to 'GodotSharp/Mono'. Enable with 'copy_mono_root=yes'. Disabled by default. - Export template dependencies: - 'data_AppName'/'data_Godot': - 'Mono': Mono files to bundle with the game. - 'etc': Mono configuration files. - 'lib': Mono dependency shared libraries. - The data directory is generated when compiling and must be bundled with the export templates. In the case of OSX, the data directory must be placed inside the 'osx.zip' export template. - In OSX, alternative location for directories (needed for app bundles) are: - 'data_AppName/Mono/etc' --> '../Resources/GodotSharp/Mono/etc' - 'data_AppName/Mono/lib' --> '../Frameworks/GodotSharp/Mono/lib' - The editor can bundle prebuilt API assemblies. - Generate them with a tools build by running: `--generate-cs-core-api <GodotSharp_OutputDir> --generate-cs-editor-api <GodotSharpEditor_OutputDir> <GodotSharp_OutputDir>/bin/Release/GodotSharp.dll` (This command will be simplified in the future and both projects will be in the same solution) - Build the solutions and copy the output files to '#bin/GodotSharp/Api'. - Fixed API assembly being added twice during the export process.
2018-10-03 19:01:57 +02:00
if (mono_domain_get() == p_domain)
2018-02-22 13:39:41 +01:00
mono_domain_set(root_domain, true);
if (!mono_domain_finalize(p_domain, 2000)) {
ERR_PRINT("Mono: Domain finalization timeout");
}
2018-02-22 13:39:41 +01:00
mono_gc_collect(mono_gc_max_generation());
_domain_assemblies_cleanup(mono_domain_get_id(p_domain));
2018-06-26 21:03:42 +02:00
MonoException *exc = NULL;
mono_domain_try_unload(p_domain, (MonoObject **)&exc);
2018-02-22 13:39:41 +01:00
2018-06-26 21:03:42 +02:00
if (exc) {
ERR_PRINTS("Exception thrown when unloading domain `" + domain_name + "`");
GDMonoUtils::debug_print_unhandled_exception(exc);
2018-02-22 13:39:41 +01:00
return FAILED;
}
return OK;
}
2017-10-02 23:24:00 +02: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];
const String *k = NULL;
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;
}
}
return NULL;
}
GDMonoClass *GDMono::get_class(const StringName &p_namespace, const StringName &p_name) {
uint32_t domain_id = mono_domain_get_id(mono_domain_get());
HashMap<String, GDMonoAssembly *> &domain_assemblies = assemblies[domain_id];
const String *k = NULL;
while ((k = domain_assemblies.next(k))) {
GDMonoAssembly *assembly = domain_assemblies.get(*k);
GDMonoClass *klass = assembly->get_class(p_namespace, p_name);
if (klass)
return klass;
}
return NULL;
}
2017-10-02 23:24:00 +02:00
void GDMono::_domain_assemblies_cleanup(uint32_t p_domain_id) {
HashMap<String, GDMonoAssembly *> &domain_assemblies = assemblies[p_domain_id];
const String *k = NULL;
while ((k = domain_assemblies.next(k))) {
memdelete(domain_assemblies.get(*k));
}
assemblies.erase(p_domain_id);
}
2018-06-26 21:03:42 +02:00
void GDMono::unhandled_exception_hook(MonoObject *p_exc, void *) {
// 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 21:03:42 +02:00
#ifdef DEBUG_ENABLED
GDMonoUtils::debug_send_unhandled_exception_error((MonoException *)p_exc);
if (ScriptDebugger::get_singleton())
ScriptDebugger::get_singleton()->idle_poll();
#endif
exit(mono_environment_exitcode_get());
2019-01-29 00:02:35 +01:00
GD_UNREACHABLE();
2018-06-26 21:03:42 +02:00
}
2017-10-02 23:24:00 +02:00
GDMono::GDMono() {
singleton = this;
gdmono_log = memnew(GDMonoLog);
runtime_initialized = false;
finalizing_scripts_domain = false;
2017-10-02 23:24:00 +02:00
root_domain = NULL;
scripts_domain = NULL;
core_api_assembly_out_of_sync = false;
#ifdef TOOLS_ENABLED
editor_api_assembly_out_of_sync = false;
#endif
2017-10-02 23:24:00 +02:00
corlib_assembly = NULL;
core_api_assembly = NULL;
2017-10-02 23:24:00 +02:00
project_assembly = NULL;
#ifdef TOOLS_ENABLED
editor_api_assembly = NULL;
tools_assembly = NULL;
tools_project_editor_assembly = NULL;
2017-10-02 23:24:00 +02:00
#endif
api_core_hash = 0;
#ifdef TOOLS_ENABLED
api_editor_hash = 0;
#endif
}
GDMono::~GDMono() {
if (is_runtime_initialized()) {
if (scripts_domain) {
2017-10-02 23:24:00 +02:00
Error err = _unload_scripts_domain();
if (err != OK) {
ERR_PRINT("Mono: Failed to unload scripts domain");
2017-10-02 23:24:00 +02:00
}
}
const uint32_t *k = NULL;
while ((k = assemblies.next(k))) {
HashMap<String, GDMonoAssembly *> &domain_assemblies = assemblies.get(*k);
const String *kk = NULL;
while ((kk = domain_assemblies.next(kk))) {
memdelete(domain_assemblies.get(*kk));
}
}
assemblies.clear();
print_verbose("Mono: Runtime cleanup...");
2017-10-02 23:24:00 +02:00
mono_jit_cleanup(root_domain);
print_verbose("Mono: Finalized");
runtime_initialized = false;
2017-10-02 23:24:00 +02:00
}
if (gdmono_log)
memdelete(gdmono_log);
singleton = NULL;
2017-10-02 23:24:00 +02:00
}
_GodotSharp *_GodotSharp::singleton = NULL;
void _GodotSharp::attach_thread() {
GDMonoUtils::attach_current_thread();
}
void _GodotSharp::detach_thread() {
GDMonoUtils::detach_current_thread();
}
int32_t _GodotSharp::get_domain_id() {
2017-10-02 23:24:00 +02:00
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 23:24:00 +02:00
}
int32_t _GodotSharp::get_scripts_domain_id() {
2017-10-02 23:24:00 +02:00
MonoDomain *domain = GDMono::get_singleton()->get_scripts_domain();
CRASH_COND(!domain); // User must check if scripts domain is loaded before calling this method
return mono_domain_get_id(domain);
2017-10-02 23:24:00 +02:00
}
bool _GodotSharp::is_scripts_domain_loaded() {
2017-10-02 23:24:00 +02:00
return GDMono::get_singleton()->is_runtime_initialized() && GDMono::get_singleton()->get_scripts_domain() != NULL;
}
2017-10-02 23:24:00 +02:00
bool _GodotSharp::_is_domain_finalizing_for_unload(int32_t p_domain_id) {
2017-10-02 23:24:00 +02:00
return is_domain_finalizing_for_unload(p_domain_id);
}
bool _GodotSharp::is_domain_finalizing_for_unload() {
2017-10-02 23:24:00 +02:00
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;
if (p_domain == GDMono::get_singleton()->get_scripts_domain() && GDMono::get_singleton()->is_finalizing_scripts_domain())
return true;
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 23:24:00 +02:00
}
void _GodotSharp::_reload_assemblies(bool p_soft_reload) {
#ifdef GD_MONO_HOT_RELOAD
CSharpLanguage::get_singleton()->reload_assemblies(p_soft_reload);
#endif
}
2017-10-02 23:24:00 +02: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);
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);
ClassDB::bind_method(D_METHOD("_reload_assemblies"), &_GodotSharp::_reload_assemblies);
2017-10-02 23:24:00 +02:00
}
_GodotSharp::_GodotSharp() {
singleton = this;
}
_GodotSharp::~_GodotSharp() {
singleton = NULL;
}