project_settings.cpp 36 KB

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