2017-10-03 05:24:00 +08:00
|
|
|
/*************************************************************************/
|
|
|
|
/* gd_mono.cpp */
|
|
|
|
/*************************************************************************/
|
|
|
|
/* This file is part of: */
|
|
|
|
/* GODOT ENGINE */
|
|
|
|
/* https://godotengine.org */
|
|
|
|
/*************************************************************************/
|
2022-01-04 04:27:34 +08:00
|
|
|
/* Copyright (c) 2007-2022 Juan Linietsky, Ariel Manzur. */
|
|
|
|
/* Copyright (c) 2014-2022 Godot Engine contributors (cf. AUTHORS.md). */
|
2017-10-03 05:24:00 +08: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-05 07:50:27 +08:00
|
|
|
|
2017-10-03 05:24:00 +08:00
|
|
|
#include "gd_mono.h"
|
|
|
|
|
2020-11-08 06:33:38 +08:00
|
|
|
#include "core/config/project_settings.h"
|
2020-02-27 10:30:20 +08:00
|
|
|
#include "core/debugger/engine_debugger.h"
|
2021-06-11 20:51:48 +08:00
|
|
|
#include "core/io/dir_access.h"
|
|
|
|
#include "core/io/file_access.h"
|
2018-09-12 00:13:45 +08:00
|
|
|
#include "core/os/os.h"
|
|
|
|
#include "core/os/thread.h"
|
2017-10-03 05:24:00 +08:00
|
|
|
|
|
|
|
#include "../csharp_script.h"
|
2022-08-05 09:32:59 +08:00
|
|
|
#include "../glue/runtime_interop.h"
|
2018-02-22 20:13:51 +08:00
|
|
|
#include "../godotsharp_dirs.h"
|
2017-10-03 05:24:00 +08:00
|
|
|
#include "../utils/path_utils.h"
|
2019-11-11 00:10:38 +08:00
|
|
|
#include "gd_mono_cache.h"
|
2017-10-03 05:24:00 +08:00
|
|
|
|
2021-12-29 06:25:16 +08:00
|
|
|
#ifdef TOOLS_ENABLED
|
2021-09-13 02:23:05 +08:00
|
|
|
#include <nethost.h>
|
2021-12-29 06:25:16 +08:00
|
|
|
#endif
|
2021-09-13 02:23:05 +08:00
|
|
|
|
|
|
|
#include <coreclr_delegates.h>
|
|
|
|
#include <hostfxr.h>
|
2022-02-28 04:57:52 +08:00
|
|
|
#ifdef UNIX_ENABLED
|
|
|
|
#include <dlfcn.h>
|
|
|
|
#endif
|
2021-09-13 02:23:05 +08:00
|
|
|
|
2022-02-28 04:57:36 +08:00
|
|
|
// TODO mobile
|
2021-09-13 02:23:05 +08:00
|
|
|
#if 0
|
2019-07-03 23:41:07 +08:00
|
|
|
#ifdef ANDROID_ENABLED
|
2020-03-19 00:40:04 +08:00
|
|
|
#include "support/android_support.h"
|
2022-07-20 14:28:22 +08:00
|
|
|
#elif defined(IOS_ENABLED)
|
2020-03-19 00:40:04 +08:00
|
|
|
#include "support/ios_support.h"
|
|
|
|
#endif
|
2019-07-03 23:41:07 +08:00
|
|
|
#endif
|
|
|
|
|
2020-04-02 07:20:12 +08:00
|
|
|
GDMono *GDMono::singleton = nullptr;
|
2017-10-03 05:24:00 +08:00
|
|
|
|
2021-09-13 02:23:05 +08:00
|
|
|
namespace {
|
2021-12-29 06:25:16 +08:00
|
|
|
hostfxr_initialize_for_dotnet_command_line_fn hostfxr_initialize_for_dotnet_command_line = nullptr;
|
2021-09-13 02:23:05 +08:00
|
|
|
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-11 00:10:38 +08:00
|
|
|
#else
|
2021-09-13 02:23:05 +08:00
|
|
|
static_assert(sizeof(char_t) == sizeof(char));
|
|
|
|
using HostFxrCharString = CharString;
|
|
|
|
#define HOSTFXR_STR(m_str) m_str
|
|
|
|
#endif
|
2019-11-11 00:10:38 +08:00
|
|
|
|
2021-09-13 02:23:05 +08:00
|
|
|
HostFxrCharString str_to_hostfxr(const String &p_str) {
|
|
|
|
#ifdef _WIN32
|
|
|
|
return p_str.utf16();
|
2020-03-19 00:40:04 +08:00
|
|
|
#else
|
2021-09-13 02:23:05 +08:00
|
|
|
return p_str.utf8();
|
2020-03-19 00:40:04 +08:00
|
|
|
#endif
|
2019-11-11 00:10:38 +08:00
|
|
|
}
|
2018-03-19 06:02:06 +08:00
|
|
|
|
2021-12-29 06:25:16 +08:00
|
|
|
#ifdef TOOLS_ENABLED
|
2021-09-13 02:23:05 +08: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
|
|
|
|
}
|
2021-12-29 06:25:16 +08:00
|
|
|
#endif
|
2019-11-11 00:10:38 +08:00
|
|
|
|
2021-09-13 02:23:05 +08:00
|
|
|
const char_t *get_data(const HostFxrCharString &p_char_str) {
|
|
|
|
return (const char_t *)p_char_str.get_data();
|
|
|
|
}
|
2019-01-30 06:00:30 +08:00
|
|
|
|
2021-12-29 06:25:16 +08:00
|
|
|
#ifdef TOOLS_ENABLED
|
|
|
|
String find_hostfxr(size_t p_known_buffet_size, get_hostfxr_parameters *p_get_hostfxr_params) {
|
|
|
|
// Pre-allocate a large buffer for the path to hostfxr
|
|
|
|
Vector<char_t> buffer;
|
|
|
|
buffer.resize(p_known_buffet_size);
|
|
|
|
|
|
|
|
int rc = get_hostfxr_path(buffer.ptrw(), &p_known_buffet_size, p_get_hostfxr_params);
|
|
|
|
|
|
|
|
ERR_FAIL_COND_V_MSG(rc != 0, String(), "get_hostfxr_path failed with code: " + itos(rc));
|
|
|
|
|
|
|
|
return str_from_hostfxr(buffer.ptr());
|
|
|
|
}
|
|
|
|
#endif
|
|
|
|
|
2021-09-13 02:23:05 +08:00
|
|
|
String find_hostfxr() {
|
2021-12-29 06:25:16 +08:00
|
|
|
#ifdef TOOLS_ENABLED
|
|
|
|
const int CoreHostLibMissingFailure = 0x80008083;
|
2021-09-13 02:23:05 +08:00
|
|
|
const int HostApiBufferTooSmall = 0x80008098;
|
2019-01-30 06:00:30 +08:00
|
|
|
|
2021-09-13 02:23:05 +08:00
|
|
|
size_t buffer_size = 0;
|
|
|
|
int rc = get_hostfxr_path(nullptr, &buffer_size, nullptr);
|
2019-01-30 06:00:30 +08:00
|
|
|
|
2021-09-13 02:23:05 +08:00
|
|
|
if (rc == HostApiBufferTooSmall) {
|
2021-12-29 06:25:16 +08:00
|
|
|
return find_hostfxr(buffer_size, nullptr);
|
|
|
|
}
|
2019-02-16 01:49:04 +08:00
|
|
|
|
2021-12-29 06:25:16 +08:00
|
|
|
if (rc == CoreHostLibMissingFailure) {
|
|
|
|
// Apparently `get_hostfxr_path` doesn't look for dotnet in `PATH`? (I suppose it needs the
|
|
|
|
// `DOTNET_ROOT` environment variable). If it fails, we try to find the dotnet executable
|
|
|
|
// in `PATH` ourselves and pass its location as `dotnet_root` to `get_hostfxr_path`.
|
|
|
|
String dotnet_exe = path::find_executable("dotnet");
|
|
|
|
|
|
|
|
if (!dotnet_exe.is_empty()) {
|
|
|
|
// The file found in PATH may be a symlink
|
|
|
|
dotnet_exe = path::abspath(path::realpath(dotnet_exe));
|
|
|
|
|
|
|
|
// TODO:
|
|
|
|
// Sometimes, the symlink may not point to the dotnet executable in the dotnet root.
|
|
|
|
// That's the case with snaps. The snap install should have been found with the
|
|
|
|
// previous `get_hostfxr_path`, but it would still be better to do this properly
|
|
|
|
// and use something like `dotnet --list-sdks/runtimes` to find the actual location.
|
|
|
|
// This way we could also check if the proper sdk or runtime is installed. This would
|
|
|
|
// allow us to fail gracefully and show some helpful information in the editor.
|
|
|
|
|
|
|
|
HostFxrCharString dotnet_root = str_to_hostfxr(dotnet_exe.get_base_dir());
|
|
|
|
|
|
|
|
get_hostfxr_parameters get_hostfxr_parameters = {
|
|
|
|
sizeof(get_hostfxr_parameters),
|
|
|
|
nullptr,
|
|
|
|
get_data(dotnet_root)
|
|
|
|
};
|
|
|
|
|
|
|
|
buffer_size = 0;
|
|
|
|
rc = get_hostfxr_path(nullptr, &buffer_size, &get_hostfxr_parameters);
|
|
|
|
if (rc == HostApiBufferTooSmall) {
|
|
|
|
return find_hostfxr(buffer_size, &get_hostfxr_parameters);
|
|
|
|
}
|
2021-09-13 02:23:05 +08:00
|
|
|
}
|
2021-12-29 06:25:16 +08:00
|
|
|
}
|
2019-01-30 06:00:30 +08:00
|
|
|
|
2021-12-29 06:25:16 +08:00
|
|
|
if (rc == CoreHostLibMissingFailure) {
|
|
|
|
ERR_PRINT(String() + ".NET: One of the dependent libraries is missing. " +
|
|
|
|
"Typically when the `hostfxr`, `hostpolicy` or `coreclr` dynamic " +
|
|
|
|
"libraries are not present in the expected locations.");
|
2019-01-30 06:00:30 +08:00
|
|
|
}
|
|
|
|
|
2021-09-13 02:23:05 +08:00
|
|
|
return String();
|
2021-12-29 06:25:16 +08:00
|
|
|
#else
|
|
|
|
|
|
|
|
#if defined(WINDOWS_ENABLED)
|
2022-02-28 04:57:52 +08:00
|
|
|
String probe_path = GodotSharpDirs::get_api_assemblies_dir()
|
|
|
|
.plus_file("hostfxr.dll");
|
2021-12-29 06:25:16 +08:00
|
|
|
#elif defined(MACOS_ENABLED)
|
2022-02-28 04:57:52 +08:00
|
|
|
String probe_path = GodotSharpDirs::get_api_assemblies_dir()
|
|
|
|
.plus_file("libhostfxr.dylib");
|
2021-12-29 06:25:16 +08:00
|
|
|
#elif defined(UNIX_ENABLED)
|
2022-02-28 04:57:52 +08:00
|
|
|
String probe_path = GodotSharpDirs::get_api_assemblies_dir()
|
|
|
|
.plus_file("libhostfxr.so");
|
2021-12-29 06:25:16 +08:00
|
|
|
#else
|
|
|
|
#error "Platform not supported (yet?)"
|
|
|
|
#endif
|
|
|
|
|
2022-02-28 04:57:52 +08:00
|
|
|
if (FileAccess::exists(probe_path)) {
|
|
|
|
return probe_path;
|
|
|
|
}
|
|
|
|
|
|
|
|
return String();
|
|
|
|
|
2021-12-29 06:25:16 +08:00
|
|
|
#endif
|
2019-01-30 06:00:30 +08:00
|
|
|
}
|
|
|
|
|
2021-12-29 06:25:16 +08:00
|
|
|
bool load_hostfxr(void *&r_hostfxr_dll_handle) {
|
2021-09-13 02:23:05 +08:00
|
|
|
String hostfxr_path = find_hostfxr();
|
2017-10-03 05:24:00 +08:00
|
|
|
|
2021-09-13 02:23:05 +08:00
|
|
|
if (hostfxr_path.is_empty()) {
|
|
|
|
return false;
|
2017-10-03 05:24:00 +08:00
|
|
|
}
|
|
|
|
|
2021-09-13 02:23:05 +08:00
|
|
|
print_verbose("Found hostfxr: " + hostfxr_path);
|
2018-10-04 01:01:57 +08:00
|
|
|
|
2021-12-29 06:25:16 +08:00
|
|
|
Error err = OS::get_singleton()->open_dynamic_library(hostfxr_path, r_hostfxr_dll_handle);
|
2019-05-21 00:34:35 +08:00
|
|
|
|
2021-09-13 02:23:05 +08:00
|
|
|
if (err != OK) {
|
|
|
|
return false;
|
2018-10-04 01:01:57 +08:00
|
|
|
}
|
2019-02-16 02:04:45 +08:00
|
|
|
|
2021-12-29 06:25:16 +08:00
|
|
|
void *lib = r_hostfxr_dll_handle;
|
|
|
|
|
2021-09-13 02:23:05 +08:00
|
|
|
void *symbol = nullptr;
|
2019-02-16 02:04:45 +08:00
|
|
|
|
2021-12-29 06:25:16 +08:00
|
|
|
err = OS::get_singleton()->get_dynamic_library_symbol_handle(lib, "hostfxr_initialize_for_dotnet_command_line", symbol);
|
|
|
|
ERR_FAIL_COND_V(err != OK, false);
|
|
|
|
hostfxr_initialize_for_dotnet_command_line = (hostfxr_initialize_for_dotnet_command_line_fn)symbol;
|
|
|
|
|
2021-09-13 02:23:05 +08: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-29 06:42:37 +08:00
|
|
|
|
2021-09-13 02:23:05 +08: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-29 06:42:37 +08:00
|
|
|
|
2021-09-13 02:23:05 +08: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-29 06:42:37 +08:00
|
|
|
|
2021-09-13 02:23:05 +08:00
|
|
|
return (hostfxr_initialize_for_runtime_config &&
|
|
|
|
hostfxr_get_runtime_delegate &&
|
|
|
|
hostfxr_close);
|
|
|
|
}
|
2019-11-29 06:42:37 +08:00
|
|
|
|
2021-12-29 06:25:16 +08:00
|
|
|
#ifdef TOOLS_ENABLED
|
|
|
|
load_assembly_and_get_function_pointer_fn initialize_hostfxr_for_config(const char_t *p_config_path) {
|
2021-09-13 02:23:05 +08:00
|
|
|
hostfxr_handle cxt = nullptr;
|
|
|
|
int rc = hostfxr_initialize_for_runtime_config(p_config_path, nullptr, &cxt);
|
|
|
|
if (rc != 0 || cxt == nullptr) {
|
|
|
|
hostfxr_close(cxt);
|
2021-12-29 06:25:16 +08:00
|
|
|
ERR_FAIL_V_MSG(nullptr, "hostfxr_initialize_for_runtime_config failed with code: " + itos(rc));
|
2021-09-13 02:23:05 +08:00
|
|
|
}
|
2019-11-29 06:42:37 +08:00
|
|
|
|
2021-09-13 02:23:05 +08:00
|
|
|
void *load_assembly_and_get_function_pointer = nullptr;
|
2017-10-03 05:24:00 +08:00
|
|
|
|
2021-09-13 02:23:05 +08: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) {
|
2021-12-29 06:25:16 +08:00
|
|
|
ERR_FAIL_V_MSG(nullptr, "hostfxr_get_runtime_delegate failed with code: " + itos(rc));
|
2021-09-13 02:23:05 +08:00
|
|
|
}
|
2019-11-29 06:42:37 +08:00
|
|
|
|
2021-09-13 02:23:05 +08:00
|
|
|
hostfxr_close(cxt);
|
2018-10-04 01:01:57 +08:00
|
|
|
|
2021-09-13 02:23:05 +08:00
|
|
|
return (load_assembly_and_get_function_pointer_fn)load_assembly_and_get_function_pointer;
|
|
|
|
}
|
2021-12-29 06:25:16 +08:00
|
|
|
#else
|
|
|
|
load_assembly_and_get_function_pointer_fn initialize_hostfxr_self_contained(
|
|
|
|
const char_t *p_main_assembly_path) {
|
|
|
|
hostfxr_handle cxt = nullptr;
|
|
|
|
|
|
|
|
List<String> cmdline_args = OS::get_singleton()->get_cmdline_args();
|
|
|
|
|
|
|
|
List<HostFxrCharString> argv_store;
|
|
|
|
Vector<const char_t *> argv;
|
|
|
|
argv.resize(cmdline_args.size() + 1);
|
|
|
|
|
|
|
|
argv.write[0] = p_main_assembly_path;
|
|
|
|
|
|
|
|
int i = 1;
|
|
|
|
for (const String &E : cmdline_args) {
|
|
|
|
HostFxrCharString &stored = argv_store.push_back(str_to_hostfxr(E))->get();
|
|
|
|
argv.write[i] = stored.ptr();
|
|
|
|
i++;
|
|
|
|
}
|
|
|
|
|
|
|
|
int rc = hostfxr_initialize_for_dotnet_command_line(argv.size(), argv.ptrw(), nullptr, &cxt);
|
|
|
|
if (rc != 0 || cxt == nullptr) {
|
|
|
|
hostfxr_close(cxt);
|
|
|
|
ERR_FAIL_V_MSG(nullptr, "hostfxr_initialize_for_dotnet_command_line failed with code: " + itos(rc));
|
|
|
|
}
|
|
|
|
|
|
|
|
void *load_assembly_and_get_function_pointer = nullptr;
|
|
|
|
|
|
|
|
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 with code: " + itos(rc));
|
|
|
|
}
|
|
|
|
|
|
|
|
hostfxr_close(cxt);
|
|
|
|
|
|
|
|
return (load_assembly_and_get_function_pointer_fn)load_assembly_and_get_function_pointer;
|
|
|
|
}
|
|
|
|
#endif
|
|
|
|
|
|
|
|
#ifdef TOOLS_ENABLED
|
2022-08-05 09:32:59 +08:00
|
|
|
using godot_plugins_initialize_fn = bool (*)(void *, bool, gdmono::PluginCallbacks *, GDMonoCache::ManagedCallbacks *, const void **, int32_t);
|
2021-12-29 06:25:16 +08:00
|
|
|
#else
|
2022-08-05 09:32:59 +08:00
|
|
|
using godot_plugins_initialize_fn = bool (*)(void *, GDMonoCache::ManagedCallbacks *, const void **, int32_t);
|
2021-12-29 06:25:16 +08:00
|
|
|
#endif
|
|
|
|
|
|
|
|
#ifdef TOOLS_ENABLED
|
|
|
|
godot_plugins_initialize_fn initialize_hostfxr_and_godot_plugins(bool &r_runtime_initialized) {
|
|
|
|
godot_plugins_initialize_fn godot_plugins_initialize = nullptr;
|
|
|
|
|
|
|
|
HostFxrCharString godot_plugins_path = str_to_hostfxr(
|
|
|
|
GodotSharpDirs::get_api_assemblies_dir().plus_file("GodotPlugins.dll"));
|
|
|
|
|
|
|
|
HostFxrCharString config_path = str_to_hostfxr(
|
|
|
|
GodotSharpDirs::get_api_assemblies_dir().plus_file("GodotPlugins.runtimeconfig.json"));
|
|
|
|
|
|
|
|
load_assembly_and_get_function_pointer_fn load_assembly_and_get_function_pointer =
|
|
|
|
initialize_hostfxr_for_config(get_data(config_path));
|
|
|
|
ERR_FAIL_NULL_V(load_assembly_and_get_function_pointer, nullptr);
|
|
|
|
|
|
|
|
r_runtime_initialized = true;
|
|
|
|
|
|
|
|
print_verbose(".NET: hostfxr initialized");
|
|
|
|
|
|
|
|
int rc = load_assembly_and_get_function_pointer(get_data(godot_plugins_path),
|
|
|
|
HOSTFXR_STR("GodotPlugins.Main, GodotPlugins"),
|
|
|
|
HOSTFXR_STR("InitializeFromEngine"),
|
|
|
|
UNMANAGEDCALLERSONLY_METHOD,
|
|
|
|
nullptr,
|
|
|
|
(void **)&godot_plugins_initialize);
|
|
|
|
ERR_FAIL_COND_V_MSG(rc != 0, nullptr, ".NET: Failed to get GodotPlugins initialization function pointer");
|
|
|
|
|
|
|
|
return godot_plugins_initialize;
|
|
|
|
}
|
|
|
|
#else
|
2022-07-28 23:41:47 +08:00
|
|
|
static String get_assembly_name() {
|
2022-07-28 23:41:51 +08:00
|
|
|
String assembly_name = ProjectSettings::get_singleton()->get_setting("dotnet/project/assembly_name");
|
|
|
|
|
|
|
|
if (assembly_name.is_empty()) {
|
|
|
|
assembly_name = ProjectSettings::get_singleton()->get_safe_project_name();
|
2022-07-28 23:41:47 +08:00
|
|
|
}
|
|
|
|
|
2022-07-28 23:41:51 +08:00
|
|
|
return assembly_name;
|
2022-07-28 23:41:47 +08:00
|
|
|
}
|
|
|
|
|
2021-12-29 06:25:16 +08:00
|
|
|
godot_plugins_initialize_fn initialize_hostfxr_and_godot_plugins(bool &r_runtime_initialized) {
|
|
|
|
godot_plugins_initialize_fn godot_plugins_initialize = nullptr;
|
|
|
|
|
2022-02-28 04:57:52 +08:00
|
|
|
String assembly_name = get_assembly_name();
|
2021-12-29 06:25:16 +08:00
|
|
|
|
|
|
|
HostFxrCharString assembly_path = str_to_hostfxr(GodotSharpDirs::get_api_assemblies_dir()
|
|
|
|
.plus_file(assembly_name + ".dll"));
|
|
|
|
|
|
|
|
load_assembly_and_get_function_pointer_fn load_assembly_and_get_function_pointer =
|
|
|
|
initialize_hostfxr_self_contained(get_data(assembly_path));
|
|
|
|
ERR_FAIL_NULL_V(load_assembly_and_get_function_pointer, nullptr);
|
|
|
|
|
|
|
|
r_runtime_initialized = true;
|
|
|
|
|
|
|
|
print_verbose(".NET: hostfxr initialized");
|
|
|
|
|
|
|
|
int rc = load_assembly_and_get_function_pointer(get_data(assembly_path),
|
|
|
|
str_to_hostfxr("GodotPlugins.Game.Main, " + assembly_name),
|
|
|
|
HOSTFXR_STR("InitializeFromGameProject"),
|
|
|
|
UNMANAGEDCALLERSONLY_METHOD,
|
|
|
|
nullptr,
|
|
|
|
(void **)&godot_plugins_initialize);
|
|
|
|
ERR_FAIL_COND_V_MSG(rc != 0, nullptr, ".NET: Failed to get GodotPlugins initialization function pointer");
|
|
|
|
|
|
|
|
return godot_plugins_initialize;
|
|
|
|
}
|
2022-02-28 04:57:52 +08:00
|
|
|
|
|
|
|
godot_plugins_initialize_fn try_load_native_aot_library(void *&r_aot_dll_handle) {
|
|
|
|
String assembly_name = get_assembly_name();
|
|
|
|
|
|
|
|
#if defined(WINDOWS_ENABLED)
|
|
|
|
String native_aot_so_path = GodotSharpDirs::get_api_assemblies_dir().plus_file(assembly_name + ".dll");
|
|
|
|
#elif defined(MACOS_ENABLED)
|
|
|
|
String native_aot_so_path = GodotSharpDirs::get_api_assemblies_dir().plus_file(assembly_name + ".dylib");
|
|
|
|
#elif defined(UNIX_ENABLED)
|
|
|
|
String native_aot_so_path = GodotSharpDirs::get_api_assemblies_dir().plus_file(assembly_name + ".so");
|
|
|
|
#else
|
|
|
|
#error "Platform not supported (yet?)"
|
|
|
|
#endif
|
|
|
|
|
|
|
|
if (FileAccess::exists(native_aot_so_path)) {
|
|
|
|
Error err = OS::get_singleton()->open_dynamic_library(native_aot_so_path, r_aot_dll_handle);
|
|
|
|
|
|
|
|
if (err != OK) {
|
|
|
|
return nullptr;
|
|
|
|
}
|
|
|
|
|
|
|
|
void *lib = r_aot_dll_handle;
|
|
|
|
|
|
|
|
void *symbol = nullptr;
|
|
|
|
|
|
|
|
err = OS::get_singleton()->get_dynamic_library_symbol_handle(lib, "godotsharp_game_main_init", symbol);
|
|
|
|
ERR_FAIL_COND_V(err != OK, nullptr);
|
|
|
|
return (godot_plugins_initialize_fn)symbol;
|
|
|
|
}
|
|
|
|
|
|
|
|
return nullptr;
|
|
|
|
}
|
2021-12-29 06:25:16 +08:00
|
|
|
#endif
|
|
|
|
|
2021-09-13 02:23:05 +08:00
|
|
|
} // namespace
|
2019-11-11 00:10:38 +08:00
|
|
|
|
2021-09-13 02:23:05 +08:00
|
|
|
static bool _on_core_api_assembly_loaded() {
|
|
|
|
if (!GDMonoCache::godot_api_cache_updated) {
|
|
|
|
return false;
|
|
|
|
}
|
2020-03-19 00:40:04 +08:00
|
|
|
|
2022-02-28 04:57:39 +08:00
|
|
|
bool debug;
|
2021-09-13 02:23:05 +08:00
|
|
|
#ifdef DEBUG_ENABLED
|
2022-02-28 04:57:39 +08:00
|
|
|
debug = true;
|
|
|
|
#else
|
|
|
|
debug = false;
|
2019-11-11 00:10:38 +08:00
|
|
|
#endif
|
2019-01-30 06:00:30 +08:00
|
|
|
|
2022-02-28 04:57:39 +08:00
|
|
|
GDMonoCache::managed_callbacks.GD_OnCoreApiAssemblyLoaded(debug);
|
|
|
|
|
2021-09-13 02:23:05 +08:00
|
|
|
return true;
|
|
|
|
}
|
2017-10-03 05:24:00 +08:00
|
|
|
|
2021-09-13 02:23:05 +08:00
|
|
|
void GDMono::initialize() {
|
|
|
|
print_verbose(".NET: Initializing module...");
|
2018-06-27 03:03:42 +08:00
|
|
|
|
2021-09-13 02:23:05 +08:00
|
|
|
_init_godot_api_hashes();
|
2019-02-11 01:34:34 +08:00
|
|
|
|
2022-02-28 04:57:52 +08:00
|
|
|
godot_plugins_initialize_fn godot_plugins_initialize = nullptr;
|
|
|
|
|
2021-12-29 06:25:16 +08:00
|
|
|
if (!load_hostfxr(hostfxr_dll_handle)) {
|
2022-02-28 04:57:52 +08:00
|
|
|
#if !defined(TOOLS_ENABLED)
|
|
|
|
godot_plugins_initialize = try_load_native_aot_library(hostfxr_dll_handle);
|
|
|
|
|
|
|
|
if (godot_plugins_initialize != nullptr) {
|
|
|
|
is_native_aot = true;
|
|
|
|
} else {
|
|
|
|
ERR_FAIL_MSG(".NET: Failed to load hostfxr");
|
|
|
|
}
|
|
|
|
#else
|
2021-09-13 02:23:05 +08:00
|
|
|
ERR_FAIL_MSG(".NET: Failed to load hostfxr");
|
2022-02-28 04:57:52 +08:00
|
|
|
#endif
|
2019-08-30 07:33:50 +08:00
|
|
|
}
|
|
|
|
|
2022-02-28 04:57:52 +08:00
|
|
|
if (!is_native_aot) {
|
|
|
|
godot_plugins_initialize = initialize_hostfxr_and_godot_plugins(runtime_initialized);
|
|
|
|
ERR_FAIL_NULL(godot_plugins_initialize);
|
|
|
|
}
|
2019-12-04 22:07:00 +08:00
|
|
|
|
2022-08-05 09:32:59 +08:00
|
|
|
int32_t interop_funcs_size = 0;
|
|
|
|
const void **interop_funcs = godotsharp::get_runtime_interop_funcs(interop_funcs_size);
|
|
|
|
|
|
|
|
GDMonoCache::ManagedCallbacks managed_callbacks{};
|
2021-12-29 06:25:16 +08:00
|
|
|
|
2022-02-28 04:57:52 +08:00
|
|
|
void *godot_dll_handle = nullptr;
|
|
|
|
|
|
|
|
#if defined(UNIX_ENABLED) && !defined(MACOS_ENABLED) && !defined(IOS_ENABLED)
|
|
|
|
// Managed code can access it on its own on other platforms
|
|
|
|
godot_dll_handle = dlopen(nullptr, RTLD_NOW);
|
|
|
|
#endif
|
|
|
|
|
2021-12-29 06:25:16 +08:00
|
|
|
#ifdef TOOLS_ENABLED
|
|
|
|
gdmono::PluginCallbacks plugin_callbacks_res;
|
2022-02-28 04:57:52 +08:00
|
|
|
bool init_ok = godot_plugins_initialize(godot_dll_handle,
|
|
|
|
Engine::get_singleton()->is_editor_hint(),
|
2022-08-05 09:32:59 +08:00
|
|
|
&plugin_callbacks_res, &managed_callbacks,
|
|
|
|
interop_funcs, interop_funcs_size);
|
2021-12-29 06:25:16 +08:00
|
|
|
ERR_FAIL_COND_MSG(!init_ok, ".NET: GodotPlugins initialization failed");
|
|
|
|
|
|
|
|
plugin_callbacks = plugin_callbacks_res;
|
|
|
|
#else
|
2022-08-05 09:32:59 +08:00
|
|
|
bool init_ok = godot_plugins_initialize(godot_dll_handle, &managed_callbacks,
|
|
|
|
interop_funcs, interop_funcs_size);
|
2021-12-29 06:25:16 +08:00
|
|
|
ERR_FAIL_COND_MSG(!init_ok, ".NET: GodotPlugins initialization failed");
|
|
|
|
#endif
|
2017-10-03 05:24:00 +08:00
|
|
|
|
2021-09-13 02:23:05 +08:00
|
|
|
GDMonoCache::update_godot_api_cache(managed_callbacks);
|
2017-10-03 05:24:00 +08:00
|
|
|
|
2021-09-13 02:23:05 +08:00
|
|
|
print_verbose(".NET: GodotPlugins initialized");
|
2017-10-03 05:24:00 +08:00
|
|
|
|
2021-09-13 02:23:05 +08:00
|
|
|
_on_core_api_assembly_loaded();
|
2019-07-11 20:01:25 +08:00
|
|
|
}
|
2019-07-03 15:44:53 +08:00
|
|
|
|
2021-12-29 06:25:16 +08:00
|
|
|
#ifdef TOOLS_ENABLED
|
2019-07-11 20:01:25 +08:00
|
|
|
void GDMono::initialize_load_assemblies() {
|
2022-02-03 08:21:52 +08:00
|
|
|
if (Engine::get_singleton()->is_project_manager_hint()) {
|
2020-05-22 08:17:06 +08:00
|
|
|
return;
|
2020-07-06 01:19:36 +08:00
|
|
|
}
|
2020-05-22 08:17:06 +08:00
|
|
|
|
2019-07-11 20:01:25 +08: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-06 01:19:36 +08:00
|
|
|
if (OS::get_singleton()->is_stdout_verbose()) {
|
2021-09-13 02:23:05 +08:00
|
|
|
print_error(".NET: Failed to load project assembly");
|
2020-07-06 01:19:36 +08:00
|
|
|
}
|
2019-07-11 20:01:25 +08:00
|
|
|
}
|
|
|
|
}
|
2021-12-29 06:25:16 +08:00
|
|
|
#endif
|
2019-07-11 20:01:25 +08:00
|
|
|
|
2019-11-29 06:42:37 +08: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 21:21:06 +08:00
|
|
|
#ifdef DEBUG_METHODS_ENABLED
|
|
|
|
get_api_core_hash();
|
2017-10-03 05:24:00 +08: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 21:21:06 +08:00
|
|
|
get_api_editor_hash();
|
2017-10-03 05:24:00 +08: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 21:21:06 +08:00
|
|
|
#endif // DEBUG_METHODS_ENABLED
|
2019-11-29 06:42:37 +08:00
|
|
|
}
|
|
|
|
|
2021-12-29 06:25:16 +08:00
|
|
|
#ifdef TOOLS_ENABLED
|
2017-10-03 05:24:00 +08:00
|
|
|
bool GDMono::_load_project_assembly() {
|
2022-07-28 23:41:51 +08:00
|
|
|
String assembly_name = ProjectSettings::get_singleton()->get_setting("dotnet/project/assembly_name");
|
|
|
|
|
|
|
|
if (assembly_name.is_empty()) {
|
|
|
|
assembly_name = ProjectSettings::get_singleton()->get_safe_project_name();
|
|
|
|
}
|
2017-10-03 05:24:00 +08:00
|
|
|
|
2021-09-13 02:23:05 +08:00
|
|
|
String assembly_path = GodotSharpDirs::get_res_temp_assemblies_dir()
|
2022-07-28 23:41:51 +08:00
|
|
|
.plus_file(assembly_name + ".dll");
|
2021-09-13 02:23:05 +08:00
|
|
|
assembly_path = ProjectSettings::get_singleton()->globalize_path(assembly_path);
|
2017-10-03 05:24:00 +08:00
|
|
|
|
2022-05-28 10:56:46 +08:00
|
|
|
String loaded_assembly_path;
|
|
|
|
bool success = plugin_callbacks.LoadProjectAssemblyCallback(assembly_path.utf16(), &loaded_assembly_path);
|
2018-10-07 05:00:18 +08:00
|
|
|
|
2022-05-28 10:56:46 +08:00
|
|
|
if (success) {
|
|
|
|
project_assembly_path = loaded_assembly_path.simplify_path();
|
|
|
|
project_assembly_modified_time = FileAccess::get_modified_time(loaded_assembly_path);
|
2018-10-07 05:00:18 +08:00
|
|
|
}
|
|
|
|
|
2022-05-28 10:56:46 +08:00
|
|
|
return success;
|
2017-10-03 05:24:00 +08:00
|
|
|
}
|
2022-05-28 10:56:46 +08:00
|
|
|
#endif
|
2017-10-03 05:24:00 +08:00
|
|
|
|
2019-01-23 01:33:36 +08:00
|
|
|
#ifdef GD_MONO_HOT_RELOAD
|
2022-05-28 10:56:46 +08:00
|
|
|
Error GDMono::reload_project_assemblies() {
|
2017-10-03 05:24:00 +08:00
|
|
|
ERR_FAIL_COND_V(!runtime_initialized, ERR_BUG);
|
|
|
|
|
2022-05-28 10:56:46 +08:00
|
|
|
finalizing_scripts_domain = true;
|
2017-10-03 05:24:00 +08:00
|
|
|
|
2022-05-28 10:56:46 +08:00
|
|
|
CSharpLanguage::get_singleton()->_on_scripts_domain_about_to_unload();
|
2018-02-22 20:13:51 +08:00
|
|
|
|
2022-05-28 10:56:46 +08:00
|
|
|
if (!get_plugin_callbacks().UnloadProjectPluginCallback()) {
|
|
|
|
ERR_FAIL_V_MSG(Error::FAILED, ".NET: Failed to unload assemblies.");
|
2021-09-13 02:21:15 +08:00
|
|
|
}
|
2018-02-22 20:13:51 +08:00
|
|
|
|
2022-05-28 10:56:46 +08:00
|
|
|
finalizing_scripts_domain = false;
|
2019-07-03 15:44:53 +08:00
|
|
|
|
2019-07-11 20:01:25 +08: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.
|
2018-09-27 05:59:38 +08:00
|
|
|
if (!_load_project_assembly()) {
|
2022-05-28 10:56:46 +08:00
|
|
|
print_error(".NET: Failed to load project assembly.");
|
2018-02-22 20:13:51 +08:00
|
|
|
return ERR_CANT_OPEN;
|
2018-09-27 05:59:38 +08:00
|
|
|
}
|
2018-02-22 20:13:51 +08:00
|
|
|
|
2017-10-03 05:24:00 +08:00
|
|
|
return OK;
|
|
|
|
}
|
|
|
|
#endif
|
2018-02-22 20:39:41 +08:00
|
|
|
|
2017-10-03 05:24:00 +08:00
|
|
|
GDMono::GDMono() {
|
|
|
|
singleton = this;
|
|
|
|
|
|
|
|
runtime_initialized = false;
|
2018-10-07 05:00:18 +08:00
|
|
|
finalizing_scripts_domain = false;
|
2017-10-03 05:24:00 +08:00
|
|
|
|
|
|
|
api_core_hash = 0;
|
|
|
|
#ifdef TOOLS_ENABLED
|
|
|
|
api_editor_hash = 0;
|
|
|
|
#endif
|
|
|
|
}
|
|
|
|
|
|
|
|
GDMono::~GDMono() {
|
2021-12-29 06:25:16 +08:00
|
|
|
finalizing_scripts_domain = true;
|
|
|
|
|
2018-09-12 08:41:54 +08:00
|
|
|
if (is_runtime_initialized()) {
|
2021-12-29 06:25:16 +08:00
|
|
|
if (GDMonoCache::godot_api_cache_updated) {
|
|
|
|
GDMonoCache::managed_callbacks.DisposablesTracker_OnGodotShuttingDown();
|
2017-10-03 05:24:00 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-12-29 06:25:16 +08:00
|
|
|
if (hostfxr_dll_handle) {
|
|
|
|
OS::get_singleton()->close_dynamic_library(hostfxr_dll_handle);
|
|
|
|
}
|
|
|
|
|
2021-12-29 06:25:16 +08:00
|
|
|
finalizing_scripts_domain = false;
|
|
|
|
runtime_initialized = false;
|
|
|
|
|
2020-03-19 00:40:04 +08:00
|
|
|
#if defined(ANDROID_ENABLED)
|
|
|
|
gdmono::android::support::cleanup();
|
|
|
|
#endif
|
|
|
|
|
2020-04-02 07:20:12 +08:00
|
|
|
singleton = nullptr;
|
2017-10-03 05:24:00 +08:00
|
|
|
}
|
|
|
|
|
2021-08-13 22:46:14 +08:00
|
|
|
namespace mono_bind {
|
2017-10-03 05:24:00 +08:00
|
|
|
|
2021-08-13 22:46:14 +08:00
|
|
|
GodotSharp *GodotSharp::singleton = nullptr;
|
|
|
|
|
2021-09-13 02:23:05 +08:00
|
|
|
bool GodotSharp::_is_runtime_initialized() {
|
2020-07-06 01:19:36 +08:00
|
|
|
return GDMono::get_singleton() != nullptr && GDMono::get_singleton()->is_runtime_initialized();
|
2017-10-03 05:24:00 +08:00
|
|
|
}
|
|
|
|
|
2021-08-13 22:46:14 +08:00
|
|
|
void GodotSharp::_reload_assemblies(bool p_soft_reload) {
|
2019-07-03 15:44:53 +08:00
|
|
|
#ifdef GD_MONO_HOT_RELOAD
|
2020-07-06 01:19:36 +08:00
|
|
|
CRASH_COND(CSharpLanguage::get_singleton() == nullptr);
|
2020-04-23 08:23:25 +08: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-06 01:19:36 +08:00
|
|
|
if (CSharpLanguage::get_singleton()->is_assembly_reloading_needed()) {
|
2020-04-23 08:23:25 +08:00
|
|
|
CSharpLanguage::get_singleton()->reload_assemblies(p_soft_reload);
|
2020-07-06 01:19:36 +08:00
|
|
|
}
|
2019-07-03 15:44:53 +08:00
|
|
|
#endif
|
|
|
|
}
|
|
|
|
|
2021-08-13 22:46:14 +08:00
|
|
|
void GodotSharp::_bind_methods() {
|
2021-09-13 02:23:05 +08:00
|
|
|
ClassDB::bind_method(D_METHOD("is_runtime_initialized"), &GodotSharp::_is_runtime_initialized);
|
2021-08-13 22:46:14 +08:00
|
|
|
ClassDB::bind_method(D_METHOD("_reload_assemblies"), &GodotSharp::_reload_assemblies);
|
2017-10-03 05:24:00 +08:00
|
|
|
}
|
|
|
|
|
2021-08-13 22:46:14 +08:00
|
|
|
GodotSharp::GodotSharp() {
|
2017-10-03 05:24:00 +08:00
|
|
|
singleton = this;
|
|
|
|
}
|
|
|
|
|
2021-08-13 22:46:14 +08:00
|
|
|
GodotSharp::~GodotSharp() {
|
2020-04-02 07:20:12 +08:00
|
|
|
singleton = nullptr;
|
2017-10-03 05:24:00 +08:00
|
|
|
}
|
2021-08-13 22:46:14 +08:00
|
|
|
|
|
|
|
} // namespace mono_bind
|