]> git.lizzy.rs Git - dragonfireclient.git/blobdiff - src/settings.h
Remove stuff made obsolete by making players more ActiveObject-like and raise protoco...
[dragonfireclient.git] / src / settings.h
index f972ce3ece1ce8528d97eb2c82514ca9e5aef550..4bc22eaa1e08f47682108872b272a7790dc246d4 100644 (file)
@@ -31,6 +31,7 @@ with this program; if not, write to the Free Software Foundation, Inc.,
 #include <sstream>
 #include "debug.h"
 #include "utility.h"
+#include "log.h"
 
 enum ValueType
 {
@@ -77,11 +78,11 @@ class Settings
                
                std::string trimmedline = trim(line);
                
-               // Ignore comments
-               if(trimmedline[0] == '#')
+               // Ignore empty lines and comments
+               if(trimmedline.size() == 0 || trimmedline[0] == '#')
                        return true;
 
-               //dstream<<"trimmedline=\""<<trimmedline<<"\""<<std::endl;
+               //infostream<<"trimmedline=\""<<trimmedline<<"\""<<std::endl;
 
                Strfnd sf(trim(line));
 
@@ -94,7 +95,7 @@ class Settings
                std::string value = sf.next("\n");
                value = trim(value);
 
-               /*dstream<<"Config name=\""<<name<<"\" value=\""
+               /*infostream<<"Config name=\""<<name<<"\" value=\""
                                <<value<<"\""<<std::endl;*/
                
                m_settings[name] = value;
@@ -102,6 +103,22 @@ class Settings
                return true;
        }
 
+       void parseConfigLines(std::istream &is, const std::string &endstring)
+       {
+               for(;;){
+                       if(is.eof())
+                               break;
+                       std::string line;
+                       std::getline(is, line);
+                       std::string trimmedline = trim(line);
+                       if(endstring != ""){
+                               if(trimmedline == endstring)
+                                       break;
+                       }
+                       parseConfigLine(line);
+               }
+       }
+
        // Returns false on EOF
        bool parseConfigObject(std::istream &is)
        {
@@ -114,7 +131,7 @@ class Settings
                */
                std::string line;
                std::getline(is, line);
-               //dstream<<"got line: \""<<line<<"\""<<std::endl;
+               //infostream<<"got line: \""<<line<<"\""<<std::endl;
 
                return parseConfigLine(line);
        }
@@ -129,12 +146,12 @@ class Settings
                std::ifstream is(filename);
                if(is.good() == false)
                {
-                       dstream<<"Error opening configuration file \""
+                       errorstream<<"Error opening configuration file \""
                                        <<filename<<"\""<<std::endl;
                        return false;
                }
 
-               dstream<<"Parsing configuration file: \""
+               infostream<<"Parsing configuration file: \""
                                <<filename<<"\""<<std::endl;
                                
                while(parseConfigObject(is));
@@ -172,8 +189,8 @@ class Settings
                if(is.eof() == false)
                        line_end = "\n";
                
-               // Ignore comments
-               if(trimmedline[0] == '#')
+               // Ignore empty lines and comments
+               if(trimmedline.size() == 0 || trimmedline[0] == '#')
                {
                        dst.push_back(line+line_end);
                        return true;
@@ -199,7 +216,7 @@ class Settings
                        
                        if(newvalue != value)
                        {
-                               dstream<<"Changing value of \""<<name<<"\" = \""
+                               infostream<<"Changing value of \""<<name<<"\" = \""
                                                <<value<<"\" -> \""<<newvalue<<"\""
                                                <<std::endl;
                        }
@@ -219,7 +236,7 @@ class Settings
        */
        bool updateConfigFile(const char *filename)
        {
-               dstream<<"Updating configuration file: \""
+               infostream<<"Updating configuration file: \""
                                <<filename<<"\""<<std::endl;
                
                core::list<std::string> objects;
@@ -230,7 +247,7 @@ class Settings
                        std::ifstream is(filename);
                        if(is.good() == false)
                        {
-                               dstream<<"INFO: updateConfigFile():"
+                               infostream<<"updateConfigFile():"
                                                " Error opening configuration file"
                                                " for reading: \""
                                                <<filename<<"\""<<std::endl;
@@ -248,7 +265,7 @@ class Settings
                        std::ofstream os(filename);
                        if(os.good() == false)
                        {
-                               dstream<<"Error opening configuration file"
+                               errorstream<<"Error opening configuration file"
                                                " for writing: \""
                                                <<filename<<"\""<<std::endl;
                                return false;
@@ -275,7 +292,7 @@ class Settings
                                        continue;
                                std::string name = i.getNode()->getKey();
                                std::string value = i.getNode()->getValue();
-                               dstream<<"Adding \""<<name<<"\" = \""<<value<<"\""
+                               infostream<<"Adding \""<<name<<"\" = \""<<value<<"\""
                                                <<std::endl;
                                os<<name<<" = "<<value<<"\n";
                        }
@@ -300,7 +317,7 @@ class Settings
                        std::string argname = argv[i];
                        if(argname.substr(0, 2) != "--")
                        {
-                               dstream<<"Invalid command-line parameter \""
+                               errorstream<<"Invalid command-line parameter \""
                                                <<argname<<"\": --<option> expected."<<std::endl;
                                return false;
                        }
@@ -312,7 +329,7 @@ class Settings
                        n = allowed_options.find(name);
                        if(n == NULL)
                        {
-                               dstream<<"Unknown command-line parameter \""
+                               errorstream<<"Unknown command-line parameter \""
                                                <<argname<<"\""<<std::endl;
                                return false;
                        }
@@ -329,7 +346,7 @@ class Settings
                        {
                                if(i >= argc)
                                {
-                                       dstream<<"Invalid command-line parameter \""
+                                       errorstream<<"Invalid command-line parameter \""
                                                        <<name<<"\": missing value"<<std::endl;
                                        return false;
                                }
@@ -338,7 +355,7 @@ class Settings
                        }
                        
 
-                       dstream<<"Valid command-line parameter: \""
+                       infostream<<"Valid command-line parameter: \""
                                        <<name<<"\" = \""<<value<<"\""
                                        <<std::endl;
                        set(name, value);
@@ -387,7 +404,7 @@ class Settings
                        n = m_defaults.find(name);
                        if(n == NULL)
                        {
-                               dstream<<"INFO: Settings: Setting not found: \""
+                               infostream<<"Settings: Setting not found: \""
                                                <<name<<"\""<<std::endl;
                                throw SettingNotFoundException("Setting not found");
                        }
@@ -481,6 +498,16 @@ class Settings
                return value;
        }
 
+       v2f getV2F(std::string name)
+       {
+               v2f value;
+               Strfnd f(get(name));
+               f.next("(");
+               value.X = stof(f.next(","));
+               value.Y = stof(f.next(")"));
+               return value;
+       }
+
        u64 getU64(std::string name)
        {
                u64 value = 0;
@@ -515,6 +542,13 @@ class Settings
                set(name, os.str());
        }
 
+       void setV2F(std::string name, v2f value)
+       {
+               std::ostringstream os;
+               os<<"("<<value.X<<","<<value.Y<<")";
+               set(name, os.str());
+       }
+
        void setU64(std::string name, u64 value)
        {
                std::ostringstream os;
@@ -530,6 +564,47 @@ class Settings
                m_defaults.clear();
        }
 
+       void updateValue(Settings &other, const std::string &name)
+       {
+               JMutexAutoLock lock(m_mutex);
+               
+               if(&other == this)
+                       return;
+
+               try{
+                       std::string val = other.get(name);
+                       m_settings[name] = val;
+               } catch(SettingNotFoundException &e){
+               }
+
+               return;
+       }
+
+       void update(Settings &other)
+       {
+               JMutexAutoLock lock(m_mutex);
+               JMutexAutoLock lock2(other.m_mutex);
+               
+               if(&other == this)
+                       return;
+
+               for(core::map<std::string, std::string>::Iterator
+                               i = other.m_settings.getIterator();
+                               i.atEnd() == false; i++)
+               {
+                       m_settings[i.getNode()->getKey()] = i.getNode()->getValue();
+               }
+               
+               for(core::map<std::string, std::string>::Iterator
+                               i = other.m_defaults.getIterator();
+                               i.atEnd() == false; i++)
+               {
+                       m_defaults[i.getNode()->getKey()] = i.getNode()->getValue();
+               }
+
+               return;
+       }
+
        Settings & operator+=(Settings &other)
        {
                JMutexAutoLock lock(m_mutex);