register_types.cpp 8.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231
  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/editor_translation_parser.h"
  51. #include "editor/export/editor_export.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 int DEFAULT_SCRIPT_MODE = EditorExportPreset::MODE_SCRIPT_BINARY_TOKENS_COMPRESSED;
  68. int 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;
  86. source.parse_utf8(reinterpret_cast<const char *>(file.ptr()), file.size());
  87. GDScriptTokenizerBuffer::CompressMode compress_mode = script_mode == EditorExportPreset::MODE_SCRIPT_BINARY_TOKENS_COMPRESSED ? GDScriptTokenizerBuffer::COMPRESS_ZSTD : GDScriptTokenizerBuffer::COMPRESS_NONE;
  88. file = GDScriptTokenizerBuffer::parse_code_string(source, compress_mode);
  89. if (file.is_empty()) {
  90. return;
  91. }
  92. add_file(p_path.get_basename() + ".gdc", file, true);
  93. }
  94. public:
  95. virtual String get_name() const override { return "GDScript"; }
  96. };
  97. static void _editor_init() {
  98. Ref<EditorExportGDScript> gd_export;
  99. gd_export.instantiate();
  100. EditorExport::get_singleton()->add_export_plugin(gd_export);
  101. #ifdef TOOLS_ENABLED
  102. Ref<GDScriptSyntaxHighlighter> gdscript_syntax_highlighter;
  103. gdscript_syntax_highlighter.instantiate();
  104. ScriptEditor::get_singleton()->register_syntax_highlighter(gdscript_syntax_highlighter);
  105. #endif
  106. #ifndef GDSCRIPT_NO_LSP
  107. register_lsp_types();
  108. GDScriptLanguageServer *lsp_plugin = memnew(GDScriptLanguageServer);
  109. EditorNode::get_singleton()->add_editor_plugin(lsp_plugin);
  110. Engine::get_singleton()->add_singleton(Engine::Singleton("GDScriptLanguageProtocol", GDScriptLanguageProtocol::get_singleton()));
  111. #endif // !GDSCRIPT_NO_LSP
  112. }
  113. #endif // TOOLS_ENABLED
  114. void initialize_gdscript_module(ModuleInitializationLevel p_level) {
  115. if (p_level == MODULE_INITIALIZATION_LEVEL_SERVERS) {
  116. GDREGISTER_CLASS(GDScript);
  117. script_language_gd = memnew(GDScriptLanguage);
  118. ScriptServer::register_language(script_language_gd);
  119. resource_loader_gd.instantiate();
  120. ResourceLoader::add_resource_format_loader(resource_loader_gd);
  121. resource_saver_gd.instantiate();
  122. ResourceSaver::add_resource_format_saver(resource_saver_gd);
  123. gdscript_cache = memnew(GDScriptCache);
  124. GDScriptUtilityFunctions::register_functions();
  125. }
  126. #ifdef TOOLS_ENABLED
  127. if (p_level == MODULE_INITIALIZATION_LEVEL_SERVERS) {
  128. EditorNode::add_init_callback(_editor_init);
  129. gdscript_translation_parser_plugin.instantiate();
  130. EditorTranslationParser::get_singleton()->add_parser(gdscript_translation_parser_plugin, EditorTranslationParser::STANDARD);
  131. } else if (p_level == MODULE_INITIALIZATION_LEVEL_EDITOR) {
  132. ClassDB::APIType prev_api = ClassDB::get_current_api();
  133. ClassDB::set_current_api(ClassDB::API_EDITOR);
  134. GDREGISTER_CLASS(GDScriptSyntaxHighlighter);
  135. ClassDB::set_current_api(prev_api);
  136. }
  137. #endif // TOOLS_ENABLED
  138. }
  139. void uninitialize_gdscript_module(ModuleInitializationLevel p_level) {
  140. if (p_level == MODULE_INITIALIZATION_LEVEL_SERVERS) {
  141. ScriptServer::unregister_language(script_language_gd);
  142. if (gdscript_cache) {
  143. memdelete(gdscript_cache);
  144. }
  145. if (script_language_gd) {
  146. memdelete(script_language_gd);
  147. }
  148. ResourceLoader::remove_resource_format_loader(resource_loader_gd);
  149. resource_loader_gd.unref();
  150. ResourceSaver::remove_resource_format_saver(resource_saver_gd);
  151. resource_saver_gd.unref();
  152. GDScriptParser::cleanup();
  153. GDScriptUtilityFunctions::unregister_functions();
  154. }
  155. #ifdef TOOLS_ENABLED
  156. if (p_level == MODULE_INITIALIZATION_LEVEL_EDITOR) {
  157. EditorTranslationParser::get_singleton()->remove_parser(gdscript_translation_parser_plugin, EditorTranslationParser::STANDARD);
  158. gdscript_translation_parser_plugin.unref();
  159. }
  160. #endif // TOOLS_ENABLED
  161. }
  162. #ifdef TESTS_ENABLED
  163. void test_tokenizer() {
  164. GDScriptTests::test(GDScriptTests::TestType::TEST_TOKENIZER);
  165. }
  166. void test_tokenizer_buffer() {
  167. GDScriptTests::test(GDScriptTests::TestType::TEST_TOKENIZER_BUFFER);
  168. }
  169. void test_parser() {
  170. GDScriptTests::test(GDScriptTests::TestType::TEST_PARSER);
  171. }
  172. void test_compiler() {
  173. GDScriptTests::test(GDScriptTests::TestType::TEST_COMPILER);
  174. }
  175. void test_bytecode() {
  176. GDScriptTests::test(GDScriptTests::TestType::TEST_BYTECODE);
  177. }
  178. REGISTER_TEST_COMMAND("gdscript-tokenizer", &test_tokenizer);
  179. REGISTER_TEST_COMMAND("gdscript-tokenizer-buffer", &test_tokenizer_buffer);
  180. REGISTER_TEST_COMMAND("gdscript-parser", &test_parser);
  181. REGISTER_TEST_COMMAND("gdscript-compiler", &test_compiler);
  182. REGISTER_TEST_COMMAND("gdscript-bytecode", &test_bytecode);
  183. #endif