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) 2013-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/>.
24 //#define TRACE_READ_DELTA
25 //#define TRACE_WRITE_DELTA
26 //#define TRACE_SET_VALUE
32 #include "nel/misc/types_nl.h"
33 #include "nel/misc/debug.h"
34 #include "nel/misc/file.h"
35 #include "nel/misc/i_xml.h"
37 #include "cdb_synchronised.h"
39 #include "interface_v3/interface_manager.h"
41 //#include <iostream.h>
42 #include <libxml/parser.h>
48 #include "game_share/ryzom_database_banks.h"
54 using namespace NLMISC
;
57 uint32 NbDatabaseChanges
= 0;
60 //-----------------------------------------------
63 //-----------------------------------------------
64 CCDBSynchronised::CCDBSynchronised() : CCDBManager("SERVER", NB_CDB_BANKS
), _InitInProgress(true), _InitDeltaReceived(0)
68 extern const char *CDBBankNames
[INVALID_CDB_BANK
+1];
70 //-----------------------------------------------
73 //-----------------------------------------------
74 void CCDBSynchronised::init( const string
&fileName
, NLMISC::IProgressCallback
&progressCallBack
)
79 if (file
.open (fileName
))
85 //Parse the parser output!!!
86 bankHandler
.resetNodeBankMapping(); // in case the game is restarted from start
87 bankHandler
.fillBankNames( CDBBankNames
, INVALID_CDB_BANK
+ 1 );
88 if( _Database
== NULL
)
89 _Database
= new CCDBNodeBranch( "SERVER" );
90 _Database
->init( read
.getRootNode (), progressCallBack
, true, &bankHandler
);
93 catch (const Exception
&e
)
96 nlwarning ("CFormLoader: Error while loading the form %s: %s", fileName
.c_str(), e
.what());
101 //-----------------------------------------------
104 //-----------------------------------------------
105 void CCDBSynchronised::read( const string
&fileName
)
111 if (_Database
== NULL
)
113 throw CCDBSynchronised::EDBNotInit();
118 if (!f
.open(fileName
, true))
120 nlerror("can't open file : %s\n", fileName
.c_str());
126 f
.getline(line
, 1024);
129 nlinfo("%s:%i", fileName
.c_str(), linecount
);
134 char * buffer
= new char[strlen(line
)+1];
135 strcpy(buffer
, line
);
138 token
= strtok(buffer
," \t");
143 fromString((const char*)token
, value
);
146 token
= strtok(NULL
," \n");
150 string
propName(token
);
152 // set the value of the property
153 ICDBNode::CTextId
txtId(propName
);
154 _Database
->setProp(txtId
, value
);
166 //-----------------------------------------------
169 //-----------------------------------------------
170 void CCDBSynchronised::write( const string
&fileName
)
176 FILE * f
= nlfopen(fileName
, "w");
179 ICDBNode::CTextId id
;
180 _Database
->write(id
,f
);
189 nlwarning("<CCDBSynchronised::write> can't write %s : the database has not been initialized",fileName
.c_str());
194 //-----------------------------------------------
197 //-----------------------------------------------
198 void CCDBSynchronised::readDelta( NLMISC::TGameCycle gc
, CBitMemStream
& s
, uint bank
)
200 nldebug("Update DB");
204 nlwarning("<CCDBSynchronised::readDelta> the database has not been initialized");
208 //displayBitStream2( f, f.getPosInBit(), f.getPosInBit() + 64 );
209 uint16 propertyCount
= 0;
210 s
.serial( propertyCount
);
212 if ( NLMISC::ICDBNode::isDatabaseVerbose() )
213 nlinfo( "CDB: Reading delta (%hu changes)", propertyCount
);
214 NbDatabaseChanges
+= propertyCount
;
216 for( uint i
=0; i
!=propertyCount
; ++i
)
218 _Database
->readAndMapDelta( gc
, s
, bank
, &bankHandler
);
221 /*// Read "client only" property changes
222 bool hasCOPropChanges;
223 s.serialBit( hasCOPropChanges );
224 if ( hasCOPropChanges )
226 uint32 nbCOPropChanges;
227 s.serial( nbCOPropChanges, 4 );
228 if ( nbCOPropChanges == 15 )
230 s.serial( nbCOPropChanges );
232 for ( uint i=0; i!=nbCOPropChanges; ++i )
234 string propName; //TEMP
236 s.serial( propName );
238 setProp( propName, value );
245 //-----------------------------------------------
248 //-----------------------------------------------
249 sint64
CCDBSynchronised::getProp( const string
&name
)
253 ICDBNode::CTextId
txtId( name
);
254 return _Database
->getProp( txtId
);
257 throw CCDBSynchronised::EDBNotInit();
261 //-----------------------------------------------
263 // Set the value of a property (the update flag is set to true)
264 // \param name is the name of the property
265 // \param value is the value of the property
266 // \return bool : 'true' if the property was found.
267 //-----------------------------------------------
268 bool CCDBSynchronised::setProp(const string
&name
, sint64 value
)
272 nlwarning("<CCDBSynchronised::setProp> the database has not been initialized");
276 #ifdef TRACE_SET_VALUE
277 nlinfo("Set value %" NL_I64
"d for Prop %s", value
,name
.c_str() );
281 ICDBNode::CTextId
txtId( name
);
282 return _Database
->setProp( txtId
, value
);
287 //-----------------------------------------------
290 //-----------------------------------------------
291 string
CCDBSynchronised::getString( uint32 id
)
293 map
<uint32
,string
>::iterator itStr
= _Strings
.find( id
);
294 if( itStr
!= _Strings
.end() )
296 return (*itStr
).second
;
299 nlwarning("<CCDBSynchronised::getString> string with id %d was not found",id
);
305 //-----------------------------------------------
308 //-----------------------------------------------
309 void CCDBSynchronised::setString( uint32 id
, const std::string
&str
)
315 //-----------------------------------------------
318 //-----------------------------------------------
319 void CCDBSynchronised::clear()
328 // clear CCDBNodeBranch static data
329 branchObservingHandler
.reset();
334 void CCDBSynchronised::writeInitInProgressIntoUIDB()
336 CInterfaceManager
*pIM
= CInterfaceManager::getInstance();
339 NLMISC::CCDBNodeLeaf
*node
= m_CDBInitInProgressDB
? (&*m_CDBInitInProgressDB
)
340 : &*(m_CDBInitInProgressDB
= NLGUI::CDBManager::getInstance()->getDbProp("UI:VARIABLES:CDB_INIT_IN_PROGRESS"));
341 node
->setValueBool(_InitInProgress
);
344 nlwarning("InterfaceManager not created");
348 #ifdef TRACE_READ_DELTA
349 #undef TRACE_READ_DELTA
352 #ifdef TRACE_WRITE_DELTA
353 #undef TRACE_WRITE_DELTA
356 #ifdef TRACE_SET_VALUE
357 #undef TRACE_SET_VALUE