2017-10-02 21:24:00 +00:00
|
|
|
/*************************************************************************/
|
|
|
|
/* gd_mono.cpp */
|
|
|
|
/*************************************************************************/
|
|
|
|
/* This file is part of: */
|
|
|
|
/* GODOT ENGINE */
|
|
|
|
/* https://godotengine.org */
|
|
|
|
/*************************************************************************/
|
2022-01-03 20:27:34 +00:00
|
|
|
/* Copyright (c) 2007-2022 Juan Linietsky, Ariel Manzur. */
|
|
|
|
/* Copyright (c) 2014-2022 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"
|
|
|
|
|
2020-11-07 22:33:38 +00:00
|
|
|
#include "core/config/project_settings.h"
|
2020-02-27 02:30:20 +00:00
|
|
|
#include "core/debugger/engine_debugger.h"
|
2021-06-11 12:51:48 +00:00
|
|
|
#include "core/io/dir_access.h"
|
|
|
|
#include "core/io/file_access.h"
|
2018-09-11 16:13:45 +00:00
|
|
|
#include "core/os/os.h"
|
|
|
|
#include "core/os/thread.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"
|
2017-10-02 21:24:00 +00:00
|
|
|
|
2021-09-12 18:23:05 +00:00
|
|
|
#include <nethost.h>
|
|
|
|
|
|
|
|
#include <coreclr_delegates.h>
|
|
|
|
#include <hostfxr.h>
|
|
|
|
|
|
|
|
#warning TODO mobile
|
|
|
|
#if 0
|
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"
|
2022-07-20 06:28:22 +00:00
|
|
|
#elif defined(IOS_ENABLED)
|
2020-03-18 16:40:04 +00:00
|
|
|
#include "support/ios_support.h"
|
|
|
|
#endif
|
2019-07-03 15:41:07 +00:00
|
|
|
#endif
|
|
|
|
|
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 {
|
|
|
|
|
2021-09-12 18:23:05 +00:00
|
|
|
#warning "TODO .NET debugging and profiling. What's needed?"
|
|
|
|
#if 0
|
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)) {
|
2021-04-25 14:40:58 +00:00
|
|
|
const String mono_env_ops = OS::get_singleton()->get_environment(env_var_name);
|
2019-12-16 19:34:30 +00:00
|
|
|
// 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)) {
|
2021-04-25 14:40:58 +00:00
|
|
|
const String ops = mono_env_ops.substr(prefix.length(), mono_env_ops.length());
|
2019-12-16 19:34:30 +00:00
|
|
|
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() ||
|
2020-12-15 12:04:21 +00:00
|
|
|
ProjectSettings::get_singleton()->get_resource_path().is_empty() ||
|
2022-02-03 00:21:52 +00:00
|
|
|
Engine::get_singleton()->is_project_manager_hint()) {
|
2020-07-05 17:19:36 +00:00
|
|
|
if (da_args.size() == 0) {
|
2019-07-03 07:44:53 +00:00
|
|
|
return;
|
2020-07-05 17:19:36 +00:00
|
|
|
}
|
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) +
|
2021-10-28 13:19:35 +00:00
|
|
|
",embedding=1,server=y,suspend=" + (da_suspend ? "y,timeout=" + itos(da_timeout) : "n"))
|
2019-01-21 21:44:05 +00:00
|
|
|
.utf8();
|
|
|
|
}
|
2019-09-25 20:02:24 +00:00
|
|
|
#else
|
2022-02-16 12:56:32 +00:00
|
|
|
if (da_args.length() == 0) {
|
2019-09-25 20:02:24 +00:00
|
|
|
return; // Exported games don't use the project settings to setup the debugger agent
|
2022-02-16 12:56:32 +00:00
|
|
|
}
|
2019-09-25 20:02:24 +00:00
|
|
|
#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);
|
|
|
|
}
|
2019-11-10 16:10:38 +00:00
|
|
|
#endif
|
2021-09-12 18:23:05 +00:00
|
|
|
} // namespace
|
2019-11-10 16:10:38 +00:00
|
|
|
|
2021-09-12 18:23:05 +00:00
|
|
|
namespace {
|
|
|
|
hostfxr_initialize_for_runtime_config_fn hostfxr_initialize_for_runtime_config = nullptr;
|
|
|
|
hostfxr_get_runtime_delegate_fn hostfxr_get_runtime_delegate = nullptr;
|
|
|
|
hostfxr_close_fn hostfxr_close = nullptr;
|
|
|
|
|
|
|
|
#ifdef _WIN32
|
|
|
|
static_assert(sizeof(char_t) == sizeof(char16_t));
|
|
|
|
using HostFxrCharString = Char16String;
|
|
|
|
#define HOSTFXR_STR(m_str) L##m_str
|
2019-11-10 16:10:38 +00:00
|
|
|
#else
|
2021-09-12 18:23:05 +00:00
|
|
|
static_assert(sizeof(char_t) == sizeof(char));
|
|
|
|
using HostFxrCharString = CharString;
|
|
|
|
#define HOSTFXR_STR(m_str) m_str
|
|
|
|
#endif
|
2019-11-10 16:10:38 +00:00
|
|
|
|
2021-09-12 18:23:05 +00:00
|
|
|
HostFxrCharString str_to_hostfxr(const String &p_str) {
|
|
|
|
#ifdef _WIN32
|
|
|
|
return p_str.utf16();
|
2020-03-18 16:40:04 +00:00
|
|
|
#else
|
2021-09-12 18:23:05 +00:00
|
|
|
return p_str.utf8();
|
2020-03-18 16:40:04 +00:00
|
|
|
#endif
|
2019-11-10 16:10:38 +00:00
|
|
|
}
|
2018-03-18 22:02:06 +00:00
|
|
|
|
2021-09-12 18:23:05 +00:00
|
|
|
String str_from_hostfxr(const char_t *p_buffer) {
|
|
|
|
#ifdef _WIN32
|
|
|
|
return String::utf16((const char16_t *)p_buffer);
|
|
|
|
#else
|
|
|
|
return String::utf8((const char *)p_buffer);
|
|
|
|
#endif
|
|
|
|
}
|
2019-11-10 16:10:38 +00:00
|
|
|
|
2021-09-12 18:23:05 +00:00
|
|
|
const char_t *get_data(const HostFxrCharString &p_char_str) {
|
|
|
|
return (const char_t *)p_char_str.get_data();
|
|
|
|
}
|
2019-01-29 22:00:30 +00:00
|
|
|
|
2021-09-12 18:23:05 +00:00
|
|
|
String find_hostfxr() {
|
|
|
|
const int HostApiBufferTooSmall = 0x80008098;
|
2019-01-29 22:00:30 +00:00
|
|
|
|
2021-09-12 18:23:05 +00:00
|
|
|
size_t buffer_size = 0;
|
|
|
|
int rc = get_hostfxr_path(nullptr, &buffer_size, nullptr);
|
2019-01-29 22:00:30 +00:00
|
|
|
|
2021-09-12 18:23:05 +00:00
|
|
|
if (rc == HostApiBufferTooSmall) {
|
|
|
|
// Pre-allocate a large buffer for the path to hostfxr
|
|
|
|
Vector<char_t> buffer;
|
|
|
|
buffer.resize(buffer_size);
|
2019-01-29 22:00:30 +00:00
|
|
|
|
2021-09-12 18:23:05 +00:00
|
|
|
rc = get_hostfxr_path(buffer.ptrw(), &buffer_size, nullptr);
|
2019-02-15 17:49:04 +00:00
|
|
|
|
2021-09-12 18:23:05 +00:00
|
|
|
if (rc != 0) {
|
|
|
|
return String();
|
|
|
|
}
|
2019-01-29 22:00:30 +00:00
|
|
|
|
2021-09-12 18:23:05 +00:00
|
|
|
return str_from_hostfxr(buffer.ptr());
|
2019-01-29 22:00:30 +00:00
|
|
|
}
|
|
|
|
|
2021-09-12 18:23:05 +00:00
|
|
|
return String();
|
2019-01-29 22:00:30 +00:00
|
|
|
}
|
|
|
|
|
2021-09-12 18:23:05 +00:00
|
|
|
// Forward declarations
|
|
|
|
bool load_hostfxr() {
|
|
|
|
String hostfxr_path = find_hostfxr();
|
2017-10-02 21:24:00 +00:00
|
|
|
|
2021-09-12 18:23:05 +00:00
|
|
|
if (hostfxr_path.is_empty()) {
|
|
|
|
return false;
|
2017-10-02 21:24:00 +00:00
|
|
|
}
|
|
|
|
|
2021-09-12 18:23:05 +00:00
|
|
|
print_verbose("Found hostfxr: " + hostfxr_path);
|
2018-10-03 17:01:57 +00:00
|
|
|
|
2021-09-12 18:23:05 +00:00
|
|
|
void *lib = nullptr;
|
|
|
|
Error err = OS::get_singleton()->open_dynamic_library(hostfxr_path, lib);
|
|
|
|
// TODO: Clean up lib handle when shutting down
|
2019-05-20 16:34:35 +00:00
|
|
|
|
2021-09-12 18:23:05 +00:00
|
|
|
if (err != OK) {
|
|
|
|
return false;
|
2018-10-03 17:01:57 +00:00
|
|
|
}
|
2019-02-15 18:04:45 +00:00
|
|
|
|
2021-09-12 18:23:05 +00:00
|
|
|
void *symbol = nullptr;
|
2019-02-15 18:04:45 +00:00
|
|
|
|
2021-09-12 18:23:05 +00:00
|
|
|
err = OS::get_singleton()->get_dynamic_library_symbol_handle(lib, "hostfxr_initialize_for_runtime_config", symbol);
|
|
|
|
ERR_FAIL_COND_V(err != OK, false);
|
|
|
|
hostfxr_initialize_for_runtime_config = (hostfxr_initialize_for_runtime_config_fn)symbol;
|
2019-11-28 22:42:37 +00:00
|
|
|
|
2021-09-12 18:23:05 +00:00
|
|
|
err = OS::get_singleton()->get_dynamic_library_symbol_handle(lib, "hostfxr_get_runtime_delegate", symbol);
|
|
|
|
ERR_FAIL_COND_V(err != OK, false);
|
|
|
|
hostfxr_get_runtime_delegate = (hostfxr_get_runtime_delegate_fn)symbol;
|
2019-11-28 22:42:37 +00:00
|
|
|
|
2021-09-12 18:23:05 +00:00
|
|
|
err = OS::get_singleton()->get_dynamic_library_symbol_handle(lib, "hostfxr_close", symbol);
|
|
|
|
ERR_FAIL_COND_V(err != OK, false);
|
|
|
|
hostfxr_close = (hostfxr_close_fn)symbol;
|
2019-11-28 22:42:37 +00:00
|
|
|
|
2021-09-12 18:23:05 +00:00
|
|
|
return (hostfxr_initialize_for_runtime_config &&
|
|
|
|
hostfxr_get_runtime_delegate &&
|
|
|
|
hostfxr_close);
|
|
|
|
}
|
2019-11-28 22:42:37 +00:00
|
|
|
|
2021-09-12 18:23:05 +00:00
|
|
|
load_assembly_and_get_function_pointer_fn initialize_hostfxr(const char_t *p_config_path) {
|
|
|
|
hostfxr_handle cxt = nullptr;
|
|
|
|
int rc = hostfxr_initialize_for_runtime_config(p_config_path, nullptr, &cxt);
|
|
|
|
if (rc != 0 || cxt == nullptr) {
|
|
|
|
hostfxr_close(cxt);
|
|
|
|
ERR_FAIL_V_MSG(nullptr, "hostfxr_initialize_for_runtime_config failed");
|
|
|
|
}
|
2019-11-28 22:42:37 +00:00
|
|
|
|
2021-09-12 18:23:05 +00:00
|
|
|
void *load_assembly_and_get_function_pointer = nullptr;
|
2017-10-02 21:24:00 +00:00
|
|
|
|
2021-09-12 18:23:05 +00:00
|
|
|
rc = hostfxr_get_runtime_delegate(cxt,
|
|
|
|
hdt_load_assembly_and_get_function_pointer, &load_assembly_and_get_function_pointer);
|
|
|
|
if (rc != 0 || load_assembly_and_get_function_pointer == nullptr) {
|
|
|
|
ERR_FAIL_V_MSG(nullptr, "hostfxr_get_runtime_delegate failed");
|
|
|
|
}
|
2019-11-28 22:42:37 +00:00
|
|
|
|
2021-09-12 18:23:05 +00:00
|
|
|
hostfxr_close(cxt);
|
2018-10-03 17:01:57 +00:00
|
|
|
|
2021-09-12 18:23:05 +00:00
|
|
|
return (load_assembly_and_get_function_pointer_fn)load_assembly_and_get_function_pointer;
|
|
|
|
}
|
|
|
|
} // namespace
|
2019-11-10 16:10:38 +00:00
|
|
|
|
2021-09-12 18:23:05 +00:00
|
|
|
static bool _on_core_api_assembly_loaded() {
|
|
|
|
if (!GDMonoCache::godot_api_cache_updated) {
|
|
|
|
return false;
|
|
|
|
}
|
2020-03-18 16:40:04 +00:00
|
|
|
|
2021-09-12 18:23:05 +00:00
|
|
|
GDMonoCache::managed_callbacks.Dispatcher_InitializeDefaultGodotTaskScheduler();
|
|
|
|
|
|
|
|
#ifdef DEBUG_ENABLED
|
|
|
|
// Install the trace listener now before the project assembly is loaded
|
|
|
|
GDMonoCache::managed_callbacks.DebuggingUtils_InstallTraceListener();
|
2019-11-10 16:10:38 +00:00
|
|
|
#endif
|
2019-01-29 22:00:30 +00:00
|
|
|
|
2021-09-12 18:23:05 +00:00
|
|
|
return true;
|
|
|
|
}
|
2017-10-02 21:24:00 +00:00
|
|
|
|
2021-09-12 18:23:05 +00:00
|
|
|
void GDMono::initialize() {
|
|
|
|
ERR_FAIL_NULL(Engine::get_singleton());
|
2017-10-02 21:24:00 +00:00
|
|
|
|
2021-09-12 18:23:05 +00:00
|
|
|
print_verbose(".NET: Initializing module...");
|
2018-06-26 19:03:42 +00:00
|
|
|
|
2021-09-12 18:23:05 +00:00
|
|
|
_init_godot_api_hashes();
|
2019-02-10 17:34:34 +00:00
|
|
|
|
2021-09-12 18:23:05 +00:00
|
|
|
if (!load_hostfxr()) {
|
|
|
|
ERR_FAIL_MSG(".NET: Failed to load hostfxr");
|
2019-08-29 23:33:50 +00:00
|
|
|
}
|
|
|
|
|
2021-09-12 18:23:05 +00:00
|
|
|
auto config_path = str_to_hostfxr(GodotSharpDirs::get_api_assemblies_dir()
|
|
|
|
.plus_file("GodotPlugins.runtimeconfig.json"));
|
2019-12-04 14:07:00 +00:00
|
|
|
|
2021-09-12 18:23:05 +00:00
|
|
|
load_assembly_and_get_function_pointer_fn load_assembly_and_get_function_pointer =
|
|
|
|
initialize_hostfxr(get_data(config_path));
|
|
|
|
ERR_FAIL_NULL(load_assembly_and_get_function_pointer);
|
2017-10-02 21:24:00 +00:00
|
|
|
|
2021-09-12 18:23:05 +00:00
|
|
|
runtime_initialized = true;
|
2017-10-02 21:24:00 +00:00
|
|
|
|
2021-09-12 18:23:05 +00:00
|
|
|
print_verbose(".NET: hostfxr initialized");
|
2018-03-18 22:02:06 +00:00
|
|
|
|
2021-09-12 18:23:05 +00:00
|
|
|
auto godot_plugins_path = str_to_hostfxr(GodotSharpDirs::get_api_assemblies_dir()
|
|
|
|
.plus_file("GodotPlugins.dll"));
|
2017-10-02 21:24:00 +00:00
|
|
|
|
2021-09-12 18:23:05 +00:00
|
|
|
using godot_plugins_initialize_fn = bool (*)(bool, PluginCallbacks *, GDMonoCache::ManagedCallbacks *);
|
|
|
|
godot_plugins_initialize_fn godot_plugins_initialize = nullptr;
|
2017-10-02 21:24:00 +00:00
|
|
|
|
2021-09-12 18:23:05 +00:00
|
|
|
int rc = load_assembly_and_get_function_pointer(get_data(godot_plugins_path),
|
|
|
|
HOSTFXR_STR("GodotPlugins.Main, GodotPlugins"),
|
|
|
|
HOSTFXR_STR("Initialize"),
|
|
|
|
UNMANAGEDCALLERSONLY_METHOD,
|
|
|
|
nullptr,
|
|
|
|
(void **)&godot_plugins_initialize);
|
|
|
|
ERR_FAIL_COND_MSG(rc != 0, ".NET: Failed to get Godot.Plugins Initialize function pointer");
|
2019-12-04 14:07:00 +00:00
|
|
|
|
2021-09-12 18:23:05 +00:00
|
|
|
PluginCallbacks aux_plugin_callbacks;
|
|
|
|
GDMonoCache::ManagedCallbacks managed_callbacks;
|
|
|
|
bool init_ok = godot_plugins_initialize(Engine::get_singleton()->is_editor_hint(),
|
|
|
|
&aux_plugin_callbacks, &managed_callbacks);
|
|
|
|
ERR_FAIL_COND_MSG(!init_ok, ".NET: Call to Godot.Plugins Initialize failed");
|
2017-10-02 21:24:00 +00:00
|
|
|
|
2021-09-12 18:23:05 +00:00
|
|
|
GDMonoCache::update_godot_api_cache(managed_callbacks);
|
|
|
|
plugin_callbacks = aux_plugin_callbacks;
|
2017-10-02 21:24:00 +00:00
|
|
|
|
2021-09-12 18:23:05 +00:00
|
|
|
print_verbose(".NET: GodotPlugins initialized");
|
2017-10-02 21:24:00 +00:00
|
|
|
|
2021-09-12 18:23:05 +00:00
|
|
|
_on_core_api_assembly_loaded();
|
2019-07-11 12:01:25 +00:00
|
|
|
}
|
2019-07-03 07:44:53 +00:00
|
|
|
|
2019-07-11 12:01:25 +00:00
|
|
|
void GDMono::initialize_load_assemblies() {
|
|
|
|
#if defined(TOOLS_ENABLED)
|
2022-02-03 00:21:52 +00:00
|
|
|
if (Engine::get_singleton()->is_project_manager_hint()) {
|
2020-05-22 00:17:06 +00:00
|
|
|
return;
|
2020-07-05 17:19:36 +00:00
|
|
|
}
|
2020-05-30 05:21:24 +00:00
|
|
|
#endif
|
2020-05-22 00:17:06 +00:00
|
|
|
|
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()) {
|
2020-07-05 17:19:36 +00:00
|
|
|
if (OS::get_singleton()->is_stdout_verbose()) {
|
2021-09-12 18:23:05 +00:00
|
|
|
print_error(".NET: Failed to load project assembly");
|
2020-07-05 17:19:36 +00:00
|
|
|
}
|
2019-07-11 12:01:25 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-11-28 22:42:37 +00:00
|
|
|
void GDMono::_init_godot_api_hashes() {
|
C#: Move marshaling logic and generated glue to C#
We will be progressively moving most code to C#.
The plan is to only use Mono's embedding APIs to set things at launch.
This will make it much easier to later support CoreCLR too which
doesn't have rich embedding APIs.
Additionally the code in C# is more maintainable and makes it easier
to implement new features, e.g.: runtime codegen which we could use to
avoid using reflection for marshaling everytime a field, property or
method is accessed.
SOME NOTES ON INTEROP
We make the same assumptions as GDNative about the size of the Godot
structures we use. We take it a bit further by also assuming the layout
of fields in some cases, which is riskier but let's us squeeze out some
performance by avoiding unnecessary managed to native calls.
Code that deals with native structs is less safe than before as there's
no RAII and copy constructors in C#. It's like using the GDNative C API
directly. One has to take special care to free values they own.
Perhaps we could use roslyn analyzers to check this, but I don't know
any that uses attributes to determine what's owned or borrowed.
As to why we maily use pointers for native structs instead of ref/out:
- AFAIK (and confirmed with a benchmark) ref/out are pinned
during P/Invoke calls and that has a cost.
- Native struct fields can't be ref/out in the first place.
- A `using` local can't be passed as ref/out, only `in`. Calling a
method or property on an `in` value makes a silent copy, so we want
to avoid `in`.
REGARDING THE BUILD SYSTEM
There's no longer a `mono_glue=yes/no` SCons options. We no longer
need to build with `mono_glue=no`, generate the glue and then build
again with `mono_glue=yes`. We build only once and generate the glue
(which is in C# now).
However, SCons no longer builds the C# projects for us. Instead one
must run `build_assemblies.py`, e.g.:
```sh
%godot_src_root%/modules/mono/build_scripts/build_assemblies.py \
--godot-output-dir=%godot_src_root%/bin \
--godot-target=release_debug`
```
We could turn this into a custom build target, but I don't know how
to do that with SCons (it's possible with Meson).
OTHER NOTES
Most of the moved code doesn't follow the C# naming convention and
still has the word Mono in the names despite no longer dealing with
Mono's embedding APIs. This is just temporary while transitioning,
to make it easier to understand what was moved where.
2021-05-03 13:21:06 +00:00
|
|
|
#ifdef DEBUG_METHODS_ENABLED
|
|
|
|
get_api_core_hash();
|
2017-10-02 21:24:00 +00:00
|
|
|
|
|
|
|
#ifdef TOOLS_ENABLED
|
C#: Move marshaling logic and generated glue to C#
We will be progressively moving most code to C#.
The plan is to only use Mono's embedding APIs to set things at launch.
This will make it much easier to later support CoreCLR too which
doesn't have rich embedding APIs.
Additionally the code in C# is more maintainable and makes it easier
to implement new features, e.g.: runtime codegen which we could use to
avoid using reflection for marshaling everytime a field, property or
method is accessed.
SOME NOTES ON INTEROP
We make the same assumptions as GDNative about the size of the Godot
structures we use. We take it a bit further by also assuming the layout
of fields in some cases, which is riskier but let's us squeeze out some
performance by avoiding unnecessary managed to native calls.
Code that deals with native structs is less safe than before as there's
no RAII and copy constructors in C#. It's like using the GDNative C API
directly. One has to take special care to free values they own.
Perhaps we could use roslyn analyzers to check this, but I don't know
any that uses attributes to determine what's owned or borrowed.
As to why we maily use pointers for native structs instead of ref/out:
- AFAIK (and confirmed with a benchmark) ref/out are pinned
during P/Invoke calls and that has a cost.
- Native struct fields can't be ref/out in the first place.
- A `using` local can't be passed as ref/out, only `in`. Calling a
method or property on an `in` value makes a silent copy, so we want
to avoid `in`.
REGARDING THE BUILD SYSTEM
There's no longer a `mono_glue=yes/no` SCons options. We no longer
need to build with `mono_glue=no`, generate the glue and then build
again with `mono_glue=yes`. We build only once and generate the glue
(which is in C# now).
However, SCons no longer builds the C# projects for us. Instead one
must run `build_assemblies.py`, e.g.:
```sh
%godot_src_root%/modules/mono/build_scripts/build_assemblies.py \
--godot-output-dir=%godot_src_root%/bin \
--godot-target=release_debug`
```
We could turn this into a custom build target, but I don't know how
to do that with SCons (it's possible with Meson).
OTHER NOTES
Most of the moved code doesn't follow the C# naming convention and
still has the word Mono in the names despite no longer dealing with
Mono's embedding APIs. This is just temporary while transitioning,
to make it easier to understand what was moved where.
2021-05-03 13:21:06 +00:00
|
|
|
get_api_editor_hash();
|
2017-10-02 21:24:00 +00:00
|
|
|
#endif // TOOLS_ENABLED
|
C#: Move marshaling logic and generated glue to C#
We will be progressively moving most code to C#.
The plan is to only use Mono's embedding APIs to set things at launch.
This will make it much easier to later support CoreCLR too which
doesn't have rich embedding APIs.
Additionally the code in C# is more maintainable and makes it easier
to implement new features, e.g.: runtime codegen which we could use to
avoid using reflection for marshaling everytime a field, property or
method is accessed.
SOME NOTES ON INTEROP
We make the same assumptions as GDNative about the size of the Godot
structures we use. We take it a bit further by also assuming the layout
of fields in some cases, which is riskier but let's us squeeze out some
performance by avoiding unnecessary managed to native calls.
Code that deals with native structs is less safe than before as there's
no RAII and copy constructors in C#. It's like using the GDNative C API
directly. One has to take special care to free values they own.
Perhaps we could use roslyn analyzers to check this, but I don't know
any that uses attributes to determine what's owned or borrowed.
As to why we maily use pointers for native structs instead of ref/out:
- AFAIK (and confirmed with a benchmark) ref/out are pinned
during P/Invoke calls and that has a cost.
- Native struct fields can't be ref/out in the first place.
- A `using` local can't be passed as ref/out, only `in`. Calling a
method or property on an `in` value makes a silent copy, so we want
to avoid `in`.
REGARDING THE BUILD SYSTEM
There's no longer a `mono_glue=yes/no` SCons options. We no longer
need to build with `mono_glue=no`, generate the glue and then build
again with `mono_glue=yes`. We build only once and generate the glue
(which is in C# now).
However, SCons no longer builds the C# projects for us. Instead one
must run `build_assemblies.py`, e.g.:
```sh
%godot_src_root%/modules/mono/build_scripts/build_assemblies.py \
--godot-output-dir=%godot_src_root%/bin \
--godot-target=release_debug`
```
We could turn this into a custom build target, but I don't know how
to do that with SCons (it's possible with Meson).
OTHER NOTES
Most of the moved code doesn't follow the C# naming convention and
still has the word Mono in the names despite no longer dealing with
Mono's embedding APIs. This is just temporary while transitioning,
to make it easier to understand what was moved where.
2021-05-03 13:21:06 +00:00
|
|
|
#endif // DEBUG_METHODS_ENABLED
|
2019-11-28 22:42:37 +00:00
|
|
|
}
|
|
|
|
|
2017-10-02 21:24:00 +00:00
|
|
|
bool GDMono::_load_project_assembly() {
|
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);
|
2020-12-15 12:04:21 +00:00
|
|
|
if (appname_safe.is_empty()) {
|
2019-07-03 15:41:07 +00:00
|
|
|
appname_safe = "UnnamedProject";
|
2017-10-27 19:22:54 +00:00
|
|
|
}
|
2017-10-02 21:24:00 +00:00
|
|
|
|
2021-09-12 18:23:05 +00:00
|
|
|
String assembly_path = GodotSharpDirs::get_res_temp_assemblies_dir()
|
|
|
|
.plus_file(appname_safe + ".dll");
|
|
|
|
assembly_path = ProjectSettings::get_singleton()->globalize_path(assembly_path);
|
2017-10-02 21:24:00 +00:00
|
|
|
|
2021-09-12 18:23:05 +00:00
|
|
|
return plugin_callbacks.LoadProjectAssemblyCallback(assembly_path.utf16());
|
2017-10-02 21:24:00 +00:00
|
|
|
}
|
|
|
|
|
2021-09-12 18:23:05 +00:00
|
|
|
#warning TODO hot-reload
|
|
|
|
#if 0
|
2017-10-02 21:24:00 +00:00
|
|
|
Error GDMono::_unload_scripts_domain() {
|
|
|
|
ERR_FAIL_NULL_V(scripts_domain, ERR_BUG);
|
|
|
|
|
2021-09-12 17:50:13 +00:00
|
|
|
CSharpLanguage::get_singleton()->_on_scripts_domain_about_to_unload();
|
|
|
|
|
2020-03-18 16:40:04 +00:00
|
|
|
print_verbose("Mono: Finalizing scripts domain...");
|
2017-10-02 21:24:00 +00:00
|
|
|
|
2020-07-05 17:19:36 +00:00
|
|
|
if (mono_domain_get() != root_domain) {
|
2017-10-02 21:24:00 +00:00
|
|
|
mono_domain_set(root_domain, true);
|
2020-07-05 17:19:36 +00:00
|
|
|
}
|
2017-10-02 21:24:00 +00:00
|
|
|
|
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());
|
|
|
|
|
2021-09-12 18:21:15 +00:00
|
|
|
core_api_assembly = nullptr;
|
2019-10-12 19:27:56 +00:00
|
|
|
#ifdef TOOLS_ENABLED
|
2021-09-12 18:21:15 +00:00
|
|
|
editor_api_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;
|
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;
|
|
|
|
}
|
|
|
|
|
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-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
|
|
|
|
2021-09-12 18:21:15 +00:00
|
|
|
if (!_try_load_api_assemblies()) {
|
|
|
|
CRASH_NOW_MSG("Failed to load one of the 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
|
|
|
#endif
|
2018-02-22 12:39:41 +00:00
|
|
|
|
2021-09-12 18:23:05 +00:00
|
|
|
#warning TODO Reimplement in C#
|
|
|
|
#if 0
|
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-07-05 17:19:36 +00:00
|
|
|
if (EngineDebugger::is_active()) {
|
2020-02-27 02:30:20 +00:00
|
|
|
EngineDebugger::get_singleton()->poll_events(false);
|
2020-07-05 17:19:36 +00:00
|
|
|
}
|
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
|
|
|
}
|
2021-09-12 18:23:05 +00:00
|
|
|
#endif
|
2018-06-26 19:03:42 +00:00
|
|
|
|
2017-10-02 21:24:00 +00:00
|
|
|
GDMono::GDMono() {
|
|
|
|
singleton = this;
|
|
|
|
|
|
|
|
runtime_initialized = false;
|
2018-10-06 21:00:18 +00:00
|
|
|
finalizing_scripts_domain = false;
|
2017-10-02 21:24:00 +00:00
|
|
|
|
|
|
|
api_core_hash = 0;
|
|
|
|
#ifdef TOOLS_ENABLED
|
|
|
|
api_editor_hash = 0;
|
|
|
|
#endif
|
|
|
|
}
|
|
|
|
|
|
|
|
GDMono::~GDMono() {
|
2021-12-28 22:25:16 +00:00
|
|
|
finalizing_scripts_domain = true;
|
|
|
|
|
2018-09-12 00:41:54 +00:00
|
|
|
if (is_runtime_initialized()) {
|
2021-12-28 22:25:16 +00:00
|
|
|
if (GDMonoCache::godot_api_cache_updated) {
|
|
|
|
GDMonoCache::managed_callbacks.DisposablesTracker_OnGodotShuttingDown();
|
2017-10-02 21:24:00 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-12-28 22:25:16 +00:00
|
|
|
finalizing_scripts_domain = false;
|
|
|
|
runtime_initialized = false;
|
|
|
|
|
2020-03-18 16:40:04 +00:00
|
|
|
#if defined(ANDROID_ENABLED)
|
|
|
|
gdmono::android::support::cleanup();
|
|
|
|
#endif
|
|
|
|
|
2020-04-01 23:20:12 +00:00
|
|
|
singleton = nullptr;
|
2017-10-02 21:24:00 +00:00
|
|
|
}
|
|
|
|
|
2021-08-13 14:46:14 +00:00
|
|
|
namespace mono_bind {
|
2017-10-02 21:24:00 +00:00
|
|
|
|
2021-08-13 14:46:14 +00:00
|
|
|
GodotSharp *GodotSharp::singleton = nullptr;
|
|
|
|
|
2021-09-12 18:23:05 +00:00
|
|
|
bool GodotSharp::_is_runtime_initialized() {
|
2020-07-05 17:19:36 +00:00
|
|
|
return GDMono::get_singleton() != nullptr && GDMono::get_singleton()->is_runtime_initialized();
|
2017-10-02 21:24:00 +00:00
|
|
|
}
|
|
|
|
|
2021-08-13 14:46:14 +00:00
|
|
|
void GodotSharp::_reload_assemblies(bool p_soft_reload) {
|
2019-07-03 07:44:53 +00:00
|
|
|
#ifdef GD_MONO_HOT_RELOAD
|
2020-07-05 17:19:36 +00:00
|
|
|
CRASH_COND(CSharpLanguage::get_singleton() == nullptr);
|
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.
|
2020-07-05 17:19:36 +00:00
|
|
|
if (CSharpLanguage::get_singleton()->is_assembly_reloading_needed()) {
|
2020-04-23 00:23:25 +00:00
|
|
|
CSharpLanguage::get_singleton()->reload_assemblies(p_soft_reload);
|
2020-07-05 17:19:36 +00:00
|
|
|
}
|
2019-07-03 07:44:53 +00:00
|
|
|
#endif
|
|
|
|
}
|
|
|
|
|
2021-08-13 14:46:14 +00:00
|
|
|
void GodotSharp::_bind_methods() {
|
2021-09-12 18:23:05 +00:00
|
|
|
ClassDB::bind_method(D_METHOD("is_runtime_initialized"), &GodotSharp::_is_runtime_initialized);
|
2021-08-13 14:46:14 +00:00
|
|
|
ClassDB::bind_method(D_METHOD("_reload_assemblies"), &GodotSharp::_reload_assemblies);
|
2017-10-02 21:24:00 +00:00
|
|
|
}
|
|
|
|
|
2021-08-13 14:46:14 +00:00
|
|
|
GodotSharp::GodotSharp() {
|
2017-10-02 21:24:00 +00:00
|
|
|
singleton = this;
|
|
|
|
}
|
|
|
|
|
2021-08-13 14:46:14 +00:00
|
|
|
GodotSharp::~GodotSharp() {
|
2020-04-01 23:20:12 +00:00
|
|
|
singleton = nullptr;
|
2017-10-02 21:24:00 +00:00
|
|
|
}
|
2021-08-13 14:46:14 +00:00
|
|
|
|
|
|
|
} // namespace mono_bind
|