register_types.cpp 5.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170
  1. /*************************************************************************/
  2. /* register_types.cpp */
  3. /*************************************************************************/
  4. /* This file is part of: */
  5. /* GODOT ENGINE */
  6. /* http://www.godotengine.org */
  7. /*************************************************************************/
  8. /* Copyright (c) 2007-2017 Juan Linietsky, Ariel Manzur. */
  9. /* */
  10. /* Permission is hereby granted, free of charge, to any person obtaining */
  11. /* a copy of this software and associated documentation files (the */
  12. /* "Software"), to deal in the Software without restriction, including */
  13. /* without limitation the rights to use, copy, modify, merge, publish, */
  14. /* distribute, sublicense, and/or sell copies of the Software, and to */
  15. /* permit persons to whom the Software is furnished to do so, subject to */
  16. /* the following conditions: */
  17. /* */
  18. /* The above copyright notice and this permission notice shall be */
  19. /* included in all copies or substantial portions of the Software. */
  20. /* */
  21. /* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, */
  22. /* EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF */
  23. /* MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.*/
  24. /* IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY */
  25. /* CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, */
  26. /* TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE */
  27. /* SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. */
  28. /*************************************************************************/
  29. #include "register_types.h"
  30. #include "gd_script.h"
  31. #include "io/resource_loader.h"
  32. #include "os/file_access.h"
  33. #include "io/file_access_encrypted.h"
  34. GDScriptLanguage *script_language_gd=NULL;
  35. ResourceFormatLoaderGDScript *resource_loader_gd=NULL;
  36. ResourceFormatSaverGDScript *resource_saver_gd=NULL;
  37. #ifdef TOOLS_ENABLED
  38. #include "tools/editor/editor_import_export.h"
  39. #include "gd_tokenizer.h"
  40. #include "tools/editor/editor_node.h"
  41. #include "tools/editor/editor_settings.h"
  42. class EditorExportGDScript : public EditorExportPlugin {
  43. GDCLASS(EditorExportGDScript,EditorExportPlugin);
  44. public:
  45. virtual Vector<uint8_t> custom_export(String& p_path,const Ref<EditorExportPlatform> &p_platform) {
  46. //compile gdscript to bytecode
  47. if (EditorImportExport::get_singleton()->script_get_action()!=EditorImportExport::SCRIPT_ACTION_NONE) {
  48. if (p_path.ends_with(".gd")) {
  49. Vector<uint8_t> file = FileAccess::get_file_as_array(p_path);
  50. if (file.empty())
  51. return file;
  52. String txt;
  53. txt.parse_utf8((const char*)file.ptr(),file.size());
  54. file = GDTokenizerBuffer::parse_code_string(txt);
  55. if (!file.empty()) {
  56. if (EditorImportExport::get_singleton()->script_get_action()==EditorImportExport::SCRIPT_ACTION_ENCRYPT) {
  57. String tmp_path=EditorSettings::get_singleton()->get_settings_path().plus_file("tmp/script.gde");
  58. FileAccess *fa = FileAccess::open(tmp_path,FileAccess::WRITE);
  59. String skey=EditorImportExport::get_singleton()->script_get_encryption_key().to_lower();
  60. Vector<uint8_t> key;
  61. key.resize(32);
  62. for(int i=0;i<32;i++) {
  63. int v=0;
  64. if (i*2<skey.length()) {
  65. CharType ct = skey[i*2];
  66. if (ct>='0' && ct<='9')
  67. ct=ct-'0';
  68. else if (ct>='a' && ct<='f')
  69. ct=10+ct-'a';
  70. v|=ct<<4;
  71. }
  72. if (i*2+1<skey.length()) {
  73. CharType ct = skey[i*2+1];
  74. if (ct>='0' && ct<='9')
  75. ct=ct-'0';
  76. else if (ct>='a' && ct<='f')
  77. ct=10+ct-'a';
  78. v|=ct;
  79. }
  80. key[i]=v;
  81. }
  82. FileAccessEncrypted *fae=memnew(FileAccessEncrypted);
  83. Error err = fae->open_and_parse(fa,key,FileAccessEncrypted::MODE_WRITE_AES256);
  84. if (err==OK) {
  85. fae->store_buffer(file.ptr(),file.size());
  86. p_path=p_path.get_basename()+".gde";
  87. }
  88. memdelete(fae);
  89. file=FileAccess::get_file_as_array(tmp_path);
  90. return file;
  91. } else {
  92. p_path=p_path.get_basename()+".gdc";
  93. return file;
  94. }
  95. }
  96. }
  97. }
  98. return Vector<uint8_t>();
  99. }
  100. EditorExportGDScript(){}
  101. };
  102. static void register_editor_plugin() {
  103. Ref<EditorExportGDScript> egd = memnew( EditorExportGDScript );
  104. EditorImportExport::get_singleton()->add_export_plugin(egd);
  105. }
  106. #endif
  107. void register_gdscript_types() {
  108. ClassDB::register_class<GDScript>();
  109. ClassDB::register_virtual_class<GDFunctionState>();
  110. script_language_gd=memnew( GDScriptLanguage );
  111. //script_language_gd->init();
  112. ScriptServer::register_language(script_language_gd);
  113. resource_loader_gd=memnew( ResourceFormatLoaderGDScript );
  114. ResourceLoader::add_resource_format_loader(resource_loader_gd);
  115. resource_saver_gd=memnew( ResourceFormatSaverGDScript );
  116. ResourceSaver::add_resource_format_saver(resource_saver_gd);
  117. #ifdef TOOLS_ENABLED
  118. EditorNode::add_init_callback(register_editor_plugin);
  119. #endif
  120. }
  121. void unregister_gdscript_types() {
  122. ScriptServer::unregister_language(script_language_gd);
  123. if (script_language_gd)
  124. memdelete( script_language_gd );
  125. if (resource_loader_gd)
  126. memdelete( resource_loader_gd );
  127. if (resource_saver_gd)
  128. memdelete( resource_saver_gd );
  129. }