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>
6 // Copyright (C) 2016 Jan BOON (Kaetemi) <jan.boon@kaetemi.be>
8 // This program is free software: you can redistribute it and/or modify
9 // it under the terms of the GNU Affero General Public License as
10 // published by the Free Software Foundation, either version 3 of the
11 // License, or (at your option) any later version.
13 // This program is distributed in the hope that it will be useful,
14 // but WITHOUT ANY WARRANTY; without even the implied warranty of
15 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 // GNU Affero General Public License for more details.
18 // You should have received a copy of the GNU Affero General Public License
19 // along with this program. If not, see <http://www.gnu.org/licenses/>.
23 //#define TRACE_READ_DELTA
24 //#define TRACE_WRITE_DELTA
25 //#define TRACE_SET_VALUE
27 #define DELAYED_OBSERVERS
32 #include "nel/misc/cdb_leaf.h"
33 #include "nel/misc/xml_auto_ptr.h"
34 #include "nel/misc/bit_mem_stream.h"
35 //#include <iostream.h>
49 //-----------------------------------------------
51 //-----------------------------------------------
52 void CCDBNodeLeaf::init( xmlNodePtr node
, IProgressCallback
&/* progressCallBack */, bool /* mapBanks */, CCDBBankHandler
* /* bankHandler */ )
55 CXMLAutoPtr
nullable((const char*)xmlGetProp (node
, (xmlChar
*)"nullable"));
56 if ((const char *) nullable
!= NULL
)
58 m_Nullable
= (nullable
.getDatas()[0] == '1');
66 CXMLAutoPtr
type((const char*)xmlGetProp (node
, (xmlChar
*)"type"));
67 nlassert((const char *) type
!= NULL
);
69 // IF type is an INT with n bits [1,64].
70 if ((type
.getDatas()[0] == 'I') || (type
.getDatas()[0] == 'U'))
73 fromString((const char *) (type
.getDatas() + 1), nbBit
);
75 if(nbBit
>=1 && nbBit
<=64)
76 _Type
=(ICDBNode::EPropType
)nbBit
;
79 nlwarning("CCDBNodeLeaf::init : property is an INT and should be between [1,64] but it is %d bit(s).", nbBit
);
80 _Type
= ICDBNode::UNKNOWN
;
83 else if (type
.getDatas()[0] == 'S')
86 fromString((const char *) (type
.getDatas() + 1), nbBit
);
88 if(nbBit
>=1 && nbBit
<=64)
89 _Type
= (ICDBNode::EPropType
)(nbBit
+64);
92 nlwarning("CCDBNodeLeaf::init : property is an SINT and should be between [1,64] but it is %d bit(s).", nbBit
);
93 _Type
= ICDBNode::UNKNOWN
;
100 if(!strcmp(type
, "TEXT"))
101 _Type
= ICDBNode::TEXT
;
102 // ELSE type unknown.
105 nlwarning("CCDBNodeLeaf::init : type '%s' is unknown.", type
.getDatas());
106 _Type
= ICDBNode::UNKNOWN
;
113 //-----------------------------------------------
116 //-----------------------------------------------
117 ICDBNode
* CCDBNodeLeaf::getNode( uint16
/* idx */ )
123 //-----------------------------------------------
126 //-----------------------------------------------
127 ICDBNode
* CCDBNodeLeaf::getNode (const CTextId
& id
, bool /* bCreate */)
129 if (_DBSM
->localUnmap(_Name
) == id
.readNext())
131 if (id
.size() == id
.getCurrentIndex())
137 //-----------------------------------------------
140 //-----------------------------------------------
141 void CCDBNodeLeaf::write( CTextId
& id
, FILE * f
)
143 fprintf(f
,"%" NL_I64
"d\t%s\n",_Property
,id
.toString().c_str());
146 //-----------------------------------------------
148 //-----------------------------------------------
149 void CCDBNodeLeaf::readDelta(TGameCycle gc
, CBitMemStream
& f
)
151 // If the property Type is valid.
152 if(_Type
> UNKNOWN
&& _Type
< Nb_Prop_Type
)
154 // Read the Property Value according to the Property Type.
166 else if (_Type
<= I64
)
170 f
.serial(recvd
, bits
);
173 // if the DB update is older than last DB update, abort (but after the read!!)
178 _oldProperty
= _Property
;
181 _Property
= (sint64
)recvd
;
184 if (! ((_Type
== TEXT
) || (_Type
<= I64
)))
187 sint64 mask
= (((sint64
)1)<<bits
)-(sint64
)1;
188 if( (_Property
>> (bits
-1))==1 )
193 if ( verboseDatabase
)
195 nlinfo( "CDB: Read value (%u bits) %" NL_I64
"d", bits
, _Property
);
198 // bkup the date of change
205 nlwarning("CCDBNodeLeaf::readDelta : Property Type Unknown ('%d') -> not serialized.", (uint
)_Type
);
209 //-----------------------------------------------
211 //-----------------------------------------------
212 void CCDBNodeLeaf::resetData(TGameCycle gc
, bool forceReset
)
219 else if (gc
>=_LastChangeGC
) // apply only if happens after the DB change
225 // Same version but without observer notification:
226 // if ((!forceReset) && (gc<_LastChangeGC)) // if !forceReset, apply only if happens after the DB change
232 // _LastChangeGC = gc;
233 // _oldProperty = _Property;
234 // if (_Property != 0)
239 //-----------------------------------------------
242 //-----------------------------------------------
243 sint64
CCDBNodeLeaf::getProp( CTextId
& id
)
245 // assert that there are no lines left in the textid
246 nlassert( id
.getCurrentIndex() == id
.size() );
248 // Return the property value.
254 //-----------------------------------------------
256 // Set the value of a property (the update flag is set to true)
257 // \param id is the text id of the property/grp
258 // \param name is the name of the property
259 // \param value is the value of the property
260 // \return bool : 'false' if id is too long.
261 //-----------------------------------------------
262 bool CCDBNodeLeaf::setProp( CTextId
& id
, sint64 value
)
264 // assert that there are no lines left in the textid
265 if(id
.getCurrentIndex() != id
.size())
268 // Set the property value (and set "_Changed" flag with 'true');
269 CCDBNodeLeaf::setValue64(value
);
276 //-----------------------------------------------
278 //-----------------------------------------------
279 bool CCDBNodeLeaf::setPropCheckGC(TGameCycle gc
, sint64 value
)
281 // Apply only if happens after the DB change
282 if(gc
>=_LastChangeGC
)
287 // Set the property value (and set "_Changed" flag with 'true');
288 CCDBNodeLeaf::setValue64(value
);
296 //-----------------------------------------------
299 //-----------------------------------------------
300 void CCDBNodeLeaf::clear()
307 //-----------------------------------------------
308 //-----------------------------------------------
309 void CCDBNodeLeaf::setValue64(sint64 prop
)
311 if (_Property
!= prop
)
318 _oldProperty
= _Property
;
325 void CCDBNodeLeaf::setValue32(sint32 prop
)
327 sint64 newVal
= (sint64
)prop
;
331 void CCDBNodeLeaf::setValue16(sint16 prop
)
333 sint64 newVal
= (sint64
)prop
;
338 void CCDBNodeLeaf::setValue8(sint8 prop
)
340 sint64 newVal
= (sint64
)prop
;
344 void CCDBNodeLeaf::setValueBool(bool prop
)
346 sint64 newVal
= (sint64
)prop
;
350 void CCDBNodeLeaf::setValueRGBA (const CRGBA
&color
)
352 sint64 newVal
= (uint32
)(color
.R
+(color
.G
<<8)+(color
.B
<<16)+(color
.A
<<24));
357 void CCDBNodeLeaf::display(const std::string
&prefix
)
359 nlinfo("%sL %s", prefix
.c_str(), _DBSM
->localUnmap(_Name
).c_str());
362 //-----------------------------------------------
365 //-----------------------------------------------
366 bool CCDBNodeLeaf::addObserver(IPropertyObserver
* observer
,CTextId
& /* id */)
368 _Observers
.push_back(observer
);
372 //-----------------------------------------------
375 //-----------------------------------------------
376 bool CCDBNodeLeaf::removeObserver(IPropertyObserver
* observer
, CTextId
& /* id */)
378 std::vector
<IPropertyObserver
*>::iterator endIt
= std::remove(_Observers
.begin(), _Observers
.end(), observer
);
379 if (endIt
== _Observers
.end()) return false; // no observer has been removed..
380 _Observers
.erase(endIt
, _Observers
.end());
384 //-----------------------------------------------
385 void CCDBNodeLeaf::notifyObservers()
387 std::vector
<IPropertyObserver
*> obs
= _Observers
;
389 for (std::vector
<IPropertyObserver
*>::const_iterator it
= obs
.begin(); it
!= obs
.end(); it
++)
393 // mark parent branchs
395 _Parent
->onLeafChanged( _Name
);
403 #ifdef TRACE_READ_DELTA
404 #undef TRACE_READ_DELTA
407 #ifdef TRACE_WRITE_DELTA
408 #undef TRACE_WRITE_DELTA
411 #ifdef TRACE_SET_VALUE
412 #undef TRACE_SET_VALUE
414 //#############################################################################################