editor_export_platform.cpp 64 KB

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