project_settings.cpp 34 KB

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