editor_export_platform.cpp 62 KB

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