project_settings.cpp 47 KB

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