editor_export_platform.cpp 62 KB

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