project_settings.cpp 29 KB

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