editor_export.cpp 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418
  1. /**************************************************************************/
  2. /* editor_export.cpp */
  3. /**************************************************************************/
  4. /* This file is part of: */
  5. /* GODOT ENGINE */
  6. /* https://godotengine.org */
  7. /**************************************************************************/
  8. /* Copyright (c) 2014-present Godot Engine contributors (see AUTHORS.md). */
  9. /* Copyright (c) 2007-2014 Juan Linietsky, Ariel Manzur. */
  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/config/project_settings.h"
  32. #include "core/io/config_file.h"
  33. #include "editor/import/resource_importer_texture_settings.h"
  34. EditorExport *EditorExport::singleton = nullptr;
  35. void EditorExport::_save() {
  36. Ref<ConfigFile> config;
  37. Ref<ConfigFile> credentials;
  38. config.instantiate();
  39. credentials.instantiate();
  40. for (int i = 0; i < export_presets.size(); i++) {
  41. Ref<EditorExportPreset> preset = export_presets[i];
  42. String section = "preset." + itos(i);
  43. config->set_value(section, "name", preset->get_name());
  44. config->set_value(section, "platform", preset->get_platform()->get_name());
  45. config->set_value(section, "runnable", preset->is_runnable());
  46. config->set_value(section, "dedicated_server", preset->is_dedicated_server());
  47. config->set_value(section, "custom_features", preset->get_custom_features());
  48. bool save_files = false;
  49. switch (preset->get_export_filter()) {
  50. case EditorExportPreset::EXPORT_ALL_RESOURCES: {
  51. config->set_value(section, "export_filter", "all_resources");
  52. } break;
  53. case EditorExportPreset::EXPORT_SELECTED_SCENES: {
  54. config->set_value(section, "export_filter", "scenes");
  55. save_files = true;
  56. } break;
  57. case EditorExportPreset::EXPORT_SELECTED_RESOURCES: {
  58. config->set_value(section, "export_filter", "resources");
  59. save_files = true;
  60. } break;
  61. case EditorExportPreset::EXCLUDE_SELECTED_RESOURCES: {
  62. config->set_value(section, "export_filter", "exclude");
  63. save_files = true;
  64. } break;
  65. case EditorExportPreset::EXPORT_CUSTOMIZED: {
  66. config->set_value(section, "export_filter", "customized");
  67. config->set_value(section, "customized_files", preset->get_customized_files());
  68. save_files = false;
  69. };
  70. }
  71. if (save_files) {
  72. Vector<String> export_files = preset->get_files_to_export();
  73. config->set_value(section, "export_files", export_files);
  74. }
  75. config->set_value(section, "include_filter", preset->get_include_filter());
  76. config->set_value(section, "exclude_filter", preset->get_exclude_filter());
  77. config->set_value(section, "export_path", preset->get_export_path());
  78. config->set_value(section, "encryption_include_filters", preset->get_enc_in_filter());
  79. config->set_value(section, "encryption_exclude_filters", preset->get_enc_ex_filter());
  80. config->set_value(section, "encrypt_pck", preset->get_enc_pck());
  81. config->set_value(section, "encrypt_directory", preset->get_enc_directory());
  82. credentials->set_value(section, "script_encryption_key", preset->get_script_encryption_key());
  83. String option_section = "preset." + itos(i) + ".options";
  84. for (const KeyValue<StringName, Variant> &E : preset->values) {
  85. PropertyInfo *prop = preset->properties.getptr(E.key);
  86. if (prop && prop->usage & PROPERTY_USAGE_SECRET) {
  87. credentials->set_value(option_section, E.key, E.value);
  88. } else {
  89. config->set_value(option_section, E.key, E.value);
  90. }
  91. }
  92. }
  93. config->save("res://export_presets.cfg");
  94. credentials->save("res://.godot/export_credentials.cfg");
  95. }
  96. void EditorExport::save_presets() {
  97. if (block_save) {
  98. return;
  99. }
  100. save_timer->start();
  101. }
  102. void EditorExport::_bind_methods() {
  103. ADD_SIGNAL(MethodInfo("export_presets_updated"));
  104. }
  105. void EditorExport::add_export_platform(const Ref<EditorExportPlatform> &p_platform) {
  106. export_platforms.push_back(p_platform);
  107. should_update_presets = true;
  108. }
  109. int EditorExport::get_export_platform_count() {
  110. return export_platforms.size();
  111. }
  112. Ref<EditorExportPlatform> EditorExport::get_export_platform(int p_idx) {
  113. ERR_FAIL_INDEX_V(p_idx, export_platforms.size(), Ref<EditorExportPlatform>());
  114. return export_platforms[p_idx];
  115. }
  116. void EditorExport::add_export_preset(const Ref<EditorExportPreset> &p_preset, int p_at_pos) {
  117. if (p_at_pos < 0) {
  118. export_presets.push_back(p_preset);
  119. } else {
  120. export_presets.insert(p_at_pos, p_preset);
  121. }
  122. }
  123. String EditorExportPlatform::test_etc2() const {
  124. if (!ResourceImporterTextureSettings::should_import_etc2_astc()) {
  125. return TTR("Target platform requires 'ETC2/ASTC' texture compression. Enable 'Import ETC2 ASTC' in Project Settings.") + "\n";
  126. }
  127. return String();
  128. }
  129. String EditorExportPlatform::test_bc() const {
  130. if (!ResourceImporterTextureSettings::should_import_s3tc_bptc()) {
  131. return TTR("Target platform requires 'S3TC/BPTC' texture compression. Enable 'Import S3TC BPTC' in Project Settings.") + "\n";
  132. }
  133. return String();
  134. }
  135. int EditorExport::get_export_preset_count() const {
  136. return export_presets.size();
  137. }
  138. Ref<EditorExportPreset> EditorExport::get_export_preset(int p_idx) {
  139. ERR_FAIL_INDEX_V(p_idx, export_presets.size(), Ref<EditorExportPreset>());
  140. return export_presets[p_idx];
  141. }
  142. void EditorExport::remove_export_preset(int p_idx) {
  143. export_presets.remove_at(p_idx);
  144. save_presets();
  145. }
  146. void EditorExport::add_export_plugin(const Ref<EditorExportPlugin> &p_plugin) {
  147. if (!export_plugins.has(p_plugin)) {
  148. export_plugins.push_back(p_plugin);
  149. should_update_presets = true;
  150. }
  151. }
  152. void EditorExport::remove_export_plugin(const Ref<EditorExportPlugin> &p_plugin) {
  153. export_plugins.erase(p_plugin);
  154. should_update_presets = true;
  155. }
  156. Vector<Ref<EditorExportPlugin>> EditorExport::get_export_plugins() {
  157. return export_plugins;
  158. }
  159. void EditorExport::_notification(int p_what) {
  160. switch (p_what) {
  161. case NOTIFICATION_ENTER_TREE: {
  162. load_config();
  163. } break;
  164. case NOTIFICATION_PROCESS: {
  165. update_export_presets();
  166. } break;
  167. case NOTIFICATION_EXIT_TREE: {
  168. for (int i = 0; i < export_platforms.size(); i++) {
  169. export_platforms.write[i]->cleanup();
  170. }
  171. } break;
  172. }
  173. }
  174. void EditorExport::load_config() {
  175. Ref<ConfigFile> config;
  176. config.instantiate();
  177. Error err = config->load("res://export_presets.cfg");
  178. if (err != OK) {
  179. return;
  180. }
  181. Ref<ConfigFile> credentials;
  182. credentials.instantiate();
  183. err = credentials->load("res://.godot/export_credentials.cfg");
  184. if (!(err == OK || err == ERR_FILE_NOT_FOUND)) {
  185. return;
  186. }
  187. block_save = true;
  188. int index = 0;
  189. while (true) {
  190. String section = "preset." + itos(index);
  191. if (!config->has_section(section)) {
  192. break;
  193. }
  194. String platform = config->get_value(section, "platform");
  195. // Forward compatibility with Linux platform after 4.3.
  196. if (platform == "Linux") {
  197. platform = "Linux/X11";
  198. }
  199. Ref<EditorExportPreset> preset;
  200. for (int i = 0; i < export_platforms.size(); i++) {
  201. if (export_platforms[i]->get_name() == platform) {
  202. preset = export_platforms.write[i]->create_preset();
  203. break;
  204. }
  205. }
  206. if (!preset.is_valid()) {
  207. index++;
  208. ERR_CONTINUE(!preset.is_valid());
  209. }
  210. preset->set_name(config->get_value(section, "name"));
  211. preset->set_runnable(config->get_value(section, "runnable"));
  212. preset->set_dedicated_server(config->get_value(section, "dedicated_server", false));
  213. if (config->has_section_key(section, "custom_features")) {
  214. preset->set_custom_features(config->get_value(section, "custom_features"));
  215. }
  216. String export_filter = config->get_value(section, "export_filter");
  217. bool get_files = false;
  218. if (export_filter == "all_resources") {
  219. preset->set_export_filter(EditorExportPreset::EXPORT_ALL_RESOURCES);
  220. } else if (export_filter == "scenes") {
  221. preset->set_export_filter(EditorExportPreset::EXPORT_SELECTED_SCENES);
  222. get_files = true;
  223. } else if (export_filter == "resources") {
  224. preset->set_export_filter(EditorExportPreset::EXPORT_SELECTED_RESOURCES);
  225. get_files = true;
  226. } else if (export_filter == "exclude") {
  227. preset->set_export_filter(EditorExportPreset::EXCLUDE_SELECTED_RESOURCES);
  228. get_files = true;
  229. } else if (export_filter == "customized") {
  230. preset->set_export_filter(EditorExportPreset::EXPORT_CUSTOMIZED);
  231. preset->set_customized_files(config->get_value(section, "customized_files", Dictionary()));
  232. get_files = false;
  233. }
  234. if (get_files) {
  235. Vector<String> files = config->get_value(section, "export_files");
  236. for (int i = 0; i < files.size(); i++) {
  237. if (!FileAccess::exists(files[i])) {
  238. preset->remove_export_file(files[i]);
  239. } else {
  240. preset->add_export_file(files[i]);
  241. }
  242. }
  243. }
  244. preset->set_include_filter(config->get_value(section, "include_filter"));
  245. preset->set_exclude_filter(config->get_value(section, "exclude_filter"));
  246. preset->set_export_path(config->get_value(section, "export_path", ""));
  247. if (config->has_section_key(section, "encrypt_pck")) {
  248. preset->set_enc_pck(config->get_value(section, "encrypt_pck"));
  249. }
  250. if (config->has_section_key(section, "encrypt_directory")) {
  251. preset->set_enc_directory(config->get_value(section, "encrypt_directory"));
  252. }
  253. if (config->has_section_key(section, "encryption_include_filters")) {
  254. preset->set_enc_in_filter(config->get_value(section, "encryption_include_filters"));
  255. }
  256. if (config->has_section_key(section, "encryption_exclude_filters")) {
  257. preset->set_enc_ex_filter(config->get_value(section, "encryption_exclude_filters"));
  258. }
  259. if (credentials->has_section_key(section, "script_encryption_key")) {
  260. preset->set_script_encryption_key(credentials->get_value(section, "script_encryption_key"));
  261. }
  262. String option_section = "preset." + itos(index) + ".options";
  263. List<String> options;
  264. config->get_section_keys(option_section, &options);
  265. for (const String &E : options) {
  266. Variant value = config->get_value(option_section, E);
  267. preset->set(E, value);
  268. }
  269. if (credentials->has_section(option_section)) {
  270. options.clear();
  271. credentials->get_section_keys(option_section, &options);
  272. for (const String &E : options) {
  273. // Drop values for secret properties that no longer exist, or during the next save they would end up in the regular config file.
  274. if (preset->get_properties().has(E)) {
  275. Variant value = credentials->get_value(option_section, E);
  276. preset->set(E, value);
  277. }
  278. }
  279. }
  280. add_export_preset(preset);
  281. index++;
  282. }
  283. block_save = false;
  284. }
  285. void EditorExport::update_export_presets() {
  286. HashMap<StringName, List<EditorExportPlatform::ExportOption>> platform_options;
  287. for (int i = 0; i < export_platforms.size(); i++) {
  288. Ref<EditorExportPlatform> platform = export_platforms[i];
  289. bool should_update = should_update_presets;
  290. should_update |= platform->should_update_export_options();
  291. for (int j = 0; j < export_plugins.size(); j++) {
  292. should_update |= export_plugins.write[j]->_should_update_export_options(platform);
  293. }
  294. if (should_update) {
  295. List<EditorExportPlatform::ExportOption> options;
  296. platform->get_export_options(&options);
  297. for (int j = 0; j < export_plugins.size(); j++) {
  298. export_plugins[j]->_get_export_options(platform, &options);
  299. }
  300. platform_options[platform->get_name()] = options;
  301. }
  302. }
  303. should_update_presets = false;
  304. bool export_presets_updated = false;
  305. for (int i = 0; i < export_presets.size(); i++) {
  306. Ref<EditorExportPreset> preset = export_presets[i];
  307. if (platform_options.has(preset->get_platform()->get_name())) {
  308. export_presets_updated = true;
  309. List<EditorExportPlatform::ExportOption> options = platform_options[preset->get_platform()->get_name()];
  310. // Clear the preset properties prior to reloading, keep the values to preserve options from plugins that may be currently disabled.
  311. preset->properties.clear();
  312. preset->update_visibility.clear();
  313. for (const EditorExportPlatform::ExportOption &E : options) {
  314. StringName option_name = E.option.name;
  315. preset->properties[option_name] = E.option;
  316. if (!preset->has(option_name)) {
  317. preset->values[option_name] = E.default_value;
  318. }
  319. preset->update_visibility[option_name] = E.update_visibility;
  320. }
  321. }
  322. }
  323. if (export_presets_updated) {
  324. emit_signal(_export_presets_updated);
  325. }
  326. }
  327. bool EditorExport::poll_export_platforms() {
  328. bool changed = false;
  329. for (int i = 0; i < export_platforms.size(); i++) {
  330. if (export_platforms.write[i]->poll_export()) {
  331. changed = true;
  332. }
  333. }
  334. return changed;
  335. }
  336. EditorExport::EditorExport() {
  337. save_timer = memnew(Timer);
  338. add_child(save_timer);
  339. save_timer->set_wait_time(0.8);
  340. save_timer->set_one_shot(true);
  341. save_timer->connect("timeout", callable_mp(this, &EditorExport::_save));
  342. _export_presets_updated = "export_presets_updated";
  343. singleton = this;
  344. set_process(true);
  345. }
  346. EditorExport::~EditorExport() {
  347. }