register_types.cpp 8.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228
  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. #include "register_types.h"
  31. #include "gdscript.h"
  32. #include "gdscript_analyzer.h"
  33. #include "gdscript_cache.h"
  34. #include "gdscript_tokenizer.h"
  35. #include "gdscript_tokenizer_buffer.h"
  36. #include "gdscript_utility_functions.h"
  37. #ifdef TOOLS_ENABLED
  38. #include "editor/gdscript_highlighter.h"
  39. #include "editor/gdscript_translation_parser_plugin.h"
  40. #ifndef GDSCRIPT_NO_LSP
  41. #include "language_server/gdscript_language_server.h"
  42. #endif
  43. #endif // TOOLS_ENABLED
  44. #ifdef TESTS_ENABLED
  45. #include "tests/test_gdscript.h"
  46. #endif
  47. #include "core/io/dir_access.h"
  48. #include "core/io/file_access.h"
  49. #include "core/io/file_access_encrypted.h"
  50. #include "core/io/resource_loader.h"
  51. #ifdef TOOLS_ENABLED
  52. #include "editor/editor_node.h"
  53. #include "editor/editor_settings.h"
  54. #include "editor/editor_translation_parser.h"
  55. #include "editor/export/editor_export.h"
  56. #ifndef GDSCRIPT_NO_LSP
  57. #include "core/config/engine.h"
  58. #endif
  59. #endif // TOOLS_ENABLED
  60. #ifdef TESTS_ENABLED
  61. #include "tests/test_macros.h"
  62. #endif
  63. GDScriptLanguage *script_language_gd = nullptr;
  64. Ref<ResourceFormatLoaderGDScript> resource_loader_gd;
  65. Ref<ResourceFormatSaverGDScript> resource_saver_gd;
  66. GDScriptCache *gdscript_cache = nullptr;
  67. #ifdef TOOLS_ENABLED
  68. Ref<GDScriptEditorTranslationParserPlugin> gdscript_translation_parser_plugin;
  69. class EditorExportGDScript : public EditorExportPlugin {
  70. GDCLASS(EditorExportGDScript, EditorExportPlugin);
  71. static constexpr int DEFAULT_SCRIPT_MODE = EditorExportPreset::MODE_SCRIPT_BINARY_TOKENS_COMPRESSED;
  72. int script_mode = DEFAULT_SCRIPT_MODE;
  73. protected:
  74. virtual void _export_begin(const HashSet<String> &p_features, bool p_debug, const String &p_path, int p_flags) override {
  75. script_mode = DEFAULT_SCRIPT_MODE;
  76. const Ref<EditorExportPreset> &preset = get_export_preset();
  77. if (preset.is_valid()) {
  78. script_mode = preset->get_script_export_mode();
  79. }
  80. }
  81. virtual void _export_file(const String &p_path, const String &p_type, const HashSet<String> &p_features) override {
  82. if (p_path.get_extension() != "gd" || script_mode == EditorExportPreset::MODE_SCRIPT_TEXT) {
  83. return;
  84. }
  85. Vector<uint8_t> file = FileAccess::get_file_as_bytes(p_path);
  86. if (file.is_empty()) {
  87. return;
  88. }
  89. String source;
  90. source.parse_utf8(reinterpret_cast<const char *>(file.ptr()), file.size());
  91. GDScriptTokenizerBuffer::CompressMode compress_mode = script_mode == EditorExportPreset::MODE_SCRIPT_BINARY_TOKENS_COMPRESSED ? GDScriptTokenizerBuffer::COMPRESS_ZSTD : GDScriptTokenizerBuffer::COMPRESS_NONE;
  92. file = GDScriptTokenizerBuffer::parse_code_string(source, compress_mode);
  93. if (file.is_empty()) {
  94. return;
  95. }
  96. add_file(p_path.get_basename() + ".gdc", file, true);
  97. }
  98. public:
  99. virtual String get_name() const override { return "GDScript"; }
  100. };
  101. static void _editor_init() {
  102. Ref<EditorExportGDScript> gd_export;
  103. gd_export.instantiate();
  104. EditorExport::get_singleton()->add_export_plugin(gd_export);
  105. #ifdef TOOLS_ENABLED
  106. Ref<GDScriptSyntaxHighlighter> gdscript_syntax_highlighter;
  107. gdscript_syntax_highlighter.instantiate();
  108. ScriptEditor::get_singleton()->register_syntax_highlighter(gdscript_syntax_highlighter);
  109. #endif
  110. #ifndef GDSCRIPT_NO_LSP
  111. register_lsp_types();
  112. GDScriptLanguageServer *lsp_plugin = memnew(GDScriptLanguageServer);
  113. EditorNode::get_singleton()->add_editor_plugin(lsp_plugin);
  114. Engine::get_singleton()->add_singleton(Engine::Singleton("GDScriptLanguageProtocol", GDScriptLanguageProtocol::get_singleton()));
  115. #endif // !GDSCRIPT_NO_LSP
  116. }
  117. #endif // TOOLS_ENABLED
  118. void initialize_gdscript_module(ModuleInitializationLevel p_level) {
  119. if (p_level == MODULE_INITIALIZATION_LEVEL_SERVERS) {
  120. GDREGISTER_CLASS(GDScript);
  121. script_language_gd = memnew(GDScriptLanguage);
  122. ScriptServer::register_language(script_language_gd);
  123. resource_loader_gd.instantiate();
  124. ResourceLoader::add_resource_format_loader(resource_loader_gd);
  125. resource_saver_gd.instantiate();
  126. ResourceSaver::add_resource_format_saver(resource_saver_gd);
  127. gdscript_cache = memnew(GDScriptCache);
  128. GDScriptUtilityFunctions::register_functions();
  129. }
  130. #ifdef TOOLS_ENABLED
  131. if (p_level == MODULE_INITIALIZATION_LEVEL_SERVERS) {
  132. EditorNode::add_init_callback(_editor_init);
  133. gdscript_translation_parser_plugin.instantiate();
  134. EditorTranslationParser::get_singleton()->add_parser(gdscript_translation_parser_plugin, EditorTranslationParser::STANDARD);
  135. }
  136. #endif // TOOLS_ENABLED
  137. }
  138. void uninitialize_gdscript_module(ModuleInitializationLevel p_level) {
  139. if (p_level == MODULE_INITIALIZATION_LEVEL_SERVERS) {
  140. ScriptServer::unregister_language(script_language_gd);
  141. if (gdscript_cache) {
  142. memdelete(gdscript_cache);
  143. }
  144. if (script_language_gd) {
  145. memdelete(script_language_gd);
  146. }
  147. ResourceLoader::remove_resource_format_loader(resource_loader_gd);
  148. resource_loader_gd.unref();
  149. ResourceSaver::remove_resource_format_saver(resource_saver_gd);
  150. resource_saver_gd.unref();
  151. GDScriptParser::cleanup();
  152. GDScriptUtilityFunctions::unregister_functions();
  153. }
  154. #ifdef TOOLS_ENABLED
  155. if (p_level == MODULE_INITIALIZATION_LEVEL_EDITOR) {
  156. EditorTranslationParser::get_singleton()->remove_parser(gdscript_translation_parser_plugin, EditorTranslationParser::STANDARD);
  157. gdscript_translation_parser_plugin.unref();
  158. }
  159. #endif // TOOLS_ENABLED
  160. }
  161. #ifdef TESTS_ENABLED
  162. void test_tokenizer() {
  163. GDScriptTests::test(GDScriptTests::TestType::TEST_TOKENIZER);
  164. }
  165. void test_tokenizer_buffer() {
  166. GDScriptTests::test(GDScriptTests::TestType::TEST_TOKENIZER_BUFFER);
  167. }
  168. void test_parser() {
  169. GDScriptTests::test(GDScriptTests::TestType::TEST_PARSER);
  170. }
  171. void test_compiler() {
  172. GDScriptTests::test(GDScriptTests::TestType::TEST_COMPILER);
  173. }
  174. void test_bytecode() {
  175. GDScriptTests::test(GDScriptTests::TestType::TEST_BYTECODE);
  176. }
  177. REGISTER_TEST_COMMAND("gdscript-tokenizer", &test_tokenizer);
  178. REGISTER_TEST_COMMAND("gdscript-tokenizer-buffer", &test_tokenizer_buffer);
  179. REGISTER_TEST_COMMAND("gdscript-parser", &test_parser);
  180. REGISTER_TEST_COMMAND("gdscript-compiler", &test_compiler);
  181. REGISTER_TEST_COMMAND("gdscript-bytecode", &test_bytecode);
  182. #endif