settings.cpp 22 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056
  1. /*
  2. Minetest
  3. Copyright (C) 2010-2013 celeron55, Perttu Ahola <celeron55@gmail.com>
  4. This program is free software; you can redistribute it and/or modify
  5. it under the terms of the GNU Lesser General Public License as published by
  6. the Free Software Foundation; either version 2.1 of the License, or
  7. (at your option) any later version.
  8. This program is distributed in the hope that it will be useful,
  9. but WITHOUT ANY WARRANTY; without even the implied warranty of
  10. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  11. GNU Lesser General Public License for more details.
  12. You should have received a copy of the GNU Lesser General Public License along
  13. with this program; if not, write to the Free Software Foundation, Inc.,
  14. 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
  15. */
  16. #include "settings.h"
  17. #include "irrlichttypes_bloated.h"
  18. #include "exceptions.h"
  19. #include "threading/mutex_auto_lock.h"
  20. #include "util/strfnd.h"
  21. #include <iostream>
  22. #include <fstream>
  23. #include <sstream>
  24. #include "debug.h"
  25. #include "log.h"
  26. #include "util/serialize.h"
  27. #include "filesys.h"
  28. #include "noise.h"
  29. #include <cctype>
  30. #include <algorithm>
  31. static Settings main_settings;
  32. Settings *g_settings = &main_settings;
  33. std::string g_settings_path;
  34. Settings::~Settings()
  35. {
  36. clear();
  37. }
  38. Settings & Settings::operator += (const Settings &other)
  39. {
  40. update(other);
  41. return *this;
  42. }
  43. Settings & Settings::operator = (const Settings &other)
  44. {
  45. if (&other == this)
  46. return *this;
  47. MutexAutoLock lock(m_mutex);
  48. MutexAutoLock lock2(other.m_mutex);
  49. clearNoLock();
  50. updateNoLock(other);
  51. return *this;
  52. }
  53. bool Settings::checkNameValid(const std::string &name)
  54. {
  55. bool valid = name.find_first_of("=\"{}#") == std::string::npos;
  56. if (valid)
  57. valid = std::find_if(name.begin(), name.end(), ::isspace) == name.end();
  58. if (!valid) {
  59. errorstream << "Invalid setting name \"" << name << "\""
  60. << std::endl;
  61. return false;
  62. }
  63. return true;
  64. }
  65. bool Settings::checkValueValid(const std::string &value)
  66. {
  67. if (value.substr(0, 3) == "\"\"\"" ||
  68. value.find("\n\"\"\"") != std::string::npos) {
  69. errorstream << "Invalid character sequence '\"\"\"' found in"
  70. " setting value!" << std::endl;
  71. return false;
  72. }
  73. return true;
  74. }
  75. std::string Settings::getMultiline(std::istream &is, size_t *num_lines)
  76. {
  77. size_t lines = 1;
  78. std::string value;
  79. std::string line;
  80. while (is.good()) {
  81. lines++;
  82. std::getline(is, line);
  83. if (line == "\"\"\"")
  84. break;
  85. value += line;
  86. value.push_back('\n');
  87. }
  88. size_t len = value.size();
  89. if (len)
  90. value.erase(len - 1);
  91. if (num_lines)
  92. *num_lines = lines;
  93. return value;
  94. }
  95. bool Settings::readConfigFile(const char *filename)
  96. {
  97. std::ifstream is(filename);
  98. if (!is.good())
  99. return false;
  100. return parseConfigLines(is, "");
  101. }
  102. bool Settings::parseConfigLines(std::istream &is, const std::string &end)
  103. {
  104. MutexAutoLock lock(m_mutex);
  105. std::string line, name, value;
  106. while (is.good()) {
  107. std::getline(is, line);
  108. SettingsParseEvent event = parseConfigObject(line, end, name, value);
  109. switch (event) {
  110. case SPE_NONE:
  111. case SPE_INVALID:
  112. case SPE_COMMENT:
  113. break;
  114. case SPE_KVPAIR:
  115. m_settings[name] = SettingsEntry(value);
  116. break;
  117. case SPE_END:
  118. return true;
  119. case SPE_GROUP: {
  120. Settings *group = new Settings;
  121. if (!group->parseConfigLines(is, "}")) {
  122. delete group;
  123. return false;
  124. }
  125. m_settings[name] = SettingsEntry(group);
  126. break;
  127. }
  128. case SPE_MULTILINE:
  129. m_settings[name] = SettingsEntry(getMultiline(is));
  130. break;
  131. }
  132. }
  133. return end.empty();
  134. }
  135. void Settings::writeLines(std::ostream &os, u32 tab_depth) const
  136. {
  137. MutexAutoLock lock(m_mutex);
  138. for (const auto &setting_it : m_settings)
  139. printEntry(os, setting_it.first, setting_it.second, tab_depth);
  140. }
  141. void Settings::printEntry(std::ostream &os, const std::string &name,
  142. const SettingsEntry &entry, u32 tab_depth)
  143. {
  144. for (u32 i = 0; i != tab_depth; i++)
  145. os << "\t";
  146. if (entry.is_group) {
  147. os << name << " = {\n";
  148. entry.group->writeLines(os, tab_depth + 1);
  149. for (u32 i = 0; i != tab_depth; i++)
  150. os << "\t";
  151. os << "}\n";
  152. } else {
  153. os << name << " = ";
  154. if (entry.value.find('\n') != std::string::npos)
  155. os << "\"\"\"\n" << entry.value << "\n\"\"\"\n";
  156. else
  157. os << entry.value << "\n";
  158. }
  159. }
  160. bool Settings::updateConfigObject(std::istream &is, std::ostream &os,
  161. const std::string &end, u32 tab_depth)
  162. {
  163. SettingEntries::const_iterator it;
  164. std::set<std::string> present_entries;
  165. std::string line, name, value;
  166. bool was_modified = false;
  167. bool end_found = false;
  168. // Add any settings that exist in the config file with the current value
  169. // in the object if existing
  170. while (is.good() && !end_found) {
  171. std::getline(is, line);
  172. SettingsParseEvent event = parseConfigObject(line, end, name, value);
  173. switch (event) {
  174. case SPE_END:
  175. os << line << (is.eof() ? "" : "\n");
  176. end_found = true;
  177. break;
  178. case SPE_MULTILINE:
  179. value = getMultiline(is);
  180. /* FALLTHROUGH */
  181. case SPE_KVPAIR:
  182. it = m_settings.find(name);
  183. if (it != m_settings.end() &&
  184. (it->second.is_group || it->second.value != value)) {
  185. printEntry(os, name, it->second, tab_depth);
  186. was_modified = true;
  187. } else if (it == m_settings.end()) {
  188. // Remove by skipping
  189. was_modified = true;
  190. break;
  191. } else {
  192. os << line << "\n";
  193. if (event == SPE_MULTILINE)
  194. os << value << "\n\"\"\"\n";
  195. }
  196. present_entries.insert(name);
  197. break;
  198. case SPE_GROUP:
  199. it = m_settings.find(name);
  200. if (it != m_settings.end() && it->second.is_group) {
  201. os << line << "\n";
  202. sanity_check(it->second.group != NULL);
  203. was_modified |= it->second.group->updateConfigObject(is, os,
  204. "}", tab_depth + 1);
  205. } else if (it == m_settings.end()) {
  206. // Remove by skipping
  207. was_modified = true;
  208. Settings removed_group; // Move 'is' to group end
  209. std::stringstream ss;
  210. removed_group.updateConfigObject(is, ss, "}", tab_depth + 1);
  211. break;
  212. } else {
  213. printEntry(os, name, it->second, tab_depth);
  214. was_modified = true;
  215. }
  216. present_entries.insert(name);
  217. break;
  218. default:
  219. os << line << (is.eof() ? "" : "\n");
  220. break;
  221. }
  222. }
  223. // Add any settings in the object that don't exist in the config file yet
  224. for (it = m_settings.begin(); it != m_settings.end(); ++it) {
  225. if (present_entries.find(it->first) != present_entries.end())
  226. continue;
  227. printEntry(os, it->first, it->second, tab_depth);
  228. was_modified = true;
  229. }
  230. return was_modified;
  231. }
  232. bool Settings::updateConfigFile(const char *filename)
  233. {
  234. MutexAutoLock lock(m_mutex);
  235. std::ifstream is(filename);
  236. std::ostringstream os(std::ios_base::binary);
  237. bool was_modified = updateConfigObject(is, os, "");
  238. is.close();
  239. if (!was_modified)
  240. return true;
  241. if (!fs::safeWriteToFile(filename, os.str())) {
  242. errorstream << "Error writing configuration file: \""
  243. << filename << "\"" << std::endl;
  244. return false;
  245. }
  246. return true;
  247. }
  248. bool Settings::parseCommandLine(int argc, char *argv[],
  249. std::map<std::string, ValueSpec> &allowed_options)
  250. {
  251. int nonopt_index = 0;
  252. for (int i = 1; i < argc; i++) {
  253. std::string arg_name = argv[i];
  254. if (arg_name.substr(0, 2) != "--") {
  255. // If option doesn't start with -, read it in as nonoptX
  256. if (arg_name[0] != '-'){
  257. std::string name = "nonopt";
  258. name += itos(nonopt_index);
  259. set(name, arg_name);
  260. nonopt_index++;
  261. continue;
  262. }
  263. errorstream << "Invalid command-line parameter \""
  264. << arg_name << "\": --<option> expected." << std::endl;
  265. return false;
  266. }
  267. std::string name = arg_name.substr(2);
  268. std::map<std::string, ValueSpec>::iterator n;
  269. n = allowed_options.find(name);
  270. if (n == allowed_options.end()) {
  271. errorstream << "Unknown command-line parameter \""
  272. << arg_name << "\"" << std::endl;
  273. return false;
  274. }
  275. ValueType type = n->second.type;
  276. std::string value;
  277. if (type == VALUETYPE_FLAG) {
  278. value = "true";
  279. } else {
  280. if ((i + 1) >= argc) {
  281. errorstream << "Invalid command-line parameter \""
  282. << name << "\": missing value" << std::endl;
  283. return false;
  284. }
  285. value = argv[++i];
  286. }
  287. set(name, value);
  288. }
  289. return true;
  290. }
  291. /***********
  292. * Getters *
  293. ***********/
  294. const SettingsEntry &Settings::getEntry(const std::string &name) const
  295. {
  296. MutexAutoLock lock(m_mutex);
  297. SettingEntries::const_iterator n;
  298. if ((n = m_settings.find(name)) == m_settings.end()) {
  299. if ((n = m_defaults.find(name)) == m_defaults.end())
  300. throw SettingNotFoundException("Setting [" + name + "] not found.");
  301. }
  302. return n->second;
  303. }
  304. const SettingsEntry &Settings::getEntryDefault(const std::string &name) const
  305. {
  306. MutexAutoLock lock(m_mutex);
  307. SettingEntries::const_iterator n;
  308. if ((n = m_defaults.find(name)) == m_defaults.end()) {
  309. throw SettingNotFoundException("Setting [" + name + "] not found.");
  310. }
  311. return n->second;
  312. }
  313. Settings *Settings::getGroup(const std::string &name) const
  314. {
  315. const SettingsEntry &entry = getEntry(name);
  316. if (!entry.is_group)
  317. throw SettingNotFoundException("Setting [" + name + "] is not a group.");
  318. return entry.group;
  319. }
  320. const std::string &Settings::get(const std::string &name) const
  321. {
  322. const SettingsEntry &entry = getEntry(name);
  323. if (entry.is_group)
  324. throw SettingNotFoundException("Setting [" + name + "] is a group.");
  325. return entry.value;
  326. }
  327. const std::string &Settings::getDefault(const std::string &name) const
  328. {
  329. const SettingsEntry &entry = getEntryDefault(name);
  330. if (entry.is_group)
  331. throw SettingNotFoundException("Setting [" + name + "] is a group.");
  332. return entry.value;
  333. }
  334. bool Settings::getBool(const std::string &name) const
  335. {
  336. return is_yes(get(name));
  337. }
  338. u16 Settings::getU16(const std::string &name) const
  339. {
  340. return stoi(get(name), 0, 65535);
  341. }
  342. s16 Settings::getS16(const std::string &name) const
  343. {
  344. return stoi(get(name), -32768, 32767);
  345. }
  346. u32 Settings::getU32(const std::string &name) const
  347. {
  348. return (u32) stoi(get(name));
  349. }
  350. s32 Settings::getS32(const std::string &name) const
  351. {
  352. return stoi(get(name));
  353. }
  354. float Settings::getFloat(const std::string &name) const
  355. {
  356. return stof(get(name));
  357. }
  358. u64 Settings::getU64(const std::string &name) const
  359. {
  360. u64 value = 0;
  361. std::string s = get(name);
  362. std::istringstream ss(s);
  363. ss >> value;
  364. return value;
  365. }
  366. v2f Settings::getV2F(const std::string &name) const
  367. {
  368. v2f value;
  369. Strfnd f(get(name));
  370. f.next("(");
  371. value.X = stof(f.next(","));
  372. value.Y = stof(f.next(")"));
  373. return value;
  374. }
  375. v3f Settings::getV3F(const std::string &name) const
  376. {
  377. v3f value;
  378. Strfnd f(get(name));
  379. f.next("(");
  380. value.X = stof(f.next(","));
  381. value.Y = stof(f.next(","));
  382. value.Z = stof(f.next(")"));
  383. return value;
  384. }
  385. u32 Settings::getFlagStr(const std::string &name, const FlagDesc *flagdesc,
  386. u32 *flagmask) const
  387. {
  388. std::string val = get(name);
  389. return std::isdigit(val[0])
  390. ? stoi(val)
  391. : readFlagString(val, flagdesc, flagmask);
  392. }
  393. // N.B. if getStruct() is used to read a non-POD aggregate type,
  394. // the behavior is undefined.
  395. bool Settings::getStruct(const std::string &name, const std::string &format,
  396. void *out, size_t olen) const
  397. {
  398. std::string valstr;
  399. try {
  400. valstr = get(name);
  401. } catch (SettingNotFoundException &e) {
  402. return false;
  403. }
  404. if (!deSerializeStringToStruct(valstr, format, out, olen))
  405. return false;
  406. return true;
  407. }
  408. bool Settings::getNoiseParams(const std::string &name, NoiseParams &np) const
  409. {
  410. return getNoiseParamsFromGroup(name, np) || getNoiseParamsFromValue(name, np);
  411. }
  412. bool Settings::getNoiseParamsFromValue(const std::string &name,
  413. NoiseParams &np) const
  414. {
  415. std::string value;
  416. if (!getNoEx(name, value))
  417. return false;
  418. Strfnd f(value);
  419. np.offset = stof(f.next(","));
  420. np.scale = stof(f.next(","));
  421. f.next("(");
  422. np.spread.X = stof(f.next(","));
  423. np.spread.Y = stof(f.next(","));
  424. np.spread.Z = stof(f.next(")"));
  425. f.next(",");
  426. np.seed = stoi(f.next(","));
  427. np.octaves = stoi(f.next(","));
  428. np.persist = stof(f.next(","));
  429. std::string optional_params = f.next("");
  430. if (!optional_params.empty())
  431. np.lacunarity = stof(optional_params);
  432. return true;
  433. }
  434. bool Settings::getNoiseParamsFromGroup(const std::string &name,
  435. NoiseParams &np) const
  436. {
  437. Settings *group = NULL;
  438. if (!getGroupNoEx(name, group))
  439. return false;
  440. group->getFloatNoEx("offset", np.offset);
  441. group->getFloatNoEx("scale", np.scale);
  442. group->getV3FNoEx("spread", np.spread);
  443. group->getS32NoEx("seed", np.seed);
  444. group->getU16NoEx("octaves", np.octaves);
  445. group->getFloatNoEx("persistence", np.persist);
  446. group->getFloatNoEx("lacunarity", np.lacunarity);
  447. np.flags = 0;
  448. if (!group->getFlagStrNoEx("flags", np.flags, flagdesc_noiseparams))
  449. np.flags = NOISE_FLAG_DEFAULTS;
  450. return true;
  451. }
  452. bool Settings::exists(const std::string &name) const
  453. {
  454. MutexAutoLock lock(m_mutex);
  455. return (m_settings.find(name) != m_settings.end() ||
  456. m_defaults.find(name) != m_defaults.end());
  457. }
  458. std::vector<std::string> Settings::getNames() const
  459. {
  460. std::vector<std::string> names;
  461. for (const auto &settings_it : m_settings) {
  462. names.push_back(settings_it.first);
  463. }
  464. return names;
  465. }
  466. /***************************************
  467. * Getters that don't throw exceptions *
  468. ***************************************/
  469. bool Settings::getEntryNoEx(const std::string &name, SettingsEntry &val) const
  470. {
  471. try {
  472. val = getEntry(name);
  473. return true;
  474. } catch (SettingNotFoundException &e) {
  475. return false;
  476. }
  477. }
  478. bool Settings::getEntryDefaultNoEx(const std::string &name, SettingsEntry &val) const
  479. {
  480. try {
  481. val = getEntryDefault(name);
  482. return true;
  483. } catch (SettingNotFoundException &e) {
  484. return false;
  485. }
  486. }
  487. bool Settings::getGroupNoEx(const std::string &name, Settings *&val) const
  488. {
  489. try {
  490. val = getGroup(name);
  491. return true;
  492. } catch (SettingNotFoundException &e) {
  493. return false;
  494. }
  495. }
  496. bool Settings::getNoEx(const std::string &name, std::string &val) const
  497. {
  498. try {
  499. val = get(name);
  500. return true;
  501. } catch (SettingNotFoundException &e) {
  502. return false;
  503. }
  504. }
  505. bool Settings::getDefaultNoEx(const std::string &name, std::string &val) const
  506. {
  507. try {
  508. val = getDefault(name);
  509. return true;
  510. } catch (SettingNotFoundException &e) {
  511. return false;
  512. }
  513. }
  514. bool Settings::getFlag(const std::string &name) const
  515. {
  516. try {
  517. return getBool(name);
  518. } catch(SettingNotFoundException &e) {
  519. return false;
  520. }
  521. }
  522. bool Settings::getFloatNoEx(const std::string &name, float &val) const
  523. {
  524. try {
  525. val = getFloat(name);
  526. return true;
  527. } catch (SettingNotFoundException &e) {
  528. return false;
  529. }
  530. }
  531. bool Settings::getU16NoEx(const std::string &name, u16 &val) const
  532. {
  533. try {
  534. val = getU16(name);
  535. return true;
  536. } catch (SettingNotFoundException &e) {
  537. return false;
  538. }
  539. }
  540. bool Settings::getS16NoEx(const std::string &name, s16 &val) const
  541. {
  542. try {
  543. val = getS16(name);
  544. return true;
  545. } catch (SettingNotFoundException &e) {
  546. return false;
  547. }
  548. }
  549. bool Settings::getS32NoEx(const std::string &name, s32 &val) const
  550. {
  551. try {
  552. val = getS32(name);
  553. return true;
  554. } catch (SettingNotFoundException &e) {
  555. return false;
  556. }
  557. }
  558. bool Settings::getU64NoEx(const std::string &name, u64 &val) const
  559. {
  560. try {
  561. val = getU64(name);
  562. return true;
  563. } catch (SettingNotFoundException &e) {
  564. return false;
  565. }
  566. }
  567. bool Settings::getV2FNoEx(const std::string &name, v2f &val) const
  568. {
  569. try {
  570. val = getV2F(name);
  571. return true;
  572. } catch (SettingNotFoundException &e) {
  573. return false;
  574. }
  575. }
  576. bool Settings::getV3FNoEx(const std::string &name, v3f &val) const
  577. {
  578. try {
  579. val = getV3F(name);
  580. return true;
  581. } catch (SettingNotFoundException &e) {
  582. return false;
  583. }
  584. }
  585. // N.B. getFlagStrNoEx() does not set val, but merely modifies it. Thus,
  586. // val must be initialized before using getFlagStrNoEx(). The intention of
  587. // this is to simplify modifying a flags field from a default value.
  588. bool Settings::getFlagStrNoEx(const std::string &name, u32 &val,
  589. FlagDesc *flagdesc) const
  590. {
  591. try {
  592. u32 flags, flagmask;
  593. flags = getFlagStr(name, flagdesc, &flagmask);
  594. val &= ~flagmask;
  595. val |= flags;
  596. return true;
  597. } catch (SettingNotFoundException &e) {
  598. return false;
  599. }
  600. }
  601. /***********
  602. * Setters *
  603. ***********/
  604. bool Settings::setEntry(const std::string &name, const void *data,
  605. bool set_group, bool set_default)
  606. {
  607. Settings *old_group = NULL;
  608. if (!checkNameValid(name))
  609. return false;
  610. if (!set_group && !checkValueValid(*(const std::string *)data))
  611. return false;
  612. {
  613. MutexAutoLock lock(m_mutex);
  614. SettingsEntry &entry = set_default ? m_defaults[name] : m_settings[name];
  615. old_group = entry.group;
  616. entry.value = set_group ? "" : *(const std::string *)data;
  617. entry.group = set_group ? *(Settings **)data : NULL;
  618. entry.is_group = set_group;
  619. }
  620. delete old_group;
  621. return true;
  622. }
  623. bool Settings::set(const std::string &name, const std::string &value)
  624. {
  625. if (!setEntry(name, &value, false, false))
  626. return false;
  627. doCallbacks(name);
  628. return true;
  629. }
  630. bool Settings::setDefault(const std::string &name, const std::string &value)
  631. {
  632. return setEntry(name, &value, false, true);
  633. }
  634. bool Settings::setGroup(const std::string &name, Settings *group)
  635. {
  636. return setEntry(name, &group, true, false);
  637. }
  638. bool Settings::setGroupDefault(const std::string &name, Settings *group)
  639. {
  640. return setEntry(name, &group, true, true);
  641. }
  642. bool Settings::setBool(const std::string &name, bool value)
  643. {
  644. return set(name, value ? "true" : "false");
  645. }
  646. bool Settings::setS16(const std::string &name, s16 value)
  647. {
  648. return set(name, itos(value));
  649. }
  650. bool Settings::setU16(const std::string &name, u16 value)
  651. {
  652. return set(name, itos(value));
  653. }
  654. bool Settings::setS32(const std::string &name, s32 value)
  655. {
  656. return set(name, itos(value));
  657. }
  658. bool Settings::setU64(const std::string &name, u64 value)
  659. {
  660. std::ostringstream os;
  661. os << value;
  662. return set(name, os.str());
  663. }
  664. bool Settings::setFloat(const std::string &name, float value)
  665. {
  666. return set(name, ftos(value));
  667. }
  668. bool Settings::setV2F(const std::string &name, v2f value)
  669. {
  670. std::ostringstream os;
  671. os << "(" << value.X << "," << value.Y << ")";
  672. return set(name, os.str());
  673. }
  674. bool Settings::setV3F(const std::string &name, v3f value)
  675. {
  676. std::ostringstream os;
  677. os << "(" << value.X << "," << value.Y << "," << value.Z << ")";
  678. return set(name, os.str());
  679. }
  680. bool Settings::setFlagStr(const std::string &name, u32 flags,
  681. const FlagDesc *flagdesc, u32 flagmask)
  682. {
  683. return set(name, writeFlagString(flags, flagdesc, flagmask));
  684. }
  685. bool Settings::setStruct(const std::string &name, const std::string &format,
  686. void *value)
  687. {
  688. std::string structstr;
  689. if (!serializeStructToString(&structstr, format, value))
  690. return false;
  691. return set(name, structstr);
  692. }
  693. bool Settings::setNoiseParams(const std::string &name,
  694. const NoiseParams &np, bool set_default)
  695. {
  696. Settings *group = new Settings;
  697. group->setFloat("offset", np.offset);
  698. group->setFloat("scale", np.scale);
  699. group->setV3F("spread", np.spread);
  700. group->setS32("seed", np.seed);
  701. group->setU16("octaves", np.octaves);
  702. group->setFloat("persistence", np.persist);
  703. group->setFloat("lacunarity", np.lacunarity);
  704. group->setFlagStr("flags", np.flags, flagdesc_noiseparams, np.flags);
  705. return setEntry(name, &group, true, set_default);
  706. }
  707. bool Settings::remove(const std::string &name)
  708. {
  709. // Lock as short as possible, unlock before doCallbacks()
  710. m_mutex.lock();
  711. SettingEntries::iterator it = m_settings.find(name);
  712. if (it != m_settings.end()) {
  713. delete it->second.group;
  714. m_settings.erase(it);
  715. m_mutex.unlock();
  716. doCallbacks(name);
  717. return true;
  718. }
  719. m_mutex.unlock();
  720. return false;
  721. }
  722. void Settings::clear()
  723. {
  724. MutexAutoLock lock(m_mutex);
  725. clearNoLock();
  726. }
  727. void Settings::clearDefaults()
  728. {
  729. MutexAutoLock lock(m_mutex);
  730. clearDefaultsNoLock();
  731. }
  732. void Settings::updateValue(const Settings &other, const std::string &name)
  733. {
  734. if (&other == this)
  735. return;
  736. MutexAutoLock lock(m_mutex);
  737. try {
  738. m_settings[name] = other.get(name);
  739. } catch (SettingNotFoundException &e) {
  740. }
  741. }
  742. void Settings::update(const Settings &other)
  743. {
  744. if (&other == this)
  745. return;
  746. MutexAutoLock lock(m_mutex);
  747. MutexAutoLock lock2(other.m_mutex);
  748. updateNoLock(other);
  749. }
  750. SettingsParseEvent Settings::parseConfigObject(const std::string &line,
  751. const std::string &end, std::string &name, std::string &value)
  752. {
  753. std::string trimmed_line = trim(line);
  754. if (trimmed_line.empty())
  755. return SPE_NONE;
  756. if (trimmed_line[0] == '#')
  757. return SPE_COMMENT;
  758. if (trimmed_line == end)
  759. return SPE_END;
  760. size_t pos = trimmed_line.find('=');
  761. if (pos == std::string::npos)
  762. return SPE_INVALID;
  763. name = trim(trimmed_line.substr(0, pos));
  764. value = trim(trimmed_line.substr(pos + 1));
  765. if (value == "{")
  766. return SPE_GROUP;
  767. if (value == "\"\"\"")
  768. return SPE_MULTILINE;
  769. return SPE_KVPAIR;
  770. }
  771. void Settings::updateNoLock(const Settings &other)
  772. {
  773. m_settings.insert(other.m_settings.begin(), other.m_settings.end());
  774. m_defaults.insert(other.m_defaults.begin(), other.m_defaults.end());
  775. }
  776. void Settings::clearNoLock()
  777. {
  778. for (SettingEntries::const_iterator it = m_settings.begin();
  779. it != m_settings.end(); ++it)
  780. delete it->second.group;
  781. m_settings.clear();
  782. clearDefaultsNoLock();
  783. }
  784. void Settings::clearDefaultsNoLock()
  785. {
  786. for (SettingEntries::const_iterator it = m_defaults.begin();
  787. it != m_defaults.end(); ++it)
  788. delete it->second.group;
  789. m_defaults.clear();
  790. }
  791. void Settings::registerChangedCallback(const std::string &name,
  792. SettingsChangedCallback cbf, void *userdata)
  793. {
  794. MutexAutoLock lock(m_callback_mutex);
  795. m_callbacks[name].emplace_back(cbf, userdata);
  796. }
  797. void Settings::deregisterChangedCallback(const std::string &name,
  798. SettingsChangedCallback cbf, void *userdata)
  799. {
  800. MutexAutoLock lock(m_callback_mutex);
  801. SettingsCallbackMap::iterator it_cbks = m_callbacks.find(name);
  802. if (it_cbks != m_callbacks.end()) {
  803. SettingsCallbackList &cbks = it_cbks->second;
  804. SettingsCallbackList::iterator position =
  805. std::find(cbks.begin(), cbks.end(), std::make_pair(cbf, userdata));
  806. if (position != cbks.end())
  807. cbks.erase(position);
  808. }
  809. }
  810. void Settings::doCallbacks(const std::string &name) const
  811. {
  812. MutexAutoLock lock(m_callback_mutex);
  813. SettingsCallbackMap::const_iterator it_cbks = m_callbacks.find(name);
  814. if (it_cbks != m_callbacks.end()) {
  815. SettingsCallbackList::const_iterator it;
  816. for (it = it_cbks->second.begin(); it != it_cbks->second.end(); ++it)
  817. (it->first)(name, it->second);
  818. }
  819. }