| 1 | /**************************************************************************/ |
| 2 | /* register_types.cpp */ |
| 3 | /**************************************************************************/ |
| 4 | /* This file is part of: */ |
| 5 | /* GODOT ENGINE */ |
| 6 | /* https://godotengine.org */ |
| 7 | /**************************************************************************/ |
| 8 | /* Copyright (c) 2014-present Godot Engine contributors (see AUTHORS.md). */ |
| 9 | /* Copyright (c) 2007-2014 Juan Linietsky, Ariel Manzur. */ |
| 10 | /* */ |
| 11 | /* Permission is hereby granted, free of charge, to any person obtaining */ |
| 12 | /* a copy of this software and associated documentation files (the */ |
| 13 | /* "Software"), to deal in the Software without restriction, including */ |
| 14 | /* without limitation the rights to use, copy, modify, merge, publish, */ |
| 15 | /* distribute, sublicense, and/or sell copies of the Software, and to */ |
| 16 | /* permit persons to whom the Software is furnished to do so, subject to */ |
| 17 | /* the following conditions: */ |
| 18 | /* */ |
| 19 | /* The above copyright notice and this permission notice shall be */ |
| 20 | /* included in all copies or substantial portions of the Software. */ |
| 21 | /* */ |
| 22 | /* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, */ |
| 23 | /* EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF */ |
| 24 | /* MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. */ |
| 25 | /* IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY */ |
| 26 | /* CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, */ |
| 27 | /* TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE */ |
| 28 | /* SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. */ |
| 29 | /**************************************************************************/ |
| 30 | |
| 31 | #include "register_types.h" |
| 32 | |
| 33 | #include "gdscript.h" |
| 34 | #include "gdscript_analyzer.h" |
| 35 | #include "gdscript_cache.h" |
| 36 | #include "gdscript_tokenizer.h" |
| 37 | #include "gdscript_utility_functions.h" |
| 38 | |
| 39 | #ifdef TOOLS_ENABLED |
| 40 | #include "editor/gdscript_highlighter.h" |
| 41 | #include "editor/gdscript_translation_parser_plugin.h" |
| 42 | |
| 43 | #ifndef GDSCRIPT_NO_LSP |
| 44 | #include "language_server/gdscript_language_server.h" |
| 45 | #endif |
| 46 | #endif // TOOLS_ENABLED |
| 47 | |
| 48 | #ifdef TESTS_ENABLED |
| 49 | #include "tests/test_gdscript.h" |
| 50 | #endif |
| 51 | |
| 52 | #include "core/io/dir_access.h" |
| 53 | #include "core/io/file_access.h" |
| 54 | #include "core/io/file_access_encrypted.h" |
| 55 | #include "core/io/resource_loader.h" |
| 56 | |
| 57 | #ifdef TOOLS_ENABLED |
| 58 | #include "editor/editor_node.h" |
| 59 | #include "editor/editor_settings.h" |
| 60 | #include "editor/editor_translation_parser.h" |
| 61 | #include "editor/export/editor_export.h" |
| 62 | |
| 63 | #ifndef GDSCRIPT_NO_LSP |
| 64 | #include "core/config/engine.h" |
| 65 | #endif |
| 66 | #endif // TOOLS_ENABLED |
| 67 | |
| 68 | #ifdef TESTS_ENABLED |
| 69 | #include "tests/test_macros.h" |
| 70 | #endif |
| 71 | |
| 72 | GDScriptLanguage *script_language_gd = nullptr; |
| 73 | Ref<ResourceFormatLoaderGDScript> resource_loader_gd; |
| 74 | Ref<ResourceFormatSaverGDScript> resource_saver_gd; |
| 75 | GDScriptCache *gdscript_cache = nullptr; |
| 76 | |
| 77 | #ifdef TOOLS_ENABLED |
| 78 | |
| 79 | Ref<GDScriptEditorTranslationParserPlugin> gdscript_translation_parser_plugin; |
| 80 | |
| 81 | class EditorExportGDScript : public EditorExportPlugin { |
| 82 | GDCLASS(EditorExportGDScript, EditorExportPlugin); |
| 83 | |
| 84 | public: |
| 85 | virtual void _export_file(const String &p_path, const String &p_type, const HashSet<String> &p_features) override { |
| 86 | String script_key; |
| 87 | |
| 88 | const Ref<EditorExportPreset> &preset = get_export_preset(); |
| 89 | |
| 90 | if (preset.is_valid()) { |
| 91 | script_key = preset->get_script_encryption_key().to_lower(); |
| 92 | } |
| 93 | |
| 94 | if (!p_path.ends_with(".gd" )) { |
| 95 | return; |
| 96 | } |
| 97 | |
| 98 | return; |
| 99 | } |
| 100 | |
| 101 | virtual String get_name() const override { return "GDScript" ; } |
| 102 | }; |
| 103 | |
| 104 | static void _editor_init() { |
| 105 | Ref<EditorExportGDScript> gd_export; |
| 106 | gd_export.instantiate(); |
| 107 | EditorExport::get_singleton()->add_export_plugin(gd_export); |
| 108 | |
| 109 | #ifdef TOOLS_ENABLED |
| 110 | Ref<GDScriptSyntaxHighlighter> gdscript_syntax_highlighter; |
| 111 | gdscript_syntax_highlighter.instantiate(); |
| 112 | ScriptEditor::get_singleton()->register_syntax_highlighter(gdscript_syntax_highlighter); |
| 113 | #endif |
| 114 | |
| 115 | #ifndef GDSCRIPT_NO_LSP |
| 116 | register_lsp_types(); |
| 117 | GDScriptLanguageServer *lsp_plugin = memnew(GDScriptLanguageServer); |
| 118 | EditorNode::get_singleton()->add_editor_plugin(lsp_plugin); |
| 119 | Engine::get_singleton()->add_singleton(Engine::Singleton("GDScriptLanguageProtocol" , GDScriptLanguageProtocol::get_singleton())); |
| 120 | #endif // !GDSCRIPT_NO_LSP |
| 121 | } |
| 122 | |
| 123 | #endif // TOOLS_ENABLED |
| 124 | |
| 125 | void initialize_gdscript_module(ModuleInitializationLevel p_level) { |
| 126 | if (p_level == MODULE_INITIALIZATION_LEVEL_SERVERS) { |
| 127 | GDREGISTER_CLASS(GDScript); |
| 128 | |
| 129 | script_language_gd = memnew(GDScriptLanguage); |
| 130 | ScriptServer::register_language(script_language_gd); |
| 131 | |
| 132 | resource_loader_gd.instantiate(); |
| 133 | ResourceLoader::add_resource_format_loader(resource_loader_gd); |
| 134 | |
| 135 | resource_saver_gd.instantiate(); |
| 136 | ResourceSaver::add_resource_format_saver(resource_saver_gd); |
| 137 | |
| 138 | gdscript_cache = memnew(GDScriptCache); |
| 139 | |
| 140 | GDScriptUtilityFunctions::register_functions(); |
| 141 | } |
| 142 | |
| 143 | #ifdef TOOLS_ENABLED |
| 144 | if (p_level == MODULE_INITIALIZATION_LEVEL_SERVERS) { |
| 145 | EditorNode::add_init_callback(_editor_init); |
| 146 | |
| 147 | gdscript_translation_parser_plugin.instantiate(); |
| 148 | EditorTranslationParser::get_singleton()->add_parser(gdscript_translation_parser_plugin, EditorTranslationParser::STANDARD); |
| 149 | } |
| 150 | #endif // TOOLS_ENABLED |
| 151 | } |
| 152 | |
| 153 | void uninitialize_gdscript_module(ModuleInitializationLevel p_level) { |
| 154 | if (p_level == MODULE_INITIALIZATION_LEVEL_SERVERS) { |
| 155 | ScriptServer::unregister_language(script_language_gd); |
| 156 | |
| 157 | if (gdscript_cache) { |
| 158 | memdelete(gdscript_cache); |
| 159 | } |
| 160 | |
| 161 | if (script_language_gd) { |
| 162 | memdelete(script_language_gd); |
| 163 | } |
| 164 | |
| 165 | ResourceLoader::remove_resource_format_loader(resource_loader_gd); |
| 166 | resource_loader_gd.unref(); |
| 167 | |
| 168 | ResourceSaver::remove_resource_format_saver(resource_saver_gd); |
| 169 | resource_saver_gd.unref(); |
| 170 | |
| 171 | GDScriptParser::cleanup(); |
| 172 | GDScriptUtilityFunctions::unregister_functions(); |
| 173 | } |
| 174 | |
| 175 | #ifdef TOOLS_ENABLED |
| 176 | if (p_level == MODULE_INITIALIZATION_LEVEL_EDITOR) { |
| 177 | EditorTranslationParser::get_singleton()->remove_parser(gdscript_translation_parser_plugin, EditorTranslationParser::STANDARD); |
| 178 | gdscript_translation_parser_plugin.unref(); |
| 179 | } |
| 180 | #endif // TOOLS_ENABLED |
| 181 | } |
| 182 | |
| 183 | #ifdef TESTS_ENABLED |
| 184 | void test_tokenizer() { |
| 185 | GDScriptTests::test(GDScriptTests::TestType::TEST_TOKENIZER); |
| 186 | } |
| 187 | |
| 188 | void test_parser() { |
| 189 | GDScriptTests::test(GDScriptTests::TestType::TEST_PARSER); |
| 190 | } |
| 191 | |
| 192 | void test_compiler() { |
| 193 | GDScriptTests::test(GDScriptTests::TestType::TEST_COMPILER); |
| 194 | } |
| 195 | |
| 196 | void test_bytecode() { |
| 197 | GDScriptTests::test(GDScriptTests::TestType::TEST_BYTECODE); |
| 198 | } |
| 199 | |
| 200 | REGISTER_TEST_COMMAND("gdscript-tokenizer" , &test_tokenizer); |
| 201 | REGISTER_TEST_COMMAND("gdscript-parser" , &test_parser); |
| 202 | REGISTER_TEST_COMMAND("gdscript-compiler" , &test_compiler); |
| 203 | REGISTER_TEST_COMMAND("gdscript-bytecode" , &test_bytecode); |
| 204 | #endif |
| 205 | |