project_settings.cpp 31 KB

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