project_settings.cpp 37 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212
  1. /*************************************************************************/
  2. /* project_settings.cpp */
  3. /*************************************************************************/
  4. /* This file is part of: */
  5. /* GODOT ENGINE */
  6. /* https://godotengine.org */
  7. /*************************************************************************/
  8. /* Copyright (c) 2007-2019 Juan Linietsky, Ariel Manzur. */
  9. /* Copyright (c) 2014-2019 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/bind/core_bind.h"
  32. #include "core/core_string_names.h"
  33. #include "core/io/file_access_network.h"
  34. #include "core/io/file_access_pack.h"
  35. #include "core/io/marshalls.h"
  36. #include "core/os/dir_access.h"
  37. #include "core/os/file_access.h"
  38. #include "core/os/keyboard.h"
  39. #include "core/os/os.h"
  40. #include "core/variant_parser.h"
  41. #include <zlib.h>
  42. ProjectSettings *ProjectSettings::singleton = NULL;
  43. ProjectSettings *ProjectSettings::get_singleton() {
  44. return singleton;
  45. }
  46. String ProjectSettings::get_resource_path() const {
  47. return resource_path;
  48. };
  49. String ProjectSettings::localize_path(const String &p_path) const {
  50. if (resource_path == "")
  51. return p_path; //not initialized yet
  52. if (p_path.begins_with("res://") || p_path.begins_with("user://") ||
  53. (p_path.is_abs_path() && !p_path.begins_with(resource_path)))
  54. return p_path.simplify_path();
  55. DirAccess *dir = DirAccess::create(DirAccess::ACCESS_FILESYSTEM);
  56. String path = p_path.replace("\\", "/").simplify_path();
  57. if (dir->change_dir(path) == OK) {
  58. String cwd = dir->get_current_dir();
  59. cwd = cwd.replace("\\", "/");
  60. memdelete(dir);
  61. // Ensure that we end with a '/'.
  62. // This is important to ensure that we do not wrongly localize the resource path
  63. // in an absolute path that just happens to contain this string but points to a
  64. // different folder (e.g. "/my/project" as resource_path would be contained in
  65. // "/my/project_data", even though the latter is not part of res://.
  66. // `plus_file("")` is an easy way to ensure we have a trailing '/'.
  67. const String res_path = resource_path.plus_file("");
  68. if (!cwd.begins_with(res_path)) {
  69. return p_path;
  70. };
  71. return cwd.replace_first(res_path, "res://");
  72. } else {
  73. memdelete(dir);
  74. int sep = path.find_last("/");
  75. if (sep == -1) {
  76. return "res://" + path;
  77. };
  78. String parent = path.substr(0, sep);
  79. String plocal = localize_path(parent);
  80. if (plocal == "") {
  81. return "";
  82. };
  83. return plocal + path.substr(sep, path.size() - sep);
  84. };
  85. }
  86. void ProjectSettings::set_initial_value(const String &p_name, const Variant &p_value) {
  87. ERR_FAIL_COND(!props.has(p_name));
  88. props[p_name].initial = p_value;
  89. }
  90. void ProjectSettings::set_restart_if_changed(const String &p_name, bool p_restart) {
  91. ERR_FAIL_COND(!props.has(p_name));
  92. props[p_name].restart_if_changed = p_restart;
  93. }
  94. String ProjectSettings::globalize_path(const String &p_path) const {
  95. if (p_path.begins_with("res://")) {
  96. if (resource_path != "") {
  97. return p_path.replace("res:/", resource_path);
  98. };
  99. return p_path.replace("res://", "");
  100. } else if (p_path.begins_with("user://")) {
  101. String data_dir = OS::get_singleton()->get_user_data_dir();
  102. if (data_dir != "") {
  103. return p_path.replace("user:/", data_dir);
  104. };
  105. return p_path.replace("user://", "");
  106. }
  107. return p_path;
  108. }
  109. bool ProjectSettings::_set(const StringName &p_name, const Variant &p_value) {
  110. _THREAD_SAFE_METHOD_
  111. if (p_value.get_type() == Variant::NIL)
  112. props.erase(p_name);
  113. else {
  114. if (p_name == CoreStringNames::get_singleton()->_custom_features) {
  115. Vector<String> custom_feature_array = String(p_value).split(",");
  116. for (int i = 0; i < custom_feature_array.size(); i++) {
  117. custom_features.insert(custom_feature_array[i]);
  118. }
  119. return true;
  120. }
  121. if (!disable_feature_overrides) {
  122. int dot = p_name.operator String().find(".");
  123. if (dot != -1) {
  124. Vector<String> s = p_name.operator String().split(".");
  125. bool override_valid = false;
  126. for (int i = 1; i < s.size(); i++) {
  127. String feature = s[i].strip_edges();
  128. if (OS::get_singleton()->has_feature(feature) || custom_features.has(feature)) {
  129. override_valid = true;
  130. break;
  131. }
  132. }
  133. if (override_valid) {
  134. feature_overrides[s[0]] = p_name;
  135. }
  136. }
  137. }
  138. if (props.has(p_name)) {
  139. if (!props[p_name].overridden)
  140. props[p_name].variant = p_value;
  141. } else {
  142. props[p_name] = VariantContainer(p_value, last_order++);
  143. }
  144. }
  145. return true;
  146. }
  147. bool ProjectSettings::_get(const StringName &p_name, Variant &r_ret) const {
  148. _THREAD_SAFE_METHOD_
  149. StringName name = p_name;
  150. if (!disable_feature_overrides && feature_overrides.has(name)) {
  151. name = feature_overrides[name];
  152. }
  153. if (!props.has(name)) {
  154. WARN_PRINTS("Property not found: " + String(name));
  155. return false;
  156. }
  157. r_ret = props[name].variant;
  158. return true;
  159. }
  160. struct _VCSort {
  161. String name;
  162. Variant::Type type;
  163. int order;
  164. int flags;
  165. bool operator<(const _VCSort &p_vcs) const { return order == p_vcs.order ? name < p_vcs.name : order < p_vcs.order; }
  166. };
  167. void ProjectSettings::_get_property_list(List<PropertyInfo> *p_list) const {
  168. _THREAD_SAFE_METHOD_
  169. Set<_VCSort> vclist;
  170. for (Map<StringName, VariantContainer>::Element *E = props.front(); E; E = E->next()) {
  171. const VariantContainer *v = &E->get();
  172. if (v->hide_from_editor)
  173. continue;
  174. _VCSort vc;
  175. vc.name = E->key();
  176. vc.order = v->order;
  177. vc.type = v->variant.get_type();
  178. if (vc.name.begins_with("input/") || vc.name.begins_with("import/") || vc.name.begins_with("export/") || vc.name.begins_with("/remap") || vc.name.begins_with("/locale") || vc.name.begins_with("/autoload"))
  179. vc.flags = PROPERTY_USAGE_STORAGE;
  180. else
  181. vc.flags = PROPERTY_USAGE_EDITOR | PROPERTY_USAGE_STORAGE;
  182. if (v->restart_if_changed) {
  183. vc.flags |= PROPERTY_USAGE_RESTART_IF_CHANGED;
  184. }
  185. vclist.insert(vc);
  186. }
  187. for (Set<_VCSort>::Element *E = vclist.front(); E; E = E->next()) {
  188. String prop_info_name = E->get().name;
  189. int dot = prop_info_name.find(".");
  190. if (dot != -1)
  191. prop_info_name = prop_info_name.substr(0, dot);
  192. if (custom_prop_info.has(prop_info_name)) {
  193. PropertyInfo pi = custom_prop_info[prop_info_name];
  194. pi.name = E->get().name;
  195. pi.usage = E->get().flags;
  196. p_list->push_back(pi);
  197. } else
  198. p_list->push_back(PropertyInfo(E->get().type, E->get().name, PROPERTY_HINT_NONE, "", E->get().flags));
  199. }
  200. }
  201. bool ProjectSettings::_load_resource_pack(const String &p_pack) {
  202. if (PackedData::get_singleton()->is_disabled())
  203. return false;
  204. bool ok = PackedData::get_singleton()->add_pack(p_pack) == OK;
  205. if (!ok)
  206. return false;
  207. //if data.pck is found, all directory access will be from here
  208. DirAccess::make_default<DirAccessPack>(DirAccess::ACCESS_RESOURCES);
  209. using_datapack = true;
  210. return true;
  211. }
  212. void ProjectSettings::_convert_to_last_version(int p_from_version) {
  213. if (p_from_version <= 3) {
  214. // Converts the actions from array to dictionary (array of events to dictionary with deadzone + events)
  215. for (Map<StringName, ProjectSettings::VariantContainer>::Element *E = props.front(); E; E = E->next()) {
  216. Variant value = E->get().variant;
  217. if (String(E->key()).begins_with("input/") && value.get_type() == Variant::ARRAY) {
  218. Array array = value;
  219. Dictionary action;
  220. action["deadzone"] = Variant(0.5f);
  221. action["events"] = array;
  222. E->get().variant = action;
  223. }
  224. }
  225. }
  226. }
  227. /*
  228. * This method is responsible for loading a project.godot file and/or data file
  229. * using the following merit order:
  230. * - If using NetworkClient, try to lookup project file or fail.
  231. * - If --main-pack was passed by the user (`p_main_pack`), load it or fail.
  232. * - Search for .pck file matching binary name. There are two possibilities:
  233. * o exec_path.get_basename() + '.pck' (e.g. 'win_game.exe' -> 'win_game.pck')
  234. * o exec_path + '.pck' (e.g. 'linux_game' -> 'linux_game.pck')
  235. * For each tentative, if the file exists, load it or fail.
  236. * - On relevant platforms (Android/iOS), lookup project file in OS resource path.
  237. * If found, load it or fail.
  238. * - Lookup project file in passed `p_path` (--path passed by the user), i.e. we
  239. * are running from source code.
  240. * If not found and `p_upwards` is true (--upwards passed by the user), look for
  241. * project files in parent folders up to the system root (used to run a game
  242. * from command line while in a subfolder).
  243. * If a project file is found, load it or fail.
  244. * If nothing was found, error out.
  245. */
  246. Error ProjectSettings::_setup(const String &p_path, const String &p_main_pack, bool p_upwards) {
  247. // If looking for files in a network client, use it directly
  248. if (FileAccessNetworkClient::get_singleton()) {
  249. Error err = _load_settings_text_or_binary("res://project.godot", "res://project.binary");
  250. if (err == OK) {
  251. // Optional, we don't mind if it fails
  252. _load_settings_text("res://override.cfg");
  253. }
  254. return err;
  255. }
  256. // Attempt with a user-defined main pack first
  257. if (p_main_pack != "") {
  258. bool ok = _load_resource_pack(p_main_pack);
  259. ERR_FAIL_COND_V(!ok, ERR_CANT_OPEN);
  260. Error err = _load_settings_text_or_binary("res://project.godot", "res://project.binary");
  261. if (err == OK) {
  262. // Load override from location of the main pack
  263. // Optional, we don't mind if it fails
  264. _load_settings_text(p_main_pack.get_base_dir().plus_file("override.cfg"));
  265. }
  266. return err;
  267. }
  268. // Attempt with exec_name.pck
  269. // (This is the usual case when distributing a Godot game.)
  270. // Based on the OS, it can be the exec path + '.pck' (Linux w/o extension, macOS in .app bundle)
  271. // or the exec path's basename + '.pck' (Windows).
  272. // We need to test both possibilities as extensions for Linux binaries are optional
  273. // (so both 'mygame.bin' and 'mygame' should be able to find 'mygame.pck').
  274. String exec_path = OS::get_singleton()->get_executable_path();
  275. if (exec_path != "") {
  276. bool found = false;
  277. String exec_dir = exec_path.get_base_dir();
  278. String exec_filename = exec_path.get_file();
  279. String exec_basename = exec_filename.get_basename();
  280. // Try to load data pack at the location of the executable
  281. // As mentioned above, we have two potential names to attempt
  282. if (_load_resource_pack(exec_dir.plus_file(exec_basename + ".pck")) ||
  283. _load_resource_pack(exec_dir.plus_file(exec_filename + ".pck"))) {
  284. found = true;
  285. } else {
  286. // If we couldn't find them next to the executable, we attempt
  287. // the current working directory. Same story, two tests.
  288. if (_load_resource_pack(exec_basename + ".pck") ||
  289. _load_resource_pack(exec_filename + ".pck")) {
  290. found = true;
  291. }
  292. }
  293. // If we opened our package, try and load our project
  294. if (found) {
  295. Error err = _load_settings_text_or_binary("res://project.godot", "res://project.binary");
  296. if (err == OK) {
  297. // Load override from location of executable
  298. // Optional, we don't mind if it fails
  299. _load_settings_text(exec_path.get_base_dir().plus_file("override.cfg"));
  300. }
  301. return err;
  302. }
  303. }
  304. // Try to use the filesystem for files, according to OS. (only Android -when reading from pck- and iOS use this)
  305. if (OS::get_singleton()->get_resource_dir() != "") {
  306. // OS will call ProjectSettings->get_resource_path which will be empty if not overridden!
  307. // If the OS would rather use a specific location, then it will not be empty.
  308. resource_path = OS::get_singleton()->get_resource_dir().replace("\\", "/");
  309. if (resource_path != "" && resource_path[resource_path.length() - 1] == '/') {
  310. resource_path = resource_path.substr(0, resource_path.length() - 1); // chop end
  311. }
  312. Error err = _load_settings_text_or_binary("res://project.godot", "res://project.binary");
  313. if (err == OK) {
  314. // Optional, we don't mind if it fails
  315. _load_settings_text("res://override.cfg");
  316. }
  317. return err;
  318. }
  319. // Nothing was found, try to find a project file in provided path (`p_path`)
  320. // or, if requested (`p_upwards`) in parent directories.
  321. DirAccess *d = DirAccess::create(DirAccess::ACCESS_FILESYSTEM);
  322. ERR_FAIL_COND_V(!d, ERR_CANT_CREATE);
  323. d->change_dir(p_path);
  324. String current_dir = d->get_current_dir();
  325. String candidate = current_dir;
  326. bool found = false;
  327. Error err;
  328. while (true) {
  329. err = _load_settings_text_or_binary(current_dir.plus_file("project.godot"), current_dir.plus_file("project.binary"));
  330. if (err == OK) {
  331. // Optional, we don't mind if it fails
  332. _load_settings_text(current_dir.plus_file("override.cfg"));
  333. candidate = current_dir;
  334. found = true;
  335. break;
  336. }
  337. if (p_upwards) {
  338. // Try to load settings ascending through parent directories
  339. d->change_dir("..");
  340. if (d->get_current_dir() == current_dir)
  341. break; // not doing anything useful
  342. current_dir = d->get_current_dir();
  343. } else {
  344. break;
  345. }
  346. }
  347. resource_path = candidate;
  348. resource_path = resource_path.replace("\\", "/"); // windows path to unix path just in case
  349. memdelete(d);
  350. if (!found)
  351. return err;
  352. if (resource_path.length() && resource_path[resource_path.length() - 1] == '/')
  353. resource_path = resource_path.substr(0, resource_path.length() - 1); // chop end
  354. return OK;
  355. }
  356. Error ProjectSettings::setup(const String &p_path, const String &p_main_pack, bool p_upwards) {
  357. Error err = _setup(p_path, p_main_pack, p_upwards);
  358. if (err == OK) {
  359. String custom_settings = GLOBAL_DEF("application/config/project_settings_override", "");
  360. if (custom_settings != "") {
  361. _load_settings_text(custom_settings);
  362. }
  363. }
  364. return err;
  365. }
  366. bool ProjectSettings::has_setting(String p_var) const {
  367. _THREAD_SAFE_METHOD_
  368. return props.has(p_var);
  369. }
  370. void ProjectSettings::set_registering_order(bool p_enable) {
  371. registering_order = p_enable;
  372. }
  373. Error ProjectSettings::_load_settings_binary(const String p_path) {
  374. Error err;
  375. FileAccess *f = FileAccess::open(p_path, FileAccess::READ, &err);
  376. if (err != OK) {
  377. return err;
  378. }
  379. uint8_t hdr[4];
  380. f->get_buffer(hdr, 4);
  381. if (hdr[0] != 'E' || hdr[1] != 'C' || hdr[2] != 'F' || hdr[3] != 'G') {
  382. memdelete(f);
  383. ERR_EXPLAIN("Corrupted header in binary project.binary (not ECFG)");
  384. ERR_FAIL_V(ERR_FILE_CORRUPT);
  385. }
  386. uint32_t count = f->get_32();
  387. for (uint32_t i = 0; i < count; i++) {
  388. uint32_t slen = f->get_32();
  389. CharString cs;
  390. cs.resize(slen + 1);
  391. cs[slen] = 0;
  392. f->get_buffer((uint8_t *)cs.ptr(), slen);
  393. String key;
  394. key.parse_utf8(cs.ptr());
  395. uint32_t vlen = f->get_32();
  396. Vector<uint8_t> d;
  397. d.resize(vlen);
  398. f->get_buffer(d.ptrw(), vlen);
  399. Variant value;
  400. err = decode_variant(value, d.ptr(), d.size(), NULL, true);
  401. ERR_EXPLAIN("Error decoding property: " + key);
  402. ERR_CONTINUE(err != OK);
  403. set(key, value);
  404. }
  405. return OK;
  406. }
  407. Error ProjectSettings::_load_settings_text(const String p_path) {
  408. Error err;
  409. FileAccess *f = FileAccess::open(p_path, FileAccess::READ, &err);
  410. if (!f) {
  411. // FIXME: Above 'err' error code is ERR_FILE_CANT_OPEN if the file is missing
  412. // This needs to be streamlined if we want decent error reporting
  413. return ERR_FILE_NOT_FOUND;
  414. }
  415. VariantParser::StreamFile stream;
  416. stream.f = f;
  417. String assign;
  418. Variant value;
  419. VariantParser::Tag next_tag;
  420. int lines = 0;
  421. String error_text;
  422. String section;
  423. int config_version = 0;
  424. while (true) {
  425. assign = Variant();
  426. next_tag.fields.clear();
  427. next_tag.name = String();
  428. err = VariantParser::parse_tag_assign_eof(&stream, lines, error_text, next_tag, assign, value, NULL, true);
  429. if (err == ERR_FILE_EOF) {
  430. memdelete(f);
  431. // If we're loading a project.godot from source code, we can operate some
  432. // ProjectSettings conversions if need be.
  433. _convert_to_last_version(config_version);
  434. return OK;
  435. } else if (err != OK) {
  436. ERR_PRINTS("Error parsing " + p_path + " at line " + itos(lines) + ": " + error_text + " File might be corrupted.");
  437. memdelete(f);
  438. return err;
  439. }
  440. if (assign != String()) {
  441. if (section == String() && assign == "config_version") {
  442. config_version = value;
  443. if (config_version > CONFIG_VERSION) {
  444. memdelete(f);
  445. ERR_EXPLAIN(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));
  446. ERR_FAIL_V(ERR_FILE_CANT_OPEN);
  447. }
  448. } else {
  449. if (section == String()) {
  450. set(assign, value);
  451. } else {
  452. set(section + "/" + assign, value);
  453. }
  454. }
  455. } else if (next_tag.name != String()) {
  456. section = next_tag.name;
  457. }
  458. }
  459. }
  460. Error ProjectSettings::_load_settings_text_or_binary(const String p_text_path, const String p_bin_path) {
  461. // Attempt first to load the text-based project.godot file
  462. Error err_text = _load_settings_text(p_text_path);
  463. if (err_text == OK) {
  464. return OK;
  465. } else if (err_text != ERR_FILE_NOT_FOUND) {
  466. // If the text-based file exists but can't be loaded, we want to know it
  467. ERR_PRINTS("Couldn't load file '" + p_text_path + "', error code " + itos(err_text) + ".");
  468. return err_text;
  469. }
  470. // Fallback to binary project.binary file if text-based was not found
  471. Error err_bin = _load_settings_binary(p_bin_path);
  472. return err_bin;
  473. }
  474. int ProjectSettings::get_order(const String &p_name) const {
  475. ERR_FAIL_COND_V(!props.has(p_name), -1);
  476. return props[p_name].order;
  477. }
  478. void ProjectSettings::set_order(const String &p_name, int p_order) {
  479. ERR_FAIL_COND(!props.has(p_name));
  480. props[p_name].order = p_order;
  481. }
  482. void ProjectSettings::set_builtin_order(const String &p_name) {
  483. ERR_FAIL_COND(!props.has(p_name));
  484. if (props[p_name].order >= NO_BUILTIN_ORDER_BASE) {
  485. props[p_name].order = last_builtin_order++;
  486. }
  487. }
  488. void ProjectSettings::clear(const String &p_name) {
  489. ERR_FAIL_COND(!props.has(p_name));
  490. props.erase(p_name);
  491. }
  492. Error ProjectSettings::save() {
  493. return save_custom(get_resource_path().plus_file("project.godot"));
  494. }
  495. Error ProjectSettings::_save_settings_binary(const String &p_file, const Map<String, List<String> > &props, const CustomMap &p_custom, const String &p_custom_features) {
  496. Error err;
  497. FileAccess *file = FileAccess::open(p_file, FileAccess::WRITE, &err);
  498. if (err != OK) {
  499. ERR_EXPLAIN("Couldn't save project.binary at " + p_file);
  500. ERR_FAIL_COND_V(err, err);
  501. }
  502. uint8_t hdr[4] = { 'E', 'C', 'F', 'G' };
  503. file->store_buffer(hdr, 4);
  504. int count = 0;
  505. for (Map<String, List<String> >::Element *E = props.front(); E; E = E->next()) {
  506. for (List<String>::Element *F = E->get().front(); F; F = F->next()) {
  507. count++;
  508. }
  509. }
  510. if (p_custom_features != String()) {
  511. file->store_32(count + 1);
  512. //store how many properties are saved, add one for custom featuers, which must always go first
  513. String key = CoreStringNames::get_singleton()->_custom_features;
  514. file->store_32(key.length());
  515. file->store_string(key);
  516. int len;
  517. err = encode_variant(p_custom_features, NULL, len, false);
  518. if (err != OK) {
  519. memdelete(file);
  520. ERR_FAIL_V(err);
  521. }
  522. Vector<uint8_t> buff;
  523. buff.resize(len);
  524. err = encode_variant(p_custom_features, buff.ptrw(), len, false);
  525. if (err != OK) {
  526. memdelete(file);
  527. ERR_FAIL_V(err);
  528. }
  529. file->store_32(len);
  530. file->store_buffer(buff.ptr(), buff.size());
  531. } else {
  532. file->store_32(count); //store how many properties are saved
  533. }
  534. for (Map<String, List<String> >::Element *E = props.front(); E; E = E->next()) {
  535. for (List<String>::Element *F = E->get().front(); F; F = F->next()) {
  536. String key = F->get();
  537. if (E->key() != "")
  538. key = E->key() + "/" + key;
  539. Variant value;
  540. if (p_custom.has(key))
  541. value = p_custom[key];
  542. else
  543. value = get(key);
  544. file->store_32(key.length());
  545. file->store_string(key);
  546. int len;
  547. err = encode_variant(value, NULL, len, true);
  548. if (err != OK)
  549. memdelete(file);
  550. ERR_FAIL_COND_V(err != OK, ERR_INVALID_DATA);
  551. Vector<uint8_t> buff;
  552. buff.resize(len);
  553. err = encode_variant(value, buff.ptrw(), len, true);
  554. if (err != OK)
  555. memdelete(file);
  556. ERR_FAIL_COND_V(err != OK, ERR_INVALID_DATA);
  557. file->store_32(len);
  558. file->store_buffer(buff.ptr(), buff.size());
  559. }
  560. }
  561. file->close();
  562. memdelete(file);
  563. return OK;
  564. }
  565. Error ProjectSettings::_save_settings_text(const String &p_file, const Map<String, List<String> > &props, const CustomMap &p_custom, const String &p_custom_features) {
  566. Error err;
  567. FileAccess *file = FileAccess::open(p_file, FileAccess::WRITE, &err);
  568. if (err) {
  569. ERR_EXPLAIN("Couldn't save project.godot - " + p_file);
  570. ERR_FAIL_COND_V(err, err);
  571. }
  572. file->store_line("; Engine configuration file.");
  573. file->store_line("; It's best edited using the editor UI and not directly,");
  574. file->store_line("; since the parameters that go here are not all obvious.");
  575. file->store_line(";");
  576. file->store_line("; Format:");
  577. file->store_line("; [section] ; section goes between []");
  578. file->store_line("; param=value ; assign values to parameters");
  579. file->store_line("");
  580. file->store_string("config_version=" + itos(CONFIG_VERSION) + "\n");
  581. if (p_custom_features != String())
  582. file->store_string("custom_features=\"" + p_custom_features + "\"\n");
  583. file->store_string("\n");
  584. for (Map<String, List<String> >::Element *E = props.front(); E; E = E->next()) {
  585. if (E != props.front())
  586. file->store_string("\n");
  587. if (E->key() != "")
  588. file->store_string("[" + E->key() + "]\n\n");
  589. for (List<String>::Element *F = E->get().front(); F; F = F->next()) {
  590. String key = F->get();
  591. if (E->key() != "")
  592. key = E->key() + "/" + key;
  593. Variant value;
  594. if (p_custom.has(key))
  595. value = p_custom[key];
  596. else
  597. value = get(key);
  598. String vstr;
  599. VariantWriter::write_to_string(value, vstr);
  600. if (F->get().find(" ") != -1)
  601. file->store_string(F->get().quote() + "=" + vstr + "\n");
  602. else
  603. file->store_string(F->get() + "=" + vstr + "\n");
  604. }
  605. }
  606. file->close();
  607. memdelete(file);
  608. return OK;
  609. }
  610. Error ProjectSettings::_save_custom_bnd(const String &p_file) { // add other params as dictionary and array?
  611. return save_custom(p_file);
  612. };
  613. Error ProjectSettings::save_custom(const String &p_path, const CustomMap &p_custom, const Vector<String> &p_custom_features, bool p_merge_with_current) {
  614. ERR_FAIL_COND_V(p_path == "", ERR_INVALID_PARAMETER);
  615. Set<_VCSort> vclist;
  616. if (p_merge_with_current) {
  617. for (Map<StringName, VariantContainer>::Element *G = props.front(); G; G = G->next()) {
  618. const VariantContainer *v = &G->get();
  619. if (v->hide_from_editor)
  620. continue;
  621. if (p_custom.has(G->key()))
  622. continue;
  623. _VCSort vc;
  624. vc.name = G->key(); //*k;
  625. vc.order = v->order;
  626. vc.type = v->variant.get_type();
  627. vc.flags = PROPERTY_USAGE_EDITOR | PROPERTY_USAGE_STORAGE;
  628. if (v->variant == v->initial)
  629. continue;
  630. vclist.insert(vc);
  631. }
  632. }
  633. for (const Map<String, Variant>::Element *E = p_custom.front(); E; E = E->next()) {
  634. // Lookup global prop to store in the same order
  635. Map<StringName, VariantContainer>::Element *global_prop = props.find(E->key());
  636. _VCSort vc;
  637. vc.name = E->key();
  638. vc.order = global_prop ? global_prop->get().order : 0xFFFFFFF;
  639. vc.type = E->get().get_type();
  640. vc.flags = PROPERTY_USAGE_STORAGE;
  641. vclist.insert(vc);
  642. }
  643. Map<String, List<String> > props;
  644. for (Set<_VCSort>::Element *E = vclist.front(); E; E = E->next()) {
  645. String category = E->get().name;
  646. String name = E->get().name;
  647. int div = category.find("/");
  648. if (div < 0)
  649. category = "";
  650. else {
  651. category = category.substr(0, div);
  652. name = name.substr(div + 1, name.size());
  653. }
  654. props[category].push_back(name);
  655. }
  656. String custom_features;
  657. for (int i = 0; i < p_custom_features.size(); i++) {
  658. if (i > 0)
  659. custom_features += ",";
  660. String f = p_custom_features[i].strip_edges().replace("\"", "");
  661. custom_features += f;
  662. }
  663. if (p_path.ends_with(".godot"))
  664. return _save_settings_text(p_path, props, p_custom, custom_features);
  665. else if (p_path.ends_with(".binary"))
  666. return _save_settings_binary(p_path, props, p_custom, custom_features);
  667. else {
  668. ERR_EXPLAIN("Unknown config file format: " + p_path);
  669. ERR_FAIL_V(ERR_FILE_UNRECOGNIZED);
  670. }
  671. return OK;
  672. }
  673. Variant _GLOBAL_DEF(const String &p_var, const Variant &p_default, bool p_restart_if_changed) {
  674. Variant ret;
  675. if (!ProjectSettings::get_singleton()->has_setting(p_var)) {
  676. ProjectSettings::get_singleton()->set(p_var, p_default);
  677. }
  678. ret = ProjectSettings::get_singleton()->get(p_var);
  679. ProjectSettings::get_singleton()->set_initial_value(p_var, p_default);
  680. ProjectSettings::get_singleton()->set_builtin_order(p_var);
  681. ProjectSettings::get_singleton()->set_restart_if_changed(p_var, p_restart_if_changed);
  682. return ret;
  683. }
  684. Vector<String> ProjectSettings::get_optimizer_presets() const {
  685. List<PropertyInfo> pi;
  686. ProjectSettings::get_singleton()->get_property_list(&pi);
  687. Vector<String> names;
  688. for (List<PropertyInfo>::Element *E = pi.front(); E; E = E->next()) {
  689. if (!E->get().name.begins_with("optimizer_presets/"))
  690. continue;
  691. names.push_back(E->get().name.get_slicec('/', 1));
  692. }
  693. names.sort();
  694. return names;
  695. }
  696. void ProjectSettings::_add_property_info_bind(const Dictionary &p_info) {
  697. ERR_FAIL_COND(!p_info.has("name"));
  698. ERR_FAIL_COND(!p_info.has("type"));
  699. PropertyInfo pinfo;
  700. pinfo.name = p_info["name"];
  701. ERR_FAIL_COND(!props.has(pinfo.name));
  702. pinfo.type = Variant::Type(p_info["type"].operator int());
  703. ERR_FAIL_INDEX(pinfo.type, Variant::VARIANT_MAX);
  704. if (p_info.has("hint"))
  705. pinfo.hint = PropertyHint(p_info["hint"].operator int());
  706. if (p_info.has("hint_string"))
  707. pinfo.hint_string = p_info["hint_string"];
  708. set_custom_property_info(pinfo.name, pinfo);
  709. }
  710. void ProjectSettings::set_custom_property_info(const String &p_prop, const PropertyInfo &p_info) {
  711. ERR_FAIL_COND(!props.has(p_prop));
  712. custom_prop_info[p_prop] = p_info;
  713. custom_prop_info[p_prop].name = p_prop;
  714. }
  715. const Map<StringName, PropertyInfo> &ProjectSettings::get_custom_property_info() const {
  716. return custom_prop_info;
  717. }
  718. void ProjectSettings::set_disable_feature_overrides(bool p_disable) {
  719. disable_feature_overrides = p_disable;
  720. }
  721. bool ProjectSettings::is_using_datapack() const {
  722. return using_datapack;
  723. }
  724. bool ProjectSettings::property_can_revert(const String &p_name) {
  725. if (!props.has(p_name))
  726. return false;
  727. return props[p_name].initial != props[p_name].variant;
  728. }
  729. Variant ProjectSettings::property_get_revert(const String &p_name) {
  730. if (!props.has(p_name))
  731. return Variant();
  732. return props[p_name].initial;
  733. }
  734. void ProjectSettings::set_setting(const String &p_setting, const Variant &p_value) {
  735. set(p_setting, p_value);
  736. }
  737. Variant ProjectSettings::get_setting(const String &p_setting) const {
  738. return get(p_setting);
  739. }
  740. bool ProjectSettings::has_custom_feature(const String &p_feature) const {
  741. return custom_features.has(p_feature);
  742. }
  743. void ProjectSettings::_bind_methods() {
  744. ClassDB::bind_method(D_METHOD("has_setting", "name"), &ProjectSettings::has_setting);
  745. ClassDB::bind_method(D_METHOD("set_setting", "name", "value"), &ProjectSettings::set_setting);
  746. ClassDB::bind_method(D_METHOD("get_setting", "name"), &ProjectSettings::get_setting);
  747. ClassDB::bind_method(D_METHOD("set_order", "name", "position"), &ProjectSettings::set_order);
  748. ClassDB::bind_method(D_METHOD("get_order", "name"), &ProjectSettings::get_order);
  749. ClassDB::bind_method(D_METHOD("set_initial_value", "name", "value"), &ProjectSettings::set_initial_value);
  750. ClassDB::bind_method(D_METHOD("add_property_info", "hint"), &ProjectSettings::_add_property_info_bind);
  751. ClassDB::bind_method(D_METHOD("clear", "name"), &ProjectSettings::clear);
  752. ClassDB::bind_method(D_METHOD("localize_path", "path"), &ProjectSettings::localize_path);
  753. ClassDB::bind_method(D_METHOD("globalize_path", "path"), &ProjectSettings::globalize_path);
  754. ClassDB::bind_method(D_METHOD("save"), &ProjectSettings::save);
  755. ClassDB::bind_method(D_METHOD("load_resource_pack", "pack"), &ProjectSettings::_load_resource_pack);
  756. ClassDB::bind_method(D_METHOD("property_can_revert", "name"), &ProjectSettings::property_can_revert);
  757. ClassDB::bind_method(D_METHOD("property_get_revert", "name"), &ProjectSettings::property_get_revert);
  758. ClassDB::bind_method(D_METHOD("save_custom", "file"), &ProjectSettings::_save_custom_bnd);
  759. }
  760. ProjectSettings::ProjectSettings() {
  761. singleton = this;
  762. last_order = NO_BUILTIN_ORDER_BASE;
  763. last_builtin_order = 0;
  764. disable_feature_overrides = false;
  765. registering_order = true;
  766. Array events;
  767. Dictionary action;
  768. Ref<InputEventKey> key;
  769. Ref<InputEventJoypadButton> joyb;
  770. GLOBAL_DEF("application/config/name", "");
  771. GLOBAL_DEF("application/run/main_scene", "");
  772. custom_prop_info["application/run/main_scene"] = PropertyInfo(Variant::STRING, "application/run/main_scene", PROPERTY_HINT_FILE, "*.tscn,*.scn,*.res");
  773. GLOBAL_DEF("application/run/disable_stdout", false);
  774. GLOBAL_DEF("application/run/disable_stderr", false);
  775. GLOBAL_DEF("application/config/use_custom_user_dir", false);
  776. GLOBAL_DEF("application/config/custom_user_dir_name", "");
  777. GLOBAL_DEF("application/config/project_settings_override", "");
  778. GLOBAL_DEF("audio/default_bus_layout", "res://default_bus_layout.tres");
  779. custom_prop_info["audio/default_bus_layout"] = PropertyInfo(Variant::STRING, "audio/default_bus_layout", PROPERTY_HINT_FILE, "*.tres");
  780. PoolStringArray extensions = PoolStringArray();
  781. extensions.push_back("gd");
  782. if (Engine::get_singleton()->has_singleton("GodotSharp"))
  783. extensions.push_back("cs");
  784. extensions.push_back("shader");
  785. GLOBAL_DEF("editor/search_in_file_extensions", extensions);
  786. custom_prop_info["editor/search_in_file_extensions"] = PropertyInfo(Variant::POOL_STRING_ARRAY, "editor/search_in_file_extensions");
  787. action = Dictionary();
  788. action["deadzone"] = Variant(0.5f);
  789. events = Array();
  790. key.instance();
  791. key->set_scancode(KEY_ENTER);
  792. events.push_back(key);
  793. key.instance();
  794. key->set_scancode(KEY_KP_ENTER);
  795. events.push_back(key);
  796. key.instance();
  797. key->set_scancode(KEY_SPACE);
  798. events.push_back(key);
  799. joyb.instance();
  800. joyb->set_button_index(JOY_BUTTON_0);
  801. events.push_back(joyb);
  802. action["events"] = events;
  803. GLOBAL_DEF("input/ui_accept", action);
  804. input_presets.push_back("input/ui_accept");
  805. action = Dictionary();
  806. action["deadzone"] = Variant(0.5f);
  807. events = Array();
  808. key.instance();
  809. key->set_scancode(KEY_SPACE);
  810. events.push_back(key);
  811. joyb.instance();
  812. joyb->set_button_index(JOY_BUTTON_3);
  813. events.push_back(joyb);
  814. action["events"] = events;
  815. GLOBAL_DEF("input/ui_select", action);
  816. input_presets.push_back("input/ui_select");
  817. action = Dictionary();
  818. action["deadzone"] = Variant(0.5f);
  819. events = Array();
  820. key.instance();
  821. key->set_scancode(KEY_ESCAPE);
  822. events.push_back(key);
  823. joyb.instance();
  824. joyb->set_button_index(JOY_BUTTON_1);
  825. events.push_back(joyb);
  826. action["events"] = events;
  827. GLOBAL_DEF("input/ui_cancel", action);
  828. input_presets.push_back("input/ui_cancel");
  829. action = Dictionary();
  830. action["deadzone"] = Variant(0.5f);
  831. events = Array();
  832. key.instance();
  833. key->set_scancode(KEY_TAB);
  834. events.push_back(key);
  835. action["events"] = events;
  836. GLOBAL_DEF("input/ui_focus_next", action);
  837. input_presets.push_back("input/ui_focus_next");
  838. action = Dictionary();
  839. action["deadzone"] = Variant(0.5f);
  840. events = Array();
  841. key.instance();
  842. key->set_scancode(KEY_TAB);
  843. key->set_shift(true);
  844. events.push_back(key);
  845. action["events"] = events;
  846. GLOBAL_DEF("input/ui_focus_prev", action);
  847. input_presets.push_back("input/ui_focus_prev");
  848. action = Dictionary();
  849. action["deadzone"] = Variant(0.5f);
  850. events = Array();
  851. key.instance();
  852. key->set_scancode(KEY_LEFT);
  853. events.push_back(key);
  854. joyb.instance();
  855. joyb->set_button_index(JOY_DPAD_LEFT);
  856. events.push_back(joyb);
  857. action["events"] = events;
  858. GLOBAL_DEF("input/ui_left", action);
  859. input_presets.push_back("input/ui_left");
  860. action = Dictionary();
  861. action["deadzone"] = Variant(0.5f);
  862. events = Array();
  863. key.instance();
  864. key->set_scancode(KEY_RIGHT);
  865. events.push_back(key);
  866. joyb.instance();
  867. joyb->set_button_index(JOY_DPAD_RIGHT);
  868. events.push_back(joyb);
  869. action["events"] = events;
  870. GLOBAL_DEF("input/ui_right", action);
  871. input_presets.push_back("input/ui_right");
  872. action = Dictionary();
  873. action["deadzone"] = Variant(0.5f);
  874. events = Array();
  875. key.instance();
  876. key->set_scancode(KEY_UP);
  877. events.push_back(key);
  878. joyb.instance();
  879. joyb->set_button_index(JOY_DPAD_UP);
  880. events.push_back(joyb);
  881. action["events"] = events;
  882. GLOBAL_DEF("input/ui_up", action);
  883. input_presets.push_back("input/ui_up");
  884. action = Dictionary();
  885. action["deadzone"] = Variant(0.5f);
  886. events = Array();
  887. key.instance();
  888. key->set_scancode(KEY_DOWN);
  889. events.push_back(key);
  890. joyb.instance();
  891. joyb->set_button_index(JOY_DPAD_DOWN);
  892. events.push_back(joyb);
  893. action["events"] = events;
  894. GLOBAL_DEF("input/ui_down", action);
  895. input_presets.push_back("input/ui_down");
  896. action = Dictionary();
  897. action["deadzone"] = Variant(0.5f);
  898. events = Array();
  899. key.instance();
  900. key->set_scancode(KEY_PAGEUP);
  901. events.push_back(key);
  902. action["events"] = events;
  903. GLOBAL_DEF("input/ui_page_up", action);
  904. input_presets.push_back("input/ui_page_up");
  905. action = Dictionary();
  906. action["deadzone"] = Variant(0.5f);
  907. events = Array();
  908. key.instance();
  909. key->set_scancode(KEY_PAGEDOWN);
  910. events.push_back(key);
  911. action["events"] = events;
  912. GLOBAL_DEF("input/ui_page_down", action);
  913. input_presets.push_back("input/ui_page_down");
  914. action = Dictionary();
  915. action["deadzone"] = Variant(0.5f);
  916. events = Array();
  917. key.instance();
  918. key->set_scancode(KEY_HOME);
  919. events.push_back(key);
  920. action["events"] = events;
  921. GLOBAL_DEF("input/ui_home", action);
  922. input_presets.push_back("input/ui_home");
  923. action = Dictionary();
  924. action["deadzone"] = Variant(0.5f);
  925. events = Array();
  926. key.instance();
  927. key->set_scancode(KEY_END);
  928. events.push_back(key);
  929. action["events"] = events;
  930. GLOBAL_DEF("input/ui_end", action);
  931. input_presets.push_back("input/ui_end");
  932. custom_prop_info["display/window/handheld/orientation"] = PropertyInfo(Variant::STRING, "display/window/handheld/orientation", PROPERTY_HINT_ENUM, "landscape,portrait,reverse_landscape,reverse_portrait,sensor_landscape,sensor_portrait,sensor");
  933. custom_prop_info["rendering/threads/thread_model"] = PropertyInfo(Variant::INT, "rendering/threads/thread_model", PROPERTY_HINT_ENUM, "Single-Unsafe,Single-Safe,Multi-Threaded");
  934. custom_prop_info["physics/2d/thread_model"] = PropertyInfo(Variant::INT, "physics/2d/thread_model", PROPERTY_HINT_ENUM, "Single-Unsafe,Single-Safe,Multi-Threaded");
  935. custom_prop_info["rendering/quality/intended_usage/framebuffer_allocation"] = PropertyInfo(Variant::INT, "rendering/quality/intended_usage/framebuffer_allocation", PROPERTY_HINT_ENUM, "2D,2D Without Sampling,3D,3D Without Effects");
  936. GLOBAL_DEF("debug/settings/profiler/max_functions", 16384);
  937. custom_prop_info["debug/settings/profiler/max_functions"] = PropertyInfo(Variant::INT, "debug/settings/profiler/max_functions", PROPERTY_HINT_RANGE, "128,65535,1");
  938. //assigning here, because using GLOBAL_GET on every block for compressing can be slow
  939. Compression::zstd_long_distance_matching = GLOBAL_DEF("compression/formats/zstd/long_distance_matching", false);
  940. custom_prop_info["compression/formats/zstd/long_distance_matching"] = PropertyInfo(Variant::BOOL, "compression/formats/zstd/long_distance_matching");
  941. Compression::zstd_level = GLOBAL_DEF("compression/formats/zstd/compression_level", 3);
  942. custom_prop_info["compression/formats/zstd/compression_level"] = PropertyInfo(Variant::INT, "compression/formats/zstd/compression_level", PROPERTY_HINT_RANGE, "1,22,1");
  943. Compression::zstd_window_log_size = GLOBAL_DEF("compression/formats/zstd/window_log_size", 27);
  944. custom_prop_info["compression/formats/zstd/window_log_size"] = PropertyInfo(Variant::INT, "compression/formats/zstd/window_log_size", PROPERTY_HINT_RANGE, "10,30,1");
  945. Compression::zlib_level = GLOBAL_DEF("compression/formats/zlib/compression_level", Z_DEFAULT_COMPRESSION);
  946. custom_prop_info["compression/formats/zlib/compression_level"] = PropertyInfo(Variant::INT, "compression/formats/zlib/compression_level", PROPERTY_HINT_RANGE, "-1,9,1");
  947. Compression::gzip_level = GLOBAL_DEF("compression/formats/gzip/compression_level", Z_DEFAULT_COMPRESSION);
  948. custom_prop_info["compression/formats/gzip/compression_level"] = PropertyInfo(Variant::INT, "compression/formats/gzip/compression_level", PROPERTY_HINT_RANGE, "-1,9,1");
  949. // Would ideally be defined in an Android-specific file, but then it doesn't appear in the docs
  950. GLOBAL_DEF("android/modules", "");
  951. using_datapack = false;
  952. }
  953. ProjectSettings::~ProjectSettings() {
  954. singleton = NULL;
  955. }