project_settings.cpp 32 KB

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