project_settings.cpp 72 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888
  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/input/input_map.h"
  33. #include "core/io/config_file.h"
  34. #include "core/io/dir_access.h"
  35. #include "core/io/file_access.h"
  36. #include "core/io/file_access_pack.h"
  37. #include "core/io/marshalls.h"
  38. #include "core/io/resource_uid.h"
  39. #include "core/object/script_language.h"
  40. #include "core/templates/rb_set.h"
  41. #include "core/variant/typed_array.h"
  42. #include "core/variant/variant_parser.h"
  43. #include "core/version.h"
  44. #include "servers/rendering/rendering_server.h"
  45. #ifdef TOOLS_ENABLED
  46. #include "modules/modules_enabled.gen.h" // For mono.
  47. #endif // TOOLS_ENABLED
  48. ProjectSettings *ProjectSettings::get_singleton() {
  49. return singleton;
  50. }
  51. String ProjectSettings::get_project_data_dir_name() const {
  52. return project_data_dir_name;
  53. }
  54. String ProjectSettings::get_project_data_path() const {
  55. return "res://" + get_project_data_dir_name();
  56. }
  57. String ProjectSettings::get_resource_path() const {
  58. return resource_path;
  59. }
  60. // This returns paths like "res://.godot/imported".
  61. String ProjectSettings::get_imported_files_path() const {
  62. return get_project_data_path().path_join("imported");
  63. }
  64. #ifdef TOOLS_ENABLED
  65. // Returns the features that a project must have when opened with this build of Godot.
  66. // This is used by the project manager to provide the initial_settings for config/features.
  67. const PackedStringArray ProjectSettings::get_required_features() {
  68. PackedStringArray features;
  69. features.append(GODOT_VERSION_BRANCH);
  70. #ifdef REAL_T_IS_DOUBLE
  71. features.append("Double Precision");
  72. #endif
  73. return features;
  74. }
  75. // Returns the features supported by this build of Godot. Includes all required features.
  76. const PackedStringArray ProjectSettings::_get_supported_features() {
  77. PackedStringArray features = get_required_features();
  78. #ifdef LIBGODOT_ENABLED
  79. features.append("LibGodot");
  80. #endif
  81. #ifdef MODULE_MONO_ENABLED
  82. features.append("C#");
  83. #endif
  84. // Allow pinning to a specific patch number or build type by marking
  85. // them as supported. They're only used if the user adds them manually.
  86. features.append(GODOT_VERSION_BRANCH "." _MKSTR(GODOT_VERSION_PATCH));
  87. features.append(GODOT_VERSION_FULL_CONFIG);
  88. features.append(GODOT_VERSION_FULL_BUILD);
  89. #ifdef RD_ENABLED
  90. features.append("Forward Plus");
  91. features.append("Mobile");
  92. #endif
  93. #ifdef GLES3_ENABLED
  94. features.append("GL Compatibility");
  95. #endif
  96. return features;
  97. }
  98. // Returns the features that this project needs but this build of Godot lacks.
  99. const PackedStringArray ProjectSettings::get_unsupported_features(const PackedStringArray &p_project_features) {
  100. PackedStringArray unsupported_features;
  101. PackedStringArray supported_features = singleton->_get_supported_features();
  102. for (int i = 0; i < p_project_features.size(); i++) {
  103. if (!supported_features.has(p_project_features[i])) {
  104. // Temporary compatibility code to ease upgrade to 4.0 beta 2+.
  105. if (p_project_features[i].begins_with("Vulkan")) {
  106. continue;
  107. }
  108. unsupported_features.append(p_project_features[i]);
  109. }
  110. }
  111. unsupported_features.sort();
  112. return unsupported_features;
  113. }
  114. // Returns the features that both this project has and this build of Godot has, ensuring required features exist.
  115. const PackedStringArray ProjectSettings::_trim_to_supported_features(const PackedStringArray &p_project_features) {
  116. // Remove unsupported features if present.
  117. PackedStringArray features = PackedStringArray(p_project_features);
  118. PackedStringArray supported_features = _get_supported_features();
  119. for (int i = p_project_features.size() - 1; i > -1; i--) {
  120. if (!supported_features.has(p_project_features[i])) {
  121. features.remove_at(i);
  122. }
  123. }
  124. // Add required features if not present.
  125. PackedStringArray required_features = get_required_features();
  126. for (int i = 0; i < required_features.size(); i++) {
  127. if (!features.has(required_features[i])) {
  128. features.append(required_features[i]);
  129. }
  130. }
  131. features.sort();
  132. return features;
  133. }
  134. #endif // TOOLS_ENABLED
  135. String ProjectSettings::localize_path(const String &p_path) const {
  136. String path = p_path.simplify_path();
  137. if (resource_path.is_empty() || (path.is_absolute_path() && !path.begins_with(resource_path))) {
  138. return path;
  139. }
  140. // Check if we have a special path (like res://) or a protocol identifier.
  141. int p = path.find("://");
  142. bool found = false;
  143. if (p > 0) {
  144. found = true;
  145. for (int i = 0; i < p; i++) {
  146. if (!is_ascii_alphanumeric_char(path[i])) {
  147. found = false;
  148. break;
  149. }
  150. }
  151. }
  152. if (found) {
  153. return path;
  154. }
  155. Ref<DirAccess> dir = DirAccess::create(DirAccess::ACCESS_FILESYSTEM);
  156. if (dir->change_dir(path) == OK) {
  157. String cwd = dir->get_current_dir();
  158. cwd = cwd.replace_char('\\', '/');
  159. // Ensure that we end with a '/'.
  160. // This is important to ensure that we do not wrongly localize the resource path
  161. // in an absolute path that just happens to contain this string but points to a
  162. // different folder (e.g. "/my/project" as resource_path would be contained in
  163. // "/my/project_data", even though the latter is not part of res://.
  164. // `path_join("")` is an easy way to ensure we have a trailing '/'.
  165. const String res_path = resource_path.path_join("");
  166. // DirAccess::get_current_dir() is not guaranteed to return a path that with a trailing '/',
  167. // so we must make sure we have it as well in order to compare with 'res_path'.
  168. cwd = cwd.path_join("");
  169. if (!cwd.begins_with(res_path)) {
  170. return path;
  171. }
  172. return cwd.replace_first(res_path, "res://");
  173. } else {
  174. int sep = path.rfind_char('/');
  175. if (sep == -1) {
  176. return "res://" + path;
  177. }
  178. String parent = path.substr(0, sep);
  179. String plocal = localize_path(parent);
  180. if (plocal.is_empty()) {
  181. return "";
  182. }
  183. // Only strip the starting '/' from 'path' if its parent ('plocal') ends with '/'
  184. if (plocal[plocal.length() - 1] == '/') {
  185. sep += 1;
  186. }
  187. return plocal + path.substr(sep);
  188. }
  189. }
  190. void ProjectSettings::set_initial_value(const String &p_name, const Variant &p_value) {
  191. ERR_FAIL_COND_MSG(!props.has(p_name), vformat("Request for nonexistent project setting: '%s'.", p_name));
  192. // Duplicate so that if value is array or dictionary, changing the setting will not change the stored initial value.
  193. props[p_name].initial = p_value.duplicate();
  194. }
  195. void ProjectSettings::set_restart_if_changed(const String &p_name, bool p_restart) {
  196. ERR_FAIL_COND_MSG(!props.has(p_name), vformat("Request for nonexistent project setting: '%s'.", p_name));
  197. props[p_name].restart_if_changed = p_restart;
  198. }
  199. void ProjectSettings::set_as_basic(const String &p_name, bool p_basic) {
  200. ERR_FAIL_COND_MSG(!props.has(p_name), vformat("Request for nonexistent project setting: '%s'.", p_name));
  201. props[p_name].basic = p_basic;
  202. }
  203. void ProjectSettings::set_as_internal(const String &p_name, bool p_internal) {
  204. ERR_FAIL_COND_MSG(!props.has(p_name), vformat("Request for nonexistent project setting: '%s'.", p_name));
  205. props[p_name].internal = p_internal;
  206. }
  207. void ProjectSettings::set_ignore_value_in_docs(const String &p_name, bool p_ignore) {
  208. ERR_FAIL_COND_MSG(!props.has(p_name), vformat("Request for nonexistent project setting: '%s'.", p_name));
  209. #ifdef DEBUG_ENABLED
  210. props[p_name].ignore_value_in_docs = p_ignore;
  211. #endif // DEBUG_ENABLED
  212. }
  213. bool ProjectSettings::get_ignore_value_in_docs(const String &p_name) const {
  214. ERR_FAIL_COND_V_MSG(!props.has(p_name), false, vformat("Request for nonexistent project setting: '%s'.", p_name));
  215. #ifdef DEBUG_ENABLED
  216. return props[p_name].ignore_value_in_docs;
  217. #else
  218. return false;
  219. #endif // DEBUG_ENABLED
  220. }
  221. void ProjectSettings::add_hidden_prefix(const String &p_prefix) {
  222. ERR_FAIL_COND_MSG(hidden_prefixes.has(p_prefix), vformat("Hidden prefix '%s' already exists.", p_prefix));
  223. hidden_prefixes.push_back(p_prefix);
  224. }
  225. String ProjectSettings::globalize_path(const String &p_path) const {
  226. if (p_path.begins_with("res://")) {
  227. if (!resource_path.is_empty()) {
  228. return p_path.replace("res:/", resource_path);
  229. }
  230. return p_path.replace("res://", "");
  231. } else if (p_path.begins_with("uid://")) {
  232. const String path = ResourceUID::uid_to_path(p_path);
  233. if (!resource_path.is_empty()) {
  234. return path.replace("res:/", resource_path);
  235. }
  236. return path.replace("res://", "");
  237. } else if (p_path.begins_with("user://")) {
  238. String data_dir = OS::get_singleton()->get_user_data_dir();
  239. if (!data_dir.is_empty()) {
  240. return p_path.replace("user:/", data_dir);
  241. }
  242. return p_path.replace("user://", "");
  243. }
  244. return p_path;
  245. }
  246. bool ProjectSettings::_set(const StringName &p_name, const Variant &p_value) {
  247. _THREAD_SAFE_METHOD_
  248. // Early return if value hasn't changed (unless it's being deleted)
  249. if (p_value.get_type() != Variant::NIL) {
  250. if (props.has(p_name) && props[p_name].variant == p_value) {
  251. return true;
  252. }
  253. }
  254. if (p_value.get_type() == Variant::NIL) {
  255. props.erase(p_name);
  256. if (p_name.operator String().begins_with("autoload/")) {
  257. String node_name = p_name.operator String().get_slicec('/', 1);
  258. if (autoloads.has(node_name)) {
  259. remove_autoload(node_name);
  260. }
  261. } else if (p_name.operator String().begins_with("global_group/")) {
  262. String group_name = p_name.operator String().get_slicec('/', 1);
  263. if (global_groups.has(group_name)) {
  264. remove_global_group(group_name);
  265. }
  266. }
  267. } else {
  268. if (p_name == CoreStringName(_custom_features)) {
  269. Vector<String> custom_feature_array = String(p_value).split(",");
  270. for (int i = 0; i < custom_feature_array.size(); i++) {
  271. custom_features.insert(custom_feature_array[i]);
  272. }
  273. _version++;
  274. _queue_changed(p_name);
  275. return true;
  276. }
  277. { // Feature overrides.
  278. int dot = p_name.operator String().find_char('.');
  279. if (dot != -1) {
  280. Vector<String> s = p_name.operator String().split(".");
  281. for (int i = 1; i < s.size(); i++) {
  282. String feature = s[i].strip_edges();
  283. Pair<StringName, StringName> feature_override(feature, p_name);
  284. if (!feature_overrides.has(s[0])) {
  285. feature_overrides[s[0]] = LocalVector<Pair<StringName, StringName>>();
  286. }
  287. feature_overrides[s[0]].push_back(feature_override);
  288. }
  289. }
  290. }
  291. if (props.has(p_name)) {
  292. props[p_name].variant = p_value;
  293. } else {
  294. props[p_name] = VariantContainer(p_value, last_order++);
  295. }
  296. if (p_name.operator String().begins_with("autoload_prepend/")) {
  297. String node_name = p_name.operator String().get_slicec('/', 1);
  298. AutoloadInfo autoload;
  299. autoload.name = node_name;
  300. String path = p_value;
  301. if (path.begins_with("*")) {
  302. autoload.is_singleton = true;
  303. autoload.path = path.substr(1).simplify_path();
  304. } else {
  305. autoload.path = path.simplify_path();
  306. }
  307. add_autoload(autoload, true);
  308. } else if (p_name.operator String().begins_with("autoload/")) {
  309. String node_name = p_name.operator String().get_slicec('/', 1);
  310. AutoloadInfo autoload;
  311. autoload.name = node_name;
  312. String path = p_value;
  313. if (path.begins_with("*")) {
  314. autoload.is_singleton = true;
  315. autoload.path = path.substr(1).simplify_path();
  316. } else {
  317. autoload.path = path.simplify_path();
  318. }
  319. add_autoload(autoload);
  320. } else if (p_name.operator String().begins_with("global_group/")) {
  321. String group_name = p_name.operator String().get_slicec('/', 1);
  322. add_global_group(group_name, p_value);
  323. }
  324. }
  325. _version++;
  326. _queue_changed(p_name);
  327. return true;
  328. }
  329. bool ProjectSettings::_get(const StringName &p_name, Variant &r_ret) const {
  330. _THREAD_SAFE_METHOD_
  331. if (!props.has(p_name)) {
  332. return false;
  333. }
  334. r_ret = props[p_name].variant;
  335. return true;
  336. }
  337. Variant ProjectSettings::get_setting_with_override_and_custom_features(const StringName &p_name, const Vector<String> &p_features) const {
  338. _THREAD_SAFE_METHOD_
  339. StringName name = p_name;
  340. if (feature_overrides.has(name)) {
  341. const LocalVector<Pair<StringName, StringName>> &overrides = feature_overrides[name];
  342. for (uint32_t i = 0; i < overrides.size(); i++) {
  343. if (p_features.has(String(overrides[i].first).to_lower())) {
  344. if (props.has(overrides[i].second)) {
  345. name = overrides[i].second;
  346. break;
  347. }
  348. }
  349. }
  350. }
  351. if (!props.has(name)) {
  352. WARN_PRINT("Property not found: " + String(name));
  353. return Variant();
  354. }
  355. return props[name].variant;
  356. }
  357. Variant ProjectSettings::get_setting_with_override(const StringName &p_name) const {
  358. _THREAD_SAFE_METHOD_
  359. const LocalVector<Pair<StringName, StringName>> *overrides = feature_overrides.getptr(p_name);
  360. if (overrides) {
  361. for (uint32_t i = 0; i < overrides->size(); i++) {
  362. if (!OS::get_singleton()->has_feature((*overrides)[i].first)) {
  363. continue;
  364. }
  365. // Custom features are checked in OS.has_feature() already. No need to check twice.
  366. const RBMap<StringName, VariantContainer>::Element *override_prop = props.find((*overrides)[i].second);
  367. if (override_prop) {
  368. return override_prop->get().variant;
  369. }
  370. }
  371. }
  372. const RBMap<StringName, VariantContainer>::Element *prop = props.find(p_name);
  373. if (!prop) {
  374. WARN_PRINT(vformat("Property not found: '%s'.", p_name));
  375. return Variant();
  376. }
  377. return prop->get().variant;
  378. }
  379. struct _VCSort {
  380. String name;
  381. Variant::Type type = Variant::VARIANT_MAX;
  382. int order = 0;
  383. uint32_t flags = 0;
  384. bool operator<(const _VCSort &p_vcs) const { return order == p_vcs.order ? name < p_vcs.name : order < p_vcs.order; }
  385. };
  386. void ProjectSettings::_get_property_list(List<PropertyInfo> *p_list) const {
  387. _THREAD_SAFE_METHOD_
  388. RBSet<_VCSort> vclist;
  389. HashMap<String, LocalVector<_VCSort>> setting_overrides;
  390. for (const KeyValue<StringName, VariantContainer> &E : props) {
  391. const VariantContainer *v = &E.value;
  392. if (v->hide_from_editor) {
  393. continue;
  394. }
  395. _VCSort vc;
  396. vc.name = E.key;
  397. vc.order = v->order;
  398. vc.type = v->variant.get_type();
  399. bool internal = v->internal;
  400. if (!internal) {
  401. for (const String &F : hidden_prefixes) {
  402. if (vc.name.begins_with(F)) {
  403. internal = true;
  404. break;
  405. }
  406. }
  407. }
  408. if (internal) {
  409. vc.flags = PROPERTY_USAGE_STORAGE;
  410. } else {
  411. vc.flags = PROPERTY_USAGE_EDITOR | PROPERTY_USAGE_STORAGE;
  412. }
  413. if (v->internal) {
  414. vc.flags |= PROPERTY_USAGE_INTERNAL;
  415. }
  416. if (v->basic) {
  417. vc.flags |= PROPERTY_USAGE_EDITOR_BASIC_SETTING;
  418. }
  419. if (v->restart_if_changed) {
  420. vc.flags |= PROPERTY_USAGE_RESTART_IF_CHANGED;
  421. }
  422. int dot = vc.name.rfind_char('.');
  423. if (dot != -1) {
  424. StringName n = vc.name.substr(0, dot);
  425. if (props.has(n)) {
  426. // Property is an override.
  427. setting_overrides[n].push_back(vc);
  428. } else {
  429. vclist.insert(vc);
  430. }
  431. } else {
  432. vclist.insert(vc);
  433. }
  434. }
  435. for (const _VCSort &base : vclist) {
  436. if (custom_prop_info.has(base.name)) {
  437. PropertyInfo pi = custom_prop_info[base.name];
  438. pi.name = base.name;
  439. pi.usage = base.flags;
  440. p_list->push_back(pi);
  441. #ifdef TOOLS_ENABLED
  442. } else if (base.name.begins_with(EDITOR_SETTING_OVERRIDE_PREFIX)) {
  443. PropertyInfo info(base.type, base.name, PROPERTY_HINT_NONE, "", base.flags);
  444. const PropertyInfo *pi = editor_settings_info.getptr(base.name.trim_prefix(EDITOR_SETTING_OVERRIDE_PREFIX));
  445. if (pi) {
  446. info.usage = pi->usage;
  447. info.hint = pi->hint;
  448. info.hint_string = pi->hint_string;
  449. }
  450. p_list->push_back(info);
  451. #endif
  452. } else {
  453. p_list->push_back(PropertyInfo(base.type, base.name, PROPERTY_HINT_NONE, "", base.flags));
  454. }
  455. if (setting_overrides.has(base.name)) {
  456. for (const _VCSort &over : setting_overrides.get(base.name)) {
  457. if (custom_prop_info.has(over.name)) {
  458. PropertyInfo pi = custom_prop_info[over.name];
  459. pi.name = over.name;
  460. pi.usage = over.flags;
  461. p_list->push_back(pi);
  462. } else if (custom_prop_info.has(base.name)) {
  463. // Fallback to base property info.
  464. PropertyInfo pi = custom_prop_info[base.name];
  465. pi.name = over.name;
  466. pi.usage = over.flags;
  467. p_list->push_back(pi);
  468. } else {
  469. p_list->push_back(PropertyInfo(over.type, over.name, PROPERTY_HINT_NONE, "", over.flags));
  470. }
  471. }
  472. }
  473. }
  474. }
  475. void ProjectSettings::_queue_changed(const StringName &p_name) {
  476. changed_settings.insert(p_name);
  477. if (!MessageQueue::get_singleton() || MessageQueue::get_singleton()->get_max_buffer_usage() == 0) {
  478. return;
  479. }
  480. // Only queue the deferred call once per frame.
  481. if (!is_changed) {
  482. is_changed = true;
  483. callable_mp(this, &ProjectSettings::_emit_changed).call_deferred();
  484. }
  485. }
  486. void ProjectSettings::_emit_changed() {
  487. if (!is_changed) {
  488. return;
  489. }
  490. is_changed = false;
  491. // Emit the general settings_changed signal to indicate changes are complete.
  492. emit_signal("settings_changed");
  493. // Clear the changed settings after emitting the signal
  494. changed_settings.clear();
  495. }
  496. bool ProjectSettings::load_resource_pack(const String &p_pack, bool p_replace_files, int p_offset) {
  497. return ProjectSettings::_load_resource_pack(p_pack, p_replace_files, p_offset, false);
  498. }
  499. bool ProjectSettings::_load_resource_pack(const String &p_pack, bool p_replace_files, int p_offset, bool p_main_pack) {
  500. if (PackedData::get_singleton()->is_disabled()) {
  501. return false;
  502. }
  503. if (p_pack == "res://") {
  504. // Loading the resource directory as a pack source is reserved for internal use only.
  505. return false;
  506. }
  507. if (!p_main_pack && !using_datapack && !OS::get_singleton()->get_resource_dir().is_empty()) {
  508. // Add the project's resource file system to PackedData so directory access keeps working when
  509. // the game is running without a main pack, like in the editor or on Android.
  510. PackedData::get_singleton()->add_pack_source(memnew(PackedSourceDirectory));
  511. PackedData::get_singleton()->add_pack("res://", false, 0);
  512. DirAccess::make_default<DirAccessPack>(DirAccess::ACCESS_RESOURCES);
  513. using_datapack = true;
  514. }
  515. bool ok = PackedData::get_singleton()->add_pack(p_pack, p_replace_files, p_offset) == OK;
  516. if (!ok) {
  517. return false;
  518. }
  519. if (project_loaded) {
  520. // This pack may have declared new global classes (make sure they are picked up).
  521. refresh_global_class_list();
  522. // This pack may have defined new UIDs, make sure they are cached.
  523. ResourceUID::get_singleton()->load_from_cache(false);
  524. }
  525. // If the data pack was found, all directory access will be from here.
  526. if (!using_datapack) {
  527. DirAccess::make_default<DirAccessPack>(DirAccess::ACCESS_RESOURCES);
  528. using_datapack = true;
  529. }
  530. return true;
  531. }
  532. void ProjectSettings::_convert_to_last_version(int p_from_version) {
  533. #ifndef DISABLE_DEPRECATED
  534. if (p_from_version <= 3) {
  535. // Converts the actions from array to dictionary (array of events to dictionary with deadzone + events)
  536. for (KeyValue<StringName, ProjectSettings::VariantContainer> &E : props) {
  537. Variant value = E.value.variant;
  538. if (String(E.key).begins_with("input/") && value.get_type() == Variant::ARRAY) {
  539. Array array = value;
  540. Dictionary action;
  541. action["deadzone"] = Variant(0.5f);
  542. action["events"] = array;
  543. E.value.variant = action;
  544. }
  545. }
  546. } else if (p_from_version <= 6) {
  547. // Check if we still have legacy boot splash (removed in 4.6), map it to new project setting, then remove legacy setting.
  548. if (has_setting("application/boot_splash/fullsize")) {
  549. set_setting("application/boot_splash/stretch_mode", RenderingServer::map_scaling_option_to_stretch_mode(get_setting("application/boot_splash/fullsize")));
  550. set_setting("application/boot_splash/fullsize", Variant());
  551. }
  552. }
  553. #endif // DISABLE_DEPRECATED
  554. }
  555. /*
  556. * This method is responsible for loading a project.godot file and/or data file
  557. * using the following merit order:
  558. * - If using NetworkClient, try to lookup project file or fail.
  559. * - If --main-pack was passed by the user (`p_main_pack`), load it or fail.
  560. * - Search for project PCKs automatically. For each step we try loading a potential
  561. * PCK, and if it doesn't work, we proceed to the next step. If any step succeeds,
  562. * we try loading the project settings, and abort if it fails. Steps:
  563. * o Bundled PCK in the executable.
  564. * o [macOS only] PCK with same basename as the binary in the .app resource dir.
  565. * o PCK with same basename as the binary in the binary's directory. We handle both
  566. * changing the extension to '.pck' (e.g. 'win_game.exe' -> 'win_game.pck') and
  567. * appending '.pck' to the binary name (e.g. 'linux_game' -> 'linux_game.pck').
  568. * o PCK with the same basename as the binary in the current working directory.
  569. * Same as above for the two possible PCK file names.
  570. * - On Android, look for 'assets.sparsepck' and try loading it, if it doesn't work,
  571. * proceed to the next step.
  572. * - On relevant platforms (Android/iOS), lookup project file in OS resource path.
  573. * If found, load it or fail.
  574. * - Lookup project file in passed `p_path` (--path passed by the user), i.e. we
  575. * are running from source code.
  576. * If not found and `p_upwards` is true, look for project files in parent folders
  577. * up to the system root (used to run a game from command line while in a subfolder).
  578. * If a project file is found, load it or fail.
  579. * If nothing was found, error out.
  580. */
  581. Error ProjectSettings::_setup(const String &p_path, const String &p_main_pack, bool p_upwards, bool p_ignore_override) {
  582. if (!OS::get_singleton()->get_resource_dir().is_empty()) {
  583. // OS will call ProjectSettings->get_resource_path which will be empty if not overridden!
  584. // If the OS would rather use a specific location, then it will not be empty.
  585. resource_path = OS::get_singleton()->get_resource_dir().replace_char('\\', '/');
  586. if (!resource_path.is_empty() && resource_path[resource_path.length() - 1] == '/') {
  587. resource_path = resource_path.substr(0, resource_path.length() - 1); // Chop end.
  588. }
  589. }
  590. // Attempt with a user-defined main pack first
  591. if (!p_main_pack.is_empty()) {
  592. bool ok = _load_resource_pack(p_main_pack, false, 0, true);
  593. ERR_FAIL_COND_V_MSG(!ok, ERR_CANT_OPEN, vformat("Cannot open resource pack '%s'.", p_main_pack));
  594. Error err = _load_settings_text_or_binary("res://project.godot", "res://project.binary");
  595. #ifdef OVERRIDE_ENABLED
  596. if (err == OK && !p_ignore_override) {
  597. // Load override from location of the main pack
  598. // Optional, we don't mind if it fails
  599. bool disable_override = GLOBAL_GET("application/config/disable_project_settings_override");
  600. if (!disable_override) {
  601. _load_settings_text(p_main_pack.get_base_dir().path_join("override.cfg"));
  602. }
  603. }
  604. #endif // OVERRIDE_ENABLED
  605. return err;
  606. }
  607. String exec_path = OS::get_singleton()->get_executable_path();
  608. if (!exec_path.is_empty()) {
  609. // We do several tests sequentially until one succeeds to find a PCK,
  610. // and if so, we attempt loading it at the end.
  611. // Attempt with PCK bundled into executable.
  612. bool found = _load_resource_pack(exec_path, false, 0, true);
  613. // Attempt with exec_name.pck.
  614. // (This is the usual case when distributing a Godot game.)
  615. String exec_dir = exec_path.get_base_dir();
  616. String exec_filename = exec_path.get_file();
  617. String exec_basename = exec_filename.get_basename();
  618. // Based on the OS, it can be the exec path + '.pck' (Linux w/o extension, macOS in .app bundle)
  619. // or the exec path's basename + '.pck' (Windows).
  620. // We need to test both possibilities as extensions for Linux binaries are optional
  621. // (so both 'mygame.bin' and 'mygame' should be able to find 'mygame.pck').
  622. #if defined(MACOS_ENABLED) || defined(APPLE_EMBEDDED_ENABLED)
  623. if (!found) {
  624. // Attempt to load PCK from macOS .app bundle resources.
  625. found = _load_resource_pack(OS::get_singleton()->get_bundle_resource_dir().path_join(exec_basename + ".pck"), false, 0, true) || _load_resource_pack(OS::get_singleton()->get_bundle_resource_dir().path_join(exec_filename + ".pck"), false, 0, true);
  626. }
  627. #endif
  628. if (!found) {
  629. // Try to load data pack at the location of the executable.
  630. // As mentioned above, we have two potential names to attempt.
  631. found = _load_resource_pack(exec_dir.path_join(exec_basename + ".pck"), false, 0, true) || _load_resource_pack(exec_dir.path_join(exec_filename + ".pck"), false, 0, true);
  632. }
  633. if (!found) {
  634. // If we couldn't find them next to the executable, we attempt
  635. // the current working directory. Same story, two tests.
  636. found = _load_resource_pack(exec_basename + ".pck", false, 0, true) || _load_resource_pack(exec_filename + ".pck", false, 0, true);
  637. }
  638. // If we opened our package, try and load our project.
  639. if (found) {
  640. Error err = _load_settings_text_or_binary("res://project.godot", "res://project.binary");
  641. #ifdef OVERRIDE_ENABLED
  642. if (err == OK && !p_ignore_override) {
  643. // Load overrides from the PCK and the executable location.
  644. // Optional, we don't mind if either fails.
  645. bool disable_override = GLOBAL_GET("application/config/disable_project_settings_override");
  646. if (!disable_override) {
  647. _load_settings_text("res://override.cfg");
  648. _load_settings_text(exec_path.get_base_dir().path_join("override.cfg"));
  649. }
  650. }
  651. #endif // OVERRIDE_ENABLED
  652. return err;
  653. }
  654. }
  655. #ifdef ANDROID_ENABLED
  656. // Attempt to load sparse PCK assets.
  657. _load_resource_pack("res://assets.sparsepck", false, 0, true);
  658. #endif
  659. // Try to use the filesystem for files, according to OS.
  660. // (Only Android -when reading from PCK-.)
  661. if (!OS::get_singleton()->get_resource_dir().is_empty()) {
  662. Error err = _load_settings_text_or_binary("res://project.godot", "res://project.binary");
  663. #ifdef OVERRIDE_ENABLED
  664. if (err == OK && !p_ignore_override) {
  665. // Optional, we don't mind if it fails.
  666. bool disable_override = GLOBAL_GET("application/config/disable_project_settings_override");
  667. if (!disable_override) {
  668. _load_settings_text("res://override.cfg");
  669. }
  670. }
  671. #endif // OVERRIDE_ENABLED
  672. return err;
  673. }
  674. #if defined(MACOS_ENABLED) || defined(APPLE_EMBEDDED_ENABLED)
  675. // Attempt to load project file from macOS .app bundle resources.
  676. resource_path = OS::get_singleton()->get_bundle_resource_dir();
  677. if (!resource_path.is_empty()) {
  678. if (resource_path[resource_path.length() - 1] == '/') {
  679. resource_path = resource_path.substr(0, resource_path.length() - 1); // Chop end.
  680. }
  681. Ref<DirAccess> d = DirAccess::create(DirAccess::ACCESS_FILESYSTEM);
  682. ERR_FAIL_COND_V_MSG(d.is_null(), ERR_CANT_CREATE, vformat("Cannot create DirAccess for path '%s'.", resource_path));
  683. d->change_dir(resource_path);
  684. Error err;
  685. err = _load_settings_text_or_binary(resource_path.path_join("project.godot"), resource_path.path_join("project.binary"));
  686. if (err == OK && !p_ignore_override) {
  687. // Optional, we don't mind if it fails.
  688. #ifdef OVERRIDE_ENABLED
  689. bool disable_override = GLOBAL_GET("application/config/disable_project_settings_override");
  690. if (!disable_override) {
  691. _load_settings_text(resource_path.path_join("override.cfg"));
  692. }
  693. #endif // OVERRIDE_ENABLED
  694. return err;
  695. }
  696. }
  697. #endif // MACOS_ENABLED
  698. // Nothing was found, try to find a project file in provided path (`p_path`)
  699. // or, if requested (`p_upwards`) in parent directories.
  700. Ref<DirAccess> d = DirAccess::create(DirAccess::ACCESS_FILESYSTEM);
  701. ERR_FAIL_COND_V_MSG(d.is_null(), ERR_CANT_CREATE, vformat("Cannot create DirAccess for path '%s'.", p_path));
  702. d->change_dir(p_path);
  703. String current_dir = d->get_current_dir();
  704. bool found = false;
  705. Error err;
  706. while (true) {
  707. // Set the resource path early so things can be resolved when loading.
  708. resource_path = current_dir;
  709. resource_path = resource_path.replace_char('\\', '/'); // Windows path to Unix path just in case.
  710. err = _load_settings_text_or_binary(current_dir.path_join("project.godot"), current_dir.path_join("project.binary"));
  711. if (err == OK) {
  712. #ifdef OVERRIDE_ENABLED
  713. if (!p_ignore_override) {
  714. // Optional, we don't mind if it fails.
  715. bool disable_override = GLOBAL_GET("application/config/disable_project_settings_override");
  716. if (!disable_override) {
  717. _load_settings_text(current_dir.path_join("override.cfg"));
  718. }
  719. }
  720. #endif // OVERRIDE_ENABLED
  721. found = true;
  722. break;
  723. }
  724. #if defined(OVERRIDE_PATH_ENABLED)
  725. if (p_upwards) {
  726. // Try to load settings ascending through parent directories
  727. d->change_dir("..");
  728. if (d->get_current_dir() == current_dir) {
  729. break; // not doing anything useful
  730. }
  731. current_dir = d->get_current_dir();
  732. } else {
  733. #else
  734. {
  735. #endif
  736. break;
  737. }
  738. }
  739. if (!found) {
  740. return err;
  741. }
  742. if (resource_path.length() && resource_path[resource_path.length() - 1] == '/') {
  743. resource_path = resource_path.substr(0, resource_path.length() - 1); // Chop end.
  744. }
  745. return OK;
  746. }
  747. Error ProjectSettings::setup(const String &p_path, const String &p_main_pack, bool p_upwards, bool p_ignore_override) {
  748. Error err = _setup(p_path, p_main_pack, p_upwards, p_ignore_override);
  749. #ifdef OVERRIDE_ENABLED
  750. if (err == OK && !p_ignore_override) {
  751. bool disable_override = GLOBAL_GET("application/config/disable_project_settings_override");
  752. if (!disable_override) {
  753. String custom_settings = GLOBAL_GET("application/config/project_settings_override");
  754. if (!custom_settings.is_empty()) {
  755. _load_settings_text(custom_settings);
  756. }
  757. }
  758. }
  759. #endif
  760. // Updating the default value after the project settings have loaded.
  761. bool use_hidden_directory = GLOBAL_GET("application/config/use_hidden_project_data_directory");
  762. project_data_dir_name = (use_hidden_directory ? "." : "") + PROJECT_DATA_DIR_NAME_SUFFIX;
  763. // Using GLOBAL_GET on every block for compressing can be slow, so assigning here.
  764. Compression::zstd_long_distance_matching = GLOBAL_GET("compression/formats/zstd/long_distance_matching");
  765. Compression::zstd_level = GLOBAL_GET("compression/formats/zstd/compression_level");
  766. Compression::zstd_window_log_size = GLOBAL_GET("compression/formats/zstd/window_log_size");
  767. Compression::zlib_level = GLOBAL_GET("compression/formats/zlib/compression_level");
  768. Compression::gzip_level = GLOBAL_GET("compression/formats/gzip/compression_level");
  769. load_scene_groups_cache();
  770. project_loaded = err == OK;
  771. return err;
  772. }
  773. bool ProjectSettings::has_setting(const String &p_var) const {
  774. _THREAD_SAFE_METHOD_
  775. return props.has(p_var);
  776. }
  777. Error ProjectSettings::_load_settings_binary(const String &p_path) {
  778. Error err;
  779. Ref<FileAccess> f = FileAccess::open(p_path, FileAccess::READ, &err);
  780. if (err != OK) {
  781. return err;
  782. }
  783. uint8_t hdr[4];
  784. f->get_buffer(hdr, 4);
  785. 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).");
  786. uint32_t count = f->get_32();
  787. for (uint32_t i = 0; i < count; i++) {
  788. uint32_t slen = f->get_32();
  789. CharString cs;
  790. cs.resize_uninitialized(slen + 1);
  791. cs[slen] = 0;
  792. f->get_buffer((uint8_t *)cs.ptr(), slen);
  793. String key = String::utf8(cs.ptr(), slen);
  794. uint32_t vlen = f->get_32();
  795. Vector<uint8_t> d;
  796. d.resize(vlen);
  797. f->get_buffer(d.ptrw(), vlen);
  798. Variant value;
  799. err = decode_variant(value, d.ptr(), d.size(), nullptr, true);
  800. ERR_CONTINUE_MSG(err != OK, vformat("Error decoding property: '%s'.", key));
  801. set(key, value);
  802. }
  803. return OK;
  804. }
  805. Error ProjectSettings::_load_settings_text(const String &p_path) {
  806. Error err;
  807. Ref<FileAccess> f = FileAccess::open(p_path, FileAccess::READ, &err);
  808. if (f.is_null()) {
  809. // FIXME: Above 'err' error code is ERR_FILE_CANT_OPEN if the file is missing
  810. // This needs to be streamlined if we want decent error reporting
  811. return ERR_FILE_NOT_FOUND;
  812. }
  813. VariantParser::StreamFile stream;
  814. stream.f = f;
  815. String assign;
  816. Variant value;
  817. VariantParser::Tag next_tag;
  818. int lines = 0;
  819. String error_text;
  820. String section;
  821. int config_version = 0;
  822. while (true) {
  823. assign = Variant();
  824. next_tag.fields.clear();
  825. next_tag.name = String();
  826. err = VariantParser::parse_tag_assign_eof(&stream, lines, error_text, next_tag, assign, value, nullptr, true);
  827. if (err == ERR_FILE_EOF) {
  828. // If we're loading a project.godot from source code, we can operate some
  829. // ProjectSettings conversions if need be.
  830. _convert_to_last_version(config_version);
  831. last_save_time = FileAccess::get_modified_time(get_resource_path().path_join("project.godot"));
  832. return OK;
  833. }
  834. ERR_FAIL_COND_V_MSG(err != OK, err, vformat("Error parsing '%s' at line %d: %s File might be corrupted.", p_path, lines, error_text));
  835. if (!assign.is_empty()) {
  836. if (section.is_empty() && assign == "config_version") {
  837. config_version = value;
  838. 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));
  839. } else {
  840. if (section.is_empty()) {
  841. set(assign, value);
  842. } else {
  843. set(section + "/" + assign, value);
  844. }
  845. }
  846. } else if (!next_tag.name.is_empty()) {
  847. section = next_tag.name;
  848. }
  849. }
  850. }
  851. Error ProjectSettings::_load_settings_text_or_binary(const String &p_text_path, const String &p_bin_path) {
  852. // Attempt first to load the binary project.godot file.
  853. Error err = _load_settings_binary(p_bin_path);
  854. if (err == OK) {
  855. return OK;
  856. } else if (err != ERR_FILE_NOT_FOUND) {
  857. // If the file exists but can't be loaded, we want to know it.
  858. ERR_PRINT(vformat("Couldn't load file '%s', error code %d.", p_bin_path, err));
  859. }
  860. // Fallback to text-based project.godot file if binary was not found.
  861. err = _load_settings_text(p_text_path);
  862. if (err == OK) {
  863. #ifndef DISABLE_DEPRECATED
  864. const PackedStringArray features = get_setting("application/config/features");
  865. for (const String &feat : features) {
  866. if (feat.contains_char('.') && feat.get_slice_count(".") == 2) {
  867. int major_version = feat.get_slicec('.', 0).to_int();
  868. int minor_version = feat.get_slicec('.', 1).to_int();
  869. // Major version is irrelevant, but the extra check ensures that the feature is in fact a version string.
  870. if (major_version == 4 && minor_version < 6) {
  871. // Enable MeshInstance3D compat for projects created before 4.6.
  872. set_setting("animation/compatibility/default_parent_skeleton_in_mesh_instance_3d", true);
  873. }
  874. break;
  875. }
  876. }
  877. #endif
  878. return OK;
  879. } else if (err != ERR_FILE_NOT_FOUND) {
  880. ERR_PRINT(vformat("Couldn't load file '%s', error code %d.", p_text_path, err));
  881. }
  882. return err;
  883. }
  884. Error ProjectSettings::load_custom(const String &p_path) {
  885. if (p_path.ends_with(".binary")) {
  886. return _load_settings_binary(p_path);
  887. }
  888. return _load_settings_text(p_path);
  889. }
  890. int ProjectSettings::get_order(const String &p_name) const {
  891. ERR_FAIL_COND_V_MSG(!props.has(p_name), -1, vformat("Request for nonexistent project setting: '%s'.", p_name));
  892. return props[p_name].order;
  893. }
  894. void ProjectSettings::set_order(const String &p_name, int p_order) {
  895. ERR_FAIL_COND_MSG(!props.has(p_name), vformat("Request for nonexistent project setting: '%s'.", p_name));
  896. props[p_name].order = p_order;
  897. }
  898. void ProjectSettings::set_builtin_order(const String &p_name) {
  899. ERR_FAIL_COND_MSG(!props.has(p_name), vformat("Request for nonexistent project setting: '%s'.", p_name));
  900. if (props[p_name].order >= NO_BUILTIN_ORDER_BASE) {
  901. props[p_name].order = last_builtin_order++;
  902. }
  903. }
  904. bool ProjectSettings::is_builtin_setting(const String &p_name) const {
  905. // Return true because a false negative is worse than a false positive.
  906. ERR_FAIL_COND_V_MSG(!props.has(p_name), true, vformat("Request for nonexistent project setting: '%s'.", p_name));
  907. return props[p_name].order < NO_BUILTIN_ORDER_BASE;
  908. }
  909. void ProjectSettings::clear(const String &p_name) {
  910. ERR_FAIL_COND_MSG(!props.has(p_name), vformat("Request for nonexistent project setting: '%s'.", p_name));
  911. props.erase(p_name);
  912. }
  913. Error ProjectSettings::save() {
  914. Error error = save_custom(get_resource_path().path_join("project.godot"));
  915. if (error == OK) {
  916. last_save_time = FileAccess::get_modified_time(get_resource_path().path_join("project.godot"));
  917. }
  918. return error;
  919. }
  920. 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) {
  921. Error err;
  922. Ref<FileAccess> file = FileAccess::open(p_file, FileAccess::WRITE, &err);
  923. ERR_FAIL_COND_V_MSG(err != OK, err, vformat("Couldn't save project.binary at '%s'.", p_file));
  924. uint8_t hdr[4] = { 'E', 'C', 'F', 'G' };
  925. file->store_buffer(hdr, 4);
  926. int count = 0;
  927. for (const KeyValue<String, List<String>> &E : p_props) {
  928. count += E.value.size();
  929. }
  930. if (!p_custom_features.is_empty()) {
  931. // Store how many properties are saved, add one for custom features, which must always go first.
  932. file->store_32(uint32_t(count + 1));
  933. String key = CoreStringName(_custom_features);
  934. file->store_pascal_string(key);
  935. int len;
  936. err = encode_variant(p_custom_features, nullptr, len, false);
  937. ERR_FAIL_COND_V(err != OK, err);
  938. Vector<uint8_t> buff;
  939. buff.resize(len);
  940. err = encode_variant(p_custom_features, buff.ptrw(), len, false);
  941. ERR_FAIL_COND_V(err != OK, err);
  942. file->store_32(uint32_t(len));
  943. file->store_buffer(buff.ptr(), buff.size());
  944. } else {
  945. // Store how many properties are saved.
  946. file->store_32(uint32_t(count));
  947. }
  948. for (const KeyValue<String, List<String>> &E : p_props) {
  949. for (const String &key : E.value) {
  950. String k = key;
  951. if (!E.key.is_empty()) {
  952. k = E.key + "/" + k;
  953. }
  954. Variant value;
  955. if (p_custom.has(k)) {
  956. value = p_custom[k];
  957. } else {
  958. value = get(k);
  959. }
  960. file->store_pascal_string(k);
  961. int len;
  962. err = encode_variant(value, nullptr, len, true);
  963. ERR_FAIL_COND_V_MSG(err != OK, ERR_INVALID_DATA, "Error when trying to encode Variant.");
  964. Vector<uint8_t> buff;
  965. buff.resize(len);
  966. err = encode_variant(value, buff.ptrw(), len, true);
  967. ERR_FAIL_COND_V_MSG(err != OK, ERR_INVALID_DATA, "Error when trying to encode Variant.");
  968. file->store_32(uint32_t(len));
  969. file->store_buffer(buff.ptr(), buff.size());
  970. }
  971. }
  972. return OK;
  973. }
  974. 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) {
  975. Error err;
  976. Ref<FileAccess> file = FileAccess::open(p_file, FileAccess::WRITE, &err);
  977. ERR_FAIL_COND_V_MSG(err != OK, err, vformat("Couldn't save project.godot - %s.", p_file));
  978. file->store_line("; Engine configuration file.");
  979. file->store_line("; It's best edited using the editor UI and not directly,");
  980. file->store_line("; since the parameters that go here are not all obvious.");
  981. file->store_line(";");
  982. file->store_line("; Format:");
  983. file->store_line("; [section] ; section goes between []");
  984. file->store_line("; param=value ; assign values to parameters");
  985. file->store_line("");
  986. file->store_string("config_version=" + itos(CONFIG_VERSION) + "\n");
  987. if (!p_custom_features.is_empty()) {
  988. file->store_string("custom_features=\"" + p_custom_features + "\"\n");
  989. }
  990. file->store_string("\n");
  991. for (const KeyValue<String, List<String>> &E : p_props) {
  992. if (E.key != p_props.begin()->key) {
  993. file->store_string("\n");
  994. }
  995. if (!E.key.is_empty()) {
  996. file->store_string("[" + E.key + "]\n\n");
  997. }
  998. for (const String &F : E.value) {
  999. String key = F;
  1000. if (!E.key.is_empty()) {
  1001. key = E.key + "/" + key;
  1002. }
  1003. Variant value;
  1004. if (p_custom.has(key)) {
  1005. value = p_custom[key];
  1006. } else {
  1007. value = get(key);
  1008. }
  1009. String vstr;
  1010. VariantWriter::write_to_string(value, vstr);
  1011. file->store_string(F.property_name_encode() + "=" + vstr + "\n");
  1012. }
  1013. }
  1014. return OK;
  1015. }
  1016. Error ProjectSettings::_save_custom_bnd(const String &p_file) { // add other params as dictionary and array?
  1017. return save_custom(p_file);
  1018. }
  1019. #ifdef TOOLS_ENABLED
  1020. bool _csproj_exists(const String &p_root_dir) {
  1021. Ref<DirAccess> dir = DirAccess::open(p_root_dir);
  1022. ERR_FAIL_COND_V(dir.is_null(), false);
  1023. dir->list_dir_begin();
  1024. String file_name = dir->_get_next();
  1025. while (file_name != "") {
  1026. if (!dir->current_is_dir() && file_name.get_extension() == "csproj") {
  1027. return true;
  1028. }
  1029. file_name = dir->_get_next();
  1030. }
  1031. return false;
  1032. }
  1033. #endif // TOOLS_ENABLED
  1034. Error ProjectSettings::save_custom(const String &p_path, const CustomMap &p_custom, const Vector<String> &p_custom_features, bool p_merge_with_current) {
  1035. ERR_FAIL_COND_V_MSG(p_path.is_empty(), ERR_INVALID_PARAMETER, "Project settings save path cannot be empty.");
  1036. #ifdef TOOLS_ENABLED
  1037. PackedStringArray project_features = get_setting("application/config/features");
  1038. // If there is no feature list currently present, force one to generate.
  1039. if (project_features.is_empty()) {
  1040. project_features = ProjectSettings::get_required_features();
  1041. }
  1042. // Check the rendering API.
  1043. const String rendering_api = has_setting("rendering/renderer/rendering_method") ? (String)get_setting("rendering/renderer/rendering_method") : String();
  1044. if (!rendering_api.is_empty()) {
  1045. // Add the rendering API as a project feature if it doesn't already exist.
  1046. if (!project_features.has(rendering_api)) {
  1047. project_features.append(rendering_api);
  1048. }
  1049. }
  1050. // Check for the existence of a csproj file.
  1051. if (_csproj_exists(get_resource_path())) {
  1052. // If there is a csproj file, add the C# feature if it doesn't already exist.
  1053. if (!project_features.has("C#")) {
  1054. project_features.append("C#");
  1055. }
  1056. } else {
  1057. // If there isn't a csproj file, remove the C# feature if it exists.
  1058. if (project_features.has("C#")) {
  1059. project_features.remove_at(project_features.find("C#"));
  1060. }
  1061. }
  1062. project_features = _trim_to_supported_features(project_features);
  1063. set_setting("application/config/features", project_features);
  1064. #endif // TOOLS_ENABLED
  1065. RBSet<_VCSort> vclist;
  1066. if (p_merge_with_current) {
  1067. for (const KeyValue<StringName, VariantContainer> &G : props) {
  1068. const VariantContainer *v = &G.value;
  1069. if (v->hide_from_editor) {
  1070. continue;
  1071. }
  1072. if (p_custom.has(G.key)) {
  1073. continue;
  1074. }
  1075. _VCSort vc;
  1076. vc.name = G.key; //*k;
  1077. vc.order = v->order;
  1078. vc.type = v->variant.get_type();
  1079. vc.flags = PROPERTY_USAGE_EDITOR | PROPERTY_USAGE_STORAGE;
  1080. if (v->variant == v->initial) {
  1081. continue;
  1082. }
  1083. vclist.insert(vc);
  1084. }
  1085. }
  1086. for (const KeyValue<String, Variant> &E : p_custom) {
  1087. // Lookup global prop to store in the same order
  1088. RBMap<StringName, VariantContainer>::Iterator global_prop = props.find(E.key);
  1089. _VCSort vc;
  1090. vc.name = E.key;
  1091. vc.order = global_prop ? global_prop->value.order : 0xFFFFFFF;
  1092. vc.type = E.value.get_type();
  1093. vc.flags = PROPERTY_USAGE_STORAGE;
  1094. vclist.insert(vc);
  1095. }
  1096. RBMap<String, List<String>> save_props;
  1097. for (const _VCSort &E : vclist) {
  1098. String category = E.name;
  1099. String name = E.name;
  1100. int div = category.find_char('/');
  1101. if (div < 0) {
  1102. category = "";
  1103. } else {
  1104. category = category.substr(0, div);
  1105. name = name.substr(div + 1);
  1106. }
  1107. save_props[category].push_back(name);
  1108. }
  1109. String save_features;
  1110. for (int i = 0; i < p_custom_features.size(); i++) {
  1111. if (i > 0) {
  1112. save_features += ",";
  1113. }
  1114. String f = p_custom_features[i].strip_edges().remove_char('\"');
  1115. save_features += f;
  1116. }
  1117. if (p_path.ends_with(".godot") || p_path.ends_with("override.cfg")) {
  1118. return _save_settings_text(p_path, save_props, p_custom, save_features);
  1119. } else if (p_path.ends_with(".binary")) {
  1120. return _save_settings_binary(p_path, save_props, p_custom, save_features);
  1121. } else {
  1122. ERR_FAIL_V_MSG(ERR_FILE_UNRECOGNIZED, vformat("Unknown config file format: '%s'.", p_path));
  1123. }
  1124. }
  1125. 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) {
  1126. Variant ret;
  1127. if (!ProjectSettings::get_singleton()->has_setting(p_var)) {
  1128. ProjectSettings::get_singleton()->set(p_var, p_default);
  1129. }
  1130. ret = GLOBAL_GET(p_var);
  1131. ProjectSettings::get_singleton()->set_initial_value(p_var, p_default);
  1132. ProjectSettings::get_singleton()->set_builtin_order(p_var);
  1133. ProjectSettings::get_singleton()->set_as_basic(p_var, p_basic);
  1134. ProjectSettings::get_singleton()->set_restart_if_changed(p_var, p_restart_if_changed);
  1135. ProjectSettings::get_singleton()->set_ignore_value_in_docs(p_var, p_ignore_value_in_docs);
  1136. ProjectSettings::get_singleton()->set_as_internal(p_var, p_internal);
  1137. return ret;
  1138. }
  1139. Variant _GLOBAL_DEF(const PropertyInfo &p_info, const Variant &p_default, bool p_restart_if_changed, bool p_ignore_value_in_docs, bool p_basic, bool p_internal) {
  1140. Variant ret = _GLOBAL_DEF(p_info.name, p_default, p_restart_if_changed, p_ignore_value_in_docs, p_basic, p_internal);
  1141. ProjectSettings::get_singleton()->set_custom_property_info(p_info);
  1142. return ret;
  1143. }
  1144. void ProjectSettings::_add_property_info_bind(const Dictionary &p_info) {
  1145. ERR_FAIL_COND_MSG(!p_info.has("name"), "Property info is missing \"name\" field.");
  1146. ERR_FAIL_COND_MSG(!p_info.has("type"), "Property info is missing \"type\" field.");
  1147. if (p_info.has("usage")) {
  1148. WARN_PRINT("\"usage\" is not supported in add_property_info().");
  1149. }
  1150. PropertyInfo pinfo;
  1151. pinfo.name = p_info["name"];
  1152. ERR_FAIL_COND(!props.has(pinfo.name));
  1153. pinfo.type = Variant::Type(p_info["type"].operator int());
  1154. ERR_FAIL_INDEX(pinfo.type, Variant::VARIANT_MAX);
  1155. if (p_info.has("hint")) {
  1156. pinfo.hint = PropertyHint(p_info["hint"].operator int());
  1157. }
  1158. if (p_info.has("hint_string")) {
  1159. pinfo.hint_string = p_info["hint_string"];
  1160. }
  1161. set_custom_property_info(pinfo);
  1162. }
  1163. void ProjectSettings::set_custom_property_info(const PropertyInfo &p_info) {
  1164. const String &prop_name = p_info.name;
  1165. ERR_FAIL_COND(!props.has(prop_name));
  1166. custom_prop_info[prop_name] = p_info;
  1167. }
  1168. const HashMap<StringName, PropertyInfo> &ProjectSettings::get_custom_property_info() const {
  1169. return custom_prop_info;
  1170. }
  1171. bool ProjectSettings::is_using_datapack() const {
  1172. return using_datapack;
  1173. }
  1174. bool ProjectSettings::is_project_loaded() const {
  1175. return project_loaded;
  1176. }
  1177. bool ProjectSettings::_property_can_revert(const StringName &p_name) const {
  1178. return props.has(p_name) && !String(p_name).begins_with(EDITOR_SETTING_OVERRIDE_PREFIX);
  1179. }
  1180. bool ProjectSettings::_property_get_revert(const StringName &p_name, Variant &r_property) const {
  1181. const RBMap<StringName, ProjectSettings::VariantContainer>::Element *value = props.find(p_name);
  1182. if (value) {
  1183. r_property = value->value().initial.duplicate();
  1184. return true;
  1185. }
  1186. return false;
  1187. }
  1188. void ProjectSettings::set_setting(const String &p_setting, const Variant &p_value) {
  1189. set(p_setting, p_value);
  1190. }
  1191. Variant ProjectSettings::get_setting(const String &p_setting, const Variant &p_default_value) const {
  1192. if (has_setting(p_setting)) {
  1193. return get(p_setting);
  1194. } else {
  1195. return p_default_value;
  1196. }
  1197. }
  1198. PackedStringArray ProjectSettings::get_changed_settings() const {
  1199. PackedStringArray arr;
  1200. for (const StringName &setting : changed_settings) {
  1201. arr.push_back(setting);
  1202. }
  1203. return arr;
  1204. }
  1205. bool ProjectSettings::check_changed_settings_in_group(const String &p_setting_prefix) const {
  1206. for (const StringName &setting : changed_settings) {
  1207. if (String(setting).begins_with(p_setting_prefix)) {
  1208. return true;
  1209. }
  1210. }
  1211. return false;
  1212. }
  1213. void ProjectSettings::refresh_global_class_list() {
  1214. // This is called after mounting a new PCK file to pick up class changes.
  1215. is_global_class_list_loaded = false; // Make sure we read from the freshly mounted PCK.
  1216. Array script_classes = get_global_class_list();
  1217. for (int i = 0; i < script_classes.size(); i++) {
  1218. Dictionary c = script_classes[i];
  1219. if (!c.has("class") || !c.has("language") || !c.has("path") || !c.has("base") || !c.has("is_abstract") || !c.has("is_tool")) {
  1220. continue;
  1221. }
  1222. ScriptServer::add_global_class(c["class"], c["base"], c["language"], c["path"], c["is_abstract"], c["is_tool"]);
  1223. }
  1224. }
  1225. TypedArray<Dictionary> ProjectSettings::get_global_class_list() {
  1226. if (is_global_class_list_loaded) {
  1227. return global_class_list;
  1228. }
  1229. Ref<ConfigFile> cf;
  1230. cf.instantiate();
  1231. if (cf->load(get_global_class_list_path()) == OK) {
  1232. global_class_list = cf->get_value("", "list", Array());
  1233. } else {
  1234. #ifndef TOOLS_ENABLED
  1235. // Script classes can't be recreated in exported project, so print an error.
  1236. ERR_PRINT("Could not load global script cache.");
  1237. #endif
  1238. }
  1239. // File read succeeded or failed. If it failed, assume everything is still okay.
  1240. // We will later receive updated class data in store_global_class_list().
  1241. is_global_class_list_loaded = true;
  1242. return global_class_list;
  1243. }
  1244. String ProjectSettings::get_global_class_list_path() const {
  1245. return get_project_data_path().path_join("global_script_class_cache.cfg");
  1246. }
  1247. void ProjectSettings::store_global_class_list(const Array &p_classes) {
  1248. Ref<ConfigFile> cf;
  1249. cf.instantiate();
  1250. cf->set_value("", "list", p_classes);
  1251. cf->save(get_global_class_list_path());
  1252. global_class_list = p_classes;
  1253. }
  1254. bool ProjectSettings::has_custom_feature(const String &p_feature) const {
  1255. return custom_features.has(p_feature);
  1256. }
  1257. const HashMap<StringName, ProjectSettings::AutoloadInfo> &ProjectSettings::get_autoload_list() const {
  1258. return autoloads;
  1259. }
  1260. void ProjectSettings::add_autoload(const AutoloadInfo &p_autoload, bool p_front_insert) {
  1261. ERR_FAIL_COND_MSG(p_autoload.name == StringName(), "Trying to add autoload with no name.");
  1262. if (p_front_insert) {
  1263. if (autoloads.has(p_autoload.name)) {
  1264. autoloads.erase(p_autoload.name);
  1265. }
  1266. autoloads.insert(p_autoload.name, p_autoload, true);
  1267. } else {
  1268. autoloads[p_autoload.name] = p_autoload;
  1269. }
  1270. }
  1271. void ProjectSettings::remove_autoload(const StringName &p_autoload) {
  1272. ERR_FAIL_COND_MSG(!autoloads.has(p_autoload), "Trying to remove non-existent autoload.");
  1273. autoloads.erase(p_autoload);
  1274. }
  1275. bool ProjectSettings::has_autoload(const StringName &p_autoload) const {
  1276. return autoloads.has(p_autoload);
  1277. }
  1278. ProjectSettings::AutoloadInfo ProjectSettings::get_autoload(const StringName &p_name) const {
  1279. ERR_FAIL_COND_V_MSG(!autoloads.has(p_name), AutoloadInfo(), "Trying to get non-existent autoload.");
  1280. return autoloads[p_name];
  1281. }
  1282. void ProjectSettings::fix_autoload_paths() {
  1283. for (KeyValue<StringName, AutoloadInfo> &kv : autoloads) {
  1284. kv.value.path = ResourceUID::ensure_path(kv.value.path);
  1285. }
  1286. }
  1287. const HashMap<StringName, String> &ProjectSettings::get_global_groups_list() const {
  1288. return global_groups;
  1289. }
  1290. void ProjectSettings::add_global_group(const StringName &p_name, const String &p_description) {
  1291. ERR_FAIL_COND_MSG(p_name == StringName(), "Trying to add global group with no name.");
  1292. global_groups[p_name] = p_description;
  1293. }
  1294. void ProjectSettings::remove_global_group(const StringName &p_name) {
  1295. ERR_FAIL_COND_MSG(!global_groups.has(p_name), "Trying to remove non-existent global group.");
  1296. global_groups.erase(p_name);
  1297. }
  1298. bool ProjectSettings::has_global_group(const StringName &p_name) const {
  1299. return global_groups.has(p_name);
  1300. }
  1301. void ProjectSettings::remove_scene_groups_cache(const StringName &p_path) {
  1302. scene_groups_cache.erase(p_path);
  1303. }
  1304. void ProjectSettings::add_scene_groups_cache(const StringName &p_path, const HashSet<StringName> &p_cache) {
  1305. scene_groups_cache[p_path] = p_cache;
  1306. }
  1307. void ProjectSettings::save_scene_groups_cache() {
  1308. Ref<ConfigFile> cf;
  1309. cf.instantiate();
  1310. for (const KeyValue<StringName, HashSet<StringName>> &E : scene_groups_cache) {
  1311. if (E.value.is_empty()) {
  1312. continue;
  1313. }
  1314. Array list;
  1315. for (const StringName &group : E.value) {
  1316. list.push_back(group);
  1317. }
  1318. cf->set_value(E.key, "groups", list);
  1319. }
  1320. cf->save(get_scene_groups_cache_path());
  1321. }
  1322. String ProjectSettings::get_scene_groups_cache_path() const {
  1323. return get_project_data_path().path_join("scene_groups_cache.cfg");
  1324. }
  1325. void ProjectSettings::load_scene_groups_cache() {
  1326. Ref<ConfigFile> cf;
  1327. cf.instantiate();
  1328. if (cf->load(get_scene_groups_cache_path()) == OK) {
  1329. Vector<String> scene_paths = cf->get_sections();
  1330. for (const String &E : scene_paths) {
  1331. Array scene_groups = cf->get_value(E, "groups", Array());
  1332. HashSet<StringName> cache;
  1333. for (const Variant &scene_group : scene_groups) {
  1334. cache.insert(scene_group);
  1335. }
  1336. add_scene_groups_cache(E, cache);
  1337. }
  1338. }
  1339. }
  1340. const HashMap<StringName, HashSet<StringName>> &ProjectSettings::get_scene_groups_cache() const {
  1341. return scene_groups_cache;
  1342. }
  1343. #ifdef TOOLS_ENABLED
  1344. void ProjectSettings::get_argument_options(const StringName &p_function, int p_idx, List<String> *r_options) const {
  1345. const String pf = p_function;
  1346. if (p_idx == 0) {
  1347. if (pf == "has_setting" || pf == "set_setting" || pf == "get_setting" || pf == "get_setting_with_override" ||
  1348. pf == "set_order" || pf == "get_order" || pf == "set_initial_value" || pf == "set_as_basic" ||
  1349. pf == "set_as_internal" || pf == "set_restart_if_changed" || pf == "clear") {
  1350. for (const KeyValue<StringName, VariantContainer> &E : props) {
  1351. if (E.value.hide_from_editor) {
  1352. continue;
  1353. }
  1354. r_options->push_back(String(E.key).quote());
  1355. }
  1356. }
  1357. }
  1358. Object::get_argument_options(p_function, p_idx, r_options);
  1359. }
  1360. #endif
  1361. void ProjectSettings::set_editor_setting_override(const String &p_setting, const Variant &p_value) {
  1362. set_setting(EDITOR_SETTING_OVERRIDE_PREFIX + p_setting, p_value);
  1363. }
  1364. bool ProjectSettings::has_editor_setting_override(const String &p_setting) const {
  1365. return has_setting(EDITOR_SETTING_OVERRIDE_PREFIX + p_setting);
  1366. }
  1367. Variant ProjectSettings::get_editor_setting_override(const String &p_setting) const {
  1368. return get_setting(EDITOR_SETTING_OVERRIDE_PREFIX + p_setting);
  1369. }
  1370. void ProjectSettings::_bind_methods() {
  1371. ClassDB::bind_method(D_METHOD("has_setting", "name"), &ProjectSettings::has_setting);
  1372. ClassDB::bind_method(D_METHOD("set_setting", "name", "value"), &ProjectSettings::set_setting);
  1373. ClassDB::bind_method(D_METHOD("get_setting", "name", "default_value"), &ProjectSettings::get_setting, DEFVAL(Variant()));
  1374. ClassDB::bind_method(D_METHOD("get_setting_with_override", "name"), &ProjectSettings::get_setting_with_override);
  1375. ClassDB::bind_method(D_METHOD("get_global_class_list"), &ProjectSettings::get_global_class_list);
  1376. ClassDB::bind_method(D_METHOD("get_setting_with_override_and_custom_features", "name", "features"), &ProjectSettings::get_setting_with_override_and_custom_features);
  1377. ClassDB::bind_method(D_METHOD("set_order", "name", "position"), &ProjectSettings::set_order);
  1378. ClassDB::bind_method(D_METHOD("get_order", "name"), &ProjectSettings::get_order);
  1379. ClassDB::bind_method(D_METHOD("set_initial_value", "name", "value"), &ProjectSettings::set_initial_value);
  1380. ClassDB::bind_method(D_METHOD("set_as_basic", "name", "basic"), &ProjectSettings::set_as_basic);
  1381. ClassDB::bind_method(D_METHOD("set_as_internal", "name", "internal"), &ProjectSettings::set_as_internal);
  1382. ClassDB::bind_method(D_METHOD("add_property_info", "hint"), &ProjectSettings::_add_property_info_bind);
  1383. ClassDB::bind_method(D_METHOD("set_restart_if_changed", "name", "restart"), &ProjectSettings::set_restart_if_changed);
  1384. ClassDB::bind_method(D_METHOD("clear", "name"), &ProjectSettings::clear);
  1385. ClassDB::bind_method(D_METHOD("localize_path", "path"), &ProjectSettings::localize_path);
  1386. ClassDB::bind_method(D_METHOD("globalize_path", "path"), &ProjectSettings::globalize_path);
  1387. ClassDB::bind_method(D_METHOD("save"), &ProjectSettings::save);
  1388. ClassDB::bind_method(D_METHOD("load_resource_pack", "pack", "replace_files", "offset"), &ProjectSettings::load_resource_pack, DEFVAL(true), DEFVAL(0));
  1389. ClassDB::bind_method(D_METHOD("save_custom", "file"), &ProjectSettings::_save_custom_bnd);
  1390. // Change tracking methods
  1391. ClassDB::bind_method(D_METHOD("get_changed_settings"), &ProjectSettings::get_changed_settings);
  1392. ClassDB::bind_method(D_METHOD("check_changed_settings_in_group", "setting_prefix"), &ProjectSettings::check_changed_settings_in_group);
  1393. ADD_SIGNAL(MethodInfo("settings_changed"));
  1394. }
  1395. void ProjectSettings::_add_builtin_input_map() {
  1396. if (InputMap::get_singleton()) {
  1397. HashMap<String, List<Ref<InputEvent>>> builtins = InputMap::get_singleton()->get_builtins();
  1398. for (KeyValue<String, List<Ref<InputEvent>>> &E : builtins) {
  1399. Array events;
  1400. // Convert list of input events into array
  1401. for (const Ref<InputEvent> &event : E.value) {
  1402. events.push_back(event);
  1403. }
  1404. Dictionary action;
  1405. action["deadzone"] = Variant(InputMap::DEFAULT_TOGGLE_DEADZONE);
  1406. action["events"] = events;
  1407. String action_name = "input/" + E.key;
  1408. GLOBAL_DEF(action_name, action);
  1409. input_presets.push_back(action_name);
  1410. }
  1411. }
  1412. }
  1413. ProjectSettings::ProjectSettings() {
  1414. // Initialization of engine variables should be done in the setup() method,
  1415. // so that the values can be overridden from project.godot or project.binary.
  1416. CRASH_COND_MSG(singleton != nullptr, "Instantiating a new ProjectSettings singleton is not supported.");
  1417. singleton = this;
  1418. #ifdef TOOLS_ENABLED
  1419. // Available only at runtime in editor builds. Needs to be processed before anything else to work properly.
  1420. if (!Engine::get_singleton()->is_editor_hint()) {
  1421. String editor_features = OS::get_singleton()->get_environment("GODOT_EDITOR_CUSTOM_FEATURES");
  1422. if (!editor_features.is_empty()) {
  1423. PackedStringArray feature_list = editor_features.split(",");
  1424. for (const String &s : feature_list) {
  1425. custom_features.insert(s);
  1426. }
  1427. }
  1428. }
  1429. #endif
  1430. GLOBAL_DEF_BASIC("application/config/name", "");
  1431. GLOBAL_DEF(PropertyInfo(Variant::DICTIONARY, "application/config/name_localized", PROPERTY_HINT_LOCALIZABLE_STRING), Dictionary());
  1432. GLOBAL_DEF_BASIC(PropertyInfo(Variant::STRING, "application/config/description", PROPERTY_HINT_MULTILINE_TEXT), "");
  1433. GLOBAL_DEF_BASIC("application/config/version", "");
  1434. GLOBAL_DEF_INTERNAL(PropertyInfo(Variant::STRING, "application/config/tags"), PackedStringArray());
  1435. GLOBAL_DEF_BASIC(PropertyInfo(Variant::STRING, "application/run/main_scene", PROPERTY_HINT_FILE, "*.tscn,*.scn,*.res"), "");
  1436. GLOBAL_DEF("application/run/disable_stdout", false);
  1437. GLOBAL_DEF("application/run/disable_stderr", false);
  1438. GLOBAL_DEF("application/run/print_header", true);
  1439. GLOBAL_DEF("application/run/enable_alt_space_menu", false);
  1440. GLOBAL_DEF_RST("application/config/use_hidden_project_data_directory", true);
  1441. GLOBAL_DEF("application/config/use_custom_user_dir", false);
  1442. GLOBAL_DEF("application/config/custom_user_dir_name", "");
  1443. GLOBAL_DEF("application/config/project_settings_override", "");
  1444. GLOBAL_DEF("application/config/disable_project_settings_override", false);
  1445. GLOBAL_DEF("application/run/main_loop_type", "SceneTree");
  1446. GLOBAL_DEF("application/config/auto_accept_quit", true);
  1447. GLOBAL_DEF("application/config/quit_on_go_back", true);
  1448. GLOBAL_DEF_BASIC(PropertyInfo(Variant::INT, "accessibility/general/accessibility_support", PROPERTY_HINT_ENUM, "Auto (When Screen Reader is Running),Always Active,Disabled"), 0);
  1449. GLOBAL_DEF_BASIC(PropertyInfo(Variant::INT, "accessibility/general/updates_per_second", PROPERTY_HINT_RANGE, "1,100,1"), 60);
  1450. // The default window size is tuned to:
  1451. // - Have a 16:9 aspect ratio,
  1452. // - Have both dimensions divisible by 8 to better play along with video recording,
  1453. // - Be displayable correctly in windowed mode on a 1366×768 display (tested on Windows 10 with default settings).
  1454. GLOBAL_DEF_BASIC(PropertyInfo(Variant::INT, "display/window/size/viewport_width", PROPERTY_HINT_RANGE, "1,7680,1,or_greater"), 1152); // 8K resolution
  1455. GLOBAL_DEF_BASIC(PropertyInfo(Variant::INT, "display/window/size/viewport_height", PROPERTY_HINT_RANGE, "1,4320,1,or_greater"), 648); // 8K resolution
  1456. GLOBAL_DEF_BASIC(PropertyInfo(Variant::INT, "display/window/size/mode", PROPERTY_HINT_ENUM, "Windowed,Minimized,Maximized,Fullscreen,Exclusive Fullscreen"), 0);
  1457. // Keep the enum values in sync with the `Window::WINDOW_INITIAL_POSITION_` enum.
  1458. GLOBAL_DEF_BASIC(PropertyInfo(Variant::INT, "display/window/size/initial_position_type", PROPERTY_HINT_ENUM, "Absolute:0,Center of Primary Screen:1,Center of Other Screen:3,Center of Screen With Mouse Pointer:4,Center of Screen With Keyboard Focus:5"), 1);
  1459. GLOBAL_DEF_BASIC(PropertyInfo(Variant::VECTOR2I, "display/window/size/initial_position"), Vector2i());
  1460. // Keep the enum values in sync with the `DisplayServer::SCREEN_` enum.
  1461. GLOBAL_DEF_BASIC(PropertyInfo(Variant::INT, "display/window/size/initial_screen", PROPERTY_HINT_RANGE, "0,64,1,or_greater"), 0);
  1462. GLOBAL_DEF_BASIC("display/window/size/resizable", true);
  1463. GLOBAL_DEF_BASIC("display/window/size/borderless", false);
  1464. GLOBAL_DEF("display/window/size/always_on_top", false);
  1465. GLOBAL_DEF("display/window/size/transparent", false);
  1466. GLOBAL_DEF("display/window/size/extend_to_title", false);
  1467. GLOBAL_DEF("display/window/size/no_focus", false);
  1468. GLOBAL_DEF("display/window/size/sharp_corners", false);
  1469. GLOBAL_DEF("display/window/size/minimize_disabled", false);
  1470. GLOBAL_DEF("display/window/size/maximize_disabled", false);
  1471. GLOBAL_DEF(PropertyInfo(Variant::INT, "display/window/size/window_width_override", PROPERTY_HINT_RANGE, "0,7680,1,or_greater"), 0); // 8K resolution
  1472. GLOBAL_DEF(PropertyInfo(Variant::INT, "display/window/size/window_height_override", PROPERTY_HINT_RANGE, "0,4320,1,or_greater"), 0); // 8K resolution
  1473. GLOBAL_DEF("display/window/energy_saving/keep_screen_on", true);
  1474. GLOBAL_DEF("animation/warnings/check_invalid_track_paths", true);
  1475. GLOBAL_DEF("animation/warnings/check_angle_interpolation_type_conflicting", true);
  1476. #ifndef DISABLE_DEPRECATED
  1477. GLOBAL_DEF_RST("animation/compatibility/default_parent_skeleton_in_mesh_instance_3d", false);
  1478. #endif
  1479. GLOBAL_DEF_BASIC(PropertyInfo(Variant::STRING, "audio/buses/default_bus_layout", PROPERTY_HINT_FILE, "*.tres"), "res://default_bus_layout.tres");
  1480. GLOBAL_DEF(PropertyInfo(Variant::INT, "audio/general/default_playback_type", PROPERTY_HINT_ENUM, "Stream,Sample"), 0);
  1481. GLOBAL_DEF(PropertyInfo(Variant::INT, "audio/general/default_playback_type.web", PROPERTY_HINT_ENUM, "Stream,Sample"), 1);
  1482. GLOBAL_DEF_RST("audio/general/text_to_speech", false);
  1483. GLOBAL_DEF_RST(PropertyInfo(Variant::FLOAT, "audio/general/2d_panning_strength", PROPERTY_HINT_RANGE, "0,2,0.01"), 0.5f);
  1484. GLOBAL_DEF_RST(PropertyInfo(Variant::FLOAT, "audio/general/3d_panning_strength", PROPERTY_HINT_RANGE, "0,2,0.01"), 0.5f);
  1485. GLOBAL_DEF(PropertyInfo(Variant::INT, "audio/general/ios/session_category", PROPERTY_HINT_ENUM, "Ambient,Multi Route,Play and Record,Playback,Record,Solo Ambient"), 0);
  1486. GLOBAL_DEF("audio/general/ios/mix_with_others", false);
  1487. _add_builtin_input_map();
  1488. // Keep the enum values in sync with the `DisplayServer::ScreenOrientation` enum.
  1489. 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");
  1490. GLOBAL_DEF("display/window/subwindows/embed_subwindows", true);
  1491. // Keep the enum values in sync with the `DisplayServer::VSyncMode` enum.
  1492. custom_prop_info["display/window/vsync/vsync_mode"] = PropertyInfo(Variant::INT, "display/window/vsync/vsync_mode", PROPERTY_HINT_ENUM, "Disabled,Enabled,Adaptive,Mailbox");
  1493. GLOBAL_DEF("display/window/frame_pacing/android/enable_frame_pacing", true);
  1494. GLOBAL_DEF(PropertyInfo(Variant::INT, "display/window/frame_pacing/android/swappy_mode", PROPERTY_HINT_ENUM, "pipeline_forced_on,auto_fps_pipeline_forced_on,auto_fps_auto_pipeline"), 2);
  1495. #ifdef DISABLE_DEPRECATED
  1496. custom_prop_info["rendering/driver/threads/thread_model"] = PropertyInfo(Variant::INT, "rendering/driver/threads/thread_model", PROPERTY_HINT_ENUM, "Safe:1,Separate");
  1497. #else
  1498. custom_prop_info["rendering/driver/threads/thread_model"] = PropertyInfo(Variant::INT, "rendering/driver/threads/thread_model", PROPERTY_HINT_ENUM, "Unsafe (deprecated),Safe,Separate");
  1499. #endif
  1500. #ifndef PHYSICS_2D_DISABLED
  1501. GLOBAL_DEF("physics/2d/run_on_separate_thread", false);
  1502. #endif // PHYSICS_2D_DISABLED
  1503. #ifndef PHYSICS_3D_DISABLED
  1504. GLOBAL_DEF("physics/3d/run_on_separate_thread", false);
  1505. #endif // PHYSICS_3D_DISABLED
  1506. GLOBAL_DEF_BASIC(PropertyInfo(Variant::STRING, "display/window/stretch/mode", PROPERTY_HINT_ENUM, "disabled,canvas_items,viewport"), "disabled");
  1507. GLOBAL_DEF_BASIC(PropertyInfo(Variant::STRING, "display/window/stretch/aspect", PROPERTY_HINT_ENUM, "ignore,keep,keep_width,keep_height,expand"), "keep");
  1508. GLOBAL_DEF_BASIC(PropertyInfo(Variant::FLOAT, "display/window/stretch/scale", PROPERTY_HINT_RANGE, "0.5,8.0,0.01"), 1.0);
  1509. GLOBAL_DEF_BASIC(PropertyInfo(Variant::STRING, "display/window/stretch/scale_mode", PROPERTY_HINT_ENUM, "fractional,integer"), "fractional");
  1510. GLOBAL_DEF(PropertyInfo(Variant::INT, "debug/settings/profiler/max_functions", PROPERTY_HINT_RANGE, "128,65535,1"), 16384);
  1511. GLOBAL_DEF_RST(PropertyInfo(Variant::INT, "debug/settings/profiler/max_timestamp_query_elements", PROPERTY_HINT_RANGE, "256,65535,1"), 256);
  1512. GLOBAL_DEF(PropertyInfo(Variant::BOOL, "compression/formats/zstd/long_distance_matching"), Compression::zstd_long_distance_matching);
  1513. GLOBAL_DEF(PropertyInfo(Variant::INT, "compression/formats/zstd/compression_level", PROPERTY_HINT_RANGE, "1,22,1"), Compression::zstd_level);
  1514. GLOBAL_DEF(PropertyInfo(Variant::INT, "compression/formats/zstd/window_log_size", PROPERTY_HINT_RANGE, "10,30,1"), Compression::zstd_window_log_size);
  1515. GLOBAL_DEF(PropertyInfo(Variant::INT, "compression/formats/zlib/compression_level", PROPERTY_HINT_RANGE, "-1,9,1"), Compression::zlib_level);
  1516. GLOBAL_DEF(PropertyInfo(Variant::INT, "compression/formats/gzip/compression_level", PROPERTY_HINT_RANGE, "-1,9,1"), Compression::gzip_level);
  1517. GLOBAL_DEF("debug/settings/crash_handler/message",
  1518. String("Please include this when reporting the bug to the project developer."));
  1519. GLOBAL_DEF("debug/settings/crash_handler/message.editor",
  1520. String("Please include this when reporting the bug on: https://github.com/godotengine/godot/issues"));
  1521. GLOBAL_DEF_RST(PropertyInfo(Variant::INT, "rendering/occlusion_culling/bvh_build_quality", PROPERTY_HINT_ENUM, "Low,Medium,High"), 2);
  1522. GLOBAL_DEF_RST("rendering/occlusion_culling/jitter_projection", true);
  1523. GLOBAL_DEF_RST("internationalization/rendering/force_right_to_left_layout_direction", false);
  1524. GLOBAL_DEF_BASIC(PropertyInfo(Variant::INT, "internationalization/rendering/root_node_layout_direction", PROPERTY_HINT_ENUM, "Based on Application Locale,Left-to-Right,Right-to-Left,Based on System Locale"), 0);
  1525. GLOBAL_DEF_BASIC("internationalization/rendering/root_node_auto_translate", true);
  1526. GLOBAL_DEF(PropertyInfo(Variant::INT, "gui/timers/incremental_search_max_interval_msec", PROPERTY_HINT_RANGE, "0,10000,1,or_greater"), 2000);
  1527. GLOBAL_DEF(PropertyInfo(Variant::FLOAT, "gui/timers/tooltip_delay_sec", PROPERTY_HINT_RANGE, "0,5,0.01,or_greater"), 0.5);
  1528. #ifdef TOOLS_ENABLED
  1529. GLOBAL_DEF("gui/timers/tooltip_delay_sec.editor_hint", 0.5);
  1530. #endif
  1531. GLOBAL_DEF("gui/common/drag_threshold", 10);
  1532. GLOBAL_DEF_BASIC("gui/common/snap_controls_to_pixels", true);
  1533. GLOBAL_DEF(PropertyInfo(Variant::INT, "gui/common/show_focus_state_on_pointer_event", PROPERTY_HINT_ENUM, "Never,Control Supports Keyboard Input,Always"), 1);
  1534. GLOBAL_DEF_BASIC("gui/fonts/dynamic_fonts/use_oversampling", true);
  1535. GLOBAL_DEF_RST(PropertyInfo(Variant::INT, "rendering/rendering_device/vsync/frame_queue_size", PROPERTY_HINT_RANGE, "2,3,1"), 2);
  1536. GLOBAL_DEF_RST(PropertyInfo(Variant::INT, "rendering/rendering_device/vsync/swapchain_image_count", PROPERTY_HINT_RANGE, "2,4,1"), 3);
  1537. GLOBAL_DEF(PropertyInfo(Variant::INT, "rendering/rendering_device/staging_buffer/block_size_kb", PROPERTY_HINT_RANGE, "4,2048,1,or_greater"), 256);
  1538. GLOBAL_DEF(PropertyInfo(Variant::INT, "rendering/rendering_device/staging_buffer/max_size_mb", PROPERTY_HINT_RANGE, "1,1024,1,or_greater"), 128);
  1539. GLOBAL_DEF(PropertyInfo(Variant::INT, "rendering/rendering_device/staging_buffer/texture_upload_region_size_px", PROPERTY_HINT_RANGE, "1,256,1,or_greater"), 64);
  1540. GLOBAL_DEF(PropertyInfo(Variant::INT, "rendering/rendering_device/staging_buffer/texture_download_region_size_px", PROPERTY_HINT_RANGE, "1,256,1,or_greater"), 64);
  1541. GLOBAL_DEF_RST(PropertyInfo(Variant::BOOL, "rendering/rendering_device/pipeline_cache/enable"), true);
  1542. GLOBAL_DEF(PropertyInfo(Variant::FLOAT, "rendering/rendering_device/pipeline_cache/save_chunk_size_mb", PROPERTY_HINT_RANGE, "0.000001,64.0,0.001,or_greater"), 3.0);
  1543. GLOBAL_DEF(PropertyInfo(Variant::INT, "rendering/rendering_device/vulkan/max_descriptors_per_pool", PROPERTY_HINT_RANGE, "1,256,1,or_greater"), 64);
  1544. GLOBAL_DEF_RST("rendering/rendering_device/d3d12/max_resource_descriptors", 65536);
  1545. custom_prop_info["rendering/rendering_device/d3d12/max_resource_descriptors"] = PropertyInfo(Variant::INT, "rendering/rendering_device/d3d12/max_resource_descriptors", PROPERTY_HINT_RANGE, "512,1000000");
  1546. GLOBAL_DEF_RST("rendering/rendering_device/d3d12/max_sampler_descriptors", 1024);
  1547. custom_prop_info["rendering/rendering_device/d3d12/max_sampler_descriptors"] = PropertyInfo(Variant::INT, "rendering/rendering_device/d3d12/max_sampler_descriptors", PROPERTY_HINT_RANGE, "256,2048");
  1548. // The default value must match the minor part of the Agility SDK version
  1549. // installed by the scripts provided in the repository
  1550. // (check `misc/scripts/install_d3d12_sdk_windows.py`).
  1551. // For example, if the script installs 1.618.5, the default value must be 618.
  1552. GLOBAL_DEF_RST(PropertyInfo(Variant::INT, "rendering/rendering_device/d3d12/agility_sdk_version", PROPERTY_HINT_RANGE, "0,10000,1,or_greater,hide_control"), 618);
  1553. GLOBAL_DEF_BASIC(PropertyInfo(Variant::INT, "rendering/textures/canvas_textures/default_texture_filter", PROPERTY_HINT_ENUM, "Nearest,Linear,Linear Mipmap,Nearest Mipmap"), 1);
  1554. GLOBAL_DEF_BASIC(PropertyInfo(Variant::INT, "rendering/textures/canvas_textures/default_texture_repeat", PROPERTY_HINT_ENUM, "Disable,Enable,Mirror"), 0);
  1555. GLOBAL_DEF("collada/use_ambient", false);
  1556. // Input settings
  1557. GLOBAL_DEF_BASIC("input_devices/pointing/android/enable_long_press_as_right_click", false);
  1558. GLOBAL_DEF_BASIC("input_devices/pointing/android/enable_pan_and_scale_gestures", false);
  1559. GLOBAL_DEF_BASIC(PropertyInfo(Variant::INT, "input_devices/pointing/android/rotary_input_scroll_axis", PROPERTY_HINT_ENUM, "Horizontal,Vertical"), 1);
  1560. GLOBAL_DEF("input_devices/pointing/android/override_volume_buttons", false);
  1561. GLOBAL_DEF_BASIC("input_devices/pointing/android/disable_scroll_deadzone", false);
  1562. // These properties will not show up in the dialog. If you want to exclude whole groups, use add_hidden_prefix().
  1563. GLOBAL_DEF_INTERNAL("application/config/features", PackedStringArray());
  1564. GLOBAL_DEF_INTERNAL("internationalization/locale/translation_remaps", PackedStringArray());
  1565. GLOBAL_DEF_INTERNAL("internationalization/locale/translations", PackedStringArray());
  1566. GLOBAL_DEF_INTERNAL("internationalization/locale/translations_pot_files", PackedStringArray());
  1567. GLOBAL_DEF_INTERNAL("internationalization/locale/translation_add_builtin_strings_to_pot", false);
  1568. #if !defined(NAVIGATION_2D_DISABLED) || !defined(NAVIGATION_3D_DISABLED)
  1569. GLOBAL_DEF("navigation/world/map_use_async_iterations", true);
  1570. GLOBAL_DEF("navigation/world/region_use_async_iterations", true);
  1571. GLOBAL_DEF("navigation/avoidance/thread_model/avoidance_use_multiple_threads", true);
  1572. GLOBAL_DEF("navigation/avoidance/thread_model/avoidance_use_high_priority_threads", true);
  1573. GLOBAL_DEF("navigation/pathfinding/max_threads", 4);
  1574. GLOBAL_DEF("navigation/baking/use_crash_prevention_checks", true);
  1575. GLOBAL_DEF("navigation/baking/thread_model/baking_use_multiple_threads", true);
  1576. GLOBAL_DEF("navigation/baking/thread_model/baking_use_high_priority_threads", true);
  1577. #endif // !defined(NAVIGATION_2D_DISABLED) || !defined(NAVIGATION_3D_DISABLED)
  1578. #ifndef NAVIGATION_2D_DISABLED
  1579. GLOBAL_DEF("navigation/2d/warnings/navmesh_edge_merge_errors", true);
  1580. GLOBAL_DEF("navigation/2d/warnings/navmesh_cell_size_mismatch", true);
  1581. #endif // NAVIGATION_2D_DISABLED
  1582. #ifndef NAVIGATION_3D_DISABLED
  1583. GLOBAL_DEF("navigation/3d/warnings/navmesh_edge_merge_errors", true);
  1584. GLOBAL_DEF("navigation/3d/warnings/navmesh_cell_size_mismatch", true);
  1585. #endif // NAVIGATION_3D_DISABLED
  1586. ProjectSettings::get_singleton()->add_hidden_prefix("input/");
  1587. }
  1588. ProjectSettings::ProjectSettings(const String &p_path) {
  1589. if (load_custom(p_path) == OK) {
  1590. resource_path = p_path.get_base_dir();
  1591. project_loaded = true;
  1592. }
  1593. }
  1594. ProjectSettings::~ProjectSettings() {
  1595. if (singleton == this) {
  1596. singleton = nullptr;
  1597. }
  1598. }