project_settings.cpp 30 KB

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