project_settings.cpp 32 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095
  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 4
  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 = String(p_value).split(",");
  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. void ProjectSettings::_convert_to_last_version() {
  200. if (!has_setting("config_version") || (int)get_setting("config_version") <= 3) {
  201. // Converts the actions from array to dictionary (array of events to dictionary with deadzone + events)
  202. for (Map<StringName, ProjectSettings::VariantContainer>::Element *E = props.front(); E; E = E->next()) {
  203. Variant value = E->get().variant;
  204. if (String(E->key()).begins_with("input/") && value.get_type() == Variant::ARRAY) {
  205. Array array = value;
  206. Dictionary action;
  207. action["deadzone"] = Variant(0.5f);
  208. action["events"] = array;
  209. E->get().variant = action;
  210. }
  211. }
  212. }
  213. }
  214. Error ProjectSettings::setup(const String &p_path, const String &p_main_pack, bool p_upwards) {
  215. //If looking for files in network, just use network!
  216. if (FileAccessNetworkClient::get_singleton()) {
  217. Error err = _load_settings_text_or_binary("res://project.godot", "res://project.binary");
  218. if (err == OK) {
  219. // Optional, we don't mind if it fails
  220. _load_settings_text("res://override.cfg");
  221. }
  222. return err;
  223. }
  224. String exec_path = OS::get_singleton()->get_executable_path();
  225. //Attempt with a passed main pack first
  226. if (p_main_pack != "") {
  227. bool ok = _load_resource_pack(p_main_pack);
  228. ERR_FAIL_COND_V(!ok, ERR_CANT_OPEN);
  229. Error err = _load_settings_text_or_binary("res://project.godot", "res://project.binary");
  230. if (err == OK) {
  231. // Load override from location of the main pack
  232. // Optional, we don't mind if it fails
  233. _load_settings_text(p_main_pack.get_base_dir().plus_file("override.cfg"));
  234. }
  235. return err;
  236. }
  237. //Attempt with execname.pck
  238. if (exec_path != "") {
  239. bool found = false;
  240. // 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!)
  241. String filebase_name = exec_path.get_file().get_basename();
  242. // try to open at the location of executable
  243. String datapack_name = exec_path.get_base_dir().plus_file(filebase_name) + ".pck";
  244. if (_load_resource_pack(datapack_name)) {
  245. found = true;
  246. } else {
  247. datapack_name = filebase_name + ".pck";
  248. if (_load_resource_pack(datapack_name)) {
  249. found = true;
  250. }
  251. }
  252. // if we opened our package, try and load our project...
  253. if (found) {
  254. Error err = _load_settings_text_or_binary("res://project.godot", "res://project.binary");
  255. if (err == OK) {
  256. // Load override from location of executable
  257. // Optional, we don't mind if it fails
  258. _load_settings_text(exec_path.get_base_dir().plus_file("override.cfg"));
  259. }
  260. return err;
  261. }
  262. }
  263. //Try to use the filesystem for files, according to OS. (only Android -when reading from pck- and iOS use this)
  264. if (OS::get_singleton()->get_resource_dir() != "") {
  265. //OS will call Globals->get_resource_path which will be empty if not overridden!
  266. //if the OS would rather use somewhere else, then it will not be empty.
  267. resource_path = OS::get_singleton()->get_resource_dir().replace("\\", "/");
  268. if (resource_path.length() && resource_path[resource_path.length() - 1] == '/')
  269. resource_path = resource_path.substr(0, resource_path.length() - 1); // chop end
  270. // data.pck and data.zip are deprecated and no longer supported, apologies.
  271. // make sure this is loaded from the resource path
  272. Error err = _load_settings_text_or_binary("res://project.godot", "res://project.binary");
  273. if (err == OK) {
  274. // Optional, we don't mind if it fails
  275. _load_settings_text("res://override.cfg");
  276. }
  277. return err;
  278. }
  279. //Nothing was found, try to find a project.godot somewhere!
  280. DirAccess *d = DirAccess::create(DirAccess::ACCESS_FILESYSTEM);
  281. ERR_FAIL_COND_V(!d, ERR_CANT_CREATE);
  282. d->change_dir(p_path);
  283. String candidate = d->get_current_dir();
  284. String current_dir = d->get_current_dir();
  285. bool found = false;
  286. Error err;
  287. while (true) {
  288. err = _load_settings_text_or_binary(current_dir.plus_file("project.godot"), current_dir.plus_file("project.binary"));
  289. if (err == OK) {
  290. // Optional, we don't mind if it fails
  291. _load_settings_text(current_dir.plus_file("override.cfg"));
  292. candidate = current_dir;
  293. found = true;
  294. break;
  295. }
  296. if (p_upwards) {
  297. // Try to load settings ascending through dirs shape!
  298. d->change_dir("..");
  299. if (d->get_current_dir() == current_dir)
  300. break; //not doing anything useful
  301. current_dir = d->get_current_dir();
  302. } else {
  303. break;
  304. }
  305. }
  306. resource_path = candidate;
  307. resource_path = resource_path.replace("\\", "/"); // windows path to unix path just in case
  308. memdelete(d);
  309. if (!found)
  310. return err;
  311. if (resource_path.length() && resource_path[resource_path.length() - 1] == '/')
  312. resource_path = resource_path.substr(0, resource_path.length() - 1); // chop end
  313. _convert_to_last_version();
  314. return OK;
  315. }
  316. bool ProjectSettings::has_setting(String p_var) const {
  317. _THREAD_SAFE_METHOD_
  318. return props.has(p_var);
  319. }
  320. void ProjectSettings::set_registering_order(bool p_enable) {
  321. registering_order = p_enable;
  322. }
  323. Error ProjectSettings::_load_settings_binary(const String p_path) {
  324. Error err;
  325. FileAccess *f = FileAccess::open(p_path, FileAccess::READ, &err);
  326. if (err != OK) {
  327. return err;
  328. }
  329. uint8_t hdr[4];
  330. f->get_buffer(hdr, 4);
  331. if (hdr[0] != 'E' || hdr[1] != 'C' || hdr[2] != 'F' || hdr[3] != 'G') {
  332. memdelete(f);
  333. ERR_EXPLAIN("Corrupted header in binary project.binary (not ECFG)");
  334. ERR_FAIL_V(ERR_FILE_CORRUPT;)
  335. }
  336. uint32_t count = f->get_32();
  337. for (uint32_t i = 0; i < count; i++) {
  338. uint32_t slen = f->get_32();
  339. CharString cs;
  340. cs.resize(slen + 1);
  341. cs[slen] = 0;
  342. f->get_buffer((uint8_t *)cs.ptr(), slen);
  343. String key;
  344. key.parse_utf8(cs.ptr());
  345. uint32_t vlen = f->get_32();
  346. Vector<uint8_t> d;
  347. d.resize(vlen);
  348. f->get_buffer(d.ptrw(), vlen);
  349. Variant value;
  350. Error err = decode_variant(value, d.ptr(), d.size());
  351. ERR_EXPLAIN("Error decoding property: " + key);
  352. ERR_CONTINUE(err != OK);
  353. set(key, value);
  354. }
  355. return OK;
  356. }
  357. Error ProjectSettings::_load_settings_text(const String p_path) {
  358. Error err;
  359. FileAccess *f = FileAccess::open(p_path, FileAccess::READ, &err);
  360. if (!f) {
  361. // FIXME: Above 'err' error code is ERR_FILE_CANT_OPEN if the file is missing
  362. // This needs to be streamlined if we want decent error reporting
  363. return ERR_FILE_NOT_FOUND;
  364. }
  365. VariantParser::StreamFile stream;
  366. stream.f = f;
  367. String assign;
  368. Variant value;
  369. VariantParser::Tag next_tag;
  370. int lines = 0;
  371. String error_text;
  372. String section;
  373. while (true) {
  374. assign = Variant();
  375. next_tag.fields.clear();
  376. next_tag.name = String();
  377. err = VariantParser::parse_tag_assign_eof(&stream, lines, error_text, next_tag, assign, value, NULL, true);
  378. if (err == ERR_FILE_EOF) {
  379. memdelete(f);
  380. return OK;
  381. } else if (err != OK) {
  382. ERR_PRINTS("Error parsing " + p_path + " at line " + itos(lines) + ": " + error_text + " File might be corrupted.");
  383. memdelete(f);
  384. return err;
  385. }
  386. if (assign != String()) {
  387. if (section == String() && assign == "config_version") {
  388. int config_version = value;
  389. if (config_version > FORMAT_VERSION) {
  390. memdelete(f);
  391. ERR_FAIL_COND_V(config_version > FORMAT_VERSION, ERR_FILE_CANT_OPEN);
  392. }
  393. } else {
  394. // config_version is checked and dropped
  395. set(section + "/" + assign, value);
  396. }
  397. } else if (next_tag.name != String()) {
  398. section = next_tag.name;
  399. }
  400. }
  401. memdelete(f);
  402. return OK;
  403. }
  404. Error ProjectSettings::_load_settings_text_or_binary(const String p_text_path, const String p_bin_path) {
  405. // Attempt first to load the text-based project.godot file
  406. Error err_text = _load_settings_text(p_text_path);
  407. if (err_text == OK) {
  408. return OK;
  409. } else if (err_text != ERR_FILE_NOT_FOUND) {
  410. // If the text-based file exists but can't be loaded, we want to know it
  411. ERR_PRINTS("Couldn't load file '" + p_text_path + "', error code " + itos(err_text) + ".");
  412. return err_text;
  413. }
  414. // Fallback to binary project.binary file if text-based was not found
  415. Error err_bin = _load_settings_binary(p_bin_path);
  416. return err_bin;
  417. }
  418. int ProjectSettings::get_order(const String &p_name) const {
  419. ERR_FAIL_COND_V(!props.has(p_name), -1);
  420. return props[p_name].order;
  421. }
  422. void ProjectSettings::set_order(const String &p_name, int p_order) {
  423. ERR_FAIL_COND(!props.has(p_name));
  424. props[p_name].order = p_order;
  425. }
  426. void ProjectSettings::set_builtin_order(const String &p_name) {
  427. ERR_FAIL_COND(!props.has(p_name));
  428. if (props[p_name].order >= NO_BUILTIN_ORDER_BASE) {
  429. props[p_name].order = last_builtin_order++;
  430. }
  431. }
  432. void ProjectSettings::clear(const String &p_name) {
  433. ERR_FAIL_COND(!props.has(p_name));
  434. props.erase(p_name);
  435. }
  436. Error ProjectSettings::save() {
  437. return save_custom(get_resource_path().plus_file("project.godot"));
  438. }
  439. Error ProjectSettings::_save_settings_binary(const String &p_file, const Map<String, List<String> > &props, const CustomMap &p_custom, const String &p_custom_features) {
  440. Error err;
  441. FileAccess *file = FileAccess::open(p_file, FileAccess::WRITE, &err);
  442. if (err != OK) {
  443. ERR_EXPLAIN("Couldn't save project.binary at " + p_file);
  444. ERR_FAIL_COND_V(err, err)
  445. }
  446. uint8_t hdr[4] = { 'E', 'C', 'F', 'G' };
  447. file->store_buffer(hdr, 4);
  448. int count = 0;
  449. for (Map<String, List<String> >::Element *E = props.front(); E; E = E->next()) {
  450. for (List<String>::Element *F = E->get().front(); F; F = F->next()) {
  451. count++;
  452. }
  453. }
  454. if (p_custom_features != String()) {
  455. file->store_32(count + 1);
  456. //store how many properties are saved, add one for custom featuers, which must always go first
  457. String key = CoreStringNames::get_singleton()->_custom_features;
  458. file->store_32(key.length());
  459. file->store_string(key);
  460. int len;
  461. Error err = encode_variant(p_custom_features, NULL, len);
  462. if (err != OK) {
  463. memdelete(file);
  464. ERR_FAIL_V(err);
  465. }
  466. Vector<uint8_t> buff;
  467. buff.resize(len);
  468. err = encode_variant(p_custom_features, &buff[0], len);
  469. if (err != OK) {
  470. memdelete(file);
  471. ERR_FAIL_V(err);
  472. }
  473. file->store_32(len);
  474. file->store_buffer(buff.ptr(), buff.size());
  475. } else {
  476. file->store_32(count); //store how many properties are saved
  477. }
  478. for (Map<String, List<String> >::Element *E = props.front(); E; E = E->next()) {
  479. for (List<String>::Element *F = E->get().front(); F; F = F->next()) {
  480. String key = F->get();
  481. if (E->key() != "")
  482. key = E->key() + "/" + key;
  483. Variant value;
  484. if (p_custom.has(key))
  485. value = p_custom[key];
  486. else
  487. value = get(key);
  488. file->store_32(key.length());
  489. file->store_string(key);
  490. int len;
  491. Error err = encode_variant(value, NULL, len);
  492. if (err != OK)
  493. memdelete(file);
  494. ERR_FAIL_COND_V(err != OK, ERR_INVALID_DATA);
  495. Vector<uint8_t> buff;
  496. buff.resize(len);
  497. err = encode_variant(value, &buff[0], len);
  498. if (err != OK)
  499. memdelete(file);
  500. ERR_FAIL_COND_V(err != OK, ERR_INVALID_DATA);
  501. file->store_32(len);
  502. file->store_buffer(buff.ptr(), buff.size());
  503. }
  504. }
  505. file->close();
  506. memdelete(file);
  507. return OK;
  508. }
  509. Error ProjectSettings::_save_settings_text(const String &p_file, const Map<String, List<String> > &props, const CustomMap &p_custom, const String &p_custom_features) {
  510. Error err;
  511. FileAccess *file = FileAccess::open(p_file, FileAccess::WRITE, &err);
  512. if (err) {
  513. ERR_EXPLAIN("Couldn't save project.godot - " + p_file);
  514. ERR_FAIL_COND_V(err, err)
  515. }
  516. file->store_line("; Engine configuration file.");
  517. file->store_line("; It's best edited using the editor UI and not directly,");
  518. file->store_line("; since the parameters that go here are not all obvious.");
  519. file->store_line(";");
  520. file->store_line("; Format:");
  521. file->store_line("; [section] ; section goes between []");
  522. file->store_line("; param=value ; assign values to parameters");
  523. file->store_line("");
  524. file->store_string("config_version=" + itos(FORMAT_VERSION) + "\n");
  525. if (p_custom_features != String())
  526. file->store_string("custom_features=\"" + p_custom_features + "\"\n");
  527. file->store_string("\n");
  528. for (Map<String, List<String> >::Element *E = props.front(); E; E = E->next()) {
  529. if (E != props.front())
  530. file->store_string("\n");
  531. if (E->key() != "")
  532. file->store_string("[" + E->key() + "]\n\n");
  533. for (List<String>::Element *F = E->get().front(); F; F = F->next()) {
  534. String key = F->get();
  535. if (E->key() != "")
  536. key = E->key() + "/" + key;
  537. Variant value;
  538. if (p_custom.has(key))
  539. value = p_custom[key];
  540. else
  541. value = get(key);
  542. String vstr;
  543. VariantWriter::write_to_string(value, vstr);
  544. if (F->get().find(" ") != -1)
  545. file->store_string(F->get().quote() + "=" + vstr + "\n");
  546. else
  547. file->store_string(F->get() + "=" + vstr + "\n");
  548. }
  549. }
  550. file->close();
  551. memdelete(file);
  552. return OK;
  553. }
  554. Error ProjectSettings::_save_custom_bnd(const String &p_file) { // add other params as dictionary and array?
  555. return save_custom(p_file);
  556. };
  557. Error ProjectSettings::save_custom(const String &p_path, const CustomMap &p_custom, const Vector<String> &p_custom_features, bool p_merge_with_current) {
  558. ERR_FAIL_COND_V(p_path == "", ERR_INVALID_PARAMETER);
  559. Set<_VCSort> vclist;
  560. if (p_merge_with_current) {
  561. for (Map<StringName, VariantContainer>::Element *G = props.front(); G; G = G->next()) {
  562. const VariantContainer *v = &G->get();
  563. if (v->hide_from_editor)
  564. continue;
  565. if (p_custom.has(G->key()))
  566. continue;
  567. _VCSort vc;
  568. vc.name = G->key(); //*k;
  569. vc.order = v->order;
  570. vc.type = v->variant.get_type();
  571. vc.flags = PROPERTY_USAGE_EDITOR | PROPERTY_USAGE_STORAGE;
  572. if (v->variant == v->initial)
  573. continue;
  574. vclist.insert(vc);
  575. }
  576. }
  577. for (const Map<String, Variant>::Element *E = p_custom.front(); E; E = E->next()) {
  578. // Lookup global prop to store in the same order
  579. Map<StringName, VariantContainer>::Element *global_prop = props.find(E->key());
  580. _VCSort vc;
  581. vc.name = E->key();
  582. vc.order = global_prop ? global_prop->get().order : 0xFFFFFFF;
  583. vc.type = E->get().get_type();
  584. vc.flags = PROPERTY_USAGE_STORAGE;
  585. vclist.insert(vc);
  586. }
  587. Map<String, List<String> > props;
  588. for (Set<_VCSort>::Element *E = vclist.front(); E; E = E->next()) {
  589. String category = E->get().name;
  590. String name = E->get().name;
  591. int div = category.find("/");
  592. if (div < 0)
  593. category = "";
  594. else {
  595. category = category.substr(0, div);
  596. name = name.substr(div + 1, name.size());
  597. }
  598. props[category].push_back(name);
  599. }
  600. String custom_features;
  601. for (int i = 0; i < p_custom_features.size(); i++) {
  602. if (i > 0)
  603. custom_features += ",";
  604. String f = p_custom_features[i].strip_edges().replace("\"", "");
  605. custom_features += f;
  606. }
  607. if (p_path.ends_with(".godot"))
  608. return _save_settings_text(p_path, props, p_custom, custom_features);
  609. else if (p_path.ends_with(".binary"))
  610. return _save_settings_binary(p_path, props, p_custom, custom_features);
  611. else {
  612. ERR_EXPLAIN("Unknown config file format: " + p_path);
  613. ERR_FAIL_V(ERR_FILE_UNRECOGNIZED);
  614. }
  615. return OK;
  616. }
  617. Variant _GLOBAL_DEF(const String &p_var, const Variant &p_default) {
  618. Variant ret;
  619. if (!ProjectSettings::get_singleton()->has_setting(p_var)) {
  620. ProjectSettings::get_singleton()->set(p_var, p_default);
  621. }
  622. ret = ProjectSettings::get_singleton()->get(p_var);
  623. ProjectSettings::get_singleton()->set_initial_value(p_var, p_default);
  624. ProjectSettings::get_singleton()->set_builtin_order(p_var);
  625. return ret;
  626. }
  627. Vector<String> ProjectSettings::get_optimizer_presets() const {
  628. List<PropertyInfo> pi;
  629. ProjectSettings::get_singleton()->get_property_list(&pi);
  630. Vector<String> names;
  631. for (List<PropertyInfo>::Element *E = pi.front(); E; E = E->next()) {
  632. if (!E->get().name.begins_with("optimizer_presets/"))
  633. continue;
  634. names.push_back(E->get().name.get_slicec('/', 1));
  635. }
  636. names.sort();
  637. return names;
  638. }
  639. void ProjectSettings::_add_property_info_bind(const Dictionary &p_info) {
  640. ERR_FAIL_COND(!p_info.has("name"));
  641. ERR_FAIL_COND(!p_info.has("type"));
  642. PropertyInfo pinfo;
  643. pinfo.name = p_info["name"];
  644. ERR_FAIL_COND(!props.has(pinfo.name));
  645. pinfo.type = Variant::Type(p_info["type"].operator int());
  646. ERR_FAIL_INDEX(pinfo.type, Variant::VARIANT_MAX);
  647. if (p_info.has("hint"))
  648. pinfo.hint = PropertyHint(p_info["hint"].operator int());
  649. if (p_info.has("hint_string"))
  650. pinfo.hint_string = p_info["hint_string"];
  651. set_custom_property_info(pinfo.name, pinfo);
  652. }
  653. void ProjectSettings::set_custom_property_info(const String &p_prop, const PropertyInfo &p_info) {
  654. ERR_FAIL_COND(!props.has(p_prop));
  655. custom_prop_info[p_prop] = p_info;
  656. custom_prop_info[p_prop].name = p_prop;
  657. }
  658. void ProjectSettings::set_disable_feature_overrides(bool p_disable) {
  659. disable_feature_overrides = p_disable;
  660. }
  661. bool ProjectSettings::is_using_datapack() const {
  662. return using_datapack;
  663. }
  664. bool ProjectSettings::property_can_revert(const String &p_name) {
  665. if (!props.has(p_name))
  666. return false;
  667. return props[p_name].initial != props[p_name].variant;
  668. }
  669. Variant ProjectSettings::property_get_revert(const String &p_name) {
  670. if (!props.has(p_name))
  671. return Variant();
  672. return props[p_name].initial;
  673. }
  674. void ProjectSettings::set_setting(const String &p_setting, const Variant &p_value) {
  675. set(p_setting, p_value);
  676. }
  677. Variant ProjectSettings::get_setting(const String &p_setting) const {
  678. return get(p_setting);
  679. }
  680. void ProjectSettings::_bind_methods() {
  681. ClassDB::bind_method(D_METHOD("has_setting", "name"), &ProjectSettings::has_setting);
  682. ClassDB::bind_method(D_METHOD("set_setting", "name", "value"), &ProjectSettings::set_setting);
  683. ClassDB::bind_method(D_METHOD("get_setting", "name"), &ProjectSettings::get_setting);
  684. ClassDB::bind_method(D_METHOD("set_order", "name", "position"), &ProjectSettings::set_order);
  685. ClassDB::bind_method(D_METHOD("get_order", "name"), &ProjectSettings::get_order);
  686. ClassDB::bind_method(D_METHOD("set_initial_value", "name", "value"), &ProjectSettings::set_initial_value);
  687. ClassDB::bind_method(D_METHOD("add_property_info", "hint"), &ProjectSettings::_add_property_info_bind);
  688. ClassDB::bind_method(D_METHOD("clear", "name"), &ProjectSettings::clear);
  689. ClassDB::bind_method(D_METHOD("localize_path", "path"), &ProjectSettings::localize_path);
  690. ClassDB::bind_method(D_METHOD("globalize_path", "path"), &ProjectSettings::globalize_path);
  691. ClassDB::bind_method(D_METHOD("save"), &ProjectSettings::save);
  692. ClassDB::bind_method(D_METHOD("load_resource_pack", "pack"), &ProjectSettings::_load_resource_pack);
  693. ClassDB::bind_method(D_METHOD("property_can_revert", "name"), &ProjectSettings::property_can_revert);
  694. ClassDB::bind_method(D_METHOD("property_get_revert", "name"), &ProjectSettings::property_get_revert);
  695. ClassDB::bind_method(D_METHOD("save_custom", "file"), &ProjectSettings::_save_custom_bnd);
  696. }
  697. ProjectSettings::ProjectSettings() {
  698. singleton = this;
  699. last_order = NO_BUILTIN_ORDER_BASE;
  700. last_builtin_order = 0;
  701. disable_feature_overrides = false;
  702. registering_order = true;
  703. Array va;
  704. Ref<InputEventKey> key;
  705. Ref<InputEventJoypadButton> joyb;
  706. GLOBAL_DEF("application/config/name", "");
  707. GLOBAL_DEF("application/run/main_scene", "");
  708. custom_prop_info["application/run/main_scene"] = PropertyInfo(Variant::STRING, "application/run/main_scene", PROPERTY_HINT_FILE, "tscn,scn,res");
  709. GLOBAL_DEF("application/run/disable_stdout", false);
  710. GLOBAL_DEF("application/run/disable_stderr", false);
  711. GLOBAL_DEF("application/config/use_custom_user_dir", false);
  712. GLOBAL_DEF("application/config/custom_user_dir_name", "");
  713. key.instance();
  714. key->set_scancode(KEY_ENTER);
  715. va.push_back(key);
  716. key.instance();
  717. key->set_scancode(KEY_KP_ENTER);
  718. va.push_back(key);
  719. key.instance();
  720. key->set_scancode(KEY_SPACE);
  721. va.push_back(key);
  722. joyb.instance();
  723. joyb->set_button_index(JOY_BUTTON_0);
  724. va.push_back(joyb);
  725. GLOBAL_DEF("input/ui_accept", va);
  726. input_presets.push_back("input/ui_accept");
  727. va = Array();
  728. key.instance();
  729. key->set_scancode(KEY_SPACE);
  730. va.push_back(key);
  731. joyb.instance();
  732. joyb->set_button_index(JOY_BUTTON_3);
  733. va.push_back(joyb);
  734. GLOBAL_DEF("input/ui_select", va);
  735. input_presets.push_back("input/ui_select");
  736. va = Array();
  737. key.instance();
  738. key->set_scancode(KEY_ESCAPE);
  739. va.push_back(key);
  740. joyb.instance();
  741. joyb->set_button_index(JOY_BUTTON_1);
  742. va.push_back(joyb);
  743. GLOBAL_DEF("input/ui_cancel", va);
  744. input_presets.push_back("input/ui_cancel");
  745. va = Array();
  746. key.instance();
  747. key->set_scancode(KEY_TAB);
  748. va.push_back(key);
  749. GLOBAL_DEF("input/ui_focus_next", va);
  750. input_presets.push_back("input/ui_focus_next");
  751. va = Array();
  752. key.instance();
  753. key->set_scancode(KEY_TAB);
  754. key->set_shift(true);
  755. va.push_back(key);
  756. GLOBAL_DEF("input/ui_focus_prev", va);
  757. input_presets.push_back("input/ui_focus_prev");
  758. va = Array();
  759. key.instance();
  760. key->set_scancode(KEY_LEFT);
  761. va.push_back(key);
  762. joyb.instance();
  763. joyb->set_button_index(JOY_DPAD_LEFT);
  764. va.push_back(joyb);
  765. GLOBAL_DEF("input/ui_left", va);
  766. input_presets.push_back("input/ui_left");
  767. va = Array();
  768. key.instance();
  769. key->set_scancode(KEY_RIGHT);
  770. va.push_back(key);
  771. joyb.instance();
  772. joyb->set_button_index(JOY_DPAD_RIGHT);
  773. va.push_back(joyb);
  774. GLOBAL_DEF("input/ui_right", va);
  775. input_presets.push_back("input/ui_right");
  776. va = Array();
  777. key.instance();
  778. key->set_scancode(KEY_UP);
  779. va.push_back(key);
  780. joyb.instance();
  781. joyb->set_button_index(JOY_DPAD_UP);
  782. va.push_back(joyb);
  783. GLOBAL_DEF("input/ui_up", va);
  784. input_presets.push_back("input/ui_up");
  785. va = Array();
  786. key.instance();
  787. key->set_scancode(KEY_DOWN);
  788. va.push_back(key);
  789. joyb.instance();
  790. joyb->set_button_index(JOY_DPAD_DOWN);
  791. va.push_back(joyb);
  792. GLOBAL_DEF("input/ui_down", va);
  793. input_presets.push_back("input/ui_down");
  794. va = Array();
  795. key.instance();
  796. key->set_scancode(KEY_PAGEUP);
  797. va.push_back(key);
  798. GLOBAL_DEF("input/ui_page_up", va);
  799. input_presets.push_back("input/ui_page_up");
  800. va = Array();
  801. key.instance();
  802. key->set_scancode(KEY_PAGEDOWN);
  803. va.push_back(key);
  804. GLOBAL_DEF("input/ui_page_down", va);
  805. input_presets.push_back("input/ui_page_down");
  806. va = Array();
  807. key.instance();
  808. key->set_scancode(KEY_HOME);
  809. va.push_back(key);
  810. GLOBAL_DEF("input/ui_home", va);
  811. input_presets.push_back("input/ui_home");
  812. va = Array();
  813. key.instance();
  814. key->set_scancode(KEY_END);
  815. va.push_back(key);
  816. GLOBAL_DEF("input/ui_end", va);
  817. input_presets.push_back("input/ui_end");
  818. //GLOBAL_DEF("display/window/handheld/orientation", "landscape");
  819. 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");
  820. custom_prop_info["rendering/threads/thread_model"] = PropertyInfo(Variant::INT, "rendering/threads/thread_model", PROPERTY_HINT_ENUM, "Single-Unsafe,Single-Safe,Multi-Threaded");
  821. custom_prop_info["physics/2d/thread_model"] = PropertyInfo(Variant::INT, "physics/2d/thread_model", PROPERTY_HINT_ENUM, "Single-Unsafe,Single-Safe,Multi-Threaded");
  822. 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");
  823. GLOBAL_DEF("rendering/quality/intended_usage/framebuffer_mode", 2);
  824. GLOBAL_DEF("debug/settings/profiler/max_functions", 16384);
  825. //assigning here, because using GLOBAL_GET on every block for compressing can be slow
  826. Compression::zstd_long_distance_matching = GLOBAL_DEF("compression/formats/zstd/long_distance_matching", false);
  827. custom_prop_info["compression/formats/zstd/long_distance_matching"] = PropertyInfo(Variant::BOOL, "compression/formats/zstd/long_distance_matching");
  828. Compression::zstd_level = GLOBAL_DEF("compression/formats/zstd/compression_level", 3);
  829. custom_prop_info["compression/formats/zstd/compression_level"] = PropertyInfo(Variant::INT, "compression/formats/zstd/compression_level", PROPERTY_HINT_RANGE, "1,22,1");
  830. Compression::zstd_window_log_size = GLOBAL_DEF("compression/formats/zstd/window_log_size", 27);
  831. custom_prop_info["compression/formats/zstd/window_log_size"] = PropertyInfo(Variant::INT, "compression/formats/zstd/window_log_size", PROPERTY_HINT_RANGE, "10,30,1");
  832. Compression::zlib_level = GLOBAL_DEF("compression/formats/zlib/compression_level", Z_DEFAULT_COMPRESSION);
  833. custom_prop_info["compression/formats/zlib/compression_level"] = PropertyInfo(Variant::INT, "compression/formats/zlib/compression_level", PROPERTY_HINT_RANGE, "-1,9,1");
  834. Compression::gzip_level = GLOBAL_DEF("compression/formats/gzip/compression_level", Z_DEFAULT_COMPRESSION);
  835. custom_prop_info["compression/formats/gzip/compression_level"] = PropertyInfo(Variant::INT, "compression/formats/gzip/compression_level", PROPERTY_HINT_RANGE, "-1,9,1");
  836. using_datapack = false;
  837. }
  838. ProjectSettings::~ProjectSettings() {
  839. singleton = NULL;
  840. }