editor_export.cpp 47 KB

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