register_types.cpp 7.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204
  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_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/dir_access.h"
  47. #include "core/io/file_access.h"
  48. #include "core/io/file_access_encrypted.h"
  49. #include "core/io/resource_loader.h"
  50. #ifdef TOOLS_ENABLED
  51. #include "editor/editor_node.h"
  52. #include "editor/editor_settings.h"
  53. #include "editor/editor_translation_parser.h"
  54. #include "editor/export/editor_export.h"
  55. #ifndef GDSCRIPT_NO_LSP
  56. #include "core/config/engine.h"
  57. #endif
  58. #endif // TOOLS_ENABLED
  59. #ifdef TESTS_ENABLED
  60. #include "tests/test_macros.h"
  61. #endif
  62. GDScriptLanguage *script_language_gd = nullptr;
  63. Ref<ResourceFormatLoaderGDScript> resource_loader_gd;
  64. Ref<ResourceFormatSaverGDScript> resource_saver_gd;
  65. GDScriptCache *gdscript_cache = nullptr;
  66. #ifdef TOOLS_ENABLED
  67. Ref<GDScriptEditorTranslationParserPlugin> gdscript_translation_parser_plugin;
  68. class EditorExportGDScript : public EditorExportPlugin {
  69. GDCLASS(EditorExportGDScript, EditorExportPlugin);
  70. public:
  71. virtual void _export_file(const String &p_path, const String &p_type, const HashSet<String> &p_features) override {
  72. String script_key;
  73. const Ref<EditorExportPreset> &preset = get_export_preset();
  74. if (preset.is_valid()) {
  75. script_key = preset->get_script_encryption_key().to_lower();
  76. }
  77. if (!p_path.ends_with(".gd")) {
  78. return;
  79. }
  80. return;
  81. }
  82. virtual String get_name() const override { return "GDScript"; }
  83. };
  84. static void _editor_init() {
  85. Ref<EditorExportGDScript> gd_export;
  86. gd_export.instantiate();
  87. EditorExport::get_singleton()->add_export_plugin(gd_export);
  88. #ifdef TOOLS_ENABLED
  89. Ref<GDScriptSyntaxHighlighter> gdscript_syntax_highlighter;
  90. gdscript_syntax_highlighter.instantiate();
  91. ScriptEditor::get_singleton()->register_syntax_highlighter(gdscript_syntax_highlighter);
  92. #endif
  93. #ifndef GDSCRIPT_NO_LSP
  94. register_lsp_types();
  95. GDScriptLanguageServer *lsp_plugin = memnew(GDScriptLanguageServer);
  96. EditorNode::get_singleton()->add_editor_plugin(lsp_plugin);
  97. Engine::get_singleton()->add_singleton(Engine::Singleton("GDScriptLanguageProtocol", GDScriptLanguageProtocol::get_singleton()));
  98. #endif // !GDSCRIPT_NO_LSP
  99. }
  100. #endif // TOOLS_ENABLED
  101. void initialize_gdscript_module(ModuleInitializationLevel p_level) {
  102. if (p_level == MODULE_INITIALIZATION_LEVEL_SERVERS) {
  103. GDREGISTER_CLASS(GDScript);
  104. script_language_gd = memnew(GDScriptLanguage);
  105. ScriptServer::register_language(script_language_gd);
  106. resource_loader_gd.instantiate();
  107. ResourceLoader::add_resource_format_loader(resource_loader_gd);
  108. resource_saver_gd.instantiate();
  109. ResourceSaver::add_resource_format_saver(resource_saver_gd);
  110. gdscript_cache = memnew(GDScriptCache);
  111. GDScriptUtilityFunctions::register_functions();
  112. }
  113. #ifdef TOOLS_ENABLED
  114. if (p_level == MODULE_INITIALIZATION_LEVEL_SERVERS) {
  115. EditorNode::add_init_callback(_editor_init);
  116. gdscript_translation_parser_plugin.instantiate();
  117. EditorTranslationParser::get_singleton()->add_parser(gdscript_translation_parser_plugin, EditorTranslationParser::STANDARD);
  118. }
  119. #endif // TOOLS_ENABLED
  120. }
  121. void uninitialize_gdscript_module(ModuleInitializationLevel p_level) {
  122. if (p_level == MODULE_INITIALIZATION_LEVEL_SERVERS) {
  123. ScriptServer::unregister_language(script_language_gd);
  124. if (gdscript_cache) {
  125. memdelete(gdscript_cache);
  126. }
  127. if (script_language_gd) {
  128. memdelete(script_language_gd);
  129. }
  130. ResourceLoader::remove_resource_format_loader(resource_loader_gd);
  131. resource_loader_gd.unref();
  132. ResourceSaver::remove_resource_format_saver(resource_saver_gd);
  133. resource_saver_gd.unref();
  134. GDScriptParser::cleanup();
  135. GDScriptUtilityFunctions::unregister_functions();
  136. }
  137. #ifdef TOOLS_ENABLED
  138. if (p_level == MODULE_INITIALIZATION_LEVEL_EDITOR) {
  139. EditorTranslationParser::get_singleton()->remove_parser(gdscript_translation_parser_plugin, EditorTranslationParser::STANDARD);
  140. gdscript_translation_parser_plugin.unref();
  141. }
  142. #endif // TOOLS_ENABLED
  143. }
  144. #ifdef TESTS_ENABLED
  145. void test_tokenizer() {
  146. GDScriptTests::test(GDScriptTests::TestType::TEST_TOKENIZER);
  147. }
  148. void test_parser() {
  149. GDScriptTests::test(GDScriptTests::TestType::TEST_PARSER);
  150. }
  151. void test_compiler() {
  152. GDScriptTests::test(GDScriptTests::TestType::TEST_COMPILER);
  153. }
  154. void test_bytecode() {
  155. GDScriptTests::test(GDScriptTests::TestType::TEST_BYTECODE);
  156. }
  157. REGISTER_TEST_COMMAND("gdscript-tokenizer", &test_tokenizer);
  158. REGISTER_TEST_COMMAND("gdscript-parser", &test_parser);
  159. REGISTER_TEST_COMMAND("gdscript-compiler", &test_compiler);
  160. REGISTER_TEST_COMMAND("gdscript-bytecode", &test_bytecode);
  161. #endif