project_settings.cpp 49 KB

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