editor_export_platform.cpp 84 KB

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