editor_export_platform.cpp 88 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121212221232124212521262127212821292130213121322133213421352136213721382139214021412142214321442145214621472148214921502151215221532154215521562157215821592160216121622163216421652166216721682169217021712172217321742175217621772178217921802181218221832184218521862187218821892190219121922193219421952196219721982199220022012202220322042205220622072208220922102211221222132214221522162217221822192220222122222223222422252226222722282229223022312232223322342235223622372238223922402241224222432244224522462247224822492250225122522253225422552256225722582259226022612262226322642265226622672268226922702271227222732274227522762277227822792280228122822283228422852286228722882289229022912292229322942295229622972298229923002301230223032304230523062307230823092310231123122313231423152316231723182319232023212322232323242325232623272328232923302331233223332334233523362337233823392340234123422343234423452346234723482349235023512352235323542355235623572358235923602361236223632364236523662367236823692370237123722373237423752376237723782379238023812382238323842385238623872388238923902391239223932394239523962397239823992400240124022403240424052406240724082409241024112412241324142415241624172418241924202421242224232424242524262427242824292430243124322433243424352436243724382439244024412442244324442445244624472448244924502451245224532454245524562457245824592460246124622463246424652466246724682469247024712472247324742475247624772478247924802481248224832484248524862487248824892490249124922493249424952496249724982499250025012502250325042505250625072508250925102511251225132514251525162517251825192520252125222523252425252526252725282529253025312532253325342535253625372538253925402541254225432544254525462547254825492550255125522553255425552556255725582559256025612562256325642565256625672568256925702571257225732574257525762577257825792580258125822583258425852586258725882589259025912592259325942595259625972598259926002601260226032604
  1. /**************************************************************************/
  2. /* editor_export_platform.cpp */
  3. /**************************************************************************/
  4. /* This file is part of: */
  5. /* GODOT ENGINE */
  6. /* https://godotengine.org */
  7. /**************************************************************************/
  8. /* Copyright (c) 2014-present Godot Engine contributors (see AUTHORS.md). */
  9. /* Copyright (c) 2007-2014 Juan Linietsky, Ariel Manzur. */
  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 "editor_export_platform.h"
  31. #include "editor_export_platform.compat.inc"
  32. #include "core/config/project_settings.h"
  33. #include "core/crypto/crypto_core.h"
  34. #include "core/extension/gdextension.h"
  35. #include "core/io/delta_encoding.h"
  36. #include "core/io/dir_access.h"
  37. #include "core/io/file_access_encrypted.h"
  38. #include "core/io/file_access_pack.h" // PACK_HEADER_MAGIC, PACK_FORMAT_VERSION
  39. #include "core/io/image.h"
  40. #include "core/io/image_loader.h"
  41. #include "core/io/resource_uid.h"
  42. #include "core/math/random_pcg.h"
  43. #include "core/os/shared_object.h"
  44. #include "core/version.h"
  45. #include "editor/editor_node.h"
  46. #include "editor/editor_string_names.h"
  47. #include "editor/export/editor_export.h"
  48. #include "editor/file_system/editor_file_system.h"
  49. #include "editor/file_system/editor_paths.h"
  50. #include "editor/script/script_editor_plugin.h"
  51. #include "editor/settings/editor_settings.h"
  52. #include "editor/themes/editor_scale.h"
  53. #include "editor_export_plugin.h"
  54. #include "scene/gui/rich_text_label.h"
  55. #include "scene/main/node.h"
  56. #include "scene/resources/packed_scene.h"
  57. #include "scene/resources/texture.h"
  58. class EditorExportSaveProxy {
  59. HashSet<String> saved_paths;
  60. EditorExportPlatform::EditorExportSaveFunction save_func;
  61. bool tracking_saves = false;
  62. public:
  63. bool has_saved(const String &p_path) const { return saved_paths.has(p_path); }
  64. Error save_file(const Ref<EditorExportPreset> &p_preset, void *p_userdata, const String &p_path, const Vector<uint8_t> &p_data, int p_file, int p_total, const Vector<String> &p_enc_in_filters, const Vector<String> &p_enc_ex_filters, const Vector<uint8_t> &p_key, uint64_t p_seed, bool p_delta) {
  65. if (tracking_saves) {
  66. saved_paths.insert(p_path.simplify_path().trim_prefix("res://"));
  67. }
  68. return save_func(p_preset, p_userdata, p_path, p_data, p_file, p_total, p_enc_in_filters, p_enc_ex_filters, p_key, p_seed, p_delta);
  69. }
  70. EditorExportSaveProxy(EditorExportPlatform::EditorExportSaveFunction p_save_func, bool p_track_saves) :
  71. save_func(p_save_func), tracking_saves(p_track_saves) {}
  72. };
  73. static int _get_pad(int p_alignment, int p_n) {
  74. int rest = p_n % p_alignment;
  75. int pad = 0;
  76. if (rest > 0) {
  77. pad = p_alignment - rest;
  78. };
  79. return pad;
  80. }
  81. static constexpr int PCK_PADDING = 16;
  82. Ref<Image> EditorExportPlatform::_load_icon_or_splash_image(const String &p_path, Error *r_error) const {
  83. Ref<Image> image;
  84. if (!p_path.is_empty() && ResourceLoader::exists(p_path) && !ResourceLoader::get_resource_type(p_path).is_empty()) {
  85. Ref<Texture2D> texture = ResourceLoader::load(p_path, "", ResourceFormatLoader::CACHE_MODE_REUSE, r_error);
  86. if (texture.is_valid()) {
  87. image = texture->get_image();
  88. if (image.is_valid() && image->is_compressed()) {
  89. image->decompress();
  90. }
  91. }
  92. }
  93. if (image.is_null()) {
  94. image.instantiate();
  95. Error err = ImageLoader::load_image(p_path, image);
  96. if (r_error) {
  97. *r_error = err;
  98. }
  99. }
  100. return image;
  101. }
  102. bool EditorExportPlatform::fill_log_messages(RichTextLabel *p_log, Error p_err) {
  103. bool has_messages = false;
  104. int msg_count = get_message_count();
  105. p_log->add_text(TTR("Project export for platform:") + " ");
  106. p_log->add_image(get_logo(), 16 * EDSCALE, 16 * EDSCALE, Color(1.0, 1.0, 1.0), INLINE_ALIGNMENT_CENTER);
  107. p_log->add_text(" ");
  108. p_log->add_text(get_name());
  109. p_log->add_text(" - ");
  110. if (p_err == OK && get_worst_message_type() < EditorExportPlatform::EXPORT_MESSAGE_ERROR) {
  111. if (get_worst_message_type() >= EditorExportPlatform::EXPORT_MESSAGE_WARNING) {
  112. p_log->add_image(p_log->get_editor_theme_icon(SNAME("StatusWarning")), 16 * EDSCALE, 16 * EDSCALE, Color(1.0, 1.0, 1.0), INLINE_ALIGNMENT_CENTER);
  113. p_log->add_text(" ");
  114. p_log->add_text(TTR("Completed with warnings."));
  115. has_messages = true;
  116. } else {
  117. p_log->add_image(p_log->get_editor_theme_icon(SNAME("StatusSuccess")), 16 * EDSCALE, 16 * EDSCALE, Color(1.0, 1.0, 1.0), INLINE_ALIGNMENT_CENTER);
  118. p_log->add_text(" ");
  119. p_log->add_text(TTR("Completed successfully."));
  120. if (msg_count > 0) {
  121. has_messages = true;
  122. }
  123. }
  124. } else {
  125. p_log->add_image(p_log->get_editor_theme_icon(SNAME("StatusError")), 16 * EDSCALE, 16 * EDSCALE, Color(1.0, 1.0, 1.0), INLINE_ALIGNMENT_CENTER);
  126. p_log->add_text(" ");
  127. p_log->add_text(TTR("Failed."));
  128. has_messages = true;
  129. }
  130. p_log->add_newline();
  131. if (msg_count > 0) {
  132. p_log->push_table(2);
  133. p_log->set_table_column_expand(0, false);
  134. p_log->set_table_column_expand(1, true);
  135. for (int m = 0; m < msg_count; m++) {
  136. EditorExportPlatform::ExportMessage msg = get_message(m);
  137. Color color = p_log->get_theme_color(SceneStringName(font_color), SNAME("Label"));
  138. Ref<Texture> icon;
  139. switch (msg.msg_type) {
  140. case EditorExportPlatform::EXPORT_MESSAGE_INFO: {
  141. color = p_log->get_theme_color(SceneStringName(font_color), EditorStringName(Editor)) * Color(1, 1, 1, 0.6);
  142. } break;
  143. case EditorExportPlatform::EXPORT_MESSAGE_WARNING: {
  144. icon = p_log->get_editor_theme_icon(SNAME("Warning"));
  145. color = p_log->get_theme_color(SNAME("warning_color"), EditorStringName(Editor));
  146. } break;
  147. case EditorExportPlatform::EXPORT_MESSAGE_ERROR: {
  148. icon = p_log->get_editor_theme_icon(SNAME("Error"));
  149. color = p_log->get_theme_color(SNAME("error_color"), EditorStringName(Editor));
  150. } break;
  151. default:
  152. break;
  153. }
  154. p_log->push_cell();
  155. p_log->add_text("\t");
  156. if (icon.is_valid()) {
  157. p_log->add_image(icon);
  158. }
  159. p_log->pop();
  160. p_log->push_cell();
  161. p_log->push_color(color);
  162. p_log->add_text(vformat("[%s]: %s", msg.category, msg.text));
  163. p_log->pop();
  164. p_log->pop();
  165. }
  166. p_log->pop();
  167. p_log->add_newline();
  168. } else if (p_err != OK) {
  169. // We failed but don't show any user-facing messages. This is bad and should not
  170. // be allowed, but just in case this happens, let's give the user something at least.
  171. p_log->push_table(2);
  172. p_log->set_table_column_expand(0, false);
  173. p_log->set_table_column_expand(1, true);
  174. {
  175. Color color = p_log->get_theme_color(SNAME("error_color"), EditorStringName(Editor));
  176. Ref<Texture> icon = p_log->get_editor_theme_icon(SNAME("Error"));
  177. p_log->push_cell();
  178. p_log->add_text("\t");
  179. if (icon.is_valid()) {
  180. p_log->add_image(icon);
  181. }
  182. p_log->pop();
  183. p_log->push_cell();
  184. p_log->push_color(color);
  185. p_log->add_text(vformat("[%s]: %s", TTR("Unknown Error"), vformat(TTR("Export failed with error code %d."), p_err)));
  186. p_log->pop();
  187. p_log->pop();
  188. }
  189. p_log->pop();
  190. p_log->add_newline();
  191. }
  192. p_log->add_newline();
  193. return has_messages;
  194. }
  195. Error EditorExportPlatform::_load_patches(const Vector<String> &p_patches) {
  196. Error err = OK;
  197. if (!p_patches.is_empty()) {
  198. for (const String &path : p_patches) {
  199. err = PackedData::get_singleton()->add_pack(path, true, 0);
  200. if (err != OK) {
  201. add_message(EXPORT_MESSAGE_ERROR, TTR("Patch Creation"), vformat(TTR("Could not load patch pack with path \"%s\"."), path));
  202. return err;
  203. }
  204. }
  205. }
  206. return err;
  207. }
  208. void EditorExportPlatform::_unload_patches() {
  209. PackedData::get_singleton()->clear();
  210. }
  211. Error EditorExportPlatform::_encrypt_and_store_data(Ref<FileAccess> p_fd, const String &p_path, const Vector<uint8_t> &p_data, const Vector<String> &p_enc_in_filters, const Vector<String> &p_enc_ex_filters, const Vector<uint8_t> &p_key, uint64_t p_seed, bool &r_encrypt) {
  212. r_encrypt = false;
  213. for (int i = 0; i < p_enc_in_filters.size(); ++i) {
  214. if (p_path.matchn(p_enc_in_filters[i]) || p_path.trim_prefix("res://").matchn(p_enc_in_filters[i])) {
  215. r_encrypt = true;
  216. break;
  217. }
  218. }
  219. for (int i = 0; i < p_enc_ex_filters.size(); ++i) {
  220. if (p_path.matchn(p_enc_ex_filters[i]) || p_path.trim_prefix("res://").matchn(p_enc_ex_filters[i])) {
  221. r_encrypt = false;
  222. break;
  223. }
  224. }
  225. Ref<FileAccessEncrypted> fae;
  226. Ref<FileAccess> ftmp = p_fd;
  227. if (r_encrypt) {
  228. Vector<uint8_t> iv;
  229. if (p_seed != 0) {
  230. uint64_t seed = p_seed;
  231. const uint8_t *ptr = p_data.ptr();
  232. int64_t len = p_data.size();
  233. for (int64_t i = 0; i < len; i++) {
  234. seed = ((seed << 5) + seed) ^ ptr[i];
  235. }
  236. RandomPCG rng = RandomPCG(seed);
  237. iv.resize(16);
  238. for (int i = 0; i < 16; i++) {
  239. iv.write[i] = rng.rand() % 256;
  240. }
  241. }
  242. fae.instantiate();
  243. ERR_FAIL_COND_V(fae.is_null(), ERR_FILE_CANT_OPEN);
  244. Error err = fae->open_and_parse(ftmp, p_key, FileAccessEncrypted::MODE_WRITE_AES256, false, iv);
  245. ERR_FAIL_COND_V(err != OK, ERR_FILE_CANT_OPEN);
  246. ftmp = fae;
  247. }
  248. // Store file content.
  249. ftmp->store_buffer(p_data.ptr(), p_data.size());
  250. if (fae.is_valid()) {
  251. ftmp.unref();
  252. fae.unref();
  253. }
  254. return OK;
  255. }
  256. Error EditorExportPlatform::_save_pack_file(const Ref<EditorExportPreset> &p_preset, void *p_userdata, const String &p_path, const Vector<uint8_t> &p_data, int p_file, int p_total, const Vector<String> &p_enc_in_filters, const Vector<String> &p_enc_ex_filters, const Vector<uint8_t> &p_key, uint64_t p_seed, bool p_delta) {
  257. ERR_FAIL_COND_V_MSG(p_total < 1, ERR_PARAMETER_RANGE_ERROR, "Must select at least one file to export.");
  258. PackData *pd = (PackData *)p_userdata;
  259. const String simplified_path = simplify_path(p_path);
  260. Ref<FileAccess> ftmp;
  261. if (pd->use_sparse_pck) {
  262. ftmp = FileAccess::open(pd->path.get_base_dir().path_join(simplified_path.trim_prefix("res://")), FileAccess::WRITE);
  263. } else {
  264. ftmp = pd->f;
  265. }
  266. SavedData sd;
  267. sd.path_utf8 = simplified_path.trim_prefix("res://").utf8();
  268. sd.ofs = (pd->use_sparse_pck) ? 0 : pd->f->get_position();
  269. sd.size = p_data.size();
  270. sd.delta = p_delta;
  271. Error err = _encrypt_and_store_data(ftmp, simplified_path, p_data, p_enc_in_filters, p_enc_ex_filters, p_key, p_seed, sd.encrypted);
  272. if (err != OK) {
  273. return err;
  274. }
  275. if (!pd->use_sparse_pck) {
  276. ERR_FAIL_COND_V(pd->f->get_position() - sd.ofs < (uint64_t)p_data.size(), ERR_FILE_CANT_WRITE);
  277. }
  278. if (!pd->use_sparse_pck) {
  279. int pad = _get_pad(PCK_PADDING, pd->f->get_position());
  280. for (int i = 0; i < pad; i++) {
  281. pd->f->store_8(0);
  282. }
  283. }
  284. // Store MD5 of original file.
  285. {
  286. unsigned char hash[16];
  287. CryptoCore::md5(p_data.ptr(), p_data.size(), hash);
  288. sd.md5.resize(16);
  289. for (int i = 0; i < 16; i++) {
  290. sd.md5.write[i] = hash[i];
  291. }
  292. }
  293. pd->file_ofs.push_back(sd);
  294. // TRANSLATORS: This is an editor progress label describing the storing of a file.
  295. if (pd->ep->step(vformat(TTR("Storing File: %s"), p_path), 2 + p_file * 100 / p_total, false)) {
  296. return ERR_SKIP;
  297. }
  298. return OK;
  299. }
  300. Error EditorExportPlatform::_save_pack_patch_file(const Ref<EditorExportPreset> &p_preset, void *p_userdata, const String &p_path, const Vector<uint8_t> &p_data, int p_file, int p_total, const Vector<String> &p_enc_in_filters, const Vector<String> &p_enc_ex_filters, const Vector<uint8_t> &p_key, uint64_t p_seed, bool p_delta) {
  301. Ref<FileAccess> old_file = PackedData::get_singleton()->try_open_path(p_path);
  302. if (old_file.is_null()) {
  303. return _save_pack_file(p_preset, p_userdata, p_path, p_data, p_file, p_total, p_enc_in_filters, p_enc_ex_filters, p_key, p_seed, false);
  304. }
  305. Vector<uint8_t> old_data = old_file->get_buffer(old_file->get_length());
  306. // We can't rely on the MD5 as stored in the PCKs, since delta patches could have made it stale.
  307. if (p_data == old_data) {
  308. return OK; // Do nothing if the file hasn't changed.
  309. }
  310. if (!p_preset->is_patch_delta_encoding_enabled()) {
  311. return _save_pack_file(p_preset, p_userdata, p_path, p_data, p_file, p_total, p_enc_in_filters, p_enc_ex_filters, p_key, p_seed, false);
  312. }
  313. bool delta = false;
  314. for (const String &filter : p_preset->get_patch_delta_include_filter().split(",", false)) {
  315. String filter_stripped = filter.strip_edges();
  316. if (p_path.matchn(filter_stripped) || p_path.trim_prefix("res://").matchn(filter_stripped)) {
  317. delta = true;
  318. break;
  319. }
  320. }
  321. for (const String &filter : p_preset->get_patch_delta_exclude_filter().split(",", false)) {
  322. String filter_stripped = filter.strip_edges();
  323. if (p_path.matchn(filter_stripped) || p_path.trim_prefix("res://").matchn(filter_stripped)) {
  324. delta = false;
  325. break;
  326. }
  327. }
  328. Vector<uint8_t> patch_data = p_data;
  329. if (delta) {
  330. Error err = DeltaEncoding::encode_delta(old_data, p_data, patch_data, p_preset->get_patch_delta_zstd_level());
  331. if (err != OK) {
  332. return err;
  333. }
  334. int64_t reduction_bytes = MAX(0, p_data.size() - patch_data.size());
  335. double reduction_ratio = reduction_bytes / (double)p_data.size();
  336. if (reduction_ratio >= p_preset->get_patch_delta_min_reduction()) {
  337. print_verbose(vformat("Used delta encoding for patch of \"%s\", resulting in a patch of %d bytes, which reduced the size by %.1f%% (%d bytes) compared to the actual file.", p_path, patch_data.size(), reduction_ratio * 100, reduction_bytes));
  338. } else {
  339. print_verbose(vformat("Skipped delta encoding for patch of \"%s\", as it resulted in a patch of %d bytes, which only reduced the size by %.1f%% (%d bytes) compared to the actual file.", p_path, patch_data.size(), reduction_ratio * 100, reduction_bytes));
  340. patch_data = p_data;
  341. delta = false;
  342. }
  343. } else {
  344. print_verbose(vformat("Skipped delta encoding for patch of \"%s\", due to include/exclude filters.", p_path));
  345. }
  346. return _save_pack_file(p_preset, p_userdata, p_path, patch_data, p_file, p_total, p_enc_in_filters, p_enc_ex_filters, p_key, p_seed, delta);
  347. }
  348. Error EditorExportPlatform::_save_zip_file(const Ref<EditorExportPreset> &p_preset, void *p_userdata, const String &p_path, const Vector<uint8_t> &p_data, int p_file, int p_total, const Vector<String> &p_enc_in_filters, const Vector<String> &p_enc_ex_filters, const Vector<uint8_t> &p_key, uint64_t p_seed, bool p_delta) {
  349. ERR_FAIL_COND_V_MSG(p_total < 1, ERR_PARAMETER_RANGE_ERROR, "Must select at least one file to export.");
  350. const String path = simplify_path(p_path).replace_first("res://", "");
  351. ZipData *zd = (ZipData *)p_userdata;
  352. zipFile zip = (zipFile)zd->zip;
  353. zipOpenNewFileInZip(zip,
  354. path.utf8().get_data(),
  355. nullptr,
  356. nullptr,
  357. 0,
  358. nullptr,
  359. 0,
  360. nullptr,
  361. Z_DEFLATED,
  362. Z_DEFAULT_COMPRESSION);
  363. zipWriteInFileInZip(zip, p_data.ptr(), p_data.size());
  364. zipCloseFileInZip(zip);
  365. zd->file_count += 1;
  366. if (zd->ep->step(TTR("Storing File:") + " " + p_path, 2 + p_file * 100 / p_total, false)) {
  367. return ERR_SKIP;
  368. }
  369. return OK;
  370. }
  371. Error EditorExportPlatform::_save_zip_patch_file(const Ref<EditorExportPreset> &p_preset, void *p_userdata, const String &p_path, const Vector<uint8_t> &p_data, int p_file, int p_total, const Vector<String> &p_enc_in_filters, const Vector<String> &p_enc_ex_filters, const Vector<uint8_t> &p_key, uint64_t p_seed, bool p_delta) {
  372. Ref<FileAccess> old_file = PackedData::get_singleton()->try_open_path(p_path);
  373. if (old_file.is_valid()) {
  374. Vector<uint8_t> old_data = old_file->get_buffer(old_file->get_length());
  375. // We can't rely on the MD5 as stored in the PCKs, since delta patches could have made it stale.
  376. if (p_data == old_data) {
  377. return OK; // Do nothing if the file hasn't changed.
  378. }
  379. }
  380. return _save_zip_file(p_preset, p_userdata, p_path, p_data, p_file, p_total, p_enc_in_filters, p_enc_ex_filters, p_key, p_seed, p_delta);
  381. }
  382. Ref<Texture2D> EditorExportPlatform::get_option_icon(int p_index) const {
  383. Ref<Theme> theme = EditorNode::get_singleton()->get_editor_theme();
  384. ERR_FAIL_COND_V(theme.is_null(), Ref<Texture2D>());
  385. return theme->get_icon(SNAME("Play"), EditorStringName(EditorIcons));
  386. }
  387. String EditorExportPlatform::find_export_template(const String &template_file_name, String *err) const {
  388. String current_version = GODOT_VERSION_FULL_CONFIG;
  389. String template_path = EditorPaths::get_singleton()->get_export_templates_dir().path_join(current_version).path_join(template_file_name);
  390. if (FileAccess::exists(template_path)) {
  391. return template_path;
  392. }
  393. // Not found
  394. if (err) {
  395. *err += TTR("No export template found at the expected path:") + "\n" + template_path + "\n";
  396. }
  397. return String();
  398. }
  399. bool EditorExportPlatform::exists_export_template(const String &template_file_name, String *err) const {
  400. return find_export_template(template_file_name, err) != "";
  401. }
  402. Ref<EditorExportPreset> EditorExportPlatform::create_preset() {
  403. Ref<EditorExportPreset> preset;
  404. preset.instantiate();
  405. preset->platform = Ref<EditorExportPlatform>(this);
  406. List<ExportOption> options;
  407. get_export_options(&options);
  408. Vector<Ref<EditorExportPlugin>> export_plugins = EditorExport::get_singleton()->get_export_plugins();
  409. for (int i = 0; i < export_plugins.size(); i++) {
  410. export_plugins.write[i]->_get_export_options(Ref<EditorExportPlatform>(this), &options);
  411. }
  412. for (const ExportOption &E : options) {
  413. StringName option_name = E.option.name;
  414. preset->properties[option_name] = E.option;
  415. preset->values[option_name] = E.default_value;
  416. preset->update_visibility[option_name] = E.update_visibility;
  417. }
  418. return preset;
  419. }
  420. void EditorExportPlatform::_export_find_resources(EditorFileSystemDirectory *p_dir, HashSet<String> &p_paths) {
  421. for (int i = 0; i < p_dir->get_subdir_count(); i++) {
  422. _export_find_resources(p_dir->get_subdir(i), p_paths);
  423. }
  424. for (int i = 0; i < p_dir->get_file_count(); i++) {
  425. if (p_dir->get_file_type(i) == "TextFile") {
  426. continue;
  427. }
  428. p_paths.insert(p_dir->get_file_path(i));
  429. }
  430. }
  431. void EditorExportPlatform::_export_find_customized_resources(const Ref<EditorExportPreset> &p_preset, EditorFileSystemDirectory *p_dir, EditorExportPreset::FileExportMode p_mode, HashSet<String> &p_paths) {
  432. for (int i = 0; i < p_dir->get_subdir_count(); i++) {
  433. EditorFileSystemDirectory *subdir = p_dir->get_subdir(i);
  434. _export_find_customized_resources(p_preset, subdir, p_preset->get_file_export_mode(subdir->get_path(), p_mode), p_paths);
  435. }
  436. for (int i = 0; i < p_dir->get_file_count(); i++) {
  437. if (p_dir->get_file_type(i) == "TextFile") {
  438. continue;
  439. }
  440. String path = p_dir->get_file_path(i);
  441. EditorExportPreset::FileExportMode file_mode = p_preset->get_file_export_mode(path, p_mode);
  442. if (file_mode != EditorExportPreset::MODE_FILE_REMOVE) {
  443. p_paths.insert(path);
  444. }
  445. }
  446. }
  447. void EditorExportPlatform::_export_find_dependencies(const String &p_path, HashSet<String> &p_paths) {
  448. if (p_paths.has(p_path)) {
  449. return;
  450. }
  451. p_paths.insert(p_path);
  452. EditorFileSystemDirectory *dir;
  453. int file_idx;
  454. dir = EditorFileSystem::get_singleton()->find_file(p_path, &file_idx);
  455. if (!dir) {
  456. return;
  457. }
  458. Vector<String> deps = dir->get_file_deps(file_idx);
  459. for (int i = 0; i < deps.size(); i++) {
  460. _export_find_dependencies(deps[i], p_paths);
  461. }
  462. }
  463. void EditorExportPlatform::_edit_files_with_filter(Ref<DirAccess> &da, const Vector<String> &p_filters, HashSet<String> &r_list, bool exclude) {
  464. da->list_dir_begin();
  465. String cur_dir = da->get_current_dir().replace_char('\\', '/');
  466. if (!cur_dir.ends_with("/")) {
  467. cur_dir += "/";
  468. }
  469. String cur_dir_no_prefix = cur_dir.replace("res://", "");
  470. Vector<String> dirs;
  471. String f = da->get_next();
  472. while (!f.is_empty()) {
  473. if (da->current_is_dir()) {
  474. dirs.push_back(f);
  475. } else {
  476. String fullpath = cur_dir + f;
  477. // Test also against path without res:// so that filters like `file.txt` can work.
  478. String fullpath_no_prefix = cur_dir_no_prefix + f;
  479. for (int i = 0; i < p_filters.size(); ++i) {
  480. if (fullpath.matchn(p_filters[i]) || fullpath_no_prefix.matchn(p_filters[i])) {
  481. if (!exclude) {
  482. r_list.insert(fullpath);
  483. } else {
  484. r_list.erase(fullpath);
  485. }
  486. }
  487. }
  488. }
  489. f = da->get_next();
  490. }
  491. da->list_dir_end();
  492. for (int i = 0; i < dirs.size(); ++i) {
  493. const String &dir = dirs[i];
  494. if (dir.begins_with(".")) {
  495. continue;
  496. }
  497. if (EditorFileSystem::_should_skip_directory(cur_dir + dir)) {
  498. continue;
  499. }
  500. da->change_dir(dir);
  501. _edit_files_with_filter(da, p_filters, r_list, exclude);
  502. da->change_dir("..");
  503. }
  504. }
  505. void EditorExportPlatform::_edit_filter_list(HashSet<String> &r_list, const String &p_filter, bool exclude) {
  506. if (p_filter.is_empty()) {
  507. return;
  508. }
  509. Vector<String> split = p_filter.split(",");
  510. Vector<String> filters;
  511. for (int i = 0; i < split.size(); i++) {
  512. String f = split[i].strip_edges();
  513. if (f.is_empty()) {
  514. continue;
  515. }
  516. filters.push_back(f);
  517. }
  518. Ref<DirAccess> da = DirAccess::create(DirAccess::ACCESS_RESOURCES);
  519. ERR_FAIL_COND(da.is_null());
  520. _edit_files_with_filter(da, filters, r_list, exclude);
  521. }
  522. HashSet<String> EditorExportPlatform::get_features(const Ref<EditorExportPreset> &p_preset, bool p_debug) const {
  523. Ref<EditorExportPlatform> platform = p_preset->get_platform();
  524. List<String> feature_list;
  525. platform->get_platform_features(&feature_list);
  526. platform->get_preset_features(p_preset, &feature_list);
  527. HashSet<String> result;
  528. for (const String &E : feature_list) {
  529. result.insert(E);
  530. }
  531. result.insert("template");
  532. if (p_debug) {
  533. result.insert("debug");
  534. result.insert("template_debug");
  535. } else {
  536. result.insert("release");
  537. result.insert("template_release");
  538. }
  539. #ifdef REAL_T_IS_DOUBLE
  540. result.insert("double");
  541. #else
  542. result.insert("single");
  543. #endif // REAL_T_IS_DOUBLE
  544. if (!p_preset->get_custom_features().is_empty()) {
  545. Vector<String> tmp_custom_list = p_preset->get_custom_features().split(",");
  546. for (int i = 0; i < tmp_custom_list.size(); i++) {
  547. String f = tmp_custom_list[i].strip_edges();
  548. if (!f.is_empty()) {
  549. result.insert(f);
  550. }
  551. }
  552. }
  553. return result;
  554. }
  555. EditorExportPlatform::ExportNotifier::ExportNotifier(EditorExportPlatform &p_platform, const Ref<EditorExportPreset> &p_preset, bool p_debug, const String &p_path, BitField<EditorExportPlatform::DebugFlags> p_flags) {
  556. HashSet<String> features = p_platform.get_features(p_preset, p_debug);
  557. Vector<Ref<EditorExportPlugin>> export_plugins = EditorExport::get_singleton()->get_export_plugins();
  558. //initial export plugin callback
  559. for (int i = 0; i < export_plugins.size(); i++) {
  560. export_plugins.write[i]->set_export_preset(p_preset);
  561. if (GDVIRTUAL_IS_OVERRIDDEN_PTR(export_plugins[i], _export_begin)) {
  562. PackedStringArray features_psa;
  563. for (const String &feature : features) {
  564. features_psa.push_back(feature);
  565. }
  566. export_plugins.write[i]->_export_begin_script(features_psa, p_debug, p_path, p_flags);
  567. } else {
  568. export_plugins.write[i]->_export_begin(features, p_debug, p_path, p_flags);
  569. }
  570. }
  571. }
  572. EditorExportPlatform::ExportNotifier::~ExportNotifier() {
  573. Vector<Ref<EditorExportPlugin>> export_plugins = EditorExport::get_singleton()->get_export_plugins();
  574. for (int i = 0; i < export_plugins.size(); i++) {
  575. if (GDVIRTUAL_IS_OVERRIDDEN_PTR(export_plugins[i], _export_end)) {
  576. export_plugins.write[i]->_export_end_script();
  577. } else {
  578. export_plugins.write[i]->_export_end();
  579. }
  580. export_plugins.write[i]->_export_end_clear();
  581. export_plugins.write[i]->set_export_preset(Ref<EditorExportPreset>());
  582. }
  583. }
  584. bool EditorExportPlatform::_export_customize_dictionary(Dictionary &dict, LocalVector<Ref<EditorExportPlugin>> &customize_resources_plugins) {
  585. bool changed = false;
  586. for (const Variant &K : dict.get_key_list()) {
  587. Variant v = dict[K];
  588. switch (v.get_type()) {
  589. case Variant::OBJECT: {
  590. Ref<Resource> res = v;
  591. if (res.is_valid()) {
  592. for (Ref<EditorExportPlugin> &plugin : customize_resources_plugins) {
  593. Ref<Resource> new_res = plugin->_customize_resource(res, "");
  594. if (new_res.is_valid()) {
  595. changed = true;
  596. if (new_res != res) {
  597. dict[K] = new_res;
  598. res = new_res;
  599. }
  600. break;
  601. }
  602. }
  603. // If it was not replaced, go through and see if there is something to replace.
  604. if (res.is_valid() && !res->get_path().is_resource_file() && _export_customize_object(res.ptr(), customize_resources_plugins)) {
  605. changed = true;
  606. }
  607. }
  608. } break;
  609. case Variant::DICTIONARY: {
  610. Dictionary d = v;
  611. if (_export_customize_dictionary(d, customize_resources_plugins)) {
  612. changed = true;
  613. }
  614. } break;
  615. case Variant::ARRAY: {
  616. Array a = v;
  617. if (_export_customize_array(a, customize_resources_plugins)) {
  618. changed = true;
  619. }
  620. } break;
  621. default: {
  622. }
  623. }
  624. }
  625. return changed;
  626. }
  627. bool EditorExportPlatform::_export_customize_array(Array &arr, LocalVector<Ref<EditorExportPlugin>> &customize_resources_plugins) {
  628. bool changed = false;
  629. for (int i = 0; i < arr.size(); i++) {
  630. Variant v = arr.get(i);
  631. switch (v.get_type()) {
  632. case Variant::OBJECT: {
  633. Ref<Resource> res = v;
  634. if (res.is_valid()) {
  635. for (Ref<EditorExportPlugin> &plugin : customize_resources_plugins) {
  636. Ref<Resource> new_res = plugin->_customize_resource(res, "");
  637. if (new_res.is_valid()) {
  638. changed = true;
  639. if (new_res != res) {
  640. arr.set(i, new_res);
  641. res = new_res;
  642. }
  643. break;
  644. }
  645. }
  646. // If it was not replaced, go through and see if there is something to replace.
  647. if (res.is_valid() && !res->get_path().is_resource_file() && _export_customize_object(res.ptr(), customize_resources_plugins)) {
  648. changed = true;
  649. }
  650. }
  651. } break;
  652. case Variant::DICTIONARY: {
  653. Dictionary d = v;
  654. if (_export_customize_dictionary(d, customize_resources_plugins)) {
  655. changed = true;
  656. }
  657. } break;
  658. case Variant::ARRAY: {
  659. Array a = v;
  660. if (_export_customize_array(a, customize_resources_plugins)) {
  661. changed = true;
  662. }
  663. } break;
  664. default: {
  665. }
  666. }
  667. }
  668. return changed;
  669. }
  670. bool EditorExportPlatform::_export_customize_object(Object *p_object, LocalVector<Ref<EditorExportPlugin>> &customize_resources_plugins) {
  671. bool changed = false;
  672. List<PropertyInfo> props;
  673. p_object->get_property_list(&props);
  674. for (const PropertyInfo &E : props) {
  675. switch (E.type) {
  676. case Variant::OBJECT: {
  677. Ref<Resource> res = p_object->get(E.name);
  678. if (res.is_valid()) {
  679. for (Ref<EditorExportPlugin> &plugin : customize_resources_plugins) {
  680. Ref<Resource> new_res = plugin->_customize_resource(res, "");
  681. if (new_res.is_valid()) {
  682. changed = true;
  683. if (new_res != res) {
  684. p_object->set(E.name, new_res);
  685. res = new_res;
  686. }
  687. break;
  688. }
  689. }
  690. // If it was not replaced, go through and see if there is something to replace.
  691. if (res.is_valid() && !res->get_path().is_resource_file() && _export_customize_object(res.ptr(), customize_resources_plugins)) {
  692. changed = true;
  693. }
  694. }
  695. } break;
  696. case Variant::DICTIONARY: {
  697. Dictionary d = p_object->get(E.name);
  698. if (_export_customize_dictionary(d, customize_resources_plugins)) {
  699. if (p_object->get(E.name) != d) {
  700. p_object->set(E.name, d);
  701. }
  702. changed = true;
  703. }
  704. } break;
  705. case Variant::ARRAY: {
  706. Array a = p_object->get(E.name);
  707. if (_export_customize_array(a, customize_resources_plugins)) {
  708. if (p_object->get(E.name) != a) {
  709. p_object->set(E.name, a);
  710. }
  711. changed = true;
  712. }
  713. } break;
  714. default: {
  715. }
  716. }
  717. }
  718. return changed;
  719. }
  720. bool EditorExportPlatform::_is_editable_ancestor(Node *p_root, Node *p_node) {
  721. while (p_node != nullptr && p_node != p_root) {
  722. if (p_root->is_editable_instance(p_node)) {
  723. return true;
  724. }
  725. p_node = p_node->get_owner();
  726. }
  727. return false;
  728. }
  729. bool EditorExportPlatform::_export_customize_scene_resources(Node *p_root, Node *p_node, LocalVector<Ref<EditorExportPlugin>> &customize_resources_plugins) {
  730. bool changed = false;
  731. if (p_root == p_node || p_node->get_owner() == p_root || _is_editable_ancestor(p_root, p_node)) {
  732. if (_export_customize_object(p_node, customize_resources_plugins)) {
  733. changed = true;
  734. }
  735. }
  736. for (int i = 0; i < p_node->get_child_count(); i++) {
  737. if (_export_customize_scene_resources(p_root, p_node->get_child(i), customize_resources_plugins)) {
  738. changed = true;
  739. }
  740. }
  741. return changed;
  742. }
  743. String EditorExportPlatform::_export_customize(const String &p_path, LocalVector<Ref<EditorExportPlugin>> &customize_resources_plugins, LocalVector<Ref<EditorExportPlugin>> &customize_scenes_plugins, HashMap<String, FileExportCache> &export_cache, const String &export_base_path, bool p_force_save) {
  744. if (!p_force_save && customize_resources_plugins.is_empty() && customize_scenes_plugins.is_empty()) {
  745. return p_path; // do none
  746. }
  747. // Check if a cache exists
  748. if (export_cache.has(p_path)) {
  749. FileExportCache &fec = export_cache[p_path];
  750. if (fec.saved_path.is_empty() || FileAccess::exists(fec.saved_path)) {
  751. // Destination file exists (was not erased) or not needed
  752. uint64_t mod_time = FileAccess::get_modified_time(p_path);
  753. if (fec.source_modified_time == mod_time) {
  754. // Cached (modified time matches).
  755. fec.used = true;
  756. return fec.saved_path.is_empty() ? p_path : fec.saved_path;
  757. }
  758. String md5 = FileAccess::get_md5(p_path);
  759. if (FileAccess::exists(p_path + ".import")) {
  760. // Also consider the import file in the string
  761. md5 += FileAccess::get_md5(p_path + ".import");
  762. }
  763. if (fec.source_md5 == md5) {
  764. // Cached (md5 matches).
  765. fec.source_modified_time = mod_time;
  766. fec.used = true;
  767. return fec.saved_path.is_empty() ? p_path : fec.saved_path;
  768. }
  769. }
  770. }
  771. FileExportCache fec;
  772. fec.used = true;
  773. fec.source_modified_time = FileAccess::get_modified_time(p_path);
  774. String md5 = FileAccess::get_md5(p_path);
  775. if (FileAccess::exists(p_path + ".import")) {
  776. // Also consider the import file in the string
  777. md5 += FileAccess::get_md5(p_path + ".import");
  778. }
  779. fec.source_md5 = md5;
  780. // Check if it should convert
  781. String type = ResourceLoader::get_resource_type(p_path);
  782. bool modified = false;
  783. String save_path;
  784. if (type == "PackedScene") { // Its a scene.
  785. Ref<PackedScene> ps = ResourceLoader::load(p_path, "PackedScene", ResourceFormatLoader::CACHE_MODE_IGNORE);
  786. ERR_FAIL_COND_V(ps.is_null(), p_path);
  787. Node *node = ps->instantiate(PackedScene::GEN_EDIT_STATE_INSTANCE); // Make sure the child scene root gets the correct inheritance chain.
  788. ERR_FAIL_NULL_V(node, p_path);
  789. if (!customize_scenes_plugins.is_empty()) {
  790. for (Ref<EditorExportPlugin> &plugin : customize_scenes_plugins) {
  791. Node *customized = plugin->_customize_scene(node, p_path);
  792. if (customized != nullptr) {
  793. node = customized;
  794. modified = true;
  795. }
  796. }
  797. }
  798. if (!customize_resources_plugins.is_empty()) {
  799. if (_export_customize_scene_resources(node, node, customize_resources_plugins)) {
  800. modified = true;
  801. }
  802. }
  803. if (modified || p_force_save) {
  804. // If modified, save it again. This is also used for TSCN -> SCN conversion on export.
  805. String base_file = p_path.get_file().get_basename() + ".scn"; // use SCN for saving (binary) and repack (If conversting, TSCN PackedScene representation is inefficient, so repacking is also desired).
  806. save_path = export_base_path.path_join("export-" + p_path.md5_text() + "-" + base_file);
  807. Ref<PackedScene> s;
  808. s.instantiate();
  809. s->pack(node);
  810. Error err = ResourceSaver::save(s, save_path);
  811. ERR_FAIL_COND_V_MSG(err != OK, p_path, "Unable to save export scene file to: " + save_path);
  812. }
  813. node->queue_free();
  814. } else {
  815. Ref<Resource> res = ResourceLoader::load(p_path, "", ResourceFormatLoader::CACHE_MODE_IGNORE);
  816. ERR_FAIL_COND_V(res.is_null(), p_path);
  817. if (!customize_resources_plugins.is_empty()) {
  818. for (Ref<EditorExportPlugin> &plugin : customize_resources_plugins) {
  819. Ref<Resource> new_res = plugin->_customize_resource(res, p_path);
  820. if (new_res.is_valid()) {
  821. modified = true;
  822. if (new_res != res) {
  823. res = new_res;
  824. }
  825. break;
  826. }
  827. }
  828. if (_export_customize_object(res.ptr(), customize_resources_plugins)) {
  829. modified = true;
  830. }
  831. }
  832. if (modified || p_force_save) {
  833. // If modified, save it again. This is also used for TRES -> RES conversion on export.
  834. String base_file = p_path.get_file().get_basename() + ".res"; // use RES for saving (binary)
  835. save_path = export_base_path.path_join("export-" + p_path.md5_text() + "-" + base_file);
  836. Error err = ResourceSaver::save(res, save_path);
  837. ERR_FAIL_COND_V_MSG(err != OK, p_path, "Unable to save export resource file to: " + save_path);
  838. }
  839. }
  840. fec.saved_path = save_path;
  841. export_cache[p_path] = fec;
  842. return save_path.is_empty() ? p_path : save_path;
  843. }
  844. String EditorExportPlatform::_get_script_encryption_key(const Ref<EditorExportPreset> &p_preset) const {
  845. const String from_env = OS::get_singleton()->get_environment(ENV_SCRIPT_ENCRYPTION_KEY);
  846. if (!from_env.is_empty()) {
  847. return from_env.to_lower();
  848. }
  849. return p_preset->get_script_encryption_key().to_lower();
  850. }
  851. Dictionary EditorExportPlatform::get_internal_export_files(const Ref<EditorExportPreset> &p_preset, bool p_debug) {
  852. Dictionary files;
  853. // Text server support data.
  854. if (TS->has_feature(TextServer::FEATURE_USE_SUPPORT_DATA)) {
  855. bool include_data = (bool)get_project_setting(p_preset, "internationalization/locale/include_text_server_data");
  856. if (!include_data) {
  857. Vector<String> translations = get_project_setting(p_preset, "internationalization/locale/translations");
  858. translations.push_back(get_project_setting(p_preset, "internationalization/locale/fallback"));
  859. for (const String &t : translations) {
  860. if (TS->is_locale_using_support_data(t)) {
  861. include_data = true;
  862. break;
  863. }
  864. }
  865. }
  866. if (include_data) {
  867. String ts_name = TS->get_support_data_filename();
  868. String ts_target = "res://" + ts_name;
  869. if (!ts_name.is_empty()) {
  870. bool export_ok = false;
  871. if (FileAccess::exists(ts_target)) { // Include user supplied data file.
  872. const PackedByteArray &ts_data = FileAccess::get_file_as_bytes(ts_target);
  873. if (!ts_data.is_empty()) {
  874. add_message(EXPORT_MESSAGE_INFO, TTR("Export"), TTR("Using user provided text server data, text display in the exported project might be broken if export template was built with different ICU version!"));
  875. files[ts_target] = ts_data;
  876. export_ok = true;
  877. }
  878. } else {
  879. String current_version = GODOT_VERSION_FULL_CONFIG;
  880. String template_path = EditorPaths::get_singleton()->get_export_templates_dir().path_join(current_version);
  881. if (p_debug && p_preset->has("custom_template/debug") && p_preset->get("custom_template/debug") != "") {
  882. template_path = p_preset->get("custom_template/debug").operator String().get_base_dir();
  883. } else if (!p_debug && p_preset->has("custom_template/release") && p_preset->get("custom_template/release") != "") {
  884. template_path = p_preset->get("custom_template/release").operator String().get_base_dir();
  885. }
  886. String data_file_name = template_path.path_join(ts_name);
  887. if (FileAccess::exists(data_file_name)) {
  888. const PackedByteArray &ts_data = FileAccess::get_file_as_bytes(data_file_name);
  889. if (!ts_data.is_empty()) {
  890. print_line("Using text server data from export templates.");
  891. files[ts_target] = ts_data;
  892. export_ok = true;
  893. }
  894. } else {
  895. const PackedByteArray &ts_data = TS->get_support_data();
  896. if (!ts_data.is_empty()) {
  897. add_message(EXPORT_MESSAGE_INFO, TTR("Export"), TTR("Using editor embedded text server data, text display in the exported project might be broken if export template was built with different ICU version!"));
  898. files[ts_target] = ts_data;
  899. export_ok = true;
  900. }
  901. }
  902. }
  903. if (!export_ok) {
  904. add_message(EXPORT_MESSAGE_WARNING, TTR("Export"), TTR("Missing text server data, text display in the exported project might be broken!"));
  905. }
  906. }
  907. }
  908. }
  909. return files;
  910. }
  911. Vector<String> EditorExportPlatform::get_forced_export_files(const Ref<EditorExportPreset> &p_preset) {
  912. Vector<String> files;
  913. files.push_back(ProjectSettings::get_singleton()->get_global_class_list_path());
  914. String icon = ResourceUID::ensure_path(get_project_setting(p_preset, "application/config/icon"));
  915. String splash = ResourceUID::ensure_path(get_project_setting(p_preset, "application/boot_splash/image"));
  916. if (!icon.is_empty() && FileAccess::exists(icon)) {
  917. files.push_back(icon);
  918. }
  919. if (!splash.is_empty() && FileAccess::exists(splash) && icon != splash) {
  920. files.push_back(splash);
  921. }
  922. String resource_cache_file = ResourceUID::get_cache_file();
  923. if (FileAccess::exists(resource_cache_file)) {
  924. files.push_back(resource_cache_file);
  925. }
  926. String extension_list_config_file = GDExtension::get_extension_list_config_file();
  927. if (FileAccess::exists(extension_list_config_file)) {
  928. files.push_back(extension_list_config_file);
  929. }
  930. return files;
  931. }
  932. Error EditorExportPlatform::_script_save_file(const Ref<EditorExportPreset> &p_preset, void *p_userdata, const String &p_path, const Vector<uint8_t> &p_data, int p_file, int p_total, const Vector<String> &p_enc_in_filters, const Vector<String> &p_enc_ex_filters, const Vector<uint8_t> &p_key, uint64_t p_seed, bool p_delta) {
  933. Callable cb = ((ScriptCallbackData *)p_userdata)->file_cb;
  934. ERR_FAIL_COND_V(!cb.is_valid(), FAILED);
  935. const String simplified_path = simplify_path(p_path);
  936. Variant path = simplified_path;
  937. Variant data = p_data;
  938. Variant file = p_file;
  939. Variant total = p_total;
  940. Variant enc_in = p_enc_in_filters;
  941. Variant enc_ex = p_enc_ex_filters;
  942. Variant enc_key = p_key;
  943. Variant ret;
  944. Callable::CallError ce;
  945. const Variant *args[7] = { &path, &data, &file, &total, &enc_in, &enc_ex, &enc_key };
  946. cb.callp(args, 7, ret, ce);
  947. ERR_FAIL_COND_V_MSG(ce.error != Callable::CallError::CALL_OK, FAILED, vformat("Failed to execute file save callback: %s.", Variant::get_callable_error_text(cb, args, 7, ce)));
  948. return (Error)ret.operator int();
  949. }
  950. Error EditorExportPlatform::_script_add_shared_object(const Ref<EditorExportPreset> &p_preset, void *p_userdata, const SharedObject &p_so) {
  951. Callable cb = ((ScriptCallbackData *)p_userdata)->so_cb;
  952. if (!cb.is_valid()) {
  953. return OK; // Optional.
  954. }
  955. Variant path = p_so.path;
  956. Variant tags = p_so.tags;
  957. Variant target = p_so.target;
  958. Variant ret;
  959. Callable::CallError ce;
  960. const Variant *args[3] = { &path, &tags, &target };
  961. cb.callp(args, 3, ret, ce);
  962. ERR_FAIL_COND_V_MSG(ce.error != Callable::CallError::CALL_OK, FAILED, vformat("Failed to execute shared object save callback: %s.", Variant::get_callable_error_text(cb, args, 3, ce)));
  963. return (Error)ret.operator int();
  964. }
  965. Error EditorExportPlatform::_export_project_files(const Ref<EditorExportPreset> &p_preset, bool p_debug, const Callable &p_save_func, const Callable &p_so_func) {
  966. ScriptCallbackData data;
  967. data.file_cb = p_save_func;
  968. data.so_cb = p_so_func;
  969. return export_project_files(p_preset, p_debug, _script_save_file, nullptr, &data, _script_add_shared_object);
  970. }
  971. Error EditorExportPlatform::export_project_files(const Ref<EditorExportPreset> &p_preset, bool p_debug, EditorExportSaveFunction p_save_func, EditorExportRemoveFunction p_remove_func, void *p_udata, EditorExportSaveSharedObject p_so_func) {
  972. //figure out paths of files that will be exported
  973. HashSet<String> paths;
  974. Vector<String> path_remaps;
  975. if (p_preset->get_export_filter() == EditorExportPreset::EXPORT_ALL_RESOURCES) {
  976. //find stuff
  977. _export_find_resources(EditorFileSystem::get_singleton()->get_filesystem(), paths);
  978. } else if (p_preset->get_export_filter() == EditorExportPreset::EXCLUDE_SELECTED_RESOURCES) {
  979. _export_find_resources(EditorFileSystem::get_singleton()->get_filesystem(), paths);
  980. Vector<String> files = p_preset->get_files_to_export();
  981. for (int i = 0; i < files.size(); i++) {
  982. paths.erase(files[i]);
  983. }
  984. } else if (p_preset->get_export_filter() == EditorExportPreset::EXPORT_CUSTOMIZED) {
  985. _export_find_customized_resources(p_preset, EditorFileSystem::get_singleton()->get_filesystem(), p_preset->get_file_export_mode("res://"), paths);
  986. } else {
  987. bool scenes_only = p_preset->get_export_filter() == EditorExportPreset::EXPORT_SELECTED_SCENES;
  988. Vector<String> files = p_preset->get_files_to_export();
  989. for (int i = 0; i < files.size(); i++) {
  990. if (scenes_only && ResourceLoader::get_resource_type(files[i]) != "PackedScene") {
  991. continue;
  992. }
  993. _export_find_dependencies(files[i], paths);
  994. }
  995. // Add autoload resources and their dependencies
  996. List<PropertyInfo> props;
  997. ProjectSettings::get_singleton()->get_property_list(&props);
  998. for (const PropertyInfo &pi : props) {
  999. if (!pi.name.begins_with("autoload/")) {
  1000. continue;
  1001. }
  1002. String autoload_path = get_project_setting(p_preset, pi.name);
  1003. if (autoload_path.begins_with("*")) {
  1004. autoload_path = autoload_path.substr(1);
  1005. }
  1006. _export_find_dependencies(autoload_path, paths);
  1007. }
  1008. }
  1009. //add native icons to non-resource include list
  1010. _edit_filter_list(paths, String("*.icns"), false);
  1011. _edit_filter_list(paths, String("*.ico"), false);
  1012. _edit_filter_list(paths, p_preset->get_include_filter(), false);
  1013. _edit_filter_list(paths, p_preset->get_exclude_filter(), true);
  1014. // Ignore import files, since these are automatically added to the jar later with the resources
  1015. _edit_filter_list(paths, String("*.import"), true);
  1016. // Get encryption filters.
  1017. bool enc_pck = p_preset->get_enc_pck();
  1018. Vector<String> enc_in_filters;
  1019. Vector<String> enc_ex_filters;
  1020. Vector<uint8_t> key;
  1021. uint64_t seed = 0;
  1022. if (enc_pck) {
  1023. seed = p_preset->get_seed();
  1024. Vector<String> enc_in_split = p_preset->get_enc_in_filter().split(",");
  1025. for (int i = 0; i < enc_in_split.size(); i++) {
  1026. String f = enc_in_split[i].strip_edges();
  1027. if (f.is_empty()) {
  1028. continue;
  1029. }
  1030. enc_in_filters.push_back(f);
  1031. }
  1032. Vector<String> enc_ex_split = p_preset->get_enc_ex_filter().split(",");
  1033. for (int i = 0; i < enc_ex_split.size(); i++) {
  1034. String f = enc_ex_split[i].strip_edges();
  1035. if (f.is_empty()) {
  1036. continue;
  1037. }
  1038. enc_ex_filters.push_back(f);
  1039. }
  1040. // Get encryption key.
  1041. String script_key = _get_script_encryption_key(p_preset);
  1042. key.resize(32);
  1043. if (script_key.length() == 64) {
  1044. for (int i = 0; i < 32; i++) {
  1045. int v = 0;
  1046. if (i * 2 < script_key.length()) {
  1047. char32_t ct = script_key[i * 2];
  1048. if (is_digit(ct)) {
  1049. ct = ct - '0';
  1050. } else if (ct >= 'a' && ct <= 'f') {
  1051. ct = 10 + ct - 'a';
  1052. }
  1053. v |= ct << 4;
  1054. }
  1055. if (i * 2 + 1 < script_key.length()) {
  1056. char32_t ct = script_key[i * 2 + 1];
  1057. if (is_digit(ct)) {
  1058. ct = ct - '0';
  1059. } else if (ct >= 'a' && ct <= 'f') {
  1060. ct = 10 + ct - 'a';
  1061. }
  1062. v |= ct;
  1063. }
  1064. key.write[i] = v;
  1065. }
  1066. }
  1067. }
  1068. EditorExportSaveProxy save_proxy(p_save_func, p_remove_func != nullptr);
  1069. Error err = OK;
  1070. Vector<Ref<EditorExportPlugin>> export_plugins = EditorExport::get_singleton()->get_export_plugins();
  1071. struct SortByName {
  1072. bool operator()(const Ref<EditorExportPlugin> &left, const Ref<EditorExportPlugin> &right) const {
  1073. return left->get_name() < right->get_name();
  1074. }
  1075. };
  1076. auto add_shared_objects_and_extra_files_from_export_plugins = [&]() {
  1077. for (int i = 0; i < export_plugins.size(); i++) {
  1078. if (p_so_func) {
  1079. for (int j = 0; j < export_plugins[i]->shared_objects.size(); j++) {
  1080. err = p_so_func(p_preset, p_udata, export_plugins[i]->shared_objects[j]);
  1081. if (err != OK) {
  1082. return err;
  1083. }
  1084. }
  1085. }
  1086. for (int j = 0; j < export_plugins[i]->extra_files.size(); j++) {
  1087. err = save_proxy.save_file(p_preset, p_udata, export_plugins[i]->extra_files[j].path, export_plugins[i]->extra_files[j].data, 0, paths.size(), enc_in_filters, enc_ex_filters, key, seed, false);
  1088. if (err != OK) {
  1089. return err;
  1090. }
  1091. }
  1092. export_plugins.write[i]->_clear();
  1093. }
  1094. return OK;
  1095. };
  1096. // Always sort by name, to so if for some reason they are re-arranged, it still works.
  1097. export_plugins.sort_custom<SortByName>();
  1098. HashSet<String> features = get_features(p_preset, p_debug);
  1099. PackedStringArray features_psa;
  1100. for (const String &feature : features) {
  1101. features_psa.push_back(feature);
  1102. }
  1103. // Check if custom processing is needed
  1104. uint32_t custom_resources_hash = HASH_MURMUR3_SEED;
  1105. uint32_t custom_scene_hash = HASH_MURMUR3_SEED;
  1106. LocalVector<Ref<EditorExportPlugin>> customize_resources_plugins;
  1107. LocalVector<Ref<EditorExportPlugin>> customize_scenes_plugins;
  1108. for (int i = 0; i < export_plugins.size(); i++) {
  1109. if (export_plugins.write[i]->_begin_customize_resources(Ref<EditorExportPlatform>(this), features_psa)) {
  1110. customize_resources_plugins.push_back(export_plugins[i]);
  1111. custom_resources_hash = hash_murmur3_one_64(export_plugins[i]->get_name().hash64(), custom_resources_hash);
  1112. uint64_t hash = export_plugins[i]->_get_customization_configuration_hash();
  1113. custom_resources_hash = hash_murmur3_one_64(hash, custom_resources_hash);
  1114. }
  1115. if (export_plugins.write[i]->_begin_customize_scenes(Ref<EditorExportPlatform>(this), features_psa)) {
  1116. customize_scenes_plugins.push_back(export_plugins[i]);
  1117. custom_resources_hash = hash_murmur3_one_64(export_plugins[i]->get_name().hash64(), custom_resources_hash);
  1118. uint64_t hash = export_plugins[i]->_get_customization_configuration_hash();
  1119. custom_scene_hash = hash_murmur3_one_64(hash, custom_scene_hash);
  1120. }
  1121. }
  1122. // Add any files that might've been defined during the initial steps of the export plugins.
  1123. err = add_shared_objects_and_extra_files_from_export_plugins();
  1124. if (err != OK) {
  1125. return err;
  1126. }
  1127. HashMap<String, FileExportCache> export_cache;
  1128. String export_base_path = ProjectSettings::get_singleton()->get_project_data_path().path_join("exported/") + itos(custom_resources_hash);
  1129. bool convert_text_to_binary = get_project_setting(p_preset, "editor/export/convert_text_resources_to_binary");
  1130. if (convert_text_to_binary || !customize_resources_plugins.is_empty() || !customize_scenes_plugins.is_empty()) {
  1131. // See if we have something to open
  1132. Ref<FileAccess> f = FileAccess::open(export_base_path.path_join("file_cache"), FileAccess::READ);
  1133. if (f.is_valid()) {
  1134. String l = f->get_line();
  1135. while (l != String()) {
  1136. Vector<String> fields = l.split("::");
  1137. if (fields.size() == 4) {
  1138. FileExportCache fec;
  1139. const String &path = fields[0];
  1140. fec.source_md5 = fields[1].strip_edges();
  1141. fec.source_modified_time = fields[2].strip_edges().to_int();
  1142. fec.saved_path = fields[3];
  1143. fec.used = false; // Assume unused until used.
  1144. export_cache[path] = fec;
  1145. }
  1146. l = f->get_line();
  1147. }
  1148. } else {
  1149. // create the path
  1150. Ref<DirAccess> d = DirAccess::create(DirAccess::ACCESS_RESOURCES);
  1151. d->change_dir(ProjectSettings::get_singleton()->get_project_data_path());
  1152. d->make_dir_recursive("exported/" + itos(custom_resources_hash));
  1153. }
  1154. }
  1155. for (int i = 0; i < export_plugins.size(); i++) {
  1156. export_plugins.write[i]->set_export_base_path(export_base_path);
  1157. }
  1158. //store everything in the export medium
  1159. int total = paths.size();
  1160. // idx is incremented at the beginning of the paths loop to easily allow
  1161. // for continue statements without accidentally skipping an increment.
  1162. int idx = total > 0 ? -1 : 0;
  1163. for (const String &path : paths) {
  1164. idx++;
  1165. String type = ResourceLoader::get_resource_type(path);
  1166. bool has_import_file = FileAccess::exists(path + ".import");
  1167. Ref<ConfigFile> config;
  1168. if (has_import_file) {
  1169. config.instantiate();
  1170. err = config->load(path + ".import");
  1171. if (err != OK) {
  1172. ERR_PRINT("Could not parse: '" + path + "', not exported.");
  1173. continue;
  1174. }
  1175. String importer_type = config->get_value("remap", "importer");
  1176. if (importer_type == "skip") {
  1177. // Skip file.
  1178. continue;
  1179. }
  1180. }
  1181. bool do_export = true;
  1182. for (int i = 0; i < export_plugins.size(); i++) {
  1183. if (GDVIRTUAL_IS_OVERRIDDEN_PTR(export_plugins[i], _export_file)) {
  1184. export_plugins.write[i]->_export_file_script(path, type, features_psa);
  1185. } else {
  1186. export_plugins.write[i]->_export_file(path, type, features);
  1187. }
  1188. if (p_so_func) {
  1189. for (int j = 0; j < export_plugins[i]->shared_objects.size(); j++) {
  1190. err = p_so_func(p_preset, p_udata, export_plugins[i]->shared_objects[j]);
  1191. if (err != OK) {
  1192. return err;
  1193. }
  1194. }
  1195. }
  1196. for (int j = 0; j < export_plugins[i]->extra_files.size(); j++) {
  1197. err = save_proxy.save_file(p_preset, p_udata, export_plugins[i]->extra_files[j].path, export_plugins[i]->extra_files[j].data, idx, total, enc_in_filters, enc_ex_filters, key, seed, false);
  1198. if (err != OK) {
  1199. return err;
  1200. }
  1201. if (export_plugins[i]->extra_files[j].remap) {
  1202. do_export = false; // If remap, do not.
  1203. path_remaps.push_back(path);
  1204. path_remaps.push_back(export_plugins[i]->extra_files[j].path);
  1205. }
  1206. }
  1207. if (export_plugins[i]->skipped) {
  1208. do_export = false;
  1209. }
  1210. export_plugins.write[i]->_clear();
  1211. if (!do_export) {
  1212. break;
  1213. }
  1214. }
  1215. if (!do_export) {
  1216. continue;
  1217. }
  1218. if (has_import_file) {
  1219. String importer_type = config->get_value("remap", "importer");
  1220. if (importer_type == "keep") {
  1221. // Just keep file as-is.
  1222. Vector<uint8_t> array = FileAccess::get_file_as_bytes(path);
  1223. err = save_proxy.save_file(p_preset, p_udata, path, array, idx, total, enc_in_filters, enc_ex_filters, key, seed, false);
  1224. if (err != OK) {
  1225. return err;
  1226. }
  1227. continue;
  1228. }
  1229. // Before doing this, try to see if it can be customized.
  1230. String export_path = _export_customize(path, customize_resources_plugins, customize_scenes_plugins, export_cache, export_base_path, false);
  1231. if (export_path != path) {
  1232. // It was actually customized.
  1233. // Since the original file is likely not recognized, just use the import system.
  1234. config->set_value("remap", "type", ResourceLoader::get_resource_type(export_path));
  1235. // Erase all Paths.
  1236. Vector<String> keys = config->get_section_keys("remap");
  1237. for (const String &K : keys) {
  1238. if (K.begins_with("path")) {
  1239. config->erase_section_key("remap", K);
  1240. }
  1241. }
  1242. // Set actual converted path.
  1243. config->set_value("remap", "path", export_path);
  1244. // Erase useless sections.
  1245. if (config->has_section("deps")) {
  1246. config->erase_section("deps");
  1247. }
  1248. if (config->has_section("params")) {
  1249. config->erase_section("params");
  1250. }
  1251. String import_text = config->encode_to_text();
  1252. CharString cs = import_text.utf8();
  1253. Vector<uint8_t> sarr;
  1254. sarr.resize(cs.size());
  1255. memcpy(sarr.ptrw(), cs.ptr(), sarr.size());
  1256. err = save_proxy.save_file(p_preset, p_udata, path + ".import", sarr, idx, total, enc_in_filters, enc_ex_filters, key, seed, false);
  1257. if (err != OK) {
  1258. return err;
  1259. }
  1260. // Now actual remapped file:
  1261. sarr = FileAccess::get_file_as_bytes(export_path);
  1262. err = save_proxy.save_file(p_preset, p_udata, export_path, sarr, idx, total, enc_in_filters, enc_ex_filters, key, seed, false);
  1263. if (err != OK) {
  1264. return err;
  1265. }
  1266. } else {
  1267. // File is imported and not customized, replace by what it imports.
  1268. Vector<String> remaps = config->get_section_keys("remap");
  1269. HashSet<String> remap_features;
  1270. for (const String &F : remaps) {
  1271. String remap = F;
  1272. String feature = remap.get_slicec('.', 1);
  1273. if (features.has(feature)) {
  1274. remap_features.insert(feature);
  1275. }
  1276. }
  1277. if (remap_features.size() > 1) {
  1278. resolve_platform_feature_priorities(p_preset, remap_features);
  1279. }
  1280. err = OK;
  1281. for (const String &F : remaps) {
  1282. String remap = F;
  1283. if (remap == "path") {
  1284. String remapped_path = config->get_value("remap", remap);
  1285. Vector<uint8_t> array = FileAccess::get_file_as_bytes(remapped_path);
  1286. err = save_proxy.save_file(p_preset, p_udata, remapped_path, array, idx, total, enc_in_filters, enc_ex_filters, key, seed, false);
  1287. } else if (remap.begins_with("path.")) {
  1288. String feature = remap.get_slicec('.', 1);
  1289. if (remap_features.has(feature)) {
  1290. String remapped_path = config->get_value("remap", remap);
  1291. Vector<uint8_t> array = FileAccess::get_file_as_bytes(remapped_path);
  1292. err = save_proxy.save_file(p_preset, p_udata, remapped_path, array, idx, total, enc_in_filters, enc_ex_filters, key, seed, false);
  1293. } else {
  1294. // Remove paths if feature not enabled.
  1295. config->erase_section_key("remap", remap);
  1296. }
  1297. }
  1298. }
  1299. if (err != OK) {
  1300. return err;
  1301. }
  1302. // Erase useless sections.
  1303. if (config->has_section("deps")) {
  1304. config->erase_section("deps");
  1305. }
  1306. if (config->has_section("params")) {
  1307. config->erase_section("params");
  1308. }
  1309. String import_text = config->encode_to_text();
  1310. CharString cs = import_text.utf8();
  1311. Vector<uint8_t> sarr;
  1312. sarr.resize(cs.size());
  1313. memcpy(sarr.ptrw(), cs.ptr(), sarr.size());
  1314. err = save_proxy.save_file(p_preset, p_udata, path + ".import", sarr, idx, total, enc_in_filters, enc_ex_filters, key, seed, false);
  1315. if (err != OK) {
  1316. return err;
  1317. }
  1318. }
  1319. } else {
  1320. // Just store it as it comes.
  1321. // Customization only happens if plugins did not take care of it before.
  1322. bool force_binary = convert_text_to_binary && (path.has_extension("tres") || path.has_extension("tscn"));
  1323. String export_path = _export_customize(path, customize_resources_plugins, customize_scenes_plugins, export_cache, export_base_path, force_binary);
  1324. if (export_path != path) {
  1325. // Add a remap entry.
  1326. path_remaps.push_back(path);
  1327. path_remaps.push_back(export_path);
  1328. }
  1329. Vector<uint8_t> array = FileAccess::get_file_as_bytes(export_path);
  1330. err = save_proxy.save_file(p_preset, p_udata, export_path, array, idx, total, enc_in_filters, enc_ex_filters, key, seed, false);
  1331. if (err != OK) {
  1332. return err;
  1333. }
  1334. }
  1335. }
  1336. if (convert_text_to_binary || !customize_resources_plugins.is_empty() || !customize_scenes_plugins.is_empty()) {
  1337. // End scene customization
  1338. String fcache = export_base_path.path_join("file_cache");
  1339. Ref<FileAccess> f = FileAccess::open(fcache, FileAccess::WRITE);
  1340. if (f.is_valid()) {
  1341. for (const KeyValue<String, FileExportCache> &E : export_cache) {
  1342. if (E.value.used) { // May be old, unused
  1343. String l = E.key + "::" + E.value.source_md5 + "::" + itos(E.value.source_modified_time) + "::" + E.value.saved_path;
  1344. f->store_line(l);
  1345. }
  1346. }
  1347. } else {
  1348. ERR_PRINT("Error opening export file cache: " + fcache);
  1349. }
  1350. for (Ref<EditorExportPlugin> &plugin : customize_resources_plugins) {
  1351. plugin->_end_customize_resources();
  1352. }
  1353. for (Ref<EditorExportPlugin> &plugin : customize_scenes_plugins) {
  1354. plugin->_end_customize_scenes();
  1355. }
  1356. }
  1357. // Add any files that might've been defined during the final steps of the export plugins.
  1358. err = add_shared_objects_and_extra_files_from_export_plugins();
  1359. if (err != OK) {
  1360. return err;
  1361. }
  1362. //save config!
  1363. Vector<String> custom_list;
  1364. if (!p_preset->get_custom_features().is_empty()) {
  1365. Vector<String> tmp_custom_list = p_preset->get_custom_features().split(",");
  1366. for (int i = 0; i < tmp_custom_list.size(); i++) {
  1367. String f = tmp_custom_list[i].strip_edges();
  1368. if (!f.is_empty()) {
  1369. custom_list.push_back(f);
  1370. }
  1371. }
  1372. }
  1373. for (int i = 0; i < export_plugins.size(); i++) {
  1374. custom_list.append_array(export_plugins[i]->_get_export_features(Ref<EditorExportPlatform>(this), p_debug));
  1375. }
  1376. if (path_remaps.size()) {
  1377. for (int i = 0; i < path_remaps.size(); i += 2) {
  1378. const String &from = path_remaps[i];
  1379. const String &to = path_remaps[i + 1];
  1380. String remap_file = "[remap]\n\npath=\"" + to.c_escape() + "\"\n";
  1381. CharString utf8 = remap_file.utf8();
  1382. Vector<uint8_t> new_file;
  1383. new_file.resize(utf8.length());
  1384. for (int j = 0; j < utf8.length(); j++) {
  1385. new_file.write[j] = utf8[j];
  1386. }
  1387. err = save_proxy.save_file(p_preset, p_udata, from + ".remap", new_file, idx, total, enc_in_filters, enc_ex_filters, key, seed, false);
  1388. if (err != OK) {
  1389. return err;
  1390. }
  1391. }
  1392. }
  1393. Vector<String> forced_export = get_forced_export_files(p_preset);
  1394. for (int i = 0; i < forced_export.size(); i++) {
  1395. Vector<uint8_t> array;
  1396. if (GDExtension::get_extension_list_config_file() == forced_export[i]) {
  1397. array = _filter_extension_list_config_file(forced_export[i], paths);
  1398. if (array.is_empty()) {
  1399. continue;
  1400. }
  1401. } else {
  1402. array = FileAccess::get_file_as_bytes(forced_export[i]);
  1403. }
  1404. err = save_proxy.save_file(p_preset, p_udata, forced_export[i], array, idx, total, enc_in_filters, enc_ex_filters, key, seed, false);
  1405. if (err != OK) {
  1406. return err;
  1407. }
  1408. }
  1409. Dictionary int_export = get_internal_export_files(p_preset, p_debug);
  1410. for (const KeyValue<Variant, Variant> &int_export_kv : int_export) {
  1411. const PackedByteArray &array = int_export_kv.value;
  1412. err = save_proxy.save_file(p_preset, p_udata, int_export_kv.key, array, idx, total, enc_in_filters, enc_ex_filters, key, seed, false);
  1413. if (err != OK) {
  1414. return err;
  1415. }
  1416. }
  1417. String config_file = "project.binary";
  1418. String engine_cfb = EditorPaths::get_singleton()->get_temp_dir().path_join("tmp" + config_file);
  1419. ProjectSettings::CustomMap custom_map = get_custom_project_settings(p_preset);
  1420. ProjectSettings::get_singleton()->save_custom(engine_cfb, custom_map, custom_list);
  1421. Vector<uint8_t> data = FileAccess::get_file_as_bytes(engine_cfb);
  1422. DirAccess::remove_file_or_error(engine_cfb);
  1423. err = save_proxy.save_file(p_preset, p_udata, "res://" + config_file, data, idx, total, enc_in_filters, enc_ex_filters, key, seed, false);
  1424. if (err != OK) {
  1425. return err;
  1426. }
  1427. if (p_remove_func) {
  1428. HashSet<String> currently_loaded_paths = PackedData::get_singleton()->get_file_paths();
  1429. for (const String &path : currently_loaded_paths) {
  1430. if (!save_proxy.has_saved(path)) {
  1431. err = p_remove_func(p_preset, p_udata, path);
  1432. if (err != OK) {
  1433. return err;
  1434. }
  1435. }
  1436. }
  1437. }
  1438. return OK;
  1439. }
  1440. Vector<uint8_t> EditorExportPlatform::_filter_extension_list_config_file(const String &p_config_path, const HashSet<String> &p_paths) {
  1441. Ref<FileAccess> f = FileAccess::open(p_config_path, FileAccess::READ);
  1442. if (f.is_null()) {
  1443. ERR_FAIL_V_MSG(Vector<uint8_t>(), "Can't open file from path '" + String(p_config_path) + "'.");
  1444. }
  1445. Vector<uint8_t> data;
  1446. while (!f->eof_reached()) {
  1447. String l = f->get_line().strip_edges();
  1448. if (p_paths.has(l)) {
  1449. data.append_array(l.to_utf8_buffer());
  1450. data.append('\n');
  1451. }
  1452. }
  1453. return data;
  1454. }
  1455. Error EditorExportPlatform::_pack_add_shared_object(const Ref<EditorExportPreset> &p_preset, void *p_userdata, const SharedObject &p_so) {
  1456. PackData *pack_data = (PackData *)p_userdata;
  1457. if (pack_data->so_files) {
  1458. pack_data->so_files->push_back(p_so);
  1459. }
  1460. return OK;
  1461. }
  1462. Error EditorExportPlatform::_remove_pack_file(const Ref<EditorExportPreset> &p_preset, void *p_userdata, const String &p_path) {
  1463. PackData *pd = (PackData *)p_userdata;
  1464. SavedData sd;
  1465. sd.path_utf8 = p_path.utf8();
  1466. sd.ofs = pd->f->get_position();
  1467. sd.size = 0;
  1468. sd.removal = true;
  1469. // This padding will likely never be added, as we should already be aligned when removals are added.
  1470. int pad = _get_pad(PCK_PADDING, pd->f->get_position());
  1471. for (int i = 0; i < pad; i++) {
  1472. pd->f->store_8(0);
  1473. }
  1474. sd.md5.resize_initialized(16);
  1475. pd->file_ofs.push_back(sd);
  1476. return OK;
  1477. }
  1478. Error EditorExportPlatform::_zip_add_shared_object(const Ref<EditorExportPreset> &p_preset, void *p_userdata, const SharedObject &p_so) {
  1479. ZipData *zip_data = (ZipData *)p_userdata;
  1480. if (zip_data->so_files) {
  1481. zip_data->so_files->push_back(p_so);
  1482. }
  1483. return OK;
  1484. }
  1485. void EditorExportPlatform::zip_folder_recursive(zipFile &p_zip, const String &p_root_path, const String &p_folder, const String &p_pkg_name) {
  1486. String dir = p_folder.is_empty() ? p_root_path : p_root_path.path_join(p_folder);
  1487. Ref<DirAccess> da = DirAccess::open(dir);
  1488. ERR_FAIL_COND(da.is_null());
  1489. da->list_dir_begin();
  1490. String f = da->get_next();
  1491. while (!f.is_empty()) {
  1492. if (f == "." || f == "..") {
  1493. f = da->get_next();
  1494. continue;
  1495. }
  1496. if (da->is_link(f)) {
  1497. OS::DateTime dt = OS::get_singleton()->get_datetime();
  1498. zip_fileinfo zipfi;
  1499. zipfi.tmz_date.tm_year = dt.year;
  1500. zipfi.tmz_date.tm_mon = dt.month - 1; // Note: "tm" month range - 0..11, Godot month range - 1..12, https://www.cplusplus.com/reference/ctime/tm/
  1501. zipfi.tmz_date.tm_mday = dt.day;
  1502. zipfi.tmz_date.tm_hour = dt.hour;
  1503. zipfi.tmz_date.tm_min = dt.minute;
  1504. zipfi.tmz_date.tm_sec = dt.second;
  1505. zipfi.dosDate = 0;
  1506. // 0120000: symbolic link type
  1507. // 0000755: permissions rwxr-xr-x
  1508. // 0000644: permissions rw-r--r--
  1509. uint32_t _mode = 0120644;
  1510. zipfi.external_fa = (_mode << 16L) | !(_mode & 0200);
  1511. zipfi.internal_fa = 0;
  1512. zipOpenNewFileInZip4(p_zip,
  1513. p_folder.path_join(f).utf8().get_data(),
  1514. &zipfi,
  1515. nullptr,
  1516. 0,
  1517. nullptr,
  1518. 0,
  1519. nullptr,
  1520. Z_DEFLATED,
  1521. Z_DEFAULT_COMPRESSION,
  1522. 0,
  1523. -MAX_WBITS,
  1524. DEF_MEM_LEVEL,
  1525. Z_DEFAULT_STRATEGY,
  1526. nullptr,
  1527. 0,
  1528. 0x0314, // "version made by", 0x03 - Unix, 0x14 - ZIP specification version 2.0, required to store Unix file permissions
  1529. 1 << 11); // Bit 11 is the language encoding flag. When set, filename and comment fields must be encoded using UTF-8.
  1530. const CharString target_utf8 = da->read_link(f).utf8();
  1531. zipWriteInFileInZip(p_zip, target_utf8.get_data(), target_utf8.size());
  1532. zipCloseFileInZip(p_zip);
  1533. } else if (da->current_is_dir()) {
  1534. zip_folder_recursive(p_zip, p_root_path, p_folder.path_join(f), p_pkg_name);
  1535. } else {
  1536. bool _is_executable = is_executable(dir.path_join(f));
  1537. OS::DateTime dt = OS::get_singleton()->get_datetime();
  1538. zip_fileinfo zipfi;
  1539. zipfi.tmz_date.tm_year = dt.year;
  1540. zipfi.tmz_date.tm_mon = dt.month - 1; // Note: "tm" month range - 0..11, Godot month range - 1..12, https://www.cplusplus.com/reference/ctime/tm/
  1541. zipfi.tmz_date.tm_mday = dt.day;
  1542. zipfi.tmz_date.tm_hour = dt.hour;
  1543. zipfi.tmz_date.tm_min = dt.minute;
  1544. zipfi.tmz_date.tm_sec = dt.second;
  1545. zipfi.dosDate = 0;
  1546. // 0100000: regular file type
  1547. // 0000755: permissions rwxr-xr-x
  1548. // 0000644: permissions rw-r--r--
  1549. uint32_t _mode = (_is_executable ? 0100755 : 0100644);
  1550. zipfi.external_fa = (_mode << 16L) | !(_mode & 0200);
  1551. zipfi.internal_fa = 0;
  1552. zipOpenNewFileInZip4(p_zip,
  1553. p_folder.path_join(f).utf8().get_data(),
  1554. &zipfi,
  1555. nullptr,
  1556. 0,
  1557. nullptr,
  1558. 0,
  1559. nullptr,
  1560. Z_DEFLATED,
  1561. Z_DEFAULT_COMPRESSION,
  1562. 0,
  1563. -MAX_WBITS,
  1564. DEF_MEM_LEVEL,
  1565. Z_DEFAULT_STRATEGY,
  1566. nullptr,
  1567. 0,
  1568. 0x0314, // "version made by", 0x03 - Unix, 0x14 - ZIP specification version 2.0, required to store Unix file permissions
  1569. 1 << 11); // Bit 11 is the language encoding flag. When set, filename and comment fields must be encoded using UTF-8.
  1570. Ref<FileAccess> fa = FileAccess::open(dir.path_join(f), FileAccess::READ);
  1571. if (fa.is_null()) {
  1572. add_message(EXPORT_MESSAGE_ERROR, TTR("ZIP Creation"), vformat(TTR("Could not open file to read from path \"%s\"."), dir.path_join(f)));
  1573. return;
  1574. }
  1575. const int bufsize = 16384;
  1576. uint8_t buf[bufsize];
  1577. while (true) {
  1578. uint64_t got = fa->get_buffer(buf, bufsize);
  1579. if (got == 0) {
  1580. break;
  1581. }
  1582. zipWriteInFileInZip(p_zip, buf, got);
  1583. }
  1584. zipCloseFileInZip(p_zip);
  1585. }
  1586. f = da->get_next();
  1587. }
  1588. da->list_dir_end();
  1589. }
  1590. Dictionary EditorExportPlatform::_save_pack(const Ref<EditorExportPreset> &p_preset, bool p_debug, const String &p_path, bool p_embed) {
  1591. Vector<SharedObject> so_files;
  1592. int64_t embedded_start = 0;
  1593. int64_t embedded_size = 0;
  1594. Error err_code = save_pack(p_preset, p_debug, p_path, &so_files, nullptr, nullptr, p_embed, &embedded_start, &embedded_size);
  1595. Dictionary ret;
  1596. ret["result"] = err_code;
  1597. if (err_code == OK) {
  1598. Array arr;
  1599. for (const SharedObject &E : so_files) {
  1600. Dictionary so;
  1601. so["path"] = E.path;
  1602. so["tags"] = E.tags;
  1603. so["target_folder"] = E.target;
  1604. arr.push_back(so);
  1605. }
  1606. ret["so_files"] = arr;
  1607. if (p_embed) {
  1608. ret["embedded_start"] = embedded_start;
  1609. ret["embedded_size"] = embedded_size;
  1610. }
  1611. }
  1612. return ret;
  1613. }
  1614. Dictionary EditorExportPlatform::_save_zip(const Ref<EditorExportPreset> &p_preset, bool p_debug, const String &p_path) {
  1615. Vector<SharedObject> so_files;
  1616. Error err_code = save_zip(p_preset, p_debug, p_path, &so_files);
  1617. Dictionary ret;
  1618. ret["result"] = err_code;
  1619. if (err_code == OK) {
  1620. Array arr;
  1621. for (const SharedObject &E : so_files) {
  1622. Dictionary so;
  1623. so["path"] = E.path;
  1624. so["tags"] = E.tags;
  1625. so["target_folder"] = E.target;
  1626. arr.push_back(so);
  1627. }
  1628. ret["so_files"] = arr;
  1629. }
  1630. return ret;
  1631. }
  1632. Dictionary EditorExportPlatform::_save_pack_patch(const Ref<EditorExportPreset> &p_preset, bool p_debug, const String &p_path) {
  1633. Vector<SharedObject> so_files;
  1634. Error err_code = save_pack_patch(p_preset, p_debug, p_path, &so_files);
  1635. Dictionary ret;
  1636. ret["result"] = err_code;
  1637. if (err_code == OK) {
  1638. Array arr;
  1639. for (const SharedObject &E : so_files) {
  1640. Dictionary so;
  1641. so["path"] = E.path;
  1642. so["tags"] = E.tags;
  1643. so["target_folder"] = E.target;
  1644. arr.push_back(so);
  1645. }
  1646. ret["so_files"] = arr;
  1647. }
  1648. return ret;
  1649. }
  1650. Dictionary EditorExportPlatform::_save_zip_patch(const Ref<EditorExportPreset> &p_preset, bool p_debug, const String &p_path) {
  1651. Vector<SharedObject> so_files;
  1652. Error err_code = save_zip_patch(p_preset, p_debug, p_path, &so_files);
  1653. Dictionary ret;
  1654. ret["result"] = err_code;
  1655. if (err_code == OK) {
  1656. Array arr;
  1657. for (const SharedObject &E : so_files) {
  1658. Dictionary so;
  1659. so["path"] = E.path;
  1660. so["tags"] = E.tags;
  1661. so["target_folder"] = E.target;
  1662. arr.push_back(so);
  1663. }
  1664. ret["so_files"] = arr;
  1665. }
  1666. return ret;
  1667. }
  1668. bool EditorExportPlatform::_store_header(Ref<FileAccess> p_fd, bool p_enc, bool p_sparse, uint64_t &r_file_base_ofs, uint64_t &r_dir_base_ofs) {
  1669. p_fd->store_32(PACK_HEADER_MAGIC);
  1670. p_fd->store_32(PACK_FORMAT_VERSION);
  1671. p_fd->store_32(GODOT_VERSION_MAJOR);
  1672. p_fd->store_32(GODOT_VERSION_MINOR);
  1673. p_fd->store_32(GODOT_VERSION_PATCH);
  1674. uint32_t pack_flags = PACK_REL_FILEBASE;
  1675. if (p_enc) {
  1676. pack_flags |= PACK_DIR_ENCRYPTED;
  1677. }
  1678. if (p_sparse) {
  1679. pack_flags |= PACK_SPARSE_BUNDLE;
  1680. }
  1681. p_fd->store_32(pack_flags); // Flags.
  1682. r_file_base_ofs = p_fd->get_position();
  1683. p_fd->store_64(0); // Files base offset.
  1684. r_dir_base_ofs = p_fd->get_position();
  1685. p_fd->store_64(0); // Directory offset.
  1686. for (int i = 0; i < 16; i++) {
  1687. //reserved
  1688. p_fd->store_32(0);
  1689. }
  1690. return true;
  1691. }
  1692. bool EditorExportPlatform::_encrypt_and_store_directory(Ref<FileAccess> p_fd, PackData &p_pack_data, const Vector<uint8_t> &p_key, uint64_t p_seed, uint64_t p_file_base) {
  1693. Ref<FileAccessEncrypted> fae;
  1694. Ref<FileAccess> fhead = p_fd;
  1695. fhead->store_32(p_pack_data.file_ofs.size()); //amount of files
  1696. if (!p_key.is_empty()) {
  1697. uint64_t seed = p_seed;
  1698. fae.instantiate();
  1699. if (fae.is_null()) {
  1700. return false;
  1701. }
  1702. Vector<uint8_t> iv;
  1703. if (seed != 0) {
  1704. for (int i = 0; i < p_pack_data.file_ofs.size(); i++) {
  1705. for (int64_t j = 0; j < p_pack_data.file_ofs[i].path_utf8.length(); j++) {
  1706. seed = ((seed << 5) + seed) ^ p_pack_data.file_ofs[i].path_utf8.get_data()[j];
  1707. }
  1708. for (int64_t j = 0; j < p_pack_data.file_ofs[i].md5.size(); j++) {
  1709. seed = ((seed << 5) + seed) ^ p_pack_data.file_ofs[i].md5[j];
  1710. }
  1711. seed = ((seed << 5) + seed) ^ (p_pack_data.file_ofs[i].ofs - p_file_base);
  1712. seed = ((seed << 5) + seed) ^ p_pack_data.file_ofs[i].size;
  1713. }
  1714. RandomPCG rng = RandomPCG(seed);
  1715. iv.resize(16);
  1716. for (int i = 0; i < 16; i++) {
  1717. iv.write[i] = rng.rand() % 256;
  1718. }
  1719. }
  1720. Error err = fae->open_and_parse(fhead, p_key, FileAccessEncrypted::MODE_WRITE_AES256, false, iv);
  1721. if (err != OK) {
  1722. return false;
  1723. }
  1724. fhead = fae;
  1725. }
  1726. for (int i = 0; i < p_pack_data.file_ofs.size(); i++) {
  1727. uint32_t string_len = p_pack_data.file_ofs[i].path_utf8.length();
  1728. uint32_t pad = _get_pad(4, string_len);
  1729. fhead->store_32(string_len + pad);
  1730. fhead->store_buffer((const uint8_t *)p_pack_data.file_ofs[i].path_utf8.get_data(), string_len);
  1731. for (uint32_t j = 0; j < pad; j++) {
  1732. fhead->store_8(0);
  1733. }
  1734. fhead->store_64(p_pack_data.file_ofs[i].ofs - p_file_base);
  1735. fhead->store_64(p_pack_data.file_ofs[i].size); // pay attention here, this is where file is
  1736. fhead->store_buffer(p_pack_data.file_ofs[i].md5.ptr(), 16); //also save md5 for file
  1737. uint32_t flags = 0;
  1738. if (p_pack_data.file_ofs[i].encrypted) {
  1739. flags |= PACK_FILE_ENCRYPTED;
  1740. }
  1741. if (p_pack_data.file_ofs[i].removal) {
  1742. flags |= PACK_FILE_REMOVAL;
  1743. }
  1744. if (p_pack_data.file_ofs[i].delta) {
  1745. flags |= PACK_FILE_DELTA;
  1746. }
  1747. fhead->store_32(flags);
  1748. }
  1749. if (fae.is_valid()) {
  1750. fhead.unref();
  1751. fae.unref();
  1752. }
  1753. return true;
  1754. }
  1755. Error EditorExportPlatform::save_pack(const Ref<EditorExportPreset> &p_preset, bool p_debug, const String &p_path, Vector<SharedObject> *p_so_files, EditorExportSaveFunction p_save_func, EditorExportRemoveFunction p_remove_func, bool p_embed, int64_t *r_embedded_start, int64_t *r_embedded_size) {
  1756. EditorProgress ep("savepack", TTR("Packing"), 102, true);
  1757. if (p_save_func == nullptr) {
  1758. p_save_func = _save_pack_file;
  1759. }
  1760. // Create the temporary export directory if it doesn't exist.
  1761. Ref<DirAccess> da = DirAccess::create(DirAccess::ACCESS_FILESYSTEM);
  1762. da->make_dir_recursive(EditorPaths::get_singleton()->get_temp_dir());
  1763. Ref<FileAccess> f;
  1764. int64_t embed_pos = 0;
  1765. if (!p_embed) {
  1766. // Regular output to separate PCK file.
  1767. f = FileAccess::open(p_path, FileAccess::WRITE);
  1768. if (f.is_null()) {
  1769. add_message(EXPORT_MESSAGE_ERROR, TTR("Save PCK"), vformat(TTR("Can't open file for writing at path \"%s\"."), p_path));
  1770. return ERR_CANT_CREATE;
  1771. }
  1772. } else {
  1773. // Append to executable.
  1774. f = FileAccess::open(p_path, FileAccess::READ_WRITE);
  1775. if (f.is_null()) {
  1776. add_message(EXPORT_MESSAGE_ERROR, TTR("Save PCK"), vformat(TTR("Can't open file for reading-writing at path \"%s\"."), p_path));
  1777. return ERR_FILE_CANT_OPEN;
  1778. }
  1779. f->seek_end();
  1780. embed_pos = f->get_position();
  1781. if (r_embedded_start) {
  1782. *r_embedded_start = embed_pos;
  1783. }
  1784. // Ensure embedded PCK starts at a 64-bit multiple
  1785. int pad = f->get_position() % 8;
  1786. for (int i = 0; i < pad; i++) {
  1787. f->store_8(0);
  1788. }
  1789. }
  1790. int64_t pck_start_pos = f->get_position();
  1791. uint64_t file_base_ofs = 0;
  1792. uint64_t dir_base_ofs = 0;
  1793. _store_header(f, p_preset->get_enc_pck() && p_preset->get_enc_directory(), false, file_base_ofs, dir_base_ofs);
  1794. // Align for first file.
  1795. int file_padding = _get_pad(PCK_PADDING, f->get_position());
  1796. for (int i = 0; i < file_padding; i++) {
  1797. f->store_8(0);
  1798. }
  1799. uint64_t file_base = f->get_position();
  1800. f->seek(file_base_ofs);
  1801. f->store_64(file_base - pck_start_pos); // Update files base.
  1802. f->seek(file_base);
  1803. // Write files.
  1804. PackData pd;
  1805. pd.ep = &ep;
  1806. pd.f = f;
  1807. pd.so_files = p_so_files;
  1808. pd.path = p_path;
  1809. Error err = export_project_files(p_preset, p_debug, p_save_func, p_remove_func, &pd, _pack_add_shared_object);
  1810. if (err != OK) {
  1811. add_message(EXPORT_MESSAGE_ERROR, TTR("Save PCK"), TTR("Failed to export project files."));
  1812. return err;
  1813. }
  1814. if (pd.file_ofs.is_empty()) {
  1815. add_message(EXPORT_MESSAGE_ERROR, TTR("Save PCK"), TTR("No files or changes to export."));
  1816. return FAILED;
  1817. }
  1818. pd.file_ofs.sort(); // Do sort, so we can do binary search later (where ?).
  1819. int dir_padding = _get_pad(PCK_PADDING, f->get_position());
  1820. for (int i = 0; i < dir_padding; i++) {
  1821. f->store_8(0);
  1822. }
  1823. // Write directory.
  1824. uint64_t dir_offset = f->get_position();
  1825. f->seek(dir_base_ofs);
  1826. f->store_64(dir_offset - pck_start_pos);
  1827. f->seek(dir_offset);
  1828. Vector<uint8_t> key;
  1829. if (p_preset->get_enc_pck() && p_preset->get_enc_directory()) {
  1830. String script_key = _get_script_encryption_key(p_preset);
  1831. key.resize(32);
  1832. if (script_key.length() == 64) {
  1833. for (int i = 0; i < 32; i++) {
  1834. int v = 0;
  1835. if (i * 2 < script_key.length()) {
  1836. char32_t ct = script_key[i * 2];
  1837. if (is_digit(ct)) {
  1838. ct = ct - '0';
  1839. } else if (ct >= 'a' && ct <= 'f') {
  1840. ct = 10 + ct - 'a';
  1841. }
  1842. v |= ct << 4;
  1843. }
  1844. if (i * 2 + 1 < script_key.length()) {
  1845. char32_t ct = script_key[i * 2 + 1];
  1846. if (is_digit(ct)) {
  1847. ct = ct - '0';
  1848. } else if (ct >= 'a' && ct <= 'f') {
  1849. ct = 10 + ct - 'a';
  1850. }
  1851. v |= ct;
  1852. }
  1853. key.write[i] = v;
  1854. }
  1855. }
  1856. }
  1857. if (!_encrypt_and_store_directory(f, pd, key, p_preset->get_seed(), file_base)) {
  1858. add_message(EXPORT_MESSAGE_ERROR, TTR("Save PCK"), TTR("Can't create encrypted file."));
  1859. return ERR_CANT_CREATE;
  1860. }
  1861. if (p_embed) {
  1862. // Ensure embedded data ends at a 64-bit multiple.
  1863. uint64_t embed_end = f->get_position() - embed_pos + 12;
  1864. uint64_t pad = embed_end % 8;
  1865. for (uint64_t i = 0; i < pad; i++) {
  1866. f->store_8(0);
  1867. }
  1868. uint64_t pck_size = f->get_position() - pck_start_pos;
  1869. f->store_64(pck_size);
  1870. f->store_32(PACK_HEADER_MAGIC);
  1871. if (r_embedded_size) {
  1872. *r_embedded_size = f->get_position() - embed_pos;
  1873. }
  1874. }
  1875. f->close();
  1876. return OK;
  1877. }
  1878. Error EditorExportPlatform::save_pack_patch(const Ref<EditorExportPreset> &p_preset, bool p_debug, const String &p_path, Vector<SharedObject> *p_so_files, bool p_embed, int64_t *r_embedded_start, int64_t *r_embedded_size) {
  1879. return save_pack(p_preset, p_debug, p_path, p_so_files, _save_pack_patch_file, _remove_pack_file, p_embed, r_embedded_start, r_embedded_size);
  1880. }
  1881. Error EditorExportPlatform::save_zip(const Ref<EditorExportPreset> &p_preset, bool p_debug, const String &p_path, Vector<SharedObject> *p_so_files, EditorExportSaveFunction p_save_func) {
  1882. EditorProgress ep("savezip", TTR("Packing"), 102, true);
  1883. if (p_save_func == nullptr) {
  1884. p_save_func = _save_zip_file;
  1885. }
  1886. String tmppath = EditorPaths::get_singleton()->get_temp_dir().path_join("packtmp");
  1887. Ref<FileAccess> io_fa;
  1888. zlib_filefunc_def io = zipio_create_io(&io_fa);
  1889. zipFile zip = zipOpen2(tmppath.utf8().get_data(), APPEND_STATUS_CREATE, nullptr, &io);
  1890. ZipData zd;
  1891. zd.ep = &ep;
  1892. zd.zip = zip;
  1893. zd.so_files = p_so_files;
  1894. Error err = export_project_files(p_preset, p_debug, p_save_func, nullptr, &zd, _zip_add_shared_object);
  1895. if (err != OK && err != ERR_SKIP) {
  1896. add_message(EXPORT_MESSAGE_ERROR, TTR("Save ZIP"), TTR("Failed to export project files."));
  1897. }
  1898. zipClose(zip, nullptr);
  1899. Ref<DirAccess> da = DirAccess::create(DirAccess::ACCESS_FILESYSTEM);
  1900. if (zd.file_count == 0) {
  1901. da->remove(tmppath);
  1902. add_message(EXPORT_MESSAGE_ERROR, TTR("Save PCK"), TTR("No files or changes to export."));
  1903. return FAILED;
  1904. }
  1905. err = da->rename(tmppath, p_path);
  1906. if (err != OK) {
  1907. da->remove(tmppath);
  1908. add_message(EXPORT_MESSAGE_ERROR, TTR("Save ZIP"), vformat(TTR("Failed to move temporary file \"%s\" to \"%s\"."), tmppath, p_path));
  1909. }
  1910. return OK;
  1911. }
  1912. Error EditorExportPlatform::save_zip_patch(const Ref<EditorExportPreset> &p_preset, bool p_debug, const String &p_path, Vector<SharedObject> *p_so_files) {
  1913. return save_zip(p_preset, p_debug, p_path, p_so_files, _save_zip_patch_file);
  1914. }
  1915. Error EditorExportPlatform::export_pack(const Ref<EditorExportPreset> &p_preset, bool p_debug, const String &p_path, BitField<EditorExportPlatform::DebugFlags> p_flags) {
  1916. ExportNotifier notifier(*this, p_preset, p_debug, p_path, p_flags);
  1917. return save_pack(p_preset, p_debug, p_path);
  1918. }
  1919. Error EditorExportPlatform::export_zip(const Ref<EditorExportPreset> &p_preset, bool p_debug, const String &p_path, BitField<EditorExportPlatform::DebugFlags> p_flags) {
  1920. ExportNotifier notifier(*this, p_preset, p_debug, p_path, p_flags);
  1921. return save_zip(p_preset, p_debug, p_path);
  1922. }
  1923. Error EditorExportPlatform::export_pack_patch(const Ref<EditorExportPreset> &p_preset, bool p_debug, const String &p_path, const Vector<String> &p_patches, BitField<EditorExportPlatform::DebugFlags> p_flags) {
  1924. ExportNotifier notifier(*this, p_preset, p_debug, p_path, p_flags);
  1925. Error err = _load_patches(p_patches.is_empty() ? p_preset->get_patches() : p_patches);
  1926. if (err != OK) {
  1927. return err;
  1928. }
  1929. err = save_pack_patch(p_preset, p_debug, p_path);
  1930. _unload_patches();
  1931. return err;
  1932. }
  1933. Error EditorExportPlatform::export_zip_patch(const Ref<EditorExportPreset> &p_preset, bool p_debug, const String &p_path, const Vector<String> &p_patches, BitField<EditorExportPlatform::DebugFlags> p_flags) {
  1934. ExportNotifier notifier(*this, p_preset, p_debug, p_path, p_flags);
  1935. Error err = _load_patches(p_patches.is_empty() ? p_preset->get_patches() : p_patches);
  1936. if (err != OK) {
  1937. return err;
  1938. }
  1939. err = save_zip_patch(p_preset, p_debug, p_path);
  1940. _unload_patches();
  1941. return err;
  1942. }
  1943. Vector<String> EditorExportPlatform::gen_export_flags(BitField<EditorExportPlatform::DebugFlags> p_flags) {
  1944. Vector<String> ret;
  1945. String host = EDITOR_GET("network/debug/remote_host");
  1946. int remote_port = (int)EDITOR_GET("network/debug/remote_port");
  1947. if (get_name() == "Android" && EditorSettings::get_singleton()->has_setting("export/android/use_wifi_for_remote_debug") && EDITOR_GET("export/android/use_wifi_for_remote_debug")) {
  1948. host = EDITOR_GET("export/android/wifi_remote_debug_host");
  1949. } else if (p_flags.has_flag(DEBUG_FLAG_REMOTE_DEBUG_LOCALHOST)) {
  1950. host = "localhost";
  1951. }
  1952. if (p_flags.has_flag(DEBUG_FLAG_DUMB_CLIENT)) {
  1953. int port = EDITOR_GET("filesystem/file_server/port");
  1954. String passwd = EDITOR_GET("filesystem/file_server/password");
  1955. ret.push_back("--remote-fs");
  1956. ret.push_back(host + ":" + itos(port));
  1957. if (!passwd.is_empty()) {
  1958. ret.push_back("--remote-fs-password");
  1959. ret.push_back(passwd);
  1960. }
  1961. }
  1962. if (p_flags.has_flag(DEBUG_FLAG_REMOTE_DEBUG)) {
  1963. ret.push_back("--remote-debug");
  1964. ret.push_back(get_debug_protocol() + host + ":" + String::num_int64(remote_port));
  1965. List<String> breakpoints;
  1966. ScriptEditor::get_singleton()->get_breakpoints(&breakpoints);
  1967. if (breakpoints.size()) {
  1968. ret.push_back("--breakpoints");
  1969. String bpoints;
  1970. for (List<String>::Element *E = breakpoints.front(); E; E = E->next()) {
  1971. bpoints += E->get().replace(" ", "%20");
  1972. if (E->next()) {
  1973. bpoints += ",";
  1974. }
  1975. }
  1976. ret.push_back(bpoints);
  1977. }
  1978. }
  1979. if (p_flags.has_flag(DEBUG_FLAG_VIEW_COLLISIONS)) {
  1980. ret.push_back("--debug-collisions");
  1981. }
  1982. if (p_flags.has_flag(DEBUG_FLAG_VIEW_NAVIGATION)) {
  1983. ret.push_back("--debug-navigation");
  1984. }
  1985. return ret;
  1986. }
  1987. bool EditorExportPlatform::can_export(const Ref<EditorExportPreset> &p_preset, String &r_error, bool &r_missing_templates, bool p_debug) const {
  1988. bool valid = true;
  1989. String templates_error;
  1990. valid = valid && has_valid_export_configuration(p_preset, templates_error, r_missing_templates, p_debug);
  1991. if (!templates_error.is_empty()) {
  1992. r_error += templates_error;
  1993. }
  1994. String export_plugins_warning;
  1995. Vector<Ref<EditorExportPlugin>> export_plugins = EditorExport::get_singleton()->get_export_plugins();
  1996. for (int i = 0; i < export_plugins.size(); i++) {
  1997. Ref<EditorExportPlatform> export_platform = Ref<EditorExportPlatform>(this);
  1998. if (!export_plugins[i]->supports_platform(export_platform)) {
  1999. continue;
  2000. }
  2001. String plugin_warning = export_plugins.write[i]->_has_valid_export_configuration(export_platform, p_preset);
  2002. if (!plugin_warning.is_empty()) {
  2003. export_plugins_warning += plugin_warning;
  2004. }
  2005. }
  2006. if (!export_plugins_warning.is_empty()) {
  2007. r_error += export_plugins_warning;
  2008. }
  2009. String project_configuration_error;
  2010. valid = valid && has_valid_project_configuration(p_preset, project_configuration_error);
  2011. if (!project_configuration_error.is_empty()) {
  2012. r_error += project_configuration_error;
  2013. }
  2014. return valid;
  2015. }
  2016. Error EditorExportPlatform::ssh_run_on_remote(const String &p_host, const String &p_port, const Vector<String> &p_ssh_args, const String &p_cmd_args, String *r_out, int p_port_fwd) const {
  2017. String ssh_path = EDITOR_GET("export/ssh/ssh");
  2018. if (ssh_path.is_empty()) {
  2019. ssh_path = "ssh";
  2020. }
  2021. List<String> args;
  2022. args.push_back("-p");
  2023. args.push_back(p_port);
  2024. args.push_back("-q");
  2025. args.push_back("-o");
  2026. args.push_back("LogLevel=error");
  2027. args.push_back("-o");
  2028. args.push_back("BatchMode=yes");
  2029. args.push_back("-o");
  2030. args.push_back("StrictHostKeyChecking=no");
  2031. for (const String &E : p_ssh_args) {
  2032. args.push_back(E);
  2033. }
  2034. if (p_port_fwd > 0) {
  2035. args.push_back("-R");
  2036. args.push_back(vformat("%d:localhost:%d", p_port_fwd, p_port_fwd));
  2037. }
  2038. args.push_back(p_host);
  2039. args.push_back(p_cmd_args);
  2040. String out;
  2041. int exit_code = -1;
  2042. if (OS::get_singleton()->is_stdout_verbose()) {
  2043. OS::get_singleton()->print("Executing: %s", ssh_path.utf8().get_data());
  2044. for (const String &arg : args) {
  2045. OS::get_singleton()->print(" %s", arg.utf8().get_data());
  2046. }
  2047. OS::get_singleton()->print("\n");
  2048. }
  2049. Error err = OS::get_singleton()->execute(ssh_path, args, &out, &exit_code, true);
  2050. if (out.is_empty()) {
  2051. print_verbose(vformat("Exit code: %d", exit_code));
  2052. } else {
  2053. print_verbose(vformat("Exit code: %d, Output: %s", exit_code, out.replace("\r\n", "\n")));
  2054. }
  2055. if (r_out) {
  2056. *r_out = out.replace("\r\n", "\n").get_slicec('\n', 0);
  2057. }
  2058. if (err != OK) {
  2059. return err;
  2060. } else if (exit_code != 0) {
  2061. if (!out.is_empty()) {
  2062. print_line(out);
  2063. }
  2064. return FAILED;
  2065. }
  2066. return OK;
  2067. }
  2068. Error EditorExportPlatform::ssh_run_on_remote_no_wait(const String &p_host, const String &p_port, const Vector<String> &p_ssh_args, const String &p_cmd_args, OS::ProcessID *r_pid, int p_port_fwd) const {
  2069. String ssh_path = EDITOR_GET("export/ssh/ssh");
  2070. if (ssh_path.is_empty()) {
  2071. ssh_path = "ssh";
  2072. }
  2073. List<String> args;
  2074. args.push_back("-p");
  2075. args.push_back(p_port);
  2076. args.push_back("-q");
  2077. args.push_back("-o");
  2078. args.push_back("LogLevel=error");
  2079. args.push_back("-o");
  2080. args.push_back("BatchMode=yes");
  2081. args.push_back("-o");
  2082. args.push_back("StrictHostKeyChecking=no");
  2083. for (const String &E : p_ssh_args) {
  2084. args.push_back(E);
  2085. }
  2086. if (p_port_fwd > 0) {
  2087. args.push_back("-R");
  2088. args.push_back(vformat("%d:localhost:%d", p_port_fwd, p_port_fwd));
  2089. }
  2090. args.push_back(p_host);
  2091. args.push_back(p_cmd_args);
  2092. if (OS::get_singleton()->is_stdout_verbose()) {
  2093. OS::get_singleton()->print("Executing: %s", ssh_path.utf8().get_data());
  2094. for (const String &arg : args) {
  2095. OS::get_singleton()->print(" %s", arg.utf8().get_data());
  2096. }
  2097. OS::get_singleton()->print("\n");
  2098. }
  2099. return OS::get_singleton()->create_process(ssh_path, args, r_pid);
  2100. }
  2101. Error EditorExportPlatform::ssh_push_to_remote(const String &p_host, const String &p_port, const Vector<String> &p_scp_args, const String &p_src_file, const String &p_dst_file) const {
  2102. String scp_path = EDITOR_GET("export/ssh/scp");
  2103. if (scp_path.is_empty()) {
  2104. scp_path = "scp";
  2105. }
  2106. List<String> args;
  2107. args.push_back("-P");
  2108. args.push_back(p_port);
  2109. args.push_back("-q");
  2110. args.push_back("-o");
  2111. args.push_back("LogLevel=error");
  2112. args.push_back("-o");
  2113. args.push_back("BatchMode=yes");
  2114. args.push_back("-o");
  2115. args.push_back("StrictHostKeyChecking=no");
  2116. for (const String &E : p_scp_args) {
  2117. args.push_back(E);
  2118. }
  2119. args.push_back(p_src_file);
  2120. args.push_back(vformat("%s:%s", p_host, p_dst_file));
  2121. String out;
  2122. int exit_code = -1;
  2123. if (OS::get_singleton()->is_stdout_verbose()) {
  2124. OS::get_singleton()->print("Executing: %s", scp_path.utf8().get_data());
  2125. for (const String &arg : args) {
  2126. OS::get_singleton()->print(" %s", arg.utf8().get_data());
  2127. }
  2128. OS::get_singleton()->print("\n");
  2129. }
  2130. Error err = OS::get_singleton()->execute(scp_path, args, &out, &exit_code, true);
  2131. if (err != OK) {
  2132. return err;
  2133. } else if (exit_code != 0) {
  2134. if (!out.is_empty()) {
  2135. print_line(out);
  2136. }
  2137. return FAILED;
  2138. }
  2139. return OK;
  2140. }
  2141. Array EditorExportPlatform::get_current_presets() const {
  2142. Array ret;
  2143. for (int i = 0; i < EditorExport::get_singleton()->get_export_preset_count(); i++) {
  2144. Ref<EditorExportPreset> ep = EditorExport::get_singleton()->get_export_preset(i);
  2145. if (ep->get_platform() == this) {
  2146. ret.push_back(ep);
  2147. }
  2148. }
  2149. return ret;
  2150. }
  2151. String EditorExportPlatform::simplify_path(const String &p_path) {
  2152. if (p_path.begins_with("uid://")) {
  2153. const String path = ResourceUID::uid_to_path(p_path);
  2154. print_verbose(vformat(R"(UID-referenced exported file name "%s" was replaced with "%s".)", p_path, path));
  2155. return path.simplify_path();
  2156. } else {
  2157. return p_path.simplify_path();
  2158. }
  2159. }
  2160. Variant EditorExportPlatform::get_project_setting(const Ref<EditorExportPreset> &p_preset, const StringName &p_name) {
  2161. if (p_preset.is_valid()) {
  2162. return p_preset->get_project_setting(p_name);
  2163. } else {
  2164. return GLOBAL_GET(p_name);
  2165. }
  2166. }
  2167. void EditorExportPlatform::_bind_methods() {
  2168. ClassDB::bind_method(D_METHOD("get_os_name"), &EditorExportPlatform::get_os_name);
  2169. ClassDB::bind_method(D_METHOD("create_preset"), &EditorExportPlatform::create_preset);
  2170. ClassDB::bind_method(D_METHOD("find_export_template", "template_file_name"), &EditorExportPlatform::_find_export_template);
  2171. ClassDB::bind_method(D_METHOD("get_current_presets"), &EditorExportPlatform::get_current_presets);
  2172. ClassDB::bind_method(D_METHOD("save_pack", "preset", "debug", "path", "embed"), &EditorExportPlatform::_save_pack, DEFVAL(false));
  2173. ClassDB::bind_method(D_METHOD("save_zip", "preset", "debug", "path"), &EditorExportPlatform::_save_zip);
  2174. ClassDB::bind_method(D_METHOD("save_pack_patch", "preset", "debug", "path"), &EditorExportPlatform::_save_pack_patch);
  2175. ClassDB::bind_method(D_METHOD("save_zip_patch", "preset", "debug", "path"), &EditorExportPlatform::_save_zip_patch);
  2176. ClassDB::bind_method(D_METHOD("gen_export_flags", "flags"), &EditorExportPlatform::gen_export_flags);
  2177. ClassDB::bind_method(D_METHOD("export_project_files", "preset", "debug", "save_cb", "shared_cb"), &EditorExportPlatform::_export_project_files, DEFVAL(Callable()));
  2178. ClassDB::bind_method(D_METHOD("export_project", "preset", "debug", "path", "flags"), &EditorExportPlatform::export_project, DEFVAL(0));
  2179. ClassDB::bind_method(D_METHOD("export_pack", "preset", "debug", "path", "flags"), &EditorExportPlatform::export_pack, DEFVAL(0));
  2180. ClassDB::bind_method(D_METHOD("export_zip", "preset", "debug", "path", "flags"), &EditorExportPlatform::export_zip, DEFVAL(0));
  2181. ClassDB::bind_method(D_METHOD("export_pack_patch", "preset", "debug", "path", "patches", "flags"), &EditorExportPlatform::export_pack_patch, DEFVAL(PackedStringArray()), DEFVAL(0));
  2182. ClassDB::bind_method(D_METHOD("export_zip_patch", "preset", "debug", "path", "patches", "flags"), &EditorExportPlatform::export_zip_patch, DEFVAL(PackedStringArray()), DEFVAL(0));
  2183. ClassDB::bind_method(D_METHOD("clear_messages"), &EditorExportPlatform::clear_messages);
  2184. ClassDB::bind_method(D_METHOD("add_message", "type", "category", "message"), &EditorExportPlatform::add_message);
  2185. ClassDB::bind_method(D_METHOD("get_message_count"), &EditorExportPlatform::get_message_count);
  2186. ClassDB::bind_method(D_METHOD("get_message_type", "index"), &EditorExportPlatform::_get_message_type);
  2187. ClassDB::bind_method(D_METHOD("get_message_category", "index"), &EditorExportPlatform::_get_message_category);
  2188. ClassDB::bind_method(D_METHOD("get_message_text", "index"), &EditorExportPlatform::_get_message_text);
  2189. ClassDB::bind_method(D_METHOD("get_worst_message_type"), &EditorExportPlatform::get_worst_message_type);
  2190. ClassDB::bind_method(D_METHOD("ssh_run_on_remote", "host", "port", "ssh_arg", "cmd_args", "output", "port_fwd"), &EditorExportPlatform::_ssh_run_on_remote, DEFVAL(Array()), DEFVAL(-1));
  2191. ClassDB::bind_method(D_METHOD("ssh_run_on_remote_no_wait", "host", "port", "ssh_args", "cmd_args", "port_fwd"), &EditorExportPlatform::_ssh_run_on_remote_no_wait, DEFVAL(-1));
  2192. ClassDB::bind_method(D_METHOD("ssh_push_to_remote", "host", "port", "scp_args", "src_file", "dst_file"), &EditorExportPlatform::ssh_push_to_remote);
  2193. ClassDB::bind_method(D_METHOD("get_internal_export_files", "preset", "debug"), &EditorExportPlatform::get_internal_export_files);
  2194. ClassDB::bind_static_method("EditorExportPlatform", D_METHOD("get_forced_export_files", "preset"), &EditorExportPlatform::get_forced_export_files, DEFVAL(Ref<EditorExportPreset>()));
  2195. BIND_ENUM_CONSTANT(EXPORT_MESSAGE_NONE);
  2196. BIND_ENUM_CONSTANT(EXPORT_MESSAGE_INFO);
  2197. BIND_ENUM_CONSTANT(EXPORT_MESSAGE_WARNING);
  2198. BIND_ENUM_CONSTANT(EXPORT_MESSAGE_ERROR);
  2199. BIND_BITFIELD_FLAG(DEBUG_FLAG_DUMB_CLIENT);
  2200. BIND_BITFIELD_FLAG(DEBUG_FLAG_REMOTE_DEBUG);
  2201. BIND_BITFIELD_FLAG(DEBUG_FLAG_REMOTE_DEBUG_LOCALHOST);
  2202. BIND_BITFIELD_FLAG(DEBUG_FLAG_VIEW_COLLISIONS);
  2203. BIND_BITFIELD_FLAG(DEBUG_FLAG_VIEW_NAVIGATION);
  2204. }