register_types.cpp 7.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195
  1. /*************************************************************************/
  2. /* register_types.cpp */
  3. /*************************************************************************/
  4. /* This file is part of: */
  5. /* GODOT ENGINE */
  6. /* https://godotengine.org */
  7. /*************************************************************************/
  8. /* Copyright (c) 2007-2022 Juan Linietsky, Ariel Manzur. */
  9. /* Copyright (c) 2014-2022 Godot Engine contributors (cf. AUTHORS.md). */
  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 "core/io/dir_access.h"
  32. #include "core/io/file_access.h"
  33. #include "core/io/file_access_encrypted.h"
  34. #include "core/io/resource_loader.h"
  35. #include "gdscript.h"
  36. #include "gdscript_analyzer.h"
  37. #include "gdscript_cache.h"
  38. #include "gdscript_tokenizer.h"
  39. #include "gdscript_utility_functions.h"
  40. #ifdef TESTS_ENABLED
  41. #include "tests/test_gdscript.h"
  42. #include "tests/test_macros.h"
  43. #endif
  44. GDScriptLanguage *script_language_gd = nullptr;
  45. Ref<ResourceFormatLoaderGDScript> resource_loader_gd;
  46. Ref<ResourceFormatSaverGDScript> resource_saver_gd;
  47. GDScriptCache *gdscript_cache = nullptr;
  48. #ifdef TOOLS_ENABLED
  49. #include "editor/editor_node.h"
  50. #include "editor/editor_settings.h"
  51. #include "editor/editor_translation_parser.h"
  52. #include "editor/export/editor_export.h"
  53. #include "editor/gdscript_highlighter.h"
  54. #include "editor/gdscript_translation_parser_plugin.h"
  55. #ifndef GDSCRIPT_NO_LSP
  56. #include "core/config/engine.h"
  57. #include "language_server/gdscript_language_server.h"
  58. #endif // !GDSCRIPT_NO_LSP
  59. Ref<GDScriptEditorTranslationParserPlugin> gdscript_translation_parser_plugin;
  60. class EditorExportGDScript : public EditorExportPlugin {
  61. GDCLASS(EditorExportGDScript, EditorExportPlugin);
  62. public:
  63. virtual void _export_file(const String &p_path, const String &p_type, const HashSet<String> &p_features) override {
  64. int script_mode = EditorExportPreset::MODE_SCRIPT_COMPILED;
  65. String script_key;
  66. const Ref<EditorExportPreset> &preset = get_export_preset();
  67. if (preset.is_valid()) {
  68. script_mode = preset->get_script_export_mode();
  69. script_key = preset->get_script_encryption_key().to_lower();
  70. }
  71. if (!p_path.ends_with(".gd") || script_mode == EditorExportPreset::MODE_SCRIPT_TEXT) {
  72. return;
  73. }
  74. // TODO: Re-add compiled GDScript on export.
  75. return;
  76. }
  77. virtual String _get_name() const override { return "GDScript"; }
  78. };
  79. static void _editor_init() {
  80. Ref<EditorExportGDScript> gd_export;
  81. gd_export.instantiate();
  82. EditorExport::get_singleton()->add_export_plugin(gd_export);
  83. #ifdef TOOLS_ENABLED
  84. Ref<GDScriptSyntaxHighlighter> gdscript_syntax_highlighter;
  85. gdscript_syntax_highlighter.instantiate();
  86. ScriptEditor::get_singleton()->register_syntax_highlighter(gdscript_syntax_highlighter);
  87. #endif
  88. #ifndef GDSCRIPT_NO_LSP
  89. register_lsp_types();
  90. GDScriptLanguageServer *lsp_plugin = memnew(GDScriptLanguageServer);
  91. EditorNode::get_singleton()->add_editor_plugin(lsp_plugin);
  92. Engine::get_singleton()->add_singleton(Engine::Singleton("GDScriptLanguageProtocol", GDScriptLanguageProtocol::get_singleton()));
  93. #endif // !GDSCRIPT_NO_LSP
  94. }
  95. #endif // TOOLS_ENABLED
  96. void initialize_gdscript_module(ModuleInitializationLevel p_level) {
  97. if (p_level == MODULE_INITIALIZATION_LEVEL_SERVERS) {
  98. GDREGISTER_CLASS(GDScript);
  99. script_language_gd = memnew(GDScriptLanguage);
  100. ScriptServer::register_language(script_language_gd);
  101. resource_loader_gd.instantiate();
  102. ResourceLoader::add_resource_format_loader(resource_loader_gd);
  103. resource_saver_gd.instantiate();
  104. ResourceSaver::add_resource_format_saver(resource_saver_gd);
  105. gdscript_cache = memnew(GDScriptCache);
  106. GDScriptUtilityFunctions::register_functions();
  107. }
  108. #ifdef TOOLS_ENABLED
  109. if (p_level == MODULE_INITIALIZATION_LEVEL_SERVERS) {
  110. EditorNode::add_init_callback(_editor_init);
  111. gdscript_translation_parser_plugin.instantiate();
  112. EditorTranslationParser::get_singleton()->add_parser(gdscript_translation_parser_plugin, EditorTranslationParser::STANDARD);
  113. }
  114. #endif // TOOLS_ENABLED
  115. }
  116. void uninitialize_gdscript_module(ModuleInitializationLevel p_level) {
  117. if (p_level == MODULE_INITIALIZATION_LEVEL_SERVERS) {
  118. ScriptServer::unregister_language(script_language_gd);
  119. if (gdscript_cache) {
  120. memdelete(gdscript_cache);
  121. }
  122. if (script_language_gd) {
  123. memdelete(script_language_gd);
  124. }
  125. ResourceLoader::remove_resource_format_loader(resource_loader_gd);
  126. resource_loader_gd.unref();
  127. ResourceSaver::remove_resource_format_saver(resource_saver_gd);
  128. resource_saver_gd.unref();
  129. GDScriptParser::cleanup();
  130. GDScriptUtilityFunctions::unregister_functions();
  131. }
  132. #ifdef TOOLS_ENABLED
  133. if (p_level == MODULE_INITIALIZATION_LEVEL_EDITOR) {
  134. EditorTranslationParser::get_singleton()->remove_parser(gdscript_translation_parser_plugin, EditorTranslationParser::STANDARD);
  135. gdscript_translation_parser_plugin.unref();
  136. }
  137. #endif // TOOLS_ENABLED
  138. }
  139. #ifdef TESTS_ENABLED
  140. void test_tokenizer() {
  141. GDScriptTests::test(GDScriptTests::TestType::TEST_TOKENIZER);
  142. }
  143. void test_parser() {
  144. GDScriptTests::test(GDScriptTests::TestType::TEST_PARSER);
  145. }
  146. void test_compiler() {
  147. GDScriptTests::test(GDScriptTests::TestType::TEST_COMPILER);
  148. }
  149. void test_bytecode() {
  150. GDScriptTests::test(GDScriptTests::TestType::TEST_BYTECODE);
  151. }
  152. REGISTER_TEST_COMMAND("gdscript-tokenizer", &test_tokenizer);
  153. REGISTER_TEST_COMMAND("gdscript-parser", &test_parser);
  154. REGISTER_TEST_COMMAND("gdscript-compiler", &test_compiler);
  155. REGISTER_TEST_COMMAND("gdscript-bytecode", &test_bytecode);
  156. #endif