]> git.lizzy.rs Git - minetest.git/blobdiff - src/settings.h
Support packing arbitrary graphs (#12289)
[minetest.git] / src / settings.h
index af4cae3cdf68c56ce7fa138f45e3b8dae4dab3d3..767d057f9c1b77eb4b11e7fe4095121a32eb0aa3 100644 (file)
@@ -21,6 +21,7 @@ with this program; if not, write to the Free Software Foundation, Inc.,
 
 #include "irrlichttypes_bloated.h"
 #include "util/string.h"
+#include "util/basic_macros.h"
 #include <string>
 #include <list>
 #include <set>
@@ -60,14 +61,36 @@ enum SettingsParseEvent {
        SPE_MULTILINE,
 };
 
+// Describes the global setting layers, SL_GLOBAL is where settings are read from
 enum SettingsLayer {
        SL_DEFAULTS,
        SL_GAME,
        SL_GLOBAL,
-       SL_MAP,
        SL_TOTAL_COUNT
 };
 
+// Implements the hierarchy a settings object may be part of
+class SettingsHierarchy {
+public:
+       /*
+        * A settings object that may be part of another hierarchy can
+        * occupy the index 0 as a fallback. If not set you can use 0 on your own.
+        */
+       SettingsHierarchy(Settings *fallback = nullptr);
+
+       DISABLE_CLASS_COPY(SettingsHierarchy)
+
+       Settings *getLayer(int layer) const;
+
+private:
+       friend class Settings;
+       Settings *getParent(int layer) const;
+       void onLayerCreated(int layer, Settings *obj);
+       void onLayerRemoved(int layer);
+
+       std::vector<Settings*> layers;
+};
+
 struct ValueSpec {
        ValueSpec(ValueType a_type, const char *a_help=NULL)
        {
@@ -100,13 +123,15 @@ typedef std::unordered_map<std::string, SettingsEntry> SettingEntries;
 
 class Settings {
 public:
+       /* These functions operate on the global hierarchy! */
        static Settings *createLayer(SettingsLayer sl, const std::string &end_tag = "");
        static Settings *getLayer(SettingsLayer sl);
-       SettingsLayer getLayerType() const { return m_settingslayer; }
+       /**/
 
        Settings(const std::string &end_tag = "") :
                m_end_tag(end_tag)
        {}
+       Settings(const std::string &end_tag, SettingsHierarchy *h, int settings_layer);
        ~Settings();
 
        Settings & operator += (const Settings &other);
@@ -132,7 +157,6 @@ class Settings {
 
        Settings *getGroup(const std::string &name) const;
        const std::string &get(const std::string &name) const;
-       const std::string &getDefault(const std::string &name) const;
        bool getBool(const std::string &name) const;
        u16 getU16(const std::string &name) const;
        s16 getS16(const std::string &name) const;
@@ -148,9 +172,12 @@ class Settings {
        bool getNoiseParamsFromValue(const std::string &name, NoiseParams &np) const;
        bool getNoiseParamsFromGroup(const std::string &name, NoiseParams &np) const;
 
-       // return all keys used
+       // return all keys used in this object
        std::vector<std::string> getNames() const;
+       // check if setting exists anywhere in the hierarchy
        bool exists(const std::string &name) const;
+       // check if setting exists in this object ("locally")
+       bool existsLocal(const std::string &name) const;
 
 
        /***************************************
@@ -162,6 +189,7 @@ class Settings {
        bool getFlag(const std::string &name) const;
        bool getU16NoEx(const std::string &name, u16 &val) const;
        bool getS16NoEx(const std::string &name, s16 &val) const;
+       bool getU32NoEx(const std::string &name, u32 &val) const;
        bool getS32NoEx(const std::string &name, s32 &val) const;
        bool getU64NoEx(const std::string &name, u64 &val) const;
        bool getFloatNoEx(const std::string &name, float &val) const;
@@ -201,9 +229,9 @@ class Settings {
        // remove a setting
        bool remove(const std::string &name);
 
-       /**************
-        * Miscellany *
-        **************/
+       /*****************
+        * Miscellaneous *
+        *****************/
 
        void setDefault(const std::string &name, const FlagDesc *flagdesc, u32 flags);
        const FlagDesc *getFlagDescFallback(const std::string &name) const;
@@ -215,6 +243,10 @@ class Settings {
 
        void removeSecureSettings();
 
+       // Returns the settings layer this object is.
+       // If within the global hierarchy you can cast this to enum SettingsLayer
+       inline int getLayer() const { return m_settingslayer; }
+
 private:
        /***********************
         * Reading and writing *
@@ -240,6 +272,8 @@ class Settings {
 
        // Allow TestSettings to run sanity checks using private functions.
        friend class TestSettings;
+       // For sane mutex locking when iterating
+       friend class LuaSettings;
 
        void updateNoLock(const Settings &other);
        void clearNoLock();
@@ -256,7 +290,8 @@ class Settings {
        // All methods that access m_settings/m_defaults directly should lock this.
        mutable std::mutex m_mutex;
 
-       static Settings *s_layers[SL_TOTAL_COUNT];
-       SettingsLayer m_settingslayer = SL_TOTAL_COUNT;
+       SettingsHierarchy *m_hierarchy = nullptr;
+       int m_settingslayer = -1;
+
        static std::unordered_map<std::string, const FlagDesc *> s_flags;
 };