editor_export.cpp 46 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620
  1. /*************************************************************************/
  2. /* editor_export.cpp */
  3. /*************************************************************************/
  4. /* This file is part of: */
  5. /* GODOT ENGINE */
  6. /* https://godotengine.org */
  7. /*************************************************************************/
  8. /* Copyright (c) 2007-2019 Juan Linietsky, Ariel Manzur. */
  9. /* Copyright (c) 2014-2019 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/io/config_file.h"
  32. #include "core/io/resource_loader.h"
  33. #include "core/io/resource_saver.h"
  34. #include "core/io/zip_io.h"
  35. #include "core/os/file_access.h"
  36. #include "core/project_settings.h"
  37. #include "core/script_language.h"
  38. #include "core/version.h"
  39. #include "editor/editor_file_system.h"
  40. #include "editor/plugins/script_editor_plugin.h"
  41. #include "editor_node.h"
  42. #include "editor_settings.h"
  43. #include "scene/resources/resource_format_text.h"
  44. #include "thirdparty/misc/md5.h"
  45. static int _get_pad(int p_alignment, int p_n) {
  46. int rest = p_n % p_alignment;
  47. int pad = 0;
  48. if (rest > 0) {
  49. pad = p_alignment - rest;
  50. };
  51. return pad;
  52. }
  53. #define PCK_PADDING 16
  54. bool EditorExportPreset::_set(const StringName &p_name, const Variant &p_value) {
  55. if (values.has(p_name)) {
  56. values[p_name] = p_value;
  57. EditorExport::singleton->save_presets();
  58. return true;
  59. }
  60. return false;
  61. }
  62. bool EditorExportPreset::_get(const StringName &p_name, Variant &r_ret) const {
  63. if (values.has(p_name)) {
  64. r_ret = values[p_name];
  65. return true;
  66. }
  67. return false;
  68. }
  69. void EditorExportPreset::_get_property_list(List<PropertyInfo> *p_list) const {
  70. for (const List<PropertyInfo>::Element *E = properties.front(); E; E = E->next()) {
  71. if (platform->get_option_visibility(E->get().name, values)) {
  72. p_list->push_back(E->get());
  73. }
  74. }
  75. }
  76. Ref<EditorExportPlatform> EditorExportPreset::get_platform() const {
  77. return platform;
  78. }
  79. Vector<String> EditorExportPreset::get_files_to_export() const {
  80. Vector<String> files;
  81. for (Set<String>::Element *E = selected_files.front(); E; E = E->next()) {
  82. files.push_back(E->get());
  83. }
  84. return files;
  85. }
  86. void EditorExportPreset::set_name(const String &p_name) {
  87. name = p_name;
  88. EditorExport::singleton->save_presets();
  89. }
  90. String EditorExportPreset::get_name() const {
  91. return name;
  92. }
  93. void EditorExportPreset::set_runnable(bool p_enable) {
  94. runnable = p_enable;
  95. EditorExport::singleton->save_presets();
  96. }
  97. bool EditorExportPreset::is_runnable() const {
  98. return runnable;
  99. }
  100. void EditorExportPreset::set_export_filter(ExportFilter p_filter) {
  101. export_filter = p_filter;
  102. EditorExport::singleton->save_presets();
  103. }
  104. EditorExportPreset::ExportFilter EditorExportPreset::get_export_filter() const {
  105. return export_filter;
  106. }
  107. void EditorExportPreset::set_include_filter(const String &p_include) {
  108. include_filter = p_include;
  109. EditorExport::singleton->save_presets();
  110. }
  111. String EditorExportPreset::get_include_filter() const {
  112. return include_filter;
  113. }
  114. void EditorExportPreset::set_export_path(const String &p_path) {
  115. export_path = p_path;
  116. EditorExport::singleton->save_presets();
  117. }
  118. String EditorExportPreset::get_export_path() const {
  119. return export_path;
  120. }
  121. void EditorExportPreset::set_exclude_filter(const String &p_exclude) {
  122. exclude_filter = p_exclude;
  123. EditorExport::singleton->save_presets();
  124. }
  125. String EditorExportPreset::get_exclude_filter() const {
  126. return exclude_filter;
  127. }
  128. void EditorExportPreset::add_export_file(const String &p_path) {
  129. selected_files.insert(p_path);
  130. EditorExport::singleton->save_presets();
  131. }
  132. void EditorExportPreset::remove_export_file(const String &p_path) {
  133. selected_files.erase(p_path);
  134. EditorExport::singleton->save_presets();
  135. }
  136. bool EditorExportPreset::has_export_file(const String &p_path) {
  137. return selected_files.has(p_path);
  138. }
  139. void EditorExportPreset::add_patch(const String &p_path, int p_at_pos) {
  140. if (p_at_pos < 0)
  141. patches.push_back(p_path);
  142. else
  143. patches.insert(p_at_pos, p_path);
  144. EditorExport::singleton->save_presets();
  145. }
  146. void EditorExportPreset::remove_patch(int p_idx) {
  147. patches.remove(p_idx);
  148. EditorExport::singleton->save_presets();
  149. }
  150. void EditorExportPreset::set_patch(int p_index, const String &p_path) {
  151. ERR_FAIL_INDEX(p_index, patches.size());
  152. patches.write[p_index] = p_path;
  153. EditorExport::singleton->save_presets();
  154. }
  155. String EditorExportPreset::get_patch(int p_index) {
  156. ERR_FAIL_INDEX_V(p_index, patches.size(), String());
  157. return patches[p_index];
  158. }
  159. Vector<String> EditorExportPreset::get_patches() const {
  160. return patches;
  161. }
  162. void EditorExportPreset::set_custom_features(const String &p_custom_features) {
  163. custom_features = p_custom_features;
  164. EditorExport::singleton->save_presets();
  165. }
  166. String EditorExportPreset::get_custom_features() const {
  167. return custom_features;
  168. }
  169. void EditorExportPreset::set_script_export_mode(int p_mode) {
  170. script_mode = p_mode;
  171. EditorExport::singleton->save_presets();
  172. }
  173. int EditorExportPreset::get_script_export_mode() const {
  174. return script_mode;
  175. }
  176. void EditorExportPreset::set_script_encryption_key(const String &p_key) {
  177. script_key = p_key;
  178. EditorExport::singleton->save_presets();
  179. }
  180. String EditorExportPreset::get_script_encryption_key() const {
  181. return script_key;
  182. }
  183. EditorExportPreset::EditorExportPreset() :
  184. export_filter(EXPORT_ALL_RESOURCES),
  185. export_path(""),
  186. runnable(false),
  187. script_mode(MODE_SCRIPT_COMPILED) {
  188. }
  189. ///////////////////////////////////
  190. void EditorExportPlatform::gen_debug_flags(Vector<String> &r_flags, int p_flags) {
  191. String host = EditorSettings::get_singleton()->get("network/debug/remote_host");
  192. int remote_port = (int)EditorSettings::get_singleton()->get("network/debug/remote_port");
  193. if (p_flags & DEBUG_FLAG_REMOTE_DEBUG_LOCALHOST)
  194. host = "localhost";
  195. if (p_flags & DEBUG_FLAG_DUMB_CLIENT) {
  196. int port = EditorSettings::get_singleton()->get("filesystem/file_server/port");
  197. String passwd = EditorSettings::get_singleton()->get("filesystem/file_server/password");
  198. r_flags.push_back("--remote-fs");
  199. r_flags.push_back(host + ":" + itos(port));
  200. if (passwd != "") {
  201. r_flags.push_back("--remote-fs-password");
  202. r_flags.push_back(passwd);
  203. }
  204. }
  205. if (p_flags & DEBUG_FLAG_REMOTE_DEBUG) {
  206. r_flags.push_back("--remote-debug");
  207. r_flags.push_back(host + ":" + String::num(remote_port));
  208. List<String> breakpoints;
  209. ScriptEditor::get_singleton()->get_breakpoints(&breakpoints);
  210. if (breakpoints.size()) {
  211. r_flags.push_back("--breakpoints");
  212. String bpoints;
  213. for (const List<String>::Element *E = breakpoints.front(); E; E = E->next()) {
  214. bpoints += E->get().replace(" ", "%20");
  215. if (E->next())
  216. bpoints += ",";
  217. }
  218. r_flags.push_back(bpoints);
  219. }
  220. }
  221. if (p_flags & DEBUG_FLAG_VIEW_COLLISONS) {
  222. r_flags.push_back("--debug-collisions");
  223. }
  224. if (p_flags & DEBUG_FLAG_VIEW_NAVIGATION) {
  225. r_flags.push_back("--debug-navigation");
  226. }
  227. }
  228. Error EditorExportPlatform::_save_pack_file(void *p_userdata, const String &p_path, const Vector<uint8_t> &p_data, int p_file, int p_total) {
  229. PackData *pd = (PackData *)p_userdata;
  230. SavedData sd;
  231. sd.path_utf8 = p_path.utf8();
  232. sd.ofs = pd->f->get_position();
  233. sd.size = p_data.size();
  234. pd->f->store_buffer(p_data.ptr(), p_data.size());
  235. int pad = _get_pad(PCK_PADDING, sd.size);
  236. for (int i = 0; i < pad; i++) {
  237. pd->f->store_8(0);
  238. }
  239. {
  240. MD5_CTX ctx;
  241. MD5Init(&ctx);
  242. MD5Update(&ctx, (unsigned char *)p_data.ptr(), p_data.size());
  243. MD5Final(&ctx);
  244. sd.md5.resize(16);
  245. for (int i = 0; i < 16; i++) {
  246. sd.md5.write[i] = ctx.digest[i];
  247. }
  248. }
  249. pd->file_ofs.push_back(sd);
  250. pd->ep->step(TTR("Storing File:") + " " + p_path, 2 + p_file * 100 / p_total, false);
  251. return OK;
  252. }
  253. Error EditorExportPlatform::_save_zip_file(void *p_userdata, const String &p_path, const Vector<uint8_t> &p_data, int p_file, int p_total) {
  254. String path = p_path.replace_first("res://", "");
  255. ZipData *zd = (ZipData *)p_userdata;
  256. zipFile zip = (zipFile)zd->zip;
  257. zipOpenNewFileInZip(zip,
  258. path.utf8().get_data(),
  259. NULL,
  260. NULL,
  261. 0,
  262. NULL,
  263. 0,
  264. NULL,
  265. Z_DEFLATED,
  266. Z_DEFAULT_COMPRESSION);
  267. zipWriteInFileInZip(zip, p_data.ptr(), p_data.size());
  268. zipCloseFileInZip(zip);
  269. zd->ep->step(TTR("Storing File:") + " " + p_path, 2 + p_file * 100 / p_total, false);
  270. return OK;
  271. }
  272. String EditorExportPlatform::find_export_template(String template_file_name, String *err) const {
  273. String current_version = VERSION_FULL_CONFIG;
  274. String template_path = EditorSettings::get_singleton()->get_templates_dir().plus_file(current_version).plus_file(template_file_name);
  275. if (FileAccess::exists(template_path)) {
  276. return template_path;
  277. }
  278. // Not found
  279. if (err) {
  280. *err += TTR("No export template found at the expected path:") + "\n" + template_path + "\n";
  281. }
  282. return String();
  283. }
  284. bool EditorExportPlatform::exists_export_template(String template_file_name, String *err) const {
  285. return find_export_template(template_file_name, err) != "";
  286. }
  287. Ref<EditorExportPreset> EditorExportPlatform::create_preset() {
  288. Ref<EditorExportPreset> preset;
  289. preset.instance();
  290. preset->platform = Ref<EditorExportPlatform>(this);
  291. List<ExportOption> options;
  292. get_export_options(&options);
  293. for (List<ExportOption>::Element *E = options.front(); E; E = E->next()) {
  294. preset->properties.push_back(E->get().option);
  295. preset->values[E->get().option.name] = E->get().default_value;
  296. }
  297. return preset;
  298. }
  299. void EditorExportPlatform::_export_find_resources(EditorFileSystemDirectory *p_dir, Set<String> &p_paths) {
  300. for (int i = 0; i < p_dir->get_subdir_count(); i++) {
  301. _export_find_resources(p_dir->get_subdir(i), p_paths);
  302. }
  303. for (int i = 0; i < p_dir->get_file_count(); i++) {
  304. p_paths.insert(p_dir->get_file_path(i));
  305. }
  306. }
  307. void EditorExportPlatform::_export_find_dependencies(const String &p_path, Set<String> &p_paths) {
  308. if (p_paths.has(p_path))
  309. return;
  310. p_paths.insert(p_path);
  311. EditorFileSystemDirectory *dir;
  312. int file_idx;
  313. dir = EditorFileSystem::get_singleton()->find_file(p_path, &file_idx);
  314. if (!dir)
  315. return;
  316. Vector<String> deps = dir->get_file_deps(file_idx);
  317. for (int i = 0; i < deps.size(); i++) {
  318. _export_find_dependencies(deps[i], p_paths);
  319. }
  320. }
  321. void EditorExportPlatform::_edit_files_with_filter(DirAccess *da, const Vector<String> &p_filters, Set<String> &r_list, bool exclude) {
  322. da->list_dir_begin();
  323. String cur_dir = da->get_current_dir().replace("\\", "/");
  324. if (!cur_dir.ends_with("/"))
  325. cur_dir += "/";
  326. String cur_dir_no_prefix = cur_dir.replace("res://", "");
  327. Vector<String> dirs;
  328. String f;
  329. while ((f = da->get_next()) != "") {
  330. if (da->current_is_dir())
  331. dirs.push_back(f);
  332. else {
  333. String fullpath = cur_dir + f;
  334. // Test also against path without res:// so that filters like `file.txt` can work.
  335. String fullpath_no_prefix = cur_dir_no_prefix + f;
  336. for (int i = 0; i < p_filters.size(); ++i) {
  337. if (fullpath.matchn(p_filters[i]) || fullpath_no_prefix.matchn(p_filters[i])) {
  338. if (!exclude) {
  339. r_list.insert(fullpath);
  340. } else {
  341. r_list.erase(fullpath);
  342. }
  343. }
  344. }
  345. }
  346. }
  347. da->list_dir_end();
  348. for (int i = 0; i < dirs.size(); ++i) {
  349. String dir = dirs[i];
  350. if (dir.begins_with("."))
  351. continue;
  352. da->change_dir(dir);
  353. _edit_files_with_filter(da, p_filters, r_list, exclude);
  354. da->change_dir("..");
  355. }
  356. }
  357. void EditorExportPlatform::_edit_filter_list(Set<String> &r_list, const String &p_filter, bool exclude) {
  358. if (p_filter == "")
  359. return;
  360. Vector<String> split = p_filter.split(",");
  361. Vector<String> filters;
  362. for (int i = 0; i < split.size(); i++) {
  363. String f = split[i].strip_edges();
  364. if (f.empty())
  365. continue;
  366. filters.push_back(f);
  367. }
  368. DirAccess *da = DirAccess::open("res://");
  369. ERR_FAIL_NULL(da);
  370. _edit_files_with_filter(da, filters, r_list, exclude);
  371. memdelete(da);
  372. }
  373. void EditorExportPlugin::set_export_preset(const Ref<EditorExportPreset> &p_preset) {
  374. if (p_preset.is_valid()) {
  375. export_preset = p_preset;
  376. }
  377. }
  378. Ref<EditorExportPreset> EditorExportPlugin::get_export_preset() const {
  379. return export_preset;
  380. }
  381. void EditorExportPlugin::add_file(const String &p_path, const Vector<uint8_t> &p_file, bool p_remap) {
  382. ExtraFile ef;
  383. ef.data = p_file;
  384. ef.path = p_path;
  385. ef.remap = p_remap;
  386. extra_files.push_back(ef);
  387. }
  388. void EditorExportPlugin::add_shared_object(const String &p_path, const Vector<String> &tags) {
  389. shared_objects.push_back(SharedObject(p_path, tags));
  390. }
  391. void EditorExportPlugin::add_ios_framework(const String &p_path) {
  392. ios_frameworks.push_back(p_path);
  393. }
  394. Vector<String> EditorExportPlugin::get_ios_frameworks() const {
  395. return ios_frameworks;
  396. }
  397. void EditorExportPlugin::add_ios_plist_content(const String &p_plist_content) {
  398. ios_plist_content += p_plist_content + "\n";
  399. }
  400. String EditorExportPlugin::get_ios_plist_content() const {
  401. return ios_plist_content;
  402. }
  403. void EditorExportPlugin::add_ios_linker_flags(const String &p_flags) {
  404. if (ios_linker_flags.length() > 0) {
  405. ios_linker_flags += ' ';
  406. }
  407. ios_linker_flags += p_flags;
  408. }
  409. String EditorExportPlugin::get_ios_linker_flags() const {
  410. return ios_linker_flags;
  411. }
  412. void EditorExportPlugin::add_ios_bundle_file(const String &p_path) {
  413. ios_bundle_files.push_back(p_path);
  414. }
  415. Vector<String> EditorExportPlugin::get_ios_bundle_files() const {
  416. return ios_bundle_files;
  417. }
  418. void EditorExportPlugin::add_ios_cpp_code(const String &p_code) {
  419. ios_cpp_code += p_code;
  420. }
  421. String EditorExportPlugin::get_ios_cpp_code() const {
  422. return ios_cpp_code;
  423. }
  424. void EditorExportPlugin::_export_file_script(const String &p_path, const String &p_type, const PoolVector<String> &p_features) {
  425. if (get_script_instance()) {
  426. get_script_instance()->call("_export_file", p_path, p_type, p_features);
  427. }
  428. }
  429. void EditorExportPlugin::_export_begin_script(const PoolVector<String> &p_features, bool p_debug, const String &p_path, int p_flags) {
  430. if (get_script_instance()) {
  431. get_script_instance()->call("_export_begin", p_features, p_debug, p_path, p_flags);
  432. }
  433. }
  434. void EditorExportPlugin::_export_end_script() {
  435. if (get_script_instance()) {
  436. get_script_instance()->call("_export_end");
  437. }
  438. }
  439. void EditorExportPlugin::_export_file(const String &p_path, const String &p_type, const Set<String> &p_features) {
  440. }
  441. void EditorExportPlugin::_export_begin(const Set<String> &p_features, bool p_debug, const String &p_path, int p_flags) {
  442. }
  443. void EditorExportPlugin::skip() {
  444. skipped = true;
  445. }
  446. void EditorExportPlugin::_bind_methods() {
  447. ClassDB::bind_method(D_METHOD("add_shared_object", "path", "tags"), &EditorExportPlugin::add_shared_object);
  448. ClassDB::bind_method(D_METHOD("add_file", "path", "file", "remap"), &EditorExportPlugin::add_file);
  449. ClassDB::bind_method(D_METHOD("add_ios_framework", "path"), &EditorExportPlugin::add_ios_framework);
  450. ClassDB::bind_method(D_METHOD("add_ios_plist_content", "plist_content"), &EditorExportPlugin::add_ios_plist_content);
  451. ClassDB::bind_method(D_METHOD("add_ios_linker_flags", "flags"), &EditorExportPlugin::add_ios_linker_flags);
  452. ClassDB::bind_method(D_METHOD("add_ios_bundle_file", "path"), &EditorExportPlugin::add_ios_bundle_file);
  453. ClassDB::bind_method(D_METHOD("add_ios_cpp_code", "code"), &EditorExportPlugin::add_ios_cpp_code);
  454. ClassDB::bind_method(D_METHOD("skip"), &EditorExportPlugin::skip);
  455. BIND_VMETHOD(MethodInfo("_export_file", PropertyInfo(Variant::STRING, "path"), PropertyInfo(Variant::STRING, "type"), PropertyInfo(Variant::POOL_STRING_ARRAY, "features")));
  456. BIND_VMETHOD(MethodInfo("_export_begin", PropertyInfo(Variant::POOL_STRING_ARRAY, "features"), PropertyInfo(Variant::BOOL, "is_debug"), PropertyInfo(Variant::STRING, "path"), PropertyInfo(Variant::INT, "flags")));
  457. BIND_VMETHOD(MethodInfo("_export_end"));
  458. }
  459. EditorExportPlugin::EditorExportPlugin() {
  460. skipped = false;
  461. }
  462. EditorExportPlatform::FeatureContainers EditorExportPlatform::get_feature_containers(const Ref<EditorExportPreset> &p_preset) {
  463. Ref<EditorExportPlatform> platform = p_preset->get_platform();
  464. List<String> feature_list;
  465. platform->get_platform_features(&feature_list);
  466. platform->get_preset_features(p_preset, &feature_list);
  467. FeatureContainers result;
  468. for (List<String>::Element *E = feature_list.front(); E; E = E->next()) {
  469. result.features.insert(E->get());
  470. result.features_pv.push_back(E->get());
  471. }
  472. if (p_preset->get_custom_features() != String()) {
  473. Vector<String> tmp_custom_list = p_preset->get_custom_features().split(",");
  474. for (int i = 0; i < tmp_custom_list.size(); i++) {
  475. String f = tmp_custom_list[i].strip_edges();
  476. if (f != String()) {
  477. result.features.insert(f);
  478. result.features_pv.push_back(f);
  479. }
  480. }
  481. }
  482. return result;
  483. }
  484. EditorExportPlatform::ExportNotifier::ExportNotifier(EditorExportPlatform &p_platform, const Ref<EditorExportPreset> &p_preset, bool p_debug, const String &p_path, int p_flags) {
  485. FeatureContainers features = p_platform.get_feature_containers(p_preset);
  486. Vector<Ref<EditorExportPlugin> > export_plugins = EditorExport::get_singleton()->get_export_plugins();
  487. //initial export plugin callback
  488. for (int i = 0; i < export_plugins.size(); i++) {
  489. if (export_plugins[i]->get_script_instance()) { //script based
  490. export_plugins.write[i]->_export_begin_script(features.features_pv, p_debug, p_path, p_flags);
  491. } else {
  492. export_plugins.write[i]->_export_begin(features.features, p_debug, p_path, p_flags);
  493. }
  494. }
  495. }
  496. EditorExportPlatform::ExportNotifier::~ExportNotifier() {
  497. Vector<Ref<EditorExportPlugin> > export_plugins = EditorExport::get_singleton()->get_export_plugins();
  498. for (int i = 0; i < export_plugins.size(); i++) {
  499. if (export_plugins[i]->get_script_instance()) {
  500. export_plugins.write[i]->_export_end_script();
  501. }
  502. export_plugins.write[i]->_export_end();
  503. }
  504. }
  505. Error EditorExportPlatform::export_project_files(const Ref<EditorExportPreset> &p_preset, EditorExportSaveFunction p_func, void *p_udata, EditorExportSaveSharedObject p_so_func) {
  506. //figure out paths of files that will be exported
  507. Set<String> paths;
  508. Vector<String> path_remaps;
  509. if (p_preset->get_export_filter() == EditorExportPreset::EXPORT_ALL_RESOURCES) {
  510. //find stuff
  511. _export_find_resources(EditorFileSystem::get_singleton()->get_filesystem(), paths);
  512. } else {
  513. bool scenes_only = p_preset->get_export_filter() == EditorExportPreset::EXPORT_SELECTED_SCENES;
  514. Vector<String> files = p_preset->get_files_to_export();
  515. for (int i = 0; i < files.size(); i++) {
  516. if (scenes_only && ResourceLoader::get_resource_type(files[i]) != "PackedScene")
  517. continue;
  518. _export_find_dependencies(files[i], paths);
  519. }
  520. }
  521. _edit_filter_list(paths, p_preset->get_include_filter(), false);
  522. _edit_filter_list(paths, p_preset->get_exclude_filter(), true);
  523. Vector<Ref<EditorExportPlugin> > export_plugins = EditorExport::get_singleton()->get_export_plugins();
  524. for (int i = 0; i < export_plugins.size(); i++) {
  525. export_plugins.write[i]->set_export_preset(p_preset);
  526. if (p_so_func) {
  527. for (int j = 0; j < export_plugins[i]->shared_objects.size(); j++) {
  528. p_so_func(p_udata, export_plugins[i]->shared_objects[j]);
  529. }
  530. }
  531. for (int j = 0; j < export_plugins[i]->extra_files.size(); j++) {
  532. p_func(p_udata, export_plugins[i]->extra_files[j].path, export_plugins[i]->extra_files[j].data, 0, paths.size());
  533. }
  534. export_plugins.write[i]->_clear();
  535. }
  536. FeatureContainers feature_containers = get_feature_containers(p_preset);
  537. Set<String> &features = feature_containers.features;
  538. PoolVector<String> &features_pv = feature_containers.features_pv;
  539. //store everything in the export medium
  540. int idx = 0;
  541. int total = paths.size();
  542. for (Set<String>::Element *E = paths.front(); E; E = E->next()) {
  543. String path = E->get();
  544. String type = ResourceLoader::get_resource_type(path);
  545. if (FileAccess::exists(path + ".import")) {
  546. //file is imported, replace by what it imports
  547. Ref<ConfigFile> config;
  548. config.instance();
  549. Error err = config->load(path + ".import");
  550. if (err != OK) {
  551. ERR_PRINTS("Could not parse: '" + path + "', not exported.");
  552. continue;
  553. }
  554. List<String> remaps;
  555. config->get_section_keys("remap", &remaps);
  556. Set<String> remap_features;
  557. for (List<String>::Element *F = remaps.front(); F; F = F->next()) {
  558. String remap = F->get();
  559. String feature = remap.get_slice(".", 1);
  560. if (features.has(feature)) {
  561. remap_features.insert(feature);
  562. }
  563. }
  564. if (remap_features.size() > 1) {
  565. this->resolve_platform_feature_priorities(p_preset, remap_features);
  566. }
  567. for (List<String>::Element *F = remaps.front(); F; F = F->next()) {
  568. String remap = F->get();
  569. if (remap == "path") {
  570. String remapped_path = config->get_value("remap", remap);
  571. Vector<uint8_t> array = FileAccess::get_file_as_array(remapped_path);
  572. p_func(p_udata, remapped_path, array, idx, total);
  573. } else if (remap.begins_with("path.")) {
  574. String feature = remap.get_slice(".", 1);
  575. if (remap_features.has(feature)) {
  576. String remapped_path = config->get_value("remap", remap);
  577. Vector<uint8_t> array = FileAccess::get_file_as_array(remapped_path);
  578. p_func(p_udata, remapped_path, array, idx, total);
  579. }
  580. }
  581. }
  582. //also save the .import file
  583. Vector<uint8_t> array = FileAccess::get_file_as_array(path + ".import");
  584. p_func(p_udata, path + ".import", array, idx, total);
  585. } else {
  586. bool do_export = true;
  587. for (int i = 0; i < export_plugins.size(); i++) {
  588. if (export_plugins[i]->get_script_instance()) { //script based
  589. export_plugins.write[i]->_export_file_script(path, type, features_pv);
  590. } else {
  591. export_plugins.write[i]->_export_file(path, type, features);
  592. }
  593. if (p_so_func) {
  594. for (int j = 0; j < export_plugins[i]->shared_objects.size(); j++) {
  595. p_so_func(p_udata, export_plugins[i]->shared_objects[j]);
  596. }
  597. }
  598. for (int j = 0; j < export_plugins[i]->extra_files.size(); j++) {
  599. p_func(p_udata, export_plugins[i]->extra_files[j].path, export_plugins[i]->extra_files[j].data, idx, total);
  600. if (export_plugins[i]->extra_files[j].remap) {
  601. do_export = false; //if remap, do not
  602. path_remaps.push_back(path);
  603. path_remaps.push_back(export_plugins[i]->extra_files[j].path);
  604. }
  605. }
  606. if (export_plugins[i]->skipped) {
  607. do_export = false;
  608. }
  609. export_plugins.write[i]->_clear();
  610. if (!do_export)
  611. break; //apologies, not exporting
  612. }
  613. //just store it as it comes
  614. if (do_export) {
  615. Vector<uint8_t> array = FileAccess::get_file_as_array(path);
  616. p_func(p_udata, path, array, idx, total);
  617. }
  618. }
  619. idx++;
  620. }
  621. //save config!
  622. Vector<String> custom_list;
  623. if (p_preset->get_custom_features() != String()) {
  624. Vector<String> tmp_custom_list = p_preset->get_custom_features().split(",");
  625. for (int i = 0; i < tmp_custom_list.size(); i++) {
  626. String f = tmp_custom_list[i].strip_edges();
  627. if (f != String()) {
  628. custom_list.push_back(f);
  629. }
  630. }
  631. }
  632. ProjectSettings::CustomMap custom_map;
  633. if (path_remaps.size()) {
  634. if (1) { //new remap mode, use always as it's friendlier with multiple .pck exports
  635. for (int i = 0; i < path_remaps.size(); i += 2) {
  636. String from = path_remaps[i];
  637. String to = path_remaps[i + 1];
  638. String remap_file = "[remap]\n\npath=\"" + to.c_escape() + "\"\n";
  639. CharString utf8 = remap_file.utf8();
  640. Vector<uint8_t> new_file;
  641. new_file.resize(utf8.length());
  642. for (int j = 0; j < utf8.length(); j++) {
  643. new_file.write[j] = utf8[j];
  644. }
  645. p_func(p_udata, from + ".remap", new_file, idx, total);
  646. }
  647. } else {
  648. //old remap mode, will still work, but it's unused because it's not multiple pck export friendly
  649. custom_map["path_remap/remapped_paths"] = path_remaps;
  650. }
  651. }
  652. // Store icon and splash images directly, they need to bypass the import system and be loaded as images
  653. String icon = ProjectSettings::get_singleton()->get("application/config/icon");
  654. String splash = ProjectSettings::get_singleton()->get("application/boot_splash/image");
  655. if (icon != String() && FileAccess::exists(icon)) {
  656. Vector<uint8_t> array = FileAccess::get_file_as_array(icon);
  657. p_func(p_udata, icon, array, idx, total);
  658. }
  659. if (splash != String() && FileAccess::exists(splash) && icon != splash) {
  660. Vector<uint8_t> array = FileAccess::get_file_as_array(splash);
  661. p_func(p_udata, splash, array, idx, total);
  662. }
  663. String config_file = "project.binary";
  664. String engine_cfb = EditorSettings::get_singleton()->get_cache_dir().plus_file("tmp" + config_file);
  665. ProjectSettings::get_singleton()->save_custom(engine_cfb, custom_map, custom_list);
  666. Vector<uint8_t> data = FileAccess::get_file_as_array(engine_cfb);
  667. p_func(p_udata, "res://" + config_file, data, idx, total);
  668. return OK;
  669. }
  670. Error EditorExportPlatform::_add_shared_object(void *p_userdata, const SharedObject &p_so) {
  671. PackData *pack_data = (PackData *)p_userdata;
  672. if (pack_data->so_files) {
  673. pack_data->so_files->push_back(p_so);
  674. }
  675. return OK;
  676. }
  677. Error EditorExportPlatform::save_pack(const Ref<EditorExportPreset> &p_preset, const String &p_path, Vector<SharedObject> *p_so_files) {
  678. EditorProgress ep("savepack", TTR("Packing"), 102);
  679. String tmppath = EditorSettings::get_singleton()->get_cache_dir().plus_file("packtmp");
  680. FileAccess *ftmp = FileAccess::open(tmppath, FileAccess::WRITE);
  681. ERR_FAIL_COND_V(!ftmp, ERR_CANT_CREATE)
  682. PackData pd;
  683. pd.ep = &ep;
  684. pd.f = ftmp;
  685. pd.so_files = p_so_files;
  686. Error err = export_project_files(p_preset, _save_pack_file, &pd, _add_shared_object);
  687. memdelete(ftmp); //close tmp file
  688. if (err)
  689. return err;
  690. pd.file_ofs.sort(); //do sort, so we can do binary search later
  691. FileAccess *f = FileAccess::open(p_path, FileAccess::WRITE);
  692. ERR_FAIL_COND_V(!f, ERR_CANT_CREATE)
  693. f->store_32(0x43504447); //GDPK
  694. f->store_32(1); //pack version
  695. f->store_32(VERSION_MAJOR);
  696. f->store_32(VERSION_MINOR);
  697. f->store_32(0); //hmph
  698. for (int i = 0; i < 16; i++) {
  699. //reserved
  700. f->store_32(0);
  701. }
  702. f->store_32(pd.file_ofs.size()); //amount of files
  703. size_t header_size = f->get_position();
  704. //precalculate header size
  705. for (int i = 0; i < pd.file_ofs.size(); i++) {
  706. header_size += 4; // size of path string (32 bits is enough)
  707. uint32_t string_len = pd.file_ofs[i].path_utf8.length();
  708. header_size += string_len + _get_pad(4, string_len); ///size of path string
  709. header_size += 8; // offset to file _with_ header size included
  710. header_size += 8; // size of file
  711. header_size += 16; // md5
  712. }
  713. size_t header_padding = _get_pad(PCK_PADDING, header_size);
  714. for (int i = 0; i < pd.file_ofs.size(); i++) {
  715. uint32_t string_len = pd.file_ofs[i].path_utf8.length();
  716. uint32_t pad = _get_pad(4, string_len);
  717. ;
  718. f->store_32(string_len + pad);
  719. f->store_buffer((const uint8_t *)pd.file_ofs[i].path_utf8.get_data(), string_len);
  720. for (uint32_t j = 0; j < pad; j++) {
  721. f->store_8(0);
  722. }
  723. f->store_64(pd.file_ofs[i].ofs + header_padding + header_size);
  724. f->store_64(pd.file_ofs[i].size); // pay attention here, this is where file is
  725. f->store_buffer(pd.file_ofs[i].md5.ptr(), 16); //also save md5 for file
  726. }
  727. for (uint32_t j = 0; j < header_padding; j++) {
  728. f->store_8(0);
  729. }
  730. //save the rest of the data
  731. ftmp = FileAccess::open(tmppath, FileAccess::READ);
  732. if (!ftmp) {
  733. memdelete(f);
  734. ERR_FAIL_COND_V(!ftmp, ERR_CANT_CREATE)
  735. }
  736. const int bufsize = 16384;
  737. uint8_t buf[bufsize];
  738. while (true) {
  739. int got = ftmp->get_buffer(buf, bufsize);
  740. if (got <= 0)
  741. break;
  742. f->store_buffer(buf, got);
  743. }
  744. memdelete(ftmp);
  745. f->store_32(0x43504447); //GDPK
  746. memdelete(f);
  747. return OK;
  748. }
  749. Error EditorExportPlatform::save_zip(const Ref<EditorExportPreset> &p_preset, const String &p_path) {
  750. EditorProgress ep("savezip", TTR("Packing"), 102);
  751. //FileAccess *tmp = FileAccess::open(tmppath,FileAccess::WRITE);
  752. FileAccess *src_f;
  753. zlib_filefunc_def io = zipio_create_io_from_file(&src_f);
  754. zipFile zip = zipOpen2(p_path.utf8().get_data(), APPEND_STATUS_CREATE, NULL, &io);
  755. ZipData zd;
  756. zd.ep = &ep;
  757. zd.zip = zip;
  758. Error err = export_project_files(p_preset, _save_zip_file, &zd);
  759. if (err != OK)
  760. ERR_PRINT("Failed to export project files");
  761. zipClose(zip, NULL);
  762. return OK;
  763. }
  764. Error EditorExportPlatform::export_pack(const Ref<EditorExportPreset> &p_preset, bool p_debug, const String &p_path, int p_flags) {
  765. ExportNotifier notifier(*this, p_preset, p_debug, p_path, p_flags);
  766. return save_pack(p_preset, p_path);
  767. }
  768. Error EditorExportPlatform::export_zip(const Ref<EditorExportPreset> &p_preset, bool p_debug, const String &p_path, int p_flags) {
  769. ExportNotifier notifier(*this, p_preset, p_debug, p_path, p_flags);
  770. return save_zip(p_preset, p_path);
  771. }
  772. void EditorExportPlatform::gen_export_flags(Vector<String> &r_flags, int p_flags) {
  773. String host = EditorSettings::get_singleton()->get("network/debug/remote_host");
  774. int remote_port = (int)EditorSettings::get_singleton()->get("network/debug/remote_port");
  775. if (p_flags & DEBUG_FLAG_REMOTE_DEBUG_LOCALHOST)
  776. host = "localhost";
  777. if (p_flags & DEBUG_FLAG_DUMB_CLIENT) {
  778. int port = EditorSettings::get_singleton()->get("filesystem/file_server/port");
  779. String passwd = EditorSettings::get_singleton()->get("filesystem/file_server/password");
  780. r_flags.push_back("--remote-fs");
  781. r_flags.push_back(host + ":" + itos(port));
  782. if (passwd != "") {
  783. r_flags.push_back("--remote-fs-password");
  784. r_flags.push_back(passwd);
  785. }
  786. }
  787. if (p_flags & DEBUG_FLAG_REMOTE_DEBUG) {
  788. r_flags.push_back("--remote-debug");
  789. r_flags.push_back(host + ":" + String::num(remote_port));
  790. List<String> breakpoints;
  791. ScriptEditor::get_singleton()->get_breakpoints(&breakpoints);
  792. if (breakpoints.size()) {
  793. r_flags.push_back("--breakpoints");
  794. String bpoints;
  795. for (const List<String>::Element *E = breakpoints.front(); E; E = E->next()) {
  796. bpoints += E->get().replace(" ", "%20");
  797. if (E->next())
  798. bpoints += ",";
  799. }
  800. r_flags.push_back(bpoints);
  801. }
  802. }
  803. if (p_flags & DEBUG_FLAG_VIEW_COLLISONS) {
  804. r_flags.push_back("--debug-collisions");
  805. }
  806. if (p_flags & DEBUG_FLAG_VIEW_NAVIGATION) {
  807. r_flags.push_back("--debug-navigation");
  808. }
  809. }
  810. EditorExportPlatform::EditorExportPlatform() {
  811. }
  812. ////
  813. EditorExport *EditorExport::singleton = NULL;
  814. void EditorExport::_save() {
  815. Ref<ConfigFile> config;
  816. config.instance();
  817. for (int i = 0; i < export_presets.size(); i++) {
  818. Ref<EditorExportPreset> preset = export_presets[i];
  819. String section = "preset." + itos(i);
  820. config->set_value(section, "name", preset->get_name());
  821. config->set_value(section, "platform", preset->get_platform()->get_name());
  822. config->set_value(section, "runnable", preset->is_runnable());
  823. config->set_value(section, "custom_features", preset->get_custom_features());
  824. bool save_files = false;
  825. switch (preset->get_export_filter()) {
  826. case EditorExportPreset::EXPORT_ALL_RESOURCES: {
  827. config->set_value(section, "export_filter", "all_resources");
  828. } break;
  829. case EditorExportPreset::EXPORT_SELECTED_SCENES: {
  830. config->set_value(section, "export_filter", "scenes");
  831. save_files = true;
  832. } break;
  833. case EditorExportPreset::EXPORT_SELECTED_RESOURCES: {
  834. config->set_value(section, "export_filter", "resources");
  835. save_files = true;
  836. } break;
  837. }
  838. if (save_files) {
  839. Vector<String> export_files = preset->get_files_to_export();
  840. config->set_value(section, "export_files", export_files);
  841. }
  842. config->set_value(section, "include_filter", preset->get_include_filter());
  843. config->set_value(section, "exclude_filter", preset->get_exclude_filter());
  844. config->set_value(section, "export_path", preset->get_export_path());
  845. config->set_value(section, "patch_list", preset->get_patches());
  846. config->set_value(section, "script_export_mode", preset->get_script_export_mode());
  847. config->set_value(section, "script_encryption_key", preset->get_script_encryption_key());
  848. String option_section = "preset." + itos(i) + ".options";
  849. for (const List<PropertyInfo>::Element *E = preset->get_properties().front(); E; E = E->next()) {
  850. config->set_value(option_section, E->get().name, preset->get(E->get().name));
  851. }
  852. }
  853. config->save("res://export_presets.cfg");
  854. }
  855. void EditorExport::save_presets() {
  856. if (block_save)
  857. return;
  858. save_timer->start();
  859. }
  860. void EditorExport::_bind_methods() {
  861. ClassDB::bind_method("_save", &EditorExport::_save);
  862. }
  863. void EditorExport::add_export_platform(const Ref<EditorExportPlatform> &p_platform) {
  864. export_platforms.push_back(p_platform);
  865. }
  866. int EditorExport::get_export_platform_count() {
  867. return export_platforms.size();
  868. }
  869. Ref<EditorExportPlatform> EditorExport::get_export_platform(int p_idx) {
  870. ERR_FAIL_INDEX_V(p_idx, export_platforms.size(), Ref<EditorExportPlatform>());
  871. return export_platforms[p_idx];
  872. }
  873. void EditorExport::add_export_preset(const Ref<EditorExportPreset> &p_preset, int p_at_pos) {
  874. if (p_at_pos < 0)
  875. export_presets.push_back(p_preset);
  876. else
  877. export_presets.insert(p_at_pos, p_preset);
  878. }
  879. String EditorExportPlatform::test_etc2() const {
  880. String driver = ProjectSettings::get_singleton()->get("rendering/quality/driver/driver_name");
  881. bool driver_fallback = ProjectSettings::get_singleton()->get("rendering/quality/driver/fallback_to_gles2");
  882. bool etc_supported = ProjectSettings::get_singleton()->get("rendering/vram_compression/import_etc");
  883. bool etc2_supported = ProjectSettings::get_singleton()->get("rendering/vram_compression/import_etc2");
  884. if (driver == "GLES2" && !etc_supported) {
  885. return TTR("Target platform requires 'ETC' texture compression for GLES2. Enable 'Import Etc' in Project Settings.");
  886. } else if (driver == "GLES3") {
  887. String err;
  888. if (!etc2_supported) {
  889. err += TTR("Target platform requires 'ETC2' texture compression for GLES3. Enable 'Import Etc 2' in Project Settings.");
  890. }
  891. if (driver_fallback && !etc_supported) {
  892. if (err != String())
  893. err += "\n";
  894. err += TTR("Target platform requires 'ETC' texture compression for the driver fallback to GLES2.\nEnable 'Import Etc' in Project Settings, or disable 'Driver Fallback Enabled'.");
  895. }
  896. return err;
  897. }
  898. return String();
  899. }
  900. int EditorExport::get_export_preset_count() const {
  901. return export_presets.size();
  902. }
  903. Ref<EditorExportPreset> EditorExport::get_export_preset(int p_idx) {
  904. ERR_FAIL_INDEX_V(p_idx, export_presets.size(), Ref<EditorExportPreset>());
  905. return export_presets[p_idx];
  906. }
  907. void EditorExport::remove_export_preset(int p_idx) {
  908. export_presets.remove(p_idx);
  909. save_presets();
  910. }
  911. void EditorExport::add_export_plugin(const Ref<EditorExportPlugin> &p_plugin) {
  912. if (export_plugins.find(p_plugin) == -1) {
  913. export_plugins.push_back(p_plugin);
  914. }
  915. }
  916. void EditorExport::remove_export_plugin(const Ref<EditorExportPlugin> &p_plugin) {
  917. export_plugins.erase(p_plugin);
  918. }
  919. Vector<Ref<EditorExportPlugin> > EditorExport::get_export_plugins() {
  920. return export_plugins;
  921. }
  922. void EditorExport::_notification(int p_what) {
  923. if (p_what == NOTIFICATION_ENTER_TREE) {
  924. load_config();
  925. }
  926. }
  927. void EditorExport::load_config() {
  928. Ref<ConfigFile> config;
  929. config.instance();
  930. Error err = config->load("res://export_presets.cfg");
  931. if (err != OK)
  932. return;
  933. block_save = true;
  934. int index = 0;
  935. while (true) {
  936. String section = "preset." + itos(index);
  937. if (!config->has_section(section))
  938. break;
  939. String platform = config->get_value(section, "platform");
  940. Ref<EditorExportPreset> preset;
  941. for (int i = 0; i < export_platforms.size(); i++) {
  942. if (export_platforms[i]->get_name() == platform) {
  943. preset = export_platforms.write[i]->create_preset();
  944. break;
  945. }
  946. }
  947. if (!preset.is_valid()) {
  948. index++;
  949. ERR_CONTINUE(!preset.is_valid());
  950. }
  951. preset->set_name(config->get_value(section, "name"));
  952. preset->set_runnable(config->get_value(section, "runnable"));
  953. if (config->has_section_key(section, "custom_features")) {
  954. preset->set_custom_features(config->get_value(section, "custom_features"));
  955. }
  956. String export_filter = config->get_value(section, "export_filter");
  957. bool get_files = false;
  958. if (export_filter == "all_resources") {
  959. preset->set_export_filter(EditorExportPreset::EXPORT_ALL_RESOURCES);
  960. } else if (export_filter == "scenes") {
  961. preset->set_export_filter(EditorExportPreset::EXPORT_SELECTED_SCENES);
  962. get_files = true;
  963. } else if (export_filter == "resources") {
  964. preset->set_export_filter(EditorExportPreset::EXPORT_SELECTED_RESOURCES);
  965. get_files = true;
  966. }
  967. if (get_files) {
  968. Vector<String> files = config->get_value(section, "export_files");
  969. for (int i = 0; i < files.size(); i++) {
  970. preset->add_export_file(files[i]);
  971. }
  972. }
  973. preset->set_include_filter(config->get_value(section, "include_filter"));
  974. preset->set_exclude_filter(config->get_value(section, "exclude_filter"));
  975. preset->set_export_path(config->get_value(section, "export_path", ""));
  976. Vector<String> patch_list = config->get_value(section, "patch_list");
  977. for (int i = 0; i < patch_list.size(); i++) {
  978. preset->add_patch(patch_list[i]);
  979. }
  980. if (config->has_section_key(section, "script_export_mode")) {
  981. preset->set_script_export_mode(config->get_value(section, "script_export_mode"));
  982. }
  983. if (config->has_section_key(section, "script_encryption_key")) {
  984. preset->set_script_encryption_key(config->get_value(section, "script_encryption_key"));
  985. }
  986. String option_section = "preset." + itos(index) + ".options";
  987. List<String> options;
  988. config->get_section_keys(option_section, &options);
  989. for (List<String>::Element *E = options.front(); E; E = E->next()) {
  990. Variant value = config->get_value(option_section, E->get());
  991. preset->set(E->get(), value);
  992. }
  993. add_export_preset(preset);
  994. index++;
  995. }
  996. block_save = false;
  997. }
  998. bool EditorExport::poll_export_platforms() {
  999. bool changed = false;
  1000. for (int i = 0; i < export_platforms.size(); i++) {
  1001. if (export_platforms.write[i]->poll_devices()) {
  1002. changed = true;
  1003. }
  1004. }
  1005. return changed;
  1006. }
  1007. EditorExport::EditorExport() {
  1008. save_timer = memnew(Timer);
  1009. add_child(save_timer);
  1010. save_timer->set_wait_time(0.8);
  1011. save_timer->set_one_shot(true);
  1012. save_timer->connect("timeout", this, "_save");
  1013. block_save = false;
  1014. singleton = this;
  1015. }
  1016. EditorExport::~EditorExport() {
  1017. }
  1018. //////////
  1019. void EditorExportPlatformPC::get_preset_features(const Ref<EditorExportPreset> &p_preset, List<String> *r_features) {
  1020. if (p_preset->get("texture_format/s3tc")) {
  1021. r_features->push_back("s3tc");
  1022. }
  1023. if (p_preset->get("texture_format/etc")) {
  1024. r_features->push_back("etc");
  1025. }
  1026. if (p_preset->get("texture_format/etc2")) {
  1027. r_features->push_back("etc2");
  1028. }
  1029. if (p_preset->get("binary_format/64_bits")) {
  1030. r_features->push_back("64");
  1031. } else {
  1032. r_features->push_back("32");
  1033. }
  1034. }
  1035. void EditorExportPlatformPC::get_export_options(List<ExportOption> *r_options) {
  1036. r_options->push_back(ExportOption(PropertyInfo(Variant::BOOL, "texture_format/bptc"), false));
  1037. r_options->push_back(ExportOption(PropertyInfo(Variant::BOOL, "texture_format/s3tc"), true));
  1038. r_options->push_back(ExportOption(PropertyInfo(Variant::BOOL, "texture_format/etc"), false));
  1039. r_options->push_back(ExportOption(PropertyInfo(Variant::BOOL, "texture_format/etc2"), false));
  1040. r_options->push_back(ExportOption(PropertyInfo(Variant::BOOL, "texture_format/no_bptc_fallbacks"), true));
  1041. r_options->push_back(ExportOption(PropertyInfo(Variant::BOOL, "binary_format/64_bits"), true));
  1042. r_options->push_back(ExportOption(PropertyInfo(Variant::STRING, "custom_template/release", PROPERTY_HINT_GLOBAL_FILE), ""));
  1043. r_options->push_back(ExportOption(PropertyInfo(Variant::STRING, "custom_template/debug", PROPERTY_HINT_GLOBAL_FILE), ""));
  1044. }
  1045. String EditorExportPlatformPC::get_name() const {
  1046. return name;
  1047. }
  1048. String EditorExportPlatformPC::get_os_name() const {
  1049. return os_name;
  1050. }
  1051. Ref<Texture> EditorExportPlatformPC::get_logo() const {
  1052. return logo;
  1053. }
  1054. bool EditorExportPlatformPC::can_export(const Ref<EditorExportPreset> &p_preset, String &r_error, bool &r_missing_templates) const {
  1055. String err;
  1056. bool valid = true;
  1057. bool use64 = p_preset->get("binary_format/64_bits");
  1058. if (use64 && (!exists_export_template(debug_file_64, &err) || !exists_export_template(release_file_64, &err))) {
  1059. valid = false;
  1060. }
  1061. if (!use64 && (!exists_export_template(debug_file_32, &err) || !exists_export_template(release_file_32, &err))) {
  1062. valid = false;
  1063. }
  1064. String custom_debug_binary = p_preset->get("custom_template/debug");
  1065. String custom_release_binary = p_preset->get("custom_template/release");
  1066. if (custom_debug_binary == "" && custom_release_binary == "") {
  1067. if (!err.empty())
  1068. r_error = err;
  1069. r_missing_templates = !valid;
  1070. return valid;
  1071. }
  1072. bool dvalid = true;
  1073. bool rvalid = true;
  1074. if (!FileAccess::exists(custom_debug_binary)) {
  1075. dvalid = false;
  1076. err += TTR("Custom debug template not found.") + "\n";
  1077. }
  1078. if (!FileAccess::exists(custom_release_binary)) {
  1079. rvalid = false;
  1080. err += TTR("Custom release template not found.") + "\n";
  1081. }
  1082. if (dvalid || rvalid)
  1083. valid = true;
  1084. else
  1085. valid = false;
  1086. if (!err.empty())
  1087. r_error = err;
  1088. return valid;
  1089. }
  1090. List<String> EditorExportPlatformPC::get_binary_extensions(const Ref<EditorExportPreset> &p_preset) const {
  1091. List<String> list;
  1092. for (Map<String, String>::Element *E = extensions.front(); E; E = E->next()) {
  1093. if (p_preset->get(E->key())) {
  1094. list.push_back(extensions[E->key()]);
  1095. return list;
  1096. }
  1097. }
  1098. if (extensions.has("default")) {
  1099. list.push_back(extensions["default"]);
  1100. return list;
  1101. }
  1102. return list;
  1103. }
  1104. Error EditorExportPlatformPC::export_project(const Ref<EditorExportPreset> &p_preset, bool p_debug, const String &p_path, int p_flags) {
  1105. ExportNotifier notifier(*this, p_preset, p_debug, p_path, p_flags);
  1106. if (!DirAccess::exists(p_path.get_base_dir())) {
  1107. return ERR_FILE_BAD_PATH;
  1108. }
  1109. String custom_debug = p_preset->get("custom_template/debug");
  1110. String custom_release = p_preset->get("custom_template/release");
  1111. String template_path = p_debug ? custom_debug : custom_release;
  1112. template_path = template_path.strip_edges();
  1113. if (template_path == String()) {
  1114. if (p_preset->get("binary_format/64_bits")) {
  1115. if (p_debug) {
  1116. template_path = find_export_template(debug_file_64);
  1117. } else {
  1118. template_path = find_export_template(release_file_64);
  1119. }
  1120. } else {
  1121. if (p_debug) {
  1122. template_path = find_export_template(debug_file_32);
  1123. } else {
  1124. template_path = find_export_template(release_file_32);
  1125. }
  1126. }
  1127. }
  1128. if (template_path != String() && !FileAccess::exists(template_path)) {
  1129. EditorNode::get_singleton()->show_warning(TTR("Template file not found:") + "\n" + template_path);
  1130. return ERR_FILE_NOT_FOUND;
  1131. }
  1132. DirAccess *da = DirAccess::create(DirAccess::ACCESS_FILESYSTEM);
  1133. Error err = da->copy(template_path, p_path, get_chmod_flags());
  1134. if (err == OK) {
  1135. String pck_path = p_path.get_basename() + ".pck";
  1136. Vector<SharedObject> so_files;
  1137. err = save_pack(p_preset, pck_path, &so_files);
  1138. if (err == OK && !so_files.empty()) {
  1139. //if shared object files, copy them
  1140. da = DirAccess::create(DirAccess::ACCESS_FILESYSTEM);
  1141. for (int i = 0; i < so_files.size() && err == OK; i++) {
  1142. err = da->copy(so_files[i].path, p_path.get_base_dir().plus_file(so_files[i].path.get_file()));
  1143. }
  1144. }
  1145. }
  1146. memdelete(da);
  1147. return err;
  1148. }
  1149. void EditorExportPlatformPC::set_extension(const String &p_extension, const String &p_feature_key) {
  1150. extensions[p_feature_key] = p_extension;
  1151. }
  1152. void EditorExportPlatformPC::set_name(const String &p_name) {
  1153. name = p_name;
  1154. }
  1155. void EditorExportPlatformPC::set_os_name(const String &p_name) {
  1156. os_name = p_name;
  1157. }
  1158. void EditorExportPlatformPC::set_logo(const Ref<Texture> &p_logo) {
  1159. logo = p_logo;
  1160. }
  1161. void EditorExportPlatformPC::set_release_64(const String &p_file) {
  1162. release_file_64 = p_file;
  1163. }
  1164. void EditorExportPlatformPC::set_release_32(const String &p_file) {
  1165. release_file_32 = p_file;
  1166. }
  1167. void EditorExportPlatformPC::set_debug_64(const String &p_file) {
  1168. debug_file_64 = p_file;
  1169. }
  1170. void EditorExportPlatformPC::set_debug_32(const String &p_file) {
  1171. debug_file_32 = p_file;
  1172. }
  1173. void EditorExportPlatformPC::add_platform_feature(const String &p_feature) {
  1174. extra_features.insert(p_feature);
  1175. }
  1176. void EditorExportPlatformPC::get_platform_features(List<String> *r_features) {
  1177. r_features->push_back("pc"); //all pcs support "pc"
  1178. r_features->push_back("s3tc"); //all pcs support "s3tc" compression
  1179. r_features->push_back(get_os_name()); //OS name is a feature
  1180. for (Set<String>::Element *E = extra_features.front(); E; E = E->next()) {
  1181. r_features->push_back(E->get());
  1182. }
  1183. }
  1184. void EditorExportPlatformPC::resolve_platform_feature_priorities(const Ref<EditorExportPreset> &p_preset, Set<String> &p_features) {
  1185. if (p_features.has("bptc")) {
  1186. if (p_preset->has("texture_format/no_bptc_fallbacks")) {
  1187. p_features.erase("s3tc");
  1188. }
  1189. }
  1190. }
  1191. int EditorExportPlatformPC::get_chmod_flags() const {
  1192. return chmod_flags;
  1193. }
  1194. void EditorExportPlatformPC::set_chmod_flags(int p_flags) {
  1195. chmod_flags = p_flags;
  1196. }
  1197. EditorExportPlatformPC::EditorExportPlatformPC() {
  1198. chmod_flags = -1;
  1199. }
  1200. ///////////////////////
  1201. void EditorExportTextSceneToBinaryPlugin::_export_file(const String &p_path, const String &p_type, const Set<String> &p_features) {
  1202. String extension = p_path.get_extension().to_lower();
  1203. if (extension != "tres" && extension != "tscn") {
  1204. return;
  1205. }
  1206. bool convert = GLOBAL_GET("editor/convert_text_resources_to_binary_on_export");
  1207. if (!convert)
  1208. return;
  1209. String tmp_path = EditorSettings::get_singleton()->get_cache_dir().plus_file("file.res");
  1210. Error err = ResourceFormatLoaderText::convert_file_to_binary(p_path, tmp_path);
  1211. ERR_FAIL_COND(err != OK);
  1212. Vector<uint8_t> data = FileAccess::get_file_as_array(tmp_path);
  1213. ERR_FAIL_COND(data.size() == 0);
  1214. add_file(p_path + ".converted.res", data, true);
  1215. }
  1216. EditorExportTextSceneToBinaryPlugin::EditorExportTextSceneToBinaryPlugin() {
  1217. GLOBAL_DEF("editor/convert_text_resources_to_binary_on_export", false);
  1218. }