project_settings.cpp 47 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257
  1. /*************************************************************************/
  2. /* project_settings.cpp */
  3. /*************************************************************************/
  4. /* This file is part of: */
  5. /* GODOT ENGINE */
  6. /* https://godotengine.org */
  7. /*************************************************************************/
  8. /* Copyright (c) 2007-2022 Juan Linietsky, Ariel Manzur. */
  9. /* Copyright (c) 2014-2022 Godot Engine contributors (cf. AUTHORS.md). */
  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 "project_settings.h"
  31. #include "core/core_bind.h" // For Compression enum.
  32. #include "core/core_string_names.h"
  33. #include "core/input/input_map.h"
  34. #include "core/io/dir_access.h"
  35. #include "core/io/file_access.h"
  36. #include "core/io/file_access_network.h"
  37. #include "core/io/file_access_pack.h"
  38. #include "core/io/marshalls.h"
  39. #include "core/os/keyboard.h"
  40. #include "core/variant/variant_parser.h"
  41. #include "core/version.h"
  42. #include "modules/modules_enabled.gen.h" // For mono.
  43. const String ProjectSettings::PROJECT_DATA_DIR_NAME_SUFFIX = "godot";
  44. ProjectSettings *ProjectSettings::singleton = nullptr;
  45. ProjectSettings *ProjectSettings::get_singleton() {
  46. return singleton;
  47. }
  48. String ProjectSettings::get_project_data_dir_name() const {
  49. return project_data_dir_name;
  50. }
  51. String ProjectSettings::get_project_data_path() const {
  52. return "res://" + get_project_data_dir_name();
  53. }
  54. String ProjectSettings::get_resource_path() const {
  55. return resource_path;
  56. }
  57. String ProjectSettings::get_safe_project_name() const {
  58. String safe_name = OS::get_singleton()->get_safe_dir_name(get("application/config/name"));
  59. if (safe_name.is_empty()) {
  60. safe_name = "UnnamedProject";
  61. }
  62. return safe_name;
  63. }
  64. String ProjectSettings::get_imported_files_path() const {
  65. return get_project_data_path().plus_file("imported");
  66. }
  67. // Returns the features that a project must have when opened with this build of Godot.
  68. // This is used by the project manager to provide the initial_settings for config/features.
  69. const PackedStringArray ProjectSettings::get_required_features() {
  70. PackedStringArray features = PackedStringArray();
  71. features.append(VERSION_BRANCH);
  72. #ifdef REAL_T_IS_DOUBLE
  73. features.append("Double Precision");
  74. #endif
  75. return features;
  76. }
  77. // Returns the features supported by this build of Godot. Includes all required features.
  78. const PackedStringArray ProjectSettings::_get_supported_features() {
  79. PackedStringArray features = get_required_features();
  80. #ifdef MODULE_MONO_ENABLED
  81. features.append("C#");
  82. #endif
  83. // Allow pinning to a specific patch number or build type by marking
  84. // them as supported. They're only used if the user adds them manually.
  85. features.append(VERSION_BRANCH "." _MKSTR(VERSION_PATCH));
  86. features.append(VERSION_FULL_CONFIG);
  87. features.append(VERSION_FULL_BUILD);
  88. // For now, assume Vulkan is always supported.
  89. // This should be removed if it's possible to build the editor without Vulkan.
  90. features.append("Vulkan Clustered");
  91. features.append("Vulkan Mobile");
  92. return features;
  93. }
  94. // Returns the features that this project needs but this build of Godot lacks.
  95. const PackedStringArray ProjectSettings::get_unsupported_features(const PackedStringArray &p_project_features) {
  96. PackedStringArray unsupported_features = PackedStringArray();
  97. PackedStringArray supported_features = singleton->_get_supported_features();
  98. for (int i = 0; i < p_project_features.size(); i++) {
  99. if (!supported_features.has(p_project_features[i])) {
  100. unsupported_features.append(p_project_features[i]);
  101. }
  102. }
  103. unsupported_features.sort();
  104. return unsupported_features;
  105. }
  106. // Returns the features that both this project has and this build of Godot has, ensuring required features exist.
  107. const PackedStringArray ProjectSettings::_trim_to_supported_features(const PackedStringArray &p_project_features) {
  108. // Remove unsupported features if present.
  109. PackedStringArray features = PackedStringArray(p_project_features);
  110. PackedStringArray supported_features = _get_supported_features();
  111. for (int i = p_project_features.size() - 1; i > -1; i--) {
  112. if (!supported_features.has(p_project_features[i])) {
  113. features.remove_at(i);
  114. }
  115. }
  116. // Add required features if not present.
  117. PackedStringArray required_features = get_required_features();
  118. for (int i = 0; i < required_features.size(); i++) {
  119. if (!features.has(required_features[i])) {
  120. features.append(required_features[i]);
  121. }
  122. }
  123. features.sort();
  124. return features;
  125. }
  126. String ProjectSettings::localize_path(const String &p_path) const {
  127. if (resource_path.is_empty() || p_path.begins_with("res://") || p_path.begins_with("user://") ||
  128. (p_path.is_absolute_path() && !p_path.begins_with(resource_path))) {
  129. return p_path.simplify_path();
  130. }
  131. Ref<DirAccess> dir = DirAccess::create(DirAccess::ACCESS_FILESYSTEM);
  132. String path = p_path.replace("\\", "/").simplify_path();
  133. if (dir->change_dir(path) == OK) {
  134. String cwd = dir->get_current_dir();
  135. cwd = cwd.replace("\\", "/");
  136. // Ensure that we end with a '/'.
  137. // This is important to ensure that we do not wrongly localize the resource path
  138. // in an absolute path that just happens to contain this string but points to a
  139. // different folder (e.g. "/my/project" as resource_path would be contained in
  140. // "/my/project_data", even though the latter is not part of res://.
  141. // `plus_file("")` is an easy way to ensure we have a trailing '/'.
  142. const String res_path = resource_path.plus_file("");
  143. // DirAccess::get_current_dir() is not guaranteed to return a path that with a trailing '/',
  144. // so we must make sure we have it as well in order to compare with 'res_path'.
  145. cwd = cwd.plus_file("");
  146. if (!cwd.begins_with(res_path)) {
  147. return p_path;
  148. }
  149. return cwd.replace_first(res_path, "res://");
  150. } else {
  151. int sep = path.rfind("/");
  152. if (sep == -1) {
  153. return "res://" + path;
  154. }
  155. String parent = path.substr(0, sep);
  156. String plocal = localize_path(parent);
  157. if (plocal.is_empty()) {
  158. return "";
  159. }
  160. // Only strip the starting '/' from 'path' if its parent ('plocal') ends with '/'
  161. if (plocal[plocal.length() - 1] == '/') {
  162. sep += 1;
  163. }
  164. return plocal + path.substr(sep, path.size() - sep);
  165. }
  166. }
  167. void ProjectSettings::set_initial_value(const String &p_name, const Variant &p_value) {
  168. ERR_FAIL_COND_MSG(!props.has(p_name), "Request for nonexistent project setting: " + p_name + ".");
  169. props[p_name].initial = p_value;
  170. }
  171. void ProjectSettings::set_restart_if_changed(const String &p_name, bool p_restart) {
  172. ERR_FAIL_COND_MSG(!props.has(p_name), "Request for nonexistent project setting: " + p_name + ".");
  173. props[p_name].restart_if_changed = p_restart;
  174. }
  175. void ProjectSettings::set_as_basic(const String &p_name, bool p_basic) {
  176. ERR_FAIL_COND_MSG(!props.has(p_name), "Request for nonexistent project setting: " + p_name + ".");
  177. props[p_name].basic = p_basic;
  178. }
  179. void ProjectSettings::set_as_internal(const String &p_name, bool p_internal) {
  180. ERR_FAIL_COND_MSG(!props.has(p_name), "Request for nonexistent project setting: " + p_name + ".");
  181. props[p_name].internal = p_internal;
  182. }
  183. void ProjectSettings::set_ignore_value_in_docs(const String &p_name, bool p_ignore) {
  184. ERR_FAIL_COND_MSG(!props.has(p_name), "Request for nonexistent project setting: " + p_name + ".");
  185. #ifdef DEBUG_METHODS_ENABLED
  186. props[p_name].ignore_value_in_docs = p_ignore;
  187. #endif
  188. }
  189. bool ProjectSettings::get_ignore_value_in_docs(const String &p_name) const {
  190. ERR_FAIL_COND_V_MSG(!props.has(p_name), false, "Request for nonexistent project setting: " + p_name + ".");
  191. #ifdef DEBUG_METHODS_ENABLED
  192. return props[p_name].ignore_value_in_docs;
  193. #else
  194. return false;
  195. #endif
  196. }
  197. String ProjectSettings::globalize_path(const String &p_path) const {
  198. if (p_path.begins_with("res://")) {
  199. if (!resource_path.is_empty()) {
  200. return p_path.replace("res:/", resource_path);
  201. }
  202. return p_path.replace("res://", "");
  203. } else if (p_path.begins_with("user://")) {
  204. String data_dir = OS::get_singleton()->get_user_data_dir();
  205. if (!data_dir.is_empty()) {
  206. return p_path.replace("user:/", data_dir);
  207. }
  208. return p_path.replace("user://", "");
  209. }
  210. return p_path;
  211. }
  212. bool ProjectSettings::_set(const StringName &p_name, const Variant &p_value) {
  213. _THREAD_SAFE_METHOD_
  214. if (p_value.get_type() == Variant::NIL) {
  215. props.erase(p_name);
  216. if (p_name.operator String().begins_with("autoload/")) {
  217. String node_name = p_name.operator String().split("/")[1];
  218. if (autoloads.has(node_name)) {
  219. remove_autoload(node_name);
  220. }
  221. }
  222. } else {
  223. if (p_name == CoreStringNames::get_singleton()->_custom_features) {
  224. Vector<String> custom_feature_array = String(p_value).split(",");
  225. for (int i = 0; i < custom_feature_array.size(); i++) {
  226. custom_features.insert(custom_feature_array[i]);
  227. }
  228. return true;
  229. }
  230. if (!disable_feature_overrides) {
  231. int dot = p_name.operator String().find(".");
  232. if (dot != -1) {
  233. Vector<String> s = p_name.operator String().split(".");
  234. bool override_valid = false;
  235. for (int i = 1; i < s.size(); i++) {
  236. String feature = s[i].strip_edges();
  237. if (OS::get_singleton()->has_feature(feature) || custom_features.has(feature)) {
  238. override_valid = true;
  239. break;
  240. }
  241. }
  242. if (override_valid) {
  243. feature_overrides[s[0]] = p_name;
  244. }
  245. }
  246. }
  247. if (props.has(p_name)) {
  248. if (!props[p_name].overridden) {
  249. props[p_name].variant = p_value;
  250. }
  251. } else {
  252. props[p_name] = VariantContainer(p_value, last_order++);
  253. }
  254. if (p_name.operator String().begins_with("autoload/")) {
  255. String node_name = p_name.operator String().split("/")[1];
  256. AutoloadInfo autoload;
  257. autoload.name = node_name;
  258. String path = p_value;
  259. if (path.begins_with("*")) {
  260. autoload.is_singleton = true;
  261. autoload.path = path.substr(1);
  262. } else {
  263. autoload.path = path;
  264. }
  265. add_autoload(autoload);
  266. }
  267. }
  268. return true;
  269. }
  270. bool ProjectSettings::_get(const StringName &p_name, Variant &r_ret) const {
  271. _THREAD_SAFE_METHOD_
  272. StringName name = p_name;
  273. if (!disable_feature_overrides && feature_overrides.has(name)) {
  274. name = feature_overrides[name];
  275. }
  276. if (!props.has(name)) {
  277. WARN_PRINT("Property not found: " + String(name));
  278. return false;
  279. }
  280. r_ret = props[name].variant;
  281. return true;
  282. }
  283. struct _VCSort {
  284. String name;
  285. Variant::Type type = Variant::VARIANT_MAX;
  286. int order = 0;
  287. uint32_t flags = 0;
  288. bool operator<(const _VCSort &p_vcs) const { return order == p_vcs.order ? name < p_vcs.name : order < p_vcs.order; }
  289. };
  290. void ProjectSettings::_get_property_list(List<PropertyInfo> *p_list) const {
  291. _THREAD_SAFE_METHOD_
  292. RBSet<_VCSort> vclist;
  293. for (const KeyValue<StringName, VariantContainer> &E : props) {
  294. const VariantContainer *v = &E.value;
  295. if (v->hide_from_editor) {
  296. continue;
  297. }
  298. _VCSort vc;
  299. vc.name = E.key;
  300. vc.order = v->order;
  301. vc.type = v->variant.get_type();
  302. if (v->internal || vc.name.begins_with("input/") || vc.name.begins_with("importer_defaults/") || vc.name.begins_with("import/") || vc.name.begins_with("autoload/") || vc.name.begins_with("editor_plugins/") || vc.name.begins_with("shader_globals/")) {
  303. vc.flags = PROPERTY_USAGE_STORAGE;
  304. } else {
  305. vc.flags = PROPERTY_USAGE_EDITOR | PROPERTY_USAGE_STORAGE;
  306. }
  307. if (v->basic) {
  308. vc.flags |= PROPERTY_USAGE_EDITOR_BASIC_SETTING;
  309. }
  310. if (v->restart_if_changed) {
  311. vc.flags |= PROPERTY_USAGE_RESTART_IF_CHANGED;
  312. }
  313. vclist.insert(vc);
  314. }
  315. for (const _VCSort &E : vclist) {
  316. String prop_info_name = E.name;
  317. int dot = prop_info_name.find(".");
  318. if (dot != -1 && !custom_prop_info.has(prop_info_name)) {
  319. prop_info_name = prop_info_name.substr(0, dot);
  320. }
  321. if (custom_prop_info.has(prop_info_name)) {
  322. PropertyInfo pi = custom_prop_info[prop_info_name];
  323. pi.name = E.name;
  324. pi.usage = E.flags;
  325. p_list->push_back(pi);
  326. } else {
  327. p_list->push_back(PropertyInfo(E.type, E.name, PROPERTY_HINT_NONE, "", E.flags));
  328. }
  329. }
  330. }
  331. bool ProjectSettings::_load_resource_pack(const String &p_pack, bool p_replace_files, int p_offset) {
  332. if (PackedData::get_singleton()->is_disabled()) {
  333. return false;
  334. }
  335. bool ok = PackedData::get_singleton()->add_pack(p_pack, p_replace_files, p_offset) == OK;
  336. if (!ok) {
  337. return false;
  338. }
  339. //if data.pck is found, all directory access will be from here
  340. DirAccess::make_default<DirAccessPack>(DirAccess::ACCESS_RESOURCES);
  341. using_datapack = true;
  342. return true;
  343. }
  344. void ProjectSettings::_convert_to_last_version(int p_from_version) {
  345. if (p_from_version <= 3) {
  346. // Converts the actions from array to dictionary (array of events to dictionary with deadzone + events)
  347. for (KeyValue<StringName, ProjectSettings::VariantContainer> &E : props) {
  348. Variant value = E.value.variant;
  349. if (String(E.key).begins_with("input/") && value.get_type() == Variant::ARRAY) {
  350. Array array = value;
  351. Dictionary action;
  352. action["deadzone"] = Variant(0.5f);
  353. action["events"] = array;
  354. E.value.variant = action;
  355. }
  356. }
  357. }
  358. }
  359. /*
  360. * This method is responsible for loading a project.godot file and/or data file
  361. * using the following merit order:
  362. * - If using NetworkClient, try to lookup project file or fail.
  363. * - If --main-pack was passed by the user (`p_main_pack`), load it or fail.
  364. * - Search for project PCKs automatically. For each step we try loading a potential
  365. * PCK, and if it doesn't work, we proceed to the next step. If any step succeeds,
  366. * we try loading the project settings, and abort if it fails. Steps:
  367. * o Bundled PCK in the executable.
  368. * o [macOS only] PCK with same basename as the binary in the .app resource dir.
  369. * o PCK with same basename as the binary in the binary's directory. We handle both
  370. * changing the extension to '.pck' (e.g. 'win_game.exe' -> 'win_game.pck') and
  371. * appending '.pck' to the binary name (e.g. 'linux_game' -> 'linux_game.pck').
  372. * o PCK with the same basename as the binary in the current working directory.
  373. * Same as above for the two possible PCK file names.
  374. * - On relevant platforms (Android/iOS), lookup project file in OS resource path.
  375. * If found, load it or fail.
  376. * - Lookup project file in passed `p_path` (--path passed by the user), i.e. we
  377. * are running from source code.
  378. * If not found and `p_upwards` is true (--upwards passed by the user), look for
  379. * project files in parent folders up to the system root (used to run a game
  380. * from command line while in a subfolder).
  381. * If a project file is found, load it or fail.
  382. * If nothing was found, error out.
  383. */
  384. Error ProjectSettings::_setup(const String &p_path, const String &p_main_pack, bool p_upwards, bool p_ignore_override) {
  385. // If looking for files in a network client, use it directly
  386. if (FileAccessNetworkClient::get_singleton()) {
  387. Error err = _load_settings_text_or_binary("res://project.godot", "res://project.binary");
  388. if (err == OK && !p_ignore_override) {
  389. // Optional, we don't mind if it fails
  390. _load_settings_text("res://override.cfg");
  391. }
  392. return err;
  393. }
  394. // Attempt with a user-defined main pack first
  395. if (!p_main_pack.is_empty()) {
  396. bool ok = _load_resource_pack(p_main_pack);
  397. ERR_FAIL_COND_V_MSG(!ok, ERR_CANT_OPEN, "Cannot open resource pack '" + p_main_pack + "'.");
  398. Error err = _load_settings_text_or_binary("res://project.godot", "res://project.binary");
  399. if (err == OK && !p_ignore_override) {
  400. // Load override from location of the main pack
  401. // Optional, we don't mind if it fails
  402. _load_settings_text(p_main_pack.get_base_dir().plus_file("override.cfg"));
  403. }
  404. return err;
  405. }
  406. String exec_path = OS::get_singleton()->get_executable_path();
  407. if (!exec_path.is_empty()) {
  408. // We do several tests sequentially until one succeeds to find a PCK,
  409. // and if so, we attempt loading it at the end.
  410. // Attempt with PCK bundled into executable.
  411. bool found = _load_resource_pack(exec_path);
  412. // Attempt with exec_name.pck.
  413. // (This is the usual case when distributing a Godot game.)
  414. String exec_dir = exec_path.get_base_dir();
  415. String exec_filename = exec_path.get_file();
  416. String exec_basename = exec_filename.get_basename();
  417. // Based on the OS, it can be the exec path + '.pck' (Linux w/o extension, macOS in .app bundle)
  418. // or the exec path's basename + '.pck' (Windows).
  419. // We need to test both possibilities as extensions for Linux binaries are optional
  420. // (so both 'mygame.bin' and 'mygame' should be able to find 'mygame.pck').
  421. #ifdef OSX_ENABLED
  422. if (!found) {
  423. // Attempt to load PCK from macOS .app bundle resources.
  424. found = _load_resource_pack(OS::get_singleton()->get_bundle_resource_dir().plus_file(exec_basename + ".pck")) || _load_resource_pack(OS::get_singleton()->get_bundle_resource_dir().plus_file(exec_filename + ".pck"));
  425. }
  426. #endif
  427. if (!found) {
  428. // Try to load data pack at the location of the executable.
  429. // As mentioned above, we have two potential names to attempt.
  430. found = _load_resource_pack(exec_dir.plus_file(exec_basename + ".pck")) || _load_resource_pack(exec_dir.plus_file(exec_filename + ".pck"));
  431. }
  432. if (!found) {
  433. // If we couldn't find them next to the executable, we attempt
  434. // the current working directory. Same story, two tests.
  435. found = _load_resource_pack(exec_basename + ".pck") || _load_resource_pack(exec_filename + ".pck");
  436. }
  437. // If we opened our package, try and load our project.
  438. if (found) {
  439. Error err = _load_settings_text_or_binary("res://project.godot", "res://project.binary");
  440. if (err == OK && !p_ignore_override) {
  441. // Load override from location of the executable.
  442. // Optional, we don't mind if it fails.
  443. _load_settings_text(exec_path.get_base_dir().plus_file("override.cfg"));
  444. }
  445. return err;
  446. }
  447. }
  448. // Try to use the filesystem for files, according to OS.
  449. // (Only Android -when reading from pck- and iOS use this.)
  450. if (!OS::get_singleton()->get_resource_dir().is_empty()) {
  451. // OS will call ProjectSettings->get_resource_path which will be empty if not overridden!
  452. // If the OS would rather use a specific location, then it will not be empty.
  453. resource_path = OS::get_singleton()->get_resource_dir().replace("\\", "/");
  454. if (!resource_path.is_empty() && resource_path[resource_path.length() - 1] == '/') {
  455. resource_path = resource_path.substr(0, resource_path.length() - 1); // Chop end.
  456. }
  457. Error err = _load_settings_text_or_binary("res://project.godot", "res://project.binary");
  458. if (err == OK && !p_ignore_override) {
  459. // Optional, we don't mind if it fails.
  460. _load_settings_text("res://override.cfg");
  461. }
  462. return err;
  463. }
  464. // Nothing was found, try to find a project file in provided path (`p_path`)
  465. // or, if requested (`p_upwards`) in parent directories.
  466. Ref<DirAccess> d = DirAccess::create(DirAccess::ACCESS_FILESYSTEM);
  467. ERR_FAIL_COND_V_MSG(d.is_null(), ERR_CANT_CREATE, "Cannot create DirAccess for path '" + p_path + "'.");
  468. d->change_dir(p_path);
  469. String current_dir = d->get_current_dir();
  470. bool found = false;
  471. Error err;
  472. while (true) {
  473. // Set the resource path early so things can be resolved when loading.
  474. resource_path = current_dir;
  475. resource_path = resource_path.replace("\\", "/"); // Windows path to Unix path just in case.
  476. err = _load_settings_text_or_binary(current_dir.plus_file("project.godot"), current_dir.plus_file("project.binary"));
  477. if (err == OK && !p_ignore_override) {
  478. // Optional, we don't mind if it fails.
  479. _load_settings_text(current_dir.plus_file("override.cfg"));
  480. found = true;
  481. break;
  482. }
  483. if (p_upwards) {
  484. // Try to load settings ascending through parent directories
  485. d->change_dir("..");
  486. if (d->get_current_dir() == current_dir) {
  487. break; // not doing anything useful
  488. }
  489. current_dir = d->get_current_dir();
  490. } else {
  491. break;
  492. }
  493. }
  494. if (!found) {
  495. return err;
  496. }
  497. if (resource_path.length() && resource_path[resource_path.length() - 1] == '/') {
  498. resource_path = resource_path.substr(0, resource_path.length() - 1); // Chop end.
  499. }
  500. return OK;
  501. }
  502. Error ProjectSettings::setup(const String &p_path, const String &p_main_pack, bool p_upwards, bool p_ignore_override) {
  503. Error err = _setup(p_path, p_main_pack, p_upwards, p_ignore_override);
  504. if (err == OK) {
  505. String custom_settings = GLOBAL_DEF("application/config/project_settings_override", "");
  506. if (!custom_settings.is_empty()) {
  507. _load_settings_text(custom_settings);
  508. }
  509. }
  510. // Updating the default value after the project settings have loaded.
  511. bool use_hidden_directory = GLOBAL_GET("application/config/use_hidden_project_data_directory");
  512. project_data_dir_name = (use_hidden_directory ? "." : "") + PROJECT_DATA_DIR_NAME_SUFFIX;
  513. // Using GLOBAL_GET on every block for compressing can be slow, so assigning here.
  514. Compression::zstd_long_distance_matching = GLOBAL_GET("compression/formats/zstd/long_distance_matching");
  515. Compression::zstd_level = GLOBAL_GET("compression/formats/zstd/compression_level");
  516. Compression::zstd_window_log_size = GLOBAL_GET("compression/formats/zstd/window_log_size");
  517. Compression::zlib_level = GLOBAL_GET("compression/formats/zlib/compression_level");
  518. Compression::gzip_level = GLOBAL_GET("compression/formats/gzip/compression_level");
  519. return err;
  520. }
  521. bool ProjectSettings::has_setting(String p_var) const {
  522. _THREAD_SAFE_METHOD_
  523. return props.has(p_var);
  524. }
  525. Error ProjectSettings::_load_settings_binary(const String &p_path) {
  526. Error err;
  527. Ref<FileAccess> f = FileAccess::open(p_path, FileAccess::READ, &err);
  528. if (err != OK) {
  529. return err;
  530. }
  531. uint8_t hdr[4];
  532. f->get_buffer(hdr, 4);
  533. ERR_FAIL_COND_V_MSG((hdr[0] != 'E' || hdr[1] != 'C' || hdr[2] != 'F' || hdr[3] != 'G'), ERR_FILE_CORRUPT, "Corrupted header in binary project.binary (not ECFG).");
  534. uint32_t count = f->get_32();
  535. for (uint32_t i = 0; i < count; i++) {
  536. uint32_t slen = f->get_32();
  537. CharString cs;
  538. cs.resize(slen + 1);
  539. cs[slen] = 0;
  540. f->get_buffer((uint8_t *)cs.ptr(), slen);
  541. String key;
  542. key.parse_utf8(cs.ptr());
  543. uint32_t vlen = f->get_32();
  544. Vector<uint8_t> d;
  545. d.resize(vlen);
  546. f->get_buffer(d.ptrw(), vlen);
  547. Variant value;
  548. err = decode_variant(value, d.ptr(), d.size(), nullptr, true);
  549. ERR_CONTINUE_MSG(err != OK, "Error decoding property: " + key + ".");
  550. set(key, value);
  551. }
  552. return OK;
  553. }
  554. Error ProjectSettings::_load_settings_text(const String &p_path) {
  555. Error err;
  556. Ref<FileAccess> f = FileAccess::open(p_path, FileAccess::READ, &err);
  557. if (f.is_null()) {
  558. // FIXME: Above 'err' error code is ERR_FILE_CANT_OPEN if the file is missing
  559. // This needs to be streamlined if we want decent error reporting
  560. return ERR_FILE_NOT_FOUND;
  561. }
  562. VariantParser::StreamFile stream;
  563. stream.f = f;
  564. String assign;
  565. Variant value;
  566. VariantParser::Tag next_tag;
  567. int lines = 0;
  568. String error_text;
  569. String section;
  570. int config_version = 0;
  571. while (true) {
  572. assign = Variant();
  573. next_tag.fields.clear();
  574. next_tag.name = String();
  575. err = VariantParser::parse_tag_assign_eof(&stream, lines, error_text, next_tag, assign, value, nullptr, true);
  576. if (err == ERR_FILE_EOF) {
  577. // If we're loading a project.godot from source code, we can operate some
  578. // ProjectSettings conversions if need be.
  579. _convert_to_last_version(config_version);
  580. last_save_time = FileAccess::get_modified_time(get_resource_path().plus_file("project.godot"));
  581. return OK;
  582. }
  583. ERR_FAIL_COND_V_MSG(err != OK, err, "Error parsing " + p_path + " at line " + itos(lines) + ": " + error_text + " File might be corrupted.");
  584. if (!assign.is_empty()) {
  585. if (section.is_empty() && assign == "config_version") {
  586. config_version = value;
  587. ERR_FAIL_COND_V_MSG(config_version > CONFIG_VERSION, ERR_FILE_CANT_OPEN, vformat("Can't open project at '%s', its `config_version` (%d) is from a more recent and incompatible version of the engine. Expected config version: %d.", p_path, config_version, CONFIG_VERSION));
  588. } else {
  589. if (section.is_empty()) {
  590. set(assign, value);
  591. } else {
  592. set(section + "/" + assign, value);
  593. }
  594. }
  595. } else if (!next_tag.name.is_empty()) {
  596. section = next_tag.name;
  597. }
  598. }
  599. }
  600. Error ProjectSettings::_load_settings_text_or_binary(const String &p_text_path, const String &p_bin_path) {
  601. // Attempt first to load the binary project.godot file.
  602. Error err = _load_settings_binary(p_bin_path);
  603. if (err == OK) {
  604. return OK;
  605. } else if (err != ERR_FILE_NOT_FOUND) {
  606. // If the file exists but can't be loaded, we want to know it.
  607. ERR_PRINT("Couldn't load file '" + p_bin_path + "', error code " + itos(err) + ".");
  608. }
  609. // Fallback to text-based project.godot file if binary was not found.
  610. err = _load_settings_text(p_text_path);
  611. if (err == OK) {
  612. return OK;
  613. } else if (err != ERR_FILE_NOT_FOUND) {
  614. ERR_PRINT("Couldn't load file '" + p_text_path + "', error code " + itos(err) + ".");
  615. }
  616. return err;
  617. }
  618. Error ProjectSettings::load_custom(const String &p_path) {
  619. if (p_path.ends_with(".binary")) {
  620. return _load_settings_binary(p_path);
  621. }
  622. return _load_settings_text(p_path);
  623. }
  624. int ProjectSettings::get_order(const String &p_name) const {
  625. ERR_FAIL_COND_V_MSG(!props.has(p_name), -1, "Request for nonexistent project setting: " + p_name + ".");
  626. return props[p_name].order;
  627. }
  628. void ProjectSettings::set_order(const String &p_name, int p_order) {
  629. ERR_FAIL_COND_MSG(!props.has(p_name), "Request for nonexistent project setting: " + p_name + ".");
  630. props[p_name].order = p_order;
  631. }
  632. void ProjectSettings::set_builtin_order(const String &p_name) {
  633. ERR_FAIL_COND_MSG(!props.has(p_name), "Request for nonexistent project setting: " + p_name + ".");
  634. if (props[p_name].order >= NO_BUILTIN_ORDER_BASE) {
  635. props[p_name].order = last_builtin_order++;
  636. }
  637. }
  638. bool ProjectSettings::is_builtin_setting(const String &p_name) const {
  639. // Return true because a false negative is worse than a false positive.
  640. ERR_FAIL_COND_V_MSG(!props.has(p_name), true, "Request for nonexistent project setting: " + p_name + ".");
  641. return props[p_name].order < NO_BUILTIN_ORDER_BASE;
  642. }
  643. void ProjectSettings::clear(const String &p_name) {
  644. ERR_FAIL_COND_MSG(!props.has(p_name), "Request for nonexistent project setting: " + p_name + ".");
  645. props.erase(p_name);
  646. }
  647. Error ProjectSettings::save() {
  648. Error error = save_custom(get_resource_path().plus_file("project.godot"));
  649. if (error == OK) {
  650. last_save_time = FileAccess::get_modified_time(get_resource_path().plus_file("project.godot"));
  651. }
  652. return error;
  653. }
  654. Error ProjectSettings::_save_settings_binary(const String &p_file, const RBMap<String, List<String>> &props, const CustomMap &p_custom, const String &p_custom_features) {
  655. Error err;
  656. Ref<FileAccess> file = FileAccess::open(p_file, FileAccess::WRITE, &err);
  657. ERR_FAIL_COND_V_MSG(err != OK, err, "Couldn't save project.binary at " + p_file + ".");
  658. uint8_t hdr[4] = { 'E', 'C', 'F', 'G' };
  659. file->store_buffer(hdr, 4);
  660. int count = 0;
  661. for (const KeyValue<String, List<String>> &E : props) {
  662. count += E.value.size();
  663. }
  664. if (!p_custom_features.is_empty()) {
  665. file->store_32(count + 1);
  666. //store how many properties are saved, add one for custom featuers, which must always go first
  667. String key = CoreStringNames::get_singleton()->_custom_features;
  668. file->store_pascal_string(key);
  669. int len;
  670. err = encode_variant(p_custom_features, nullptr, len, false);
  671. ERR_FAIL_COND_V(err != OK, err);
  672. Vector<uint8_t> buff;
  673. buff.resize(len);
  674. err = encode_variant(p_custom_features, buff.ptrw(), len, false);
  675. ERR_FAIL_COND_V(err != OK, err);
  676. file->store_32(len);
  677. file->store_buffer(buff.ptr(), buff.size());
  678. } else {
  679. file->store_32(count); //store how many properties are saved
  680. }
  681. for (const KeyValue<String, List<String>> &E : props) {
  682. for (const String &key : E.value) {
  683. String k = key;
  684. if (!E.key.is_empty()) {
  685. k = E.key + "/" + k;
  686. }
  687. Variant value;
  688. if (p_custom.has(k)) {
  689. value = p_custom[k];
  690. } else {
  691. value = get(k);
  692. }
  693. file->store_pascal_string(k);
  694. int len;
  695. err = encode_variant(value, nullptr, len, true);
  696. ERR_FAIL_COND_V_MSG(err != OK, ERR_INVALID_DATA, "Error when trying to encode Variant.");
  697. Vector<uint8_t> buff;
  698. buff.resize(len);
  699. err = encode_variant(value, buff.ptrw(), len, true);
  700. ERR_FAIL_COND_V_MSG(err != OK, ERR_INVALID_DATA, "Error when trying to encode Variant.");
  701. file->store_32(len);
  702. file->store_buffer(buff.ptr(), buff.size());
  703. }
  704. }
  705. return OK;
  706. }
  707. Error ProjectSettings::_save_settings_text(const String &p_file, const RBMap<String, List<String>> &props, const CustomMap &p_custom, const String &p_custom_features) {
  708. Error err;
  709. Ref<FileAccess> file = FileAccess::open(p_file, FileAccess::WRITE, &err);
  710. ERR_FAIL_COND_V_MSG(err != OK, err, "Couldn't save project.godot - " + p_file + ".");
  711. file->store_line("; Engine configuration file.");
  712. file->store_line("; It's best edited using the editor UI and not directly,");
  713. file->store_line("; since the parameters that go here are not all obvious.");
  714. file->store_line(";");
  715. file->store_line("; Format:");
  716. file->store_line("; [section] ; section goes between []");
  717. file->store_line("; param=value ; assign values to parameters");
  718. file->store_line("");
  719. file->store_string("config_version=" + itos(CONFIG_VERSION) + "\n");
  720. if (!p_custom_features.is_empty()) {
  721. file->store_string("custom_features=\"" + p_custom_features + "\"\n");
  722. }
  723. file->store_string("\n");
  724. for (const KeyValue<String, List<String>> &E : props) {
  725. if (E.key != props.begin()->key) {
  726. file->store_string("\n");
  727. }
  728. if (!E.key.is_empty()) {
  729. file->store_string("[" + E.key + "]\n\n");
  730. }
  731. for (const String &F : E.value) {
  732. String key = F;
  733. if (!E.key.is_empty()) {
  734. key = E.key + "/" + key;
  735. }
  736. Variant value;
  737. if (p_custom.has(key)) {
  738. value = p_custom[key];
  739. } else {
  740. value = get(key);
  741. }
  742. String vstr;
  743. VariantWriter::write_to_string(value, vstr);
  744. file->store_string(F.property_name_encode() + "=" + vstr + "\n");
  745. }
  746. }
  747. return OK;
  748. }
  749. Error ProjectSettings::_save_custom_bnd(const String &p_file) { // add other params as dictionary and array?
  750. return save_custom(p_file);
  751. }
  752. Error ProjectSettings::save_custom(const String &p_path, const CustomMap &p_custom, const Vector<String> &p_custom_features, bool p_merge_with_current) {
  753. ERR_FAIL_COND_V_MSG(p_path.is_empty(), ERR_INVALID_PARAMETER, "Project settings save path cannot be empty.");
  754. PackedStringArray project_features = get_setting("application/config/features");
  755. // If there is no feature list currently present, force one to generate.
  756. if (project_features.is_empty()) {
  757. project_features = ProjectSettings::get_required_features();
  758. }
  759. // Check the rendering API.
  760. const String rendering_api = has_setting("rendering/quality/driver/driver_name") ? (String)get_setting("rendering/quality/driver/driver_name") : String();
  761. if (!rendering_api.is_empty()) {
  762. // Add the rendering API as a project feature if it doesn't already exist.
  763. if (!project_features.has(rendering_api)) {
  764. project_features.append(rendering_api);
  765. }
  766. }
  767. // Check for the existence of a csproj file.
  768. if (FileAccess::exists(get_resource_path().plus_file(get_safe_project_name() + ".csproj"))) {
  769. // If there is a csproj file, add the C# feature if it doesn't already exist.
  770. if (!project_features.has("C#")) {
  771. project_features.append("C#");
  772. }
  773. } else {
  774. // If there isn't a csproj file, remove the C# feature if it exists.
  775. if (project_features.has("C#")) {
  776. project_features.remove_at(project_features.find("C#"));
  777. }
  778. }
  779. project_features = _trim_to_supported_features(project_features);
  780. set_setting("application/config/features", project_features);
  781. RBSet<_VCSort> vclist;
  782. if (p_merge_with_current) {
  783. for (const KeyValue<StringName, VariantContainer> &G : props) {
  784. const VariantContainer *v = &G.value;
  785. if (v->hide_from_editor) {
  786. continue;
  787. }
  788. if (p_custom.has(G.key)) {
  789. continue;
  790. }
  791. _VCSort vc;
  792. vc.name = G.key; //*k;
  793. vc.order = v->order;
  794. vc.type = v->variant.get_type();
  795. vc.flags = PROPERTY_USAGE_EDITOR | PROPERTY_USAGE_STORAGE;
  796. if (v->variant == v->initial) {
  797. continue;
  798. }
  799. vclist.insert(vc);
  800. }
  801. }
  802. for (const KeyValue<String, Variant> &E : p_custom) {
  803. // Lookup global prop to store in the same order
  804. RBMap<StringName, VariantContainer>::Iterator global_prop = props.find(E.key);
  805. _VCSort vc;
  806. vc.name = E.key;
  807. vc.order = global_prop ? global_prop->value.order : 0xFFFFFFF;
  808. vc.type = E.value.get_type();
  809. vc.flags = PROPERTY_USAGE_STORAGE;
  810. vclist.insert(vc);
  811. }
  812. RBMap<String, List<String>> props;
  813. for (const _VCSort &E : vclist) {
  814. String category = E.name;
  815. String name = E.name;
  816. int div = category.find("/");
  817. if (div < 0) {
  818. category = "";
  819. } else {
  820. category = category.substr(0, div);
  821. name = name.substr(div + 1, name.size());
  822. }
  823. props[category].push_back(name);
  824. }
  825. String custom_features;
  826. for (int i = 0; i < p_custom_features.size(); i++) {
  827. if (i > 0) {
  828. custom_features += ",";
  829. }
  830. String f = p_custom_features[i].strip_edges().replace("\"", "");
  831. custom_features += f;
  832. }
  833. if (p_path.ends_with(".godot") || p_path.ends_with("override.cfg")) {
  834. return _save_settings_text(p_path, props, p_custom, custom_features);
  835. } else if (p_path.ends_with(".binary")) {
  836. return _save_settings_binary(p_path, props, p_custom, custom_features);
  837. } else {
  838. ERR_FAIL_V_MSG(ERR_FILE_UNRECOGNIZED, "Unknown config file format: " + p_path + ".");
  839. }
  840. }
  841. Variant _GLOBAL_DEF(const String &p_var, const Variant &p_default, bool p_restart_if_changed, bool p_ignore_value_in_docs, bool p_basic, bool p_internal) {
  842. Variant ret;
  843. if (!ProjectSettings::get_singleton()->has_setting(p_var)) {
  844. ProjectSettings::get_singleton()->set(p_var, p_default);
  845. }
  846. ret = ProjectSettings::get_singleton()->get(p_var);
  847. ProjectSettings::get_singleton()->set_initial_value(p_var, p_default);
  848. ProjectSettings::get_singleton()->set_builtin_order(p_var);
  849. ProjectSettings::get_singleton()->set_as_basic(p_var, p_basic);
  850. ProjectSettings::get_singleton()->set_restart_if_changed(p_var, p_restart_if_changed);
  851. ProjectSettings::get_singleton()->set_ignore_value_in_docs(p_var, p_ignore_value_in_docs);
  852. ProjectSettings::get_singleton()->set_as_internal(p_var, p_internal);
  853. return ret;
  854. }
  855. Vector<String> ProjectSettings::get_optimizer_presets() const {
  856. List<PropertyInfo> pi;
  857. ProjectSettings::get_singleton()->get_property_list(&pi);
  858. Vector<String> names;
  859. for (const PropertyInfo &E : pi) {
  860. if (!E.name.begins_with("optimizer_presets/")) {
  861. continue;
  862. }
  863. names.push_back(E.name.get_slicec('/', 1));
  864. }
  865. names.sort();
  866. return names;
  867. }
  868. void ProjectSettings::_add_property_info_bind(const Dictionary &p_info) {
  869. ERR_FAIL_COND(!p_info.has("name"));
  870. ERR_FAIL_COND(!p_info.has("type"));
  871. PropertyInfo pinfo;
  872. pinfo.name = p_info["name"];
  873. ERR_FAIL_COND(!props.has(pinfo.name));
  874. pinfo.type = Variant::Type(p_info["type"].operator int());
  875. ERR_FAIL_INDEX(pinfo.type, Variant::VARIANT_MAX);
  876. if (p_info.has("hint")) {
  877. pinfo.hint = PropertyHint(p_info["hint"].operator int());
  878. }
  879. if (p_info.has("hint_string")) {
  880. pinfo.hint_string = p_info["hint_string"];
  881. }
  882. set_custom_property_info(pinfo.name, pinfo);
  883. }
  884. void ProjectSettings::set_custom_property_info(const String &p_prop, const PropertyInfo &p_info) {
  885. ERR_FAIL_COND(!props.has(p_prop));
  886. custom_prop_info[p_prop] = p_info;
  887. custom_prop_info[p_prop].name = p_prop;
  888. }
  889. const HashMap<StringName, PropertyInfo> &ProjectSettings::get_custom_property_info() const {
  890. return custom_prop_info;
  891. }
  892. void ProjectSettings::set_disable_feature_overrides(bool p_disable) {
  893. disable_feature_overrides = p_disable;
  894. }
  895. bool ProjectSettings::is_using_datapack() const {
  896. return using_datapack;
  897. }
  898. bool ProjectSettings::property_can_revert(const String &p_name) {
  899. if (!props.has(p_name)) {
  900. return false;
  901. }
  902. return props[p_name].initial != props[p_name].variant;
  903. }
  904. Variant ProjectSettings::property_get_revert(const String &p_name) {
  905. if (!props.has(p_name)) {
  906. return Variant();
  907. }
  908. return props[p_name].initial;
  909. }
  910. void ProjectSettings::set_setting(const String &p_setting, const Variant &p_value) {
  911. set(p_setting, p_value);
  912. }
  913. Variant ProjectSettings::get_setting(const String &p_setting) const {
  914. return get(p_setting);
  915. }
  916. bool ProjectSettings::has_custom_feature(const String &p_feature) const {
  917. return custom_features.has(p_feature);
  918. }
  919. const HashMap<StringName, ProjectSettings::AutoloadInfo> &ProjectSettings::get_autoload_list() const {
  920. return autoloads;
  921. }
  922. void ProjectSettings::add_autoload(const AutoloadInfo &p_autoload) {
  923. ERR_FAIL_COND_MSG(p_autoload.name == StringName(), "Trying to add autoload with no name.");
  924. autoloads[p_autoload.name] = p_autoload;
  925. }
  926. void ProjectSettings::remove_autoload(const StringName &p_autoload) {
  927. ERR_FAIL_COND_MSG(!autoloads.has(p_autoload), "Trying to remove non-existent autoload.");
  928. autoloads.erase(p_autoload);
  929. }
  930. bool ProjectSettings::has_autoload(const StringName &p_autoload) const {
  931. return autoloads.has(p_autoload);
  932. }
  933. ProjectSettings::AutoloadInfo ProjectSettings::get_autoload(const StringName &p_name) const {
  934. ERR_FAIL_COND_V_MSG(!autoloads.has(p_name), AutoloadInfo(), "Trying to get non-existent autoload.");
  935. return autoloads[p_name];
  936. }
  937. void ProjectSettings::_bind_methods() {
  938. ClassDB::bind_method(D_METHOD("has_setting", "name"), &ProjectSettings::has_setting);
  939. ClassDB::bind_method(D_METHOD("set_setting", "name", "value"), &ProjectSettings::set_setting);
  940. ClassDB::bind_method(D_METHOD("get_setting", "name"), &ProjectSettings::get_setting);
  941. ClassDB::bind_method(D_METHOD("set_order", "name", "position"), &ProjectSettings::set_order);
  942. ClassDB::bind_method(D_METHOD("get_order", "name"), &ProjectSettings::get_order);
  943. ClassDB::bind_method(D_METHOD("set_initial_value", "name", "value"), &ProjectSettings::set_initial_value);
  944. ClassDB::bind_method(D_METHOD("add_property_info", "hint"), &ProjectSettings::_add_property_info_bind);
  945. ClassDB::bind_method(D_METHOD("clear", "name"), &ProjectSettings::clear);
  946. ClassDB::bind_method(D_METHOD("localize_path", "path"), &ProjectSettings::localize_path);
  947. ClassDB::bind_method(D_METHOD("globalize_path", "path"), &ProjectSettings::globalize_path);
  948. ClassDB::bind_method(D_METHOD("save"), &ProjectSettings::save);
  949. ClassDB::bind_method(D_METHOD("load_resource_pack", "pack", "replace_files", "offset"), &ProjectSettings::_load_resource_pack, DEFVAL(true), DEFVAL(0));
  950. ClassDB::bind_method(D_METHOD("property_can_revert", "name"), &ProjectSettings::property_can_revert);
  951. ClassDB::bind_method(D_METHOD("property_get_revert", "name"), &ProjectSettings::property_get_revert);
  952. ClassDB::bind_method(D_METHOD("save_custom", "file"), &ProjectSettings::_save_custom_bnd);
  953. }
  954. void ProjectSettings::_add_builtin_input_map() {
  955. if (InputMap::get_singleton()) {
  956. HashMap<String, List<Ref<InputEvent>>> builtins = InputMap::get_singleton()->get_builtins();
  957. for (KeyValue<String, List<Ref<InputEvent>>> &E : builtins) {
  958. Array events;
  959. // Convert list of input events into array
  960. for (List<Ref<InputEvent>>::Element *I = E.value.front(); I; I = I->next()) {
  961. events.push_back(I->get());
  962. }
  963. Dictionary action;
  964. action["deadzone"] = Variant(0.5f);
  965. action["events"] = events;
  966. String action_name = "input/" + E.key;
  967. GLOBAL_DEF_INTERNAL(action_name, action);
  968. input_presets.push_back(action_name);
  969. }
  970. }
  971. }
  972. ProjectSettings::ProjectSettings() {
  973. // Initialization of engine variables should be done in the setup() method,
  974. // so that the values can be overridden from project.godot or project.binary.
  975. singleton = this;
  976. GLOBAL_DEF_BASIC("application/config/name", "");
  977. GLOBAL_DEF_BASIC("application/config/name_localized", Dictionary());
  978. custom_prop_info["application/config/name_localized"] = PropertyInfo(Variant::DICTIONARY, "application/config/name_localized", PROPERTY_HINT_LOCALIZABLE_STRING);
  979. GLOBAL_DEF_BASIC("application/config/description", "");
  980. custom_prop_info["application/config/description"] = PropertyInfo(Variant::STRING, "application/config/description", PROPERTY_HINT_MULTILINE_TEXT);
  981. GLOBAL_DEF_BASIC("application/run/main_scene", "");
  982. custom_prop_info["application/run/main_scene"] = PropertyInfo(Variant::STRING, "application/run/main_scene", PROPERTY_HINT_FILE, "*.tscn,*.scn,*.res");
  983. GLOBAL_DEF("application/run/disable_stdout", false);
  984. GLOBAL_DEF("application/run/disable_stderr", false);
  985. GLOBAL_DEF_RST("application/config/use_hidden_project_data_directory", true);
  986. GLOBAL_DEF("application/config/use_custom_user_dir", false);
  987. GLOBAL_DEF("application/config/custom_user_dir_name", "");
  988. GLOBAL_DEF("application/config/project_settings_override", "");
  989. GLOBAL_DEF_BASIC("display/window/size/viewport_width", 1024);
  990. custom_prop_info["display/window/size/viewport_width"] = PropertyInfo(Variant::INT, "display/window/size/viewport_width", PROPERTY_HINT_RANGE, "0,7680,1,or_greater"); // 8K resolution
  991. GLOBAL_DEF_BASIC("display/window/size/viewport_height", 600);
  992. custom_prop_info["display/window/size/viewport_height"] = PropertyInfo(Variant::INT, "display/window/size/viewport_height", PROPERTY_HINT_RANGE, "0,4320,1,or_greater"); // 8K resolution
  993. GLOBAL_DEF_BASIC("display/window/size/resizable", true);
  994. GLOBAL_DEF_BASIC("display/window/size/borderless", false);
  995. GLOBAL_DEF_BASIC("display/window/size/fullscreen", false);
  996. GLOBAL_DEF("display/window/size/always_on_top", false);
  997. GLOBAL_DEF("display/window/size/window_width_override", 0);
  998. custom_prop_info["display/window/size/window_width_override"] = PropertyInfo(Variant::INT, "display/window/size/window_width_override", PROPERTY_HINT_RANGE, "0,7680,1,or_greater"); // 8K resolution
  999. GLOBAL_DEF("display/window/size/window_height_override", 0);
  1000. custom_prop_info["display/window/size/window_height_override"] = PropertyInfo(Variant::INT, "display/window/size/window_height_override", PROPERTY_HINT_RANGE, "0,4320,1,or_greater"); // 8K resolution
  1001. GLOBAL_DEF_BASIC("audio/buses/default_bus_layout", "res://default_bus_layout.tres");
  1002. custom_prop_info["audio/buses/default_bus_layout"] = PropertyInfo(Variant::STRING, "audio/buses/default_bus_layout", PROPERTY_HINT_FILE, "*.tres");
  1003. GLOBAL_DEF_RST("audio/general/2d_panning_strength", 1.0f);
  1004. custom_prop_info["audio/general/2d_panning_strength"] = PropertyInfo(Variant::FLOAT, "audio/general/2d_panning_strength", PROPERTY_HINT_RANGE, "0,4,0.01");
  1005. GLOBAL_DEF_RST("audio/general/3d_panning_strength", 1.0f);
  1006. custom_prop_info["audio/general/3d_panning_strength"] = PropertyInfo(Variant::FLOAT, "audio/general/3d_panning_strength", PROPERTY_HINT_RANGE, "0,4,0.01");
  1007. PackedStringArray extensions = PackedStringArray();
  1008. extensions.push_back("gd");
  1009. if (Engine::get_singleton()->has_singleton("GodotSharp")) {
  1010. extensions.push_back("cs");
  1011. }
  1012. extensions.push_back("gdshader");
  1013. GLOBAL_DEF("editor/run/main_run_args", "");
  1014. GLOBAL_DEF("editor/script/search_in_file_extensions", extensions);
  1015. custom_prop_info["editor/script/search_in_file_extensions"] = PropertyInfo(Variant::PACKED_STRING_ARRAY, "editor/script/search_in_file_extensions");
  1016. GLOBAL_DEF("editor/script/templates_search_path", "res://script_templates");
  1017. custom_prop_info["editor/script/templates_search_path"] = PropertyInfo(Variant::STRING, "editor/script/templates_search_path", PROPERTY_HINT_DIR);
  1018. _add_builtin_input_map();
  1019. // Keep the enum values in sync with the `DisplayServer::ScreenOrientation` enum.
  1020. custom_prop_info["display/window/handheld/orientation"] = PropertyInfo(Variant::INT, "display/window/handheld/orientation", PROPERTY_HINT_ENUM, "Landscape,Portrait,Reverse Landscape,Reverse Portrait,Sensor Landscape,Sensor Portrait,Sensor");
  1021. // Keep the enum values in sync with the `DisplayServer::VSyncMode` enum.
  1022. custom_prop_info["display/window/vsync/vsync_mode"] = PropertyInfo(Variant::INT, "display/window/vsync/vsync_mode", PROPERTY_HINT_ENUM, "Disabled,Enabled,Adaptive,Mailbox");
  1023. custom_prop_info["rendering/driver/threads/thread_model"] = PropertyInfo(Variant::INT, "rendering/driver/threads/thread_model", PROPERTY_HINT_ENUM, "Single-Unsafe,Single-Safe,Multi-Threaded");
  1024. GLOBAL_DEF("physics/2d/run_on_separate_thread", false);
  1025. GLOBAL_DEF("physics/3d/run_on_separate_thread", false);
  1026. GLOBAL_DEF("debug/settings/profiler/max_functions", 16384);
  1027. custom_prop_info["debug/settings/profiler/max_functions"] = PropertyInfo(Variant::INT, "debug/settings/profiler/max_functions", PROPERTY_HINT_RANGE, "128,65535,1");
  1028. GLOBAL_DEF("compression/formats/zstd/long_distance_matching", Compression::zstd_long_distance_matching);
  1029. custom_prop_info["compression/formats/zstd/long_distance_matching"] = PropertyInfo(Variant::BOOL, "compression/formats/zstd/long_distance_matching");
  1030. GLOBAL_DEF("compression/formats/zstd/compression_level", Compression::zstd_level);
  1031. custom_prop_info["compression/formats/zstd/compression_level"] = PropertyInfo(Variant::INT, "compression/formats/zstd/compression_level", PROPERTY_HINT_RANGE, "1,22,1");
  1032. GLOBAL_DEF("compression/formats/zstd/window_log_size", Compression::zstd_window_log_size);
  1033. custom_prop_info["compression/formats/zstd/window_log_size"] = PropertyInfo(Variant::INT, "compression/formats/zstd/window_log_size", PROPERTY_HINT_RANGE, "10,30,1");
  1034. GLOBAL_DEF("compression/formats/zlib/compression_level", Compression::zlib_level);
  1035. custom_prop_info["compression/formats/zlib/compression_level"] = PropertyInfo(Variant::INT, "compression/formats/zlib/compression_level", PROPERTY_HINT_RANGE, "-1,9,1");
  1036. GLOBAL_DEF("compression/formats/gzip/compression_level", Compression::gzip_level);
  1037. custom_prop_info["compression/formats/gzip/compression_level"] = PropertyInfo(Variant::INT, "compression/formats/gzip/compression_level", PROPERTY_HINT_RANGE, "-1,9,1");
  1038. // These properties will not show up in the dialog nor in the documentation. If you want to exclude whole groups, see _get_property_list() method.
  1039. GLOBAL_DEF_INTERNAL("application/config/features", PackedStringArray());
  1040. GLOBAL_DEF_INTERNAL("internationalization/locale/translation_remaps", PackedStringArray());
  1041. GLOBAL_DEF_INTERNAL("internationalization/locale/translations", PackedStringArray());
  1042. }
  1043. ProjectSettings::~ProjectSettings() {
  1044. singleton = nullptr;
  1045. }