project_settings.cpp 31 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063
  1. /*************************************************************************/
  2. /* project_settings.cpp */
  3. /*************************************************************************/
  4. /* This file is part of: */
  5. /* GODOT ENGINE */
  6. /* https://godotengine.org */
  7. /*************************************************************************/
  8. /* Copyright (c) 2007-2018 Juan Linietsky, Ariel Manzur. */
  9. /* Copyright (c) 2014-2018 Godot Engine contributors (cf. AUTHORS.md) */
  10. /* */
  11. /* Permission is hereby granted, free of charge, to any person obtaining */
  12. /* a copy of this software and associated documentation files (the */
  13. /* "Software"), to deal in the Software without restriction, including */
  14. /* without limitation the rights to use, copy, modify, merge, publish, */
  15. /* distribute, sublicense, and/or sell copies of the Software, and to */
  16. /* permit persons to whom the Software is furnished to do so, subject to */
  17. /* the following conditions: */
  18. /* */
  19. /* The above copyright notice and this permission notice shall be */
  20. /* included in all copies or substantial portions of the Software. */
  21. /* */
  22. /* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, */
  23. /* EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF */
  24. /* MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.*/
  25. /* IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY */
  26. /* CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, */
  27. /* TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE */
  28. /* SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. */
  29. /*************************************************************************/
  30. #include "project_settings.h"
  31. #include "bind/core_bind.h"
  32. #include "core_string_names.h"
  33. #include "io/file_access_network.h"
  34. #include "io/file_access_pack.h"
  35. #include "io/marshalls.h"
  36. #include "os/dir_access.h"
  37. #include "os/file_access.h"
  38. #include "os/keyboard.h"
  39. #include "os/os.h"
  40. #include "variant_parser.h"
  41. #include <zlib.h>
  42. #define FORMAT_VERSION 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_user_data_dir();
  92. if (data_dir != "") {
  93. return p_path.replace("user:/", data_dir);
  94. };
  95. return p_path.replace("user://", "");
  96. }
  97. return p_path;
  98. }
  99. bool ProjectSettings::_set(const StringName &p_name, const Variant &p_value) {
  100. _THREAD_SAFE_METHOD_
  101. if (p_value.get_type() == Variant::NIL)
  102. props.erase(p_name);
  103. else {
  104. if (p_name == CoreStringNames::get_singleton()->_custom_features) {
  105. Vector<String> custom_feature_array = 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].overridden)
  130. props[p_name].variant = p_value;
  131. } else {
  132. props[p_name] = VariantContainer(p_value, last_order++);
  133. }
  134. }
  135. return true;
  136. }
  137. bool ProjectSettings::_get(const StringName &p_name, Variant &r_ret) const {
  138. _THREAD_SAFE_METHOD_
  139. StringName name = p_name;
  140. if (!disable_feature_overrides && feature_overrides.has(name)) {
  141. name = feature_overrides[name];
  142. }
  143. if (!props.has(name)) {
  144. print_line("WARNING: not found: " + String(name));
  145. return false;
  146. }
  147. r_ret = props[name].variant;
  148. return true;
  149. }
  150. struct _VCSort {
  151. String name;
  152. Variant::Type type;
  153. int order;
  154. int flags;
  155. bool operator<(const _VCSort &p_vcs) const { return order == p_vcs.order ? name < p_vcs.name : order < p_vcs.order; }
  156. };
  157. void ProjectSettings::_get_property_list(List<PropertyInfo> *p_list) const {
  158. _THREAD_SAFE_METHOD_
  159. Set<_VCSort> vclist;
  160. for (Map<StringName, VariantContainer>::Element *E = props.front(); E; E = E->next()) {
  161. const VariantContainer *v = &E->get();
  162. if (v->hide_from_editor)
  163. continue;
  164. _VCSort vc;
  165. vc.name = E->key();
  166. vc.order = v->order;
  167. vc.type = v->variant.get_type();
  168. if (vc.name.begins_with("input/") || vc.name.begins_with("import/") || vc.name.begins_with("export/") || vc.name.begins_with("/remap") || vc.name.begins_with("/locale") || vc.name.begins_with("/autoload"))
  169. vc.flags = PROPERTY_USAGE_STORAGE;
  170. else
  171. vc.flags = PROPERTY_USAGE_EDITOR | PROPERTY_USAGE_STORAGE;
  172. vclist.insert(vc);
  173. }
  174. for (Set<_VCSort>::Element *E = vclist.front(); E; E = E->next()) {
  175. String prop_info_name = E->get().name;
  176. int dot = prop_info_name.find(".");
  177. if (dot != -1)
  178. prop_info_name = prop_info_name.substr(0, dot);
  179. if (custom_prop_info.has(prop_info_name)) {
  180. PropertyInfo pi = custom_prop_info[prop_info_name];
  181. pi.name = E->get().name;
  182. pi.usage = E->get().flags;
  183. p_list->push_back(pi);
  184. } else
  185. p_list->push_back(PropertyInfo(E->get().type, E->get().name, PROPERTY_HINT_NONE, "", E->get().flags));
  186. }
  187. }
  188. bool ProjectSettings::_load_resource_pack(const String &p_pack) {
  189. if (PackedData::get_singleton()->is_disabled())
  190. return false;
  191. bool ok = PackedData::get_singleton()->add_pack(p_pack) == OK;
  192. if (!ok)
  193. return false;
  194. //if data.pck is found, all directory access will be from here
  195. DirAccess::make_default<DirAccessPack>(DirAccess::ACCESS_RESOURCES);
  196. using_datapack = true;
  197. return true;
  198. }
  199. Error ProjectSettings::setup(const String &p_path, const String &p_main_pack, bool p_upwards) {
  200. //If looking for files in network, just use network!
  201. if (FileAccessNetworkClient::get_singleton()) {
  202. Error err = _load_settings_text_or_binary("res://project.godot", "res://project.binary");
  203. if (err == OK) {
  204. // Optional, we don't mind if it fails
  205. _load_settings_text("res://override.cfg");
  206. }
  207. return err;
  208. }
  209. String exec_path = OS::get_singleton()->get_executable_path();
  210. //Attempt with a passed main pack first
  211. if (p_main_pack != "") {
  212. bool ok = _load_resource_pack(p_main_pack);
  213. ERR_FAIL_COND_V(!ok, ERR_CANT_OPEN);
  214. Error err = _load_settings_text_or_binary("res://project.godot", "res://project.binary");
  215. if (err == OK) {
  216. // Load override from location of the main pack
  217. // Optional, we don't mind if it fails
  218. _load_settings_text(p_main_pack.get_base_dir().plus_file("override.cfg"));
  219. }
  220. return err;
  221. }
  222. //Attempt with execname.pck
  223. if (exec_path != "") {
  224. bool found = false;
  225. // 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!)
  226. String filebase_name = exec_path.get_file().get_basename();
  227. // try to open at the location of executable
  228. String datapack_name = exec_path.get_base_dir().plus_file(filebase_name) + ".pck";
  229. if (_load_resource_pack(datapack_name)) {
  230. found = true;
  231. } else {
  232. datapack_name = filebase_name + ".pck";
  233. if (_load_resource_pack(datapack_name)) {
  234. found = true;
  235. }
  236. }
  237. // if we opened our package, try and load our project...
  238. if (found) {
  239. Error err = _load_settings_text_or_binary("res://project.godot", "res://project.binary");
  240. if (err == OK) {
  241. // Load override from location of executable
  242. // Optional, we don't mind if it fails
  243. _load_settings_text(exec_path.get_base_dir().plus_file("override.cfg"));
  244. }
  245. return err;
  246. }
  247. }
  248. //Try to use the filesystem for files, according to OS. (only Android -when reading from pck- and iOS use this)
  249. if (OS::get_singleton()->get_resource_dir() != "") {
  250. //OS will call Globals->get_resource_path which will be empty if not overridden!
  251. //if the OS would rather use somewhere else, then it will not be empty.
  252. resource_path = OS::get_singleton()->get_resource_dir().replace("\\", "/");
  253. if (resource_path.length() && resource_path[resource_path.length() - 1] == '/')
  254. resource_path = resource_path.substr(0, resource_path.length() - 1); // chop end
  255. // data.pck and data.zip are deprecated and no longer supported, apologies.
  256. // make sure this is loaded from the resource path
  257. Error err = _load_settings_text_or_binary("res://project.godot", "res://project.binary");
  258. if (err == OK) {
  259. // Optional, we don't mind if it fails
  260. _load_settings_text("res://override.cfg");
  261. }
  262. return err;
  263. }
  264. //Nothing was found, try to find a project.godot somewhere!
  265. DirAccess *d = DirAccess::create(DirAccess::ACCESS_FILESYSTEM);
  266. ERR_FAIL_COND_V(!d, ERR_CANT_CREATE);
  267. d->change_dir(p_path);
  268. String candidate = d->get_current_dir();
  269. String current_dir = d->get_current_dir();
  270. bool found = false;
  271. Error err;
  272. while (true) {
  273. err = _load_settings_text_or_binary(current_dir.plus_file("project.godot"), current_dir.plus_file("project.binary"));
  274. if (err == OK) {
  275. // Optional, we don't mind if it fails
  276. _load_settings_text(current_dir.plus_file("override.cfg"));
  277. candidate = current_dir;
  278. found = true;
  279. break;
  280. }
  281. if (p_upwards) {
  282. // Try to load settings ascending through dirs shape!
  283. d->change_dir("..");
  284. if (d->get_current_dir() == current_dir)
  285. break; //not doing anything useful
  286. current_dir = d->get_current_dir();
  287. } else {
  288. break;
  289. }
  290. }
  291. resource_path = candidate;
  292. resource_path = resource_path.replace("\\", "/"); // windows path to unix path just in case
  293. memdelete(d);
  294. if (!found)
  295. return err;
  296. if (resource_path.length() && resource_path[resource_path.length() - 1] == '/')
  297. resource_path = resource_path.substr(0, resource_path.length() - 1); // chop end
  298. return OK;
  299. }
  300. bool ProjectSettings::has_setting(String p_var) const {
  301. _THREAD_SAFE_METHOD_
  302. return props.has(p_var);
  303. }
  304. void ProjectSettings::set_registering_order(bool p_enable) {
  305. registering_order = p_enable;
  306. }
  307. Error ProjectSettings::_load_settings_binary(const String p_path) {
  308. Error err;
  309. FileAccess *f = FileAccess::open(p_path, FileAccess::READ, &err);
  310. if (err != OK) {
  311. return err;
  312. }
  313. uint8_t hdr[4];
  314. f->get_buffer(hdr, 4);
  315. if (hdr[0] != 'E' || hdr[1] != 'C' || hdr[2] != 'F' || hdr[3] != 'G') {
  316. memdelete(f);
  317. ERR_EXPLAIN("Corrupted header in binary project.binary (not ECFG)");
  318. ERR_FAIL_V(ERR_FILE_CORRUPT;)
  319. }
  320. uint32_t count = f->get_32();
  321. for (uint32_t i = 0; i < count; i++) {
  322. uint32_t slen = f->get_32();
  323. CharString cs;
  324. cs.resize(slen + 1);
  325. cs[slen] = 0;
  326. f->get_buffer((uint8_t *)cs.ptr(), slen);
  327. String key;
  328. key.parse_utf8(cs.ptr());
  329. uint32_t vlen = f->get_32();
  330. Vector<uint8_t> d;
  331. d.resize(vlen);
  332. f->get_buffer(d.ptrw(), vlen);
  333. Variant value;
  334. Error err = decode_variant(value, d.ptr(), d.size());
  335. ERR_EXPLAIN("Error decoding property: " + key);
  336. ERR_CONTINUE(err != OK);
  337. set(key, value);
  338. }
  339. return OK;
  340. }
  341. Error ProjectSettings::_load_settings_text(const String p_path) {
  342. Error err;
  343. FileAccess *f = FileAccess::open(p_path, FileAccess::READ, &err);
  344. if (!f) {
  345. // FIXME: Above 'err' error code is ERR_FILE_CANT_OPEN if the file is missing
  346. // This needs to be streamlined if we want decent error reporting
  347. return ERR_FILE_NOT_FOUND;
  348. }
  349. VariantParser::StreamFile stream;
  350. stream.f = f;
  351. String assign;
  352. Variant value;
  353. VariantParser::Tag next_tag;
  354. int lines = 0;
  355. String error_text;
  356. String section;
  357. while (true) {
  358. assign = Variant();
  359. next_tag.fields.clear();
  360. next_tag.name = String();
  361. err = VariantParser::parse_tag_assign_eof(&stream, lines, error_text, next_tag, assign, value, NULL, true);
  362. if (err == ERR_FILE_EOF) {
  363. memdelete(f);
  364. return OK;
  365. } else if (err != OK) {
  366. ERR_PRINTS("Error parsing " + p_path + " at line " + itos(lines) + ": " + error_text + " File might be corrupted.");
  367. memdelete(f);
  368. return err;
  369. }
  370. if (assign != String()) {
  371. if (section == String() && assign == "config_version") {
  372. int config_version = value;
  373. if (config_version > FORMAT_VERSION) {
  374. memdelete(f);
  375. ERR_FAIL_COND_V(config_version > FORMAT_VERSION, ERR_FILE_CANT_OPEN);
  376. }
  377. } else {
  378. // config_version is checked and dropped
  379. set(section + "/" + assign, value);
  380. }
  381. } else if (next_tag.name != String()) {
  382. section = next_tag.name;
  383. }
  384. }
  385. memdelete(f);
  386. return OK;
  387. }
  388. Error ProjectSettings::_load_settings_text_or_binary(const String p_text_path, const String p_bin_path) {
  389. // Attempt first to load the text-based project.godot file
  390. Error err_text = _load_settings_text(p_text_path);
  391. if (err_text == OK) {
  392. return OK;
  393. } else if (err_text != ERR_FILE_NOT_FOUND) {
  394. // If the text-based file exists but can't be loaded, we want to know it
  395. ERR_PRINTS("Couldn't load file '" + p_text_path + "', error code " + itos(err_text) + ".");
  396. return err_text;
  397. }
  398. // Fallback to binary project.binary file if text-based was not found
  399. Error err_bin = _load_settings_binary(p_bin_path);
  400. return err_bin;
  401. }
  402. int ProjectSettings::get_order(const String &p_name) const {
  403. ERR_FAIL_COND_V(!props.has(p_name), -1);
  404. return props[p_name].order;
  405. }
  406. void ProjectSettings::set_order(const String &p_name, int p_order) {
  407. ERR_FAIL_COND(!props.has(p_name));
  408. props[p_name].order = p_order;
  409. }
  410. void ProjectSettings::set_builtin_order(const String &p_name) {
  411. ERR_FAIL_COND(!props.has(p_name));
  412. if (props[p_name].order >= NO_BUILTIN_ORDER_BASE) {
  413. props[p_name].order = last_builtin_order++;
  414. }
  415. }
  416. void ProjectSettings::clear(const String &p_name) {
  417. ERR_FAIL_COND(!props.has(p_name));
  418. props.erase(p_name);
  419. }
  420. Error ProjectSettings::save() {
  421. return save_custom(get_resource_path().plus_file("project.godot"));
  422. }
  423. Error ProjectSettings::_save_settings_binary(const String &p_file, const Map<String, List<String> > &props, const CustomMap &p_custom, const String &p_custom_features) {
  424. Error err;
  425. FileAccess *file = FileAccess::open(p_file, FileAccess::WRITE, &err);
  426. if (err != OK) {
  427. ERR_EXPLAIN("Couldn't save project.binary at " + p_file);
  428. ERR_FAIL_COND_V(err, err)
  429. }
  430. uint8_t hdr[4] = { 'E', 'C', 'F', 'G' };
  431. file->store_buffer(hdr, 4);
  432. int count = 0;
  433. for (Map<String, List<String> >::Element *E = props.front(); E; E = E->next()) {
  434. for (List<String>::Element *F = E->get().front(); F; F = F->next()) {
  435. count++;
  436. }
  437. }
  438. if (p_custom_features != String()) {
  439. file->store_32(count + 1);
  440. //store how many properties are saved, add one for custom featuers, which must always go first
  441. String key = CoreStringNames::get_singleton()->_custom_features;
  442. file->store_32(key.length());
  443. file->store_string(key);
  444. int len;
  445. Error err = encode_variant(p_custom_features, NULL, len);
  446. if (err != OK) {
  447. memdelete(file);
  448. ERR_FAIL_V(err);
  449. }
  450. Vector<uint8_t> buff;
  451. buff.resize(len);
  452. err = encode_variant(p_custom_features, &buff[0], len);
  453. if (err != OK) {
  454. memdelete(file);
  455. ERR_FAIL_V(err);
  456. }
  457. file->store_32(len);
  458. file->store_buffer(buff.ptr(), buff.size());
  459. } else {
  460. file->store_32(count); //store how many properties are saved
  461. }
  462. for (Map<String, List<String> >::Element *E = props.front(); E; E = E->next()) {
  463. for (List<String>::Element *F = E->get().front(); F; F = F->next()) {
  464. String key = F->get();
  465. if (E->key() != "")
  466. key = E->key() + "/" + key;
  467. Variant value;
  468. if (p_custom.has(key))
  469. value = p_custom[key];
  470. else
  471. value = get(key);
  472. file->store_32(key.length());
  473. file->store_string(key);
  474. int len;
  475. Error err = encode_variant(value, NULL, len);
  476. if (err != OK)
  477. memdelete(file);
  478. ERR_FAIL_COND_V(err != OK, ERR_INVALID_DATA);
  479. Vector<uint8_t> buff;
  480. buff.resize(len);
  481. err = encode_variant(value, &buff[0], len);
  482. if (err != OK)
  483. memdelete(file);
  484. ERR_FAIL_COND_V(err != OK, ERR_INVALID_DATA);
  485. file->store_32(len);
  486. file->store_buffer(buff.ptr(), buff.size());
  487. }
  488. }
  489. file->close();
  490. memdelete(file);
  491. return OK;
  492. }
  493. Error ProjectSettings::_save_settings_text(const String &p_file, const Map<String, List<String> > &props, const CustomMap &p_custom, const String &p_custom_features) {
  494. Error err;
  495. FileAccess *file = FileAccess::open(p_file, FileAccess::WRITE, &err);
  496. if (err) {
  497. ERR_EXPLAIN("Couldn't save project.godot - " + p_file);
  498. ERR_FAIL_COND_V(err, err)
  499. }
  500. file->store_line("; Engine configuration file.");
  501. file->store_line("; It's best edited using the editor UI and not directly,");
  502. file->store_line("; since the parameters that go here are not all obvious.");
  503. file->store_line(";");
  504. file->store_line("; Format:");
  505. file->store_line("; [section] ; section goes between []");
  506. file->store_line("; param=value ; assign values to parameters");
  507. file->store_line("");
  508. file->store_string("config_version=" + itos(FORMAT_VERSION) + "\n");
  509. if (p_custom_features != String())
  510. file->store_string("custom_features=\"" + p_custom_features + "\"\n");
  511. file->store_string("\n");
  512. for (Map<String, List<String> >::Element *E = props.front(); E; E = E->next()) {
  513. if (E != props.front())
  514. file->store_string("\n");
  515. if (E->key() != "")
  516. file->store_string("[" + E->key() + "]\n\n");
  517. for (List<String>::Element *F = E->get().front(); F; F = F->next()) {
  518. String key = F->get();
  519. if (E->key() != "")
  520. key = E->key() + "/" + key;
  521. Variant value;
  522. if (p_custom.has(key))
  523. value = p_custom[key];
  524. else
  525. value = get(key);
  526. String vstr;
  527. VariantWriter::write_to_string(value, vstr);
  528. if (F->get().find(" ") != -1)
  529. file->store_string(F->get().quote() + "=" + vstr + "\n");
  530. else
  531. file->store_string(F->get() + "=" + vstr + "\n");
  532. }
  533. }
  534. file->close();
  535. memdelete(file);
  536. return OK;
  537. }
  538. Error ProjectSettings::_save_custom_bnd(const String &p_file) { // add other params as dictionary and array?
  539. return save_custom(p_file);
  540. };
  541. Error ProjectSettings::save_custom(const String &p_path, const CustomMap &p_custom, const Vector<String> &p_custom_features, bool p_merge_with_current) {
  542. ERR_FAIL_COND_V(p_path == "", ERR_INVALID_PARAMETER);
  543. Set<_VCSort> vclist;
  544. if (p_merge_with_current) {
  545. for (Map<StringName, VariantContainer>::Element *G = props.front(); G; G = G->next()) {
  546. const VariantContainer *v = &G->get();
  547. if (v->hide_from_editor)
  548. continue;
  549. if (p_custom.has(G->key()))
  550. continue;
  551. _VCSort vc;
  552. vc.name = G->key(); //*k;
  553. vc.order = v->order;
  554. vc.type = v->variant.get_type();
  555. vc.flags = PROPERTY_USAGE_EDITOR | PROPERTY_USAGE_STORAGE;
  556. if (v->variant == v->initial)
  557. continue;
  558. vclist.insert(vc);
  559. }
  560. }
  561. for (const Map<String, Variant>::Element *E = p_custom.front(); E; E = E->next()) {
  562. // Lookup global prop to store in the same order
  563. Map<StringName, VariantContainer>::Element *global_prop = props.find(E->key());
  564. _VCSort vc;
  565. vc.name = E->key();
  566. vc.order = global_prop ? global_prop->get().order : 0xFFFFFFF;
  567. vc.type = E->get().get_type();
  568. vc.flags = PROPERTY_USAGE_STORAGE;
  569. vclist.insert(vc);
  570. }
  571. Map<String, List<String> > props;
  572. for (Set<_VCSort>::Element *E = vclist.front(); E; E = E->next()) {
  573. String category = E->get().name;
  574. String name = E->get().name;
  575. int div = category.find("/");
  576. if (div < 0)
  577. category = "";
  578. else {
  579. category = category.substr(0, div);
  580. name = name.substr(div + 1, name.size());
  581. }
  582. props[category].push_back(name);
  583. }
  584. String custom_features;
  585. for (int i = 0; i < p_custom_features.size(); i++) {
  586. if (i > 0)
  587. custom_features += ",";
  588. String f = p_custom_features[i].strip_edges().replace("\"", "");
  589. custom_features += f;
  590. }
  591. if (p_path.ends_with(".godot"))
  592. return _save_settings_text(p_path, props, p_custom, custom_features);
  593. else if (p_path.ends_with(".binary"))
  594. return _save_settings_binary(p_path, props, p_custom, custom_features);
  595. else {
  596. ERR_EXPLAIN("Unknown config file format: " + p_path);
  597. ERR_FAIL_V(ERR_FILE_UNRECOGNIZED);
  598. }
  599. return OK;
  600. }
  601. Variant _GLOBAL_DEF(const String &p_var, const Variant &p_default) {
  602. Variant ret;
  603. if (ProjectSettings::get_singleton()->has_setting(p_var)) {
  604. ret = ProjectSettings::get_singleton()->get(p_var);
  605. } else {
  606. ProjectSettings::get_singleton()->set(p_var, p_default);
  607. ret = p_default;
  608. }
  609. ProjectSettings::get_singleton()->set_initial_value(p_var, p_default);
  610. ProjectSettings::get_singleton()->set_builtin_order(p_var);
  611. return ret;
  612. }
  613. Vector<String> ProjectSettings::get_optimizer_presets() const {
  614. List<PropertyInfo> pi;
  615. ProjectSettings::get_singleton()->get_property_list(&pi);
  616. Vector<String> names;
  617. for (List<PropertyInfo>::Element *E = pi.front(); E; E = E->next()) {
  618. if (!E->get().name.begins_with("optimizer_presets/"))
  619. continue;
  620. names.push_back(E->get().name.get_slicec('/', 1));
  621. }
  622. names.sort();
  623. return names;
  624. }
  625. void ProjectSettings::_add_property_info_bind(const Dictionary &p_info) {
  626. ERR_FAIL_COND(!p_info.has("name"));
  627. ERR_FAIL_COND(!p_info.has("type"));
  628. PropertyInfo pinfo;
  629. pinfo.name = p_info["name"];
  630. ERR_FAIL_COND(!props.has(pinfo.name));
  631. pinfo.type = Variant::Type(p_info["type"].operator int());
  632. ERR_FAIL_INDEX(pinfo.type, Variant::VARIANT_MAX);
  633. if (p_info.has("hint"))
  634. pinfo.hint = PropertyHint(p_info["hint"].operator int());
  635. if (p_info.has("hint_string"))
  636. pinfo.hint_string = p_info["hint_string"];
  637. set_custom_property_info(pinfo.name, pinfo);
  638. }
  639. void ProjectSettings::set_custom_property_info(const String &p_prop, const PropertyInfo &p_info) {
  640. ERR_FAIL_COND(!props.has(p_prop));
  641. custom_prop_info[p_prop] = p_info;
  642. custom_prop_info[p_prop].name = p_prop;
  643. }
  644. void ProjectSettings::set_disable_feature_overrides(bool p_disable) {
  645. disable_feature_overrides = p_disable;
  646. }
  647. bool ProjectSettings::is_using_datapack() const {
  648. return using_datapack;
  649. }
  650. bool ProjectSettings::property_can_revert(const String &p_name) {
  651. if (!props.has(p_name))
  652. return false;
  653. return props[p_name].initial != props[p_name].variant;
  654. }
  655. Variant ProjectSettings::property_get_revert(const String &p_name) {
  656. if (!props.has(p_name))
  657. return Variant();
  658. return props[p_name].initial;
  659. }
  660. void ProjectSettings::set_setting(const String &p_setting, const Variant &p_value) {
  661. set(p_setting, p_value);
  662. }
  663. Variant ProjectSettings::get_setting(const String &p_setting) const {
  664. return get(p_setting);
  665. }
  666. void ProjectSettings::_bind_methods() {
  667. ClassDB::bind_method(D_METHOD("has_setting", "name"), &ProjectSettings::has_setting);
  668. ClassDB::bind_method(D_METHOD("set_setting", "name", "value"), &ProjectSettings::set_setting);
  669. ClassDB::bind_method(D_METHOD("get_setting", "name"), &ProjectSettings::get_setting);
  670. ClassDB::bind_method(D_METHOD("set_order", "name", "position"), &ProjectSettings::set_order);
  671. ClassDB::bind_method(D_METHOD("get_order", "name"), &ProjectSettings::get_order);
  672. ClassDB::bind_method(D_METHOD("set_initial_value", "name", "value"), &ProjectSettings::set_initial_value);
  673. ClassDB::bind_method(D_METHOD("add_property_info", "hint"), &ProjectSettings::_add_property_info_bind);
  674. ClassDB::bind_method(D_METHOD("clear", "name"), &ProjectSettings::clear);
  675. ClassDB::bind_method(D_METHOD("localize_path", "path"), &ProjectSettings::localize_path);
  676. ClassDB::bind_method(D_METHOD("globalize_path", "path"), &ProjectSettings::globalize_path);
  677. ClassDB::bind_method(D_METHOD("save"), &ProjectSettings::save);
  678. ClassDB::bind_method(D_METHOD("load_resource_pack", "pack"), &ProjectSettings::_load_resource_pack);
  679. ClassDB::bind_method(D_METHOD("property_can_revert", "name"), &ProjectSettings::property_can_revert);
  680. ClassDB::bind_method(D_METHOD("property_get_revert", "name"), &ProjectSettings::property_get_revert);
  681. ClassDB::bind_method(D_METHOD("save_custom", "file"), &ProjectSettings::_save_custom_bnd);
  682. }
  683. ProjectSettings::ProjectSettings() {
  684. singleton = this;
  685. last_order = NO_BUILTIN_ORDER_BASE;
  686. last_builtin_order = 0;
  687. disable_feature_overrides = false;
  688. registering_order = true;
  689. Array va;
  690. Ref<InputEventKey> key;
  691. Ref<InputEventJoypadButton> joyb;
  692. GLOBAL_DEF("application/config/name", "");
  693. GLOBAL_DEF("application/run/main_scene", "");
  694. custom_prop_info["application/run/main_scene"] = PropertyInfo(Variant::STRING, "application/run/main_scene", PROPERTY_HINT_FILE, "tscn,scn,res");
  695. GLOBAL_DEF("application/run/disable_stdout", false);
  696. GLOBAL_DEF("application/run/disable_stderr", false);
  697. GLOBAL_DEF("application/config/use_custom_user_dir", false);
  698. GLOBAL_DEF("application/config/custom_user_dir_name", "");
  699. key.instance();
  700. key->set_scancode(KEY_ENTER);
  701. va.push_back(key);
  702. key.instance();
  703. key->set_scancode(KEY_KP_ENTER);
  704. va.push_back(key);
  705. key.instance();
  706. key->set_scancode(KEY_SPACE);
  707. va.push_back(key);
  708. joyb.instance();
  709. joyb->set_button_index(JOY_BUTTON_0);
  710. va.push_back(joyb);
  711. GLOBAL_DEF("input/ui_accept", va);
  712. input_presets.push_back("input/ui_accept");
  713. va = Array();
  714. key.instance();
  715. key->set_scancode(KEY_SPACE);
  716. va.push_back(key);
  717. joyb.instance();
  718. joyb->set_button_index(JOY_BUTTON_3);
  719. va.push_back(joyb);
  720. GLOBAL_DEF("input/ui_select", va);
  721. input_presets.push_back("input/ui_select");
  722. va = Array();
  723. key.instance();
  724. key->set_scancode(KEY_ESCAPE);
  725. va.push_back(key);
  726. joyb.instance();
  727. joyb->set_button_index(JOY_BUTTON_1);
  728. va.push_back(joyb);
  729. GLOBAL_DEF("input/ui_cancel", va);
  730. input_presets.push_back("input/ui_cancel");
  731. va = Array();
  732. key.instance();
  733. key->set_scancode(KEY_TAB);
  734. va.push_back(key);
  735. GLOBAL_DEF("input/ui_focus_next", va);
  736. input_presets.push_back("input/ui_focus_next");
  737. va = Array();
  738. key.instance();
  739. key->set_scancode(KEY_TAB);
  740. key->set_shift(true);
  741. va.push_back(key);
  742. GLOBAL_DEF("input/ui_focus_prev", va);
  743. input_presets.push_back("input/ui_focus_prev");
  744. va = Array();
  745. key.instance();
  746. key->set_scancode(KEY_LEFT);
  747. va.push_back(key);
  748. joyb.instance();
  749. joyb->set_button_index(JOY_DPAD_LEFT);
  750. va.push_back(joyb);
  751. GLOBAL_DEF("input/ui_left", va);
  752. input_presets.push_back("input/ui_left");
  753. va = Array();
  754. key.instance();
  755. key->set_scancode(KEY_RIGHT);
  756. va.push_back(key);
  757. joyb.instance();
  758. joyb->set_button_index(JOY_DPAD_RIGHT);
  759. va.push_back(joyb);
  760. GLOBAL_DEF("input/ui_right", va);
  761. input_presets.push_back("input/ui_right");
  762. va = Array();
  763. key.instance();
  764. key->set_scancode(KEY_UP);
  765. va.push_back(key);
  766. joyb.instance();
  767. joyb->set_button_index(JOY_DPAD_UP);
  768. va.push_back(joyb);
  769. GLOBAL_DEF("input/ui_up", va);
  770. input_presets.push_back("input/ui_up");
  771. va = Array();
  772. key.instance();
  773. key->set_scancode(KEY_DOWN);
  774. va.push_back(key);
  775. joyb.instance();
  776. joyb->set_button_index(JOY_DPAD_DOWN);
  777. va.push_back(joyb);
  778. GLOBAL_DEF("input/ui_down", va);
  779. input_presets.push_back("input/ui_down");
  780. va = Array();
  781. key.instance();
  782. key->set_scancode(KEY_PAGEUP);
  783. va.push_back(key);
  784. GLOBAL_DEF("input/ui_page_up", va);
  785. input_presets.push_back("input/ui_page_up");
  786. va = Array();
  787. key.instance();
  788. key->set_scancode(KEY_PAGEDOWN);
  789. va.push_back(key);
  790. GLOBAL_DEF("input/ui_page_down", va);
  791. input_presets.push_back("input/ui_page_down");
  792. //GLOBAL_DEF("display/window/handheld/orientation", "landscape");
  793. 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");
  794. custom_prop_info["rendering/threads/thread_model"] = PropertyInfo(Variant::INT, "rendering/threads/thread_model", PROPERTY_HINT_ENUM, "Single-Unsafe,Single-Safe,Multi-Threaded");
  795. custom_prop_info["physics/2d/thread_model"] = PropertyInfo(Variant::INT, "physics/2d/thread_model", PROPERTY_HINT_ENUM, "Single-Unsafe,Single-Safe,Multi-Threaded");
  796. 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");
  797. GLOBAL_DEF("rendering/quality/intended_usage/framebuffer_mode", 2);
  798. GLOBAL_DEF("debug/settings/profiler/max_functions", 16384);
  799. //assigning here, because using GLOBAL_GET on every block for compressing can be slow
  800. Compression::zstd_long_distance_matching = GLOBAL_DEF("compression/formats/zstd/long_distance_matching", false);
  801. custom_prop_info["compression/formats/zstd/long_distance_matching"] = PropertyInfo(Variant::BOOL, "compression/formats/zstd/long_distance_matching");
  802. Compression::zstd_level = GLOBAL_DEF("compression/formats/zstd/compression_level", 3);
  803. custom_prop_info["compression/formats/zstd/compression_level"] = PropertyInfo(Variant::INT, "compression/formats/zstd/compression_level", PROPERTY_HINT_RANGE, "1,22,1");
  804. Compression::zstd_window_log_size = GLOBAL_DEF("compression/formats/zstd/window_log_size", 27);
  805. custom_prop_info["compression/formats/zstd/window_log_size"] = PropertyInfo(Variant::INT, "compression/formats/zstd/window_log_size", PROPERTY_HINT_RANGE, "10,30,1");
  806. Compression::zlib_level = GLOBAL_DEF("compression/formats/zlib/compression_level", Z_DEFAULT_COMPRESSION);
  807. custom_prop_info["compression/formats/zlib/compression_level"] = PropertyInfo(Variant::INT, "compression/formats/zlib/compression_level", PROPERTY_HINT_RANGE, "-1,9,1");
  808. Compression::gzip_level = GLOBAL_DEF("compression/formats/gzip/compression_level", Z_DEFAULT_COMPRESSION);
  809. custom_prop_info["compression/formats/gzip/compression_level"] = PropertyInfo(Variant::INT, "compression/formats/gzip/compression_level", PROPERTY_HINT_RANGE, "-1,9,1");
  810. using_datapack = false;
  811. }
  812. ProjectSettings::~ProjectSettings() {
  813. singleton = NULL;
  814. }