project_settings.cpp 51 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373
  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. if (resource_path.is_empty() || (p_path.is_absolute_path() && !p_path.begins_with(resource_path))) {
  133. return p_path.simplify_path();
  134. }
  135. // Check if we have a special path (like res://) or a protocol identifier.
  136. int p = p_path.find("://");
  137. bool found = false;
  138. if (p > 0) {
  139. found = true;
  140. for (int i = 0; i < p; i++) {
  141. if (!is_ascii_alphanumeric_char(p_path[i])) {
  142. found = false;
  143. break;
  144. }
  145. }
  146. }
  147. if (found) {
  148. return p_path.simplify_path();
  149. }
  150. Ref<DirAccess> dir = DirAccess::create(DirAccess::ACCESS_FILESYSTEM);
  151. String path = p_path.replace("\\", "/").simplify_path();
  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 p_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. String ProjectSettings::globalize_path(const String &p_path) const {
  218. if (p_path.begins_with("res://")) {
  219. if (!resource_path.is_empty()) {
  220. return p_path.replace("res:/", resource_path);
  221. }
  222. return p_path.replace("res://", "");
  223. } else if (p_path.begins_with("user://")) {
  224. String data_dir = OS::get_singleton()->get_user_data_dir();
  225. if (!data_dir.is_empty()) {
  226. return p_path.replace("user:/", data_dir);
  227. }
  228. return p_path.replace("user://", "");
  229. }
  230. return p_path;
  231. }
  232. bool ProjectSettings::_set(const StringName &p_name, const Variant &p_value) {
  233. _THREAD_SAFE_METHOD_
  234. if (p_value.get_type() == Variant::NIL) {
  235. props.erase(p_name);
  236. if (p_name.operator String().begins_with("autoload/")) {
  237. String node_name = p_name.operator String().split("/")[1];
  238. if (autoloads.has(node_name)) {
  239. remove_autoload(node_name);
  240. }
  241. }
  242. } else {
  243. if (p_name == CoreStringNames::get_singleton()->_custom_features) {
  244. Vector<String> custom_feature_array = String(p_value).split(",");
  245. for (int i = 0; i < custom_feature_array.size(); i++) {
  246. custom_features.insert(custom_feature_array[i]);
  247. }
  248. return true;
  249. }
  250. { // Feature overrides.
  251. int dot = p_name.operator String().find(".");
  252. if (dot != -1) {
  253. Vector<String> s = p_name.operator String().split(".");
  254. for (int i = 1; i < s.size(); i++) {
  255. String feature = s[i].strip_edges();
  256. Pair<StringName, StringName> feature_override(feature, p_name);
  257. if (!feature_overrides.has(s[0])) {
  258. feature_overrides[s[0]] = LocalVector<Pair<StringName, StringName>>();
  259. }
  260. feature_overrides[s[0]].push_back(feature_override);
  261. }
  262. }
  263. }
  264. if (props.has(p_name)) {
  265. props[p_name].variant = p_value;
  266. } else {
  267. props[p_name] = VariantContainer(p_value, last_order++);
  268. }
  269. if (p_name.operator String().begins_with("autoload/")) {
  270. String node_name = p_name.operator String().split("/")[1];
  271. AutoloadInfo autoload;
  272. autoload.name = node_name;
  273. String path = p_value;
  274. if (path.begins_with("*")) {
  275. autoload.is_singleton = true;
  276. autoload.path = path.substr(1);
  277. } else {
  278. autoload.path = path;
  279. }
  280. add_autoload(autoload);
  281. }
  282. }
  283. return true;
  284. }
  285. bool ProjectSettings::_get(const StringName &p_name, Variant &r_ret) const {
  286. _THREAD_SAFE_METHOD_
  287. if (!props.has(p_name)) {
  288. WARN_PRINT("Property not found: " + String(p_name));
  289. return false;
  290. }
  291. r_ret = props[p_name].variant;
  292. return true;
  293. }
  294. Variant ProjectSettings::get_setting_with_override(const StringName &p_name) const {
  295. _THREAD_SAFE_METHOD_
  296. StringName name = p_name;
  297. if (feature_overrides.has(name)) {
  298. const LocalVector<Pair<StringName, StringName>> &overrides = feature_overrides[name];
  299. for (uint32_t i = 0; i < overrides.size(); i++) {
  300. if (OS::get_singleton()->has_feature(overrides[i].first)) { // Custom features are checked in OS.has_feature() already. No need to check twice.
  301. if (props.has(overrides[i].second)) {
  302. name = overrides[i].second;
  303. break;
  304. }
  305. }
  306. }
  307. }
  308. if (!props.has(name)) {
  309. WARN_PRINT("Property not found: " + String(name));
  310. return Variant();
  311. }
  312. return props[name].variant;
  313. }
  314. struct _VCSort {
  315. String name;
  316. Variant::Type type = Variant::VARIANT_MAX;
  317. int order = 0;
  318. uint32_t flags = 0;
  319. bool operator<(const _VCSort &p_vcs) const { return order == p_vcs.order ? name < p_vcs.name : order < p_vcs.order; }
  320. };
  321. void ProjectSettings::_get_property_list(List<PropertyInfo> *p_list) const {
  322. _THREAD_SAFE_METHOD_
  323. RBSet<_VCSort> vclist;
  324. for (const KeyValue<StringName, VariantContainer> &E : props) {
  325. const VariantContainer *v = &E.value;
  326. if (v->hide_from_editor) {
  327. continue;
  328. }
  329. _VCSort vc;
  330. vc.name = E.key;
  331. vc.order = v->order;
  332. vc.type = v->variant.get_type();
  333. 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/")) {
  334. vc.flags = PROPERTY_USAGE_STORAGE;
  335. } else {
  336. vc.flags = PROPERTY_USAGE_EDITOR | PROPERTY_USAGE_STORAGE;
  337. }
  338. if (v->internal) {
  339. vc.flags |= PROPERTY_USAGE_INTERNAL;
  340. }
  341. if (v->basic) {
  342. vc.flags |= PROPERTY_USAGE_EDITOR_BASIC_SETTING;
  343. }
  344. if (v->restart_if_changed) {
  345. vc.flags |= PROPERTY_USAGE_RESTART_IF_CHANGED;
  346. }
  347. vclist.insert(vc);
  348. }
  349. for (const _VCSort &E : vclist) {
  350. String prop_info_name = E.name;
  351. int dot = prop_info_name.find(".");
  352. if (dot != -1 && !custom_prop_info.has(prop_info_name)) {
  353. prop_info_name = prop_info_name.substr(0, dot);
  354. }
  355. if (custom_prop_info.has(prop_info_name)) {
  356. PropertyInfo pi = custom_prop_info[prop_info_name];
  357. pi.name = E.name;
  358. pi.usage = E.flags;
  359. p_list->push_back(pi);
  360. } else {
  361. p_list->push_back(PropertyInfo(E.type, E.name, PROPERTY_HINT_NONE, "", E.flags));
  362. }
  363. }
  364. }
  365. bool ProjectSettings::_load_resource_pack(const String &p_pack, bool p_replace_files, int p_offset) {
  366. if (PackedData::get_singleton()->is_disabled()) {
  367. return false;
  368. }
  369. bool ok = PackedData::get_singleton()->add_pack(p_pack, p_replace_files, p_offset) == OK;
  370. if (!ok) {
  371. return false;
  372. }
  373. //if data.pck is found, all directory access will be from here
  374. DirAccess::make_default<DirAccessPack>(DirAccess::ACCESS_RESOURCES);
  375. using_datapack = true;
  376. return true;
  377. }
  378. void ProjectSettings::_convert_to_last_version(int p_from_version) {
  379. if (p_from_version <= 3) {
  380. // Converts the actions from array to dictionary (array of events to dictionary with deadzone + events)
  381. for (KeyValue<StringName, ProjectSettings::VariantContainer> &E : props) {
  382. Variant value = E.value.variant;
  383. if (String(E.key).begins_with("input/") && value.get_type() == Variant::ARRAY) {
  384. Array array = value;
  385. Dictionary action;
  386. action["deadzone"] = Variant(0.5f);
  387. action["events"] = array;
  388. E.value.variant = action;
  389. }
  390. }
  391. }
  392. }
  393. /*
  394. * This method is responsible for loading a project.godot file and/or data file
  395. * using the following merit order:
  396. * - If using NetworkClient, try to lookup project file or fail.
  397. * - If --main-pack was passed by the user (`p_main_pack`), load it or fail.
  398. * - Search for project PCKs automatically. For each step we try loading a potential
  399. * PCK, and if it doesn't work, we proceed to the next step. If any step succeeds,
  400. * we try loading the project settings, and abort if it fails. Steps:
  401. * o Bundled PCK in the executable.
  402. * o [macOS only] PCK with same basename as the binary in the .app resource dir.
  403. * o PCK with same basename as the binary in the binary's directory. We handle both
  404. * changing the extension to '.pck' (e.g. 'win_game.exe' -> 'win_game.pck') and
  405. * appending '.pck' to the binary name (e.g. 'linux_game' -> 'linux_game.pck').
  406. * o PCK with the same basename as the binary in the current working directory.
  407. * Same as above for the two possible PCK file names.
  408. * - On relevant platforms (Android/iOS), lookup project file in OS resource path.
  409. * If found, load it or fail.
  410. * - Lookup project file in passed `p_path` (--path passed by the user), i.e. we
  411. * are running from source code.
  412. * If not found and `p_upwards` is true (--upwards passed by the user), look for
  413. * project files in parent folders up to the system root (used to run a game
  414. * from command line while in a subfolder).
  415. * If a project file is found, load it or fail.
  416. * If nothing was found, error out.
  417. */
  418. Error ProjectSettings::_setup(const String &p_path, const String &p_main_pack, bool p_upwards, bool p_ignore_override) {
  419. if (!OS::get_singleton()->get_resource_dir().is_empty()) {
  420. // OS will call ProjectSettings->get_resource_path which will be empty if not overridden!
  421. // If the OS would rather use a specific location, then it will not be empty.
  422. resource_path = OS::get_singleton()->get_resource_dir().replace("\\", "/");
  423. if (!resource_path.is_empty() && resource_path[resource_path.length() - 1] == '/') {
  424. resource_path = resource_path.substr(0, resource_path.length() - 1); // Chop end.
  425. }
  426. }
  427. // Attempt with a user-defined main pack first
  428. if (!p_main_pack.is_empty()) {
  429. bool ok = _load_resource_pack(p_main_pack);
  430. ERR_FAIL_COND_V_MSG(!ok, ERR_CANT_OPEN, "Cannot open resource pack '" + p_main_pack + "'.");
  431. Error err = _load_settings_text_or_binary("res://project.godot", "res://project.binary");
  432. if (err == OK && !p_ignore_override) {
  433. // Load override from location of the main pack
  434. // Optional, we don't mind if it fails
  435. _load_settings_text(p_main_pack.get_base_dir().path_join("override.cfg"));
  436. }
  437. return err;
  438. }
  439. String exec_path = OS::get_singleton()->get_executable_path();
  440. if (!exec_path.is_empty()) {
  441. // We do several tests sequentially until one succeeds to find a PCK,
  442. // and if so, we attempt loading it at the end.
  443. // Attempt with PCK bundled into executable.
  444. bool found = _load_resource_pack(exec_path);
  445. // Attempt with exec_name.pck.
  446. // (This is the usual case when distributing a Godot game.)
  447. String exec_dir = exec_path.get_base_dir();
  448. String exec_filename = exec_path.get_file();
  449. String exec_basename = exec_filename.get_basename();
  450. // Based on the OS, it can be the exec path + '.pck' (Linux w/o extension, macOS in .app bundle)
  451. // or the exec path's basename + '.pck' (Windows).
  452. // We need to test both possibilities as extensions for Linux binaries are optional
  453. // (so both 'mygame.bin' and 'mygame' should be able to find 'mygame.pck').
  454. #ifdef MACOS_ENABLED
  455. if (!found) {
  456. // Attempt to load PCK from macOS .app bundle resources.
  457. found = _load_resource_pack(OS::get_singleton()->get_bundle_resource_dir().path_join(exec_basename + ".pck")) || _load_resource_pack(OS::get_singleton()->get_bundle_resource_dir().path_join(exec_filename + ".pck"));
  458. }
  459. #endif
  460. if (!found) {
  461. // Try to load data pack at the location of the executable.
  462. // As mentioned above, we have two potential names to attempt.
  463. found = _load_resource_pack(exec_dir.path_join(exec_basename + ".pck")) || _load_resource_pack(exec_dir.path_join(exec_filename + ".pck"));
  464. }
  465. if (!found) {
  466. // If we couldn't find them next to the executable, we attempt
  467. // the current working directory. Same story, two tests.
  468. found = _load_resource_pack(exec_basename + ".pck") || _load_resource_pack(exec_filename + ".pck");
  469. }
  470. // If we opened our package, try and load our project.
  471. if (found) {
  472. Error err = _load_settings_text_or_binary("res://project.godot", "res://project.binary");
  473. if (err == OK && !p_ignore_override) {
  474. // Load overrides from the PCK and the executable location.
  475. // Optional, we don't mind if either fails.
  476. _load_settings_text("res://override.cfg");
  477. _load_settings_text(exec_path.get_base_dir().path_join("override.cfg"));
  478. }
  479. return err;
  480. }
  481. }
  482. // Try to use the filesystem for files, according to OS.
  483. // (Only Android -when reading from pck- and iOS use this.)
  484. if (!OS::get_singleton()->get_resource_dir().is_empty()) {
  485. Error err = _load_settings_text_or_binary("res://project.godot", "res://project.binary");
  486. if (err == OK && !p_ignore_override) {
  487. // Optional, we don't mind if it fails.
  488. _load_settings_text("res://override.cfg");
  489. }
  490. return err;
  491. }
  492. // Nothing was found, try to find a project file in provided path (`p_path`)
  493. // or, if requested (`p_upwards`) in parent directories.
  494. Ref<DirAccess> d = DirAccess::create(DirAccess::ACCESS_FILESYSTEM);
  495. ERR_FAIL_COND_V_MSG(d.is_null(), ERR_CANT_CREATE, "Cannot create DirAccess for path '" + p_path + "'.");
  496. d->change_dir(p_path);
  497. String current_dir = d->get_current_dir();
  498. bool found = false;
  499. Error err;
  500. while (true) {
  501. // Set the resource path early so things can be resolved when loading.
  502. resource_path = current_dir;
  503. resource_path = resource_path.replace("\\", "/"); // Windows path to Unix path just in case.
  504. err = _load_settings_text_or_binary(current_dir.path_join("project.godot"), current_dir.path_join("project.binary"));
  505. if (err == OK && !p_ignore_override) {
  506. // Optional, we don't mind if it fails.
  507. _load_settings_text(current_dir.path_join("override.cfg"));
  508. found = true;
  509. break;
  510. }
  511. if (p_upwards) {
  512. // Try to load settings ascending through parent directories
  513. d->change_dir("..");
  514. if (d->get_current_dir() == current_dir) {
  515. break; // not doing anything useful
  516. }
  517. current_dir = d->get_current_dir();
  518. } else {
  519. break;
  520. }
  521. }
  522. if (!found) {
  523. return err;
  524. }
  525. if (resource_path.length() && resource_path[resource_path.length() - 1] == '/') {
  526. resource_path = resource_path.substr(0, resource_path.length() - 1); // Chop end.
  527. }
  528. return OK;
  529. }
  530. Error ProjectSettings::setup(const String &p_path, const String &p_main_pack, bool p_upwards, bool p_ignore_override) {
  531. Error err = _setup(p_path, p_main_pack, p_upwards, p_ignore_override);
  532. if (err == OK && !p_ignore_override) {
  533. String custom_settings = GLOBAL_GET("application/config/project_settings_override");
  534. if (!custom_settings.is_empty()) {
  535. _load_settings_text(custom_settings);
  536. }
  537. }
  538. // Updating the default value after the project settings have loaded.
  539. bool use_hidden_directory = GLOBAL_GET("application/config/use_hidden_project_data_directory");
  540. project_data_dir_name = (use_hidden_directory ? "." : "") + PROJECT_DATA_DIR_NAME_SUFFIX;
  541. // Using GLOBAL_GET on every block for compressing can be slow, so assigning here.
  542. Compression::zstd_long_distance_matching = GLOBAL_GET("compression/formats/zstd/long_distance_matching");
  543. Compression::zstd_level = GLOBAL_GET("compression/formats/zstd/compression_level");
  544. Compression::zstd_window_log_size = GLOBAL_GET("compression/formats/zstd/window_log_size");
  545. Compression::zlib_level = GLOBAL_GET("compression/formats/zlib/compression_level");
  546. Compression::gzip_level = GLOBAL_GET("compression/formats/gzip/compression_level");
  547. project_loaded = err == OK;
  548. return err;
  549. }
  550. bool ProjectSettings::has_setting(String p_var) const {
  551. _THREAD_SAFE_METHOD_
  552. return props.has(p_var);
  553. }
  554. Error ProjectSettings::_load_settings_binary(const String &p_path) {
  555. Error err;
  556. Ref<FileAccess> f = FileAccess::open(p_path, FileAccess::READ, &err);
  557. if (err != OK) {
  558. return err;
  559. }
  560. uint8_t hdr[4];
  561. f->get_buffer(hdr, 4);
  562. 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).");
  563. uint32_t count = f->get_32();
  564. for (uint32_t i = 0; i < count; i++) {
  565. uint32_t slen = f->get_32();
  566. CharString cs;
  567. cs.resize(slen + 1);
  568. cs[slen] = 0;
  569. f->get_buffer((uint8_t *)cs.ptr(), slen);
  570. String key;
  571. key.parse_utf8(cs.ptr());
  572. uint32_t vlen = f->get_32();
  573. Vector<uint8_t> d;
  574. d.resize(vlen);
  575. f->get_buffer(d.ptrw(), vlen);
  576. Variant value;
  577. err = decode_variant(value, d.ptr(), d.size(), nullptr, true);
  578. ERR_CONTINUE_MSG(err != OK, "Error decoding property: " + key + ".");
  579. set(key, value);
  580. }
  581. return OK;
  582. }
  583. Error ProjectSettings::_load_settings_text(const String &p_path) {
  584. Error err;
  585. Ref<FileAccess> f = FileAccess::open(p_path, FileAccess::READ, &err);
  586. if (f.is_null()) {
  587. // FIXME: Above 'err' error code is ERR_FILE_CANT_OPEN if the file is missing
  588. // This needs to be streamlined if we want decent error reporting
  589. return ERR_FILE_NOT_FOUND;
  590. }
  591. VariantParser::StreamFile stream;
  592. stream.f = f;
  593. String assign;
  594. Variant value;
  595. VariantParser::Tag next_tag;
  596. int lines = 0;
  597. String error_text;
  598. String section;
  599. int config_version = 0;
  600. while (true) {
  601. assign = Variant();
  602. next_tag.fields.clear();
  603. next_tag.name = String();
  604. err = VariantParser::parse_tag_assign_eof(&stream, lines, error_text, next_tag, assign, value, nullptr, true);
  605. if (err == ERR_FILE_EOF) {
  606. // If we're loading a project.godot from source code, we can operate some
  607. // ProjectSettings conversions if need be.
  608. _convert_to_last_version(config_version);
  609. last_save_time = FileAccess::get_modified_time(get_resource_path().path_join("project.godot"));
  610. return OK;
  611. }
  612. ERR_FAIL_COND_V_MSG(err != OK, err, "Error parsing " + p_path + " at line " + itos(lines) + ": " + error_text + " File might be corrupted.");
  613. if (!assign.is_empty()) {
  614. if (section.is_empty() && assign == "config_version") {
  615. config_version = value;
  616. 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));
  617. } else {
  618. if (section.is_empty()) {
  619. set(assign, value);
  620. } else {
  621. set(section + "/" + assign, value);
  622. }
  623. }
  624. } else if (!next_tag.name.is_empty()) {
  625. section = next_tag.name;
  626. }
  627. }
  628. }
  629. Error ProjectSettings::_load_settings_text_or_binary(const String &p_text_path, const String &p_bin_path) {
  630. // Attempt first to load the binary project.godot file.
  631. Error err = _load_settings_binary(p_bin_path);
  632. if (err == OK) {
  633. return OK;
  634. } else if (err != ERR_FILE_NOT_FOUND) {
  635. // If the file exists but can't be loaded, we want to know it.
  636. ERR_PRINT("Couldn't load file '" + p_bin_path + "', error code " + itos(err) + ".");
  637. }
  638. // Fallback to text-based project.godot file if binary was not found.
  639. err = _load_settings_text(p_text_path);
  640. if (err == OK) {
  641. return OK;
  642. } else if (err != ERR_FILE_NOT_FOUND) {
  643. ERR_PRINT("Couldn't load file '" + p_text_path + "', error code " + itos(err) + ".");
  644. }
  645. return err;
  646. }
  647. Error ProjectSettings::load_custom(const String &p_path) {
  648. if (p_path.ends_with(".binary")) {
  649. return _load_settings_binary(p_path);
  650. }
  651. return _load_settings_text(p_path);
  652. }
  653. int ProjectSettings::get_order(const String &p_name) const {
  654. ERR_FAIL_COND_V_MSG(!props.has(p_name), -1, "Request for nonexistent project setting: " + p_name + ".");
  655. return props[p_name].order;
  656. }
  657. void ProjectSettings::set_order(const String &p_name, int p_order) {
  658. ERR_FAIL_COND_MSG(!props.has(p_name), "Request for nonexistent project setting: " + p_name + ".");
  659. props[p_name].order = p_order;
  660. }
  661. void ProjectSettings::set_builtin_order(const String &p_name) {
  662. ERR_FAIL_COND_MSG(!props.has(p_name), "Request for nonexistent project setting: " + p_name + ".");
  663. if (props[p_name].order >= NO_BUILTIN_ORDER_BASE) {
  664. props[p_name].order = last_builtin_order++;
  665. }
  666. }
  667. bool ProjectSettings::is_builtin_setting(const String &p_name) const {
  668. // Return true because a false negative is worse than a false positive.
  669. ERR_FAIL_COND_V_MSG(!props.has(p_name), true, "Request for nonexistent project setting: " + p_name + ".");
  670. return props[p_name].order < NO_BUILTIN_ORDER_BASE;
  671. }
  672. void ProjectSettings::clear(const String &p_name) {
  673. ERR_FAIL_COND_MSG(!props.has(p_name), "Request for nonexistent project setting: " + p_name + ".");
  674. props.erase(p_name);
  675. }
  676. Error ProjectSettings::save() {
  677. Error error = save_custom(get_resource_path().path_join("project.godot"));
  678. if (error == OK) {
  679. last_save_time = FileAccess::get_modified_time(get_resource_path().path_join("project.godot"));
  680. }
  681. return error;
  682. }
  683. 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) {
  684. Error err;
  685. Ref<FileAccess> file = FileAccess::open(p_file, FileAccess::WRITE, &err);
  686. ERR_FAIL_COND_V_MSG(err != OK, err, "Couldn't save project.binary at " + p_file + ".");
  687. uint8_t hdr[4] = { 'E', 'C', 'F', 'G' };
  688. file->store_buffer(hdr, 4);
  689. int count = 0;
  690. for (const KeyValue<String, List<String>> &E : p_props) {
  691. count += E.value.size();
  692. }
  693. if (!p_custom_features.is_empty()) {
  694. file->store_32(count + 1);
  695. // Store how many properties are saved, add one for custom features, which must always go first.
  696. String key = CoreStringNames::get_singleton()->_custom_features;
  697. file->store_pascal_string(key);
  698. int len;
  699. err = encode_variant(p_custom_features, nullptr, len, false);
  700. ERR_FAIL_COND_V(err != OK, err);
  701. Vector<uint8_t> buff;
  702. buff.resize(len);
  703. err = encode_variant(p_custom_features, buff.ptrw(), len, false);
  704. ERR_FAIL_COND_V(err != OK, err);
  705. file->store_32(len);
  706. file->store_buffer(buff.ptr(), buff.size());
  707. } else {
  708. file->store_32(count); //store how many properties are saved
  709. }
  710. for (const KeyValue<String, List<String>> &E : p_props) {
  711. for (const String &key : E.value) {
  712. String k = key;
  713. if (!E.key.is_empty()) {
  714. k = E.key + "/" + k;
  715. }
  716. Variant value;
  717. if (p_custom.has(k)) {
  718. value = p_custom[k];
  719. } else {
  720. value = get(k);
  721. }
  722. file->store_pascal_string(k);
  723. int len;
  724. err = encode_variant(value, nullptr, len, true);
  725. ERR_FAIL_COND_V_MSG(err != OK, ERR_INVALID_DATA, "Error when trying to encode Variant.");
  726. Vector<uint8_t> buff;
  727. buff.resize(len);
  728. err = encode_variant(value, buff.ptrw(), len, true);
  729. ERR_FAIL_COND_V_MSG(err != OK, ERR_INVALID_DATA, "Error when trying to encode Variant.");
  730. file->store_32(len);
  731. file->store_buffer(buff.ptr(), buff.size());
  732. }
  733. }
  734. return OK;
  735. }
  736. 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) {
  737. Error err;
  738. Ref<FileAccess> file = FileAccess::open(p_file, FileAccess::WRITE, &err);
  739. ERR_FAIL_COND_V_MSG(err != OK, err, "Couldn't save project.godot - " + p_file + ".");
  740. file->store_line("; Engine configuration file.");
  741. file->store_line("; It's best edited using the editor UI and not directly,");
  742. file->store_line("; since the parameters that go here are not all obvious.");
  743. file->store_line(";");
  744. file->store_line("; Format:");
  745. file->store_line("; [section] ; section goes between []");
  746. file->store_line("; param=value ; assign values to parameters");
  747. file->store_line("");
  748. file->store_string("config_version=" + itos(CONFIG_VERSION) + "\n");
  749. if (!p_custom_features.is_empty()) {
  750. file->store_string("custom_features=\"" + p_custom_features + "\"\n");
  751. }
  752. file->store_string("\n");
  753. for (const KeyValue<String, List<String>> &E : p_props) {
  754. if (E.key != p_props.begin()->key) {
  755. file->store_string("\n");
  756. }
  757. if (!E.key.is_empty()) {
  758. file->store_string("[" + E.key + "]\n\n");
  759. }
  760. for (const String &F : E.value) {
  761. String key = F;
  762. if (!E.key.is_empty()) {
  763. key = E.key + "/" + key;
  764. }
  765. Variant value;
  766. if (p_custom.has(key)) {
  767. value = p_custom[key];
  768. } else {
  769. value = get(key);
  770. }
  771. String vstr;
  772. VariantWriter::write_to_string(value, vstr);
  773. file->store_string(F.property_name_encode() + "=" + vstr + "\n");
  774. }
  775. }
  776. return OK;
  777. }
  778. Error ProjectSettings::_save_custom_bnd(const String &p_file) { // add other params as dictionary and array?
  779. return save_custom(p_file);
  780. }
  781. #ifdef TOOLS_ENABLED
  782. bool _csproj_exists(String p_root_dir) {
  783. Ref<DirAccess> dir = DirAccess::open(p_root_dir);
  784. dir->list_dir_begin();
  785. String file_name = dir->_get_next();
  786. while (file_name != "") {
  787. if (!dir->current_is_dir() && file_name.get_extension() == "csproj") {
  788. return true;
  789. }
  790. file_name = dir->_get_next();
  791. }
  792. return false;
  793. }
  794. #endif // TOOLS_ENABLED
  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 (_csproj_exists(get_resource_path())) {
  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("set_as_basic", "name", "basic"), &ProjectSettings::set_as_basic);
  1019. ClassDB::bind_method(D_METHOD("set_as_internal", "name", "internal"), &ProjectSettings::set_as_internal);
  1020. ClassDB::bind_method(D_METHOD("add_property_info", "hint"), &ProjectSettings::_add_property_info_bind);
  1021. ClassDB::bind_method(D_METHOD("set_restart_if_changed", "name", "restart"), &ProjectSettings::set_restart_if_changed);
  1022. ClassDB::bind_method(D_METHOD("clear", "name"), &ProjectSettings::clear);
  1023. ClassDB::bind_method(D_METHOD("localize_path", "path"), &ProjectSettings::localize_path);
  1024. ClassDB::bind_method(D_METHOD("globalize_path", "path"), &ProjectSettings::globalize_path);
  1025. ClassDB::bind_method(D_METHOD("save"), &ProjectSettings::save);
  1026. ClassDB::bind_method(D_METHOD("load_resource_pack", "pack", "replace_files", "offset"), &ProjectSettings::_load_resource_pack, DEFVAL(true), DEFVAL(0));
  1027. ClassDB::bind_method(D_METHOD("save_custom", "file"), &ProjectSettings::_save_custom_bnd);
  1028. }
  1029. void ProjectSettings::_add_builtin_input_map() {
  1030. if (InputMap::get_singleton()) {
  1031. HashMap<String, List<Ref<InputEvent>>> builtins = InputMap::get_singleton()->get_builtins();
  1032. for (KeyValue<String, List<Ref<InputEvent>>> &E : builtins) {
  1033. Array events;
  1034. // Convert list of input events into array
  1035. for (List<Ref<InputEvent>>::Element *I = E.value.front(); I; I = I->next()) {
  1036. events.push_back(I->get());
  1037. }
  1038. Dictionary action;
  1039. action["deadzone"] = Variant(0.5f);
  1040. action["events"] = events;
  1041. String action_name = "input/" + E.key;
  1042. GLOBAL_DEF(action_name, action);
  1043. input_presets.push_back(action_name);
  1044. }
  1045. }
  1046. }
  1047. ProjectSettings::ProjectSettings() {
  1048. // Initialization of engine variables should be done in the setup() method,
  1049. // so that the values can be overridden from project.godot or project.binary.
  1050. CRASH_COND_MSG(singleton != nullptr, "Instantiating a new ProjectSettings singleton is not supported.");
  1051. singleton = this;
  1052. GLOBAL_DEF_BASIC("application/config/name", "");
  1053. GLOBAL_DEF_BASIC(PropertyInfo(Variant::DICTIONARY, "application/config/name_localized", PROPERTY_HINT_LOCALIZABLE_STRING), Dictionary());
  1054. GLOBAL_DEF_BASIC(PropertyInfo(Variant::STRING, "application/config/description", PROPERTY_HINT_MULTILINE_TEXT), "");
  1055. GLOBAL_DEF_INTERNAL(PropertyInfo(Variant::STRING, "application/config/tags"), PackedStringArray());
  1056. GLOBAL_DEF_BASIC(PropertyInfo(Variant::STRING, "application/run/main_scene", PROPERTY_HINT_FILE, "*.tscn,*.scn,*.res"), "");
  1057. GLOBAL_DEF("application/run/disable_stdout", false);
  1058. GLOBAL_DEF("application/run/disable_stderr", false);
  1059. GLOBAL_DEF_RST("application/config/use_hidden_project_data_directory", true);
  1060. GLOBAL_DEF("application/config/use_custom_user_dir", false);
  1061. GLOBAL_DEF("application/config/custom_user_dir_name", "");
  1062. GLOBAL_DEF("application/config/project_settings_override", "");
  1063. GLOBAL_DEF("application/run/main_loop_type", "SceneTree");
  1064. GLOBAL_DEF("application/config/auto_accept_quit", true);
  1065. GLOBAL_DEF("application/config/quit_on_go_back", true);
  1066. // The default window size is tuned to:
  1067. // - Have a 16:9 aspect ratio,
  1068. // - Have both dimensions divisible by 8 to better play along with video recording,
  1069. // - Be displayable correctly in windowed mode on a 1366×768 display (tested on Windows 10 with default settings).
  1070. GLOBAL_DEF_BASIC(PropertyInfo(Variant::INT, "display/window/size/viewport_width", PROPERTY_HINT_RANGE, "0,7680,1,or_greater"), 1152); // 8K resolution
  1071. GLOBAL_DEF_BASIC(PropertyInfo(Variant::INT, "display/window/size/viewport_height", PROPERTY_HINT_RANGE, "0,4320,1,or_greater"), 648); // 8K resolution
  1072. GLOBAL_DEF_BASIC(PropertyInfo(Variant::INT, "display/window/size/mode", PROPERTY_HINT_ENUM, "Windowed,Minimized,Maximized,Fullscreen,Exclusive Fullscreen"), 0);
  1073. // Keep the enum values in sync with the `DisplayServer::SCREEN_` enum.
  1074. 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);
  1075. GLOBAL_DEF_BASIC(PropertyInfo(Variant::VECTOR2I, "display/window/size/initial_position"), Vector2i());
  1076. GLOBAL_DEF_BASIC(PropertyInfo(Variant::INT, "display/window/size/initial_screen", PROPERTY_HINT_RANGE, "0,64,1,or_greater"), 0);
  1077. GLOBAL_DEF_BASIC("display/window/size/resizable", true);
  1078. GLOBAL_DEF_BASIC("display/window/size/borderless", false);
  1079. GLOBAL_DEF("display/window/size/always_on_top", false);
  1080. GLOBAL_DEF("display/window/size/transparent", false);
  1081. GLOBAL_DEF("display/window/size/extend_to_title", false);
  1082. GLOBAL_DEF("display/window/size/no_focus", false);
  1083. GLOBAL_DEF(PropertyInfo(Variant::INT, "display/window/size/window_width_override", PROPERTY_HINT_RANGE, "0,7680,1,or_greater"), 0); // 8K resolution
  1084. GLOBAL_DEF(PropertyInfo(Variant::INT, "display/window/size/window_height_override", PROPERTY_HINT_RANGE, "0,4320,1,or_greater"), 0); // 8K resolution
  1085. GLOBAL_DEF("display/window/energy_saving/keep_screen_on", true);
  1086. GLOBAL_DEF("display/window/energy_saving/keep_screen_on.editor", false);
  1087. GLOBAL_DEF_BASIC(PropertyInfo(Variant::STRING, "audio/buses/default_bus_layout", PROPERTY_HINT_FILE, "*.tres"), "res://default_bus_layout.tres");
  1088. GLOBAL_DEF_RST("audio/general/text_to_speech", false);
  1089. GLOBAL_DEF_RST(PropertyInfo(Variant::FLOAT, "audio/general/2d_panning_strength", PROPERTY_HINT_RANGE, "0,2,0.01"), 0.5f);
  1090. GLOBAL_DEF_RST(PropertyInfo(Variant::FLOAT, "audio/general/3d_panning_strength", PROPERTY_HINT_RANGE, "0,2,0.01"), 0.5f);
  1091. PackedStringArray extensions;
  1092. extensions.push_back("gd");
  1093. if (Engine::get_singleton()->has_singleton("GodotSharp")) {
  1094. extensions.push_back("cs");
  1095. }
  1096. extensions.push_back("gdshader");
  1097. GLOBAL_DEF(PropertyInfo(Variant::PACKED_STRING_ARRAY, "editor/script/search_in_file_extensions"), extensions);
  1098. _add_builtin_input_map();
  1099. // Keep the enum values in sync with the `DisplayServer::ScreenOrientation` enum.
  1100. 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");
  1101. GLOBAL_DEF("display/window/subwindows/embed_subwindows", true);
  1102. // Keep the enum values in sync with the `DisplayServer::VSyncMode` enum.
  1103. custom_prop_info["display/window/vsync/vsync_mode"] = PropertyInfo(Variant::INT, "display/window/vsync/vsync_mode", PROPERTY_HINT_ENUM, "Disabled,Enabled,Adaptive,Mailbox");
  1104. 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");
  1105. GLOBAL_DEF("physics/2d/run_on_separate_thread", false);
  1106. GLOBAL_DEF("physics/3d/run_on_separate_thread", false);
  1107. GLOBAL_DEF_BASIC(PropertyInfo(Variant::STRING, "display/window/stretch/mode", PROPERTY_HINT_ENUM, "disabled,canvas_items,viewport"), "disabled");
  1108. GLOBAL_DEF_BASIC(PropertyInfo(Variant::STRING, "display/window/stretch/aspect", PROPERTY_HINT_ENUM, "ignore,keep,keep_width,keep_height,expand"), "keep");
  1109. GLOBAL_DEF_BASIC(PropertyInfo(Variant::FLOAT, "display/window/stretch/scale", PROPERTY_HINT_RANGE, "0.5,8.0,0.01"), 1.0);
  1110. GLOBAL_DEF(PropertyInfo(Variant::INT, "debug/settings/profiler/max_functions", PROPERTY_HINT_RANGE, "128,65535,1"), 16384);
  1111. GLOBAL_DEF(PropertyInfo(Variant::BOOL, "compression/formats/zstd/long_distance_matching"), Compression::zstd_long_distance_matching);
  1112. GLOBAL_DEF(PropertyInfo(Variant::INT, "compression/formats/zstd/compression_level", PROPERTY_HINT_RANGE, "1,22,1"), Compression::zstd_level);
  1113. GLOBAL_DEF(PropertyInfo(Variant::INT, "compression/formats/zstd/window_log_size", PROPERTY_HINT_RANGE, "10,30,1"), Compression::zstd_window_log_size);
  1114. GLOBAL_DEF(PropertyInfo(Variant::INT, "compression/formats/zlib/compression_level", PROPERTY_HINT_RANGE, "-1,9,1"), Compression::zlib_level);
  1115. GLOBAL_DEF(PropertyInfo(Variant::INT, "compression/formats/gzip/compression_level", PROPERTY_HINT_RANGE, "-1,9,1"), Compression::gzip_level);
  1116. GLOBAL_DEF("debug/settings/crash_handler/message",
  1117. String("Please include this when reporting the bug to the project developer."));
  1118. GLOBAL_DEF("debug/settings/crash_handler/message.editor",
  1119. String("Please include this when reporting the bug on: https://github.com/godotengine/godot/issues"));
  1120. GLOBAL_DEF_RST(PropertyInfo(Variant::INT, "rendering/occlusion_culling/bvh_build_quality", PROPERTY_HINT_ENUM, "Low,Medium,High"), 2);
  1121. 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.
  1122. GLOBAL_DEF_RST("internationalization/rendering/force_right_to_left_layout_direction", false);
  1123. 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);
  1124. GLOBAL_DEF(PropertyInfo(Variant::INT, "gui/timers/incremental_search_max_interval_msec", PROPERTY_HINT_RANGE, "0,10000,1,or_greater"), 2000);
  1125. GLOBAL_DEF_BASIC("gui/common/snap_controls_to_pixels", true);
  1126. GLOBAL_DEF_BASIC("gui/fonts/dynamic_fonts/use_oversampling", true);
  1127. GLOBAL_DEF("rendering/rendering_device/staging_buffer/block_size_kb", 256);
  1128. GLOBAL_DEF("rendering/rendering_device/staging_buffer/max_size_mb", 128);
  1129. GLOBAL_DEF("rendering/rendering_device/staging_buffer/texture_upload_region_size_px", 64);
  1130. GLOBAL_DEF("rendering/rendering_device/pipeline_cache/save_chunk_size_mb", 3.0);
  1131. GLOBAL_DEF("rendering/rendering_device/vulkan/max_descriptors_per_pool", 64);
  1132. GLOBAL_DEF_BASIC(PropertyInfo(Variant::INT, "rendering/textures/canvas_textures/default_texture_filter", PROPERTY_HINT_ENUM, "Nearest,Linear,Linear Mipmap,Nearest Mipmap"), 1);
  1133. GLOBAL_DEF_BASIC(PropertyInfo(Variant::INT, "rendering/textures/canvas_textures/default_texture_repeat", PROPERTY_HINT_ENUM, "Disable,Enable,Mirror"), 0);
  1134. // 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.
  1135. GLOBAL_DEF_INTERNAL("application/config/features", PackedStringArray());
  1136. GLOBAL_DEF_INTERNAL("internationalization/locale/translation_remaps", PackedStringArray());
  1137. GLOBAL_DEF_INTERNAL("internationalization/locale/translations", PackedStringArray());
  1138. GLOBAL_DEF_INTERNAL("internationalization/locale/translations_pot_files", PackedStringArray());
  1139. }
  1140. ProjectSettings::~ProjectSettings() {
  1141. singleton = nullptr;
  1142. }