editor_export.cpp 16 KB

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