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