/*************************************************************************/ /* csharp_script.cpp */ /*************************************************************************/ /* 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). */ /* */ /* 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. */ /*************************************************************************/ #include "csharp_script.h" #include #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/os/mutex.h" #include "core/os/os.h" #include "core/os/thread.h" #ifdef TOOLS_ENABLED #include "core/os/keyboard.h" #include "editor/bindings_generator.h" #include "editor/editor_internal_calls.h" #include "editor/editor_node.h" #include "editor/editor_settings.h" #include "editor/node_dock.h" #include "editor/script_templates/templates.gen.h" #endif #ifdef DEBUG_METHODS_ENABLED #include "class_db_api_json.h" #endif #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/string_utils.h" #define CACHED_STRING_NAME(m_var) (CSharpLanguage::get_singleton()->get_string_names().m_var) #ifdef TOOLS_ENABLED static bool _create_project_solution_if_needed() { String sln_path = GodotSharpDirs::get_project_sln_path(); String csproj_path = GodotSharpDirs::get_project_csproj_path(); if (!FileAccess::exists(sln_path) || !FileAccess::exists(csproj_path)) { // A solution does not yet exist, create a new one CRASH_COND(CSharpLanguage::get_singleton()->get_godotsharp_editor() == nullptr); return CSharpLanguage::get_singleton()->get_godotsharp_editor()->call("CreateProjectSolution"); } return true; } #endif CSharpLanguage *CSharpLanguage::singleton = nullptr; GDNativeInstanceBindingCallbacks CSharpLanguage::_instance_binding_callbacks = { &_instance_binding_create_callback, &_instance_binding_free_callback, &_instance_binding_reference_callback }; String CSharpLanguage::get_name() const { return "C#"; } String CSharpLanguage::get_type() const { return "CSharpScript"; } String CSharpLanguage::get_extension() const { return "cs"; } Error CSharpLanguage::execute_file(const String &p_path) { // ?? return OK; } extern void *godotsharp_pinvoke_funcs[178]; [[maybe_unused]] volatile void **do_not_strip_godotsharp_pinvoke_funcs; #ifdef TOOLS_ENABLED extern void *godotsharp_editor_pinvoke_funcs[32]; [[maybe_unused]] volatile void **do_not_strip_godotsharp_editor_pinvoke_funcs; #endif void CSharpLanguage::init() { #ifdef DEBUG_METHODS_ENABLED if (OS::get_singleton()->get_cmdline_args().find("--class-db-json")) { class_db_api_to_json("user://class_db_api.json", ClassDB::API_CORE); #ifdef TOOLS_ENABLED class_db_api_to_json("user://class_db_api_editor.json", ClassDB::API_EDITOR); #endif } #endif // Hopefully this will be enough for all compilers. Otherwise we could use the printf on fake getenv trick. do_not_strip_godotsharp_pinvoke_funcs = (volatile void **)godotsharp_pinvoke_funcs; #ifdef TOOLS_ENABLED do_not_strip_godotsharp_editor_pinvoke_funcs = (volatile void **)godotsharp_editor_pinvoke_funcs; #endif #if defined(TOOLS_ENABLED) && defined(DEBUG_METHODS_ENABLED) // Generate the bindings here, before loading assemblies. The Godot assemblies // may be missing if the glue wasn't generated yet in order to build them. List cmdline_args = OS::get_singleton()->get_cmdline_args(); BindingsGenerator::handle_cmdline_args(cmdline_args); #endif gdmono = memnew(GDMono); gdmono->initialize(); if (gdmono->is_runtime_initialized()) { gdmono->initialize_load_assemblies(); } #ifdef TOOLS_ENABLED EditorNode::add_init_callback(&_editor_init_callback); #endif } void CSharpLanguage::finish() { finalize(); } void CSharpLanguage::finalize() { if (finalized) { return; } finalizing = true; // Make sure all script binding gchandles are released before finalizing GDMono for (KeyValue &E : script_bindings) { CSharpScriptBinding &script_binding = E.value; if (!script_binding.gchandle.is_released()) { script_binding.gchandle.release(); script_binding.inited = false; } } if (gdmono) { memdelete(gdmono); gdmono = nullptr; } // Clear here, after finalizing all domains to make sure there is nothing else referencing the elements. script_bindings.clear(); #ifdef DEBUG_ENABLED for (const KeyValue &E : unsafe_object_references) { const ObjectID &id = E.key; Object *obj = ObjectDB::get_instance(id); if (obj) { ERR_PRINT("Leaked unsafe reference to object: " + obj->to_string()); } else { ERR_PRINT("Leaked unsafe reference to deleted object: " + itos(id)); } } #endif memdelete(managed_callable_middleman); finalizing = false; finalized = true; } void CSharpLanguage::get_reserved_words(List *p_words) const { static const char *_reserved_words[] = { // Reserved keywords "abstract", "as", "base", "bool", "break", "byte", "case", "catch", "char", "checked", "class", "const", "continue", "decimal", "default", "delegate", "do", "double", "else", "enum", "event", "explicit", "extern", "false", "finally", "fixed", "float", "for", "foreach", "goto", "if", "implicit", "in", "int", "interface", "internal", "is", "lock", "long", "namespace", "new", "null", "object", "operator", "out", "override", "params", "private", "protected", "public", "readonly", "ref", "return", "sbyte", "sealed", "short", "sizeof", "stackalloc", "static", "string", "struct", "switch", "this", "throw", "true", "try", "typeof", "uint", "ulong", "unchecked", "unsafe", "ushort", "using", "virtual", "void", "volatile", "while", // Contextual keywords. Not reserved words, but I guess we should include // them because this seems to be used only for syntax highlighting. "add", "alias", "ascending", "async", "await", "by", "descending", "dynamic", "equals", "from", "get", "global", "group", "into", "join", "let", "nameof", "on", "orderby", "partial", "remove", "select", "set", "value", "var", "when", "where", "yield", nullptr }; const char **w = _reserved_words; while (*w) { p_words->push_back(*w); w++; } } bool CSharpLanguage::is_control_flow_keyword(String p_keyword) const { return p_keyword == "break" || p_keyword == "case" || p_keyword == "catch" || p_keyword == "continue" || p_keyword == "default" || p_keyword == "do" || p_keyword == "else" || p_keyword == "finally" || p_keyword == "for" || p_keyword == "foreach" || p_keyword == "goto" || p_keyword == "if" || p_keyword == "return" || p_keyword == "switch" || p_keyword == "throw" || p_keyword == "try" || p_keyword == "while"; } void CSharpLanguage::get_comment_delimiters(List *p_delimiters) const { p_delimiters->push_back("//"); // single-line comment p_delimiters->push_back("/* */"); // delimited comment } void CSharpLanguage::get_string_delimiters(List *p_delimiters) const { p_delimiters->push_back("' '"); // character literal p_delimiters->push_back("\" \""); // regular string literal p_delimiters->push_back("@\" \""); // verbatim string literal // Generic string highlighting suffices as a workaround for now. } static String get_base_class_name(const String &p_base_class_name, const String p_class_name) { String base_class = p_base_class_name; if (p_class_name == base_class) { base_class = "Godot." + base_class; } return base_class; } bool CSharpLanguage::is_using_templates() { return true; } Ref