editor_import_export.cpp 63 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084208520862087208820892090209120922093209420952096209720982099210021012102210321042105210621072108210921102111211221132114211521162117211821192120212121222123212421252126212721282129213021312132213321342135213621372138213921402141214221432144214521462147214821492150215121522153215421552156215721582159216021612162216321642165216621672168216921702171
  1. /*************************************************************************/
  2. /* editor_import_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_import_export.h"
  31. #include "editor/editor_file_system.h"
  32. #include "editor/plugins/script_editor_plugin.h"
  33. #include "editor_node.h"
  34. #include "editor_settings.h"
  35. #include "globals.h"
  36. #include "io/config_file.h"
  37. #include "io/resource_loader.h"
  38. #include "io/resource_saver.h"
  39. #include "io/zip_io.h"
  40. #include "io_plugins/editor_texture_import_plugin.h"
  41. #include "os/dir_access.h"
  42. #include "os/file_access.h"
  43. #include "script_language.h"
  44. #include "version.h"
  45. #include "thirdparty/misc/md5.h"
  46. String EditorImportPlugin::validate_source_path(const String &p_path) {
  47. String gp = Globals::get_singleton()->globalize_path(p_path);
  48. String rp = Globals::get_singleton()->get_resource_path();
  49. if (!rp.ends_with("/"))
  50. rp += "/";
  51. return rp.path_to_file(gp);
  52. }
  53. String EditorImportPlugin::expand_source_path(const String &p_path) {
  54. if (p_path.is_rel_path()) {
  55. return Globals::get_singleton()->get_resource_path().plus_file(p_path).simplify_path();
  56. } else {
  57. return p_path;
  58. }
  59. }
  60. String EditorImportPlugin::_validate_source_path(const String &p_path) {
  61. return validate_source_path(p_path);
  62. }
  63. String EditorImportPlugin::_expand_source_path(const String &p_path) {
  64. return expand_source_path(p_path);
  65. }
  66. void EditorImportPlugin::_bind_methods() {
  67. ObjectTypeDB::bind_method(_MD("validate_source_path", "path"), &EditorImportPlugin::_validate_source_path);
  68. ObjectTypeDB::bind_method(_MD("expand_source_path", "path"), &EditorImportPlugin::_expand_source_path);
  69. ObjectTypeDB::add_virtual_method(get_type_static(), MethodInfo(Variant::STRING, "get_name"));
  70. ObjectTypeDB::add_virtual_method(get_type_static(), MethodInfo(Variant::STRING, "get_visible_name"));
  71. ObjectTypeDB::add_virtual_method(get_type_static(), MethodInfo("import_dialog", PropertyInfo(Variant::STRING, "from")));
  72. ObjectTypeDB::add_virtual_method(get_type_static(), MethodInfo(Variant::INT, "import", PropertyInfo(Variant::STRING, "path"), PropertyInfo(Variant::OBJECT, "from", PROPERTY_HINT_RESOURCE_TYPE, "ResourceImportMetadata")));
  73. ObjectTypeDB::add_virtual_method(get_type_static(), MethodInfo(Variant::RAW_ARRAY, "custom_export", PropertyInfo(Variant::STRING, "path"), PropertyInfo(Variant::OBJECT, "platform", PROPERTY_HINT_RESOURCE_TYPE, "EditorExportPlatform")));
  74. ObjectTypeDB::add_virtual_method(get_type_static(), MethodInfo("import_from_drop", PropertyInfo(Variant::STRING_ARRAY, "files"), PropertyInfo(Variant::STRING, "dest_path")));
  75. ObjectTypeDB::add_virtual_method(get_type_static(), MethodInfo("reimport_multiple_files", PropertyInfo(Variant::STRING_ARRAY, "files")));
  76. ObjectTypeDB::add_virtual_method(get_type_static(), MethodInfo(Variant::BOOL, "can_reimport_multiple_files"));
  77. // BIND_VMETHOD( mi );
  78. }
  79. String EditorImportPlugin::get_name() const {
  80. if (get_script_instance() && get_script_instance()->has_method("get_name")) {
  81. return get_script_instance()->call("get_name");
  82. }
  83. ERR_FAIL_V("");
  84. }
  85. String EditorImportPlugin::get_visible_name() const {
  86. if (get_script_instance() && get_script_instance()->has_method("get_visible_name")) {
  87. return get_script_instance()->call("get_visible_name");
  88. }
  89. ERR_FAIL_V("");
  90. }
  91. void EditorImportPlugin::import_dialog(const String &p_from) {
  92. if (get_script_instance() && get_script_instance()->has_method("import_dialog")) {
  93. get_script_instance()->call("import_dialog", p_from);
  94. return;
  95. }
  96. ERR_FAIL();
  97. }
  98. Error EditorImportPlugin::import(const String &p_path, const Ref<ResourceImportMetadata> &p_from) {
  99. if (get_script_instance() && get_script_instance()->has_method("import")) {
  100. return Error(get_script_instance()->call("import", p_path, p_from).operator int());
  101. }
  102. ERR_FAIL_V(ERR_UNAVAILABLE);
  103. }
  104. Vector<uint8_t> EditorImportPlugin::custom_export(const String &p_path, const Ref<EditorExportPlatform> &p_platform) {
  105. if (get_script_instance() && get_script_instance()->has_method("custom_export")) {
  106. get_script_instance()->call("custom_export", p_path, p_platform);
  107. }
  108. return Vector<uint8_t>();
  109. }
  110. bool EditorImportPlugin::can_reimport_multiple_files() const {
  111. if (get_script_instance() && get_script_instance()->has_method("can_reimport_multiple_files")) {
  112. return get_script_instance()->call("can_reimport_multiple_files");
  113. }
  114. return false;
  115. }
  116. void EditorImportPlugin::reimport_multiple_files(const Vector<String> &p_list) {
  117. if (get_script_instance() && get_script_instance()->has_method("reimport_multiple_files")) {
  118. get_script_instance()->call("reimport_multiple_files", p_list);
  119. }
  120. }
  121. void EditorImportPlugin::import_from_drop(const Vector<String> &p_drop, const String &p_dest_path) {
  122. if (get_script_instance() && get_script_instance()->has_method("import_from_drop")) {
  123. get_script_instance()->call("import_from_drop", p_drop, p_dest_path);
  124. }
  125. }
  126. EditorImportPlugin::EditorImportPlugin() {
  127. }
  128. /////////////////////////////////////////////////////////////////////////////////////////////////////
  129. void EditorExportPlugin::_bind_methods() {
  130. MethodInfo mi = MethodInfo("custom_export:Variant", PropertyInfo(Variant::STRING, "name"), PropertyInfo(Variant::OBJECT, "platform", PROPERTY_HINT_RESOURCE_TYPE, "EditorExportPlatform"));
  131. mi.return_val.type = Variant::RAW_ARRAY;
  132. BIND_VMETHOD(mi);
  133. }
  134. Vector<uint8_t> EditorExportPlugin::custom_export(String &p_path, const Ref<EditorExportPlatform> &p_platform) {
  135. if (get_script_instance()) {
  136. Variant d = get_script_instance()->call("custom_export", p_path, p_platform);
  137. if (d.get_type() == Variant::NIL)
  138. return Vector<uint8_t>();
  139. if (d.get_type() == Variant::RAW_ARRAY)
  140. return d;
  141. ERR_FAIL_COND_V(d.get_type() != Variant::DICTIONARY, Vector<uint8_t>());
  142. Dictionary dict = d;
  143. ERR_FAIL_COND_V(!dict.has("name"), Vector<uint8_t>());
  144. ERR_FAIL_COND_V(!dict.has("data"), Vector<uint8_t>());
  145. p_path = dict["name"];
  146. return dict["data"];
  147. }
  148. return Vector<uint8_t>();
  149. }
  150. EditorExportPlugin::EditorExportPlugin() {
  151. }
  152. /////////////////////////////////////////////////////////////////////////////////////////////////////
  153. static void _split_by_comma(List<String> &p_list, const String &p_src) {
  154. if (p_src == "")
  155. return;
  156. Vector<String> split = p_src.split(",");
  157. for (int i = 0; i < split.size(); i++) {
  158. String f = split[i].strip_edges();
  159. if (f.empty())
  160. continue;
  161. p_list.push_back(f);
  162. }
  163. }
  164. static void _add_to_list(EditorFileSystemDirectory *p_efsd, Set<StringName> &r_list, const String &p_filter) {
  165. List<String> filters;
  166. _split_by_comma(filters, p_filter);
  167. for (int i = 0; i < p_efsd->get_subdir_count(); i++) {
  168. if (p_filter == "" || filters.find(p_efsd->get_subdir(i)->get_name()) == NULL)
  169. _add_to_list(p_efsd->get_subdir(i), r_list, p_filter);
  170. else
  171. print_line("Folder ignored by settings: " + p_efsd->get_subdir(i)->get_name());
  172. }
  173. for (int i = 0; i < p_efsd->get_file_count(); i++) {
  174. r_list.insert(p_efsd->get_file_path(i));
  175. }
  176. }
  177. struct __EESortDepCmp {
  178. _FORCE_INLINE_ bool operator()(const StringName &p_l, const StringName &p_r) const {
  179. return p_l.operator String() < p_r.operator String();
  180. }
  181. };
  182. static void _edit_files_with_filter(DirAccess *da, const List<String> &p_filters, Set<StringName> &r_list, bool exclude) {
  183. List<String> files;
  184. List<String> dirs;
  185. da->list_dir_begin();
  186. String f = da->get_next();
  187. while (f != "") {
  188. if (da->current_is_dir())
  189. dirs.push_back(f);
  190. else
  191. files.push_back(f);
  192. f = da->get_next();
  193. }
  194. String r = da->get_current_dir().replace("\\", "/");
  195. if (!r.ends_with("/"))
  196. r += "/";
  197. for (List<String>::Element *E = files.front(); E; E = E->next()) {
  198. String fullpath = r + E->get();
  199. for (const List<String>::Element *F = p_filters.front(); F; F = F->next()) {
  200. if (fullpath.matchn(F->get())) {
  201. String act = TTR("Added:") + " ";
  202. if (!exclude) {
  203. r_list.insert(fullpath);
  204. } else {
  205. act = TTR("Removed:") + " ";
  206. r_list.erase(fullpath);
  207. }
  208. print_line(act + fullpath);
  209. }
  210. }
  211. }
  212. da->list_dir_end();
  213. for (List<String>::Element *E = dirs.front(); E; E = E->next()) {
  214. if (E->get().begins_with("."))
  215. continue;
  216. da->change_dir(E->get());
  217. _edit_files_with_filter(da, p_filters, r_list, exclude);
  218. da->change_dir("..");
  219. }
  220. }
  221. static void _edit_filter_list(Set<StringName> &r_list, const String &p_filter, bool exclude) {
  222. if (p_filter == "")
  223. return;
  224. List<String> filters;
  225. _split_by_comma(filters, p_filter);
  226. DirAccess *da = DirAccess::open("res://");
  227. ERR_FAIL_NULL(da);
  228. _edit_files_with_filter(da, filters, r_list, exclude);
  229. memdelete(da);
  230. }
  231. static void _add_filter_to_list(Set<StringName> &r_list, const String &p_filter) {
  232. _edit_filter_list(r_list, p_filter, false);
  233. }
  234. static void _remove_filter_from_list(Set<StringName> &r_list, const String &p_filter) {
  235. _edit_filter_list(r_list, p_filter, true);
  236. }
  237. bool EditorExportPlatform::_set(const StringName &p_name, const Variant &p_value) {
  238. String n = p_name;
  239. if (n == "debug/debugging_enabled") {
  240. set_debugging_enabled(p_value);
  241. } else {
  242. return false;
  243. }
  244. return true;
  245. }
  246. bool EditorExportPlatform::_get(const StringName &p_name, Variant &r_ret) const {
  247. String n = p_name;
  248. if (n == "debug/debugging_enabled") {
  249. r_ret = is_debugging_enabled();
  250. } else {
  251. return false;
  252. }
  253. return true;
  254. }
  255. void EditorExportPlatform::_get_property_list(List<PropertyInfo> *p_list) const {
  256. p_list->push_front(PropertyInfo(Variant::BOOL, "debug/debugging_enabled"));
  257. }
  258. Vector<uint8_t> EditorExportPlatform::get_exported_file_default(String &p_fname) const {
  259. FileAccess *f = FileAccess::open(p_fname, FileAccess::READ);
  260. ERR_FAIL_COND_V(!f, Vector<uint8_t>());
  261. Vector<uint8_t> ret;
  262. ret.resize(f->get_len());
  263. int rbs = f->get_buffer(ret.ptr(), ret.size());
  264. memdelete(f);
  265. return ret;
  266. }
  267. Vector<uint8_t> EditorExportPlatform::get_exported_file(String &p_fname) const {
  268. Ref<EditorExportPlatform> ep = EditorImportExport::get_singleton()->get_export_platform(get_name());
  269. for (int i = 0; i < EditorImportExport::get_singleton()->get_export_plugin_count(); i++) {
  270. Vector<uint8_t> data = EditorImportExport::get_singleton()->get_export_plugin(i)->custom_export(p_fname, ep);
  271. if (data.size())
  272. return data;
  273. }
  274. return get_exported_file_default(p_fname);
  275. }
  276. Vector<StringName> EditorExportPlatform::get_dependencies(bool p_bundles) const {
  277. Set<StringName> exported;
  278. if (FileAccess::exists("res://engine.cfg"))
  279. exported.insert("res://engine.cfg");
  280. if (EditorImportExport::get_singleton()->get_export_filter() != EditorImportExport::EXPORT_SELECTED) {
  281. String filter;
  282. if (EditorImportExport::get_singleton()->get_export_filter() == EditorImportExport::EXPORT_ALL) {
  283. _add_filter_to_list(exported, "*");
  284. } else {
  285. String cf = EditorImportExport::get_singleton()->get_export_custom_filter_exclude_dir();
  286. _add_to_list(EditorFileSystem::get_singleton()->get_filesystem(), exported, cf);
  287. cf = EditorImportExport::get_singleton()->get_export_custom_filter();
  288. if (cf != "")
  289. cf += ",";
  290. cf += "*.flags";
  291. _add_filter_to_list(exported, cf);
  292. cf = EditorImportExport::get_singleton()->get_export_custom_filter_exclude();
  293. _remove_filter_from_list(exported, cf);
  294. }
  295. } else {
  296. Map<String, Map<String, String> > remapped_paths;
  297. Set<String> scene_extensions;
  298. Set<String> resource_extensions;
  299. {
  300. List<String> l;
  301. // SceneLoader::get_recognized_extensions(&l);
  302. // for(List<String>::Element *E=l.front();E;E=E->next()) {
  303. //
  304. // scene_extensions.insert(E->get());
  305. // }
  306. ResourceLoader::get_recognized_extensions_for_type("", &l);
  307. for (List<String>::Element *E = l.front(); E; E = E->next()) {
  308. resource_extensions.insert(E->get());
  309. }
  310. }
  311. List<StringName> toexport;
  312. EditorImportExport::get_singleton()->get_export_file_list(&toexport);
  313. print_line("TO EXPORT: " + itos(toexport.size()));
  314. for (List<StringName>::Element *E = toexport.front(); E; E = E->next()) {
  315. print_line("DEP: " + String(E->get()));
  316. exported.insert(E->get());
  317. if (p_bundles && EditorImportExport::get_singleton()->get_export_file_action(E->get()) == EditorImportExport::ACTION_BUNDLE) {
  318. print_line("NO BECAUSE OF BUNDLE!");
  319. continue; //no dependencies needed to be copied
  320. }
  321. List<String> testsubs;
  322. testsubs.push_back(E->get());
  323. while (testsubs.size()) {
  324. //recursive subdep search!
  325. List<String> deplist;
  326. ResourceLoader::get_dependencies(testsubs.front()->get(), &deplist);
  327. testsubs.pop_front();
  328. List<String> subdeps;
  329. for (List<String>::Element *F = deplist.front(); F; F = F->next()) {
  330. StringName dep = F->get();
  331. if (exported.has(dep) || EditorImportExport::get_singleton()->get_export_file_action(dep) != EditorImportExport::ACTION_NONE)
  332. continue; //dependency added or to be added
  333. print_line(" SUBDEP: " + String(dep));
  334. exported.insert(dep);
  335. testsubs.push_back(dep);
  336. }
  337. }
  338. }
  339. String cf = EditorImportExport::get_singleton()->get_export_custom_filter();
  340. if (cf != "")
  341. cf += ",";
  342. cf += "*.flags";
  343. _add_filter_to_list(exported, cf);
  344. cf = EditorImportExport::get_singleton()->get_export_custom_filter_exclude();
  345. _remove_filter_from_list(exported, cf);
  346. }
  347. Vector<StringName> ret;
  348. ret.resize(exported.size());
  349. int idx = 0;
  350. for (Set<StringName>::Element *E = exported.front(); E; E = E->next()) {
  351. ret[idx++] = E->get();
  352. }
  353. SortArray<StringName, __EESortDepCmp> sort; //some platforms work better if this is sorted
  354. sort.sort(ret.ptr(), ret.size());
  355. return ret;
  356. }
  357. String EditorExportPlatform::find_export_template(String template_file_name, String *err) const {
  358. String user_file = EditorSettings::get_singleton()->get_settings_path() + "/templates/" + template_file_name;
  359. String system_file = OS::get_singleton()->get_installed_templates_path();
  360. bool has_system_path = (system_file != "");
  361. system_file += template_file_name;
  362. // Prefer user file
  363. if (FileAccess::exists(user_file)) {
  364. return user_file;
  365. }
  366. // Now check system file
  367. if (has_system_path) {
  368. if (FileAccess::exists(system_file)) {
  369. return system_file;
  370. }
  371. }
  372. // Not found
  373. if (err) {
  374. *err += "No export template found at \"" + user_file + "\"";
  375. if (has_system_path)
  376. *err += "\n or \"" + system_file + "\".";
  377. else
  378. *err += ".";
  379. }
  380. return "";
  381. }
  382. bool EditorExportPlatform::exists_export_template(String template_file_name, String *err) const {
  383. return find_export_template(template_file_name, err) != "";
  384. }
  385. ///////////////////////////////////////
  386. bool EditorExportPlatform::is_debugging_enabled() const {
  387. return debugging_enabled;
  388. }
  389. void EditorExportPlatform::set_debugging_enabled(bool p_enabled) {
  390. debugging_enabled = p_enabled;
  391. }
  392. int EditorExportPlatform::get_chmod_flags() const {
  393. return chmod_flags;
  394. }
  395. void EditorExportPlatform::set_chmod_flags(int p_flags) {
  396. chmod_flags = p_flags;
  397. }
  398. bool EditorExportPlatformPC::_set(const StringName &p_name, const Variant &p_value) {
  399. String n = p_name;
  400. if (n == "custom_binary/release") {
  401. custom_release_binary = p_value;
  402. } else if (n == "custom_binary/debug") {
  403. custom_debug_binary = p_value;
  404. } else if (n == "resources/pack_mode") {
  405. export_mode = ExportMode(int(p_value));
  406. } else if (n == "resources/bundle_dependencies_(for_optical_disc)") {
  407. bundle = p_value;
  408. } else if (n == "binary/64_bits") {
  409. use64 = p_value;
  410. } else
  411. return false;
  412. return true;
  413. }
  414. bool EditorExportPlatformPC::_get(const StringName &p_name, Variant &r_ret) const {
  415. String n = p_name;
  416. if (n == "custom_binary/release") {
  417. r_ret = custom_release_binary;
  418. } else if (n == "custom_binary/debug") {
  419. r_ret = custom_debug_binary;
  420. } else if (n == "resources/pack_mode") {
  421. r_ret = export_mode;
  422. } else if (n == "resources/bundle_dependencies_(for_optical_disc)") {
  423. r_ret = bundle;
  424. } else if (n == "binary/64_bits") {
  425. r_ret = use64;
  426. } else
  427. return false;
  428. return true;
  429. }
  430. void EditorExportPlatformPC::_get_property_list(List<PropertyInfo> *p_list) const {
  431. p_list->push_back(PropertyInfo(Variant::STRING, "custom_binary/debug", PROPERTY_HINT_GLOBAL_FILE, binary_extension));
  432. p_list->push_back(PropertyInfo(Variant::STRING, "custom_binary/release", PROPERTY_HINT_GLOBAL_FILE, binary_extension));
  433. p_list->push_back(PropertyInfo(Variant::INT, "resources/pack_mode", PROPERTY_HINT_ENUM, "Pack into executable,Pack into binary file (.pck),Pack into archive file (.zip)"));
  434. p_list->push_back(PropertyInfo(Variant::BOOL, "resources/bundle_dependencies_(for_optical_disc)"));
  435. p_list->push_back(PropertyInfo(Variant::BOOL, "binary/64_bits"));
  436. }
  437. static void _exp_add_dep(Map<StringName, List<StringName> > &deps, const StringName &p_path) {
  438. if (deps.has(p_path))
  439. return; //already done
  440. deps.insert(p_path, List<StringName>());
  441. List<StringName> &deplist = deps[p_path];
  442. Set<StringName> depset;
  443. List<String> dl;
  444. ResourceLoader::get_dependencies(p_path, &dl);
  445. //added in order so child dependencies are always added bfore parent dependencies
  446. for (List<String>::Element *E = dl.front(); E; E = E->next()) {
  447. if (!deps.has(E->get()))
  448. _exp_add_dep(deps, E->get());
  449. for (List<StringName>::Element *F = deps[E->get()].front(); F; F = F->next()) {
  450. if (!depset.has(F->get())) {
  451. depset.insert(F->get());
  452. deplist.push_back(F->get());
  453. }
  454. }
  455. if (!depset.has(E->get())) {
  456. depset.insert(E->get());
  457. deplist.push_back(E->get());
  458. }
  459. }
  460. }
  461. Error EditorExportPlatform::export_project_files(EditorExportSaveFunction p_func, void *p_udata, bool p_make_bundles) {
  462. /* ALL FILES AND DEPENDENCIES */
  463. Vector<StringName> files = get_dependencies(p_make_bundles);
  464. Map<StringName, List<StringName> > deps;
  465. if (false) {
  466. for (int i = 0; i < files.size(); i++) {
  467. _exp_add_dep(deps, files[i]);
  468. }
  469. }
  470. /* GROUP ATLAS */
  471. List<StringName> groups;
  472. EditorImportExport::get_singleton()->image_export_get_groups(&groups);
  473. Map<StringName, StringName> remap_files;
  474. Set<StringName> saved;
  475. int counter = 0;
  476. for (List<StringName>::Element *E = groups.front(); E; E = E->next()) {
  477. if (!EditorImportExport::get_singleton()->image_export_group_get_make_atlas(E->get()))
  478. continue; //uninterested, only process for atlas!
  479. List<StringName> atlas_images;
  480. EditorImportExport::get_singleton()->image_export_get_images_in_group(E->get(), &atlas_images);
  481. atlas_images.sort_custom<StringName::AlphCompare>();
  482. for (List<StringName>::Element *F = atlas_images.front(); F;) {
  483. List<StringName>::Element *N = F->next();
  484. if (!FileAccess::exists(F->get())) {
  485. atlas_images.erase(F);
  486. }
  487. F = N;
  488. }
  489. if (atlas_images.size() <= 1)
  490. continue;
  491. int group_format = 0;
  492. float group_lossy_quality = EditorImportExport::get_singleton()->image_export_group_get_lossy_quality(E->get());
  493. int group_shrink = EditorImportExport::get_singleton()->image_export_group_get_shrink(E->get());
  494. group_shrink *= EditorImportExport::get_singleton()->get_export_image_shrink();
  495. switch (EditorImportExport::get_singleton()->image_export_group_get_image_action(E->get())) {
  496. case EditorImportExport::IMAGE_ACTION_KEEP:
  497. case EditorImportExport::IMAGE_ACTION_NONE: {
  498. switch (EditorImportExport::get_singleton()->get_export_image_action()) {
  499. case EditorImportExport::IMAGE_ACTION_NONE: {
  500. group_format = EditorTextureImportPlugin::IMAGE_FORMAT_COMPRESS_DISK_LOSSLESS; //?
  501. } break; //use default
  502. case EditorImportExport::IMAGE_ACTION_COMPRESS_DISK: {
  503. group_format = EditorTextureImportPlugin::IMAGE_FORMAT_COMPRESS_DISK_LOSSY;
  504. } break; //use default
  505. case EditorImportExport::IMAGE_ACTION_COMPRESS_RAM: {
  506. group_format = EditorTextureImportPlugin::IMAGE_FORMAT_COMPRESS_RAM;
  507. } break; //use default
  508. }
  509. group_lossy_quality = EditorImportExport::get_singleton()->get_export_image_quality();
  510. } break; //use default
  511. case EditorImportExport::IMAGE_ACTION_COMPRESS_DISK: {
  512. group_format = EditorTextureImportPlugin::IMAGE_FORMAT_COMPRESS_DISK_LOSSY;
  513. } break; //use default
  514. case EditorImportExport::IMAGE_ACTION_COMPRESS_RAM: {
  515. group_format = EditorTextureImportPlugin::IMAGE_FORMAT_COMPRESS_RAM;
  516. } break; //use default
  517. }
  518. String image_list_md5;
  519. {
  520. MD5_CTX ctx;
  521. MD5Init(&ctx);
  522. for (List<StringName>::Element *F = atlas_images.front(); F; F = F->next()) {
  523. String p = F->get();
  524. MD5Update(&ctx, (unsigned char *)p.utf8().get_data(), p.utf8().length());
  525. }
  526. MD5Final(&ctx);
  527. image_list_md5 = String::md5(ctx.digest);
  528. }
  529. //ok see if cached
  530. String md5;
  531. bool atlas_valid = true;
  532. String atlas_name;
  533. {
  534. MD5_CTX ctx;
  535. MD5Init(&ctx);
  536. String path = Globals::get_singleton()->get_resource_path() + "::" + String(E->get()) + "::" + get_name();
  537. MD5Update(&ctx, (unsigned char *)path.utf8().get_data(), path.utf8().length());
  538. MD5Final(&ctx);
  539. md5 = String::md5(ctx.digest);
  540. }
  541. FileAccess *f = NULL;
  542. if (!FileAccess::exists(EditorSettings::get_singleton()->get_settings_path() + "/tmp/atlas-" + md5)) {
  543. print_line("NO MD5 INVALID");
  544. atlas_valid = false;
  545. }
  546. if (atlas_valid)
  547. f = FileAccess::open(EditorSettings::get_singleton()->get_settings_path() + "/tmp/atlas-" + md5, FileAccess::READ);
  548. if (atlas_valid) {
  549. //compare options
  550. Dictionary options;
  551. options.parse_json(f->get_line());
  552. if (!options.has("lossy_quality") || float(options["lossy_quality"]) != group_lossy_quality)
  553. atlas_valid = false;
  554. else if (!options.has("shrink") || int(options["shrink"]) != group_shrink)
  555. atlas_valid = false;
  556. else if (!options.has("image_format") || int(options["image_format"]) != group_format)
  557. atlas_valid = false;
  558. if (!atlas_valid)
  559. print_line("JSON INVALID");
  560. }
  561. if (atlas_valid) {
  562. //check md5 of list of image /names/
  563. if (f->get_line().strip_edges() != image_list_md5) {
  564. atlas_valid = false;
  565. print_line("IMAGE MD5 INVALID!");
  566. }
  567. }
  568. Vector<Rect2> rects;
  569. bool resave_deps = false;
  570. if (atlas_valid) {
  571. //check if images were not modified
  572. for (List<StringName>::Element *F = atlas_images.front(); F; F = F->next()) {
  573. Vector<String> slices = f->get_line().strip_edges().split("::");
  574. if (slices.size() != 10) {
  575. atlas_valid = false;
  576. print_line("CANT SLICE IN 10");
  577. break;
  578. }
  579. uint64_t mod_time = slices[0].to_int64();
  580. uint64_t file_mod_time = FileAccess::get_modified_time(F->get());
  581. if (mod_time != file_mod_time) {
  582. String image_md5 = slices[1];
  583. String file_md5 = FileAccess::get_md5(F->get());
  584. if (image_md5 != file_md5) {
  585. atlas_valid = false;
  586. print_line("IMAGE INVALID " + slices[0]);
  587. break;
  588. } else {
  589. resave_deps = true;
  590. }
  591. }
  592. if (atlas_valid) {
  593. //push back region and margin
  594. rects.push_back(Rect2(slices[2].to_float(), slices[3].to_float(), slices[4].to_float(), slices[5].to_float()));
  595. rects.push_back(Rect2(slices[6].to_float(), slices[7].to_float(), slices[8].to_float(), slices[9].to_float()));
  596. }
  597. }
  598. }
  599. if (f) {
  600. memdelete(f);
  601. f = NULL;
  602. }
  603. print_line("ATLAS VALID? " + itos(atlas_valid) + " RESAVE DEPS? " + itos(resave_deps));
  604. if (!atlas_valid) {
  605. rects.clear();
  606. //oh well, atlas is not valid. need to make new one....
  607. String dst_file = EditorSettings::get_singleton()->get_settings_path() + "/tmp/atlas-" + md5 + ".tex";
  608. Ref<ResourceImportMetadata> imd = memnew(ResourceImportMetadata);
  609. //imd->set_editor();
  610. for (List<StringName>::Element *F = atlas_images.front(); F; F = F->next()) {
  611. imd->add_source(EditorImportPlugin::validate_source_path(F->get()), FileAccess::get_md5(F->get()));
  612. }
  613. imd->set_option("format", group_format);
  614. int flags = 0;
  615. if (Globals::get_singleton()->get("image_loader/filter"))
  616. flags |= EditorTextureImportPlugin::IMAGE_FLAG_FILTER;
  617. if (!Globals::get_singleton()->get("image_loader/gen_mipmaps"))
  618. flags |= EditorTextureImportPlugin::IMAGE_FLAG_NO_MIPMAPS;
  619. if (Globals::get_singleton()->get("image_loader/repeat"))
  620. flags |= EditorTextureImportPlugin::IMAGE_FLAG_REPEAT;
  621. flags |= EditorTextureImportPlugin::IMAGE_FLAG_FIX_BORDER_ALPHA;
  622. imd->set_option("flags", flags);
  623. imd->set_option("quality", group_lossy_quality);
  624. imd->set_option("atlas", true);
  625. imd->set_option("crop", true);
  626. imd->set_option("shrink", group_shrink);
  627. Ref<EditorTextureImportPlugin> plugin = EditorImportExport::get_singleton()->get_import_plugin_by_name("texture");
  628. Error err = plugin->import2(dst_file, imd, get_image_compression(), true);
  629. if (err) {
  630. EditorNode::add_io_error(TTR("Error saving atlas:") + " " + dst_file.get_file());
  631. return ERR_CANT_CREATE;
  632. }
  633. ERR_FAIL_COND_V(imd->get_option("rects") == Variant(), ERR_BUG);
  634. Array r_rects = imd->get_option("rects");
  635. rects.resize(r_rects.size());
  636. for (int i = 0; i < r_rects.size(); i++) {
  637. //get back region and margins
  638. rects[i] = r_rects[i];
  639. }
  640. resave_deps = true;
  641. }
  642. //atlas is valid (or it was just saved i guess), create the atex files and save them
  643. if (resave_deps) {
  644. f = FileAccess::open(EditorSettings::get_singleton()->get_settings_path() + "/tmp/atlas-" + md5, FileAccess::WRITE);
  645. Dictionary options;
  646. options["lossy_quality"] = group_lossy_quality;
  647. options["shrink"] = EditorImportExport::get_singleton()->image_export_group_get_shrink(E->get());
  648. options["image_format"] = group_format;
  649. f->store_line(options.to_json());
  650. f->store_line(image_list_md5);
  651. }
  652. //go through all ATEX files
  653. {
  654. Ref<ImageTexture> atlas = memnew(ImageTexture); //fake atlas!
  655. String atlas_path = "res://atlas-" + md5 + ".tex";
  656. atlas->set_path(atlas_path);
  657. int idx = 0;
  658. for (List<StringName>::Element *F = atlas_images.front(); F; F = F->next()) {
  659. String p = F->get();
  660. Ref<AtlasTexture> atex = memnew(AtlasTexture);
  661. atex->set_atlas(atlas);
  662. Rect2 region = rects[idx++];
  663. Rect2 margin = rects[idx++];
  664. atex->set_region(region);
  665. atex->set_margin(margin);
  666. String path = EditorSettings::get_singleton()->get_settings_path() + "/tmp/tmpatlas.atex";
  667. Error err = ResourceSaver::save(path, atex);
  668. if (err != OK) {
  669. EditorNode::add_io_error(TTR("Could not save atlas subtexture:") + " " + path);
  670. return ERR_CANT_CREATE;
  671. }
  672. Vector<uint8_t> data = FileAccess::get_file_as_array(path);
  673. String dst_path = F->get().operator String().basename() + ".atex";
  674. err = p_func(p_udata, dst_path, data, counter++, files.size());
  675. saved.insert(dst_path);
  676. if (err)
  677. return err;
  678. if (f) {
  679. //recreating deps..
  680. String depline;
  681. // depline=String(F->get())+"::"+itos(FileAccess::get_modified_time(F->get()))+"::"+FileAccess::get_md5(F->get()); name unneccesary by top md5
  682. depline = itos(FileAccess::get_modified_time(F->get())) + "::" + FileAccess::get_md5(F->get());
  683. depline += "::" + itos(region.pos.x) + "::" + itos(region.pos.y) + "::" + itos(region.size.x) + "::" + itos(region.size.y);
  684. depline += "::" + itos(margin.pos.x) + "::" + itos(margin.pos.y) + "::" + itos(margin.size.x) + "::" + itos(margin.size.y);
  685. f->store_line(depline);
  686. }
  687. remap_files[F->get()] = dst_path;
  688. }
  689. Vector<uint8_t> atlas_data = FileAccess::get_file_as_array(EditorSettings::get_singleton()->get_settings_path() + "/tmp/atlas-" + md5 + ".tex");
  690. Error err = p_func(p_udata, atlas_path, atlas_data, counter, files.size());
  691. saved.insert(atlas_path);
  692. if (err)
  693. return err;
  694. }
  695. if (f) {
  696. memdelete(f);
  697. }
  698. }
  699. StringName engine_cfg = "res://engine.cfg";
  700. StringName boot_splash;
  701. {
  702. String splash = Globals::get_singleton()->get("application/boot_splash"); //avoid splash from being converted
  703. splash = splash.strip_edges();
  704. if (splash != String()) {
  705. if (!splash.begins_with("res://"))
  706. splash = "res://" + splash;
  707. splash = splash.simplify_path();
  708. boot_splash = splash;
  709. }
  710. }
  711. for (int i = 0; i < files.size(); i++) {
  712. if (remap_files.has(files[i]) || files[i] == engine_cfg) //gonna be remapped (happened before!)
  713. continue; //from atlas?
  714. String src = files[i];
  715. Vector<uint8_t> buf;
  716. if (src == boot_splash)
  717. buf = get_exported_file_default(src); //bootsplash must be kept if used
  718. else
  719. buf = get_exported_file(src);
  720. ERR_CONTINUE(saved.has(src));
  721. Error err = p_func(p_udata, src, buf, counter++, files.size());
  722. if (err)
  723. return err;
  724. saved.insert(src);
  725. if (src != String(files[i]))
  726. remap_files[files[i]] = src;
  727. }
  728. {
  729. //make binary engine.cfg config
  730. Map<String, Variant> custom;
  731. if (remap_files.size()) {
  732. Vector<String> remapsprop;
  733. for (Map<StringName, StringName>::Element *E = remap_files.front(); E; E = E->next()) {
  734. remapsprop.push_back(E->key());
  735. remapsprop.push_back(E->get());
  736. }
  737. custom["remap/all"] = remapsprop;
  738. }
  739. //add presaved dependencies
  740. for (Map<StringName, List<StringName> >::Element *E = deps.front(); E; E = E->next()) {
  741. if (E->get().size() == 0)
  742. continue; //no deps
  743. String key;
  744. Vector<StringName> deps;
  745. //if bundle continue (when bundles supported obviously)
  746. if (remap_files.has(E->key())) {
  747. key = remap_files[E->key()];
  748. } else {
  749. key = E->key();
  750. }
  751. deps.resize(E->get().size());
  752. int i = 0;
  753. for (List<StringName>::Element *F = E->get().front(); F; F = F->next()) {
  754. deps[i++] = F->get();
  755. print_line(" -" + String(F->get()));
  756. }
  757. NodePath prop(deps, true, String()); //seems best to use this for performance
  758. custom["deps/" + key.md5_text()] = prop;
  759. }
  760. String remap_file = "engine.cfb";
  761. String engine_cfb = EditorSettings::get_singleton()->get_settings_path() + "/tmp/tmp" + remap_file;
  762. Globals::get_singleton()->save_custom(engine_cfb, custom);
  763. Vector<uint8_t> data = FileAccess::get_file_as_array(engine_cfb);
  764. Error err = p_func(p_udata, "res://" + remap_file, data, counter, files.size());
  765. if (err)
  766. return err;
  767. }
  768. return OK;
  769. }
  770. static int _get_pad(int p_alignment, int p_n) {
  771. int rest = p_n % p_alignment;
  772. int pad = 0;
  773. if (rest > 0) {
  774. pad = p_alignment - rest;
  775. };
  776. return pad;
  777. };
  778. void EditorExportPlatform::gen_export_flags(Vector<String> &r_flags, int p_flags) {
  779. String host = EditorSettings::get_singleton()->get("network/debug_host");
  780. int remote_port = (int)EditorSettings::get_singleton()->get("network/debug_port");
  781. if (p_flags & EXPORT_REMOTE_DEBUG_LOCALHOST)
  782. host = "localhost";
  783. if (p_flags & EXPORT_DUMB_CLIENT) {
  784. int port = EditorSettings::get_singleton()->get("file_server/port");
  785. String passwd = EditorSettings::get_singleton()->get("file_server/password");
  786. r_flags.push_back("-rfs");
  787. r_flags.push_back(host + ":" + itos(port));
  788. if (passwd != "") {
  789. r_flags.push_back("-rfs_pass");
  790. r_flags.push_back(passwd);
  791. }
  792. }
  793. if (p_flags & EXPORT_REMOTE_DEBUG) {
  794. r_flags.push_back("-rdebug");
  795. r_flags.push_back(host + ":" + String::num(remote_port));
  796. List<String> breakpoints;
  797. ScriptEditor::get_singleton()->get_breakpoints(&breakpoints);
  798. if (breakpoints.size()) {
  799. r_flags.push_back("-bp");
  800. String bpoints;
  801. for (const List<String>::Element *E = breakpoints.front(); E; E = E->next()) {
  802. bpoints += E->get().replace(" ", "%20");
  803. if (E->next())
  804. bpoints += ",";
  805. }
  806. r_flags.push_back(bpoints);
  807. }
  808. }
  809. if (p_flags & EXPORT_VIEW_COLLISONS) {
  810. r_flags.push_back("-debugcol");
  811. }
  812. if (p_flags & EXPORT_VIEW_NAVIGATION) {
  813. r_flags.push_back("-debugnav");
  814. }
  815. }
  816. Error EditorExportPlatform::save_pack_file(void *p_userdata, const String &p_path, const Vector<uint8_t> &p_data, int p_file, int p_total) {
  817. PackData *pd = (PackData *)p_userdata;
  818. CharString cs = p_path.utf8();
  819. pd->f->store_32(cs.length());
  820. pd->f->store_buffer((uint8_t *)cs.get_data(), cs.length());
  821. TempData td;
  822. td.pos = pd->f->get_pos();
  823. td.ofs = pd->ftmp->get_pos();
  824. td.size = p_data.size();
  825. pd->file_ofs.push_back(td);
  826. pd->f->store_64(0); //ofs
  827. pd->f->store_64(0); //size
  828. {
  829. MD5_CTX ctx;
  830. MD5Init(&ctx);
  831. MD5Update(&ctx, (unsigned char *)p_data.ptr(), p_data.size());
  832. MD5Final(&ctx);
  833. pd->f->store_buffer(ctx.digest, 16);
  834. }
  835. pd->ep->step(TTR("Storing File:") + " " + p_path, 2 + p_file * 100 / p_total, false);
  836. pd->count++;
  837. pd->ftmp->store_buffer(p_data.ptr(), p_data.size());
  838. if (pd->alignment > 1) {
  839. int pad = _get_pad(pd->alignment, pd->ftmp->get_pos());
  840. for (int i = 0; i < pad; i++) {
  841. pd->ftmp->store_8(0);
  842. };
  843. };
  844. return OK;
  845. }
  846. Error EditorExportPlatform::save_zip_file(void *p_userdata, const String &p_path, const Vector<uint8_t> &p_data, int p_file, int p_total) {
  847. String path = p_path.replace_first("res://", "");
  848. ZipData *zd = (ZipData *)p_userdata;
  849. zipFile zip = (zipFile)zd->zip;
  850. zipOpenNewFileInZip(zip,
  851. path.utf8().get_data(),
  852. NULL,
  853. NULL,
  854. 0,
  855. NULL,
  856. 0,
  857. NULL,
  858. Z_DEFLATED,
  859. Z_DEFAULT_COMPRESSION);
  860. zipWriteInFileInZip(zip, p_data.ptr(), p_data.size());
  861. zipCloseFileInZip(zip);
  862. zd->ep->step(TTR("Storing File:") + " " + p_path, 2 + p_file * 100 / p_total, false);
  863. zd->count++;
  864. return OK;
  865. }
  866. Error EditorExportPlatform::save_zip(const String &p_path, bool p_make_bundles) {
  867. EditorProgress ep("savezip", TTR("Packing"), 102);
  868. //FileAccess *tmp = FileAccess::open(tmppath,FileAccess::WRITE);
  869. FileAccess *src_f;
  870. zlib_filefunc_def io = zipio_create_io_from_file(&src_f);
  871. zipFile zip = zipOpen2(p_path.utf8().get_data(), APPEND_STATUS_CREATE, NULL, &io);
  872. ZipData zd;
  873. zd.count = 0;
  874. zd.ep = &ep;
  875. zd.zip = zip;
  876. Error err = export_project_files(save_zip_file, &zd, p_make_bundles);
  877. zipClose(zip, NULL);
  878. return err;
  879. }
  880. Error EditorExportPlatform::save_pack(FileAccess *dst, bool p_make_bundles, int p_alignment) {
  881. EditorProgress ep("savepack", TTR("Packing"), 102);
  882. String tmppath = EditorSettings::get_singleton()->get_settings_path() + "/tmp/packtmp";
  883. FileAccess *tmp = FileAccess::open(tmppath, FileAccess::WRITE);
  884. uint64_t ofs_begin = dst->get_pos();
  885. dst->store_32(0x43504447); //GDPK
  886. dst->store_32(0); //pack version
  887. dst->store_32(VERSION_MAJOR);
  888. dst->store_32(VERSION_MINOR);
  889. dst->store_32(0); //hmph
  890. for (int i = 0; i < 16; i++) {
  891. //reserved
  892. dst->store_32(0);
  893. }
  894. size_t fcountpos = dst->get_pos();
  895. dst->store_32(0);
  896. PackData pd;
  897. pd.ep = &ep;
  898. pd.f = dst;
  899. pd.ftmp = tmp;
  900. pd.count = 0;
  901. pd.alignment = p_alignment;
  902. Error err = export_project_files(save_pack_file, &pd, p_make_bundles);
  903. memdelete(tmp);
  904. if (err)
  905. return err;
  906. if (p_alignment > 1) {
  907. int pad = _get_pad(p_alignment, dst->get_pos());
  908. for (int i = 0; i < pad; i++) {
  909. dst->store_8(0);
  910. };
  911. };
  912. size_t ofsplus = dst->get_pos();
  913. //append file
  914. tmp = FileAccess::open(tmppath, FileAccess::READ);
  915. ERR_FAIL_COND_V(!tmp, ERR_CANT_OPEN;)
  916. const int bufsize = 16384;
  917. uint8_t buf[bufsize];
  918. while (true) {
  919. int got = tmp->get_buffer(buf, bufsize);
  920. if (got <= 0)
  921. break;
  922. dst->store_buffer(buf, got);
  923. }
  924. memdelete(tmp);
  925. dst->store_64(dst->get_pos() - ofs_begin);
  926. dst->store_32(0x43504447); //GDPK
  927. //fix offsets
  928. dst->seek(fcountpos);
  929. dst->store_32(pd.count);
  930. for (int i = 0; i < pd.file_ofs.size(); i++) {
  931. dst->seek(pd.file_ofs[i].pos);
  932. dst->store_64(pd.file_ofs[i].ofs + ofsplus);
  933. dst->store_64(pd.file_ofs[i].size);
  934. }
  935. return OK;
  936. }
  937. EditorExportPlatform::EditorExportPlatform() {
  938. debugging_enabled = true;
  939. chmod_flags = 0;
  940. }
  941. Error EditorExportPlatformPC::export_project(const String &p_path, bool p_debug, int p_flags) {
  942. EditorProgress ep("export", vformat(TTR("Exporting for %s"), get_name()), 102);
  943. const int BUFSIZE = 32768;
  944. ep.step(TTR("Setting Up.."), 0);
  945. String exe_path = "";
  946. if (p_debug)
  947. exe_path = custom_debug_binary;
  948. else
  949. exe_path = custom_release_binary;
  950. if (exe_path == "") {
  951. String fname;
  952. if (use64) {
  953. if (p_debug)
  954. fname = debug_binary64;
  955. else
  956. fname = release_binary64;
  957. } else {
  958. if (p_debug)
  959. fname = debug_binary32;
  960. else
  961. fname = release_binary32;
  962. }
  963. String err = "";
  964. exe_path = find_export_template(fname, &err);
  965. if (exe_path == "") {
  966. EditorNode::add_io_error(err);
  967. return ERR_FILE_CANT_READ;
  968. }
  969. }
  970. FileAccess *src_exe = FileAccess::open(exe_path, FileAccess::READ);
  971. if (!src_exe) {
  972. EditorNode::add_io_error("Couldn't read source executable at:\n " + exe_path);
  973. return ERR_FILE_CANT_READ;
  974. }
  975. FileAccess *dst = FileAccess::open(p_path, FileAccess::WRITE);
  976. if (!dst) {
  977. EditorNode::add_io_error("Can't copy executable file to:\n " + p_path);
  978. return ERR_FILE_CANT_WRITE;
  979. }
  980. uint8_t buff[32768];
  981. while (true) {
  982. int c = src_exe->get_buffer(buff, BUFSIZE);
  983. if (c > 0) {
  984. dst->store_buffer(buff, c);
  985. } else {
  986. break;
  987. }
  988. }
  989. String dstfile = p_path.replace_first("res://", "").replace("\\", "/");
  990. if (export_mode != EXPORT_EXE) {
  991. String dstfile_extension = export_mode == EXPORT_ZIP ? ".zip" : ".pck";
  992. if (dstfile.find("/") != -1)
  993. dstfile = dstfile.get_base_dir() + "/data" + dstfile_extension;
  994. else
  995. dstfile = "data" + dstfile_extension;
  996. if (export_mode == EXPORT_PACK) {
  997. memdelete(dst);
  998. dst = FileAccess::open(dstfile, FileAccess::WRITE);
  999. if (!dst) {
  1000. EditorNode::add_io_error("Can't write data pack to:\n " + p_path);
  1001. return ERR_FILE_CANT_WRITE;
  1002. }
  1003. }
  1004. }
  1005. memdelete(src_exe);
  1006. Error err = export_mode == EXPORT_ZIP ? save_zip(dstfile, bundle) : save_pack(dst, bundle);
  1007. dst->close();
  1008. if (err == OK) {
  1009. int flags = get_chmod_flags();
  1010. if (flags) {
  1011. err = dst->_chmod(p_path, flags);
  1012. // If exporting from a platform with no chmod support (i.e., Windows), don't fail
  1013. if (err == ERR_UNAVAILABLE)
  1014. err = OK;
  1015. }
  1016. }
  1017. memdelete(dst);
  1018. return err;
  1019. }
  1020. void EditorExportPlatformPC::set_binary_extension(const String &p_extension) {
  1021. binary_extension = p_extension;
  1022. }
  1023. bool EditorExportPlatformPC::can_export(String *r_error) const {
  1024. String err;
  1025. bool valid = true;
  1026. if (use64 && (!exists_export_template(debug_binary64)) || !exists_export_template(release_binary64)) {
  1027. valid = false;
  1028. err = "No 64 bits export templates found.\nDownload and install export templates.\n";
  1029. }
  1030. if (!use64 && (!exists_export_template(debug_binary32) || !exists_export_template(release_binary32))) {
  1031. valid = false;
  1032. err = "No 32 bits export templates found.\nDownload and install export templates.\n";
  1033. }
  1034. if (custom_debug_binary == "" && custom_release_binary == "") {
  1035. if (r_error) *r_error = err;
  1036. return valid;
  1037. }
  1038. bool dvalid = true;
  1039. bool rvalid = true;
  1040. if (!FileAccess::exists(custom_debug_binary)) {
  1041. dvalid = false;
  1042. err = "Custom debug binary not found.\n";
  1043. }
  1044. if (!FileAccess::exists(custom_release_binary)) {
  1045. rvalid = false;
  1046. err = "Custom release binary not found.\n";
  1047. }
  1048. if (dvalid || rvalid)
  1049. valid = true;
  1050. else
  1051. valid = false;
  1052. if (r_error)
  1053. *r_error = err;
  1054. return valid;
  1055. }
  1056. EditorExportPlatformPC::EditorExportPlatformPC() {
  1057. export_mode = EXPORT_PACK;
  1058. use64 = true;
  1059. }
  1060. ///////////////////////////////////////////////////////////////////////////////////////////////////////
  1061. EditorImportExport *EditorImportExport::singleton = NULL;
  1062. void EditorImportExport::add_import_plugin(const Ref<EditorImportPlugin> &p_plugin) {
  1063. // Need to make sure the name is unique if we are going to lookup by it
  1064. ERR_FAIL_COND(by_idx.has(p_plugin->get_name()));
  1065. by_idx[p_plugin->get_name()] = plugins.size();
  1066. plugins.push_back(p_plugin);
  1067. }
  1068. void EditorImportExport::remove_import_plugin(const Ref<EditorImportPlugin> &p_plugin) {
  1069. String plugin_name = p_plugin->get_name();
  1070. // Keep the indices the same
  1071. // Find the index of the target plugin
  1072. ERR_FAIL_COND(!by_idx.has(plugin_name));
  1073. int idx = by_idx[plugin_name];
  1074. int last_idx = plugins.size() - 1;
  1075. // Swap the last plugin and the target one
  1076. SWAP(plugins[idx], plugins[last_idx]);
  1077. // Update the index of the old last one
  1078. by_idx[plugins[idx]->get_name()] = idx;
  1079. // Remove the target plugin's by_idx entry
  1080. by_idx.erase(plugin_name);
  1081. // Erase the plugin
  1082. plugins.remove(last_idx);
  1083. }
  1084. int EditorImportExport::get_import_plugin_count() const {
  1085. return plugins.size();
  1086. }
  1087. Ref<EditorImportPlugin> EditorImportExport::get_import_plugin(int p_idx) const {
  1088. ERR_FAIL_INDEX_V(p_idx, plugins.size(), Ref<EditorImportPlugin>());
  1089. return plugins[p_idx];
  1090. }
  1091. Ref<EditorImportPlugin> EditorImportExport::get_import_plugin_by_name(const String &p_string) const {
  1092. ERR_FAIL_COND_V(!by_idx.has(p_string), Ref<EditorImportPlugin>());
  1093. return plugins[by_idx[p_string]];
  1094. }
  1095. void EditorImportExport::add_export_plugin(const Ref<EditorExportPlugin> &p_plugin) {
  1096. ERR_FAIL_COND(p_plugin.is_null());
  1097. export_plugins.push_back(p_plugin);
  1098. }
  1099. void EditorImportExport::remove_export_plugin(const Ref<EditorExportPlugin> &p_plugin) {
  1100. ERR_FAIL_COND(p_plugin.is_null());
  1101. export_plugins.erase(p_plugin);
  1102. }
  1103. int EditorImportExport::get_export_plugin_count() const {
  1104. return export_plugins.size();
  1105. }
  1106. Ref<EditorExportPlugin> EditorImportExport::get_export_plugin(int p_idx) const {
  1107. ERR_FAIL_INDEX_V(p_idx, export_plugins.size(), Ref<EditorExportPlugin>());
  1108. return export_plugins[p_idx];
  1109. }
  1110. void EditorImportExport::set_export_file_action(const StringName &p_file, FileAction p_action) {
  1111. if (p_action == ACTION_NONE) {
  1112. files.erase(p_file);
  1113. } else {
  1114. files[p_file] = p_action;
  1115. }
  1116. }
  1117. EditorImportExport::FileAction EditorImportExport::get_export_file_action(const StringName &p_file) const {
  1118. if (files.has(p_file))
  1119. return files[p_file];
  1120. return ACTION_NONE;
  1121. }
  1122. void EditorImportExport::get_export_file_list(List<StringName> *p_files) {
  1123. for (Map<StringName, FileAction>::Element *E = files.front(); E; E = E->next()) {
  1124. p_files->push_back(E->key());
  1125. }
  1126. }
  1127. void EditorImportExport::add_export_platform(const Ref<EditorExportPlatform> &p_export) {
  1128. exporters[p_export->get_name()] = p_export;
  1129. }
  1130. void EditorImportExport::get_export_platforms(List<StringName> *r_platforms) {
  1131. for (Map<StringName, Ref<EditorExportPlatform> >::Element *E = exporters.front(); E; E = E->next()) {
  1132. r_platforms->push_back(E->key());
  1133. }
  1134. }
  1135. Ref<EditorExportPlatform> EditorImportExport::get_export_platform(const StringName &p_platform) {
  1136. if (exporters.has(p_platform)) {
  1137. return exporters[p_platform];
  1138. } else {
  1139. return Ref<EditorExportPlatform>();
  1140. }
  1141. }
  1142. bool EditorImportExport::poll_export_platforms() {
  1143. bool changed = false;
  1144. for (Map<StringName, Ref<EditorExportPlatform> >::Element *E = exporters.front(); E; E = E->next()) {
  1145. if (E->get()->poll_devices())
  1146. changed = true;
  1147. }
  1148. return changed;
  1149. }
  1150. void EditorImportExport::set_export_filter(ExportFilter p_enable) {
  1151. export_filter = p_enable;
  1152. }
  1153. EditorImportExport::ExportFilter EditorImportExport::get_export_filter() const {
  1154. return export_filter;
  1155. }
  1156. void EditorImportExport::set_export_custom_filter(const String &p_custom_filter) {
  1157. export_custom_filter = p_custom_filter;
  1158. }
  1159. void EditorImportExport::set_export_custom_filter_exclude(const String &p_custom_filter) {
  1160. export_custom_filter_exclude = p_custom_filter;
  1161. }
  1162. void EditorImportExport::set_export_custom_filter_exclude_dir(const String &p_custom_filter) {
  1163. export_custom_filter_exclude_dir = p_custom_filter;
  1164. }
  1165. String EditorImportExport::get_export_custom_filter() const {
  1166. return export_custom_filter;
  1167. }
  1168. String EditorImportExport::get_export_custom_filter_exclude() const {
  1169. return export_custom_filter_exclude;
  1170. }
  1171. String EditorImportExport::get_export_custom_filter_exclude_dir() const {
  1172. return export_custom_filter_exclude_dir;
  1173. }
  1174. void EditorImportExport::set_export_image_action(ImageAction p_action) {
  1175. image_action = p_action;
  1176. }
  1177. EditorImportExport::ImageAction EditorImportExport::get_export_image_action() const {
  1178. return image_action;
  1179. }
  1180. void EditorImportExport::set_export_image_shrink(float p_shrink) {
  1181. image_shrink = p_shrink;
  1182. }
  1183. float EditorImportExport::get_export_image_shrink() const {
  1184. return image_shrink;
  1185. }
  1186. void EditorImportExport::set_export_image_quality(float p_quality) {
  1187. image_action_compress_quality = p_quality;
  1188. }
  1189. float EditorImportExport::get_export_image_quality() const {
  1190. return image_action_compress_quality;
  1191. }
  1192. void EditorImportExport::image_export_group_create(const StringName &p_name) {
  1193. ERR_FAIL_COND(image_groups.has(p_name));
  1194. ImageGroup ig;
  1195. ig.action = IMAGE_ACTION_NONE; //default
  1196. ig.make_atlas = false;
  1197. ig.shrink = 1;
  1198. ig.lossy_quality = 0.7;
  1199. image_groups[p_name] = ig;
  1200. }
  1201. bool EditorImportExport::image_export_has_group(const StringName &p_name) const {
  1202. return image_groups.has(p_name);
  1203. }
  1204. void EditorImportExport::image_export_get_groups(List<StringName> *r_name) const {
  1205. for (Map<StringName, ImageGroup>::Element *E = image_groups.front(); E; E = E->next()) {
  1206. r_name->push_back(E->key());
  1207. }
  1208. }
  1209. void EditorImportExport::image_export_group_remove(const StringName &p_name) {
  1210. ERR_FAIL_COND(!image_groups.has(p_name));
  1211. image_groups.erase(p_name);
  1212. }
  1213. void EditorImportExport::image_export_group_set_image_action(const StringName &p_export_group, ImageAction p_action) {
  1214. ERR_FAIL_COND(!image_groups.has(p_export_group));
  1215. image_groups[p_export_group].action = p_action;
  1216. }
  1217. EditorImportExport::ImageAction EditorImportExport::image_export_group_get_image_action(const StringName &p_export_group) const {
  1218. ERR_FAIL_COND_V(!image_groups.has(p_export_group), IMAGE_ACTION_NONE);
  1219. return image_groups[p_export_group].action;
  1220. }
  1221. void EditorImportExport::image_export_group_set_make_atlas(const StringName &p_export_group, bool p_make) {
  1222. ERR_FAIL_COND(!image_groups.has(p_export_group));
  1223. image_groups[p_export_group].make_atlas = p_make;
  1224. }
  1225. bool EditorImportExport::image_export_group_get_make_atlas(const StringName &p_export_group) const {
  1226. ERR_FAIL_COND_V(!image_groups.has(p_export_group), false);
  1227. return image_groups[p_export_group].make_atlas;
  1228. }
  1229. void EditorImportExport::image_export_group_set_shrink(const StringName &p_export_group, float p_amount) {
  1230. ERR_FAIL_COND(!image_groups.has(p_export_group));
  1231. image_groups[p_export_group].shrink = p_amount;
  1232. }
  1233. float EditorImportExport::image_export_group_get_shrink(const StringName &p_export_group) const {
  1234. ERR_FAIL_COND_V(!image_groups.has(p_export_group), 1);
  1235. return image_groups[p_export_group].shrink;
  1236. }
  1237. void EditorImportExport::image_export_group_set_lossy_quality(const StringName &p_export_group, float p_amount) {
  1238. ERR_FAIL_COND(!image_groups.has(p_export_group));
  1239. image_groups[p_export_group].lossy_quality = p_amount;
  1240. }
  1241. float EditorImportExport::image_export_group_get_lossy_quality(const StringName &p_export_group) const {
  1242. ERR_FAIL_COND_V(!image_groups.has(p_export_group), 1);
  1243. return image_groups[p_export_group].lossy_quality;
  1244. }
  1245. StringName EditorImportExport::image_get_export_group(const StringName &p_image) const {
  1246. if (image_group_files.has(p_image))
  1247. return image_group_files[p_image];
  1248. else
  1249. return StringName();
  1250. }
  1251. void EditorImportExport::image_add_to_export_group(const StringName &p_image, const StringName &p_export_group) {
  1252. bool emptygroup = String(p_export_group) == String();
  1253. ERR_FAIL_COND(!emptygroup && !image_groups.has(p_export_group));
  1254. if (emptygroup)
  1255. image_group_files.erase(p_image);
  1256. else
  1257. image_group_files[p_image] = p_export_group;
  1258. }
  1259. void EditorImportExport::image_export_get_images_in_group(const StringName &p_group, List<StringName> *r_images) const {
  1260. for (Map<StringName, StringName>::Element *E = image_group_files.front(); E; E = E->next()) {
  1261. if (p_group == E->get())
  1262. r_images->push_back(E->key());
  1263. }
  1264. }
  1265. void EditorImportExport::set_convert_text_scenes(bool p_convert) {
  1266. convert_text_scenes = p_convert;
  1267. }
  1268. bool EditorImportExport::get_convert_text_scenes() const {
  1269. return convert_text_scenes;
  1270. }
  1271. void EditorImportExport::load_config() {
  1272. Ref<ConfigFile> cf = memnew(ConfigFile);
  1273. Error err = cf->load("res://export.cfg");
  1274. if (err != OK)
  1275. return; //no export config to be loaded!
  1276. export_custom_filter = cf->get_value("export_filter", "filter");
  1277. export_custom_filter_exclude = cf->get_value("export_filter", "filter_exclude");
  1278. export_custom_filter_exclude_dir = cf->get_value("export_filter", "filter_exclude_dir");
  1279. String t = cf->get_value("export_filter", "type");
  1280. if (t == "selected")
  1281. export_filter = EXPORT_SELECTED;
  1282. else if (t == "resources")
  1283. export_filter = EXPORT_RESOURCES;
  1284. else if (t == "all")
  1285. export_filter = EXPORT_ALL;
  1286. if (cf->has_section("convert_images")) {
  1287. String ci = "convert_images";
  1288. String action = cf->get_value(ci, "action");
  1289. if (action == "none")
  1290. image_action = IMAGE_ACTION_NONE;
  1291. else if (action == "compress_ram")
  1292. image_action = IMAGE_ACTION_COMPRESS_RAM;
  1293. else if (action == "compress_disk")
  1294. image_action = IMAGE_ACTION_COMPRESS_DISK;
  1295. image_action_compress_quality = cf->get_value(ci, "compress_quality");
  1296. if (cf->has_section_key(ci, "shrink"))
  1297. image_shrink = cf->get_value(ci, "shrink");
  1298. else
  1299. image_shrink = 1;
  1300. String formats = cf->get_value(ci, "formats");
  1301. Vector<String> f = formats.split(",");
  1302. image_formats.clear();
  1303. for (int i = 0; i < f.size(); i++) {
  1304. image_formats.insert(f[i].strip_edges());
  1305. }
  1306. }
  1307. if (cf->has_section("convert_scenes")) {
  1308. convert_text_scenes = cf->get_value("convert_scenes", "convert_text_scenes");
  1309. }
  1310. if (cf->has_section("export_filter_files")) {
  1311. String eff = "export_filter_files";
  1312. List<String> k;
  1313. cf->get_section_keys(eff, &k);
  1314. for (List<String>::Element *E = k.front(); E; E = E->next()) {
  1315. String val = cf->get_value(eff, E->get());
  1316. if (val == "copy") {
  1317. files[E->get()] = ACTION_COPY;
  1318. } else if (val == "bundle") {
  1319. files[E->get()] = ACTION_BUNDLE;
  1320. }
  1321. }
  1322. }
  1323. List<String> sect;
  1324. cf->get_sections(&sect);
  1325. for (List<String>::Element *E = sect.front(); E; E = E->next()) {
  1326. String s = E->get();
  1327. if (!s.begins_with("platform:"))
  1328. continue;
  1329. String p = s.substr(s.find(":") + 1, s.length());
  1330. if (!exporters.has(p))
  1331. continue;
  1332. Ref<EditorExportPlatform> ep = exporters[p];
  1333. if (!ep.is_valid()) {
  1334. continue;
  1335. }
  1336. List<String> keys;
  1337. cf->get_section_keys(s, &keys);
  1338. for (List<String>::Element *F = keys.front(); F; F = F->next()) {
  1339. ep->set(F->get(), cf->get_value(s, F->get()));
  1340. }
  1341. }
  1342. //save image groups
  1343. if (cf->has_section("image_groups")) {
  1344. sect.clear();
  1345. cf->get_section_keys("image_groups", &sect);
  1346. for (List<String>::Element *E = sect.front(); E; E = E->next()) {
  1347. Dictionary d = cf->get_value("image_groups", E->get());
  1348. ImageGroup g;
  1349. g.action = IMAGE_ACTION_NONE;
  1350. g.make_atlas = false;
  1351. g.lossy_quality = 0.7;
  1352. g.shrink = 1;
  1353. if (d.has("action")) {
  1354. String action = d["action"];
  1355. if (action == "compress_ram")
  1356. g.action = IMAGE_ACTION_COMPRESS_RAM;
  1357. else if (action == "compress_disk")
  1358. g.action = IMAGE_ACTION_COMPRESS_DISK;
  1359. else if (action == "keep")
  1360. g.action = IMAGE_ACTION_KEEP;
  1361. }
  1362. if (d.has("atlas"))
  1363. g.make_atlas = d["atlas"];
  1364. if (d.has("lossy_quality"))
  1365. g.lossy_quality = d["lossy_quality"];
  1366. if (d.has("shrink")) {
  1367. g.shrink = d["shrink"];
  1368. g.shrink = CLAMP(g.shrink, 1, 8);
  1369. }
  1370. image_groups[E->get()] = g;
  1371. }
  1372. if (cf->has_section_key("image_group_files", "files")) {
  1373. Vector<String> sa = cf->get_value("image_group_files", "files");
  1374. if (sa.size() % 2 == 0) {
  1375. for (int i = 0; i < sa.size(); i += 2) {
  1376. image_group_files[sa[i]] = sa[i + 1];
  1377. }
  1378. }
  1379. }
  1380. }
  1381. if (cf->has_section("script")) {
  1382. if (cf->has_section_key("script", "action")) {
  1383. String action = cf->get_value("script", "action");
  1384. if (action == "compile")
  1385. script_action = SCRIPT_ACTION_COMPILE;
  1386. else if (action == "encrypt")
  1387. script_action = SCRIPT_ACTION_ENCRYPT;
  1388. else
  1389. script_action = SCRIPT_ACTION_NONE;
  1390. }
  1391. if (cf->has_section_key("script", "encrypt_key")) {
  1392. script_key = cf->get_value("script", "encrypt_key");
  1393. }
  1394. }
  1395. if (cf->has_section("convert_samples")) {
  1396. if (cf->has_section_key("convert_samples", "action")) {
  1397. String action = cf->get_value("convert_samples", "action");
  1398. if (action == "none") {
  1399. sample_action = SAMPLE_ACTION_NONE;
  1400. } else if (action == "compress_ram") {
  1401. sample_action = SAMPLE_ACTION_COMPRESS_RAM;
  1402. }
  1403. }
  1404. if (cf->has_section_key("convert_samples", "max_hz"))
  1405. sample_action_max_hz = cf->get_value("convert_samples", "max_hz");
  1406. if (cf->has_section_key("convert_samples", "trim"))
  1407. sample_action_trim = cf->get_value("convert_samples", "trim");
  1408. }
  1409. }
  1410. void EditorImportExport::save_config() {
  1411. Ref<ConfigFile> cf = memnew(ConfigFile);
  1412. switch (export_filter) {
  1413. case EXPORT_SELECTED: cf->set_value("export_filter", "type", "selected"); break;
  1414. case EXPORT_RESOURCES: cf->set_value("export_filter", "type", "resources"); break;
  1415. case EXPORT_ALL: cf->set_value("export_filter", "type", "all"); break;
  1416. }
  1417. cf->set_value("export_filter", "filter", export_custom_filter);
  1418. cf->set_value("export_filter", "filter_exclude", export_custom_filter_exclude);
  1419. cf->set_value("export_filter", "filter_exclude_dir", export_custom_filter_exclude_dir);
  1420. String file_action_section = "export_filter_files";
  1421. for (Map<StringName, FileAction>::Element *E = files.front(); E; E = E->next()) {
  1422. String f = E->key();
  1423. String a;
  1424. switch (E->get()) {
  1425. case ACTION_NONE: {
  1426. }
  1427. case ACTION_COPY: a = "copy"; break;
  1428. case ACTION_BUNDLE: a = "bundle"; break;
  1429. }
  1430. cf->set_value(file_action_section, f, a);
  1431. }
  1432. for (Map<StringName, Ref<EditorExportPlatform> >::Element *E = exporters.front(); E; E = E->next()) {
  1433. String pname = "platform:" + String(E->key());
  1434. Ref<EditorExportPlatform> ep = E->get();
  1435. List<PropertyInfo> pl;
  1436. ep->get_property_list(&pl);
  1437. for (List<PropertyInfo>::Element *F = pl.front(); F; F = F->next()) {
  1438. cf->set_value(pname, F->get().name, ep->get(F->get().name));
  1439. }
  1440. }
  1441. switch (image_action) {
  1442. case IMAGE_ACTION_NONE: cf->set_value("convert_images", "action", "none"); break;
  1443. case IMAGE_ACTION_COMPRESS_RAM: cf->set_value("convert_images", "action", "compress_ram"); break;
  1444. case IMAGE_ACTION_COMPRESS_DISK: cf->set_value("convert_images", "action", "compress_disk"); break;
  1445. }
  1446. cf->set_value("convert_images", "shrink", image_shrink);
  1447. cf->set_value("convert_images", "compress_quality", image_action_compress_quality);
  1448. String formats;
  1449. for (Set<String>::Element *E = image_formats.front(); E; E = E->next()) {
  1450. if (E != image_formats.front())
  1451. formats += ",";
  1452. formats += E->get();
  1453. }
  1454. cf->set_value("convert_images", "formats", formats);
  1455. //save image groups
  1456. for (Map<StringName, ImageGroup>::Element *E = image_groups.front(); E; E = E->next()) {
  1457. Dictionary d;
  1458. switch (E->get().action) {
  1459. case IMAGE_ACTION_NONE: d["action"] = "default"; break;
  1460. case IMAGE_ACTION_COMPRESS_RAM: d["action"] = "compress_ram"; break;
  1461. case IMAGE_ACTION_COMPRESS_DISK: d["action"] = "compress_disk"; break;
  1462. case IMAGE_ACTION_KEEP: d["action"] = "keep"; break;
  1463. }
  1464. d["atlas"] = E->get().make_atlas;
  1465. d["shrink"] = E->get().shrink;
  1466. d["lossy_quality"] = E->get().lossy_quality;
  1467. cf->set_value("image_groups", E->key(), d);
  1468. }
  1469. if (image_groups.size() && image_group_files.size()) {
  1470. Vector<String> igfkeys;
  1471. igfkeys.resize(image_group_files.size());
  1472. int idx = 0;
  1473. for (Map<StringName, StringName>::Element *E = image_group_files.front(); E; E = E->next()) {
  1474. igfkeys[idx++] = E->key();
  1475. }
  1476. igfkeys.sort();
  1477. Vector<String> igfsave;
  1478. igfsave.resize(image_group_files.size() * 2);
  1479. idx = 0;
  1480. for (int i = 0; i < igfkeys.size(); ++i) {
  1481. igfsave[idx++] = igfkeys[i];
  1482. igfsave[idx++] = image_group_files[igfkeys[i]];
  1483. }
  1484. cf->set_value("image_group_files", "files", igfsave);
  1485. }
  1486. switch (script_action) {
  1487. case SCRIPT_ACTION_NONE: cf->set_value("script", "action", "none"); break;
  1488. case SCRIPT_ACTION_COMPILE: cf->set_value("script", "action", "compile"); break;
  1489. case SCRIPT_ACTION_ENCRYPT: cf->set_value("script", "action", "encrypt"); break;
  1490. }
  1491. cf->set_value("convert_scenes", "convert_text_scenes", convert_text_scenes);
  1492. cf->set_value("script", "encrypt_key", script_key);
  1493. switch (sample_action) {
  1494. case SAMPLE_ACTION_NONE: cf->set_value("convert_samples", "action", "none"); break;
  1495. case SAMPLE_ACTION_COMPRESS_RAM: cf->set_value("convert_samples", "action", "compress_ram"); break;
  1496. }
  1497. cf->set_value("convert_samples", "max_hz", sample_action_max_hz);
  1498. cf->set_value("convert_samples", "trim", sample_action_trim);
  1499. cf->save("res://export.cfg");
  1500. }
  1501. void EditorImportExport::script_set_action(ScriptAction p_action) {
  1502. script_action = p_action;
  1503. }
  1504. EditorImportExport::ScriptAction EditorImportExport::script_get_action() const {
  1505. return script_action;
  1506. }
  1507. void EditorImportExport::script_set_encryption_key(const String &p_key) {
  1508. script_key = p_key;
  1509. }
  1510. String EditorImportExport::script_get_encryption_key() const {
  1511. return script_key;
  1512. }
  1513. void EditorImportExport::sample_set_action(SampleAction p_action) {
  1514. sample_action = p_action;
  1515. }
  1516. EditorImportExport::SampleAction EditorImportExport::sample_get_action() const {
  1517. return sample_action;
  1518. }
  1519. void EditorImportExport::sample_set_max_hz(int p_hz) {
  1520. sample_action_max_hz = p_hz;
  1521. }
  1522. int EditorImportExport::sample_get_max_hz() const {
  1523. return sample_action_max_hz;
  1524. }
  1525. void EditorImportExport::sample_set_trim(bool p_trim) {
  1526. sample_action_trim = p_trim;
  1527. }
  1528. bool EditorImportExport::sample_get_trim() const {
  1529. return sample_action_trim;
  1530. }
  1531. DVector<String> EditorImportExport::_get_export_file_list() {
  1532. DVector<String> fl;
  1533. for (Map<StringName, FileAction>::Element *E = files.front(); E; E = E->next()) {
  1534. fl.push_back(E->key());
  1535. }
  1536. return fl;
  1537. }
  1538. DVector<String> EditorImportExport::_get_export_platforms() {
  1539. DVector<String> ep;
  1540. for (Map<StringName, Ref<EditorExportPlatform> >::Element *E = exporters.front(); E; E = E->next()) {
  1541. ep.push_back(E->key());
  1542. }
  1543. return ep;
  1544. }
  1545. void EditorImportExport::_bind_methods() {
  1546. ObjectTypeDB::bind_method(_MD("add_import_plugin", "plugin:EditorImportPlugin"), &EditorImportExport::add_import_plugin);
  1547. ObjectTypeDB::bind_method(_MD("remove_import_plugin", "plugin:EditorImportPlugin"), &EditorImportExport::remove_import_plugin);
  1548. ObjectTypeDB::bind_method(_MD("get_import_plugin_count"), &EditorImportExport::get_import_plugin_count);
  1549. ObjectTypeDB::bind_method(_MD("get_import_plugin:EditorImportPlugin", "idx"), &EditorImportExport::get_import_plugin);
  1550. ObjectTypeDB::bind_method(_MD("get_import_plugin_by_name:EditorImportPlugin", "name"), &EditorImportExport::get_import_plugin_by_name);
  1551. ObjectTypeDB::bind_method(_MD("add_export_plugin", "plugin:EditorExportPlugin"), &EditorImportExport::add_export_plugin);
  1552. ObjectTypeDB::bind_method(_MD("remove_export_plugin", "plugin:EditorExportPlugin"), &EditorImportExport::remove_export_plugin);
  1553. ObjectTypeDB::bind_method(_MD("get_export_plugin_count"), &EditorImportExport::get_export_plugin_count);
  1554. ObjectTypeDB::bind_method(_MD("get_export_plugin:EditorExportPlugin", "idx"), &EditorImportExport::get_export_plugin);
  1555. ObjectTypeDB::bind_method(_MD("set_export_file_action", "file", "action"), &EditorImportExport::set_export_file_action);
  1556. ObjectTypeDB::bind_method(_MD("get_export_file_action", "file"), &EditorImportExport::get_export_file_action);
  1557. ObjectTypeDB::bind_method(_MD("get_export_file_list"), &EditorImportExport::_get_export_file_list);
  1558. ObjectTypeDB::bind_method(_MD("add_export_platform", "platform:EditorExportplatform"), &EditorImportExport::add_export_platform);
  1559. //ObjectTypeDB::bind_method(_MD("remove_export_platform","platform:EditorExportplatform"),&EditorImportExport::add_export_platform);
  1560. ObjectTypeDB::bind_method(_MD("get_export_platform:EditorExportPlatform", "name"), &EditorImportExport::get_export_platform);
  1561. ObjectTypeDB::bind_method(_MD("get_export_platforms"), &EditorImportExport::_get_export_platforms);
  1562. ObjectTypeDB::bind_method(_MD("set_export_filter", "filter"), &EditorImportExport::set_export_filter);
  1563. ObjectTypeDB::bind_method(_MD("get_export_filter"), &EditorImportExport::get_export_filter);
  1564. ObjectTypeDB::bind_method(_MD("set_export_custom_filter", "filter"), &EditorImportExport::set_export_custom_filter);
  1565. ObjectTypeDB::bind_method(_MD("get_export_custom_filter"), &EditorImportExport::get_export_custom_filter);
  1566. ObjectTypeDB::bind_method(_MD("set_export_custom_filter_exclude", "filter_exclude"), &EditorImportExport::set_export_custom_filter_exclude);
  1567. ObjectTypeDB::bind_method(_MD("get_export_custom_filter_exclude"), &EditorImportExport::get_export_custom_filter_exclude);
  1568. ObjectTypeDB::bind_method(_MD("set_export_custom_filter_exclude_dir", "filter_exclude_dir"), &EditorImportExport::set_export_custom_filter_exclude_dir);
  1569. ObjectTypeDB::bind_method(_MD("get_export_custom_filter_exclude_dir"), &EditorImportExport::get_export_custom_filter_exclude_dir);
  1570. ObjectTypeDB::bind_method(_MD("image_export_group_create"), &EditorImportExport::image_export_group_create);
  1571. ObjectTypeDB::bind_method(_MD("image_export_group_remove"), &EditorImportExport::image_export_group_remove);
  1572. ObjectTypeDB::bind_method(_MD("image_export_group_set_image_action"), &EditorImportExport::image_export_group_set_image_action);
  1573. ObjectTypeDB::bind_method(_MD("image_export_group_set_make_atlas"), &EditorImportExport::image_export_group_set_make_atlas);
  1574. ObjectTypeDB::bind_method(_MD("image_export_group_set_shrink"), &EditorImportExport::image_export_group_set_shrink);
  1575. ObjectTypeDB::bind_method(_MD("image_export_group_get_image_action"), &EditorImportExport::image_export_group_get_image_action);
  1576. ObjectTypeDB::bind_method(_MD("image_export_group_get_make_atlas"), &EditorImportExport::image_export_group_get_make_atlas);
  1577. ObjectTypeDB::bind_method(_MD("image_export_group_get_shrink"), &EditorImportExport::image_export_group_get_shrink);
  1578. ObjectTypeDB::bind_method(_MD("image_add_to_export_group"), &EditorImportExport::image_add_to_export_group);
  1579. ObjectTypeDB::bind_method(_MD("script_set_action"), &EditorImportExport::script_set_action);
  1580. ObjectTypeDB::bind_method(_MD("script_set_encryption_key"), &EditorImportExport::script_set_encryption_key);
  1581. ObjectTypeDB::bind_method(_MD("script_get_action"), &EditorImportExport::script_get_action);
  1582. ObjectTypeDB::bind_method(_MD("script_get_encryption_key"), &EditorImportExport::script_get_encryption_key);
  1583. BIND_CONSTANT(ACTION_NONE);
  1584. BIND_CONSTANT(ACTION_COPY);
  1585. BIND_CONSTANT(ACTION_BUNDLE);
  1586. BIND_CONSTANT(EXPORT_SELECTED);
  1587. BIND_CONSTANT(EXPORT_RESOURCES);
  1588. BIND_CONSTANT(EXPORT_ALL);
  1589. BIND_CONSTANT(IMAGE_ACTION_NONE);
  1590. BIND_CONSTANT(IMAGE_ACTION_COMPRESS_DISK);
  1591. BIND_CONSTANT(IMAGE_ACTION_COMPRESS_RAM);
  1592. BIND_CONSTANT(IMAGE_ACTION_KEEP);
  1593. BIND_CONSTANT(SCRIPT_ACTION_NONE);
  1594. BIND_CONSTANT(SCRIPT_ACTION_COMPILE);
  1595. BIND_CONSTANT(SCRIPT_ACTION_ENCRYPT);
  1596. };
  1597. EditorImportExport::EditorImportExport() {
  1598. export_filter = EXPORT_RESOURCES;
  1599. singleton = this;
  1600. image_action = IMAGE_ACTION_NONE;
  1601. image_action_compress_quality = 0.7;
  1602. image_formats.insert("png");
  1603. image_shrink = 1;
  1604. script_action = SCRIPT_ACTION_COMPILE;
  1605. sample_action = SAMPLE_ACTION_NONE;
  1606. sample_action_max_hz = 44100;
  1607. sample_action_trim = false;
  1608. convert_text_scenes = true;
  1609. }
  1610. EditorImportExport::~EditorImportExport() {
  1611. }