editor_export_preset.cpp 18 KB

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