project_settings.cpp 30 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056
  1. /*************************************************************************/
  2. /* project_settings.cpp */
  3. /*************************************************************************/
  4. /* This file is part of: */
  5. /* GODOT ENGINE */
  6. /* https://godotengine.org */
  7. /*************************************************************************/
  8. /* Copyright (c) 2007-2018 Juan Linietsky, Ariel Manzur. */
  9. /* Copyright (c) 2014-2018 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 "bind/core_bind.h"
  32. #include "core_string_names.h"
  33. #include "io/file_access_network.h"
  34. #include "io/file_access_pack.h"
  35. #include "io/marshalls.h"
  36. #include "os/dir_access.h"
  37. #include "os/file_access.h"
  38. #include "os/keyboard.h"
  39. #include "os/os.h"
  40. #include "variant_parser.h"
  41. #include <zlib.h>
  42. #define FORMAT_VERSION 3
  43. ProjectSettings *ProjectSettings::singleton = NULL;
  44. ProjectSettings *ProjectSettings::get_singleton() {
  45. return singleton;
  46. }
  47. String ProjectSettings::get_resource_path() const {
  48. return resource_path;
  49. };
  50. String ProjectSettings::localize_path(const String &p_path) const {
  51. if (resource_path == "")
  52. return p_path; //not initialied yet
  53. if (p_path.begins_with("res://") || p_path.begins_with("user://") ||
  54. (p_path.is_abs_path() && !p_path.begins_with(resource_path)))
  55. return p_path.simplify_path();
  56. DirAccess *dir = DirAccess::create(DirAccess::ACCESS_FILESYSTEM);
  57. String path = p_path.replace("\\", "/").simplify_path();
  58. if (dir->change_dir(path) == OK) {
  59. String cwd = dir->get_current_dir();
  60. cwd = cwd.replace("\\", "/");
  61. memdelete(dir);
  62. if (!cwd.begins_with(resource_path)) {
  63. return p_path;
  64. };
  65. return cwd.replace_first(resource_path, "res:/");
  66. } else {
  67. memdelete(dir);
  68. int sep = path.find_last("/");
  69. if (sep == -1) {
  70. return "res://" + path;
  71. };
  72. String parent = path.substr(0, sep);
  73. String plocal = localize_path(parent);
  74. if (plocal == "") {
  75. return "";
  76. };
  77. return plocal + path.substr(sep, path.size() - sep);
  78. };
  79. }
  80. void ProjectSettings::set_initial_value(const String &p_name, const Variant &p_value) {
  81. ERR_FAIL_COND(!props.has(p_name));
  82. props[p_name].initial = p_value;
  83. }
  84. String ProjectSettings::globalize_path(const String &p_path) const {
  85. if (p_path.begins_with("res://")) {
  86. if (resource_path != "") {
  87. return p_path.replace("res:/", resource_path);
  88. };
  89. return p_path.replace("res://", "");
  90. } else if (p_path.begins_with("user://")) {
  91. String data_dir = OS::get_singleton()->get_user_data_dir();
  92. if (data_dir != "") {
  93. return p_path.replace("user:/", data_dir);
  94. };
  95. return p_path.replace("user://", "");
  96. }
  97. return p_path;
  98. }
  99. bool ProjectSettings::_set(const StringName &p_name, const Variant &p_value) {
  100. _THREAD_SAFE_METHOD_
  101. if (p_value.get_type() == Variant::NIL)
  102. props.erase(p_name);
  103. else {
  104. if (p_name == CoreStringNames::get_singleton()->_custom_features) {
  105. Vector<String> custom_feature_array = p_value;
  106. for (int i = 0; i < custom_feature_array.size(); i++) {
  107. custom_features.insert(custom_feature_array[i]);
  108. }
  109. return true;
  110. }
  111. if (!disable_feature_overrides) {
  112. int dot = p_name.operator String().find(".");
  113. if (dot != -1) {
  114. Vector<String> s = p_name.operator String().split(".");
  115. bool override_valid = false;
  116. for (int i = 1; i < s.size(); i++) {
  117. String feature = s[i].strip_edges();
  118. if (OS::get_singleton()->has_feature(feature) || custom_features.has(feature)) {
  119. override_valid = true;
  120. break;
  121. }
  122. }
  123. if (override_valid) {
  124. feature_overrides[s[0]] = p_name;
  125. }
  126. }
  127. }
  128. if (props.has(p_name)) {
  129. if (!props[p_name].overridden)
  130. props[p_name].variant = p_value;
  131. } else {
  132. props[p_name] = VariantContainer(p_value, last_order++);
  133. }
  134. }
  135. return true;
  136. }
  137. bool ProjectSettings::_get(const StringName &p_name, Variant &r_ret) const {
  138. _THREAD_SAFE_METHOD_
  139. StringName name = p_name;
  140. if (!disable_feature_overrides && feature_overrides.has(name)) {
  141. name = feature_overrides[name];
  142. }
  143. if (!props.has(name)) {
  144. print_line("WARNING: not found: " + String(name));
  145. return false;
  146. }
  147. r_ret = props[name].variant;
  148. return true;
  149. }
  150. struct _VCSort {
  151. String name;
  152. Variant::Type type;
  153. int order;
  154. int flags;
  155. bool operator<(const _VCSort &p_vcs) const { return order == p_vcs.order ? name < p_vcs.name : order < p_vcs.order; }
  156. };
  157. void ProjectSettings::_get_property_list(List<PropertyInfo> *p_list) const {
  158. _THREAD_SAFE_METHOD_
  159. Set<_VCSort> vclist;
  160. for (Map<StringName, VariantContainer>::Element *E = props.front(); E; E = E->next()) {
  161. const VariantContainer *v = &E->get();
  162. if (v->hide_from_editor)
  163. continue;
  164. _VCSort vc;
  165. vc.name = E->key();
  166. vc.order = v->order;
  167. vc.type = v->variant.get_type();
  168. 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"))
  169. vc.flags = PROPERTY_USAGE_STORAGE;
  170. else
  171. vc.flags = PROPERTY_USAGE_EDITOR | PROPERTY_USAGE_STORAGE;
  172. vclist.insert(vc);
  173. }
  174. for (Set<_VCSort>::Element *E = vclist.front(); E; E = E->next()) {
  175. String prop_info_name = E->get().name;
  176. int dot = prop_info_name.find(".");
  177. if (dot != -1)
  178. prop_info_name = prop_info_name.substr(0, dot);
  179. if (custom_prop_info.has(prop_info_name)) {
  180. PropertyInfo pi = custom_prop_info[prop_info_name];
  181. pi.name = E->get().name;
  182. pi.usage = E->get().flags;
  183. p_list->push_back(pi);
  184. } else
  185. p_list->push_back(PropertyInfo(E->get().type, E->get().name, PROPERTY_HINT_NONE, "", E->get().flags));
  186. }
  187. }
  188. bool ProjectSettings::_load_resource_pack(const String &p_pack) {
  189. if (PackedData::get_singleton()->is_disabled())
  190. return false;
  191. bool ok = PackedData::get_singleton()->add_pack(p_pack) == OK;
  192. if (!ok)
  193. return false;
  194. //if data.pck is found, all directory access will be from here
  195. DirAccess::make_default<DirAccessPack>(DirAccess::ACCESS_RESOURCES);
  196. using_datapack = true;
  197. return true;
  198. }
  199. Error ProjectSettings::setup(const String &p_path, const String &p_main_pack, bool p_upwards) {
  200. //If looking for files in network, just use network!
  201. if (FileAccessNetworkClient::get_singleton()) {
  202. Error err = _load_settings_text_or_binary("res://project.godot", "res://project.binary");
  203. if (err == OK) {
  204. // Optional, we don't mind if it fails
  205. _load_settings_text("res://override.cfg");
  206. }
  207. return err;
  208. }
  209. String exec_path = OS::get_singleton()->get_executable_path();
  210. //Attempt with a passed main pack first
  211. if (p_main_pack != "") {
  212. bool ok = _load_resource_pack(p_main_pack);
  213. ERR_FAIL_COND_V(!ok, ERR_CANT_OPEN);
  214. Error err = _load_settings_text_or_binary("res://project.godot", "res://project.binary");
  215. if (err == OK) {
  216. // Load override from location of the main pack
  217. // Optional, we don't mind if it fails
  218. _load_settings_text(p_main_pack.get_base_dir().plus_file("override.cfg"));
  219. }
  220. return err;
  221. }
  222. //Attempt with execname.pck
  223. if (exec_path != "") {
  224. bool found = false;
  225. // get our filename without our path (note, using exec_path.get_file before get_basename anymore because not all file systems have dots in their file names!)
  226. String filebase_name = exec_path.get_file().get_basename();
  227. // try to open at the location of executable
  228. String datapack_name = exec_path.get_base_dir().plus_file(filebase_name) + ".pck";
  229. if (_load_resource_pack(datapack_name)) {
  230. found = true;
  231. } else {
  232. datapack_name = filebase_name + ".pck";
  233. if (_load_resource_pack(datapack_name)) {
  234. found = true;
  235. }
  236. }
  237. // if we opened our package, try and load our project...
  238. if (found) {
  239. Error err = _load_settings_text_or_binary("res://project.godot", "res://project.binary");
  240. if (err == OK) {
  241. // Load override from location of executable
  242. // Optional, we don't mind if it fails
  243. _load_settings_text(exec_path.get_base_dir().plus_file("override.cfg"));
  244. }
  245. return err;
  246. }
  247. }
  248. //Try to use the filesystem for files, according to OS. (only Android -when reading from pck- and iOS use this)
  249. if (OS::get_singleton()->get_resource_dir() != "") {
  250. //OS will call Globals->get_resource_path which will be empty if not overridden!
  251. //if the OS would rather use somewhere else, then it will not be empty.
  252. resource_path = OS::get_singleton()->get_resource_dir().replace("\\", "/");
  253. if (resource_path.length() && resource_path[resource_path.length() - 1] == '/')
  254. resource_path = resource_path.substr(0, resource_path.length() - 1); // chop end
  255. // data.pck and data.zip are deprecated and no longer supported, apologies.
  256. // make sure this is loaded from the resource path
  257. Error err = _load_settings_text_or_binary("res://project.godot", "res://project.binary");
  258. if (err == OK) {
  259. // Optional, we don't mind if it fails
  260. _load_settings_text("res://override.cfg");
  261. }
  262. return err;
  263. }
  264. //Nothing was found, try to find a project.godot somewhere!
  265. DirAccess *d = DirAccess::create(DirAccess::ACCESS_FILESYSTEM);
  266. ERR_FAIL_COND_V(!d, ERR_CANT_CREATE);
  267. d->change_dir(p_path);
  268. String candidate = d->get_current_dir();
  269. String current_dir = d->get_current_dir();
  270. bool found = false;
  271. Error err;
  272. while (true) {
  273. err = _load_settings_text_or_binary(current_dir.plus_file("project.godot"), current_dir.plus_file("project.binary"));
  274. if (err == OK) {
  275. // Optional, we don't mind if it fails
  276. _load_settings_text(current_dir.plus_file("override.cfg"));
  277. candidate = current_dir;
  278. found = true;
  279. break;
  280. }
  281. if (p_upwards) {
  282. // Try to load settings ascending through dirs shape!
  283. d->change_dir("..");
  284. if (d->get_current_dir() == current_dir)
  285. break; //not doing anything useful
  286. current_dir = d->get_current_dir();
  287. } else {
  288. break;
  289. }
  290. }
  291. resource_path = candidate;
  292. resource_path = resource_path.replace("\\", "/"); // windows path to unix path just in case
  293. memdelete(d);
  294. if (!found)
  295. return err;
  296. if (resource_path.length() && resource_path[resource_path.length() - 1] == '/')
  297. resource_path = resource_path.substr(0, resource_path.length() - 1); // chop end
  298. return OK;
  299. }
  300. bool ProjectSettings::has_setting(String p_var) const {
  301. _THREAD_SAFE_METHOD_
  302. return props.has(p_var);
  303. }
  304. void ProjectSettings::set_registering_order(bool p_enable) {
  305. registering_order = p_enable;
  306. }
  307. Error ProjectSettings::_load_settings_binary(const String p_path) {
  308. Error err;
  309. FileAccess *f = FileAccess::open(p_path, FileAccess::READ, &err);
  310. if (err != OK) {
  311. return err;
  312. }
  313. uint8_t hdr[4];
  314. f->get_buffer(hdr, 4);
  315. if (hdr[0] != 'E' || hdr[1] != 'C' || hdr[2] != 'F' || hdr[3] != 'G') {
  316. memdelete(f);
  317. ERR_EXPLAIN("Corrupted header in binary project.binary (not ECFG)");
  318. ERR_FAIL_V(ERR_FILE_CORRUPT;)
  319. }
  320. uint32_t count = f->get_32();
  321. for (uint32_t i = 0; i < count; i++) {
  322. uint32_t slen = f->get_32();
  323. CharString cs;
  324. cs.resize(slen + 1);
  325. cs[slen] = 0;
  326. f->get_buffer((uint8_t *)cs.ptr(), slen);
  327. String key;
  328. key.parse_utf8(cs.ptr());
  329. uint32_t vlen = f->get_32();
  330. Vector<uint8_t> d;
  331. d.resize(vlen);
  332. f->get_buffer(d.ptrw(), vlen);
  333. Variant value;
  334. Error err = decode_variant(value, d.ptr(), d.size());
  335. ERR_EXPLAIN("Error decoding property: " + key);
  336. ERR_CONTINUE(err != OK);
  337. set(key, value);
  338. }
  339. return OK;
  340. }
  341. Error ProjectSettings::_load_settings_text(const String p_path) {
  342. Error err;
  343. FileAccess *f = FileAccess::open(p_path, FileAccess::READ, &err);
  344. if (!f)
  345. return ERR_CANT_OPEN;
  346. VariantParser::StreamFile stream;
  347. stream.f = f;
  348. String assign;
  349. Variant value;
  350. VariantParser::Tag next_tag;
  351. int lines = 0;
  352. String error_text;
  353. String section;
  354. while (true) {
  355. assign = Variant();
  356. next_tag.fields.clear();
  357. next_tag.name = String();
  358. err = VariantParser::parse_tag_assign_eof(&stream, lines, error_text, next_tag, assign, value, NULL, true);
  359. if (err == ERR_FILE_EOF) {
  360. memdelete(f);
  361. return OK;
  362. } else if (err != OK) {
  363. ERR_PRINTS("Error parsing " + p_path + " at line " + itos(lines) + ": " + error_text + " File might be corrupted.");
  364. memdelete(f);
  365. return err;
  366. }
  367. if (assign != String()) {
  368. if (section == String() && assign == "config_version") {
  369. int config_version = value;
  370. if (config_version > FORMAT_VERSION) {
  371. memdelete(f);
  372. ERR_FAIL_COND_V(config_version > FORMAT_VERSION, ERR_FILE_CANT_OPEN);
  373. }
  374. } else {
  375. // config_version is checked and dropped
  376. set(section + "/" + assign, value);
  377. }
  378. } else if (next_tag.name != String()) {
  379. section = next_tag.name;
  380. }
  381. }
  382. memdelete(f);
  383. return OK;
  384. }
  385. Error ProjectSettings::_load_settings_text_or_binary(const String p_text_path, const String p_bin_path) {
  386. // Attempt first to load the text-based project.godot file
  387. Error err_text = _load_settings_text(p_text_path);
  388. if (err_text == OK) {
  389. return OK;
  390. } else if (err_text != ERR_FILE_NOT_FOUND) {
  391. // If the text-based file exists but can't be loaded, we want to know it
  392. return err_text;
  393. }
  394. // Fallback to binary project.binary file if text-based was not found
  395. Error err_bin = _load_settings_binary(p_bin_path);
  396. return err_bin;
  397. }
  398. int ProjectSettings::get_order(const String &p_name) const {
  399. ERR_FAIL_COND_V(!props.has(p_name), -1);
  400. return props[p_name].order;
  401. }
  402. void ProjectSettings::set_order(const String &p_name, int p_order) {
  403. ERR_FAIL_COND(!props.has(p_name));
  404. props[p_name].order = p_order;
  405. }
  406. void ProjectSettings::set_builtin_order(const String &p_name) {
  407. ERR_FAIL_COND(!props.has(p_name));
  408. if (props[p_name].order >= NO_BUILTIN_ORDER_BASE) {
  409. props[p_name].order = last_builtin_order++;
  410. }
  411. }
  412. void ProjectSettings::clear(const String &p_name) {
  413. ERR_FAIL_COND(!props.has(p_name));
  414. props.erase(p_name);
  415. }
  416. Error ProjectSettings::save() {
  417. return save_custom(get_resource_path().plus_file("project.godot"));
  418. }
  419. Error ProjectSettings::_save_settings_binary(const String &p_file, const Map<String, List<String> > &props, const CustomMap &p_custom, const String &p_custom_features) {
  420. Error err;
  421. FileAccess *file = FileAccess::open(p_file, FileAccess::WRITE, &err);
  422. if (err != OK) {
  423. ERR_EXPLAIN("Couldn't save project.binary at " + p_file);
  424. ERR_FAIL_COND_V(err, err)
  425. }
  426. uint8_t hdr[4] = { 'E', 'C', 'F', 'G' };
  427. file->store_buffer(hdr, 4);
  428. int count = 0;
  429. for (Map<String, List<String> >::Element *E = props.front(); E; E = E->next()) {
  430. for (List<String>::Element *F = E->get().front(); F; F = F->next()) {
  431. count++;
  432. }
  433. }
  434. if (p_custom_features != String()) {
  435. file->store_32(count + 1);
  436. //store how many properties are saved, add one for custom featuers, which must always go first
  437. String key = CoreStringNames::get_singleton()->_custom_features;
  438. file->store_32(key.length());
  439. file->store_string(key);
  440. int len;
  441. Error err = encode_variant(p_custom_features, NULL, len);
  442. if (err != OK) {
  443. memdelete(file);
  444. ERR_FAIL_V(err);
  445. }
  446. Vector<uint8_t> buff;
  447. buff.resize(len);
  448. err = encode_variant(p_custom_features, &buff[0], len);
  449. if (err != OK) {
  450. memdelete(file);
  451. ERR_FAIL_V(err);
  452. }
  453. file->store_32(len);
  454. file->store_buffer(buff.ptr(), buff.size());
  455. } else {
  456. file->store_32(count); //store how many properties are saved
  457. }
  458. for (Map<String, List<String> >::Element *E = props.front(); E; E = E->next()) {
  459. for (List<String>::Element *F = E->get().front(); F; F = F->next()) {
  460. String key = F->get();
  461. if (E->key() != "")
  462. key = E->key() + "/" + key;
  463. Variant value;
  464. if (p_custom.has(key))
  465. value = p_custom[key];
  466. else
  467. value = get(key);
  468. file->store_32(key.length());
  469. file->store_string(key);
  470. int len;
  471. Error err = encode_variant(value, NULL, len);
  472. if (err != OK)
  473. memdelete(file);
  474. ERR_FAIL_COND_V(err != OK, ERR_INVALID_DATA);
  475. Vector<uint8_t> buff;
  476. buff.resize(len);
  477. err = encode_variant(value, &buff[0], len);
  478. if (err != OK)
  479. memdelete(file);
  480. ERR_FAIL_COND_V(err != OK, ERR_INVALID_DATA);
  481. file->store_32(len);
  482. file->store_buffer(buff.ptr(), buff.size());
  483. }
  484. }
  485. file->close();
  486. memdelete(file);
  487. return OK;
  488. }
  489. Error ProjectSettings::_save_settings_text(const String &p_file, const Map<String, List<String> > &props, const CustomMap &p_custom, const String &p_custom_features) {
  490. Error err;
  491. FileAccess *file = FileAccess::open(p_file, FileAccess::WRITE, &err);
  492. if (err) {
  493. ERR_EXPLAIN("Couldn't save project.godot - " + p_file);
  494. ERR_FAIL_COND_V(err, err)
  495. }
  496. file->store_line("; Engine configuration file.");
  497. file->store_line("; It's best edited using the editor UI and not directly,");
  498. file->store_line("; since the parameters that go here are not all obvious.");
  499. file->store_line(";");
  500. file->store_line("; Format:");
  501. file->store_line("; [section] ; section goes between []");
  502. file->store_line("; param=value ; assign values to parameters");
  503. file->store_line("");
  504. file->store_string("config_version=" + itos(FORMAT_VERSION) + "\n");
  505. if (p_custom_features != String())
  506. file->store_string("custom_features=\"" + p_custom_features + "\"\n");
  507. file->store_string("\n");
  508. for (Map<String, List<String> >::Element *E = props.front(); E; E = E->next()) {
  509. if (E != props.front())
  510. file->store_string("\n");
  511. if (E->key() != "")
  512. file->store_string("[" + E->key() + "]\n\n");
  513. for (List<String>::Element *F = E->get().front(); F; F = F->next()) {
  514. String key = F->get();
  515. if (E->key() != "")
  516. key = E->key() + "/" + key;
  517. Variant value;
  518. if (p_custom.has(key))
  519. value = p_custom[key];
  520. else
  521. value = get(key);
  522. String vstr;
  523. VariantWriter::write_to_string(value, vstr);
  524. file->store_string(F->get() + "=" + vstr + "\n");
  525. }
  526. }
  527. file->close();
  528. memdelete(file);
  529. return OK;
  530. }
  531. Error ProjectSettings::_save_custom_bnd(const String &p_file) { // add other params as dictionary and array?
  532. return save_custom(p_file);
  533. };
  534. Error ProjectSettings::save_custom(const String &p_path, const CustomMap &p_custom, const Vector<String> &p_custom_features, bool p_merge_with_current) {
  535. ERR_FAIL_COND_V(p_path == "", ERR_INVALID_PARAMETER);
  536. Set<_VCSort> vclist;
  537. if (p_merge_with_current) {
  538. for (Map<StringName, VariantContainer>::Element *G = props.front(); G; G = G->next()) {
  539. const VariantContainer *v = &G->get();
  540. if (v->hide_from_editor)
  541. continue;
  542. if (p_custom.has(G->key()))
  543. continue;
  544. _VCSort vc;
  545. vc.name = G->key(); //*k;
  546. vc.order = v->order;
  547. vc.type = v->variant.get_type();
  548. vc.flags = PROPERTY_USAGE_EDITOR | PROPERTY_USAGE_STORAGE;
  549. if (v->variant == v->initial)
  550. continue;
  551. vclist.insert(vc);
  552. }
  553. }
  554. for (const Map<String, Variant>::Element *E = p_custom.front(); E; E = E->next()) {
  555. // Lookup global prop to store in the same order
  556. Map<StringName, VariantContainer>::Element *global_prop = props.find(E->key());
  557. _VCSort vc;
  558. vc.name = E->key();
  559. vc.order = global_prop ? global_prop->get().order : 0xFFFFFFF;
  560. vc.type = E->get().get_type();
  561. vc.flags = PROPERTY_USAGE_STORAGE;
  562. vclist.insert(vc);
  563. }
  564. Map<String, List<String> > props;
  565. for (Set<_VCSort>::Element *E = vclist.front(); E; E = E->next()) {
  566. String category = E->get().name;
  567. String name = E->get().name;
  568. int div = category.find("/");
  569. if (div < 0)
  570. category = "";
  571. else {
  572. category = category.substr(0, div);
  573. name = name.substr(div + 1, name.size());
  574. }
  575. props[category].push_back(name);
  576. }
  577. String custom_features;
  578. for (int i = 0; i < p_custom_features.size(); i++) {
  579. if (i > 0)
  580. custom_features += ",";
  581. String f = p_custom_features[i].strip_edges().replace("\"", "");
  582. custom_features += f;
  583. }
  584. if (p_path.ends_with(".godot"))
  585. return _save_settings_text(p_path, props, p_custom, custom_features);
  586. else if (p_path.ends_with(".binary"))
  587. return _save_settings_binary(p_path, props, p_custom, custom_features);
  588. else {
  589. ERR_EXPLAIN("Unknown config file format: " + p_path);
  590. ERR_FAIL_V(ERR_FILE_UNRECOGNIZED);
  591. }
  592. return OK;
  593. }
  594. Variant _GLOBAL_DEF(const String &p_var, const Variant &p_default) {
  595. Variant ret;
  596. if (ProjectSettings::get_singleton()->has_setting(p_var)) {
  597. ret = ProjectSettings::get_singleton()->get(p_var);
  598. } else {
  599. ProjectSettings::get_singleton()->set(p_var, p_default);
  600. ret = p_default;
  601. }
  602. ProjectSettings::get_singleton()->set_initial_value(p_var, p_default);
  603. ProjectSettings::get_singleton()->set_builtin_order(p_var);
  604. return ret;
  605. }
  606. Vector<String> ProjectSettings::get_optimizer_presets() const {
  607. List<PropertyInfo> pi;
  608. ProjectSettings::get_singleton()->get_property_list(&pi);
  609. Vector<String> names;
  610. for (List<PropertyInfo>::Element *E = pi.front(); E; E = E->next()) {
  611. if (!E->get().name.begins_with("optimizer_presets/"))
  612. continue;
  613. names.push_back(E->get().name.get_slicec('/', 1));
  614. }
  615. names.sort();
  616. return names;
  617. }
  618. void ProjectSettings::_add_property_info_bind(const Dictionary &p_info) {
  619. ERR_FAIL_COND(!p_info.has("name"));
  620. ERR_FAIL_COND(!p_info.has("type"));
  621. PropertyInfo pinfo;
  622. pinfo.name = p_info["name"];
  623. ERR_FAIL_COND(!props.has(pinfo.name));
  624. pinfo.type = Variant::Type(p_info["type"].operator int());
  625. ERR_FAIL_INDEX(pinfo.type, Variant::VARIANT_MAX);
  626. if (p_info.has("hint"))
  627. pinfo.hint = PropertyHint(p_info["hint"].operator int());
  628. if (p_info.has("hint_string"))
  629. pinfo.hint_string = p_info["hint_string"];
  630. set_custom_property_info(pinfo.name, pinfo);
  631. }
  632. void ProjectSettings::set_custom_property_info(const String &p_prop, const PropertyInfo &p_info) {
  633. ERR_FAIL_COND(!props.has(p_prop));
  634. custom_prop_info[p_prop] = p_info;
  635. custom_prop_info[p_prop].name = p_prop;
  636. }
  637. void ProjectSettings::set_disable_feature_overrides(bool p_disable) {
  638. disable_feature_overrides = p_disable;
  639. }
  640. bool ProjectSettings::is_using_datapack() const {
  641. return using_datapack;
  642. }
  643. bool ProjectSettings::property_can_revert(const String &p_name) {
  644. if (!props.has(p_name))
  645. return false;
  646. return props[p_name].initial != props[p_name].variant;
  647. }
  648. Variant ProjectSettings::property_get_revert(const String &p_name) {
  649. if (!props.has(p_name))
  650. return Variant();
  651. return props[p_name].initial;
  652. }
  653. void ProjectSettings::set_setting(const String &p_setting, const Variant &p_value) {
  654. set(p_setting, p_value);
  655. }
  656. Variant ProjectSettings::get_setting(const String &p_setting) const {
  657. return get(p_setting);
  658. }
  659. void ProjectSettings::_bind_methods() {
  660. ClassDB::bind_method(D_METHOD("has_setting", "name"), &ProjectSettings::has_setting);
  661. ClassDB::bind_method(D_METHOD("set_setting", "name", "value"), &ProjectSettings::set_setting);
  662. ClassDB::bind_method(D_METHOD("get_setting", "name"), &ProjectSettings::get_setting);
  663. ClassDB::bind_method(D_METHOD("set_order", "name", "position"), &ProjectSettings::set_order);
  664. ClassDB::bind_method(D_METHOD("get_order", "name"), &ProjectSettings::get_order);
  665. ClassDB::bind_method(D_METHOD("set_initial_value", "name", "value"), &ProjectSettings::set_initial_value);
  666. ClassDB::bind_method(D_METHOD("add_property_info", "hint"), &ProjectSettings::_add_property_info_bind);
  667. ClassDB::bind_method(D_METHOD("clear", "name"), &ProjectSettings::clear);
  668. ClassDB::bind_method(D_METHOD("localize_path", "path"), &ProjectSettings::localize_path);
  669. ClassDB::bind_method(D_METHOD("globalize_path", "path"), &ProjectSettings::globalize_path);
  670. ClassDB::bind_method(D_METHOD("save"), &ProjectSettings::save);
  671. ClassDB::bind_method(D_METHOD("load_resource_pack", "pack"), &ProjectSettings::_load_resource_pack);
  672. ClassDB::bind_method(D_METHOD("property_can_revert", "name"), &ProjectSettings::property_can_revert);
  673. ClassDB::bind_method(D_METHOD("property_get_revert", "name"), &ProjectSettings::property_get_revert);
  674. ClassDB::bind_method(D_METHOD("save_custom", "file"), &ProjectSettings::_save_custom_bnd);
  675. }
  676. ProjectSettings::ProjectSettings() {
  677. singleton = this;
  678. last_order = NO_BUILTIN_ORDER_BASE;
  679. last_builtin_order = 0;
  680. disable_feature_overrides = false;
  681. registering_order = true;
  682. Array va;
  683. Ref<InputEventKey> key;
  684. Ref<InputEventJoypadButton> joyb;
  685. GLOBAL_DEF("application/config/name", "");
  686. GLOBAL_DEF("application/run/main_scene", "");
  687. custom_prop_info["application/run/main_scene"] = PropertyInfo(Variant::STRING, "application/run/main_scene", PROPERTY_HINT_FILE, "tscn,scn,res");
  688. GLOBAL_DEF("application/run/disable_stdout", false);
  689. GLOBAL_DEF("application/run/disable_stderr", false);
  690. GLOBAL_DEF("application/config/use_custom_user_dir", false);
  691. GLOBAL_DEF("application/config/custom_user_dir_name", "");
  692. key.instance();
  693. key->set_scancode(KEY_ENTER);
  694. va.push_back(key);
  695. key.instance();
  696. key->set_scancode(KEY_KP_ENTER);
  697. va.push_back(key);
  698. key.instance();
  699. key->set_scancode(KEY_SPACE);
  700. va.push_back(key);
  701. joyb.instance();
  702. joyb->set_button_index(JOY_BUTTON_0);
  703. va.push_back(joyb);
  704. GLOBAL_DEF("input/ui_accept", va);
  705. input_presets.push_back("input/ui_accept");
  706. va = Array();
  707. key.instance();
  708. key->set_scancode(KEY_SPACE);
  709. va.push_back(key);
  710. joyb.instance();
  711. joyb->set_button_index(JOY_BUTTON_3);
  712. va.push_back(joyb);
  713. GLOBAL_DEF("input/ui_select", va);
  714. input_presets.push_back("input/ui_select");
  715. va = Array();
  716. key.instance();
  717. key->set_scancode(KEY_ESCAPE);
  718. va.push_back(key);
  719. joyb.instance();
  720. joyb->set_button_index(JOY_BUTTON_1);
  721. va.push_back(joyb);
  722. GLOBAL_DEF("input/ui_cancel", va);
  723. input_presets.push_back("input/ui_cancel");
  724. va = Array();
  725. key.instance();
  726. key->set_scancode(KEY_TAB);
  727. va.push_back(key);
  728. GLOBAL_DEF("input/ui_focus_next", va);
  729. input_presets.push_back("input/ui_focus_next");
  730. va = Array();
  731. key.instance();
  732. key->set_scancode(KEY_TAB);
  733. key->set_shift(true);
  734. va.push_back(key);
  735. GLOBAL_DEF("input/ui_focus_prev", va);
  736. input_presets.push_back("input/ui_focus_prev");
  737. va = Array();
  738. key.instance();
  739. key->set_scancode(KEY_LEFT);
  740. va.push_back(key);
  741. joyb.instance();
  742. joyb->set_button_index(JOY_DPAD_LEFT);
  743. va.push_back(joyb);
  744. GLOBAL_DEF("input/ui_left", va);
  745. input_presets.push_back("input/ui_left");
  746. va = Array();
  747. key.instance();
  748. key->set_scancode(KEY_RIGHT);
  749. va.push_back(key);
  750. joyb.instance();
  751. joyb->set_button_index(JOY_DPAD_RIGHT);
  752. va.push_back(joyb);
  753. GLOBAL_DEF("input/ui_right", va);
  754. input_presets.push_back("input/ui_right");
  755. va = Array();
  756. key.instance();
  757. key->set_scancode(KEY_UP);
  758. va.push_back(key);
  759. joyb.instance();
  760. joyb->set_button_index(JOY_DPAD_UP);
  761. va.push_back(joyb);
  762. GLOBAL_DEF("input/ui_up", va);
  763. input_presets.push_back("input/ui_up");
  764. va = Array();
  765. key.instance();
  766. key->set_scancode(KEY_DOWN);
  767. va.push_back(key);
  768. joyb.instance();
  769. joyb->set_button_index(JOY_DPAD_DOWN);
  770. va.push_back(joyb);
  771. GLOBAL_DEF("input/ui_down", va);
  772. input_presets.push_back("input/ui_down");
  773. va = Array();
  774. key.instance();
  775. key->set_scancode(KEY_PAGEUP);
  776. va.push_back(key);
  777. GLOBAL_DEF("input/ui_page_up", va);
  778. input_presets.push_back("input/ui_page_up");
  779. va = Array();
  780. key.instance();
  781. key->set_scancode(KEY_PAGEDOWN);
  782. va.push_back(key);
  783. GLOBAL_DEF("input/ui_page_down", va);
  784. input_presets.push_back("input/ui_page_down");
  785. //GLOBAL_DEF("display/window/handheld/orientation", "landscape");
  786. 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");
  787. custom_prop_info["rendering/threads/thread_model"] = PropertyInfo(Variant::INT, "rendering/threads/thread_model", PROPERTY_HINT_ENUM, "Single-Unsafe,Single-Safe,Multi-Threaded");
  788. custom_prop_info["physics/2d/thread_model"] = PropertyInfo(Variant::INT, "physics/2d/thread_model", PROPERTY_HINT_ENUM, "Single-Unsafe,Single-Safe,Multi-Threaded");
  789. 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");
  790. GLOBAL_DEF("rendering/quality/intended_usage/framebuffer_mode", 2);
  791. GLOBAL_DEF("debug/settings/profiler/max_functions", 16384);
  792. //assigning here, because using GLOBAL_GET on every block for compressing can be slow
  793. Compression::zstd_long_distance_matching = GLOBAL_DEF("compression/formats/zstd/long_distance_matching", false);
  794. custom_prop_info["compression/formats/zstd/long_distance_matching"] = PropertyInfo(Variant::BOOL, "compression/formats/zstd/long_distance_matching");
  795. Compression::zstd_level = GLOBAL_DEF("compression/formats/zstd/compression_level", 3);
  796. custom_prop_info["compression/formats/zstd/compression_level"] = PropertyInfo(Variant::INT, "compression/formats/zstd/compression_level", PROPERTY_HINT_RANGE, "1,22,1");
  797. Compression::zstd_window_log_size = GLOBAL_DEF("compression/formats/zstd/window_log_size", 27);
  798. custom_prop_info["compression/formats/zstd/window_log_size"] = PropertyInfo(Variant::INT, "compression/formats/zstd/window_log_size", PROPERTY_HINT_RANGE, "10,30,1");
  799. Compression::zlib_level = GLOBAL_DEF("compression/formats/zlib/compression_level", Z_DEFAULT_COMPRESSION);
  800. custom_prop_info["compression/formats/zlib/compression_level"] = PropertyInfo(Variant::INT, "compression/formats/zlib/compression_level", PROPERTY_HINT_RANGE, "-1,9,1");
  801. Compression::gzip_level = GLOBAL_DEF("compression/formats/gzip/compression_level", Z_DEFAULT_COMPRESSION);
  802. custom_prop_info["compression/formats/gzip/compression_level"] = PropertyInfo(Variant::INT, "compression/formats/gzip/compression_level", PROPERTY_HINT_RANGE, "-1,9,1");
  803. using_datapack = false;
  804. }
  805. ProjectSettings::~ProjectSettings() {
  806. singleton = NULL;
  807. }