/*************************************************************************/ /* csharp_script.cpp */ /*************************************************************************/ /* This file is part of: */ /* GODOT ENGINE */ /* https://godotengine.org */ /*************************************************************************/ /* Copyright (c) 2007-2020 Juan Linietsky, Ariel Manzur. */ /* Copyright (c) 2014-2020 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 #include "core/io/json.h" #include "core/os/file_access.h" #include "core/os/os.h" #include "core/os/thread.h" #include "core/project_settings.h" #ifdef TOOLS_ENABLED #include "editor/bindings_generator.h" #include "editor/csharp_project.h" #include "editor/editor_node.h" #include "editor/node_dock.h" #endif #ifdef DEBUG_METHODS_ENABLED #include "class_db_api_json.h" #endif #include "editor/editor_internal_calls.h" #include "godotsharp_dirs.h" #include "mono_gd/gd_mono_cache.h" #include "mono_gd/gd_mono_class.h" #include "mono_gd/gd_mono_marshal.h" #include "mono_gd/gd_mono_utils.h" #include "signal_awaiter_utils.h" #include "utils/macros.h" #include "utils/mutex_utils.h" #include "utils/string_utils.h" #include "utils/thread_local.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() == NULL); return CSharpLanguage::get_singleton()->get_godotsharp_editor()->call("CreateProjectSolution"); } return true; } #endif CSharpLanguage *CSharpLanguage::singleton = NULL; 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; } 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 gdmono = memnew(GDMono); gdmono->initialize(); #if defined(TOOLS_ENABLED) && defined(DEBUG_METHODS_ENABLED) // Generate bindings here, before loading assemblies. 'initialize_load_assemblies' aborts // the applications if the api assemblies or the main tools assembly is missing, but this // is not a problem for BindingsGenerator as it only needs the tools project editor assembly. List cmdline_args = OS::get_singleton()->get_cmdline_args(); BindingsGenerator::handle_cmdline_args(cmdline_args); #endif #ifndef MONO_GLUE_ENABLED print_line("Run this binary with '--generate-mono-glue path/to/modules/mono/glue'"); #endif if (gdmono->is_runtime_initialized()) gdmono->initialize_load_assemblies(); #ifdef TOOLS_ENABLED EditorNode::add_init_callback(&_editor_init_callback); #endif } void CSharpLanguage::finish() { finalizing = true; // Make sure all script binding gchandles are released before finalizing GDMono for (Map::Element *E = script_bindings.front(); E; E = E->next()) { CSharpScriptBinding &script_binding = E->value(); if (script_binding.gchandle.is_valid()) { script_binding.gchandle->release(); script_binding.inited = false; } } if (gdmono) { memdelete(gdmono); gdmono = NULL; } // Clear here, after finalizing all domains to make sure there is nothing else referencing the elements. script_bindings.clear(); #ifdef DEBUG_ENABLED for (Map::Element *E = unsafe_object_references.front(); E; E = E->next()) { const ObjectID &id = E->get(); Object *obj = ObjectDB::get_instance(id); if (obj) { ERR_PRINT("Leaked unsafe reference to object: " + obj->get_class() + ":" + itos(id)); } else { ERR_PRINT("Leaked unsafe reference to deleted object: " + itos(id)); } } #endif finalizing = false; } 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", 0 }; const char **w = _reserved_words; while (*w) { p_words->push_back(*w); w++; } } 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 } 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; } Ref