project_settings.cpp 32 KB

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