Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
godotengine
GitHub Repository: godotengine/godot
Path: blob/master/editor/export/editor_export_preset.cpp
20803 views
1
/**************************************************************************/
2
/* editor_export_preset.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_preset.h"
32
#include "editor_export_preset.compat.inc"
33
34
#include "core/config/project_settings.h"
35
#include "core/io/dir_access.h"
36
#include "editor/export/editor_export.h"
37
#include "editor/settings/editor_settings.h"
38
39
bool EditorExportPreset::_set(const StringName &p_name, const Variant &p_value) {
40
values[p_name] = p_value;
41
EditorExport::singleton->save_presets();
42
if (update_visibility.has(p_name)) {
43
if (update_visibility[p_name]) {
44
update_value_overrides();
45
notify_property_list_changed();
46
}
47
return true;
48
}
49
50
return false;
51
}
52
53
bool EditorExportPreset::_get(const StringName &p_name, Variant &r_ret) const {
54
if (value_overrides.has(p_name)) {
55
r_ret = value_overrides[p_name];
56
return true;
57
}
58
59
if (values.has(p_name)) {
60
r_ret = values[p_name];
61
return true;
62
}
63
64
return false;
65
}
66
67
Variant EditorExportPreset::get_project_setting(const StringName &p_name) {
68
List<String> ftr_list;
69
platform->get_platform_features(&ftr_list);
70
platform->get_preset_features(this, &ftr_list);
71
72
Vector<String> features;
73
for (const String &E : ftr_list) {
74
features.push_back(E);
75
}
76
77
if (!get_custom_features().is_empty()) {
78
Vector<String> tmp_custom_list = get_custom_features().split(",");
79
80
for (int i = 0; i < tmp_custom_list.size(); i++) {
81
String f = tmp_custom_list[i].strip_edges();
82
if (!f.is_empty()) {
83
features.push_back(f);
84
}
85
}
86
}
87
return ProjectSettings::get_singleton()->get_setting_with_override_and_custom_features(p_name, features);
88
}
89
90
void EditorExportPreset::_bind_methods() {
91
ClassDB::bind_method(D_METHOD("_get_property_warning", "name"), &EditorExportPreset::_get_property_warning);
92
93
ClassDB::bind_method(D_METHOD("has", "property"), &EditorExportPreset::has);
94
95
ClassDB::bind_method(D_METHOD("get_files_to_export"), &EditorExportPreset::get_files_to_export);
96
ClassDB::bind_method(D_METHOD("get_customized_files"), &EditorExportPreset::get_customized_files);
97
ClassDB::bind_method(D_METHOD("get_customized_files_count"), &EditorExportPreset::get_customized_files_count);
98
ClassDB::bind_method(D_METHOD("has_export_file", "path"), &EditorExportPreset::has_export_file);
99
ClassDB::bind_method(D_METHOD("get_file_export_mode", "path", "default"), &EditorExportPreset::get_file_export_mode, DEFVAL(MODE_FILE_NOT_CUSTOMIZED));
100
ClassDB::bind_method(D_METHOD("get_project_setting", "name"), &EditorExportPreset::get_project_setting);
101
102
ClassDB::bind_method(D_METHOD("get_preset_name"), &EditorExportPreset::get_name);
103
ClassDB::bind_method(D_METHOD("is_runnable"), &EditorExportPreset::is_runnable);
104
ClassDB::bind_method(D_METHOD("are_advanced_options_enabled"), &EditorExportPreset::are_advanced_options_enabled);
105
ClassDB::bind_method(D_METHOD("is_dedicated_server"), &EditorExportPreset::is_dedicated_server);
106
ClassDB::bind_method(D_METHOD("get_export_filter"), &EditorExportPreset::get_export_filter);
107
ClassDB::bind_method(D_METHOD("get_include_filter"), &EditorExportPreset::get_include_filter);
108
ClassDB::bind_method(D_METHOD("get_exclude_filter"), &EditorExportPreset::get_exclude_filter);
109
ClassDB::bind_method(D_METHOD("get_custom_features"), &EditorExportPreset::get_custom_features);
110
ClassDB::bind_method(D_METHOD("get_patches"), &EditorExportPreset::get_patches);
111
ClassDB::bind_method(D_METHOD("get_export_path"), &EditorExportPreset::get_export_path);
112
ClassDB::bind_method(D_METHOD("get_encryption_in_filter"), &EditorExportPreset::get_enc_in_filter);
113
ClassDB::bind_method(D_METHOD("get_encryption_ex_filter"), &EditorExportPreset::get_enc_ex_filter);
114
ClassDB::bind_method(D_METHOD("get_encrypt_pck"), &EditorExportPreset::get_enc_pck);
115
ClassDB::bind_method(D_METHOD("get_encrypt_directory"), &EditorExportPreset::get_enc_directory);
116
ClassDB::bind_method(D_METHOD("get_encryption_key"), &EditorExportPreset::get_script_encryption_key);
117
ClassDB::bind_method(D_METHOD("get_script_export_mode"), &EditorExportPreset::get_script_export_mode);
118
119
ClassDB::bind_method(D_METHOD("get_or_env", "name", "env_var"), &EditorExportPreset::_get_or_env);
120
ClassDB::bind_method(D_METHOD("get_version", "name", "windows_version"), &EditorExportPreset::get_version);
121
122
BIND_ENUM_CONSTANT(EXPORT_ALL_RESOURCES);
123
BIND_ENUM_CONSTANT(EXPORT_SELECTED_SCENES);
124
BIND_ENUM_CONSTANT(EXPORT_SELECTED_RESOURCES);
125
BIND_ENUM_CONSTANT(EXCLUDE_SELECTED_RESOURCES);
126
BIND_ENUM_CONSTANT(EXPORT_CUSTOMIZED);
127
128
BIND_ENUM_CONSTANT(MODE_FILE_NOT_CUSTOMIZED);
129
BIND_ENUM_CONSTANT(MODE_FILE_STRIP);
130
BIND_ENUM_CONSTANT(MODE_FILE_KEEP);
131
BIND_ENUM_CONSTANT(MODE_FILE_REMOVE);
132
133
BIND_ENUM_CONSTANT(MODE_SCRIPT_TEXT);
134
BIND_ENUM_CONSTANT(MODE_SCRIPT_BINARY_TOKENS);
135
BIND_ENUM_CONSTANT(MODE_SCRIPT_BINARY_TOKENS_COMPRESSED);
136
}
137
138
String EditorExportPreset::_get_property_warning(const StringName &p_name) const {
139
if (value_overrides.has(p_name)) {
140
return String();
141
}
142
143
String warning = platform->get_export_option_warning(this, p_name);
144
if (!warning.is_empty()) {
145
warning += "\n";
146
}
147
148
// Get property warning from editor export plugins.
149
Vector<Ref<EditorExportPlugin>> export_plugins = EditorExport::get_singleton()->get_export_plugins();
150
for (int i = 0; i < export_plugins.size(); i++) {
151
if (!export_plugins[i]->supports_platform(platform)) {
152
continue;
153
}
154
155
export_plugins.write[i]->set_export_preset(Ref<EditorExportPreset>(this));
156
String plugin_warning = export_plugins[i]->_get_export_option_warning(platform, p_name);
157
if (!plugin_warning.is_empty()) {
158
warning += plugin_warning + "\n";
159
}
160
}
161
162
return warning;
163
}
164
165
void EditorExportPreset::_get_property_list(List<PropertyInfo> *p_list) const {
166
for (const KeyValue<StringName, PropertyInfo> &E : properties) {
167
if (!value_overrides.has(E.key)) {
168
bool property_visible = platform->get_export_option_visibility(this, E.key);
169
if (!property_visible) {
170
continue;
171
}
172
173
// Get option visibility from editor export plugins.
174
Vector<Ref<EditorExportPlugin>> export_plugins = EditorExport::get_singleton()->get_export_plugins();
175
for (int i = 0; i < export_plugins.size(); i++) {
176
if (!export_plugins[i]->supports_platform(platform)) {
177
continue;
178
}
179
180
export_plugins.write[i]->set_export_preset(Ref<EditorExportPreset>(this));
181
property_visible = export_plugins[i]->_get_export_option_visibility(platform, E.key);
182
if (!property_visible) {
183
break;
184
}
185
}
186
187
if (property_visible) {
188
p_list->push_back(E.value);
189
}
190
}
191
}
192
}
193
194
Ref<EditorExportPlatform> EditorExportPreset::get_platform() const {
195
return platform;
196
}
197
198
void EditorExportPreset::update_files() {
199
{
200
Vector<String> to_remove;
201
for (const String &E : selected_files) {
202
if (!FileAccess::exists(E)) {
203
to_remove.push_back(E);
204
}
205
}
206
for (int i = 0; i < to_remove.size(); ++i) {
207
selected_files.erase(to_remove[i]);
208
}
209
}
210
211
{
212
Vector<String> to_remove;
213
for (const KeyValue<String, FileExportMode> &E : customized_files) {
214
if (!FileAccess::exists(E.key) && !DirAccess::exists(E.key)) {
215
to_remove.push_back(E.key);
216
}
217
}
218
for (int i = 0; i < to_remove.size(); ++i) {
219
customized_files.erase(to_remove[i]);
220
}
221
}
222
}
223
224
void EditorExportPreset::update_value_overrides() {
225
Vector<Ref<EditorExportPlugin>> export_plugins = EditorExport::get_singleton()->get_export_plugins();
226
HashMap<StringName, Variant> new_value_overrides;
227
228
value_overrides.clear();
229
230
for (int i = 0; i < export_plugins.size(); i++) {
231
if (!export_plugins[i]->supports_platform(platform)) {
232
continue;
233
}
234
235
export_plugins.write[i]->set_export_preset(Ref<EditorExportPreset>(this));
236
237
Dictionary plugin_overrides = export_plugins[i]->_get_export_options_overrides(platform);
238
if (!plugin_overrides.is_empty()) {
239
for (const KeyValue<Variant, Variant> &kv : plugin_overrides) {
240
const StringName &key = kv.key;
241
const Variant &value = kv.value;
242
if (new_value_overrides.has(key) && new_value_overrides[key] != value) {
243
WARN_PRINT_ED(vformat("Editor export plugin '%s' overrides pre-existing export option override '%s' with new value.", export_plugins[i]->get_name(), key));
244
}
245
new_value_overrides[key] = value;
246
}
247
}
248
}
249
250
value_overrides = new_value_overrides;
251
notify_property_list_changed();
252
}
253
254
Vector<String> EditorExportPreset::get_files_to_export() const {
255
Vector<String> files;
256
for (const String &E : selected_files) {
257
files.push_back(E);
258
}
259
return files;
260
}
261
262
HashSet<String> EditorExportPreset::get_selected_files() const {
263
return selected_files;
264
}
265
266
void EditorExportPreset::set_selected_files(const HashSet<String> &p_files) {
267
selected_files = p_files;
268
}
269
270
Dictionary EditorExportPreset::get_customized_files() const {
271
Dictionary files;
272
for (const KeyValue<String, FileExportMode> &E : customized_files) {
273
String mode;
274
switch (E.value) {
275
case MODE_FILE_NOT_CUSTOMIZED: {
276
continue;
277
} break;
278
case MODE_FILE_STRIP: {
279
mode = "strip";
280
} break;
281
case MODE_FILE_KEEP: {
282
mode = "keep";
283
} break;
284
case MODE_FILE_REMOVE: {
285
mode = "remove";
286
}
287
}
288
files[E.key] = mode;
289
}
290
return files;
291
}
292
293
int EditorExportPreset::get_customized_files_count() const {
294
return customized_files.size();
295
}
296
297
void EditorExportPreset::set_customized_files(const Dictionary &p_files) {
298
for (const Variant *key = p_files.next(nullptr); key; key = p_files.next(key)) {
299
EditorExportPreset::FileExportMode mode = EditorExportPreset::MODE_FILE_NOT_CUSTOMIZED;
300
String value = p_files[*key];
301
if (value == "strip") {
302
mode = EditorExportPreset::MODE_FILE_STRIP;
303
} else if (value == "keep") {
304
mode = EditorExportPreset::MODE_FILE_KEEP;
305
} else if (value == "remove") {
306
mode = EditorExportPreset::MODE_FILE_REMOVE;
307
}
308
set_file_export_mode(*key, mode);
309
}
310
}
311
312
void EditorExportPreset::set_name(const String &p_name) {
313
name = p_name;
314
EditorExport::singleton->save_presets();
315
}
316
317
String EditorExportPreset::get_name() const {
318
return name;
319
}
320
321
void EditorExportPreset::set_runnable(bool p_enable) {
322
if (p_enable) {
323
EditorExport::singleton->set_runnable_preset(this);
324
} else {
325
EditorExport::singleton->unset_runnable_preset(this);
326
}
327
}
328
329
bool EditorExportPreset::is_runnable() const {
330
return EditorExport::singleton->get_runnable_preset_for_platform(platform).ptr() == this;
331
}
332
333
bool EditorExportPreset::are_advanced_options_enabled() const {
334
return EDITOR_GET("_export_preset_advanced_mode");
335
}
336
337
void EditorExportPreset::set_dedicated_server(bool p_enable) {
338
dedicated_server = p_enable;
339
EditorExport::singleton->save_presets();
340
}
341
342
bool EditorExportPreset::is_dedicated_server() const {
343
return dedicated_server;
344
}
345
346
void EditorExportPreset::set_export_filter(ExportFilter p_filter) {
347
export_filter = p_filter;
348
EditorExport::singleton->save_presets();
349
}
350
351
EditorExportPreset::ExportFilter EditorExportPreset::get_export_filter() const {
352
return export_filter;
353
}
354
355
void EditorExportPreset::set_include_filter(const String &p_include) {
356
include_filter = p_include;
357
EditorExport::singleton->save_presets();
358
}
359
360
String EditorExportPreset::get_include_filter() const {
361
return include_filter;
362
}
363
364
void EditorExportPreset::set_export_path(const String &p_path) {
365
export_path = p_path;
366
/* NOTE(SonerSound): if there is a need to implement a PropertyHint that specifically indicates a relative path,
367
* this should be removed. */
368
if (export_path.is_absolute_path()) {
369
String res_path = OS::get_singleton()->get_resource_dir();
370
export_path = res_path.path_to_file(export_path);
371
}
372
EditorExport::singleton->save_presets();
373
}
374
375
String EditorExportPreset::get_export_path() const {
376
return export_path;
377
}
378
379
void EditorExportPreset::set_exclude_filter(const String &p_exclude) {
380
exclude_filter = p_exclude;
381
EditorExport::singleton->save_presets();
382
}
383
384
String EditorExportPreset::get_exclude_filter() const {
385
return exclude_filter;
386
}
387
388
void EditorExportPreset::add_export_file(const String &p_path) {
389
selected_files.insert(p_path);
390
EditorExport::singleton->save_presets();
391
}
392
393
void EditorExportPreset::remove_export_file(const String &p_path) {
394
selected_files.erase(p_path);
395
EditorExport::singleton->save_presets();
396
}
397
398
bool EditorExportPreset::has_export_file(const String &p_path) {
399
return selected_files.has(p_path);
400
}
401
402
void EditorExportPreset::set_file_export_mode(const String &p_path, EditorExportPreset::FileExportMode p_mode) {
403
if (p_mode == FileExportMode::MODE_FILE_NOT_CUSTOMIZED) {
404
customized_files.erase(p_path);
405
} else {
406
customized_files.insert(p_path, p_mode);
407
}
408
EditorExport::singleton->save_presets();
409
}
410
411
EditorExportPreset::FileExportMode EditorExportPreset::get_file_export_mode(const String &p_path, EditorExportPreset::FileExportMode p_default) const {
412
HashMap<String, FileExportMode>::ConstIterator i = customized_files.find(p_path);
413
if (i) {
414
return i->value;
415
}
416
return p_default;
417
}
418
419
void EditorExportPreset::add_patch(const String &p_path, int p_at_pos) {
420
ERR_FAIL_COND_EDMSG(patches.has(p_path), vformat("Failed to add patch \"%s\". Patches must be unique.", p_path));
421
422
if (p_at_pos < 0) {
423
patches.push_back(p_path);
424
} else {
425
patches.insert(p_at_pos, p_path);
426
}
427
428
EditorExport::singleton->save_presets();
429
}
430
431
void EditorExportPreset::set_patch(int p_index, const String &p_path) {
432
remove_patch(p_index);
433
add_patch(p_path, p_index);
434
}
435
436
String EditorExportPreset::get_patch(int p_index) {
437
ERR_FAIL_INDEX_V(p_index, patches.size(), String());
438
return patches[p_index];
439
}
440
441
void EditorExportPreset::remove_patch(int p_index) {
442
ERR_FAIL_INDEX(p_index, patches.size());
443
patches.remove_at(p_index);
444
EditorExport::singleton->save_presets();
445
}
446
447
void EditorExportPreset::set_patches(const Vector<String> &p_patches) {
448
patches = p_patches;
449
}
450
451
Vector<String> EditorExportPreset::get_patches() const {
452
return patches;
453
}
454
455
void EditorExportPreset::set_patch_delta_encoding_enabled(bool p_enable) {
456
patch_delta_encoding_enabled = p_enable;
457
EditorExport::singleton->save_presets();
458
}
459
460
bool EditorExportPreset::is_patch_delta_encoding_enabled() const {
461
return patch_delta_encoding_enabled;
462
}
463
464
void EditorExportPreset::set_patch_delta_zstd_level(int p_level) {
465
patch_delta_zstd_level = p_level;
466
EditorExport::singleton->save_presets();
467
}
468
469
int EditorExportPreset::get_patch_delta_zstd_level() const {
470
return patch_delta_zstd_level;
471
}
472
473
void EditorExportPreset::set_patch_delta_min_reduction(double p_ratio) {
474
patch_delta_min_reduction = p_ratio;
475
EditorExport::singleton->save_presets();
476
}
477
478
double EditorExportPreset::get_patch_delta_min_reduction() const {
479
return patch_delta_min_reduction;
480
}
481
482
void EditorExportPreset::set_patch_delta_include_filter(const String &p_filter) {
483
patch_delta_include_filter = p_filter;
484
EditorExport::singleton->save_presets();
485
}
486
487
String EditorExportPreset::get_patch_delta_include_filter() const {
488
return patch_delta_include_filter;
489
}
490
491
void EditorExportPreset::set_patch_delta_exclude_filter(const String &p_filter) {
492
patch_delta_exclude_filter = p_filter;
493
EditorExport::singleton->save_presets();
494
}
495
496
String EditorExportPreset::get_patch_delta_exclude_filter() const {
497
return patch_delta_exclude_filter;
498
}
499
500
void EditorExportPreset::set_custom_features(const String &p_custom_features) {
501
custom_features = p_custom_features;
502
EditorExport::singleton->save_presets();
503
}
504
505
String EditorExportPreset::get_custom_features() const {
506
return custom_features;
507
}
508
509
void EditorExportPreset::set_enc_in_filter(const String &p_filter) {
510
enc_in_filters = p_filter;
511
EditorExport::singleton->save_presets();
512
}
513
514
String EditorExportPreset::get_enc_in_filter() const {
515
return enc_in_filters;
516
}
517
518
void EditorExportPreset::set_enc_ex_filter(const String &p_filter) {
519
enc_ex_filters = p_filter;
520
EditorExport::singleton->save_presets();
521
}
522
523
String EditorExportPreset::get_enc_ex_filter() const {
524
return enc_ex_filters;
525
}
526
527
void EditorExportPreset::set_seed(uint64_t p_seed) {
528
seed = p_seed;
529
EditorExport::singleton->save_presets();
530
}
531
532
uint64_t EditorExportPreset::get_seed() const {
533
return seed;
534
}
535
536
void EditorExportPreset::set_enc_pck(bool p_enabled) {
537
enc_pck = p_enabled;
538
EditorExport::singleton->save_presets();
539
}
540
541
bool EditorExportPreset::get_enc_pck() const {
542
return enc_pck;
543
}
544
545
void EditorExportPreset::set_enc_directory(bool p_enabled) {
546
enc_directory = p_enabled;
547
EditorExport::singleton->save_presets();
548
}
549
550
bool EditorExportPreset::get_enc_directory() const {
551
return enc_directory;
552
}
553
554
void EditorExportPreset::set_script_encryption_key(const String &p_key) {
555
script_key = p_key;
556
EditorExport::singleton->save_presets();
557
}
558
559
String EditorExportPreset::get_script_encryption_key() const {
560
return script_key;
561
}
562
563
void EditorExportPreset::set_script_export_mode(ScriptExportMode p_mode) {
564
script_mode = p_mode;
565
EditorExport::singleton->save_presets();
566
}
567
568
EditorExportPreset::ScriptExportMode EditorExportPreset::get_script_export_mode() const {
569
return script_mode;
570
}
571
572
Variant EditorExportPreset::get_or_env(const StringName &p_name, const String &p_env_var, bool *r_valid) const {
573
const String from_env = OS::get_singleton()->get_environment(p_env_var);
574
if (!from_env.is_empty()) {
575
if (r_valid) {
576
*r_valid = true;
577
}
578
return from_env;
579
}
580
return get(p_name, r_valid);
581
}
582
583
_FORCE_INLINE_ bool _check_digits(const String &p_str) {
584
for (int i = 0; i < p_str.length(); i++) {
585
char32_t c = p_str.operator[](i);
586
if (!is_digit(c)) {
587
return false;
588
}
589
}
590
return true;
591
}
592
593
String EditorExportPreset::get_version(const StringName &p_preset_string, bool p_windows_version) const {
594
String result = get(p_preset_string);
595
if (result.is_empty()) {
596
result = GLOBAL_GET("application/config/version");
597
598
// Split and validate version number components.
599
const PackedStringArray result_split = result.split(".", false);
600
bool valid_version = !result_split.is_empty();
601
for (const String &E : result_split) {
602
if (!_check_digits(E)) {
603
valid_version = false;
604
break;
605
}
606
}
607
608
if (valid_version) {
609
if (p_windows_version) {
610
// Modify version number to match Windows constraints (version numbers must have 4 components).
611
if (result_split.size() == 1) {
612
result = result + ".0.0.0";
613
} else if (result_split.size() == 2) {
614
result = result + ".0.0";
615
} else if (result_split.size() == 3) {
616
result = result + ".0";
617
} else {
618
result = vformat("%s.%s.%s.%s", result_split[0], result_split[1], result_split[2], result_split[3]);
619
}
620
} else {
621
result = String(".").join(result_split);
622
}
623
} else {
624
if (!result.is_empty()) {
625
WARN_PRINT(vformat("Invalid version number \"%s\". The version number can only contain numeric characters (0-9) and non-consecutive periods (.).", result));
626
}
627
if (p_windows_version) {
628
result = "1.0.0.0";
629
} else {
630
result = "1.0.0";
631
}
632
}
633
}
634
635
return result;
636
}
637
638