register_types.cpp 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153
  1. /*************************************************/
  2. /* register_script_types.cpp */
  3. /*************************************************/
  4. /* This file is part of: */
  5. /* GODOT ENGINE */
  6. /*************************************************/
  7. /* Source code within this file is: */
  8. /* (c) 2007-2010 Juan Linietsky, Ariel Manzur */
  9. /* All Rights Reserved. */
  10. /*************************************************/
  11. #include "register_types.h"
  12. #include "gd_script.h"
  13. #include "io/resource_loader.h"
  14. #include "os/file_access.h"
  15. #include "io/file_access_encrypted.h"
  16. GDScriptLanguage *script_language_gd=NULL;
  17. ResourceFormatLoaderGDScript *resource_loader_gd=NULL;
  18. ResourceFormatSaverGDScript *resource_saver_gd=NULL;
  19. #ifdef TOOLS_ENABLED
  20. #include "tools/editor/editor_import_export.h"
  21. #include "gd_tokenizer.h"
  22. #include "tools/editor/editor_node.h"
  23. #include "tools/editor/editor_settings.h"
  24. class EditorExportGDScript : public EditorExportPlugin {
  25. OBJ_TYPE(EditorExportGDScript,EditorExportPlugin);
  26. public:
  27. virtual Vector<uint8_t> custom_export(String& p_path,const Ref<EditorExportPlatform> &p_platform) {
  28. //compile gdscript to bytecode
  29. if (EditorImportExport::get_singleton()->script_get_action()!=EditorImportExport::SCRIPT_ACTION_NONE) {
  30. if (p_path.ends_with(".gd")) {
  31. Vector<uint8_t> file = FileAccess::get_file_as_array(p_path);
  32. if (file.empty())
  33. return file;
  34. String txt;
  35. txt.parse_utf8((const char*)file.ptr(),file.size());
  36. file = GDTokenizerBuffer::parse_code_string(txt);
  37. if (!file.empty()) {
  38. if (EditorImportExport::get_singleton()->script_get_action()==EditorImportExport::SCRIPT_ACTION_ENCRYPT) {
  39. String tmp_path=EditorSettings::get_singleton()->get_settings_path().plus_file("tmp/script.gde");
  40. FileAccess *fa = FileAccess::open(tmp_path,FileAccess::WRITE);
  41. String skey=EditorImportExport::get_singleton()->script_get_encryption_key().to_lower();
  42. Vector<uint8_t> key;
  43. key.resize(32);
  44. for(int i=0;i<32;i++) {
  45. int v=0;
  46. if (i*2<skey.length()) {
  47. CharType ct = skey[i*2];
  48. if (ct>='0' && ct<='9')
  49. ct=ct-'0';
  50. else if (ct>='a' && ct<='f')
  51. ct=10+ct-'a';
  52. v|=ct<<4;
  53. }
  54. if (i*2+1<skey.length()) {
  55. CharType ct = skey[i*2+1];
  56. if (ct>='0' && ct<='9')
  57. ct=ct-'0';
  58. else if (ct>='a' && ct<='f')
  59. ct=10+ct-'a';
  60. v|=ct;
  61. }
  62. key[i]=v;
  63. }
  64. FileAccessEncrypted *fae=memnew(FileAccessEncrypted);
  65. Error err = fae->open_and_parse(fa,key,FileAccessEncrypted::MODE_WRITE_AES256);
  66. if (err==OK) {
  67. fae->store_buffer(file.ptr(),file.size());
  68. p_path=p_path.basename()+".gde";
  69. }
  70. memdelete(fae);
  71. file=FileAccess::get_file_as_array(tmp_path);
  72. return file;
  73. } else {
  74. p_path=p_path.basename()+".gdc";
  75. return file;
  76. }
  77. }
  78. }
  79. }
  80. return Vector<uint8_t>();
  81. }
  82. EditorExportGDScript(){}
  83. };
  84. static void register_editor_plugin() {
  85. Ref<EditorExportGDScript> egd = memnew( EditorExportGDScript );
  86. EditorImportExport::get_singleton()->add_export_plugin(egd);
  87. }
  88. #endif
  89. void register_gdscript_types() {
  90. ObjectTypeDB::register_type<GDScript>();
  91. ObjectTypeDB::register_virtual_type<GDFunctionState>();
  92. script_language_gd=memnew( GDScriptLanguage );
  93. //script_language_gd->init();
  94. ScriptServer::register_language(script_language_gd);
  95. resource_loader_gd=memnew( ResourceFormatLoaderGDScript );
  96. ResourceLoader::add_resource_format_loader(resource_loader_gd);
  97. resource_saver_gd=memnew( ResourceFormatSaverGDScript );
  98. ResourceSaver::add_resource_format_saver(resource_saver_gd);
  99. #ifdef TOOLS_ENABLED
  100. EditorNode::add_init_callback(register_editor_plugin);
  101. #endif
  102. }
  103. void unregister_gdscript_types() {
  104. if (script_language_gd)
  105. memdelete( script_language_gd );
  106. if (resource_loader_gd)
  107. memdelete( resource_loader_gd );
  108. if (resource_saver_gd)
  109. memdelete( resource_saver_gd );
  110. }