settings.cpp 21 KB

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