editor_export.cpp 61 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004
  1. /*************************************************************************/
  2. /* editor_export.cpp */
  3. /*************************************************************************/
  4. /* This file is part of: */
  5. /* GODOT ENGINE */
  6. /* https://godotengine.org */
  7. /*************************************************************************/
  8. /* Copyright (c) 2007-2021 Juan Linietsky, Ariel Manzur. */
  9. /* Copyright (c) 2014-2021 Godot Engine contributors (cf. AUTHORS.md). */
  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.h"
  31. #include "core/config/project_settings.h"
  32. #include "core/crypto/crypto_core.h"
  33. #include "core/io/config_file.h"
  34. #include "core/io/dir_access.h"
  35. #include "core/io/file_access.h"
  36. #include "core/io/file_access_encrypted.h"
  37. #include "core/io/file_access_pack.h" // PACK_HEADER_MAGIC, PACK_FORMAT_VERSION
  38. #include "core/io/resource_loader.h"
  39. #include "core/io/resource_saver.h"
  40. #include "core/io/zip_io.h"
  41. #include "core/object/script_language.h"
  42. #include "core/version.h"
  43. #include "editor/editor_file_system.h"
  44. #include "editor/plugins/script_editor_plugin.h"
  45. #include "editor_node.h"
  46. #include "editor_settings.h"
  47. #include "scene/resources/resource_format_text.h"
  48. static int _get_pad(int p_alignment, int p_n) {
  49. int rest = p_n % p_alignment;
  50. int pad = 0;
  51. if (rest > 0) {
  52. pad = p_alignment - rest;
  53. };
  54. return pad;
  55. }
  56. #define PCK_PADDING 16
  57. bool EditorExportPreset::_set(const StringName &p_name, const Variant &p_value) {
  58. if (values.has(p_name)) {
  59. values[p_name] = p_value;
  60. EditorExport::singleton->save_presets();
  61. return true;
  62. }
  63. return false;
  64. }
  65. bool EditorExportPreset::_get(const StringName &p_name, Variant &r_ret) const {
  66. if (values.has(p_name)) {
  67. r_ret = values[p_name];
  68. return true;
  69. }
  70. return false;
  71. }
  72. void EditorExportPreset::_get_property_list(List<PropertyInfo> *p_list) const {
  73. for (const List<PropertyInfo>::Element *E = properties.front(); E; E = E->next()) {
  74. if (platform->get_option_visibility(E->get().name, values)) {
  75. p_list->push_back(E->get());
  76. }
  77. }
  78. }
  79. Ref<EditorExportPlatform> EditorExportPreset::get_platform() const {
  80. return platform;
  81. }
  82. void EditorExportPreset::update_files_to_export() {
  83. Vector<String> to_remove;
  84. for (Set<String>::Element *E = selected_files.front(); E; E = E->next()) {
  85. if (!FileAccess::exists(E->get())) {
  86. to_remove.push_back(E->get());
  87. }
  88. }
  89. for (int i = 0; i < to_remove.size(); ++i) {
  90. selected_files.erase(to_remove[i]);
  91. }
  92. }
  93. Vector<String> EditorExportPreset::get_files_to_export() const {
  94. Vector<String> files;
  95. for (Set<String>::Element *E = selected_files.front(); E; E = E->next()) {
  96. files.push_back(E->get());
  97. }
  98. return files;
  99. }
  100. void EditorExportPreset::set_name(const String &p_name) {
  101. name = p_name;
  102. EditorExport::singleton->save_presets();
  103. }
  104. String EditorExportPreset::get_name() const {
  105. return name;
  106. }
  107. void EditorExportPreset::set_runnable(bool p_enable) {
  108. runnable = p_enable;
  109. EditorExport::singleton->save_presets();
  110. }
  111. bool EditorExportPreset::is_runnable() const {
  112. return runnable;
  113. }
  114. void EditorExportPreset::set_export_filter(ExportFilter p_filter) {
  115. export_filter = p_filter;
  116. EditorExport::singleton->save_presets();
  117. }
  118. EditorExportPreset::ExportFilter EditorExportPreset::get_export_filter() const {
  119. return export_filter;
  120. }
  121. void EditorExportPreset::set_include_filter(const String &p_include) {
  122. include_filter = p_include;
  123. EditorExport::singleton->save_presets();
  124. }
  125. String EditorExportPreset::get_include_filter() const {
  126. return include_filter;
  127. }
  128. void EditorExportPreset::set_export_path(const String &p_path) {
  129. export_path = p_path;
  130. /* NOTE(SonerSound): if there is a need to implement a PropertyHint that specifically indicates a relative path,
  131. * this should be removed. */
  132. if (export_path.is_abs_path()) {
  133. String res_path = OS::get_singleton()->get_resource_dir();
  134. export_path = res_path.path_to_file(export_path);
  135. }
  136. EditorExport::singleton->save_presets();
  137. }
  138. String EditorExportPreset::get_export_path() const {
  139. return export_path;
  140. }
  141. void EditorExportPreset::set_exclude_filter(const String &p_exclude) {
  142. exclude_filter = p_exclude;
  143. EditorExport::singleton->save_presets();
  144. }
  145. String EditorExportPreset::get_exclude_filter() const {
  146. return exclude_filter;
  147. }
  148. void EditorExportPreset::add_export_file(const String &p_path) {
  149. selected_files.insert(p_path);
  150. EditorExport::singleton->save_presets();
  151. }
  152. void EditorExportPreset::remove_export_file(const String &p_path) {
  153. selected_files.erase(p_path);
  154. EditorExport::singleton->save_presets();
  155. }
  156. bool EditorExportPreset::has_export_file(const String &p_path) {
  157. return selected_files.has(p_path);
  158. }
  159. void EditorExportPreset::set_custom_features(const String &p_custom_features) {
  160. custom_features = p_custom_features;
  161. EditorExport::singleton->save_presets();
  162. }
  163. String EditorExportPreset::get_custom_features() const {
  164. return custom_features;
  165. }
  166. void EditorExportPreset::set_enc_in_filter(const String &p_filter) {
  167. enc_in_filters = p_filter;
  168. EditorExport::singleton->save_presets();
  169. }
  170. String EditorExportPreset::get_enc_in_filter() const {
  171. return enc_in_filters;
  172. }
  173. void EditorExportPreset::set_enc_ex_filter(const String &p_filter) {
  174. enc_ex_filters = p_filter;
  175. EditorExport::singleton->save_presets();
  176. }
  177. String EditorExportPreset::get_enc_ex_filter() const {
  178. return enc_ex_filters;
  179. }
  180. void EditorExportPreset::set_enc_pck(bool p_enabled) {
  181. enc_pck = p_enabled;
  182. EditorExport::singleton->save_presets();
  183. }
  184. bool EditorExportPreset::get_enc_pck() const {
  185. return enc_pck;
  186. }
  187. void EditorExportPreset::set_enc_directory(bool p_enabled) {
  188. enc_directory = p_enabled;
  189. EditorExport::singleton->save_presets();
  190. }
  191. bool EditorExportPreset::get_enc_directory() const {
  192. return enc_directory;
  193. }
  194. void EditorExportPreset::set_script_export_mode(int p_mode) {
  195. script_mode = p_mode;
  196. EditorExport::singleton->save_presets();
  197. }
  198. int EditorExportPreset::get_script_export_mode() const {
  199. return script_mode;
  200. }
  201. void EditorExportPreset::set_script_encryption_key(const String &p_key) {
  202. script_key = p_key;
  203. EditorExport::singleton->save_presets();
  204. }
  205. String EditorExportPreset::get_script_encryption_key() const {
  206. return script_key;
  207. }
  208. ///////////////////////////////////
  209. void EditorExportPlatform::gen_debug_flags(Vector<String> &r_flags, int p_flags) {
  210. String host = EditorSettings::get_singleton()->get("network/debug/remote_host");
  211. int remote_port = (int)EditorSettings::get_singleton()->get("network/debug/remote_port");
  212. if (p_flags & DEBUG_FLAG_REMOTE_DEBUG_LOCALHOST) {
  213. host = "localhost";
  214. }
  215. if (p_flags & DEBUG_FLAG_DUMB_CLIENT) {
  216. int port = EditorSettings::get_singleton()->get("filesystem/file_server/port");
  217. String passwd = EditorSettings::get_singleton()->get("filesystem/file_server/password");
  218. r_flags.push_back("--remote-fs");
  219. r_flags.push_back(host + ":" + itos(port));
  220. if (passwd != "") {
  221. r_flags.push_back("--remote-fs-password");
  222. r_flags.push_back(passwd);
  223. }
  224. }
  225. if (p_flags & DEBUG_FLAG_REMOTE_DEBUG) {
  226. r_flags.push_back("--remote-debug");
  227. r_flags.push_back(get_debug_protocol() + host + ":" + String::num(remote_port));
  228. List<String> breakpoints;
  229. ScriptEditor::get_singleton()->get_breakpoints(&breakpoints);
  230. if (breakpoints.size()) {
  231. r_flags.push_back("--breakpoints");
  232. String bpoints;
  233. for (const List<String>::Element *E = breakpoints.front(); E; E = E->next()) {
  234. bpoints += E->get().replace(" ", "%20");
  235. if (E->next()) {
  236. bpoints += ",";
  237. }
  238. }
  239. r_flags.push_back(bpoints);
  240. }
  241. }
  242. if (p_flags & DEBUG_FLAG_VIEW_COLLISONS) {
  243. r_flags.push_back("--debug-collisions");
  244. }
  245. if (p_flags & DEBUG_FLAG_VIEW_NAVIGATION) {
  246. r_flags.push_back("--debug-navigation");
  247. }
  248. }
  249. 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) {
  250. ERR_FAIL_COND_V_MSG(p_total < 1, ERR_PARAMETER_RANGE_ERROR, "Must select at least one file to export.");
  251. PackData *pd = (PackData *)p_userdata;
  252. SavedData sd;
  253. sd.path_utf8 = p_path.utf8();
  254. sd.ofs = pd->f->get_position();
  255. sd.size = p_data.size();
  256. sd.encrypted = false;
  257. for (int i = 0; i < p_enc_in_filters.size(); ++i) {
  258. if (p_path.matchn(p_enc_in_filters[i]) || p_path.replace("res://", "").matchn(p_enc_in_filters[i])) {
  259. sd.encrypted = true;
  260. break;
  261. }
  262. }
  263. for (int i = 0; i < p_enc_ex_filters.size(); ++i) {
  264. if (p_path.matchn(p_enc_ex_filters[i]) || p_path.replace("res://", "").matchn(p_enc_ex_filters[i])) {
  265. sd.encrypted = false;
  266. break;
  267. }
  268. }
  269. FileAccessEncrypted *fae = nullptr;
  270. FileAccess *ftmp = pd->f;
  271. if (sd.encrypted) {
  272. fae = memnew(FileAccessEncrypted);
  273. ERR_FAIL_COND_V(!fae, ERR_SKIP);
  274. Error err = fae->open_and_parse(ftmp, p_key, FileAccessEncrypted::MODE_WRITE_AES256, false);
  275. ERR_FAIL_COND_V(err != OK, ERR_SKIP);
  276. ftmp = fae;
  277. }
  278. // Store file content.
  279. ftmp->store_buffer(p_data.ptr(), p_data.size());
  280. if (fae) {
  281. fae->release();
  282. memdelete(fae);
  283. }
  284. int pad = _get_pad(PCK_PADDING, pd->f->get_position());
  285. for (int i = 0; i < pad; i++) {
  286. pd->f->store_8(Math::rand() % 256);
  287. }
  288. // Store MD5 of original file.
  289. {
  290. unsigned char hash[16];
  291. CryptoCore::md5(p_data.ptr(), p_data.size(), hash);
  292. sd.md5.resize(16);
  293. for (int i = 0; i < 16; i++) {
  294. sd.md5.write[i] = hash[i];
  295. }
  296. }
  297. pd->file_ofs.push_back(sd);
  298. if (pd->ep->step(TTR("Storing File:") + " " + p_path, 2 + p_file * 100 / p_total, false)) {
  299. return ERR_SKIP;
  300. }
  301. return OK;
  302. }
  303. 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) {
  304. ERR_FAIL_COND_V_MSG(p_total < 1, ERR_PARAMETER_RANGE_ERROR, "Must select at least one file to export.");
  305. String path = p_path.replace_first("res://", "");
  306. ZipData *zd = (ZipData *)p_userdata;
  307. zipFile zip = (zipFile)zd->zip;
  308. zipOpenNewFileInZip(zip,
  309. path.utf8().get_data(),
  310. nullptr,
  311. nullptr,
  312. 0,
  313. nullptr,
  314. 0,
  315. nullptr,
  316. Z_DEFLATED,
  317. Z_DEFAULT_COMPRESSION);
  318. zipWriteInFileInZip(zip, p_data.ptr(), p_data.size());
  319. zipCloseFileInZip(zip);
  320. if (zd->ep->step(TTR("Storing File:") + " " + p_path, 2 + p_file * 100 / p_total, false)) {
  321. return ERR_SKIP;
  322. }
  323. return OK;
  324. }
  325. Ref<ImageTexture> EditorExportPlatform::get_option_icon(int p_index) const {
  326. Ref<Theme> theme = EditorNode::get_singleton()->get_editor_theme();
  327. ERR_FAIL_COND_V(theme.is_null(), Ref<ImageTexture>());
  328. if (EditorNode::get_singleton()->get_main_control()->is_layout_rtl()) {
  329. return theme->get_icon("PlayBackwards", "EditorIcons");
  330. } else {
  331. return theme->get_icon("Play", "EditorIcons");
  332. }
  333. }
  334. String EditorExportPlatform::find_export_template(String template_file_name, String *err) const {
  335. String current_version = VERSION_FULL_CONFIG;
  336. String template_path = EditorSettings::get_singleton()->get_templates_dir().plus_file(current_version).plus_file(template_file_name);
  337. if (FileAccess::exists(template_path)) {
  338. return template_path;
  339. }
  340. // Not found
  341. if (err) {
  342. *err += TTR("No export template found at the expected path:") + "\n" + template_path + "\n";
  343. }
  344. return String();
  345. }
  346. bool EditorExportPlatform::exists_export_template(String template_file_name, String *err) const {
  347. return find_export_template(template_file_name, err) != "";
  348. }
  349. Ref<EditorExportPreset> EditorExportPlatform::create_preset() {
  350. Ref<EditorExportPreset> preset;
  351. preset.instance();
  352. preset->platform = Ref<EditorExportPlatform>(this);
  353. List<ExportOption> options;
  354. get_export_options(&options);
  355. for (List<ExportOption>::Element *E = options.front(); E; E = E->next()) {
  356. preset->properties.push_back(E->get().option);
  357. preset->values[E->get().option.name] = E->get().default_value;
  358. }
  359. return preset;
  360. }
  361. void EditorExportPlatform::_export_find_resources(EditorFileSystemDirectory *p_dir, Set<String> &p_paths) {
  362. for (int i = 0; i < p_dir->get_subdir_count(); i++) {
  363. _export_find_resources(p_dir->get_subdir(i), p_paths);
  364. }
  365. for (int i = 0; i < p_dir->get_file_count(); i++) {
  366. p_paths.insert(p_dir->get_file_path(i));
  367. }
  368. }
  369. void EditorExportPlatform::_export_find_dependencies(const String &p_path, Set<String> &p_paths) {
  370. if (p_paths.has(p_path)) {
  371. return;
  372. }
  373. p_paths.insert(p_path);
  374. EditorFileSystemDirectory *dir;
  375. int file_idx;
  376. dir = EditorFileSystem::get_singleton()->find_file(p_path, &file_idx);
  377. if (!dir) {
  378. return;
  379. }
  380. Vector<String> deps = dir->get_file_deps(file_idx);
  381. for (int i = 0; i < deps.size(); i++) {
  382. _export_find_dependencies(deps[i], p_paths);
  383. }
  384. }
  385. void EditorExportPlatform::_edit_files_with_filter(DirAccess *da, const Vector<String> &p_filters, Set<String> &r_list, bool exclude) {
  386. da->list_dir_begin();
  387. String cur_dir = da->get_current_dir().replace("\\", "/");
  388. if (!cur_dir.ends_with("/")) {
  389. cur_dir += "/";
  390. }
  391. String cur_dir_no_prefix = cur_dir.replace("res://", "");
  392. Vector<String> dirs;
  393. String f;
  394. while ((f = da->get_next()) != "") {
  395. if (da->current_is_dir()) {
  396. dirs.push_back(f);
  397. } else {
  398. String fullpath = cur_dir + f;
  399. // Test also against path without res:// so that filters like `file.txt` can work.
  400. String fullpath_no_prefix = cur_dir_no_prefix + f;
  401. for (int i = 0; i < p_filters.size(); ++i) {
  402. if (fullpath.matchn(p_filters[i]) || fullpath_no_prefix.matchn(p_filters[i])) {
  403. if (!exclude) {
  404. r_list.insert(fullpath);
  405. } else {
  406. r_list.erase(fullpath);
  407. }
  408. }
  409. }
  410. }
  411. }
  412. da->list_dir_end();
  413. for (int i = 0; i < dirs.size(); ++i) {
  414. String dir = dirs[i];
  415. if (dir.begins_with(".")) {
  416. continue;
  417. }
  418. if (EditorFileSystem::_should_skip_directory(cur_dir + dir)) {
  419. continue;
  420. }
  421. da->change_dir(dir);
  422. _edit_files_with_filter(da, p_filters, r_list, exclude);
  423. da->change_dir("..");
  424. }
  425. }
  426. void EditorExportPlatform::_edit_filter_list(Set<String> &r_list, const String &p_filter, bool exclude) {
  427. if (p_filter == "") {
  428. return;
  429. }
  430. Vector<String> split = p_filter.split(",");
  431. Vector<String> filters;
  432. for (int i = 0; i < split.size(); i++) {
  433. String f = split[i].strip_edges();
  434. if (f.is_empty()) {
  435. continue;
  436. }
  437. filters.push_back(f);
  438. }
  439. DirAccess *da = DirAccess::open("res://");
  440. ERR_FAIL_NULL(da);
  441. _edit_files_with_filter(da, filters, r_list, exclude);
  442. memdelete(da);
  443. }
  444. void EditorExportPlugin::set_export_preset(const Ref<EditorExportPreset> &p_preset) {
  445. if (p_preset.is_valid()) {
  446. export_preset = p_preset;
  447. }
  448. }
  449. Ref<EditorExportPreset> EditorExportPlugin::get_export_preset() const {
  450. return export_preset;
  451. }
  452. void EditorExportPlugin::add_file(const String &p_path, const Vector<uint8_t> &p_file, bool p_remap) {
  453. ExtraFile ef;
  454. ef.data = p_file;
  455. ef.path = p_path;
  456. ef.remap = p_remap;
  457. extra_files.push_back(ef);
  458. }
  459. void EditorExportPlugin::add_shared_object(const String &p_path, const Vector<String> &tags) {
  460. shared_objects.push_back(SharedObject(p_path, tags));
  461. }
  462. void EditorExportPlugin::add_ios_framework(const String &p_path) {
  463. ios_frameworks.push_back(p_path);
  464. }
  465. void EditorExportPlugin::add_ios_embedded_framework(const String &p_path) {
  466. ios_embedded_frameworks.push_back(p_path);
  467. }
  468. Vector<String> EditorExportPlugin::get_ios_frameworks() const {
  469. return ios_frameworks;
  470. }
  471. Vector<String> EditorExportPlugin::get_ios_embedded_frameworks() const {
  472. return ios_embedded_frameworks;
  473. }
  474. void EditorExportPlugin::add_ios_plist_content(const String &p_plist_content) {
  475. ios_plist_content += p_plist_content + "\n";
  476. }
  477. String EditorExportPlugin::get_ios_plist_content() const {
  478. return ios_plist_content;
  479. }
  480. void EditorExportPlugin::add_ios_linker_flags(const String &p_flags) {
  481. if (ios_linker_flags.length() > 0) {
  482. ios_linker_flags += ' ';
  483. }
  484. ios_linker_flags += p_flags;
  485. }
  486. String EditorExportPlugin::get_ios_linker_flags() const {
  487. return ios_linker_flags;
  488. }
  489. void EditorExportPlugin::add_ios_bundle_file(const String &p_path) {
  490. ios_bundle_files.push_back(p_path);
  491. }
  492. Vector<String> EditorExportPlugin::get_ios_bundle_files() const {
  493. return ios_bundle_files;
  494. }
  495. void EditorExportPlugin::add_ios_cpp_code(const String &p_code) {
  496. ios_cpp_code += p_code;
  497. }
  498. String EditorExportPlugin::get_ios_cpp_code() const {
  499. return ios_cpp_code;
  500. }
  501. void EditorExportPlugin::add_ios_project_static_lib(const String &p_path) {
  502. ios_project_static_libs.push_back(p_path);
  503. }
  504. Vector<String> EditorExportPlugin::get_ios_project_static_libs() const {
  505. return ios_project_static_libs;
  506. }
  507. void EditorExportPlugin::_export_file_script(const String &p_path, const String &p_type, const Vector<String> &p_features) {
  508. if (get_script_instance()) {
  509. get_script_instance()->call("_export_file", p_path, p_type, p_features);
  510. }
  511. }
  512. void EditorExportPlugin::_export_begin_script(const Vector<String> &p_features, bool p_debug, const String &p_path, int p_flags) {
  513. if (get_script_instance()) {
  514. get_script_instance()->call("_export_begin", p_features, p_debug, p_path, p_flags);
  515. }
  516. }
  517. void EditorExportPlugin::_export_end_script() {
  518. if (get_script_instance()) {
  519. get_script_instance()->call("_export_end");
  520. }
  521. }
  522. void EditorExportPlugin::_export_file(const String &p_path, const String &p_type, const Set<String> &p_features) {
  523. }
  524. void EditorExportPlugin::_export_begin(const Set<String> &p_features, bool p_debug, const String &p_path, int p_flags) {
  525. }
  526. void EditorExportPlugin::skip() {
  527. skipped = true;
  528. }
  529. void EditorExportPlugin::_bind_methods() {
  530. ClassDB::bind_method(D_METHOD("add_shared_object", "path", "tags"), &EditorExportPlugin::add_shared_object);
  531. ClassDB::bind_method(D_METHOD("add_ios_project_static_lib", "path"), &EditorExportPlugin::add_ios_project_static_lib);
  532. ClassDB::bind_method(D_METHOD("add_file", "path", "file", "remap"), &EditorExportPlugin::add_file);
  533. ClassDB::bind_method(D_METHOD("add_ios_framework", "path"), &EditorExportPlugin::add_ios_framework);
  534. ClassDB::bind_method(D_METHOD("add_ios_embedded_framework", "path"), &EditorExportPlugin::add_ios_embedded_framework);
  535. ClassDB::bind_method(D_METHOD("add_ios_plist_content", "plist_content"), &EditorExportPlugin::add_ios_plist_content);
  536. ClassDB::bind_method(D_METHOD("add_ios_linker_flags", "flags"), &EditorExportPlugin::add_ios_linker_flags);
  537. ClassDB::bind_method(D_METHOD("add_ios_bundle_file", "path"), &EditorExportPlugin::add_ios_bundle_file);
  538. ClassDB::bind_method(D_METHOD("add_ios_cpp_code", "code"), &EditorExportPlugin::add_ios_cpp_code);
  539. ClassDB::bind_method(D_METHOD("skip"), &EditorExportPlugin::skip);
  540. BIND_VMETHOD(MethodInfo("_export_file", PropertyInfo(Variant::STRING, "path"), PropertyInfo(Variant::STRING, "type"), PropertyInfo(Variant::PACKED_STRING_ARRAY, "features")));
  541. BIND_VMETHOD(MethodInfo("_export_begin", PropertyInfo(Variant::PACKED_STRING_ARRAY, "features"), PropertyInfo(Variant::BOOL, "is_debug"), PropertyInfo(Variant::STRING, "path"), PropertyInfo(Variant::INT, "flags")));
  542. BIND_VMETHOD(MethodInfo("_export_end"));
  543. }
  544. EditorExportPlugin::EditorExportPlugin() {
  545. skipped = false;
  546. }
  547. EditorExportPlatform::FeatureContainers EditorExportPlatform::get_feature_containers(const Ref<EditorExportPreset> &p_preset) {
  548. Ref<EditorExportPlatform> platform = p_preset->get_platform();
  549. List<String> feature_list;
  550. platform->get_platform_features(&feature_list);
  551. platform->get_preset_features(p_preset, &feature_list);
  552. FeatureContainers result;
  553. for (List<String>::Element *E = feature_list.front(); E; E = E->next()) {
  554. result.features.insert(E->get());
  555. result.features_pv.push_back(E->get());
  556. }
  557. if (p_preset->get_custom_features() != String()) {
  558. Vector<String> tmp_custom_list = p_preset->get_custom_features().split(",");
  559. for (int i = 0; i < tmp_custom_list.size(); i++) {
  560. String f = tmp_custom_list[i].strip_edges();
  561. if (f != String()) {
  562. result.features.insert(f);
  563. result.features_pv.push_back(f);
  564. }
  565. }
  566. }
  567. return result;
  568. }
  569. EditorExportPlatform::ExportNotifier::ExportNotifier(EditorExportPlatform &p_platform, const Ref<EditorExportPreset> &p_preset, bool p_debug, const String &p_path, int p_flags) {
  570. FeatureContainers features = p_platform.get_feature_containers(p_preset);
  571. Vector<Ref<EditorExportPlugin>> export_plugins = EditorExport::get_singleton()->get_export_plugins();
  572. //initial export plugin callback
  573. for (int i = 0; i < export_plugins.size(); i++) {
  574. if (export_plugins[i]->get_script_instance()) { //script based
  575. export_plugins.write[i]->_export_begin_script(features.features_pv, p_debug, p_path, p_flags);
  576. } else {
  577. export_plugins.write[i]->_export_begin(features.features, p_debug, p_path, p_flags);
  578. }
  579. }
  580. }
  581. EditorExportPlatform::ExportNotifier::~ExportNotifier() {
  582. Vector<Ref<EditorExportPlugin>> export_plugins = EditorExport::get_singleton()->get_export_plugins();
  583. for (int i = 0; i < export_plugins.size(); i++) {
  584. if (export_plugins[i]->get_script_instance()) {
  585. export_plugins.write[i]->_export_end_script();
  586. }
  587. export_plugins.write[i]->_export_end();
  588. }
  589. }
  590. Error EditorExportPlatform::export_project_files(const Ref<EditorExportPreset> &p_preset, EditorExportSaveFunction p_func, void *p_udata, EditorExportSaveSharedObject p_so_func) {
  591. //figure out paths of files that will be exported
  592. Set<String> paths;
  593. Vector<String> path_remaps;
  594. if (p_preset->get_export_filter() == EditorExportPreset::EXPORT_ALL_RESOURCES) {
  595. //find stuff
  596. _export_find_resources(EditorFileSystem::get_singleton()->get_filesystem(), paths);
  597. } else if (p_preset->get_export_filter() == EditorExportPreset::EXCLUDE_SELECTED_RESOURCES) {
  598. _export_find_resources(EditorFileSystem::get_singleton()->get_filesystem(), paths);
  599. Vector<String> files = p_preset->get_files_to_export();
  600. for (int i = 0; i < files.size(); i++) {
  601. paths.erase(files[i]);
  602. }
  603. } else {
  604. bool scenes_only = p_preset->get_export_filter() == EditorExportPreset::EXPORT_SELECTED_SCENES;
  605. Vector<String> files = p_preset->get_files_to_export();
  606. for (int i = 0; i < files.size(); i++) {
  607. if (scenes_only && ResourceLoader::get_resource_type(files[i]) != "PackedScene") {
  608. continue;
  609. }
  610. _export_find_dependencies(files[i], paths);
  611. }
  612. // Add autoload resources and their dependencies
  613. List<PropertyInfo> props;
  614. ProjectSettings::get_singleton()->get_property_list(&props);
  615. for (List<PropertyInfo>::Element *E = props.front(); E; E = E->next()) {
  616. const PropertyInfo &pi = E->get();
  617. if (!pi.name.begins_with("autoload/")) {
  618. continue;
  619. }
  620. String autoload_path = ProjectSettings::get_singleton()->get(pi.name);
  621. if (autoload_path.begins_with("*")) {
  622. autoload_path = autoload_path.substr(1);
  623. }
  624. _export_find_dependencies(autoload_path, paths);
  625. }
  626. }
  627. //add native icons to non-resource include list
  628. _edit_filter_list(paths, String("*.icns"), false);
  629. _edit_filter_list(paths, String("*.ico"), false);
  630. _edit_filter_list(paths, p_preset->get_include_filter(), false);
  631. _edit_filter_list(paths, p_preset->get_exclude_filter(), true);
  632. // Ignore import files, since these are automatically added to the jar later with the resources
  633. _edit_filter_list(paths, String("*.import"), true);
  634. // Get encryption filters.
  635. bool enc_pck = p_preset->get_enc_pck();
  636. Vector<String> enc_in_filters;
  637. Vector<String> enc_ex_filters;
  638. Vector<uint8_t> key;
  639. if (enc_pck) {
  640. Vector<String> enc_in_split = p_preset->get_enc_in_filter().split(",");
  641. for (int i = 0; i < enc_in_split.size(); i++) {
  642. String f = enc_in_split[i].strip_edges();
  643. if (f.is_empty()) {
  644. continue;
  645. }
  646. enc_in_filters.push_back(f);
  647. }
  648. Vector<String> enc_ex_split = p_preset->get_enc_ex_filter().split(",");
  649. for (int i = 0; i < enc_ex_split.size(); i++) {
  650. String f = enc_ex_split[i].strip_edges();
  651. if (f.is_empty()) {
  652. continue;
  653. }
  654. enc_ex_filters.push_back(f);
  655. }
  656. // Get encryption key.
  657. String script_key = p_preset->get_script_encryption_key().to_lower();
  658. key.resize(32);
  659. if (script_key.length() == 64) {
  660. for (int i = 0; i < 32; i++) {
  661. int v = 0;
  662. if (i * 2 < script_key.length()) {
  663. char32_t ct = script_key[i * 2];
  664. if (ct >= '0' && ct <= '9') {
  665. ct = ct - '0';
  666. } else if (ct >= 'a' && ct <= 'f') {
  667. ct = 10 + ct - 'a';
  668. }
  669. v |= ct << 4;
  670. }
  671. if (i * 2 + 1 < script_key.length()) {
  672. char32_t ct = script_key[i * 2 + 1];
  673. if (ct >= '0' && ct <= '9') {
  674. ct = ct - '0';
  675. } else if (ct >= 'a' && ct <= 'f') {
  676. ct = 10 + ct - 'a';
  677. }
  678. v |= ct;
  679. }
  680. key.write[i] = v;
  681. }
  682. }
  683. }
  684. Error err = OK;
  685. Vector<Ref<EditorExportPlugin>> export_plugins = EditorExport::get_singleton()->get_export_plugins();
  686. for (int i = 0; i < export_plugins.size(); i++) {
  687. export_plugins.write[i]->set_export_preset(p_preset);
  688. if (p_so_func) {
  689. for (int j = 0; j < export_plugins[i]->shared_objects.size(); j++) {
  690. err = p_so_func(p_udata, export_plugins[i]->shared_objects[j]);
  691. if (err != OK) {
  692. return err;
  693. }
  694. }
  695. }
  696. for (int j = 0; j < export_plugins[i]->extra_files.size(); j++) {
  697. 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);
  698. if (err != OK) {
  699. return err;
  700. }
  701. }
  702. export_plugins.write[i]->_clear();
  703. }
  704. FeatureContainers feature_containers = get_feature_containers(p_preset);
  705. Set<String> &features = feature_containers.features;
  706. Vector<String> &features_pv = feature_containers.features_pv;
  707. //store everything in the export medium
  708. int idx = 0;
  709. int total = paths.size();
  710. for (Set<String>::Element *E = paths.front(); E; E = E->next()) {
  711. String path = E->get();
  712. String type = ResourceLoader::get_resource_type(path);
  713. if (FileAccess::exists(path + ".import")) {
  714. //file is imported, replace by what it imports
  715. Ref<ConfigFile> config;
  716. config.instance();
  717. err = config->load(path + ".import");
  718. if (err != OK) {
  719. ERR_PRINT("Could not parse: '" + path + "', not exported.");
  720. continue;
  721. }
  722. String importer_type = config->get_value("remap", "importer");
  723. if (importer_type == "keep") {
  724. //just keep file as-is
  725. Vector<uint8_t> array = FileAccess::get_file_as_array(path);
  726. err = p_func(p_udata, path, array, idx, total, enc_in_filters, enc_ex_filters, key);
  727. if (err != OK) {
  728. return err;
  729. }
  730. continue;
  731. }
  732. List<String> remaps;
  733. config->get_section_keys("remap", &remaps);
  734. Set<String> remap_features;
  735. for (List<String>::Element *F = remaps.front(); F; F = F->next()) {
  736. String remap = F->get();
  737. String feature = remap.get_slice(".", 1);
  738. if (features.has(feature)) {
  739. remap_features.insert(feature);
  740. }
  741. }
  742. if (remap_features.size() > 1) {
  743. this->resolve_platform_feature_priorities(p_preset, remap_features);
  744. }
  745. err = OK;
  746. for (List<String>::Element *F = remaps.front(); F; F = F->next()) {
  747. String remap = F->get();
  748. if (remap == "path") {
  749. String remapped_path = config->get_value("remap", remap);
  750. Vector<uint8_t> array = FileAccess::get_file_as_array(remapped_path);
  751. err = p_func(p_udata, remapped_path, array, idx, total, enc_in_filters, enc_ex_filters, key);
  752. } else if (remap.begins_with("path.")) {
  753. String feature = remap.get_slice(".", 1);
  754. if (remap_features.has(feature)) {
  755. String remapped_path = config->get_value("remap", remap);
  756. Vector<uint8_t> array = FileAccess::get_file_as_array(remapped_path);
  757. err = p_func(p_udata, remapped_path, array, idx, total, enc_in_filters, enc_ex_filters, key);
  758. }
  759. }
  760. }
  761. if (err != OK) {
  762. return err;
  763. }
  764. //also save the .import file
  765. Vector<uint8_t> array = FileAccess::get_file_as_array(path + ".import");
  766. err = p_func(p_udata, path + ".import", array, idx, total, enc_in_filters, enc_ex_filters, key);
  767. if (err != OK) {
  768. return err;
  769. }
  770. } else {
  771. bool do_export = true;
  772. for (int i = 0; i < export_plugins.size(); i++) {
  773. if (export_plugins[i]->get_script_instance()) { //script based
  774. export_plugins.write[i]->_export_file_script(path, type, features_pv);
  775. } else {
  776. export_plugins.write[i]->_export_file(path, type, features);
  777. }
  778. if (p_so_func) {
  779. for (int j = 0; j < export_plugins[i]->shared_objects.size(); j++) {
  780. err = p_so_func(p_udata, export_plugins[i]->shared_objects[j]);
  781. if (err != OK) {
  782. return err;
  783. }
  784. }
  785. }
  786. for (int j = 0; j < export_plugins[i]->extra_files.size(); j++) {
  787. 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);
  788. if (err != OK) {
  789. return err;
  790. }
  791. if (export_plugins[i]->extra_files[j].remap) {
  792. do_export = false; //if remap, do not
  793. path_remaps.push_back(path);
  794. path_remaps.push_back(export_plugins[i]->extra_files[j].path);
  795. }
  796. }
  797. if (export_plugins[i]->skipped) {
  798. do_export = false;
  799. }
  800. export_plugins.write[i]->_clear();
  801. if (!do_export) {
  802. break; //apologies, not exporting
  803. }
  804. }
  805. //just store it as it comes
  806. if (do_export) {
  807. Vector<uint8_t> array = FileAccess::get_file_as_array(path);
  808. err = p_func(p_udata, path, array, idx, total, enc_in_filters, enc_ex_filters, key);
  809. if (err != OK) {
  810. return err;
  811. }
  812. }
  813. }
  814. idx++;
  815. }
  816. //save config!
  817. Vector<String> custom_list;
  818. if (p_preset->get_custom_features() != String()) {
  819. Vector<String> tmp_custom_list = p_preset->get_custom_features().split(",");
  820. for (int i = 0; i < tmp_custom_list.size(); i++) {
  821. String f = tmp_custom_list[i].strip_edges();
  822. if (f != String()) {
  823. custom_list.push_back(f);
  824. }
  825. }
  826. }
  827. ProjectSettings::CustomMap custom_map;
  828. if (path_remaps.size()) {
  829. if (true) { //new remap mode, use always as it's friendlier with multiple .pck exports
  830. for (int i = 0; i < path_remaps.size(); i += 2) {
  831. String from = path_remaps[i];
  832. String to = path_remaps[i + 1];
  833. String remap_file = "[remap]\n\npath=\"" + to.c_escape() + "\"\n";
  834. CharString utf8 = remap_file.utf8();
  835. Vector<uint8_t> new_file;
  836. new_file.resize(utf8.length());
  837. for (int j = 0; j < utf8.length(); j++) {
  838. new_file.write[j] = utf8[j];
  839. }
  840. err = p_func(p_udata, from + ".remap", new_file, idx, total, enc_in_filters, enc_ex_filters, key);
  841. if (err != OK) {
  842. return err;
  843. }
  844. }
  845. } else {
  846. //old remap mode, will still work, but it's unused because it's not multiple pck export friendly
  847. custom_map["path_remap/remapped_paths"] = path_remaps;
  848. }
  849. }
  850. // Store icon and splash images directly, they need to bypass the import system and be loaded as images
  851. String icon = ProjectSettings::get_singleton()->get("application/config/icon");
  852. String splash = ProjectSettings::get_singleton()->get("application/boot_splash/image");
  853. if (icon != String() && FileAccess::exists(icon)) {
  854. Vector<uint8_t> array = FileAccess::get_file_as_array(icon);
  855. err = p_func(p_udata, icon, array, idx, total, enc_in_filters, enc_ex_filters, key);
  856. if (err != OK) {
  857. return err;
  858. }
  859. }
  860. if (splash != String() && FileAccess::exists(splash) && icon != splash) {
  861. Vector<uint8_t> array = FileAccess::get_file_as_array(splash);
  862. err = p_func(p_udata, splash, array, idx, total, enc_in_filters, enc_ex_filters, key);
  863. if (err != OK) {
  864. return err;
  865. }
  866. }
  867. // Store text server data if it is supported.
  868. if (TS->has_feature(TextServer::FEATURE_USE_SUPPORT_DATA)) {
  869. bool use_data = ProjectSettings::get_singleton()->get("internationalization/locale/include_text_server_data");
  870. if (use_data) {
  871. // Try using user provided data file.
  872. String ts_data = "res://" + TS->get_support_data_filename();
  873. if (FileAccess::exists(ts_data)) {
  874. Vector<uint8_t> array = FileAccess::get_file_as_array(ts_data);
  875. err = p_func(p_udata, ts_data, array, idx, total, enc_in_filters, enc_ex_filters, key);
  876. if (err != OK) {
  877. return err;
  878. }
  879. } else {
  880. // Use default text server data.
  881. String icu_data_file = EditorPaths::get_singleton()->get_cache_dir().plus_file("tmp_icu_data");
  882. TS->save_support_data(icu_data_file);
  883. Vector<uint8_t> array = FileAccess::get_file_as_array(icu_data_file);
  884. err = p_func(p_udata, ts_data, array, idx, total, enc_in_filters, enc_ex_filters, key);
  885. DirAccess::remove_file_or_error(icu_data_file);
  886. if (err != OK) {
  887. return err;
  888. }
  889. }
  890. }
  891. }
  892. String config_file = "project.binary";
  893. String engine_cfb = EditorPaths::get_singleton()->get_cache_dir().plus_file("tmp" + config_file);
  894. ProjectSettings::get_singleton()->save_custom(engine_cfb, custom_map, custom_list);
  895. Vector<uint8_t> data = FileAccess::get_file_as_array(engine_cfb);
  896. DirAccess::remove_file_or_error(engine_cfb);
  897. return p_func(p_udata, "res://" + config_file, data, idx, total, enc_in_filters, enc_ex_filters, key);
  898. }
  899. Error EditorExportPlatform::_add_shared_object(void *p_userdata, const SharedObject &p_so) {
  900. PackData *pack_data = (PackData *)p_userdata;
  901. if (pack_data->so_files) {
  902. pack_data->so_files->push_back(p_so);
  903. }
  904. return OK;
  905. }
  906. Error EditorExportPlatform::save_pack(const Ref<EditorExportPreset> &p_preset, const String &p_path, Vector<SharedObject> *p_so_files, bool p_embed, int64_t *r_embedded_start, int64_t *r_embedded_size) {
  907. EditorProgress ep("savepack", TTR("Packing"), 102, true);
  908. // Create the temporary export directory if it doesn't exist.
  909. DirAccessRef da = DirAccess::create(DirAccess::ACCESS_FILESYSTEM);
  910. da->make_dir_recursive(EditorPaths::get_singleton()->get_cache_dir());
  911. String tmppath = EditorPaths::get_singleton()->get_cache_dir().plus_file("packtmp");
  912. FileAccess *ftmp = FileAccess::open(tmppath, FileAccess::WRITE);
  913. ERR_FAIL_COND_V_MSG(!ftmp, ERR_CANT_CREATE, "Cannot create file '" + tmppath + "'.");
  914. PackData pd;
  915. pd.ep = &ep;
  916. pd.f = ftmp;
  917. pd.so_files = p_so_files;
  918. Error err = export_project_files(p_preset, _save_pack_file, &pd, _add_shared_object);
  919. memdelete(ftmp); //close tmp file
  920. if (err != OK) {
  921. DirAccess::remove_file_or_error(tmppath);
  922. ERR_PRINT("Failed to export project files");
  923. return err;
  924. }
  925. pd.file_ofs.sort(); //do sort, so we can do binary search later
  926. FileAccess *f;
  927. int64_t embed_pos = 0;
  928. if (!p_embed) {
  929. // Regular output to separate PCK file
  930. f = FileAccess::open(p_path, FileAccess::WRITE);
  931. if (!f) {
  932. DirAccess::remove_file_or_error(tmppath);
  933. ERR_FAIL_V(ERR_CANT_CREATE);
  934. }
  935. } else {
  936. // Append to executable
  937. f = FileAccess::open(p_path, FileAccess::READ_WRITE);
  938. if (!f) {
  939. DirAccess::remove_file_or_error(tmppath);
  940. ERR_FAIL_V(ERR_FILE_CANT_OPEN);
  941. }
  942. f->seek_end();
  943. embed_pos = f->get_position();
  944. if (r_embedded_start) {
  945. *r_embedded_start = embed_pos;
  946. }
  947. // Ensure embedded PCK starts at a 64-bit multiple
  948. int pad = f->get_position() % 8;
  949. for (int i = 0; i < pad; i++) {
  950. f->store_8(0);
  951. }
  952. }
  953. int64_t pck_start_pos = f->get_position();
  954. f->store_32(PACK_HEADER_MAGIC);
  955. f->store_32(PACK_FORMAT_VERSION);
  956. f->store_32(VERSION_MAJOR);
  957. f->store_32(VERSION_MINOR);
  958. f->store_32(VERSION_PATCH);
  959. uint32_t pack_flags = 0;
  960. bool enc_pck = p_preset->get_enc_pck();
  961. bool enc_directory = p_preset->get_enc_directory();
  962. if (enc_pck && enc_directory) {
  963. pack_flags |= PACK_DIR_ENCRYPTED;
  964. }
  965. f->store_32(pack_flags); // flags
  966. uint64_t file_base_ofs = f->get_position();
  967. f->store_64(0); // files base
  968. for (int i = 0; i < 16; i++) {
  969. //reserved
  970. f->store_32(0);
  971. }
  972. f->store_32(pd.file_ofs.size()); //amount of files
  973. FileAccessEncrypted *fae = nullptr;
  974. FileAccess *fhead = f;
  975. if (enc_pck && enc_directory) {
  976. String script_key = p_preset->get_script_encryption_key().to_lower();
  977. Vector<uint8_t> key;
  978. key.resize(32);
  979. if (script_key.length() == 64) {
  980. for (int i = 0; i < 32; i++) {
  981. int v = 0;
  982. if (i * 2 < script_key.length()) {
  983. char32_t ct = script_key[i * 2];
  984. if (ct >= '0' && ct <= '9') {
  985. ct = ct - '0';
  986. } else if (ct >= 'a' && ct <= 'f') {
  987. ct = 10 + ct - 'a';
  988. }
  989. v |= ct << 4;
  990. }
  991. if (i * 2 + 1 < script_key.length()) {
  992. char32_t ct = script_key[i * 2 + 1];
  993. if (ct >= '0' && ct <= '9') {
  994. ct = ct - '0';
  995. } else if (ct >= 'a' && ct <= 'f') {
  996. ct = 10 + ct - 'a';
  997. }
  998. v |= ct;
  999. }
  1000. key.write[i] = v;
  1001. }
  1002. }
  1003. fae = memnew(FileAccessEncrypted);
  1004. ERR_FAIL_COND_V(!fae, ERR_SKIP);
  1005. err = fae->open_and_parse(f, key, FileAccessEncrypted::MODE_WRITE_AES256, false);
  1006. ERR_FAIL_COND_V(err != OK, ERR_SKIP);
  1007. fhead = fae;
  1008. }
  1009. for (int i = 0; i < pd.file_ofs.size(); i++) {
  1010. uint32_t string_len = pd.file_ofs[i].path_utf8.length();
  1011. uint32_t pad = _get_pad(4, string_len);
  1012. fhead->store_32(string_len + pad);
  1013. fhead->store_buffer((const uint8_t *)pd.file_ofs[i].path_utf8.get_data(), string_len);
  1014. for (uint32_t j = 0; j < pad; j++) {
  1015. fhead->store_8(0);
  1016. }
  1017. fhead->store_64(pd.file_ofs[i].ofs);
  1018. fhead->store_64(pd.file_ofs[i].size); // pay attention here, this is where file is
  1019. fhead->store_buffer(pd.file_ofs[i].md5.ptr(), 16); //also save md5 for file
  1020. uint32_t flags = 0;
  1021. if (pd.file_ofs[i].encrypted) {
  1022. flags |= PACK_FILE_ENCRYPTED;
  1023. }
  1024. fhead->store_32(flags);
  1025. }
  1026. if (fae) {
  1027. fae->release();
  1028. memdelete(fae);
  1029. }
  1030. int header_padding = _get_pad(PCK_PADDING, f->get_position());
  1031. for (int i = 0; i < header_padding; i++) {
  1032. f->store_8(Math::rand() % 256);
  1033. }
  1034. uint64_t file_base = f->get_position();
  1035. f->seek(file_base_ofs);
  1036. f->store_64(file_base); // update files base
  1037. f->seek(file_base);
  1038. // Save the rest of the data.
  1039. ftmp = FileAccess::open(tmppath, FileAccess::READ);
  1040. if (!ftmp) {
  1041. memdelete(f);
  1042. DirAccess::remove_file_or_error(tmppath);
  1043. ERR_FAIL_V_MSG(ERR_CANT_CREATE, "Can't open file to read from path '" + String(tmppath) + "'.");
  1044. }
  1045. const int bufsize = 16384;
  1046. uint8_t buf[bufsize];
  1047. while (true) {
  1048. uint64_t got = ftmp->get_buffer(buf, bufsize);
  1049. if (got == 0) {
  1050. break;
  1051. }
  1052. f->store_buffer(buf, got);
  1053. }
  1054. memdelete(ftmp);
  1055. if (p_embed) {
  1056. // Ensure embedded data ends at a 64-bit multiple
  1057. uint64_t embed_end = f->get_position() - embed_pos + 12;
  1058. uint64_t pad = embed_end % 8;
  1059. for (uint64_t i = 0; i < pad; i++) {
  1060. f->store_8(0);
  1061. }
  1062. uint64_t pck_size = f->get_position() - pck_start_pos;
  1063. f->store_64(pck_size);
  1064. f->store_32(PACK_HEADER_MAGIC);
  1065. if (r_embedded_size) {
  1066. *r_embedded_size = f->get_position() - embed_pos;
  1067. }
  1068. }
  1069. memdelete(f);
  1070. DirAccess::remove_file_or_error(tmppath);
  1071. return OK;
  1072. }
  1073. Error EditorExportPlatform::save_zip(const Ref<EditorExportPreset> &p_preset, const String &p_path) {
  1074. EditorProgress ep("savezip", TTR("Packing"), 102, true);
  1075. FileAccess *src_f;
  1076. zlib_filefunc_def io = zipio_create_io_from_file(&src_f);
  1077. zipFile zip = zipOpen2(p_path.utf8().get_data(), APPEND_STATUS_CREATE, nullptr, &io);
  1078. ZipData zd;
  1079. zd.ep = &ep;
  1080. zd.zip = zip;
  1081. Error err = export_project_files(p_preset, _save_zip_file, &zd);
  1082. if (err != OK && err != ERR_SKIP) {
  1083. ERR_PRINT("Failed to export project files");
  1084. }
  1085. zipClose(zip, nullptr);
  1086. return OK;
  1087. }
  1088. Error EditorExportPlatform::export_pack(const Ref<EditorExportPreset> &p_preset, bool p_debug, const String &p_path, int p_flags) {
  1089. ExportNotifier notifier(*this, p_preset, p_debug, p_path, p_flags);
  1090. return save_pack(p_preset, p_path);
  1091. }
  1092. Error EditorExportPlatform::export_zip(const Ref<EditorExportPreset> &p_preset, bool p_debug, const String &p_path, int p_flags) {
  1093. ExportNotifier notifier(*this, p_preset, p_debug, p_path, p_flags);
  1094. return save_zip(p_preset, p_path);
  1095. }
  1096. void EditorExportPlatform::gen_export_flags(Vector<String> &r_flags, int p_flags) {
  1097. String host = EditorSettings::get_singleton()->get("network/debug/remote_host");
  1098. int remote_port = (int)EditorSettings::get_singleton()->get("network/debug/remote_port");
  1099. if (p_flags & DEBUG_FLAG_REMOTE_DEBUG_LOCALHOST) {
  1100. host = "localhost";
  1101. }
  1102. if (p_flags & DEBUG_FLAG_DUMB_CLIENT) {
  1103. int port = EditorSettings::get_singleton()->get("filesystem/file_server/port");
  1104. String passwd = EditorSettings::get_singleton()->get("filesystem/file_server/password");
  1105. r_flags.push_back("--remote-fs");
  1106. r_flags.push_back(host + ":" + itos(port));
  1107. if (passwd != "") {
  1108. r_flags.push_back("--remote-fs-password");
  1109. r_flags.push_back(passwd);
  1110. }
  1111. }
  1112. if (p_flags & DEBUG_FLAG_REMOTE_DEBUG) {
  1113. r_flags.push_back("--remote-debug");
  1114. r_flags.push_back(get_debug_protocol() + host + ":" + String::num(remote_port));
  1115. List<String> breakpoints;
  1116. ScriptEditor::get_singleton()->get_breakpoints(&breakpoints);
  1117. if (breakpoints.size()) {
  1118. r_flags.push_back("--breakpoints");
  1119. String bpoints;
  1120. for (const List<String>::Element *E = breakpoints.front(); E; E = E->next()) {
  1121. bpoints += E->get().replace(" ", "%20");
  1122. if (E->next()) {
  1123. bpoints += ",";
  1124. }
  1125. }
  1126. r_flags.push_back(bpoints);
  1127. }
  1128. }
  1129. if (p_flags & DEBUG_FLAG_VIEW_COLLISONS) {
  1130. r_flags.push_back("--debug-collisions");
  1131. }
  1132. if (p_flags & DEBUG_FLAG_VIEW_NAVIGATION) {
  1133. r_flags.push_back("--debug-navigation");
  1134. }
  1135. }
  1136. EditorExportPlatform::EditorExportPlatform() {
  1137. }
  1138. ////
  1139. EditorExport *EditorExport::singleton = nullptr;
  1140. void EditorExport::_save() {
  1141. Ref<ConfigFile> config;
  1142. config.instance();
  1143. for (int i = 0; i < export_presets.size(); i++) {
  1144. Ref<EditorExportPreset> preset = export_presets[i];
  1145. String section = "preset." + itos(i);
  1146. config->set_value(section, "name", preset->get_name());
  1147. config->set_value(section, "platform", preset->get_platform()->get_name());
  1148. config->set_value(section, "runnable", preset->is_runnable());
  1149. config->set_value(section, "custom_features", preset->get_custom_features());
  1150. bool save_files = false;
  1151. switch (preset->get_export_filter()) {
  1152. case EditorExportPreset::EXPORT_ALL_RESOURCES: {
  1153. config->set_value(section, "export_filter", "all_resources");
  1154. } break;
  1155. case EditorExportPreset::EXPORT_SELECTED_SCENES: {
  1156. config->set_value(section, "export_filter", "scenes");
  1157. save_files = true;
  1158. } break;
  1159. case EditorExportPreset::EXPORT_SELECTED_RESOURCES: {
  1160. config->set_value(section, "export_filter", "resources");
  1161. save_files = true;
  1162. } break;
  1163. case EditorExportPreset::EXCLUDE_SELECTED_RESOURCES: {
  1164. config->set_value(section, "export_filter", "exclude");
  1165. save_files = true;
  1166. } break;
  1167. }
  1168. if (save_files) {
  1169. Vector<String> export_files = preset->get_files_to_export();
  1170. config->set_value(section, "export_files", export_files);
  1171. }
  1172. config->set_value(section, "include_filter", preset->get_include_filter());
  1173. config->set_value(section, "exclude_filter", preset->get_exclude_filter());
  1174. config->set_value(section, "export_path", preset->get_export_path());
  1175. config->set_value(section, "encryption_include_filters", preset->get_enc_in_filter());
  1176. config->set_value(section, "encryption_exclude_filters", preset->get_enc_ex_filter());
  1177. config->set_value(section, "encrypt_pck", preset->get_enc_pck());
  1178. config->set_value(section, "encrypt_directory", preset->get_enc_directory());
  1179. config->set_value(section, "script_export_mode", preset->get_script_export_mode());
  1180. config->set_value(section, "script_encryption_key", preset->get_script_encryption_key());
  1181. String option_section = "preset." + itos(i) + ".options";
  1182. for (const List<PropertyInfo>::Element *E = preset->get_properties().front(); E; E = E->next()) {
  1183. config->set_value(option_section, E->get().name, preset->get(E->get().name));
  1184. }
  1185. }
  1186. config->save("res://export_presets.cfg");
  1187. }
  1188. void EditorExport::save_presets() {
  1189. if (block_save) {
  1190. return;
  1191. }
  1192. save_timer->start();
  1193. }
  1194. void EditorExport::_bind_methods() {
  1195. ADD_SIGNAL(MethodInfo("export_presets_updated"));
  1196. }
  1197. void EditorExport::add_export_platform(const Ref<EditorExportPlatform> &p_platform) {
  1198. export_platforms.push_back(p_platform);
  1199. }
  1200. int EditorExport::get_export_platform_count() {
  1201. return export_platforms.size();
  1202. }
  1203. Ref<EditorExportPlatform> EditorExport::get_export_platform(int p_idx) {
  1204. ERR_FAIL_INDEX_V(p_idx, export_platforms.size(), Ref<EditorExportPlatform>());
  1205. return export_platforms[p_idx];
  1206. }
  1207. void EditorExport::add_export_preset(const Ref<EditorExportPreset> &p_preset, int p_at_pos) {
  1208. if (p_at_pos < 0) {
  1209. export_presets.push_back(p_preset);
  1210. } else {
  1211. export_presets.insert(p_at_pos, p_preset);
  1212. }
  1213. }
  1214. String EditorExportPlatform::test_etc2() const {
  1215. String driver = ProjectSettings::get_singleton()->get("rendering/driver/driver_name");
  1216. bool etc_supported = ProjectSettings::get_singleton()->get("rendering/textures/vram_compression/import_etc");
  1217. bool etc2_supported = ProjectSettings::get_singleton()->get("rendering/textures/vram_compression/import_etc2");
  1218. if (driver == "GLES2" && !etc_supported) {
  1219. return TTR("Target platform requires 'ETC' texture compression for GLES2. Enable 'Import Etc' in Project Settings.");
  1220. } else if (driver == "Vulkan" && !etc2_supported) {
  1221. // FIXME: Review if this is true for Vulkan.
  1222. return TTR("Target platform requires 'ETC2' texture compression for Vulkan. Enable 'Import Etc 2' in Project Settings.");
  1223. }
  1224. return String();
  1225. }
  1226. String EditorExportPlatform::test_etc2_or_pvrtc() const {
  1227. String driver = ProjectSettings::get_singleton()->get("rendering/driver/driver_name");
  1228. bool etc2_supported = ProjectSettings::get_singleton()->get("rendering/textures/vram_compression/import_etc2");
  1229. bool pvrtc_supported = ProjectSettings::get_singleton()->get("rendering/textures/vram_compression/import_pvrtc");
  1230. if (driver == "GLES2" && !pvrtc_supported) {
  1231. return TTR("Target platform requires 'PVRTC' texture compression for GLES2. Enable 'Import Pvrtc' in Project Settings.");
  1232. } else if (driver == "Vulkan" && !etc2_supported && !pvrtc_supported) {
  1233. // FIXME: Review if this is true for Vulkan.
  1234. return TTR("Target platform requires 'ETC2' or 'PVRTC' texture compression for Vulkan. Enable 'Import Etc 2' or 'Import Pvrtc' in Project Settings.");
  1235. }
  1236. return String();
  1237. }
  1238. int EditorExport::get_export_preset_count() const {
  1239. return export_presets.size();
  1240. }
  1241. Ref<EditorExportPreset> EditorExport::get_export_preset(int p_idx) {
  1242. ERR_FAIL_INDEX_V(p_idx, export_presets.size(), Ref<EditorExportPreset>());
  1243. return export_presets[p_idx];
  1244. }
  1245. void EditorExport::remove_export_preset(int p_idx) {
  1246. export_presets.remove(p_idx);
  1247. save_presets();
  1248. }
  1249. void EditorExport::add_export_plugin(const Ref<EditorExportPlugin> &p_plugin) {
  1250. if (export_plugins.find(p_plugin) == -1) {
  1251. export_plugins.push_back(p_plugin);
  1252. }
  1253. }
  1254. void EditorExport::remove_export_plugin(const Ref<EditorExportPlugin> &p_plugin) {
  1255. export_plugins.erase(p_plugin);
  1256. }
  1257. Vector<Ref<EditorExportPlugin>> EditorExport::get_export_plugins() {
  1258. return export_plugins;
  1259. }
  1260. void EditorExport::_notification(int p_what) {
  1261. switch (p_what) {
  1262. case NOTIFICATION_ENTER_TREE: {
  1263. load_config();
  1264. } break;
  1265. case NOTIFICATION_PROCESS: {
  1266. update_export_presets();
  1267. } break;
  1268. }
  1269. }
  1270. void EditorExport::load_config() {
  1271. Ref<ConfigFile> config;
  1272. config.instance();
  1273. Error err = config->load("res://export_presets.cfg");
  1274. if (err != OK) {
  1275. return;
  1276. }
  1277. block_save = true;
  1278. int index = 0;
  1279. while (true) {
  1280. String section = "preset." + itos(index);
  1281. if (!config->has_section(section)) {
  1282. break;
  1283. }
  1284. String platform = config->get_value(section, "platform");
  1285. Ref<EditorExportPreset> preset;
  1286. for (int i = 0; i < export_platforms.size(); i++) {
  1287. if (export_platforms[i]->get_name() == platform) {
  1288. preset = export_platforms.write[i]->create_preset();
  1289. break;
  1290. }
  1291. }
  1292. if (!preset.is_valid()) {
  1293. index++;
  1294. ERR_CONTINUE(!preset.is_valid());
  1295. }
  1296. preset->set_name(config->get_value(section, "name"));
  1297. preset->set_runnable(config->get_value(section, "runnable"));
  1298. if (config->has_section_key(section, "custom_features")) {
  1299. preset->set_custom_features(config->get_value(section, "custom_features"));
  1300. }
  1301. String export_filter = config->get_value(section, "export_filter");
  1302. bool get_files = false;
  1303. if (export_filter == "all_resources") {
  1304. preset->set_export_filter(EditorExportPreset::EXPORT_ALL_RESOURCES);
  1305. } else if (export_filter == "scenes") {
  1306. preset->set_export_filter(EditorExportPreset::EXPORT_SELECTED_SCENES);
  1307. get_files = true;
  1308. } else if (export_filter == "resources") {
  1309. preset->set_export_filter(EditorExportPreset::EXPORT_SELECTED_RESOURCES);
  1310. get_files = true;
  1311. } else if (export_filter == "exclude") {
  1312. preset->set_export_filter(EditorExportPreset::EXCLUDE_SELECTED_RESOURCES);
  1313. get_files = true;
  1314. }
  1315. if (get_files) {
  1316. Vector<String> files = config->get_value(section, "export_files");
  1317. for (int i = 0; i < files.size(); i++) {
  1318. if (!FileAccess::exists(files[i])) {
  1319. preset->remove_export_file(files[i]);
  1320. } else {
  1321. preset->add_export_file(files[i]);
  1322. }
  1323. }
  1324. }
  1325. preset->set_include_filter(config->get_value(section, "include_filter"));
  1326. preset->set_exclude_filter(config->get_value(section, "exclude_filter"));
  1327. preset->set_export_path(config->get_value(section, "export_path", ""));
  1328. if (config->has_section_key(section, "encrypt_pck")) {
  1329. preset->set_enc_pck(config->get_value(section, "encrypt_pck"));
  1330. }
  1331. if (config->has_section_key(section, "encrypt_directory")) {
  1332. preset->set_enc_directory(config->get_value(section, "encrypt_directory"));
  1333. }
  1334. if (config->has_section_key(section, "encryption_include_filters")) {
  1335. preset->set_enc_in_filter(config->get_value(section, "encryption_include_filters"));
  1336. }
  1337. if (config->has_section_key(section, "encryption_exclude_filters")) {
  1338. preset->set_enc_ex_filter(config->get_value(section, "encryption_exclude_filters"));
  1339. }
  1340. if (config->has_section_key(section, "script_export_mode")) {
  1341. preset->set_script_export_mode(config->get_value(section, "script_export_mode"));
  1342. }
  1343. if (config->has_section_key(section, "script_encryption_key")) {
  1344. preset->set_script_encryption_key(config->get_value(section, "script_encryption_key"));
  1345. }
  1346. String option_section = "preset." + itos(index) + ".options";
  1347. List<String> options;
  1348. config->get_section_keys(option_section, &options);
  1349. for (List<String>::Element *E = options.front(); E; E = E->next()) {
  1350. Variant value = config->get_value(option_section, E->get());
  1351. preset->set(E->get(), value);
  1352. }
  1353. add_export_preset(preset);
  1354. index++;
  1355. }
  1356. block_save = false;
  1357. }
  1358. void EditorExport::update_export_presets() {
  1359. Map<StringName, List<EditorExportPlatform::ExportOption>> platform_options;
  1360. for (int i = 0; i < export_platforms.size(); i++) {
  1361. Ref<EditorExportPlatform> platform = export_platforms[i];
  1362. if (platform->should_update_export_options()) {
  1363. List<EditorExportPlatform::ExportOption> options;
  1364. platform->get_export_options(&options);
  1365. platform_options[platform->get_name()] = options;
  1366. }
  1367. }
  1368. bool export_presets_updated = false;
  1369. for (int i = 0; i < export_presets.size(); i++) {
  1370. Ref<EditorExportPreset> preset = export_presets[i];
  1371. if (platform_options.has(preset->get_platform()->get_name())) {
  1372. export_presets_updated = true;
  1373. List<EditorExportPlatform::ExportOption> options = platform_options[preset->get_platform()->get_name()];
  1374. // Copy the previous preset values
  1375. Map<StringName, Variant> previous_values = preset->values;
  1376. // Clear the preset properties and values prior to reloading
  1377. preset->properties.clear();
  1378. preset->values.clear();
  1379. for (List<EditorExportPlatform::ExportOption>::Element *E = options.front(); E; E = E->next()) {
  1380. preset->properties.push_back(E->get().option);
  1381. StringName option_name = E->get().option.name;
  1382. preset->values[option_name] = previous_values.has(option_name) ? previous_values[option_name] : E->get().default_value;
  1383. }
  1384. }
  1385. }
  1386. if (export_presets_updated) {
  1387. emit_signal(_export_presets_updated);
  1388. }
  1389. }
  1390. bool EditorExport::poll_export_platforms() {
  1391. bool changed = false;
  1392. for (int i = 0; i < export_platforms.size(); i++) {
  1393. if (export_platforms.write[i]->poll_export()) {
  1394. changed = true;
  1395. }
  1396. }
  1397. return changed;
  1398. }
  1399. EditorExport::EditorExport() {
  1400. save_timer = memnew(Timer);
  1401. add_child(save_timer);
  1402. save_timer->set_wait_time(0.8);
  1403. save_timer->set_one_shot(true);
  1404. save_timer->connect("timeout", callable_mp(this, &EditorExport::_save));
  1405. block_save = false;
  1406. _export_presets_updated = "export_presets_updated";
  1407. singleton = this;
  1408. set_process(true);
  1409. }
  1410. EditorExport::~EditorExport() {
  1411. }
  1412. //////////
  1413. void EditorExportPlatformPC::get_preset_features(const Ref<EditorExportPreset> &p_preset, List<String> *r_features) {
  1414. if (p_preset->get("texture_format/s3tc")) {
  1415. r_features->push_back("s3tc");
  1416. }
  1417. if (p_preset->get("texture_format/etc")) {
  1418. r_features->push_back("etc");
  1419. }
  1420. if (p_preset->get("texture_format/etc2")) {
  1421. r_features->push_back("etc2");
  1422. }
  1423. if (p_preset->get("binary_format/64_bits")) {
  1424. r_features->push_back("64");
  1425. } else {
  1426. r_features->push_back("32");
  1427. }
  1428. }
  1429. void EditorExportPlatformPC::get_export_options(List<ExportOption> *r_options) {
  1430. r_options->push_back(ExportOption(PropertyInfo(Variant::STRING, "custom_template/debug", PROPERTY_HINT_GLOBAL_FILE), ""));
  1431. r_options->push_back(ExportOption(PropertyInfo(Variant::STRING, "custom_template/release", PROPERTY_HINT_GLOBAL_FILE), ""));
  1432. r_options->push_back(ExportOption(PropertyInfo(Variant::BOOL, "binary_format/64_bits"), true));
  1433. r_options->push_back(ExportOption(PropertyInfo(Variant::BOOL, "binary_format/embed_pck"), false));
  1434. r_options->push_back(ExportOption(PropertyInfo(Variant::BOOL, "texture_format/bptc"), false));
  1435. r_options->push_back(ExportOption(PropertyInfo(Variant::BOOL, "texture_format/s3tc"), true));
  1436. r_options->push_back(ExportOption(PropertyInfo(Variant::BOOL, "texture_format/etc"), false));
  1437. r_options->push_back(ExportOption(PropertyInfo(Variant::BOOL, "texture_format/etc2"), false));
  1438. r_options->push_back(ExportOption(PropertyInfo(Variant::BOOL, "texture_format/no_bptc_fallbacks"), true));
  1439. }
  1440. String EditorExportPlatformPC::get_name() const {
  1441. return name;
  1442. }
  1443. String EditorExportPlatformPC::get_os_name() const {
  1444. return os_name;
  1445. }
  1446. Ref<Texture2D> EditorExportPlatformPC::get_logo() const {
  1447. return logo;
  1448. }
  1449. bool EditorExportPlatformPC::can_export(const Ref<EditorExportPreset> &p_preset, String &r_error, bool &r_missing_templates) const {
  1450. String err;
  1451. bool valid = false;
  1452. // Look for export templates (first official, and if defined custom templates).
  1453. bool use64 = p_preset->get("binary_format/64_bits");
  1454. bool dvalid = exists_export_template(use64 ? debug_file_64 : debug_file_32, &err);
  1455. bool rvalid = exists_export_template(use64 ? release_file_64 : release_file_32, &err);
  1456. if (p_preset->get("custom_template/debug") != "") {
  1457. dvalid = FileAccess::exists(p_preset->get("custom_template/debug"));
  1458. if (!dvalid) {
  1459. err += TTR("Custom debug template not found.") + "\n";
  1460. }
  1461. }
  1462. if (p_preset->get("custom_template/release") != "") {
  1463. rvalid = FileAccess::exists(p_preset->get("custom_template/release"));
  1464. if (!rvalid) {
  1465. err += TTR("Custom release template not found.") + "\n";
  1466. }
  1467. }
  1468. valid = dvalid || rvalid;
  1469. r_missing_templates = !valid;
  1470. if (!err.is_empty()) {
  1471. r_error = err;
  1472. }
  1473. return valid;
  1474. }
  1475. List<String> EditorExportPlatformPC::get_binary_extensions(const Ref<EditorExportPreset> &p_preset) const {
  1476. List<String> list;
  1477. for (Map<String, String>::Element *E = extensions.front(); E; E = E->next()) {
  1478. if (p_preset->get(E->key())) {
  1479. list.push_back(extensions[E->key()]);
  1480. return list;
  1481. }
  1482. }
  1483. if (extensions.has("default")) {
  1484. list.push_back(extensions["default"]);
  1485. return list;
  1486. }
  1487. return list;
  1488. }
  1489. Error EditorExportPlatformPC::export_project(const Ref<EditorExportPreset> &p_preset, bool p_debug, const String &p_path, int p_flags) {
  1490. ExportNotifier notifier(*this, p_preset, p_debug, p_path, p_flags);
  1491. if (!DirAccess::exists(p_path.get_base_dir())) {
  1492. return ERR_FILE_BAD_PATH;
  1493. }
  1494. String custom_debug = p_preset->get("custom_template/debug");
  1495. String custom_release = p_preset->get("custom_template/release");
  1496. String template_path = p_debug ? custom_debug : custom_release;
  1497. template_path = template_path.strip_edges();
  1498. if (template_path == String()) {
  1499. if (p_preset->get("binary_format/64_bits")) {
  1500. if (p_debug) {
  1501. template_path = find_export_template(debug_file_64);
  1502. } else {
  1503. template_path = find_export_template(release_file_64);
  1504. }
  1505. } else {
  1506. if (p_debug) {
  1507. template_path = find_export_template(debug_file_32);
  1508. } else {
  1509. template_path = find_export_template(release_file_32);
  1510. }
  1511. }
  1512. }
  1513. if (template_path != String() && !FileAccess::exists(template_path)) {
  1514. EditorNode::get_singleton()->show_warning(TTR("Template file not found:") + "\n" + template_path);
  1515. return ERR_FILE_NOT_FOUND;
  1516. }
  1517. DirAccess *da = DirAccess::create(DirAccess::ACCESS_FILESYSTEM);
  1518. Error err = da->copy(template_path, p_path, get_chmod_flags());
  1519. memdelete(da);
  1520. if (err == OK) {
  1521. String pck_path;
  1522. if (p_preset->get("binary_format/embed_pck")) {
  1523. pck_path = p_path;
  1524. } else {
  1525. pck_path = p_path.get_basename() + ".pck";
  1526. }
  1527. Vector<SharedObject> so_files;
  1528. int64_t embedded_pos;
  1529. int64_t embedded_size;
  1530. err = save_pack(p_preset, pck_path, &so_files, p_preset->get("binary_format/embed_pck"), &embedded_pos, &embedded_size);
  1531. if (err == OK && p_preset->get("binary_format/embed_pck")) {
  1532. if (embedded_size >= 0x100000000 && !p_preset->get("binary_format/64_bits")) {
  1533. EditorNode::get_singleton()->show_warning(TTR("On 32-bit exports the embedded PCK cannot be bigger than 4 GiB."));
  1534. return ERR_INVALID_PARAMETER;
  1535. }
  1536. FixUpEmbeddedPckFunc fixup_func = get_fixup_embedded_pck_func();
  1537. if (fixup_func) {
  1538. err = fixup_func(p_path, embedded_pos, embedded_size);
  1539. }
  1540. }
  1541. if (err == OK && !so_files.is_empty()) {
  1542. //if shared object files, copy them
  1543. da = DirAccess::create(DirAccess::ACCESS_FILESYSTEM);
  1544. for (int i = 0; i < so_files.size() && err == OK; i++) {
  1545. err = da->copy(so_files[i].path, p_path.get_base_dir().plus_file(so_files[i].path.get_file()));
  1546. if (err == OK) {
  1547. err = sign_shared_object(p_preset, p_debug, p_path.get_base_dir().plus_file(so_files[i].path.get_file()));
  1548. }
  1549. }
  1550. memdelete(da);
  1551. }
  1552. }
  1553. return err;
  1554. }
  1555. Error EditorExportPlatformPC::sign_shared_object(const Ref<EditorExportPreset> &p_preset, bool p_debug, const String &p_path) {
  1556. return OK;
  1557. }
  1558. void EditorExportPlatformPC::set_extension(const String &p_extension, const String &p_feature_key) {
  1559. extensions[p_feature_key] = p_extension;
  1560. }
  1561. void EditorExportPlatformPC::set_name(const String &p_name) {
  1562. name = p_name;
  1563. }
  1564. void EditorExportPlatformPC::set_os_name(const String &p_name) {
  1565. os_name = p_name;
  1566. }
  1567. void EditorExportPlatformPC::set_logo(const Ref<Texture2D> &p_logo) {
  1568. logo = p_logo;
  1569. }
  1570. void EditorExportPlatformPC::set_release_64(const String &p_file) {
  1571. release_file_64 = p_file;
  1572. }
  1573. void EditorExportPlatformPC::set_release_32(const String &p_file) {
  1574. release_file_32 = p_file;
  1575. }
  1576. void EditorExportPlatformPC::set_debug_64(const String &p_file) {
  1577. debug_file_64 = p_file;
  1578. }
  1579. void EditorExportPlatformPC::set_debug_32(const String &p_file) {
  1580. debug_file_32 = p_file;
  1581. }
  1582. void EditorExportPlatformPC::get_platform_features(List<String> *r_features) {
  1583. r_features->push_back("pc"); //all pcs support "pc"
  1584. r_features->push_back("s3tc"); //all pcs support "s3tc" compression
  1585. r_features->push_back(get_os_name()); //OS name is a feature
  1586. }
  1587. void EditorExportPlatformPC::resolve_platform_feature_priorities(const Ref<EditorExportPreset> &p_preset, Set<String> &p_features) {
  1588. if (p_features.has("bptc")) {
  1589. if (p_preset->has("texture_format/no_bptc_fallbacks")) {
  1590. p_features.erase("s3tc");
  1591. }
  1592. }
  1593. }
  1594. int EditorExportPlatformPC::get_chmod_flags() const {
  1595. return chmod_flags;
  1596. }
  1597. void EditorExportPlatformPC::set_chmod_flags(int p_flags) {
  1598. chmod_flags = p_flags;
  1599. }
  1600. EditorExportPlatformPC::FixUpEmbeddedPckFunc EditorExportPlatformPC::get_fixup_embedded_pck_func() const {
  1601. return fixup_embedded_pck_func;
  1602. }
  1603. void EditorExportPlatformPC::set_fixup_embedded_pck_func(FixUpEmbeddedPckFunc p_fixup_embedded_pck_func) {
  1604. fixup_embedded_pck_func = p_fixup_embedded_pck_func;
  1605. }
  1606. EditorExportPlatformPC::EditorExportPlatformPC() {
  1607. chmod_flags = -1;
  1608. fixup_embedded_pck_func = nullptr;
  1609. }
  1610. ///////////////////////
  1611. void EditorExportTextSceneToBinaryPlugin::_export_file(const String &p_path, const String &p_type, const Set<String> &p_features) {
  1612. String extension = p_path.get_extension().to_lower();
  1613. if (extension != "tres" && extension != "tscn") {
  1614. return;
  1615. }
  1616. bool convert = GLOBAL_GET("editor/export/convert_text_resources_to_binary");
  1617. if (!convert) {
  1618. return;
  1619. }
  1620. String tmp_path = EditorPaths::get_singleton()->get_cache_dir().plus_file("tmpfile.res");
  1621. Error err = ResourceFormatLoaderText::convert_file_to_binary(p_path, tmp_path);
  1622. if (err != OK) {
  1623. DirAccess::remove_file_or_error(tmp_path);
  1624. ERR_FAIL();
  1625. }
  1626. Vector<uint8_t> data = FileAccess::get_file_as_array(tmp_path);
  1627. if (data.size() == 0) {
  1628. DirAccess::remove_file_or_error(tmp_path);
  1629. ERR_FAIL();
  1630. }
  1631. DirAccess::remove_file_or_error(tmp_path);
  1632. add_file(p_path + ".converted.res", data, true);
  1633. }
  1634. EditorExportTextSceneToBinaryPlugin::EditorExportTextSceneToBinaryPlugin() {
  1635. GLOBAL_DEF("editor/export/convert_text_resources_to_binary", false);
  1636. }