editor_export_preset.cpp 20 KB

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