Merge branch '164-crash-on-patching-and-possibly-right-after-login' into main/gingo...
[ryzomcore.git] / ryzom / client / src / cdb_synchronised.cpp
blobf947d369630509ca80051b0913d8a663354ab910
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) 2013-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/>.
22 #include "stdpch.h"
24 //#define TRACE_READ_DELTA
25 //#define TRACE_WRITE_DELTA
26 //#define TRACE_SET_VALUE
29 //////////////
30 // Includes //
31 //////////////
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>
43 #include <fcntl.h>
44 #include <string.h>
46 #include <string>
48 #include "game_share/ryzom_database_banks.h"
51 ////////////////
52 // Namespaces //
53 ////////////////
54 using namespace NLMISC;
55 using namespace std;
57 uint32 NbDatabaseChanges = 0;
60 //-----------------------------------------------
61 // CCDBSynchronised
63 //-----------------------------------------------
64 CCDBSynchronised::CCDBSynchronised() : CCDBManager("SERVER", NB_CDB_BANKS), _InitInProgress(true), _InitDeltaReceived(0)
68 extern const char *CDBBankNames[INVALID_CDB_BANK+1];
70 //-----------------------------------------------
71 // init
73 //-----------------------------------------------
74 void CCDBSynchronised::init( const string &fileName, NLMISC::IProgressCallback &progressCallBack )
76 try
78 CIFile file;
79 if (file.open (fileName))
81 // Init an xml stream
82 CIXml read;
83 read.init (file);
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)
95 // Output error
96 nlwarning ("CFormLoader: Error while loading the form %s: %s", fileName.c_str(), e.what());
101 //-----------------------------------------------
102 // read
104 //-----------------------------------------------
105 void CCDBSynchronised::read( const string &fileName )
107 #ifdef _DEBUG
108 int linecount=1;
109 #endif
111 if (_Database == NULL)
113 throw CCDBSynchronised::EDBNotInit();
116 CIFile f;
118 if (!f.open(fileName, true))
120 nlerror("can't open file : %s\n", fileName.c_str());
123 while(!f.eof())
125 char line[1024];
126 f.getline(line, 1024);
128 #ifdef _DEBUG
129 nlinfo("%s:%i", fileName.c_str(), linecount);
130 linecount++;
131 #endif
133 char * token;
134 char * buffer = new char[strlen(line)+1];
135 strcpy(buffer, line);
137 // value
138 token = strtok(buffer," \t");
140 if (token)
142 sint64 value;
143 fromString((const char*)token, value);
145 // property name
146 token = strtok(NULL," \n");
148 if (token)
150 string propName(token);
152 // set the value of the property
153 ICDBNode::CTextId txtId(propName);
154 _Database->setProp(txtId, value);
158 delete [] buffer;
161 f.close();
162 } // read //
166 //-----------------------------------------------
167 // write
169 //-----------------------------------------------
170 void CCDBSynchronised::write( const string &fileName )
172 bool res = false;
174 if (_Database != 0)
176 FILE * f = nlfopen(fileName, "w");
177 if (f)
179 ICDBNode::CTextId id;
180 _Database->write(id,f);
181 fclose(f);
183 res = true;
187 if (!res)
189 nlwarning("<CCDBSynchronised::write> can't write %s : the database has not been initialized",fileName.c_str());
191 } // write //
194 //-----------------------------------------------
195 // readDelta
197 //-----------------------------------------------
198 void CCDBSynchronised::readDelta( NLMISC::TGameCycle gc, CBitMemStream& s, uint bank )
200 nldebug("Update DB");
202 if( _Database == 0 )
204 nlwarning("<CCDBSynchronised::readDelta> the database has not been initialized");
205 return;
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
235 sint64 value; //TEMP
236 s.serial( propName );
237 s.serial( value );
238 setProp( propName, value );
242 } // readDelta //
245 //-----------------------------------------------
246 // getProp
248 //-----------------------------------------------
249 sint64 CCDBSynchronised::getProp( const string &name )
251 if( _Database != 0 )
253 ICDBNode::CTextId txtId( name );
254 return _Database->getProp( txtId );
256 else
257 throw CCDBSynchronised::EDBNotInit();
258 } // getProp //
261 //-----------------------------------------------
262 // setProp
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)
270 if(_Database == 0)
272 nlwarning("<CCDBSynchronised::setProp> the database has not been initialized");
273 return false;
276 #ifdef TRACE_SET_VALUE
277 nlinfo("Set value %" NL_I64 "d for Prop %s", value,name.c_str() );
278 #endif
280 // Set the property.
281 ICDBNode::CTextId txtId( name );
282 return _Database->setProp( txtId, value );
283 } // setProp //
287 //-----------------------------------------------
288 // getString
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);
300 return "";
301 } // getString //
305 //-----------------------------------------------
306 // setString
308 //-----------------------------------------------
309 void CCDBSynchronised::setString( uint32 id, const std::string &str )
311 _Strings[id]=str;
315 //-----------------------------------------------
316 // clear
318 //-----------------------------------------------
319 void CCDBSynchronised::clear()
321 if(_Database)
323 _Database->clear();
324 delete _Database;
325 _Database = NULL;
328 // clear CCDBNodeBranch static data
329 branchObservingHandler.reset();
330 bankHandler.reset();
334 void CCDBSynchronised::writeInitInProgressIntoUIDB()
336 CInterfaceManager *pIM = CInterfaceManager::getInstance();
337 if (pIM)
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);
343 else
344 nlwarning("InterfaceManager not created");
348 #ifdef TRACE_READ_DELTA
349 #undef TRACE_READ_DELTA
350 #endif
352 #ifdef TRACE_WRITE_DELTA
353 #undef TRACE_WRITE_DELTA
354 #endif
356 #ifdef TRACE_SET_VALUE
357 #undef TRACE_SET_VALUE
358 #endif