editor_export.cpp 46 KB

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