editor_export.cpp 15 KB

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