editor_export_preset.cpp 14 KB

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