project_settings.cpp 29 KB

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