Merge branch '138-toggle-free-look-with-hotkey' into 'main/atys-live'
[ryzomcore.git] / nel / src / misc / cdb_leaf.cpp
blob51a634c4f2156276f5fdadd96f17c3a7a5bb201b
1 // Ryzom - MMORPG Framework <http://dev.ryzom.com/projects/ryzom/>
2 // Copyright (C) 2010 Winch Gate Property Limited
3 //
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>
7 //
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/>.
21 #include "stdmisc.h"
23 //#define TRACE_READ_DELTA
24 //#define TRACE_WRITE_DELTA
25 //#define TRACE_SET_VALUE
27 #define DELAYED_OBSERVERS
29 //////////////
30 // Includes //
31 //////////////
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>
37 ////////////////
38 // Namespaces //
39 ////////////////
40 using namespace std;
42 #ifdef DEBUG_NEW
43 #define new DEBUG_NEW
44 #endif
46 namespace NLMISC{
49 //-----------------------------------------------
50 // init
51 //-----------------------------------------------
52 void CCDBNodeLeaf::init( xmlNodePtr node, IProgressCallback &/* progressCallBack */, bool /* mapBanks */, CCDBBankHandler * /* bankHandler */ )
54 // Read nullable
55 CXMLAutoPtr nullable((const char*)xmlGetProp (node, (xmlChar*)"nullable"));
56 if ((const char *) nullable != NULL)
58 m_Nullable = (nullable.getDatas()[0] == '1');
60 else
62 m_Nullable = false;
65 // Read type
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'))
72 uint nbBit;
73 fromString((const char *) (type.getDatas() + 1), nbBit);
75 if(nbBit>=1 && nbBit<=64)
76 _Type=(ICDBNode::EPropType)nbBit;
77 else
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')
85 uint nbBit;
86 fromString((const char *) (type.getDatas() + 1), nbBit);
88 if(nbBit>=1 && nbBit<=64)
89 _Type = (ICDBNode::EPropType)(nbBit+64);
90 else
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;
96 // ELSE
97 else
99 // IF it is a TEXT.
100 if(!strcmp(type, "TEXT"))
101 _Type = ICDBNode::TEXT;
102 // ELSE type unknown.
103 else
105 nlwarning("CCDBNodeLeaf::init : type '%s' is unknown.", type.getDatas());
106 _Type = ICDBNode::UNKNOWN;
110 } // init //
113 //-----------------------------------------------
114 // getNode
116 //-----------------------------------------------
117 ICDBNode * CCDBNodeLeaf::getNode( uint16 /* idx */ )
119 return this;
120 } // getNode //
123 //-----------------------------------------------
124 // getNode
126 //-----------------------------------------------
127 ICDBNode * CCDBNodeLeaf::getNode (const CTextId& id, bool /* bCreate */)
129 if (_DBSM->localUnmap(_Name) == id.readNext())
131 if (id.size() == id.getCurrentIndex())
132 return this;
134 return NULL;
135 } // getNode //
137 //-----------------------------------------------
138 // write
140 //-----------------------------------------------
141 void CCDBNodeLeaf::write( CTextId& id, FILE * f)
143 fprintf(f,"%" NL_I64 "d\t%s\n",_Property,id.toString().c_str());
144 } // write //
146 //-----------------------------------------------
147 // readDelta
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.
155 uint64 recvd = 0;
157 uint64 isNull = 0;
158 if (m_Nullable)
160 f.serial(isNull, 1);
163 uint bits;
164 if (_Type == TEXT)
165 bits = 32;
166 else if (_Type <= I64)
167 bits = _Type;
168 else
169 bits = _Type - 64;
170 f.serial(recvd, bits);
173 // if the DB update is older than last DB update, abort (but after the read!!)
174 if(gc<_LastChangeGC)
175 return;
177 // bkup _oldProperty
178 _oldProperty = _Property;
180 // setup new one
181 _Property = (sint64)recvd;
183 // if signed
184 if (! ((_Type == TEXT) || (_Type <= I64)))
186 // extend bit sign
187 sint64 mask = (((sint64)1)<<bits)-(sint64)1;
188 if( (_Property >> (bits-1))==1 )
190 _Property |= ~mask;
193 if ( verboseDatabase )
195 nlinfo( "CDB: Read value (%u bits) %" NL_I64 "d", bits, _Property );
198 // bkup the date of change
199 _LastChangeGC= gc;
201 notifyObservers();
204 else
205 nlwarning("CCDBNodeLeaf::readDelta : Property Type Unknown ('%d') -> not serialized.", (uint)_Type);
206 }// readDelta //
209 //-----------------------------------------------
210 // resetData
211 //-----------------------------------------------
212 void CCDBNodeLeaf::resetData(TGameCycle gc, bool forceReset)
214 if(forceReset)
216 _LastChangeGC = 0;
217 setValue64(0);
219 else if (gc>=_LastChangeGC) // apply only if happens after the DB change
221 _LastChangeGC = gc;
222 setValue64(0);
225 // Same version but without observer notification:
226 // if ((!forceReset) && (gc<_LastChangeGC)) // if !forceReset, apply only if happens after the DB change
227 // return;
229 // if (forceReset)
230 // gc = 0;
232 // _LastChangeGC = gc;
233 // _oldProperty = _Property;
234 // if (_Property != 0)
235 // _Changed = true;
236 // _Property = 0;
239 //-----------------------------------------------
240 // getProp
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.
249 return getValue64();
250 } // getProp //
254 //-----------------------------------------------
255 // setProp
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())
266 return false;
268 // Set the property value (and set "_Changed" flag with 'true');
269 CCDBNodeLeaf::setValue64(value);
271 // Done
272 return true;
273 }// setProp //
276 //-----------------------------------------------
277 // setPropCheckGC
278 //-----------------------------------------------
279 bool CCDBNodeLeaf::setPropCheckGC(TGameCycle gc, sint64 value)
281 // Apply only if happens after the DB change
282 if(gc>=_LastChangeGC)
284 // new recent date
285 _LastChangeGC= gc;
287 // Set the property value (and set "_Changed" flag with 'true');
288 CCDBNodeLeaf::setValue64(value);
290 return true;
292 else
293 return false;
296 //-----------------------------------------------
297 // clear
299 //-----------------------------------------------
300 void CCDBNodeLeaf::clear()
303 } // clear //
307 //-----------------------------------------------
308 //-----------------------------------------------
309 void CCDBNodeLeaf::setValue64(sint64 prop)
311 if (_Property != prop)
313 if (!_Changed)
315 _Changed = true;
318 _oldProperty = _Property;
319 _Property = prop;
320 // notify observer
321 notifyObservers();
325 void CCDBNodeLeaf::setValue32(sint32 prop)
327 sint64 newVal = (sint64)prop;
328 setValue64(newVal);
331 void CCDBNodeLeaf::setValue16(sint16 prop)
333 sint64 newVal = (sint64)prop;
334 setValue64(newVal);
338 void CCDBNodeLeaf::setValue8(sint8 prop)
340 sint64 newVal = (sint64)prop;
341 setValue64(newVal);
344 void CCDBNodeLeaf::setValueBool(bool prop)
346 sint64 newVal = (sint64)prop;
347 setValue64(newVal);
350 void CCDBNodeLeaf::setValueRGBA (const CRGBA &color)
352 sint64 newVal = (uint32)(color.R+(color.G<<8)+(color.B<<16)+(color.A<<24));
353 setValue64(newVal);
357 void CCDBNodeLeaf::display(const std::string &prefix)
359 nlinfo("%sL %s", prefix.c_str(), _DBSM->localUnmap(_Name).c_str());
362 //-----------------------------------------------
363 // addObserver
365 //-----------------------------------------------
366 bool CCDBNodeLeaf::addObserver(IPropertyObserver* observer,CTextId& /* id */)
368 _Observers.push_back(observer);
369 return true;
372 //-----------------------------------------------
373 // removeObserver
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());
381 return true;
384 //-----------------------------------------------
385 void CCDBNodeLeaf::notifyObservers()
387 std::vector<IPropertyObserver*> obs = _Observers;
388 // notify observer
389 for (std::vector<IPropertyObserver*>::const_iterator it = obs.begin(); it != obs.end(); it++)
391 (*it)->update(this);
393 // mark parent branchs
394 if (_Parent)
395 _Parent->onLeafChanged( _Name );
403 #ifdef TRACE_READ_DELTA
404 #undef TRACE_READ_DELTA
405 #endif
407 #ifdef TRACE_WRITE_DELTA
408 #undef TRACE_WRITE_DELTA
409 #endif
411 #ifdef TRACE_SET_VALUE
412 #undef TRACE_SET_VALUE
413 #endif
414 //#############################################################################################