project_settings.cpp 47 KB

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