project_settings.cpp 46 KB

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