| 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 | |
| 31 | #include "editor_export.h" |
| 32 | |
| 33 | #include "core/config/project_settings.h" |
| 34 | #include "core/io/config_file.h" |
| 35 | #include "editor/settings/editor_settings.h" |
| 36 | #include "scene/main/timer.h" |
| 37 | |
| 38 | EditorExport *EditorExport::singleton = nullptr; |
| 39 | |
| 40 | void EditorExport::_save() { |
| 41 | Ref<ConfigFile> config; |
| 42 | Ref<ConfigFile> credentials; |
| 43 | config.instantiate(); |
| 44 | credentials.instantiate(); |
| 45 | for (int i = 0; i < export_presets.size(); i++) { |
| 46 | Ref<EditorExportPreset> preset = export_presets[i]; |
| 47 | String section = "preset." + itos(i); |
| 48 | |
| 49 | config->set_value(section, "name", preset->get_name()); |
| 50 | config->set_value(section, "platform", preset->get_platform()->get_name()); |
| 51 | config->set_value(section, "runnable", preset->is_runnable()); |
| 52 | config->set_value(section, "dedicated_server", preset->is_dedicated_server()); |
| 53 | config->set_value(section, "custom_features", preset->get_custom_features()); |
| 54 | |
| 55 | bool save_files = false; |
| 56 | switch (preset->get_export_filter()) { |
| 57 | case EditorExportPreset::EXPORT_ALL_RESOURCES: { |
| 58 | config->set_value(section, "export_filter", "all_resources"); |
| 59 | } break; |
| 60 | case EditorExportPreset::EXPORT_SELECTED_SCENES: { |
| 61 | config->set_value(section, "export_filter", "scenes"); |
| 62 | save_files = true; |
| 63 | } break; |
| 64 | case EditorExportPreset::EXPORT_SELECTED_RESOURCES: { |
| 65 | config->set_value(section, "export_filter", "resources"); |
| 66 | save_files = true; |
| 67 | } break; |
| 68 | case EditorExportPreset::EXCLUDE_SELECTED_RESOURCES: { |
| 69 | config->set_value(section, "export_filter", "exclude"); |
| 70 | save_files = true; |
| 71 | } break; |
| 72 | case EditorExportPreset::EXPORT_CUSTOMIZED: { |
| 73 | config->set_value(section, "export_filter", "customized"); |
| 74 | config->set_value(section, "customized_files", preset->get_customized_files()); |
| 75 | save_files = false; |
| 76 | }; |
| 77 | } |
| 78 | |
| 79 | if (save_files) { |
| 80 | Vector<String> export_files = preset->get_files_to_export(); |
| 81 | config->set_value(section, "export_files", export_files); |
| 82 | } |
| 83 | config->set_value(section, "include_filter", preset->get_include_filter()); |
| 84 | config->set_value(section, "exclude_filter", preset->get_exclude_filter()); |
| 85 | config->set_value(section, "export_path", preset->get_export_path()); |
| 86 | |
| 87 | config->set_value(section, "patches", preset->get_patches()); |
| 88 | config->set_value(section, "patch_delta_encoding", preset->is_patch_delta_encoding_enabled()); |
| 89 | config->set_value(section, "patch_delta_compression_level_zstd", preset->get_patch_delta_zstd_level()); |
| 90 | config->set_value(section, "patch_delta_min_reduction", preset->get_patch_delta_min_reduction()); |
| 91 | config->set_value(section, "patch_delta_include_filters", preset->get_patch_delta_include_filter()); |
| 92 | config->set_value(section, "patch_delta_exclude_filters", preset->get_patch_delta_exclude_filter()); |
| 93 | |
| 94 | config->set_value(section, "encryption_include_filters", preset->get_enc_in_filter()); |
| 95 | config->set_value(section, "encryption_exclude_filters", preset->get_enc_ex_filter()); |
| 96 | config->set_value(section, "seed", preset->get_seed()); |
| 97 | |
| 98 | config->set_value(section, "encrypt_pck", preset->get_enc_pck()); |
| 99 | config->set_value(section, "encrypt_directory", preset->get_enc_directory()); |
| 100 | config->set_value(section, "script_export_mode", preset->get_script_export_mode()); |
| 101 | credentials->set_value(section, "script_encryption_key", preset->get_script_encryption_key()); |
| 102 | |
| 103 | String option_section = "preset." + itos(i) + ".options"; |
| 104 | |
| 105 | for (const KeyValue<StringName, Variant> &E : preset->values) { |
| 106 | PropertyInfo *prop = preset->properties.getptr(E.key); |
| 107 | if (prop && prop->usage & PROPERTY_USAGE_SECRET) { |
| 108 | credentials->set_value(option_section, E.key, E.value); |
| 109 | } else { |
| 110 | config->set_value(option_section, E.key, E.value); |
| 111 | } |
| 112 | } |
| 113 | } |
| 114 | |
| 115 | config->save("res://export_presets.cfg"); |
| 116 | credentials->save("res://.godot/export_credentials.cfg"); |
| 117 | } |
| 118 | |
| 119 | void EditorExport::save_presets() { |
| 120 | if (block_save) { |
| 121 | return; |
| 122 | } |
| 123 | save_timer->start(); |
| 124 | } |
| 125 | |
| 126 | void EditorExport::emit_presets_runnable_changed() { |
| 127 | emit_signal(_export_presets_runnable_updated); |
| 128 | } |
| 129 | |
| 130 | void EditorExport::_bind_methods() { |
| 131 | ADD_SIGNAL(MethodInfo(_export_presets_updated)); |
| 132 | ADD_SIGNAL(MethodInfo(_export_presets_runnable_updated)); |
| 133 | } |
| 134 | |
| 135 | void EditorExport::add_export_platform(const Ref<EditorExportPlatform> &p_platform) { |
| 136 | p_platform->initialize(); |
| 137 | export_platforms.push_back(p_platform); |
| 138 | |
| 139 | should_update_presets = true; |
| 140 | should_reload_presets = true; |
| 141 | } |
| 142 | |
| 143 | void EditorExport::remove_export_platform(const Ref<EditorExportPlatform> &p_platform) { |
| 144 | export_platforms.erase(p_platform); |
| 145 | p_platform->cleanup(); |
| 146 | |
| 147 | should_update_presets = true; |
| 148 | should_reload_presets = true; |
| 149 | } |
| 150 | |
| 151 | int EditorExport::get_export_platform_count() const { |
| 152 | return export_platforms.size(); |
| 153 | } |
| 154 | |
| 155 | int EditorExport::get_export_platform_index_by_name(const String &p_name) { |
| 156 | for (int j = 0; j < get_export_platform_count(); j++) { |
| 157 | Ref<EditorExportPlatform> plat = get_export_platform(j); |
| 158 | if (!plat.is_null() && plat->get_name().nocasecmp_to(p_name) == 0) { |
| 159 | return j; |
| 160 | } |
| 161 | } |
| 162 | return -1; |
| 163 | } |
| 164 | |
| 165 | bool EditorExport::has_preset_with_name(const String &p_name, int p_exclude_index) const { |
| 166 | for (int i = 0; i < export_presets.size(); i++) { |
| 167 | if (i == p_exclude_index) { |
| 168 | continue; |
| 169 | } |
| 170 | if (export_presets[i]->get_name() == p_name) { |
| 171 | return true; |
| 172 | } |
| 173 | } |
| 174 | |
| 175 | return false; |
| 176 | } |
| 177 | |
| 178 | Ref<EditorExportPlatform> EditorExport::get_export_platform(int p_idx) { |
| 179 | ERR_FAIL_INDEX_V(p_idx, export_platforms.size(), Ref<EditorExportPlatform>()); |
| 180 | |
| 181 | return export_platforms[p_idx]; |
| 182 | } |
| 183 | |
| 184 | void EditorExport::add_export_preset(const Ref<EditorExportPreset> &p_preset, int p_at_pos) { |
| 185 | if (p_at_pos < 0) { |
| 186 | export_presets.push_back(p_preset); |
| 187 | } else { |
| 188 | export_presets.insert(p_at_pos, p_preset); |
| 189 | } |
| 190 | emit_presets_runnable_changed(); |
| 191 | } |
| 192 | |
| 193 | int EditorExport::get_export_preset_count() const { |
| 194 | return export_presets.size(); |
| 195 | } |
| 196 | |
| 197 | Ref<EditorExportPreset> EditorExport::get_export_preset(int p_idx) { |
| 198 | ERR_FAIL_INDEX_V(p_idx, export_presets.size(), Ref<EditorExportPreset>()); |
| 199 | return export_presets[p_idx]; |
| 200 | } |
| 201 | |
| 202 | void EditorExport::remove_export_preset(int p_idx) { |
| 203 | export_presets.remove_at(p_idx); |
| 204 | save_presets(); |
| 205 | emit_presets_runnable_changed(); |
| 206 | } |
| 207 | |
| 208 | void EditorExport::add_export_plugin(const Ref<EditorExportPlugin> &p_plugin) { |
| 209 | if (!export_plugins.has(p_plugin)) { |
| 210 | export_plugins.push_back(p_plugin); |
| 211 | should_update_presets = true; |
| 212 | } |
| 213 | } |
| 214 | |
| 215 | void EditorExport::remove_export_plugin(const Ref<EditorExportPlugin> &p_plugin) { |
| 216 | export_plugins.erase(p_plugin); |
| 217 | should_update_presets = true; |
| 218 | } |
| 219 | |
| 220 | Vector<Ref<EditorExportPlugin>> EditorExport::get_export_plugins() { |
| 221 | return export_plugins; |
| 222 | } |
| 223 | |
| 224 | void EditorExport::_notification(int p_what) { |
| 225 | switch (p_what) { |
| 226 | case NOTIFICATION_ENTER_TREE: { |
| 227 | load_config(); |
| 228 | } break; |
| 229 | |
| 230 | case NOTIFICATION_PROCESS: { |
| 231 | update_export_presets(); |
| 232 | } break; |
| 233 | |
| 234 | case NOTIFICATION_EXIT_TREE: { |
| 235 | for (int i = 0; i < export_platforms.size(); i++) { |
| 236 | export_platforms.write[i]->cleanup(); |
| 237 | } |
| 238 | } break; |
| 239 | |
| 240 | case EditorSettings::NOTIFICATION_EDITOR_SETTINGS_CHANGED: { |
| 241 | for (int i = 0; i < export_platforms.size(); i++) { |
| 242 | export_platforms.write[i]->notification(p_what); |
| 243 | } |
| 244 | } break; |
| 245 | } |
| 246 | } |
| 247 | |
| 248 | void EditorExport::load_config() { |
| 249 | Ref<ConfigFile> config; |
| 250 | config.instantiate(); |
| 251 | Error err = config->load("res://export_presets.cfg"); |
| 252 | if (err != OK) { |
| 253 | return; |
| 254 | } |
| 255 | |
| 256 | Ref<ConfigFile> credentials; |
| 257 | credentials.instantiate(); |
| 258 | err = credentials->load("res://.godot/export_credentials.cfg"); |
| 259 | if (!(err == OK || err == ERR_FILE_NOT_FOUND)) { |
| 260 | return; |
| 261 | } |
| 262 | |
| 263 | block_save = true; |
| 264 | |
| 265 | int index = 0; |
| 266 | while (true) { |
| 267 | String section = "preset." + itos(index); |
| 268 | if (!config->has_section(section)) { |
| 269 | break; |
| 270 | } |
| 271 | |
| 272 | String platform = config->get_value(section, "platform"); |
| 273 | #ifndef DISABLE_DEPRECATED |
| 274 | // Compatibility with Linux platform before 4.3. |
| 275 | if (platform == "Linux/X11") { |
| 276 | platform = "Linux"; |
| 277 | } |
| 278 | #endif |
| 279 | |
| 280 | Ref<EditorExportPreset> preset; |
| 281 | |
| 282 | for (int i = 0; i < export_platforms.size(); i++) { |
| 283 | if (export_platforms[i]->get_name() == platform) { |
| 284 | preset = export_platforms.write[i]->create_preset(); |
| 285 | break; |
| 286 | } |
| 287 | } |
| 288 | |
| 289 | if (preset.is_null()) { |
| 290 | index++; |
| 291 | continue; // Unknown platform, skip without error (platform might be loaded later). |
| 292 | } |
| 293 | |
| 294 | preset->set_name(config->get_value(section, "name")); |
| 295 | preset->set_runnable(config->get_value(section, "runnable")); |
| 296 | preset->set_dedicated_server(config->get_value(section, "dedicated_server", false)); |
| 297 | |
| 298 | if (config->has_section_key(section, "custom_features")) { |
| 299 | preset->set_custom_features(config->get_value(section, "custom_features")); |
| 300 | } |
| 301 | |
| 302 | String export_filter = config->get_value(section, "export_filter"); |
| 303 | |
| 304 | bool get_files = false; |
| 305 | |
| 306 | if (export_filter == "all_resources") { |
| 307 | preset->set_export_filter(EditorExportPreset::EXPORT_ALL_RESOURCES); |
| 308 | } else if (export_filter == "scenes") { |
| 309 | preset->set_export_filter(EditorExportPreset::EXPORT_SELECTED_SCENES); |
| 310 | get_files = true; |
| 311 | } else if (export_filter == "resources") { |
| 312 | preset->set_export_filter(EditorExportPreset::EXPORT_SELECTED_RESOURCES); |
| 313 | get_files = true; |
| 314 | } else if (export_filter == "exclude") { |
| 315 | preset->set_export_filter(EditorExportPreset::EXCLUDE_SELECTED_RESOURCES); |
| 316 | get_files = true; |
| 317 | } else if (export_filter == "customized") { |
| 318 | preset->set_export_filter(EditorExportPreset::EXPORT_CUSTOMIZED); |
| 319 | preset->set_customized_files(config->get_value(section, "customized_files", Dictionary())); |
| 320 | get_files = false; |
| 321 | } |
| 322 | |
| 323 | if (get_files) { |
| 324 | Vector<String> files = config->get_value(section, "export_files"); |
| 325 | |
| 326 | for (int i = 0; i < files.size(); i++) { |
| 327 | if (!FileAccess::exists(files[i])) { |
| 328 | preset->remove_export_file(files[i]); |
| 329 | } else { |
| 330 | preset->add_export_file(files[i]); |
| 331 | } |
| 332 | } |
| 333 | } |
| 334 | |
| 335 | preset->set_include_filter(config->get_value(section, "include_filter")); |
| 336 | preset->set_exclude_filter(config->get_value(section, "exclude_filter")); |
| 337 | preset->set_export_path(config->get_value(section, "export_path", "")); |
| 338 | preset->set_script_export_mode(config->get_value(section, "script_export_mode", EditorExportPreset::MODE_SCRIPT_BINARY_TOKENS_COMPRESSED)); |
| 339 | preset->set_patches(config->get_value(section, "patches", Vector<String>())); |
| 340 | |
| 341 | if (config->has_section_key(section, "patch_delta_encoding")) { |
| 342 | preset->set_patch_delta_encoding_enabled(config->get_value(section, "patch_delta_encoding")); |
| 343 | } |
| 344 | if (config->has_section_key(section, "patch_delta_compression_level_zstd")) { |
| 345 | preset->set_patch_delta_zstd_level(config->get_value(section, "patch_delta_compression_level_zstd")); |
| 346 | } |
| 347 | if (config->has_section_key(section, "patch_delta_min_reduction")) { |
| 348 | preset->set_patch_delta_min_reduction(config->get_value(section, "patch_delta_min_reduction")); |
| 349 | } |
| 350 | if (config->has_section_key(section, "patch_delta_include_filters")) { |
| 351 | preset->set_patch_delta_include_filter(config->get_value(section, "patch_delta_include_filters")); |
| 352 | } |
| 353 | if (config->has_section_key(section, "patch_delta_exclude_filters")) { |
| 354 | preset->set_patch_delta_exclude_filter(config->get_value(section, "patch_delta_exclude_filters")); |
| 355 | } |
| 356 | |
| 357 | if (config->has_section_key(section, "seed")) { |
| 358 | preset->set_seed(config->get_value(section, "seed")); |
| 359 | } |
| 360 | if (config->has_section_key(section, "encrypt_pck")) { |
| 361 | preset->set_enc_pck(config->get_value(section, "encrypt_pck")); |
| 362 | } |
| 363 | if (config->has_section_key(section, "encrypt_directory")) { |
| 364 | preset->set_enc_directory(config->get_value(section, "encrypt_directory")); |
| 365 | } |
| 366 | if (config->has_section_key(section, "encryption_include_filters")) { |
| 367 | preset->set_enc_in_filter(config->get_value(section, "encryption_include_filters")); |
| 368 | } |
| 369 | if (config->has_section_key(section, "encryption_exclude_filters")) { |
| 370 | preset->set_enc_ex_filter(config->get_value(section, "encryption_exclude_filters")); |
| 371 | } |
| 372 | if (credentials->has_section_key(section, "script_encryption_key")) { |
| 373 | preset->set_script_encryption_key(credentials->get_value(section, "script_encryption_key")); |
| 374 | } |
| 375 | |
| 376 | String option_section = "preset." + itos(index) + ".options"; |
| 377 | |
| 378 | Vector<String> options = config->get_section_keys(option_section); |
| 379 | |
| 380 | for (const String &E : options) { |
| 381 | Variant value = config->get_value(option_section, E); |
| 382 | preset->set(E, value); |
| 383 | } |
| 384 | |
| 385 | if (credentials->has_section(option_section)) { |
| 386 | options = credentials->get_section_keys(option_section); |
| 387 | |
| 388 | for (const String &E : options) { |
| 389 | // Drop values for secret properties that no longer exist, or during the next save they would end up in the regular config file. |
| 390 | if (preset->get_properties().has(E)) { |
| 391 | Variant value = credentials->get_value(option_section, E); |
| 392 | preset->set(E, value); |
| 393 | } |
| 394 | } |
| 395 | } |
| 396 | |
| 397 | add_export_preset(preset); |
| 398 | index++; |
| 399 | } |
| 400 | |
| 401 | block_save = false; |
| 402 | } |
| 403 | |
| 404 | void EditorExport::update_export_presets() { |
| 405 | HashMap<StringName, List<EditorExportPlatform::ExportOption>> platform_options; |
| 406 | |
| 407 | if (should_reload_presets) { |
| 408 | should_reload_presets = false; |
| 409 | export_presets.clear(); |
| 410 | load_config(); |
| 411 | } |
| 412 | |
| 413 | for (int i = 0; i < export_platforms.size(); i++) { |
| 414 | Ref<EditorExportPlatform> platform = export_platforms[i]; |
| 415 | |
| 416 | bool should_update = should_update_presets; |
| 417 | should_update |= platform->should_update_export_options(); |
| 418 | for (int j = 0; j < export_plugins.size(); j++) { |
| 419 | should_update |= export_plugins.write[j]->_should_update_export_options(platform); |
| 420 | } |
| 421 | |
| 422 | if (should_update) { |
| 423 | List<EditorExportPlatform::ExportOption> options; |
| 424 | platform->get_export_options(&options); |
| 425 | |
| 426 | for (int j = 0; j < export_plugins.size(); j++) { |
| 427 | export_plugins[j]->_get_export_options(platform, &options); |
| 428 | } |
| 429 | |
| 430 | platform_options[platform->get_name()] = options; |
| 431 | } |
| 432 | } |
| 433 | should_update_presets = false; |
| 434 | |
| 435 | bool export_presets_updated = false; |
| 436 | for (int i = 0; i < export_presets.size(); i++) { |
| 437 | Ref<EditorExportPreset> preset = export_presets[i]; |
| 438 | if (platform_options.has(preset->get_platform()->get_name())) { |
| 439 | export_presets_updated = true; |
| 440 | |
| 441 | bool update_value_overrides = false; |
| 442 | List<EditorExportPlatform::ExportOption> options = platform_options[preset->get_platform()->get_name()]; |
| 443 | |
| 444 | // Clear the preset properties prior to reloading, keep the values to preserve options from plugins that may be currently disabled. |
| 445 | preset->properties.clear(); |
| 446 | preset->update_visibility.clear(); |
| 447 | |
| 448 | for (const EditorExportPlatform::ExportOption &E : options) { |
| 449 | StringName option_name = E.option.name; |
| 450 | preset->properties[option_name] = E.option; |
| 451 | if (!preset->has(option_name)) { |
| 452 | preset->values[option_name] = E.default_value; |
| 453 | } |
| 454 | preset->update_visibility[option_name] = E.update_visibility; |
| 455 | if (E.update_visibility) { |
| 456 | update_value_overrides = true; |
| 457 | } |
| 458 | } |
| 459 | |
| 460 | if (update_value_overrides) { |
| 461 | preset->update_value_overrides(); |
| 462 | } |
| 463 | } |
| 464 | } |
| 465 | |
| 466 | if (export_presets_updated) { |
| 467 | emit_signal(_export_presets_updated); |
| 468 | } |
| 469 | } |
| 470 | |
| 471 | bool EditorExport::poll_export_platforms() { |
| 472 | bool changed = false; |
| 473 | for (int i = 0; i < export_platforms.size(); i++) { |
| 474 | if (export_platforms.write[i]->poll_export()) { |
| 475 | changed = true; |
| 476 | } |
| 477 | } |
| 478 | |
| 479 | return changed; |
| 480 | } |
| 481 | |
| 482 | void EditorExport::connect_presets_runnable_updated(const Callable &p_target) { |
| 483 | connect(_export_presets_runnable_updated, p_target); |
| 484 | } |
| 485 | |
| 486 | EditorExport::EditorExport() { |
| 487 | save_timer = memnew(Timer); |
| 488 | add_child(save_timer); |
| 489 | save_timer->set_wait_time(0.8); |
| 490 | save_timer->set_one_shot(true); |
| 491 | save_timer->connect("timeout", callable_mp(this, &EditorExport::_save)); |
| 492 | |
| 493 | _export_presets_updated = StringName("export_presets_updated", true); |
| 494 | _export_presets_runnable_updated = StringName("export_presets_runnable_updated", true); |
| 495 | |
| 496 | singleton = this; |
| 497 | set_process(true); |
| 498 | } |