editor_export.cpp 44 KB

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