editor_export.cpp 50 KB

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