register_types.cpp 7.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224
  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. public:
  72. virtual void _export_file(const String &p_path, const String &p_type, const HashSet<String> &p_features) override {
  73. int script_mode = EditorExportPreset::MODE_SCRIPT_BINARY_TOKENS;
  74. const Ref<EditorExportPreset> &preset = get_export_preset();
  75. if (preset.is_valid()) {
  76. script_mode = preset->get_script_export_mode();
  77. }
  78. if (!p_path.ends_with(".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. file = GDScriptTokenizerBuffer::parse_code_string(source);
  88. if (file.is_empty()) {
  89. return;
  90. }
  91. add_file(p_path.get_basename() + ".gdc", file, true);
  92. return;
  93. }
  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. }
  131. #endif // TOOLS_ENABLED
  132. }
  133. void uninitialize_gdscript_module(ModuleInitializationLevel p_level) {
  134. if (p_level == MODULE_INITIALIZATION_LEVEL_SERVERS) {
  135. ScriptServer::unregister_language(script_language_gd);
  136. if (gdscript_cache) {
  137. memdelete(gdscript_cache);
  138. }
  139. if (script_language_gd) {
  140. memdelete(script_language_gd);
  141. }
  142. ResourceLoader::remove_resource_format_loader(resource_loader_gd);
  143. resource_loader_gd.unref();
  144. ResourceSaver::remove_resource_format_saver(resource_saver_gd);
  145. resource_saver_gd.unref();
  146. GDScriptParser::cleanup();
  147. GDScriptUtilityFunctions::unregister_functions();
  148. }
  149. #ifdef TOOLS_ENABLED
  150. if (p_level == MODULE_INITIALIZATION_LEVEL_EDITOR) {
  151. EditorTranslationParser::get_singleton()->remove_parser(gdscript_translation_parser_plugin, EditorTranslationParser::STANDARD);
  152. gdscript_translation_parser_plugin.unref();
  153. }
  154. #endif // TOOLS_ENABLED
  155. }
  156. #ifdef TESTS_ENABLED
  157. void test_tokenizer() {
  158. GDScriptTests::test(GDScriptTests::TestType::TEST_TOKENIZER);
  159. }
  160. void test_tokenizer_buffer() {
  161. GDScriptTests::test(GDScriptTests::TestType::TEST_TOKENIZER_BUFFER);
  162. }
  163. void test_parser() {
  164. GDScriptTests::test(GDScriptTests::TestType::TEST_PARSER);
  165. }
  166. void test_compiler() {
  167. GDScriptTests::test(GDScriptTests::TestType::TEST_COMPILER);
  168. }
  169. void test_bytecode() {
  170. GDScriptTests::test(GDScriptTests::TestType::TEST_BYTECODE);
  171. }
  172. REGISTER_TEST_COMMAND("gdscript-tokenizer", &test_tokenizer);
  173. REGISTER_TEST_COMMAND("gdscript-tokenizer-buffer", &test_tokenizer_buffer);
  174. REGISTER_TEST_COMMAND("gdscript-parser", &test_parser);
  175. REGISTER_TEST_COMMAND("gdscript-compiler", &test_compiler);
  176. REGISTER_TEST_COMMAND("gdscript-bytecode", &test_bytecode);
  177. #endif