register_types.cpp 5.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169
  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/file_access.h"
  34. #include "editor/gdscript_highlighter.h"
  35. #include "gdscript.h"
  36. #include "gdscript_tokenizer.h"
  37. GDScriptLanguage *script_language_gd = NULL;
  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. class EditorExportGDScript : public EditorExportPlugin {
  45. GDCLASS(EditorExportGDScript, EditorExportPlugin);
  46. public:
  47. virtual void _export_file(const String &p_path, const String &p_type, const Set<String> &p_features) {
  48. int script_mode = EditorExportPreset::MODE_SCRIPT_COMPILED;
  49. String script_key;
  50. const Ref<EditorExportPreset> &preset = get_export_preset();
  51. if (preset.is_valid()) {
  52. script_mode = preset->get_script_export_mode();
  53. script_key = preset->get_script_encryption_key().to_lower();
  54. }
  55. if (!p_path.ends_with(".gd") || script_mode == EditorExportPreset::MODE_SCRIPT_TEXT)
  56. return;
  57. Vector<uint8_t> file = FileAccess::get_file_as_array(p_path);
  58. if (file.empty())
  59. return;
  60. String txt;
  61. txt.parse_utf8((const char *)file.ptr(), file.size());
  62. file = GDScriptTokenizerBuffer::parse_code_string(txt);
  63. if (!file.empty()) {
  64. if (script_mode == EditorExportPreset::MODE_SCRIPT_ENCRYPTED) {
  65. String tmp_path = EditorSettings::get_singleton()->get_settings_dir().plus_file("tmp/script.gde");
  66. FileAccess *fa = FileAccess::open(tmp_path, FileAccess::WRITE);
  67. Vector<uint8_t> key;
  68. key.resize(32);
  69. for (int i = 0; i < 32; i++) {
  70. int v = 0;
  71. if (i * 2 < script_key.length()) {
  72. CharType ct = script_key[i * 2];
  73. if (ct >= '0' && ct <= '9')
  74. ct = ct - '0';
  75. else if (ct >= 'a' && ct <= 'f')
  76. ct = 10 + ct - 'a';
  77. v |= ct << 4;
  78. }
  79. if (i * 2 + 1 < script_key.length()) {
  80. CharType ct = script_key[i * 2 + 1];
  81. if (ct >= '0' && ct <= '9')
  82. ct = ct - '0';
  83. else if (ct >= 'a' && ct <= 'f')
  84. ct = 10 + ct - 'a';
  85. v |= ct;
  86. }
  87. key.write[i] = v;
  88. }
  89. FileAccessEncrypted *fae = memnew(FileAccessEncrypted);
  90. Error err = fae->open_and_parse(fa, key, FileAccessEncrypted::MODE_WRITE_AES256);
  91. if (err == OK) {
  92. fae->store_buffer(file.ptr(), file.size());
  93. }
  94. memdelete(fae);
  95. file = FileAccess::get_file_as_array(tmp_path);
  96. add_file(p_path.get_basename() + ".gde", file, true);
  97. } else {
  98. add_file(p_path.get_basename() + ".gdc", file, true);
  99. }
  100. }
  101. }
  102. };
  103. static void _editor_init() {
  104. Ref<EditorExportGDScript> gd_export;
  105. gd_export.instance();
  106. EditorExport::get_singleton()->add_export_plugin(gd_export);
  107. }
  108. #endif
  109. void register_gdscript_types() {
  110. ClassDB::register_class<GDScript>();
  111. ClassDB::register_virtual_class<GDScriptFunctionState>();
  112. script_language_gd = memnew(GDScriptLanguage);
  113. ScriptServer::register_language(script_language_gd);
  114. resource_loader_gd.instance();
  115. ResourceLoader::add_resource_format_loader(resource_loader_gd);
  116. resource_saver_gd.instance();
  117. ResourceSaver::add_resource_format_saver(resource_saver_gd);
  118. #ifdef TOOLS_ENABLED
  119. ScriptEditor::register_create_syntax_highlighter_function(GDScriptSyntaxHighlighter::create);
  120. EditorNode::add_init_callback(_editor_init);
  121. #endif
  122. }
  123. void unregister_gdscript_types() {
  124. ScriptServer::unregister_language(script_language_gd);
  125. if (script_language_gd)
  126. memdelete(script_language_gd);
  127. ResourceLoader::remove_resource_format_loader(resource_loader_gd);
  128. resource_loader_gd.unref();
  129. ResourceSaver::remove_resource_format_saver(resource_saver_gd);
  130. resource_saver_gd.unref();
  131. }