settings.h 8.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295
  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. #pragma once
  17. #include "irrlichttypes_bloated.h"
  18. #include "util/string.h"
  19. #include "util/basic_macros.h"
  20. #include <string>
  21. #include <set>
  22. #include <map>
  23. #include <mutex>
  24. class Settings;
  25. struct NoiseParams;
  26. // Global objects
  27. extern Settings *g_settings; // Same as Settings::getLayer(SL_GLOBAL);
  28. extern std::string g_settings_path;
  29. // Type for a settings changed callback function
  30. typedef void (*SettingsChangedCallback)(const std::string &name, void *data);
  31. typedef std::vector<
  32. std::pair<
  33. SettingsChangedCallback,
  34. void *
  35. >
  36. > SettingsCallbackList;
  37. typedef std::unordered_map<std::string, SettingsCallbackList> SettingsCallbackMap;
  38. enum ValueType {
  39. VALUETYPE_STRING,
  40. VALUETYPE_FLAG // Doesn't take any arguments
  41. };
  42. enum SettingsParseEvent {
  43. SPE_NONE,
  44. SPE_INVALID,
  45. SPE_COMMENT,
  46. SPE_KVPAIR,
  47. SPE_END,
  48. SPE_GROUP,
  49. SPE_MULTILINE,
  50. };
  51. // Describes the global setting layers, SL_GLOBAL is where settings are read from
  52. enum SettingsLayer {
  53. SL_DEFAULTS,
  54. SL_GAME,
  55. SL_GLOBAL,
  56. SL_TOTAL_COUNT
  57. };
  58. // Implements the hierarchy a settings object may be part of
  59. class SettingsHierarchy {
  60. public:
  61. /*
  62. * A settings object that may be part of another hierarchy can
  63. * occupy the index 0 as a fallback. If not set you can use 0 on your own.
  64. */
  65. SettingsHierarchy(Settings *fallback = nullptr);
  66. DISABLE_CLASS_COPY(SettingsHierarchy)
  67. Settings *getLayer(int layer) const;
  68. private:
  69. friend class Settings;
  70. Settings *getParent(int layer) const;
  71. void onLayerCreated(int layer, Settings *obj);
  72. void onLayerRemoved(int layer);
  73. std::vector<Settings*> layers;
  74. };
  75. struct ValueSpec {
  76. ValueSpec(ValueType a_type, const char *a_help=NULL)
  77. {
  78. type = a_type;
  79. help = a_help;
  80. }
  81. ValueType type;
  82. const char *help;
  83. };
  84. struct SettingsEntry {
  85. SettingsEntry() = default;
  86. SettingsEntry(const std::string &value_) :
  87. value(value_)
  88. {}
  89. SettingsEntry(Settings *group_) :
  90. group(group_),
  91. is_group(true)
  92. {}
  93. std::string value = "";
  94. Settings *group = nullptr;
  95. bool is_group = false;
  96. };
  97. typedef std::unordered_map<std::string, SettingsEntry> SettingEntries;
  98. class Settings {
  99. public:
  100. /* These functions operate on the global hierarchy! */
  101. static Settings *createLayer(SettingsLayer sl, std::string_view end_tag = "");
  102. static Settings *getLayer(SettingsLayer sl);
  103. /**/
  104. Settings(std::string_view end_tag = "") :
  105. m_end_tag(end_tag)
  106. {}
  107. Settings(std::string_view end_tag, SettingsHierarchy *h, int settings_layer);
  108. ~Settings();
  109. Settings & operator=(const Settings &other);
  110. /***********************
  111. * Reading and writing *
  112. ***********************/
  113. // Read configuration file. Returns success.
  114. bool readConfigFile(const char *filename);
  115. //Updates configuration file. Returns success.
  116. bool updateConfigFile(const char *filename);
  117. // NOTE: Types of allowed_options are ignored. Returns success.
  118. bool parseCommandLine(int argc, char *argv[],
  119. const std::map<std::string, ValueSpec> &allowed_options);
  120. bool parseConfigLines(std::istream &is);
  121. void writeLines(std::ostream &os, u32 tab_depth=0) const;
  122. /***********
  123. * Getters *
  124. ***********/
  125. Settings *getGroup(const std::string &name) const;
  126. const std::string &get(const std::string &name) const;
  127. bool getBool(const std::string &name) const;
  128. u16 getU16(const std::string &name) const;
  129. s16 getS16(const std::string &name) const;
  130. u32 getU32(const std::string &name) const;
  131. s32 getS32(const std::string &name) const;
  132. u64 getU64(const std::string &name) const;
  133. float getFloat(const std::string &name) const;
  134. float getFloat(const std::string &name, float min, float max) const;
  135. v2f getV2F(const std::string &name) const;
  136. v3f getV3F(const std::string &name) const;
  137. u32 getFlagStr(const std::string &name, const FlagDesc *flagdesc,
  138. u32 *flagmask) const;
  139. bool getNoiseParams(const std::string &name, NoiseParams &np) const;
  140. bool getNoiseParamsFromValue(const std::string &name, NoiseParams &np) const;
  141. bool getNoiseParamsFromGroup(const std::string &name, NoiseParams &np) const;
  142. // return all keys used in this object
  143. std::vector<std::string> getNames() const;
  144. // check if setting exists anywhere in the hierarchy
  145. bool exists(const std::string &name) const;
  146. // check if setting exists in this object ("locally")
  147. bool existsLocal(const std::string &name) const;
  148. /***************************************
  149. * Getters that don't throw exceptions *
  150. ***************************************/
  151. bool getGroupNoEx(const std::string &name, Settings *&val) const;
  152. bool getNoEx(const std::string &name, std::string &val) const;
  153. bool getFlag(const std::string &name) const;
  154. bool getU16NoEx(const std::string &name, u16 &val) const;
  155. bool getS16NoEx(const std::string &name, s16 &val) const;
  156. bool getU32NoEx(const std::string &name, u32 &val) const;
  157. bool getS32NoEx(const std::string &name, s32 &val) const;
  158. bool getU64NoEx(const std::string &name, u64 &val) const;
  159. bool getFloatNoEx(const std::string &name, float &val) const;
  160. bool getV2FNoEx(const std::string &name, v2f &val) const;
  161. bool getV3FNoEx(const std::string &name, v3f &val) const;
  162. // Like other getters, but handling each flag individualy:
  163. // 1) Read default flags (or 0)
  164. // 2) Override using user-defined flags
  165. bool getFlagStrNoEx(const std::string &name, u32 &val,
  166. const FlagDesc *flagdesc) const;
  167. /***********
  168. * Setters *
  169. ***********/
  170. // N.B. Groups not allocated with new must be set to NULL in the settings
  171. // tree before object destruction.
  172. bool setEntry(const std::string &name, const void *entry,
  173. bool set_group);
  174. bool set(const std::string &name, const std::string &value);
  175. bool setDefault(const std::string &name, const std::string &value);
  176. bool setGroup(const std::string &name, const Settings &group);
  177. bool setBool(const std::string &name, bool value);
  178. bool setS16(const std::string &name, s16 value);
  179. bool setU16(const std::string &name, u16 value);
  180. bool setS32(const std::string &name, s32 value);
  181. bool setU64(const std::string &name, u64 value);
  182. bool setFloat(const std::string &name, float value);
  183. bool setV2F(const std::string &name, v2f value);
  184. bool setV3F(const std::string &name, v3f value);
  185. bool setFlagStr(const std::string &name, u32 flags,
  186. const FlagDesc *flagdesc = nullptr, u32 flagmask = U32_MAX);
  187. bool setNoiseParams(const std::string &name, const NoiseParams &np);
  188. // remove a setting
  189. bool remove(const std::string &name);
  190. /*****************
  191. * Miscellaneous *
  192. *****************/
  193. void setDefault(const std::string &name, const FlagDesc *flagdesc, u32 flags);
  194. const FlagDesc *getFlagDescFallback(const std::string &name) const;
  195. void registerChangedCallback(const std::string &name,
  196. SettingsChangedCallback cbf, void *userdata = NULL);
  197. void deregisterChangedCallback(const std::string &name,
  198. SettingsChangedCallback cbf, void *userdata = NULL);
  199. void removeSecureSettings();
  200. // Returns the settings layer this object is.
  201. // If within the global hierarchy you can cast this to enum SettingsLayer
  202. inline int getLayer() const { return m_settingslayer; }
  203. private:
  204. /***********************
  205. * Reading and writing *
  206. ***********************/
  207. SettingsParseEvent parseConfigObject(const std::string &line,
  208. std::string &name, std::string &value);
  209. bool updateConfigObject(std::istream &is, std::ostream &os,
  210. u32 tab_depth=0);
  211. static bool checkNameValid(std::string_view name);
  212. static bool checkValueValid(std::string_view value);
  213. static std::string getMultiline(std::istream &is, size_t *num_lines=NULL);
  214. static void printEntry(std::ostream &os, const std::string &name,
  215. const SettingsEntry &entry, u32 tab_depth=0);
  216. /***********
  217. * Getters *
  218. ***********/
  219. Settings *getParent() const;
  220. const SettingsEntry &getEntry(const std::string &name) const;
  221. // Allow TestSettings to run sanity checks using private functions.
  222. friend class TestSettings;
  223. // For sane mutex locking when iterating
  224. friend class LuaSettings;
  225. void clearNoLock();
  226. void doCallbacks(const std::string &name) const;
  227. SettingEntries m_settings;
  228. SettingsCallbackMap m_callbacks;
  229. std::string m_end_tag;
  230. mutable std::mutex m_callback_mutex;
  231. // All methods that access m_settings/m_defaults directly should lock this.
  232. mutable std::mutex m_mutex;
  233. SettingsHierarchy *m_hierarchy = nullptr;
  234. int m_settingslayer = -1;
  235. static std::unordered_map<std::string, const FlagDesc *> s_flags;
  236. };