project_settings.cpp 50 KB

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