editor_export_preset.cpp 19 KB

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