1 // NeL - MMORPG Framework <http://dev.ryzom.com/projects/nel/>
2 // Copyright (C) 2010 Winch Gate Property Limited
4 // This source file has been modified by the following contributors:
5 // Copyright (C) 2018 Jan BOON (Kaetemi) <jan.boon@kaetemi.be>
7 // This program is free software: you can redistribute it and/or modify
8 // it under the terms of the GNU Affero General Public License as
9 // published by the Free Software Foundation, either version 3 of the
10 // License, or (at your option) any later version.
12 // This program is distributed in the hope that it will be useful,
13 // but WITHOUT ANY WARRANTY; without even the implied warranty of
14 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 // GNU Affero General Public License for more details.
17 // You should have received a copy of the GNU Affero General Public License
18 // along with this program. If not, see <http://www.gnu.org/licenses/>.
20 #include <nel/misc/types_nl.h>
21 #include "configuration.h"
26 // #include <nel/misc/debug.h>
27 #include <nel/misc/config_file.h>
28 #include <nel/misc/path.h>
29 #include <nel/misc/i18n.h>
32 #include "snowballs_client.h"
33 #include "snowballs_config.h"
36 using namespace NLMISC
;
40 void CConfiguration::setAndCallback(const std::string
&varName
, void (*cb
)(CConfigFile::CVar
&var
))
42 ConfigFile
->setCallback(varName
, cb
);
43 CConfigFile::CVar
*varPtr
= ConfigFile
->getVarPtr(varName
);
46 nlwarning("Missing config variable '%s'", varName
.c_str());
54 void CConfiguration::dropCallback(const std::string
&varName
)
56 ConfigFile
->setCallback(varName
, NULL
);
59 bool CConfiguration::init()
61 nlassert(!ConfigFile
); ConfigFile
= new CConfigFile(); nlassert(ConfigFile
);
62 ConfigFile
->load(SBCLIENT_CONFIG_FILE
);
64 // set the search paths (kinda important)
65 CConfigFile::CVar
*var
;
66 var
= ConfigFile
->getVarPtr("SearchPaths");
67 uint varsize
= var
->size();
68 for (uint i
= 0; i
< varsize
; ++i
)
69 CPath::addSearchPath(var
->asString(i
), true, false);
70 var
= ConfigFile
->getVarPtr("RemapExtensions");
71 varsize
= var
->size();
72 for (uint i
= 0; i
< varsize
; i
+= 2)
73 CPath::remapExtension(var
->asString(i
), var
->asString(i
+ 1), true);
78 bool CConfiguration::release()
80 // save and release the config file
83 if (ConfigFile
->exists("SaveConfig") && ConfigFile
->getVarPtr("SaveConfig")->asBool())
90 else nlwarning("!ConfigFile");
92 // release the search paths etc
93 CPath::releaseInstance();
98 void CConfiguration::updateUtilities()
100 CConfigFile::checkConfigFiles();
103 float CConfiguration::getValue(const string
&varName
, float defaultValue
)
105 if (ConfigFile
->exists(varName
)) return ConfigFile
->getVar(varName
).asFloat();
106 CConfigFile::CVar varToCopy
;
107 varToCopy
.forceAsDouble((double)defaultValue
);
108 ConfigFile
->insertVar(varName
, varToCopy
);
112 double CConfiguration::getValue(const string
&varName
, double defaultValue
)
114 if (ConfigFile
->exists(varName
)) return ConfigFile
->getVar(varName
).asDouble();
115 CConfigFile::CVar varToCopy
;
116 varToCopy
.forceAsDouble(defaultValue
);
117 ConfigFile
->insertVar(varName
, varToCopy
);
121 int CConfiguration::getValue(const string
&varName
, int defaultValue
)
123 if (ConfigFile
->exists(varName
)) return ConfigFile
->getVar(varName
).asInt();
124 CConfigFile::CVar varToCopy
;
125 varToCopy
.forceAsInt(defaultValue
);
126 ConfigFile
->insertVar(varName
, varToCopy
);
130 string
CConfiguration::getValue(const string
&varName
, const string
&defaultValue
)
132 if (ConfigFile
->exists(varName
)) return ConfigFile
->getVar(varName
).asString();
133 CConfigFile::CVar varToCopy
;
134 varToCopy
.forceAsString(defaultValue
);
135 ConfigFile
->insertVar(varName
, varToCopy
);
139 ucstring
CConfiguration::getValue(const string
&varName
, const ucstring
&defaultValue
)
141 if (ConfigFile
->exists(varName
)) return ucstring::makeFromUtf8(ConfigFile
->getVar(varName
).asString());
142 CConfigFile::CVar varToCopy
;
143 varToCopy
.forceAsString(defaultValue
.toUtf8());
144 ConfigFile
->insertVar(varName
, varToCopy
);
148 bool CConfiguration::getValue(const string
&varName
, bool defaultValue
)
150 if (ConfigFile
->exists(varName
)) return ConfigFile
->getVar(varName
).asBool();
151 CConfigFile::CVar varToCopy
;
152 varToCopy
.forceAsInt(defaultValue
? 1 : 0);
153 ConfigFile
->insertVar(varName
, varToCopy
);
157 CRGBA
CConfiguration::getValue(const string
&varName
, const CRGBA
&defaultValue
)
159 if (ConfigFile
->exists(varName
))
161 return getValue(ConfigFile
->getVar(varName
), defaultValue
);
165 // create a new value only if one doesn't exist
166 CConfigFile::CVar varToCopy
;
167 varToCopy
.forceAsInt(defaultValue
.R
);
168 varToCopy
.setAsInt(defaultValue
.G
, 1);
169 varToCopy
.setAsInt(defaultValue
.B
, 2);
170 varToCopy
.setAsInt(defaultValue
.A
, 3);
171 ConfigFile
->insertVar(varName
, varToCopy
);
176 CRGBA
CConfiguration::getValue(const CConfigFile::CVar
&var
, const CRGBA
&defaultValue
)
180 if (var
.size() > 4) nlwarning("RGBA value in config value '%s' is too long, ignoring unused values");
181 return CRGBA(var
.asInt(0), var
.asInt(1), var
.asInt(2), var
.size() >= 4 ? var
.asInt(3) : 255);
183 nlwarning("Invalid RGBA value in config value '%s', reverting to default { %i, %i, %i, %i }", var
.Name
.c_str(), (sint
)defaultValue
.R
, (sint
)defaultValue
.G
, (sint
)defaultValue
.B
, (sint
)defaultValue
.A
);
187 } /* namespace SBCLIENT */