register_types.cpp 6.3 KB

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