feat: updated engine

This commit is contained in:
Sara Gerretsen 2026-07-10 17:04:34 +02:00
parent cbe99774ff
commit f4cf6b3999
6607 changed files with 910135 additions and 430025 deletions

View file

@ -30,15 +30,19 @@
#include "csharp_script.h"
#include "godotsharp_defs.h"
#include "godotsharp_dirs.h"
#include "managed_callable.h"
#include "mono_gd/gd_mono_cache.h"
#include "signal_awaiter_utils.h"
#include "utils/macros.h"
#include "utils/naming_utils.h"
#include "utils/path_utils.h"
#include "utils/string_utils.h"
#ifdef GD_MONO_HOT_RELOAD
#include "managed_callable.h"
#include "utils/path_utils.h"
#endif
#ifdef DEBUG_ENABLED
#include "class_db_api_json.h"
#endif // DEBUG_ENABLED
@ -48,10 +52,12 @@
#include "editor/script_templates/templates.gen.h"
#endif
#include "core/config/engine.h"
#include "core/config/project_settings.h"
#include "core/debugger/engine_debugger.h"
#include "core/debugger/script_debugger.h"
#include "core/io/file_access.h"
#include "core/object/class_db.h"
#include "core/os/mutex.h"
#include "core/os/os.h"
#include "core/os/thread.h"
@ -59,24 +65,20 @@
#ifdef TOOLS_ENABLED
#include "core/os/keyboard.h"
#include "editor/docks/inspector_dock.h"
#include "editor/docks/signals_dock.h"
#include "editor/editor_node.h"
#include "editor/file_system/editor_file_system.h"
#include "editor/settings/editor_settings.h"
#ifdef GD_MONO_HOT_RELOAD
#include "editor/docks/inspector_dock.h"
#include "editor/docks/signals_dock.h"
#endif
#endif
// Types that will be skipped over (in favor of their base types) when setting up instance bindings.
// This must be a superset of `ignored_types` in bindings_generator.cpp.
const Vector<String> ignored_types = {};
#ifdef TOOLS_ENABLED
static bool _create_project_solution_if_needed() {
CRASH_COND(CSharpLanguage::get_singleton()->get_godotsharp_editor() == nullptr);
return CSharpLanguage::get_singleton()->get_godotsharp_editor()->call("CreateProjectSolutionIfNeeded");
}
#endif
CSharpLanguage *CSharpLanguage::singleton = nullptr;
GDExtensionInstanceBindingCallbacks CSharpLanguage::_instance_binding_callbacks = {
@ -400,10 +402,6 @@ String CSharpLanguage::validate_path(const String &p_path) const {
return "";
}
Script *CSharpLanguage::create_script() const {
return memnew(CSharpScript);
}
bool CSharpLanguage::supports_builtin_mode() const {
return false;
}
@ -896,7 +894,7 @@ void CSharpLanguage::reload_assemblies(bool p_soft_reload) {
continue;
}
if (!ClassDB::is_parent_class(obj->get_class_name(), native_name)) {
if (!obj->is_class(native_name)) {
// No longer inherits the same compatible type, can't reload
scr->pending_reload_state.erase(obj_id);
continue;
@ -1153,14 +1151,14 @@ bool CSharpLanguage::setup_csharp_script_binding(CSharpScriptBinding &r_script_b
// workaround to allow GDExtension classes to be used from C# so long as they're only used through base classes that
// are registered from the engine. This will likely need to be removed whenever proper support for GDExtension
// classes is added to C#. See #75955 for more details.
while (classinfo && (!classinfo->exposed || classinfo->gdextension || ignored_types.has(classinfo->name))) {
while (classinfo && (!classinfo->exposed || classinfo->gdextension || ignored_types.has(classinfo->gdtype->get_name()))) {
classinfo = classinfo->inherits_ptr;
}
ERR_FAIL_NULL_V(classinfo, false);
type_name = classinfo->name;
type_name = classinfo->gdtype->get_name();
bool parent_is_object_class = ClassDB::is_parent_class(p_object->get_class_name(), type_name);
bool parent_is_object_class = p_object->is_class(type_name);
ERR_FAIL_COND_V_MSG(!parent_is_object_class, false,
"Type inherits from native type '" + type_name + "', so it can't be instantiated in object of type: '" + p_object->get_class() + "'.");
@ -1649,7 +1647,7 @@ bool CSharpInstance::property_get_revert(const StringName &p_name, Variant &r_re
}
void CSharpInstance::get_method_list(List<MethodInfo> *p_list) const {
if (!script->is_valid() || !script->valid) {
if (!script->is_script_valid() || !script->valid) {
return;
}
@ -1670,7 +1668,7 @@ bool CSharpInstance::has_method(const StringName &p_method) const {
}
int CSharpInstance::get_method_argument_count(const StringName &p_method, bool *r_is_valid) const {
if (!script->is_valid() || !script->valid) {
if (!script->is_script_valid() || !script->valid) {
if (r_is_valid) {
*r_is_valid = false;
}
@ -2466,7 +2464,7 @@ ScriptInstance *CSharpScript::instance_create(Object *p_this) {
ERR_FAIL_COND_V(native_name == StringName(), nullptr);
if (!ClassDB::is_parent_class(p_this->get_class_name(), native_name)) {
if (!p_this->is_class(native_name)) {
if (EngineDebugger::is_active()) {
CSharpLanguage::get_singleton()->debug_break_parse(get_path(), 0,
"Script inherits from native type '" + String(native_name) +
@ -2490,11 +2488,6 @@ PlaceHolderScriptInstance *CSharpScript::placeholder_instance_create(Object *p_t
#endif
}
bool CSharpScript::instance_has(const Object *p_this) const {
MutexLock lock(CSharpLanguage::get_singleton()->script_instances_mutex);
return instances.has((Object *)p_this);
}
bool CSharpScript::has_source_code() const {
return !source.is_empty();
}
@ -2821,123 +2814,3 @@ void CSharpScript::get_members(HashSet<StringName> *p_members) {
}
#endif // DEBUG_ENABLED
}
/*************** RESOURCE ***************/
Ref<Resource> ResourceFormatLoaderCSharpScript::load(const String &p_path, const String &p_original_path, Error *r_error, bool p_use_sub_threads, float *r_progress, CacheMode p_cache_mode) {
if (r_error) {
*r_error = ERR_FILE_CANT_OPEN;
}
// TODO ignore anything inside bin/ and obj/ in tools builds?
String real_path = p_path;
if (p_path.begins_with("csharp://")) {
// This is a virtual path used by generic types, extract the real path.
real_path = "res://" + p_path.trim_prefix("csharp://");
real_path = real_path.substr(0, real_path.rfind_char(':'));
}
Ref<CSharpScript> scr;
if (GDMonoCache::godot_api_cache_updated) {
GDMonoCache::managed_callbacks.ScriptManagerBridge_GetOrCreateScriptBridgeForPath(&p_path, &scr);
ERR_FAIL_COND_V_MSG(scr.is_null(), Ref<Resource>(), "Could not create C# script '" + real_path + "'.");
} else {
scr.instantiate();
}
#ifdef DEBUG_ENABLED
Error err = scr->load_source_code(real_path);
ERR_FAIL_COND_V_MSG(err != OK, Ref<Resource>(), "Cannot load C# script file '" + real_path + "'.");
#endif // DEBUG_ENABLED
// Only one instance of a C# script is allowed to exist.
ERR_FAIL_COND_V_MSG(!scr->get_path().is_empty() && scr->get_path() != p_original_path, Ref<Resource>(),
"The C# script path is different from the path it was registered in the C# dictionary.");
Ref<Resource> existing = ResourceCache::get_ref(p_path);
switch (p_cache_mode) {
case ResourceFormatLoader::CACHE_MODE_IGNORE:
case ResourceFormatLoader::CACHE_MODE_IGNORE_DEEP:
break;
case ResourceFormatLoader::CACHE_MODE_REUSE:
if (existing.is_null()) {
scr->set_path(p_original_path);
} else {
scr = existing;
}
break;
case ResourceFormatLoader::CACHE_MODE_REPLACE:
case ResourceFormatLoader::CACHE_MODE_REPLACE_DEEP:
scr->set_path(p_original_path, true);
break;
}
scr->reload();
if (r_error) {
*r_error = OK;
}
return scr;
}
void ResourceFormatLoaderCSharpScript::get_recognized_extensions(List<String> *p_extensions) const {
p_extensions->push_back("cs");
}
bool ResourceFormatLoaderCSharpScript::handles_type(const String &p_type) const {
return p_type == "Script" || p_type == CSharpLanguage::get_singleton()->get_type();
}
String ResourceFormatLoaderCSharpScript::get_resource_type(const String &p_path) const {
return p_path.has_extension("cs") ? CSharpLanguage::get_singleton()->get_type() : "";
}
Error ResourceFormatSaverCSharpScript::save(const Ref<Resource> &p_resource, const String &p_path, uint32_t p_flags) {
Ref<CSharpScript> sqscr = p_resource;
ERR_FAIL_COND_V(sqscr.is_null(), ERR_INVALID_PARAMETER);
String source = sqscr->get_source_code();
#ifdef TOOLS_ENABLED
if (!FileAccess::exists(p_path)) {
// The file does not yet exist, let's assume the user just created this script. In such
// cases we need to check whether the solution and csproj were already created or not.
if (!_create_project_solution_if_needed()) {
ERR_PRINT("C# project could not be created; cannot add file: '" + p_path + "'.");
}
}
#endif
{
Error err;
Ref<FileAccess> file = FileAccess::open(p_path, FileAccess::WRITE, &err);
ERR_FAIL_COND_V_MSG(err != OK, err, "Cannot save C# script file '" + p_path + "'.");
file->store_string(source);
if (file->get_error() != OK && file->get_error() != ERR_FILE_EOF) {
return ERR_CANT_CREATE;
}
}
#ifdef TOOLS_ENABLED
if (ScriptServer::is_reload_scripts_on_save_enabled()) {
CSharpLanguage::get_singleton()->reload_tool_script(p_resource, false);
}
#endif
return OK;
}
void ResourceFormatSaverCSharpScript::get_recognized_extensions(const Ref<Resource> &p_resource, List<String> *p_extensions) const {
if (Object::cast_to<CSharpScript>(p_resource.ptr())) {
p_extensions->push_back("cs");
}
}
bool ResourceFormatSaverCSharpScript::recognize(const Ref<Resource> &p_resource) const {
return Object::cast_to<CSharpScript>(p_resource.ptr()) != nullptr;
}