editor_export.cpp 14 KB

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