register_types.cpp 7.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199
  1. /*************************************************************************/
  2. /* register_types.cpp */
  3. /*************************************************************************/
  4. /* This file is part of: */
  5. /* GODOT ENGINE */
  6. /* https://godotengine.org */
  7. /*************************************************************************/
  8. /* Copyright (c) 2007-2020 Juan Linietsky, Ariel Manzur. */
  9. /* Copyright (c) 2014-2020 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/file_access_encrypted.h"
  32. #include "core/io/resource_loader.h"
  33. #include "core/os/dir_access.h"
  34. #include "core/os/file_access.h"
  35. #include "gdscript.h"
  36. #include "gdscript_tokenizer.h"
  37. GDScriptLanguage *script_language_gd = nullptr;
  38. Ref<ResourceFormatLoaderGDScript> resource_loader_gd;
  39. Ref<ResourceFormatSaverGDScript> resource_saver_gd;
  40. #ifdef TOOLS_ENABLED
  41. #include "editor/editor_export.h"
  42. #include "editor/editor_node.h"
  43. #include "editor/editor_settings.h"
  44. #include "editor/editor_translation_parser.h"
  45. #include "editor/gdscript_highlighter.h"
  46. #include "editor/gdscript_translation_parser_plugin.h"
  47. #ifndef GDSCRIPT_NO_LSP
  48. #include "core/engine.h"
  49. #include "language_server/gdscript_language_server.h"
  50. #endif // !GDSCRIPT_NO_LSP
  51. Ref<GDScriptEditorTranslationParserPlugin> gdscript_translation_parser_plugin;
  52. class EditorExportGDScript : public EditorExportPlugin {
  53. GDCLASS(EditorExportGDScript, EditorExportPlugin);
  54. public:
  55. virtual void _export_file(const String &p_path, const String &p_type, const Set<String> &p_features) override {
  56. int script_mode = EditorExportPreset::MODE_SCRIPT_COMPILED;
  57. String script_key;
  58. const Ref<EditorExportPreset> &preset = get_export_preset();
  59. if (preset.is_valid()) {
  60. script_mode = preset->get_script_export_mode();
  61. script_key = preset->get_script_encryption_key().to_lower();
  62. }
  63. if (!p_path.ends_with(".gd") || script_mode == EditorExportPreset::MODE_SCRIPT_TEXT) {
  64. return;
  65. }
  66. Vector<uint8_t> file = FileAccess::get_file_as_array(p_path);
  67. if (file.empty()) {
  68. return;
  69. }
  70. String txt;
  71. txt.parse_utf8((const char *)file.ptr(), file.size());
  72. file = GDScriptTokenizerBuffer::parse_code_string(txt);
  73. if (!file.empty()) {
  74. if (script_mode == EditorExportPreset::MODE_SCRIPT_ENCRYPTED) {
  75. String tmp_path = EditorSettings::get_singleton()->get_cache_dir().plus_file("script.gde");
  76. FileAccess *fa = FileAccess::open(tmp_path, FileAccess::WRITE);
  77. Vector<uint8_t> key;
  78. key.resize(32);
  79. for (int i = 0; i < 32; i++) {
  80. int v = 0;
  81. if (i * 2 < script_key.length()) {
  82. CharType ct = script_key[i * 2];
  83. if (ct >= '0' && ct <= '9') {
  84. ct = ct - '0';
  85. } else if (ct >= 'a' && ct <= 'f') {
  86. ct = 10 + ct - 'a';
  87. }
  88. v |= ct << 4;
  89. }
  90. if (i * 2 + 1 < script_key.length()) {
  91. CharType ct = script_key[i * 2 + 1];
  92. if (ct >= '0' && ct <= '9') {
  93. ct = ct - '0';
  94. } else if (ct >= 'a' && ct <= 'f') {
  95. ct = 10 + ct - 'a';
  96. }
  97. v |= ct;
  98. }
  99. key.write[i] = v;
  100. }
  101. FileAccessEncrypted *fae = memnew(FileAccessEncrypted);
  102. Error err = fae->open_and_parse(fa, key, FileAccessEncrypted::MODE_WRITE_AES256);
  103. if (err == OK) {
  104. fae->store_buffer(file.ptr(), file.size());
  105. }
  106. memdelete(fae);
  107. file = FileAccess::get_file_as_array(tmp_path);
  108. add_file(p_path.get_basename() + ".gde", file, true);
  109. // Clean up temporary file.
  110. DirAccess::remove_file_or_error(tmp_path);
  111. } else {
  112. add_file(p_path.get_basename() + ".gdc", file, true);
  113. }
  114. }
  115. }
  116. };
  117. static void _editor_init() {
  118. Ref<EditorExportGDScript> gd_export;
  119. gd_export.instance();
  120. EditorExport::get_singleton()->add_export_plugin(gd_export);
  121. #ifdef TOOLS_ENABLED
  122. Ref<GDScriptSyntaxHighlighter> gdscript_syntax_highlighter;
  123. gdscript_syntax_highlighter.instance();
  124. ScriptEditor::get_singleton()->register_syntax_highlighter(gdscript_syntax_highlighter);
  125. #endif
  126. #ifndef GDSCRIPT_NO_LSP
  127. register_lsp_types();
  128. GDScriptLanguageServer *lsp_plugin = memnew(GDScriptLanguageServer);
  129. EditorNode::get_singleton()->add_editor_plugin(lsp_plugin);
  130. Engine::get_singleton()->add_singleton(Engine::Singleton("GDScriptLanguageProtocol", GDScriptLanguageProtocol::get_singleton()));
  131. #endif // !GDSCRIPT_NO_LSP
  132. }
  133. #endif // TOOLS_ENABLED
  134. void register_gdscript_types() {
  135. ClassDB::register_class<GDScript>();
  136. ClassDB::register_virtual_class<GDScriptFunctionState>();
  137. script_language_gd = memnew(GDScriptLanguage);
  138. ScriptServer::register_language(script_language_gd);
  139. resource_loader_gd.instance();
  140. ResourceLoader::add_resource_format_loader(resource_loader_gd);
  141. resource_saver_gd.instance();
  142. ResourceSaver::add_resource_format_saver(resource_saver_gd);
  143. #ifdef TOOLS_ENABLED
  144. EditorNode::add_init_callback(_editor_init);
  145. gdscript_translation_parser_plugin.instance();
  146. EditorTranslationParser::get_singleton()->add_parser(gdscript_translation_parser_plugin, EditorTranslationParser::STANDARD);
  147. #endif // TOOLS_ENABLED
  148. }
  149. void unregister_gdscript_types() {
  150. ScriptServer::unregister_language(script_language_gd);
  151. if (script_language_gd) {
  152. memdelete(script_language_gd);
  153. }
  154. ResourceLoader::remove_resource_format_loader(resource_loader_gd);
  155. resource_loader_gd.unref();
  156. ResourceSaver::remove_resource_format_saver(resource_saver_gd);
  157. resource_saver_gd.unref();
  158. #ifdef TOOLS_ENABLED
  159. EditorTranslationParser::get_singleton()->remove_parser(gdscript_translation_parser_plugin, EditorTranslationParser::STANDARD);
  160. gdscript_translation_parser_plugin.unref();
  161. #endif // TOOLS_ENABLED
  162. }