You cannot select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
361 lines
7.9 KiB
C++
361 lines
7.9 KiB
C++
13 years ago
|
// Ryzom - MMORPG Framework <http://dev.ryzom.com/projects/ryzom/>
|
||
|
// Copyright (C) 2010 Winch Gate Property Limited
|
||
|
//
|
||
5 years ago
|
// This source file has been modified by the following contributors:
|
||
|
// Copyright (C) 2013 Laszlo KIS-ADAM (dfighter) <dfighter1985@gmail.com>
|
||
|
// Copyright (C) 2013-2016 Jan BOON (Kaetemi) <jan.boon@kaetemi.be>
|
||
|
//
|
||
13 years ago
|
// This program is free software: you can redistribute it and/or modify
|
||
|
// it under the terms of the GNU Affero General Public License as
|
||
|
// published by the Free Software Foundation, either version 3 of the
|
||
|
// License, or (at your option) any later version.
|
||
|
//
|
||
|
// This program is distributed in the hope that it will be useful,
|
||
|
// but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||
|
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
||
|
// GNU Affero General Public License for more details.
|
||
|
//
|
||
|
// You should have received a copy of the GNU Affero General Public License
|
||
|
// along with this program. If not, see <http://www.gnu.org/licenses/>.
|
||
|
|
||
|
|
||
12 years ago
|
#include "stdpch.h"
|
||
13 years ago
|
|
||
|
//#define TRACE_READ_DELTA
|
||
|
//#define TRACE_WRITE_DELTA
|
||
|
//#define TRACE_SET_VALUE
|
||
|
|
||
|
|
||
|
//////////////
|
||
|
// Includes //
|
||
|
//////////////
|
||
|
#include "nel/misc/types_nl.h"
|
||
|
#include "nel/misc/debug.h"
|
||
|
#include "nel/misc/file.h"
|
||
|
#include "nel/misc/i_xml.h"
|
||
|
|
||
|
#include "cdb_synchronised.h"
|
||
|
|
||
|
#include "interface_v3/interface_manager.h"
|
||
|
|
||
|
//#include <iostream.h>
|
||
|
#include <libxml/parser.h>
|
||
|
#include <fcntl.h>
|
||
|
#include <string.h>
|
||
|
|
||
|
#include <string>
|
||
|
|
||
10 years ago
|
#include "game_share/ryzom_database_banks.h"
|
||
12 years ago
|
|
||
13 years ago
|
|
||
|
////////////////
|
||
|
// Namespaces //
|
||
|
////////////////
|
||
|
using namespace NLMISC;
|
||
|
using namespace std;
|
||
|
|
||
|
uint32 NbDatabaseChanges = 0;
|
||
|
|
||
|
|
||
|
//-----------------------------------------------
|
||
|
// CCDBSynchronised
|
||
|
//
|
||
|
//-----------------------------------------------
|
||
9 years ago
|
CCDBSynchronised::CCDBSynchronised() : CCDBManager("SERVER", NB_CDB_BANKS), _InitInProgress(true), _InitDeltaReceived(0)
|
||
13 years ago
|
{
|
||
|
}
|
||
|
|
||
12 years ago
|
extern const char *CDBBankNames[INVALID_CDB_BANK+1];
|
||
13 years ago
|
|
||
|
//-----------------------------------------------
|
||
|
// init
|
||
|
//
|
||
|
//-----------------------------------------------
|
||
|
void CCDBSynchronised::init( const string &fileName, NLMISC::IProgressCallback &progressCallBack )
|
||
|
{
|
||
|
try
|
||
|
{
|
||
|
CIFile file;
|
||
|
if (file.open (fileName))
|
||
|
{
|
||
|
// Init an xml stream
|
||
|
CIXml read;
|
||
|
read.init (file);
|
||
|
|
||
|
//Parse the parser output!!!
|
||
12 years ago
|
bankHandler.resetNodeBankMapping(); // in case the game is restarted from start
|
||
|
bankHandler.fillBankNames( CDBBankNames, INVALID_CDB_BANK + 1 );
|
||
|
if( _Database == NULL )
|
||
|
_Database = new CCDBNodeBranch( "SERVER" );
|
||
|
_Database->init( read.getRootNode (), progressCallBack, true, &bankHandler );
|
||
13 years ago
|
}
|
||
|
}
|
||
|
catch (const Exception &e)
|
||
|
{
|
||
|
// Output error
|
||
|
nlwarning ("CFormLoader: Error while loading the form %s: %s", fileName.c_str(), e.what());
|
||
|
}
|
||
|
}
|
||
|
|
||
|
|
||
|
//-----------------------------------------------
|
||
|
// read
|
||
|
//
|
||
|
//-----------------------------------------------
|
||
|
void CCDBSynchronised::read( const string &fileName )
|
||
|
{
|
||
|
#ifdef _DEBUG
|
||
|
int linecount=1;
|
||
|
#endif
|
||
|
|
||
9 years ago
|
if (_Database == NULL)
|
||
13 years ago
|
{
|
||
|
throw CCDBSynchronised::EDBNotInit();
|
||
|
}
|
||
|
|
||
9 years ago
|
CIFile f;
|
||
|
|
||
|
if (!f.open(fileName, true))
|
||
13 years ago
|
{
|
||
|
nlerror("can't open file : %s\n", fileName.c_str());
|
||
|
}
|
||
|
|
||
9 years ago
|
while(!f.eof())
|
||
13 years ago
|
{
|
||
9 years ago
|
char line[1024];
|
||
|
f.getline(line, 1024);
|
||
|
|
||
13 years ago
|
#ifdef _DEBUG
|
||
9 years ago
|
nlinfo("%s:%i", fileName.c_str(), linecount);
|
||
|
linecount++;
|
||
13 years ago
|
#endif
|
||
|
|
||
|
char * token;
|
||
9 years ago
|
char * buffer = new char[strlen(line)+1];
|
||
|
strcpy(buffer, line);
|
||
13 years ago
|
|
||
|
// value
|
||
|
token = strtok(buffer," \t");
|
||
9 years ago
|
|
||
|
if (token)
|
||
|
{
|
||
|
sint64 value;
|
||
|
fromString((const char*)token, value);
|
||
|
|
||
|
// property name
|
||
|
token = strtok(NULL," \n");
|
||
|
|
||
|
if (token)
|
||
|
{
|
||
|
string propName(token);
|
||
|
|
||
|
// set the value of the property
|
||
|
ICDBNode::CTextId txtId(propName);
|
||
|
_Database->setProp(txtId, value);
|
||
|
}
|
||
|
}
|
||
|
|
||
|
delete [] buffer;
|
||
13 years ago
|
}
|
||
|
|
||
|
f.close();
|
||
|
} // read //
|
||
|
|
||
|
|
||
|
|
||
|
//-----------------------------------------------
|
||
|
// write
|
||
|
//
|
||
|
//-----------------------------------------------
|
||
|
void CCDBSynchronised::write( const string &fileName )
|
||
|
{
|
||
9 years ago
|
bool res = false;
|
||
|
|
||
9 years ago
|
if (_Database != 0)
|
||
13 years ago
|
{
|
||
9 years ago
|
FILE * f = nlfopen(fileName, "w");
|
||
|
if (f)
|
||
|
{
|
||
|
ICDBNode::CTextId id;
|
||
|
_Database->write(id,f);
|
||
|
fclose(f);
|
||
|
|
||
|
res = true;
|
||
|
}
|
||
13 years ago
|
}
|
||
9 years ago
|
|
||
|
if (!res)
|
||
13 years ago
|
{
|
||
|
nlwarning("<CCDBSynchronised::write> can't write %s : the database has not been initialized",fileName.c_str());
|
||
|
}
|
||
|
} // write //
|
||
|
|
||
|
|
||
|
//-----------------------------------------------
|
||
|
// readDelta
|
||
|
//
|
||
|
//-----------------------------------------------
|
||
12 years ago
|
void CCDBSynchronised::readDelta( NLMISC::TGameCycle gc, CBitMemStream& s, uint bank )
|
||
13 years ago
|
{
|
||
|
nldebug("Update DB");
|
||
|
|
||
|
if( _Database == 0 )
|
||
|
{
|
||
|
nlwarning("<CCDBSynchronised::readDelta> the database has not been initialized");
|
||
|
return;
|
||
|
}
|
||
|
|
||
|
//displayBitStream2( f, f.getPosInBit(), f.getPosInBit() + 64 );
|
||
|
uint16 propertyCount = 0;
|
||
|
s.serial( propertyCount );
|
||
|
|
||
12 years ago
|
if ( NLMISC::ICDBNode::isDatabaseVerbose() )
|
||
13 years ago
|
nlinfo( "CDB: Reading delta (%hu changes)", propertyCount );
|
||
|
NbDatabaseChanges += propertyCount;
|
||
|
|
||
|
for( uint i=0; i!=propertyCount; ++i )
|
||
|
{
|
||
12 years ago
|
_Database->readAndMapDelta( gc, s, bank, &bankHandler );
|
||
13 years ago
|
}
|
||
|
|
||
|
/*// Read "client only" property changes
|
||
|
bool hasCOPropChanges;
|
||
|
s.serialBit( hasCOPropChanges );
|
||
|
if ( hasCOPropChanges )
|
||
|
{
|
||
|
uint32 nbCOPropChanges;
|
||
|
s.serial( nbCOPropChanges, 4 );
|
||
|
if ( nbCOPropChanges == 15 )
|
||
|
{
|
||
|
s.serial( nbCOPropChanges );
|
||
|
}
|
||
|
for ( uint i=0; i!=nbCOPropChanges; ++i )
|
||
|
{
|
||
|
string propName; //TEMP
|
||
|
sint64 value; //TEMP
|
||
|
s.serial( propName );
|
||
|
s.serial( value );
|
||
|
setProp( propName, value );
|
||
|
}
|
||
|
}*/
|
||
|
|
||
|
} // readDelta //
|
||
|
|
||
|
|
||
|
//-----------------------------------------------
|
||
|
// getProp
|
||
|
//
|
||
|
//-----------------------------------------------
|
||
|
sint64 CCDBSynchronised::getProp( const string &name )
|
||
|
{
|
||
|
if( _Database != 0 )
|
||
|
{
|
||
|
ICDBNode::CTextId txtId( name );
|
||
|
return _Database->getProp( txtId );
|
||
|
}
|
||
|
else
|
||
|
throw CCDBSynchronised::EDBNotInit();
|
||
|
} // getProp //
|
||
|
|
||
|
|
||
|
//-----------------------------------------------
|
||
|
// setProp
|
||
|
// Set the value of a property (the update flag is set to true)
|
||
|
// \param name is the name of the property
|
||
|
// \param value is the value of the property
|
||
|
// \return bool : 'true' if the property was found.
|
||
|
//-----------------------------------------------
|
||
|
bool CCDBSynchronised::setProp(const string &name, sint64 value)
|
||
|
{
|
||
|
if(_Database == 0)
|
||
|
{
|
||
|
nlwarning("<CCDBSynchronised::setProp> the database has not been initialized");
|
||
|
return false;
|
||
|
}
|
||
|
|
||
|
#ifdef TRACE_SET_VALUE
|
||
9 years ago
|
nlinfo("Set value %" NL_I64 "d for Prop %s", value,name.c_str() );
|
||
13 years ago
|
#endif
|
||
|
|
||
|
// Set the property.
|
||
|
ICDBNode::CTextId txtId( name );
|
||
|
return _Database->setProp( txtId, value );
|
||
|
} // setProp //
|
||
|
|
||
|
|
||
|
|
||
|
//-----------------------------------------------
|
||
|
// getString
|
||
|
//
|
||
|
//-----------------------------------------------
|
||
|
string CCDBSynchronised::getString( uint32 id )
|
||
|
{
|
||
|
map<uint32,string>::iterator itStr = _Strings.find( id );
|
||
|
if( itStr != _Strings.end() )
|
||
|
{
|
||
|
return (*itStr).second;
|
||
|
}
|
||
9 years ago
|
|
||
|
nlwarning("<CCDBSynchronised::getString> string with id %d was not found",id);
|
||
|
return "";
|
||
13 years ago
|
} // getString //
|
||
|
|
||
|
|
||
|
|
||
|
//-----------------------------------------------
|
||
|
// setString
|
||
|
//
|
||
|
//-----------------------------------------------
|
||
|
void CCDBSynchronised::setString( uint32 id, const std::string &str )
|
||
|
{
|
||
|
_Strings[id]=str;
|
||
|
}
|
||
|
|
||
|
|
||
|
//-----------------------------------------------
|
||
|
// clear
|
||
|
//
|
||
|
//-----------------------------------------------
|
||
|
void CCDBSynchronised::clear()
|
||
|
{
|
||
|
if(_Database)
|
||
|
{
|
||
|
_Database->clear();
|
||
|
delete _Database;
|
||
12 years ago
|
_Database = NULL;
|
||
13 years ago
|
}
|
||
|
|
||
|
// clear CCDBNodeBranch static data
|
||
12 years ago
|
branchObservingHandler.reset();
|
||
|
bankHandler.reset();
|
||
13 years ago
|
}
|
||
|
|
||
|
|
||
|
void CCDBSynchronised::writeInitInProgressIntoUIDB()
|
||
|
{
|
||
|
CInterfaceManager *pIM = CInterfaceManager::getInstance();
|
||
|
if (pIM)
|
||
12 years ago
|
{
|
||
|
NLMISC::CCDBNodeLeaf *node = m_CDBInitInProgressDB ? (&*m_CDBInitInProgressDB)
|
||
12 years ago
|
: &*(m_CDBInitInProgressDB = NLGUI::CDBManager::getInstance()->getDbProp("UI:VARIABLES:CDB_INIT_IN_PROGRESS"));
|
||
12 years ago
|
node->setValueBool(_InitInProgress);
|
||
|
}
|
||
13 years ago
|
else
|
||
|
nlwarning("InterfaceManager not created");
|
||
|
}
|
||
|
|
||
|
|
||
|
#ifdef TRACE_READ_DELTA
|
||
|
#undef TRACE_READ_DELTA
|
||
|
#endif
|
||
|
|
||
|
#ifdef TRACE_WRITE_DELTA
|
||
|
#undef TRACE_WRITE_DELTA
|
||
|
#endif
|
||
|
|
||
|
#ifdef TRACE_SET_VALUE
|
||
|
#undef TRACE_SET_VALUE
|
||
|
#endif
|
||
|
|
||
|
|