register_types.cpp 8.1 KB

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