Debian Codegraph @

modules/gdscript/register_types.cpp

raw symbols SCIP
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_cache.h"
35 #include "gdscript_parser.h"
36 #include "gdscript_tokenizer_buffer.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/file_access.h"
53 #include "core/io/resource_loader.h"
54
55 #ifdef TOOLS_ENABLED
56 #include "editor/editor_node.h"
57 #include "editor/export/editor_export.h"
58 #include "editor/translations/editor_translation_parser.h"
59
60 #ifndef GDSCRIPT_NO_LSP
61 #include "core/config/engine.h"
62 #endif
63 #endif // TOOLS_ENABLED
64
65 #ifdef TESTS_ENABLED
66 #include "tests/test_macros.h"
67 #endif
68
69 GDScriptLanguage *script_language_gd = nullptr;
70 Ref<ResourceFormatLoaderGDScript> resource_loader_gd;
71 Ref<ResourceFormatSaverGDScript> resource_saver_gd;
72 GDScriptCache *gdscript_cache = nullptr;
73
74 #ifdef TOOLS_ENABLED
75
76 Ref<GDScriptEditorTranslationParserPlugin> gdscript_translation_parser_plugin;
77
78 class EditorExportGDScript : public EditorExportPlugin {
79 GDCLASS(EditorExportGDScript, EditorExportPlugin);
80
81 static constexpr EditorExportPreset::ScriptExportMode DEFAULT_SCRIPT_MODE = EditorExportPreset::MODE_SCRIPT_BINARY_TOKENS_COMPRESSED;
82 EditorExportPreset::ScriptExportMode script_mode = DEFAULT_SCRIPT_MODE;
83
84 protected:
85 virtual void _export_begin(const HashSet<String> &p_features, bool p_debug, const String &p_path, int p_flags) override {
86 script_mode = DEFAULT_SCRIPT_MODE;
87
88 const Ref<EditorExportPreset> &preset = get_export_preset();
89 if (preset.is_valid()) {
90 script_mode = preset->get_script_export_mode();
91 }
92 }
93
94 virtual void _export_file(const String &p_path, const String &p_type, const HashSet<String> &p_features) override {
95 if (p_path.get_extension() != "gd" || script_mode == EditorExportPreset::MODE_SCRIPT_TEXT) {
96 return;
97 }
98
99 Vector<uint8_t> file = FileAccess::get_file_as_bytes(p_path);
100 if (file.is_empty()) {
101 return;
102 }
103
104 String source = String::utf8(reinterpret_cast<const char *>(file.ptr()), file.size());
105 GDScriptTokenizerBuffer::CompressMode compress_mode = script_mode == EditorExportPreset::MODE_SCRIPT_BINARY_TOKENS_COMPRESSED ? GDScriptTokenizerBuffer::COMPRESS_ZSTD : GDScriptTokenizerBuffer::COMPRESS_NONE;
106 file = GDScriptTokenizerBuffer::parse_code_string(source, compress_mode);
107 if (file.is_empty()) {
108 return;
109 }
110
111 add_file(p_path.get_basename() + ".gdc", file, true);
112 }
113
114 public:
115 virtual String get_name() const override { return "GDScript"; }
116 };
117
118 static void _editor_init() {
119 Ref<EditorExportGDScript> gd_export;
120 gd_export.instantiate();
121 EditorExport::get_singleton()->add_export_plugin(gd_export);
122
123 #ifdef TOOLS_ENABLED
124 Ref<GDScriptSyntaxHighlighter> gdscript_syntax_highlighter;
125 gdscript_syntax_highlighter.instantiate();
126 ScriptEditor::get_singleton()->register_syntax_highlighter(gdscript_syntax_highlighter);
127 #endif
128
129 #ifndef GDSCRIPT_NO_LSP
130 register_lsp_types();
131 GDScriptLanguageServer *lsp_plugin = memnew(GDScriptLanguageServer);
132 EditorNode::get_singleton()->add_editor_plugin(lsp_plugin);
133 Engine::get_singleton()->add_singleton(Engine::Singleton("GDScriptLanguageProtocol", GDScriptLanguageProtocol::get_singleton()));
134 #endif // !GDSCRIPT_NO_LSP
135 }
136
137 #endif // TOOLS_ENABLED
138
139 void initialize_gdscript_module(ModuleInitializationLevel p_level) {
140 if (p_level == MODULE_INITIALIZATION_LEVEL_SERVERS) {
141 GDREGISTER_CLASS(GDScript);
142
143 script_language_gd = memnew(GDScriptLanguage);
144 ScriptServer::register_language(script_language_gd);
145
146 resource_loader_gd.instantiate();
147 ResourceLoader::add_resource_format_loader(resource_loader_gd);
148
149 resource_saver_gd.instantiate();
150 ResourceSaver::add_resource_format_saver(resource_saver_gd);
151
152 gdscript_cache = memnew(GDScriptCache);
153
154 GDScriptUtilityFunctions::register_functions();
155 }
156
157 #ifdef TOOLS_ENABLED
158 if (p_level == MODULE_INITIALIZATION_LEVEL_SERVERS) {
159 EditorNode::add_init_callback(_editor_init);
160
161 gdscript_translation_parser_plugin.instantiate();
162 EditorTranslationParser::get_singleton()->add_parser(gdscript_translation_parser_plugin, EditorTranslationParser::STANDARD);
163 } else if (p_level == MODULE_INITIALIZATION_LEVEL_EDITOR) {
164 GDREGISTER_CLASS(GDScriptSyntaxHighlighter);
165 }
166 #endif // TOOLS_ENABLED
167 }
168
169 void uninitialize_gdscript_module(ModuleInitializationLevel p_level) {
170 if (p_level == MODULE_INITIALIZATION_LEVEL_SERVERS) {
171 ScriptServer::unregister_language(script_language_gd);
172
173 if (gdscript_cache) {
174 memdelete(gdscript_cache);
175 }
176
177 if (script_language_gd) {
178 memdelete(script_language_gd);
179 }
180
181 ResourceLoader::remove_resource_format_loader(resource_loader_gd);
182 resource_loader_gd.unref();
183
184 ResourceSaver::remove_resource_format_saver(resource_saver_gd);
185 resource_saver_gd.unref();
186
187 GDScriptParser::cleanup();
188 GDScriptUtilityFunctions::unregister_functions();
189 }
190
191 #ifdef TOOLS_ENABLED
192 if (p_level == MODULE_INITIALIZATION_LEVEL_EDITOR) {
193 EditorTranslationParser::get_singleton()->remove_parser(gdscript_translation_parser_plugin, EditorTranslationParser::STANDARD);
194 gdscript_translation_parser_plugin.unref();
195 }
196 #endif // TOOLS_ENABLED
197 }
198
199 #ifdef TESTS_ENABLED
200 void test_tokenizer() {
201 GDScriptTests::test(GDScriptTests::TestType::TEST_TOKENIZER);
202 }
203
204 void test_tokenizer_buffer() {
205 GDScriptTests::test(GDScriptTests::TestType::TEST_TOKENIZER_BUFFER);
206 }
207
208 void test_parser() {
209 GDScriptTests::test(GDScriptTests::TestType::TEST_PARSER);
210 }
211
212 void test_compiler() {
213 GDScriptTests::test(GDScriptTests::TestType::TEST_COMPILER);
214 }
215
216 void test_bytecode() {
217 GDScriptTests::test(GDScriptTests::TestType::TEST_BYTECODE);
218 }
219
220 REGISTER_TEST_COMMAND("gdscript-tokenizer", &test_tokenizer);
221 REGISTER_TEST_COMMAND("gdscript-tokenizer-buffer", &test_tokenizer_buffer);
222 REGISTER_TEST_COMMAND("gdscript-parser", &test_parser);
223 REGISTER_TEST_COMMAND("gdscript-compiler", &test_compiler);
224 REGISTER_TEST_COMMAND("gdscript-bytecode", &test_bytecode);
225 #endif