project_settings.cpp 45 KB

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