project_settings.cpp 53 KB

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