1 // Ryzom - MMORPG Framework <http://dev.ryzom.com/projects/ryzom/>
2 // Copyright (C) 2010 Winch Gate Property Limited
4 // This source file has been modified by the following contributors:
5 // Copyright (C) 2013 Laszlo KIS-ADAM (dfighter) <dfighter1985@gmail.com>
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 #ifndef R2_CONFIG_VAR_H
21 #define R2_CONFIG_VAR_H
23 #include "nel/gui/lua_object.h"
29 // base class for he config file variables
33 CConfigVarBase(const char *varName
);
35 mutable uint32 _TimeStamp
; // If this date is not the same than the config file date, then we know the
36 // value should be updated (so, more a counter than a timestamp, actually)
40 CConfigVarBase(const CConfigVarBase
&/* other */) { nlassert(0); }
41 CConfigVarBase
&operator = (const CConfigVarBase
&/* other */) { nlassert(0); return *this; }
43 static uint32
&getConfigFileTimeStamp();
48 /** Quick access to variables defined inside r2_config.lua.
50 * Variables are updated when the editor is reseted.
51 * They are usually created at global scope.
55 * CConfigVarRGBA CV_CreatureDefaultColor("CreatureDefaultColor", CRGBA(255, 255, 255, 0))); // r2_config.lua may contains a 'CreatureDefaultColor' config value
57 * creature.setColor(CV_CreatureDefaultColor.get());
61 class CConfigVar
: public CConfigVarBase
64 CConfigVar(const char *varName
, const T
&defaultValue
= T()) : CConfigVarBase(varName
), _Default(defaultValue
) {}
65 CConfigVar(const CConfigVar
&other
) : CConfigVarBase(other
._VarName
.c_str()), _Value(other
._Value
), _Default(other
._Default
) {}
72 // forward declarations for specialisations
73 std::string
getConfigVarTypename(const float &dummy
);
74 bool getConfigVarValue(CLuaObject
&luaValue
, float &dest
);
76 std::string
getConfigVarTypename(const double &dummy
);
77 bool getConfigVarValue(CLuaObject
&luaValue
, double &dest
);
79 std::string
getConfigVarTypename(const sint32
&dummy
);
80 bool getConfigVarValue(CLuaObject
&luaValue
, sint32
&dest
);
82 std::string
getConfigVarTypename(const std::string
&dummy
);
83 bool getConfigVarValue(CLuaObject
&luaValue
, std::string
&dest
);
85 std::string
getConfigVarTypename(const NLMISC::CRGBA
&dummy
);
86 bool getConfigVarValue(CLuaObject
&luaValue
, NLMISC::CRGBA
&dest
);
88 template <class T
> const T
&CConfigVar
<T
>::get() const
90 // Relies on R2 'getConfigVarValue' and 'getConfigVarTypename' functions specialization to retrieve the value (see below)
91 if (_TimeStamp
!= CConfigVarBase::getConfigFileTimeStamp())
93 const CLuaObject
&config
= getEditor().getConfig();
96 nlwarning("Trying to access the config file value '%s' of type '%s' from r2_config.lua, but the file hasn't been parsed yet, or its parsing failed, using default value.", _VarName
.c_str(), getConfigVarTypename(_Default
).c_str());
101 CLuaObject luaValue
= config
[_VarName
];
102 if (!getConfigVarValue(luaValue
, _Value
))
104 nlwarning("Can't retrieve value of R2 config var '%s', of type '%s', using default value", _VarName
.c_str(), getConfigVarTypename(_Default
).c_str());
108 _TimeStamp
= CConfigVarBase::getConfigFileTimeStamp();
113 //////////////////////
114 // Specialisations //
115 //////////////////////
117 //------------------------------------------------------------------------------------------------
119 inline std::string
getConfigVarTypename(const float &/* dummy */) { return "float"; }
120 inline bool getConfigVarValue(CLuaObject
&luaValue
, float &dest
)
122 if (luaValue
.isNumber())
124 dest
= (float) luaValue
.toNumber();
129 typedef CConfigVar
<float> CConfigVarFloat
;
131 //------------------------------------------------------------------------------------------------
133 inline std::string
getConfigVarTypename(const double &/* dummy */) { return "float"; }
134 inline bool getConfigVarValue(CLuaObject
&luaValue
, double &dest
)
136 if (luaValue
.isNumber())
138 dest
= luaValue
.toNumber();
143 typedef CConfigVar
<double> CConfigVarDouble
;
145 //------------------------------------------------------------------------------------------------
147 inline std::string
getConfigVarTypename(const sint32
&/* dummy */) { return "sint32"; }
148 inline bool getConfigVarValue(CLuaObject
&luaValue
, sint32
&dest
)
150 if (luaValue
.isInteger())
152 dest
= (sint32
) luaValue
.toInteger();
157 typedef CConfigVar
<sint32
> CConfigVarSInt32
;
160 //------------------------------------------------------------------------------------------------
162 inline std::string
getConfigVarTypename(const std::string
&/* dummy */) { return "string"; }
163 inline bool getConfigVarValue(CLuaObject
&luaValue
, std::string
&dest
)
165 if (luaValue
.isString())
167 dest
= luaValue
.toString();
172 typedef CConfigVar
<std::string
> CConfigVarString
;
174 //------------------------------------------------------------------------------------------------
176 inline std::string
getConfigVarTypename(const NLMISC::CRGBA
&/* dummy */) { return "rgba"; }
177 inline bool getConfigVarValue(CLuaObject
&luaValue
, NLMISC::CRGBA
&dest
)
179 if (luaValue
.isRGBA())
181 dest
= luaValue
.toRGBA();
186 typedef CConfigVar
<NLMISC::CRGBA
> CConfigVarRGBA
;