2
0

project_settings.cpp 32 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107
  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. if (section == String()) {
  396. set(assign, value);
  397. } else {
  398. set(section + "/" + assign, value);
  399. }
  400. }
  401. } else if (next_tag.name != String()) {
  402. section = next_tag.name;
  403. }
  404. }
  405. memdelete(f);
  406. return OK;
  407. }
  408. Error ProjectSettings::_load_settings_text_or_binary(const String p_text_path, const String p_bin_path) {
  409. // Attempt first to load the text-based project.godot file
  410. Error err_text = _load_settings_text(p_text_path);
  411. if (err_text == OK) {
  412. return OK;
  413. } else if (err_text != ERR_FILE_NOT_FOUND) {
  414. // If the text-based file exists but can't be loaded, we want to know it
  415. ERR_PRINTS("Couldn't load file '" + p_text_path + "', error code " + itos(err_text) + ".");
  416. return err_text;
  417. }
  418. // Fallback to binary project.binary file if text-based was not found
  419. Error err_bin = _load_settings_binary(p_bin_path);
  420. return err_bin;
  421. }
  422. int ProjectSettings::get_order(const String &p_name) const {
  423. ERR_FAIL_COND_V(!props.has(p_name), -1);
  424. return props[p_name].order;
  425. }
  426. void ProjectSettings::set_order(const String &p_name, int p_order) {
  427. ERR_FAIL_COND(!props.has(p_name));
  428. props[p_name].order = p_order;
  429. }
  430. void ProjectSettings::set_builtin_order(const String &p_name) {
  431. ERR_FAIL_COND(!props.has(p_name));
  432. if (props[p_name].order >= NO_BUILTIN_ORDER_BASE) {
  433. props[p_name].order = last_builtin_order++;
  434. }
  435. }
  436. void ProjectSettings::clear(const String &p_name) {
  437. ERR_FAIL_COND(!props.has(p_name));
  438. props.erase(p_name);
  439. }
  440. Error ProjectSettings::save() {
  441. return save_custom(get_resource_path().plus_file("project.godot"));
  442. }
  443. Error ProjectSettings::_save_settings_binary(const String &p_file, const Map<String, List<String> > &props, const CustomMap &p_custom, const String &p_custom_features) {
  444. Error err;
  445. FileAccess *file = FileAccess::open(p_file, FileAccess::WRITE, &err);
  446. if (err != OK) {
  447. ERR_EXPLAIN("Couldn't save project.binary at " + p_file);
  448. ERR_FAIL_COND_V(err, err)
  449. }
  450. uint8_t hdr[4] = { 'E', 'C', 'F', 'G' };
  451. file->store_buffer(hdr, 4);
  452. int count = 0;
  453. for (Map<String, List<String> >::Element *E = props.front(); E; E = E->next()) {
  454. for (List<String>::Element *F = E->get().front(); F; F = F->next()) {
  455. count++;
  456. }
  457. }
  458. if (p_custom_features != String()) {
  459. file->store_32(count + 1);
  460. //store how many properties are saved, add one for custom featuers, which must always go first
  461. String key = CoreStringNames::get_singleton()->_custom_features;
  462. file->store_32(key.length());
  463. file->store_string(key);
  464. int len;
  465. Error err = encode_variant(p_custom_features, NULL, len);
  466. if (err != OK) {
  467. memdelete(file);
  468. ERR_FAIL_V(err);
  469. }
  470. Vector<uint8_t> buff;
  471. buff.resize(len);
  472. err = encode_variant(p_custom_features, &buff[0], len);
  473. if (err != OK) {
  474. memdelete(file);
  475. ERR_FAIL_V(err);
  476. }
  477. file->store_32(len);
  478. file->store_buffer(buff.ptr(), buff.size());
  479. } else {
  480. file->store_32(count); //store how many properties are saved
  481. }
  482. for (Map<String, List<String> >::Element *E = props.front(); E; E = E->next()) {
  483. for (List<String>::Element *F = E->get().front(); F; F = F->next()) {
  484. String key = F->get();
  485. if (E->key() != "")
  486. key = E->key() + "/" + key;
  487. Variant value;
  488. if (p_custom.has(key))
  489. value = p_custom[key];
  490. else
  491. value = get(key);
  492. file->store_32(key.length());
  493. file->store_string(key);
  494. int len;
  495. Error err = encode_variant(value, NULL, len);
  496. if (err != OK)
  497. memdelete(file);
  498. ERR_FAIL_COND_V(err != OK, ERR_INVALID_DATA);
  499. Vector<uint8_t> buff;
  500. buff.resize(len);
  501. err = encode_variant(value, &buff[0], len);
  502. if (err != OK)
  503. memdelete(file);
  504. ERR_FAIL_COND_V(err != OK, ERR_INVALID_DATA);
  505. file->store_32(len);
  506. file->store_buffer(buff.ptr(), buff.size());
  507. }
  508. }
  509. file->close();
  510. memdelete(file);
  511. return OK;
  512. }
  513. Error ProjectSettings::_save_settings_text(const String &p_file, const Map<String, List<String> > &props, const CustomMap &p_custom, const String &p_custom_features) {
  514. Error err;
  515. FileAccess *file = FileAccess::open(p_file, FileAccess::WRITE, &err);
  516. if (err) {
  517. ERR_EXPLAIN("Couldn't save project.godot - " + p_file);
  518. ERR_FAIL_COND_V(err, err)
  519. }
  520. file->store_line("; Engine configuration file.");
  521. file->store_line("; It's best edited using the editor UI and not directly,");
  522. file->store_line("; since the parameters that go here are not all obvious.");
  523. file->store_line(";");
  524. file->store_line("; Format:");
  525. file->store_line("; [section] ; section goes between []");
  526. file->store_line("; param=value ; assign values to parameters");
  527. file->store_line("");
  528. file->store_string("config_version=" + itos(FORMAT_VERSION) + "\n");
  529. if (p_custom_features != String())
  530. file->store_string("custom_features=\"" + p_custom_features + "\"\n");
  531. file->store_string("\n");
  532. for (Map<String, List<String> >::Element *E = props.front(); E; E = E->next()) {
  533. if (E != props.front())
  534. file->store_string("\n");
  535. if (E->key() != "")
  536. file->store_string("[" + E->key() + "]\n\n");
  537. for (List<String>::Element *F = E->get().front(); F; F = F->next()) {
  538. String key = F->get();
  539. if (E->key() != "")
  540. key = E->key() + "/" + key;
  541. Variant value;
  542. if (p_custom.has(key))
  543. value = p_custom[key];
  544. else
  545. value = get(key);
  546. String vstr;
  547. VariantWriter::write_to_string(value, vstr);
  548. if (F->get().find(" ") != -1)
  549. file->store_string(F->get().quote() + "=" + vstr + "\n");
  550. else
  551. file->store_string(F->get() + "=" + vstr + "\n");
  552. }
  553. }
  554. file->close();
  555. memdelete(file);
  556. return OK;
  557. }
  558. Error ProjectSettings::_save_custom_bnd(const String &p_file) { // add other params as dictionary and array?
  559. return save_custom(p_file);
  560. };
  561. Error ProjectSettings::save_custom(const String &p_path, const CustomMap &p_custom, const Vector<String> &p_custom_features, bool p_merge_with_current) {
  562. ERR_FAIL_COND_V(p_path == "", ERR_INVALID_PARAMETER);
  563. Set<_VCSort> vclist;
  564. if (p_merge_with_current) {
  565. for (Map<StringName, VariantContainer>::Element *G = props.front(); G; G = G->next()) {
  566. const VariantContainer *v = &G->get();
  567. if (v->hide_from_editor)
  568. continue;
  569. if (p_custom.has(G->key()))
  570. continue;
  571. _VCSort vc;
  572. vc.name = G->key(); //*k;
  573. vc.order = v->order;
  574. vc.type = v->variant.get_type();
  575. vc.flags = PROPERTY_USAGE_EDITOR | PROPERTY_USAGE_STORAGE;
  576. if (v->variant == v->initial)
  577. continue;
  578. vclist.insert(vc);
  579. }
  580. }
  581. for (const Map<String, Variant>::Element *E = p_custom.front(); E; E = E->next()) {
  582. // Lookup global prop to store in the same order
  583. Map<StringName, VariantContainer>::Element *global_prop = props.find(E->key());
  584. _VCSort vc;
  585. vc.name = E->key();
  586. vc.order = global_prop ? global_prop->get().order : 0xFFFFFFF;
  587. vc.type = E->get().get_type();
  588. vc.flags = PROPERTY_USAGE_STORAGE;
  589. vclist.insert(vc);
  590. }
  591. Map<String, List<String> > props;
  592. for (Set<_VCSort>::Element *E = vclist.front(); E; E = E->next()) {
  593. String category = E->get().name;
  594. String name = E->get().name;
  595. int div = category.find("/");
  596. if (div < 0)
  597. category = "";
  598. else {
  599. category = category.substr(0, div);
  600. name = name.substr(div + 1, name.size());
  601. }
  602. props[category].push_back(name);
  603. }
  604. String custom_features;
  605. for (int i = 0; i < p_custom_features.size(); i++) {
  606. if (i > 0)
  607. custom_features += ",";
  608. String f = p_custom_features[i].strip_edges().replace("\"", "");
  609. custom_features += f;
  610. }
  611. if (p_path.ends_with(".godot"))
  612. return _save_settings_text(p_path, props, p_custom, custom_features);
  613. else if (p_path.ends_with(".binary"))
  614. return _save_settings_binary(p_path, props, p_custom, custom_features);
  615. else {
  616. ERR_EXPLAIN("Unknown config file format: " + p_path);
  617. ERR_FAIL_V(ERR_FILE_UNRECOGNIZED);
  618. }
  619. return OK;
  620. }
  621. Variant _GLOBAL_DEF(const String &p_var, const Variant &p_default) {
  622. Variant ret;
  623. if (!ProjectSettings::get_singleton()->has_setting(p_var)) {
  624. ProjectSettings::get_singleton()->set(p_var, p_default);
  625. }
  626. ret = ProjectSettings::get_singleton()->get(p_var);
  627. ProjectSettings::get_singleton()->set_initial_value(p_var, p_default);
  628. ProjectSettings::get_singleton()->set_builtin_order(p_var);
  629. return ret;
  630. }
  631. Vector<String> ProjectSettings::get_optimizer_presets() const {
  632. List<PropertyInfo> pi;
  633. ProjectSettings::get_singleton()->get_property_list(&pi);
  634. Vector<String> names;
  635. for (List<PropertyInfo>::Element *E = pi.front(); E; E = E->next()) {
  636. if (!E->get().name.begins_with("optimizer_presets/"))
  637. continue;
  638. names.push_back(E->get().name.get_slicec('/', 1));
  639. }
  640. names.sort();
  641. return names;
  642. }
  643. void ProjectSettings::_add_property_info_bind(const Dictionary &p_info) {
  644. ERR_FAIL_COND(!p_info.has("name"));
  645. ERR_FAIL_COND(!p_info.has("type"));
  646. PropertyInfo pinfo;
  647. pinfo.name = p_info["name"];
  648. ERR_FAIL_COND(!props.has(pinfo.name));
  649. pinfo.type = Variant::Type(p_info["type"].operator int());
  650. ERR_FAIL_INDEX(pinfo.type, Variant::VARIANT_MAX);
  651. if (p_info.has("hint"))
  652. pinfo.hint = PropertyHint(p_info["hint"].operator int());
  653. if (p_info.has("hint_string"))
  654. pinfo.hint_string = p_info["hint_string"];
  655. set_custom_property_info(pinfo.name, pinfo);
  656. }
  657. void ProjectSettings::set_custom_property_info(const String &p_prop, const PropertyInfo &p_info) {
  658. ERR_FAIL_COND(!props.has(p_prop));
  659. custom_prop_info[p_prop] = p_info;
  660. custom_prop_info[p_prop].name = p_prop;
  661. }
  662. const Map<StringName, PropertyInfo> &ProjectSettings::get_custom_property_info() const {
  663. return custom_prop_info;
  664. }
  665. void ProjectSettings::set_disable_feature_overrides(bool p_disable) {
  666. disable_feature_overrides = p_disable;
  667. }
  668. bool ProjectSettings::is_using_datapack() const {
  669. return using_datapack;
  670. }
  671. bool ProjectSettings::property_can_revert(const String &p_name) {
  672. if (!props.has(p_name))
  673. return false;
  674. return props[p_name].initial != props[p_name].variant;
  675. }
  676. Variant ProjectSettings::property_get_revert(const String &p_name) {
  677. if (!props.has(p_name))
  678. return Variant();
  679. return props[p_name].initial;
  680. }
  681. void ProjectSettings::set_setting(const String &p_setting, const Variant &p_value) {
  682. set(p_setting, p_value);
  683. }
  684. Variant ProjectSettings::get_setting(const String &p_setting) const {
  685. return get(p_setting);
  686. }
  687. bool ProjectSettings::has_custom_feature(const String &p_feature) const {
  688. return custom_features.has(p_feature);
  689. }
  690. void ProjectSettings::_bind_methods() {
  691. ClassDB::bind_method(D_METHOD("has_setting", "name"), &ProjectSettings::has_setting);
  692. ClassDB::bind_method(D_METHOD("set_setting", "name", "value"), &ProjectSettings::set_setting);
  693. ClassDB::bind_method(D_METHOD("get_setting", "name"), &ProjectSettings::get_setting);
  694. ClassDB::bind_method(D_METHOD("set_order", "name", "position"), &ProjectSettings::set_order);
  695. ClassDB::bind_method(D_METHOD("get_order", "name"), &ProjectSettings::get_order);
  696. ClassDB::bind_method(D_METHOD("set_initial_value", "name", "value"), &ProjectSettings::set_initial_value);
  697. ClassDB::bind_method(D_METHOD("add_property_info", "hint"), &ProjectSettings::_add_property_info_bind);
  698. ClassDB::bind_method(D_METHOD("clear", "name"), &ProjectSettings::clear);
  699. ClassDB::bind_method(D_METHOD("localize_path", "path"), &ProjectSettings::localize_path);
  700. ClassDB::bind_method(D_METHOD("globalize_path", "path"), &ProjectSettings::globalize_path);
  701. ClassDB::bind_method(D_METHOD("save"), &ProjectSettings::save);
  702. ClassDB::bind_method(D_METHOD("load_resource_pack", "pack"), &ProjectSettings::_load_resource_pack);
  703. ClassDB::bind_method(D_METHOD("property_can_revert", "name"), &ProjectSettings::property_can_revert);
  704. ClassDB::bind_method(D_METHOD("property_get_revert", "name"), &ProjectSettings::property_get_revert);
  705. ClassDB::bind_method(D_METHOD("save_custom", "file"), &ProjectSettings::_save_custom_bnd);
  706. }
  707. ProjectSettings::ProjectSettings() {
  708. singleton = this;
  709. last_order = NO_BUILTIN_ORDER_BASE;
  710. last_builtin_order = 0;
  711. disable_feature_overrides = false;
  712. registering_order = true;
  713. Array va;
  714. Ref<InputEventKey> key;
  715. Ref<InputEventJoypadButton> joyb;
  716. GLOBAL_DEF("application/config/name", "");
  717. GLOBAL_DEF("application/run/main_scene", "");
  718. custom_prop_info["application/run/main_scene"] = PropertyInfo(Variant::STRING, "application/run/main_scene", PROPERTY_HINT_FILE, "tscn,scn,res");
  719. GLOBAL_DEF("application/run/disable_stdout", false);
  720. GLOBAL_DEF("application/run/disable_stderr", false);
  721. GLOBAL_DEF("application/config/use_custom_user_dir", false);
  722. GLOBAL_DEF("application/config/custom_user_dir_name", "");
  723. key.instance();
  724. key->set_scancode(KEY_ENTER);
  725. va.push_back(key);
  726. key.instance();
  727. key->set_scancode(KEY_KP_ENTER);
  728. va.push_back(key);
  729. key.instance();
  730. key->set_scancode(KEY_SPACE);
  731. va.push_back(key);
  732. joyb.instance();
  733. joyb->set_button_index(JOY_BUTTON_0);
  734. va.push_back(joyb);
  735. GLOBAL_DEF("input/ui_accept", va);
  736. input_presets.push_back("input/ui_accept");
  737. va = Array();
  738. key.instance();
  739. key->set_scancode(KEY_SPACE);
  740. va.push_back(key);
  741. joyb.instance();
  742. joyb->set_button_index(JOY_BUTTON_3);
  743. va.push_back(joyb);
  744. GLOBAL_DEF("input/ui_select", va);
  745. input_presets.push_back("input/ui_select");
  746. va = Array();
  747. key.instance();
  748. key->set_scancode(KEY_ESCAPE);
  749. va.push_back(key);
  750. joyb.instance();
  751. joyb->set_button_index(JOY_BUTTON_1);
  752. va.push_back(joyb);
  753. GLOBAL_DEF("input/ui_cancel", va);
  754. input_presets.push_back("input/ui_cancel");
  755. va = Array();
  756. key.instance();
  757. key->set_scancode(KEY_TAB);
  758. va.push_back(key);
  759. GLOBAL_DEF("input/ui_focus_next", va);
  760. input_presets.push_back("input/ui_focus_next");
  761. va = Array();
  762. key.instance();
  763. key->set_scancode(KEY_TAB);
  764. key->set_shift(true);
  765. va.push_back(key);
  766. GLOBAL_DEF("input/ui_focus_prev", va);
  767. input_presets.push_back("input/ui_focus_prev");
  768. va = Array();
  769. key.instance();
  770. key->set_scancode(KEY_LEFT);
  771. va.push_back(key);
  772. joyb.instance();
  773. joyb->set_button_index(JOY_DPAD_LEFT);
  774. va.push_back(joyb);
  775. GLOBAL_DEF("input/ui_left", va);
  776. input_presets.push_back("input/ui_left");
  777. va = Array();
  778. key.instance();
  779. key->set_scancode(KEY_RIGHT);
  780. va.push_back(key);
  781. joyb.instance();
  782. joyb->set_button_index(JOY_DPAD_RIGHT);
  783. va.push_back(joyb);
  784. GLOBAL_DEF("input/ui_right", va);
  785. input_presets.push_back("input/ui_right");
  786. va = Array();
  787. key.instance();
  788. key->set_scancode(KEY_UP);
  789. va.push_back(key);
  790. joyb.instance();
  791. joyb->set_button_index(JOY_DPAD_UP);
  792. va.push_back(joyb);
  793. GLOBAL_DEF("input/ui_up", va);
  794. input_presets.push_back("input/ui_up");
  795. va = Array();
  796. key.instance();
  797. key->set_scancode(KEY_DOWN);
  798. va.push_back(key);
  799. joyb.instance();
  800. joyb->set_button_index(JOY_DPAD_DOWN);
  801. va.push_back(joyb);
  802. GLOBAL_DEF("input/ui_down", va);
  803. input_presets.push_back("input/ui_down");
  804. va = Array();
  805. key.instance();
  806. key->set_scancode(KEY_PAGEUP);
  807. va.push_back(key);
  808. GLOBAL_DEF("input/ui_page_up", va);
  809. input_presets.push_back("input/ui_page_up");
  810. va = Array();
  811. key.instance();
  812. key->set_scancode(KEY_PAGEDOWN);
  813. va.push_back(key);
  814. GLOBAL_DEF("input/ui_page_down", va);
  815. input_presets.push_back("input/ui_page_down");
  816. va = Array();
  817. key.instance();
  818. key->set_scancode(KEY_HOME);
  819. va.push_back(key);
  820. GLOBAL_DEF("input/ui_home", va);
  821. input_presets.push_back("input/ui_home");
  822. va = Array();
  823. key.instance();
  824. key->set_scancode(KEY_END);
  825. va.push_back(key);
  826. GLOBAL_DEF("input/ui_end", va);
  827. input_presets.push_back("input/ui_end");
  828. //GLOBAL_DEF("display/window/handheld/orientation", "landscape");
  829. 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");
  830. custom_prop_info["rendering/threads/thread_model"] = PropertyInfo(Variant::INT, "rendering/threads/thread_model", PROPERTY_HINT_ENUM, "Single-Unsafe,Single-Safe,Multi-Threaded");
  831. custom_prop_info["physics/2d/thread_model"] = PropertyInfo(Variant::INT, "physics/2d/thread_model", PROPERTY_HINT_ENUM, "Single-Unsafe,Single-Safe,Multi-Threaded");
  832. 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");
  833. GLOBAL_DEF("rendering/quality/intended_usage/framebuffer_mode", 2);
  834. GLOBAL_DEF("debug/settings/profiler/max_functions", 16384);
  835. //assigning here, because using GLOBAL_GET on every block for compressing can be slow
  836. Compression::zstd_long_distance_matching = GLOBAL_DEF("compression/formats/zstd/long_distance_matching", false);
  837. custom_prop_info["compression/formats/zstd/long_distance_matching"] = PropertyInfo(Variant::BOOL, "compression/formats/zstd/long_distance_matching");
  838. Compression::zstd_level = GLOBAL_DEF("compression/formats/zstd/compression_level", 3);
  839. custom_prop_info["compression/formats/zstd/compression_level"] = PropertyInfo(Variant::INT, "compression/formats/zstd/compression_level", PROPERTY_HINT_RANGE, "1,22,1");
  840. Compression::zstd_window_log_size = GLOBAL_DEF("compression/formats/zstd/window_log_size", 27);
  841. custom_prop_info["compression/formats/zstd/window_log_size"] = PropertyInfo(Variant::INT, "compression/formats/zstd/window_log_size", PROPERTY_HINT_RANGE, "10,30,1");
  842. Compression::zlib_level = GLOBAL_DEF("compression/formats/zlib/compression_level", Z_DEFAULT_COMPRESSION);
  843. custom_prop_info["compression/formats/zlib/compression_level"] = PropertyInfo(Variant::INT, "compression/formats/zlib/compression_level", PROPERTY_HINT_RANGE, "-1,9,1");
  844. Compression::gzip_level = GLOBAL_DEF("compression/formats/gzip/compression_level", Z_DEFAULT_COMPRESSION);
  845. custom_prop_info["compression/formats/gzip/compression_level"] = PropertyInfo(Variant::INT, "compression/formats/gzip/compression_level", PROPERTY_HINT_RANGE, "-1,9,1");
  846. using_datapack = false;
  847. }
  848. ProjectSettings::~ProjectSettings() {
  849. singleton = NULL;
  850. }