project_settings.cpp 51 KB

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