editor_export_platform.cpp 79 KB

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