editor_export_platform.cpp 61 KB

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