godot/modules/mono/mono_gd/gd_mono.h

162 lines
4.7 KiB
C++
Raw Normal View History

2017-10-02 21:24:00 +00:00
/*************************************************************************/
/* gd_mono.h */
/*************************************************************************/
/* This file is part of: */
/* GODOT ENGINE */
/* https://godotengine.org */
/*************************************************************************/
/* 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. */
/*************************************************************************/
2017-10-02 21:24:00 +00:00
#ifndef GD_MONO_H
#define GD_MONO_H
#include "core/io/config_file.h"
2017-10-02 21:24:00 +00:00
#include "../godotsharp_defs.h"
#ifdef WIN32
#define GD_CLR_STDCALL __stdcall
#else
#define GD_CLR_STDCALL
#endif
namespace gdmono {
#ifdef TOOLS_ENABLED
struct PluginCallbacks {
using FuncLoadProjectAssemblyCallback = bool(GD_CLR_STDCALL *)(const char16_t *);
using FuncLoadToolsAssemblyCallback = Object *(GD_CLR_STDCALL *)(const char16_t *);
FuncLoadProjectAssemblyCallback LoadProjectAssemblyCallback = nullptr;
FuncLoadToolsAssemblyCallback LoadToolsAssemblyCallback = nullptr;
};
#endif
} // namespace gdmono
#undef GD_CLR_STDCALL
2017-10-02 21:24:00 +00:00
class GDMono {
bool runtime_initialized;
bool finalizing_scripts_domain;
void *hostfxr_dll_handle = nullptr;
#ifdef TOOLS_ENABLED
2017-10-02 21:24:00 +00:00
bool _load_project_assembly();
#endif
2017-10-02 21:24:00 +00:00
C#: Restructure code prior move to .NET Core The main focus here was to remove the majority of code that relied on Mono's embedding APIs, specially the reflection APIs. The embedding APIs we still use are the bare minimum we need for things to work. A lot of code was moved to C#. We no longer deal with any managed objects (`MonoObject*`, and such) in native code, and all marshaling is done in C#. The reason for restructuring the code and move away from embedding APIs is that once we move to .NET Core, we will be limited by the much more minimal .NET hosting. PERFORMANCE REGRESSIONS ----------------------- Some parts of the code were written with little to no concern about performance. This includes code that calls into script methods and accesses script fields, properties and events. The reason for this is that all of that will be moved to source generators, so any work prior to that would be a waste of time. DISABLED FEATURES ----------------- Some code was removed as it no longer makes sense (or won't make sense in the future). Other parts were commented out with `#if 0`s and TODO warnings because it doesn't make much sense to work on them yet as those parts will change heavily when we switch to .NET Core but also when we start introducing source generators. As such, the following features were disabled temporarily: - Assembly-reloading (will be done with ALCs in .NET Core). - Properties/fields exports and script method listing (will be handled by source generators in the future). - Exception logging in the editor and stack info for errors. - Exporting games. - Building of C# projects. We no longer copy the Godot API assemblies to the project directory, so MSBuild won't be able to find them. The idea is to turn them into NuGet packages in the future, which could also be obtained from local NuGet sources during development.
2021-09-12 18:21:15 +00:00
bool _try_load_api_assemblies();
2017-10-02 21:24:00 +00:00
Error _unload_scripts_domain();
2017-10-02 21:24:00 +00:00
uint64_t api_core_hash;
#ifdef TOOLS_ENABLED
uint64_t api_editor_hash;
#endif
void _init_godot_api_hashes();
2017-10-02 21:24:00 +00:00
#ifdef TOOLS_ENABLED
gdmono::PluginCallbacks plugin_callbacks;
2017-10-02 21:24:00 +00:00
#endif
2017-10-02 21:24:00 +00:00
protected:
static GDMono *singleton;
public:
#ifdef DEBUG_METHODS_ENABLED
2019-01-28 23:02:35 +00:00
uint64_t get_api_core_hash() {
if (api_core_hash == 0) {
2019-01-28 23:02:35 +00:00
api_core_hash = ClassDB::get_api_hash(ClassDB::API_CORE);
}
2019-01-28 23:02:35 +00:00
return api_core_hash;
}
2017-10-02 21:24:00 +00:00
#ifdef TOOLS_ENABLED
2019-01-28 23:02:35 +00:00
uint64_t get_api_editor_hash() {
if (api_editor_hash == 0) {
2019-01-28 23:02:35 +00:00
api_editor_hash = ClassDB::get_api_hash(ClassDB::API_EDITOR);
}
2019-01-28 23:02:35 +00:00
return api_editor_hash;
}
#endif // TOOLS_ENABLED
#endif // DEBUG_METHODS_ENABLED
2017-10-02 21:24:00 +00:00
_FORCE_INLINE_ static String get_expected_api_build_config() {
#ifdef TOOLS_ENABLED
return "Debug";
#else
#ifdef DEBUG_ENABLED
return "Debug";
#else
return "Release";
#endif
#endif
}
2017-10-02 21:24:00 +00:00
static GDMono *get_singleton() { return singleton; }
_FORCE_INLINE_ bool is_runtime_initialized() const { return runtime_initialized; }
_FORCE_INLINE_ bool is_finalizing_scripts_domain() { return finalizing_scripts_domain; }
#ifdef TOOLS_ENABLED
const gdmono::PluginCallbacks &get_plugin_callbacks() { return plugin_callbacks; }
#endif
#ifdef GD_MONO_HOT_RELOAD
2017-10-02 21:24:00 +00:00
Error reload_scripts_domain();
#endif
void initialize();
#ifdef TOOLS_ENABLED
void initialize_load_assemblies();
#endif
2017-10-02 21:24:00 +00:00
GDMono();
~GDMono();
};
namespace mono_bind {
class GodotSharp : public Object {
GDCLASS(GodotSharp, Object);
2017-10-02 21:24:00 +00:00
friend class GDMono;
void _reload_assemblies(bool p_soft_reload);
bool _is_runtime_initialized();
2017-10-02 21:24:00 +00:00
protected:
static GodotSharp *singleton;
2017-10-02 21:24:00 +00:00
static void _bind_methods();
public:
static GodotSharp *get_singleton() { return singleton; }
2017-10-02 21:24:00 +00:00
GodotSharp();
~GodotSharp();
2017-10-02 21:24:00 +00:00
};
} // namespace mono_bind
2017-10-02 21:24:00 +00:00
#endif // GD_MONO_H