]> git.lizzy.rs Git - dragonfireclient.git/blob - src/settings.cpp
Add mod.conf to preview clientmod (#11020)
[dragonfireclient.git] / src / settings.cpp
1 /*
2 Minetest
3 Copyright (C) 2010-2013 celeron55, Perttu Ahola <celeron55@gmail.com>
4
5 This program is free software; you can redistribute it and/or modify
6 it under the terms of the GNU Lesser General Public License as published by
7 the Free Software Foundation; either version 2.1 of the License, or
8 (at your option) any later version.
9
10 This program is distributed in the hope that it will be useful,
11 but WITHOUT ANY WARRANTY; without even the implied warranty of
12 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
13 GNU Lesser General Public License for more details.
14
15 You should have received a copy of the GNU Lesser General Public License along
16 with this program; if not, write to the Free Software Foundation, Inc.,
17 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
18 */
19
20 #include "settings.h"
21 #include "irrlichttypes_bloated.h"
22 #include "exceptions.h"
23 #include "threading/mutex_auto_lock.h"
24 #include "util/strfnd.h"
25 #include <iostream>
26 #include <fstream>
27 #include <sstream>
28 #include "debug.h"
29 #include "log.h"
30 #include "util/serialize.h"
31 #include "filesys.h"
32 #include "noise.h"
33 #include <cctype>
34 #include <algorithm>
35
36 Settings *g_settings = nullptr; // Populated in main()
37 std::string g_settings_path;
38
39 Settings *Settings::s_layers[SL_TOTAL_COUNT] = {0}; // Zeroed by compiler
40 std::unordered_map<std::string, const FlagDesc *> Settings::s_flags;
41
42
43 Settings *Settings::createLayer(SettingsLayer sl, const std::string &end_tag)
44 {
45         if ((int)sl < 0 || sl >= SL_TOTAL_COUNT)
46                 throw new BaseException("Invalid settings layer");
47
48         Settings *&pos = s_layers[(size_t)sl];
49         if (pos)
50                 throw new BaseException("Setting layer " + std::to_string(sl) + " already exists");
51
52         pos = new Settings(end_tag);
53         pos->m_settingslayer = sl;
54
55         if (sl == SL_GLOBAL)
56                 g_settings = pos;
57         return pos;
58 }
59
60
61 Settings *Settings::getLayer(SettingsLayer sl)
62 {
63         sanity_check((int)sl >= 0 && sl < SL_TOTAL_COUNT);
64         return s_layers[(size_t)sl];
65 }
66
67
68 Settings::~Settings()
69 {
70         MutexAutoLock lock(m_mutex);
71
72         if (m_settingslayer < SL_TOTAL_COUNT)
73                 s_layers[(size_t)m_settingslayer] = nullptr;
74
75         // Compatibility
76         if (m_settingslayer == SL_GLOBAL)
77                 g_settings = nullptr;
78
79         clearNoLock();
80 }
81
82
83 Settings & Settings::operator = (const Settings &other)
84 {
85         if (&other == this)
86                 return *this;
87
88         // TODO: Avoid copying Settings objects. Make this private.
89         FATAL_ERROR_IF(m_settingslayer != SL_TOTAL_COUNT && other.m_settingslayer != SL_TOTAL_COUNT,
90                 ("Tried to copy unique Setting layer " + std::to_string(m_settingslayer)).c_str());
91
92         MutexAutoLock lock(m_mutex);
93         MutexAutoLock lock2(other.m_mutex);
94
95         clearNoLock();
96         m_settings = other.m_settings;
97         m_callbacks = other.m_callbacks;
98
99         return *this;
100 }
101
102
103 bool Settings::checkNameValid(const std::string &name)
104 {
105         bool valid = name.find_first_of("=\"{}#") == std::string::npos;
106         if (valid)
107                 valid = std::find_if(name.begin(), name.end(), ::isspace) == name.end();
108
109         if (!valid) {
110                 errorstream << "Invalid setting name \"" << name << "\""
111                         << std::endl;
112                 return false;
113         }
114         return true;
115 }
116
117
118 bool Settings::checkValueValid(const std::string &value)
119 {
120         if (value.substr(0, 3) == "\"\"\"" ||
121                 value.find("\n\"\"\"") != std::string::npos) {
122                 errorstream << "Invalid character sequence '\"\"\"' found in"
123                         " setting value!" << std::endl;
124                 return false;
125         }
126         return true;
127 }
128
129 std::string Settings::getMultiline(std::istream &is, size_t *num_lines)
130 {
131         size_t lines = 1;
132         std::string value;
133         std::string line;
134
135         while (is.good()) {
136                 lines++;
137                 std::getline(is, line);
138                 if (line == "\"\"\"")
139                         break;
140                 value += line;
141                 value.push_back('\n');
142         }
143
144         size_t len = value.size();
145         if (len)
146                 value.erase(len - 1);
147
148         if (num_lines)
149                 *num_lines = lines;
150
151         return value;
152 }
153
154
155 bool Settings::readConfigFile(const char *filename)
156 {
157         std::ifstream is(filename);
158         if (!is.good())
159                 return false;
160
161         return parseConfigLines(is);
162 }
163
164
165 bool Settings::parseConfigLines(std::istream &is)
166 {
167         MutexAutoLock lock(m_mutex);
168
169         std::string line, name, value;
170
171         while (is.good()) {
172                 std::getline(is, line);
173                 SettingsParseEvent event = parseConfigObject(line, name, value);
174
175                 switch (event) {
176                 case SPE_NONE:
177                 case SPE_INVALID:
178                 case SPE_COMMENT:
179                         break;
180                 case SPE_KVPAIR:
181                         m_settings[name] = SettingsEntry(value);
182                         break;
183                 case SPE_END:
184                         return true;
185                 case SPE_GROUP: {
186                         Settings *group = new Settings("}");
187                         if (!group->parseConfigLines(is)) {
188                                 delete group;
189                                 return false;
190                         }
191                         m_settings[name] = SettingsEntry(group);
192                         break;
193                 }
194                 case SPE_MULTILINE:
195                         m_settings[name] = SettingsEntry(getMultiline(is));
196                         break;
197                 }
198         }
199
200         // false (failure) if end tag not found
201         return m_end_tag.empty();
202 }
203
204
205 void Settings::writeLines(std::ostream &os, u32 tab_depth) const
206 {
207         MutexAutoLock lock(m_mutex);
208
209         for (const auto &setting_it : m_settings)
210                 printEntry(os, setting_it.first, setting_it.second, tab_depth);
211
212         // For groups this must be "}" !
213         if (!m_end_tag.empty()) {
214                 for (u32 i = 0; i < tab_depth; i++)
215                         os << "\t";
216
217                 os << m_end_tag << "\n";
218         }
219 }
220
221
222 void Settings::printEntry(std::ostream &os, const std::string &name,
223         const SettingsEntry &entry, u32 tab_depth)
224 {
225         for (u32 i = 0; i != tab_depth; i++)
226                 os << "\t";
227
228         if (entry.is_group) {
229                 os << name << " = {\n";
230
231                 entry.group->writeLines(os, tab_depth + 1);
232
233                 // Closing bracket handled by writeLines
234         } else {
235                 os << name << " = ";
236
237                 if (entry.value.find('\n') != std::string::npos)
238                         os << "\"\"\"\n" << entry.value << "\n\"\"\"\n";
239                 else
240                         os << entry.value << "\n";
241         }
242 }
243
244
245 bool Settings::updateConfigObject(std::istream &is, std::ostream &os, u32 tab_depth)
246 {
247         SettingEntries::const_iterator it;
248         std::set<std::string> present_entries;
249         std::string line, name, value;
250         bool was_modified = false;
251         bool end_found = false;
252
253         // Add any settings that exist in the config file with the current value
254         // in the object if existing
255         while (is.good() && !end_found) {
256                 std::getline(is, line);
257                 SettingsParseEvent event = parseConfigObject(line, name, value);
258
259                 switch (event) {
260                 case SPE_END:
261                         // Skip end tag. Append later.
262                         end_found = true;
263                         break;
264                 case SPE_MULTILINE:
265                         value = getMultiline(is);
266                         /* FALLTHROUGH */
267                 case SPE_KVPAIR:
268                         it = m_settings.find(name);
269                         if (it != m_settings.end() &&
270                                         (it->second.is_group || it->second.value != value)) {
271                                 printEntry(os, name, it->second, tab_depth);
272                                 was_modified = true;
273                         } else if (it == m_settings.end()) {
274                                 // Remove by skipping
275                                 was_modified = true;
276                                 break;
277                         } else {
278                                 os << line << "\n";
279                                 if (event == SPE_MULTILINE)
280                                         os << value << "\n\"\"\"\n";
281                         }
282                         present_entries.insert(name);
283                         break;
284                 case SPE_GROUP:
285                         it = m_settings.find(name);
286                         if (it != m_settings.end() && it->second.is_group) {
287                                 os << line << "\n";
288                                 sanity_check(it->second.group != NULL);
289                                 was_modified |= it->second.group->updateConfigObject(is, os, tab_depth + 1);
290                         } else if (it == m_settings.end()) {
291                                 // Remove by skipping
292                                 was_modified = true;
293                                 Settings removed_group("}"); // Move 'is' to group end
294                                 std::stringstream ss;
295                                 removed_group.updateConfigObject(is, ss, tab_depth + 1);
296                                 break;
297                         } else {
298                                 printEntry(os, name, it->second, tab_depth);
299                                 was_modified = true;
300                         }
301                         present_entries.insert(name);
302                         break;
303                 default:
304                         os << line << (is.eof() ? "" : "\n");
305                         break;
306                 }
307         }
308
309         if (!line.empty() && is.eof())
310                 os << "\n";
311
312         // Add any settings in the object that don't exist in the config file yet
313         for (it = m_settings.begin(); it != m_settings.end(); ++it) {
314                 if (present_entries.find(it->first) != present_entries.end())
315                         continue;
316
317                 printEntry(os, it->first, it->second, tab_depth);
318                 was_modified = true;
319         }
320
321         // Append ending tag
322         if (!m_end_tag.empty()) {
323                 os << m_end_tag << "\n";
324                 was_modified |= !end_found;
325         }
326
327         return was_modified;
328 }
329
330
331 bool Settings::updateConfigFile(const char *filename)
332 {
333         MutexAutoLock lock(m_mutex);
334
335         std::ifstream is(filename);
336         std::ostringstream os(std::ios_base::binary);
337
338         bool was_modified = updateConfigObject(is, os);
339         is.close();
340
341         if (!was_modified)
342                 return true;
343
344         if (!fs::safeWriteToFile(filename, os.str())) {
345                 errorstream << "Error writing configuration file: \""
346                         << filename << "\"" << std::endl;
347                 return false;
348         }
349
350         return true;
351 }
352
353
354 bool Settings::parseCommandLine(int argc, char *argv[],
355                 std::map<std::string, ValueSpec> &allowed_options)
356 {
357         int nonopt_index = 0;
358         for (int i = 1; i < argc; i++) {
359                 std::string arg_name = argv[i];
360                 if (arg_name.substr(0, 2) != "--") {
361                         // If option doesn't start with -, read it in as nonoptX
362                         if (arg_name[0] != '-'){
363                                 std::string name = "nonopt";
364                                 name += itos(nonopt_index);
365                                 set(name, arg_name);
366                                 nonopt_index++;
367                                 continue;
368                         }
369                         errorstream << "Invalid command-line parameter \""
370                                         << arg_name << "\": --<option> expected." << std::endl;
371                         return false;
372                 }
373
374                 std::string name = arg_name.substr(2);
375
376                 std::map<std::string, ValueSpec>::iterator n;
377                 n = allowed_options.find(name);
378                 if (n == allowed_options.end()) {
379                         errorstream << "Unknown command-line parameter \""
380                                         << arg_name << "\"" << std::endl;
381                         return false;
382                 }
383
384                 ValueType type = n->second.type;
385
386                 std::string value;
387
388                 if (type == VALUETYPE_FLAG) {
389                         value = "true";
390                 } else {
391                         if ((i + 1) >= argc) {
392                                 errorstream << "Invalid command-line parameter \""
393                                                 << name << "\": missing value" << std::endl;
394                                 return false;
395                         }
396                         value = argv[++i];
397                 }
398
399                 set(name, value);
400         }
401
402         return true;
403 }
404
405
406
407 /***********
408  * Getters *
409  ***********/
410
411 Settings *Settings::getParent() const
412 {
413         // If the Settings object is within the hierarchy structure,
414         // iterate towards the origin (0) to find the next fallback layer
415         if (m_settingslayer >= SL_TOTAL_COUNT)
416                 return nullptr;
417
418         for (int i = (int)m_settingslayer - 1; i >= 0; --i) {
419                 if (s_layers[i])
420                         return s_layers[i];
421         }
422
423         // No parent
424         return nullptr;
425 }
426
427
428 const SettingsEntry &Settings::getEntry(const std::string &name) const
429 {
430         {
431                 MutexAutoLock lock(m_mutex);
432
433                 SettingEntries::const_iterator n;
434                 if ((n = m_settings.find(name)) != m_settings.end())
435                         return n->second;
436         }
437
438         if (auto parent = getParent())
439                 return parent->getEntry(name);
440
441         throw SettingNotFoundException("Setting [" + name + "] not found.");
442 }
443
444
445 Settings *Settings::getGroup(const std::string &name) const
446 {
447         const SettingsEntry &entry = getEntry(name);
448         if (!entry.is_group)
449                 throw SettingNotFoundException("Setting [" + name + "] is not a group.");
450         return entry.group;
451 }
452
453
454 const std::string &Settings::get(const std::string &name) const
455 {
456         const SettingsEntry &entry = getEntry(name);
457         if (entry.is_group)
458                 throw SettingNotFoundException("Setting [" + name + "] is a group.");
459         return entry.value;
460 }
461
462
463 bool Settings::getBool(const std::string &name) const
464 {
465         return is_yes(get(name));
466 }
467
468
469 u16 Settings::getU16(const std::string &name) const
470 {
471         return stoi(get(name), 0, 65535);
472 }
473
474
475 s16 Settings::getS16(const std::string &name) const
476 {
477         return stoi(get(name), -32768, 32767);
478 }
479
480
481 u32 Settings::getU32(const std::string &name) const
482 {
483         return (u32) stoi(get(name));
484 }
485
486 s32 Settings::getS32(const std::string &name) const
487 {
488         return stoi(get(name));
489 }
490
491
492 float Settings::getFloat(const std::string &name) const
493 {
494         return stof(get(name));
495 }
496
497
498 u64 Settings::getU64(const std::string &name) const
499 {
500         u64 value = 0;
501         std::string s = get(name);
502         std::istringstream ss(s);
503         ss >> value;
504         return value;
505 }
506
507
508 v2f Settings::getV2F(const std::string &name) const
509 {
510         v2f value;
511         Strfnd f(get(name));
512         f.next("(");
513         value.X = stof(f.next(","));
514         value.Y = stof(f.next(")"));
515         return value;
516 }
517
518
519 v3f Settings::getV3F(const std::string &name) const
520 {
521         v3f value;
522         Strfnd f(get(name));
523         f.next("(");
524         value.X = stof(f.next(","));
525         value.Y = stof(f.next(","));
526         value.Z = stof(f.next(")"));
527         return value;
528 }
529
530
531 u32 Settings::getFlagStr(const std::string &name, const FlagDesc *flagdesc,
532         u32 *flagmask) const
533 {
534         u32 flags = 0;
535
536         // Read default value (if there is any)
537         if (auto parent = getParent())
538                 flags = parent->getFlagStr(name, flagdesc, flagmask);
539
540         // Apply custom flags "on top"
541         if (m_settings.find(name) != m_settings.end()) {
542                 std::string value = get(name);
543                 u32 flags_user;
544                 u32 mask_user = U32_MAX;
545                 flags_user = std::isdigit(value[0])
546                         ? stoi(value) // Override default
547                         : readFlagString(value, flagdesc, &mask_user);
548
549                 flags &= ~mask_user;
550                 flags |=  flags_user;
551                 if (flagmask)
552                         *flagmask |= mask_user;
553         }
554
555         return flags;
556 }
557
558
559 bool Settings::getNoiseParams(const std::string &name, NoiseParams &np) const
560 {
561         if (getNoiseParamsFromGroup(name, np) || getNoiseParamsFromValue(name, np))
562                 return true;
563         if (auto parent = getParent())
564                 return parent->getNoiseParams(name, np);
565
566         return false;
567 }
568
569
570 bool Settings::getNoiseParamsFromValue(const std::string &name,
571         NoiseParams &np) const
572 {
573         std::string value;
574
575         if (!getNoEx(name, value))
576                 return false;
577
578         // Format: f32,f32,(f32,f32,f32),s32,s32,f32[,f32]
579         Strfnd f(value);
580
581         np.offset   = stof(f.next(","));
582         np.scale    = stof(f.next(","));
583         f.next("(");
584         np.spread.X = stof(f.next(","));
585         np.spread.Y = stof(f.next(","));
586         np.spread.Z = stof(f.next(")"));
587         f.next(",");
588         np.seed     = stoi(f.next(","));
589         np.octaves  = stoi(f.next(","));
590         np.persist  = stof(f.next(","));
591
592         std::string optional_params = f.next("");
593         if (!optional_params.empty())
594                 np.lacunarity = stof(optional_params);
595
596         return true;
597 }
598
599
600 bool Settings::getNoiseParamsFromGroup(const std::string &name,
601         NoiseParams &np) const
602 {
603         Settings *group = NULL;
604
605         if (!getGroupNoEx(name, group))
606                 return false;
607
608         group->getFloatNoEx("offset",      np.offset);
609         group->getFloatNoEx("scale",       np.scale);
610         group->getV3FNoEx("spread",        np.spread);
611         group->getS32NoEx("seed",          np.seed);
612         group->getU16NoEx("octaves",       np.octaves);
613         group->getFloatNoEx("persistence", np.persist);
614         group->getFloatNoEx("lacunarity",  np.lacunarity);
615
616         np.flags = 0;
617         if (!group->getFlagStrNoEx("flags", np.flags, flagdesc_noiseparams))
618                 np.flags = NOISE_FLAG_DEFAULTS;
619
620         return true;
621 }
622
623
624 bool Settings::exists(const std::string &name) const
625 {
626         MutexAutoLock lock(m_mutex);
627
628         if (m_settings.find(name) != m_settings.end())
629                 return true;
630         if (auto parent = getParent())
631                 return parent->exists(name);
632         return false;
633 }
634
635
636 std::vector<std::string> Settings::getNames() const
637 {
638         MutexAutoLock lock(m_mutex);
639
640         std::vector<std::string> names;
641         for (const auto &settings_it : m_settings) {
642                 names.push_back(settings_it.first);
643         }
644         return names;
645 }
646
647
648
649 /***************************************
650  * Getters that don't throw exceptions *
651  ***************************************/
652
653 bool Settings::getGroupNoEx(const std::string &name, Settings *&val) const
654 {
655         try {
656                 val = getGroup(name);
657                 return true;
658         } catch (SettingNotFoundException &e) {
659                 return false;
660         }
661 }
662
663
664 bool Settings::getNoEx(const std::string &name, std::string &val) const
665 {
666         try {
667                 val = get(name);
668                 return true;
669         } catch (SettingNotFoundException &e) {
670                 return false;
671         }
672 }
673
674
675 bool Settings::getFlag(const std::string &name) const
676 {
677         try {
678                 return getBool(name);
679         } catch(SettingNotFoundException &e) {
680                 return false;
681         }
682 }
683
684
685 bool Settings::getFloatNoEx(const std::string &name, float &val) const
686 {
687         try {
688                 val = getFloat(name);
689                 return true;
690         } catch (SettingNotFoundException &e) {
691                 return false;
692         }
693 }
694
695
696 bool Settings::getU16NoEx(const std::string &name, u16 &val) const
697 {
698         try {
699                 val = getU16(name);
700                 return true;
701         } catch (SettingNotFoundException &e) {
702                 return false;
703         }
704 }
705
706
707 bool Settings::getS16NoEx(const std::string &name, s16 &val) const
708 {
709         try {
710                 val = getS16(name);
711                 return true;
712         } catch (SettingNotFoundException &e) {
713                 return false;
714         }
715 }
716
717
718 bool Settings::getS32NoEx(const std::string &name, s32 &val) const
719 {
720         try {
721                 val = getS32(name);
722                 return true;
723         } catch (SettingNotFoundException &e) {
724                 return false;
725         }
726 }
727
728
729 bool Settings::getU64NoEx(const std::string &name, u64 &val) const
730 {
731         try {
732                 val = getU64(name);
733                 return true;
734         } catch (SettingNotFoundException &e) {
735                 return false;
736         }
737 }
738
739
740 bool Settings::getV2FNoEx(const std::string &name, v2f &val) const
741 {
742         try {
743                 val = getV2F(name);
744                 return true;
745         } catch (SettingNotFoundException &e) {
746                 return false;
747         }
748 }
749
750
751 bool Settings::getV3FNoEx(const std::string &name, v3f &val) const
752 {
753         try {
754                 val = getV3F(name);
755                 return true;
756         } catch (SettingNotFoundException &e) {
757                 return false;
758         }
759 }
760
761
762 bool Settings::getFlagStrNoEx(const std::string &name, u32 &val,
763         const FlagDesc *flagdesc) const
764 {
765         if (!flagdesc) {
766                 if (!(flagdesc = getFlagDescFallback(name)))
767                         return false; // Not found
768         }
769
770         try {
771                 val = getFlagStr(name, flagdesc, nullptr);
772
773                 return true;
774         } catch (SettingNotFoundException &e) {
775                 return false;
776         }
777 }
778
779
780 /***********
781  * Setters *
782  ***********/
783
784 bool Settings::setEntry(const std::string &name, const void *data,
785         bool set_group)
786 {
787         if (!checkNameValid(name))
788                 return false;
789         if (!set_group && !checkValueValid(*(const std::string *)data))
790                 return false;
791
792         Settings *old_group = NULL;
793         {
794                 MutexAutoLock lock(m_mutex);
795
796                 SettingsEntry &entry = m_settings[name];
797                 old_group = entry.group;
798
799                 entry.value    = set_group ? "" : *(const std::string *)data;
800                 entry.group    = set_group ? *(Settings **)data : NULL;
801                 entry.is_group = set_group;
802                 if (set_group)
803                         entry.group->m_end_tag = "}";
804         }
805
806         delete old_group;
807
808         return true;
809 }
810
811
812 bool Settings::set(const std::string &name, const std::string &value)
813 {
814         if (!setEntry(name, &value, false))
815                 return false;
816
817         doCallbacks(name);
818         return true;
819 }
820
821
822 // TODO: Remove this function
823 bool Settings::setDefault(const std::string &name, const std::string &value)
824 {
825         return getLayer(SL_DEFAULTS)->set(name, value);
826 }
827
828
829 bool Settings::setGroup(const std::string &name, const Settings &group)
830 {
831         // Settings must own the group pointer
832         // avoid double-free by copying the source
833         Settings *copy = new Settings();
834         *copy = group;
835         return setEntry(name, &copy, true);
836 }
837
838
839 bool Settings::setBool(const std::string &name, bool value)
840 {
841         return set(name, value ? "true" : "false");
842 }
843
844
845 bool Settings::setS16(const std::string &name, s16 value)
846 {
847         return set(name, itos(value));
848 }
849
850
851 bool Settings::setU16(const std::string &name, u16 value)
852 {
853         return set(name, itos(value));
854 }
855
856
857 bool Settings::setS32(const std::string &name, s32 value)
858 {
859         return set(name, itos(value));
860 }
861
862
863 bool Settings::setU64(const std::string &name, u64 value)
864 {
865         std::ostringstream os;
866         os << value;
867         return set(name, os.str());
868 }
869
870
871 bool Settings::setFloat(const std::string &name, float value)
872 {
873         return set(name, ftos(value));
874 }
875
876
877 bool Settings::setV2F(const std::string &name, v2f value)
878 {
879         std::ostringstream os;
880         os << "(" << value.X << "," << value.Y << ")";
881         return set(name, os.str());
882 }
883
884
885 bool Settings::setV3F(const std::string &name, v3f value)
886 {
887         std::ostringstream os;
888         os << "(" << value.X << "," << value.Y << "," << value.Z << ")";
889         return set(name, os.str());
890 }
891
892
893 bool Settings::setFlagStr(const std::string &name, u32 flags,
894         const FlagDesc *flagdesc, u32 flagmask)
895 {
896         if (!flagdesc) {
897                 if (!(flagdesc = getFlagDescFallback(name)))
898                         return false; // Not found
899         }
900
901         return set(name, writeFlagString(flags, flagdesc, flagmask));
902 }
903
904
905 bool Settings::setNoiseParams(const std::string &name, const NoiseParams &np)
906 {
907         Settings *group = new Settings;
908
909         group->setFloat("offset",      np.offset);
910         group->setFloat("scale",       np.scale);
911         group->setV3F("spread",        np.spread);
912         group->setS32("seed",          np.seed);
913         group->setU16("octaves",       np.octaves);
914         group->setFloat("persistence", np.persist);
915         group->setFloat("lacunarity",  np.lacunarity);
916         group->setFlagStr("flags",     np.flags, flagdesc_noiseparams, np.flags);
917
918         return setEntry(name, &group, true);
919 }
920
921
922 bool Settings::remove(const std::string &name)
923 {
924         // Lock as short as possible, unlock before doCallbacks()
925         m_mutex.lock();
926
927         SettingEntries::iterator it = m_settings.find(name);
928         if (it != m_settings.end()) {
929                 delete it->second.group;
930                 m_settings.erase(it);
931                 m_mutex.unlock();
932
933                 doCallbacks(name);
934                 return true;
935         }
936
937         m_mutex.unlock();
938         return false;
939 }
940
941
942 SettingsParseEvent Settings::parseConfigObject(const std::string &line,
943         std::string &name, std::string &value)
944 {
945         std::string trimmed_line = trim(line);
946
947         if (trimmed_line.empty())
948                 return SPE_NONE;
949         if (trimmed_line[0] == '#')
950                 return SPE_COMMENT;
951         if (trimmed_line == m_end_tag)
952                 return SPE_END;
953
954         size_t pos = trimmed_line.find('=');
955         if (pos == std::string::npos)
956                 return SPE_INVALID;
957
958         name  = trim(trimmed_line.substr(0, pos));
959         value = trim(trimmed_line.substr(pos + 1));
960
961         if (value == "{")
962                 return SPE_GROUP;
963         if (value == "\"\"\"")
964                 return SPE_MULTILINE;
965
966         return SPE_KVPAIR;
967 }
968
969
970 void Settings::clearNoLock()
971 {
972         for (SettingEntries::const_iterator it = m_settings.begin();
973                         it != m_settings.end(); ++it)
974                 delete it->second.group;
975         m_settings.clear();
976 }
977
978
979 void Settings::setDefault(const std::string &name, const FlagDesc *flagdesc,
980         u32 flags)
981 {
982         s_flags[name] = flagdesc;
983         setDefault(name, writeFlagString(flags, flagdesc, U32_MAX));
984 }
985
986 const FlagDesc *Settings::getFlagDescFallback(const std::string &name) const
987 {
988         auto it = s_flags.find(name);
989         return it == s_flags.end() ? nullptr : it->second;
990 }
991
992 void Settings::registerChangedCallback(const std::string &name,
993         SettingsChangedCallback cbf, void *userdata)
994 {
995         MutexAutoLock lock(m_callback_mutex);
996         m_callbacks[name].emplace_back(cbf, userdata);
997 }
998
999 void Settings::deregisterChangedCallback(const std::string &name,
1000         SettingsChangedCallback cbf, void *userdata)
1001 {
1002         MutexAutoLock lock(m_callback_mutex);
1003         SettingsCallbackMap::iterator it_cbks = m_callbacks.find(name);
1004
1005         if (it_cbks != m_callbacks.end()) {
1006                 SettingsCallbackList &cbks = it_cbks->second;
1007
1008                 SettingsCallbackList::iterator position =
1009                         std::find(cbks.begin(), cbks.end(), std::make_pair(cbf, userdata));
1010
1011                 if (position != cbks.end())
1012                         cbks.erase(position);
1013         }
1014 }
1015
1016 void Settings::removeSecureSettings()
1017 {
1018         for (const auto &name : getNames()) {
1019                 if (name.compare(0, 7, "secure.") != 0)
1020                         continue;
1021
1022                 errorstream << "Secure setting " << name
1023                                 << " isn't allowed, so was ignored."
1024                                 << std::endl;
1025                 remove(name);
1026         }
1027 }
1028
1029 void Settings::doCallbacks(const std::string &name) const
1030 {
1031         MutexAutoLock lock(m_callback_mutex);
1032
1033         SettingsCallbackMap::const_iterator it_cbks = m_callbacks.find(name);
1034         if (it_cbks != m_callbacks.end()) {
1035                 SettingsCallbackList::const_iterator it;
1036                 for (it = it_cbks->second.begin(); it != it_cbks->second.end(); ++it)
1037                         (it->first)(name, it->second);
1038         }
1039 }