// ***************************************************************************
void CInterfaceConfig::streamToDataBase (NLMISC::IStream &f, uint32 uiDbSaveVersion)
{
	if (!f.isReading())
	{
		nlwarning("stream is not in reading mode");
		return;
	}

	sint32 begPos = f.getPos();
	f.seek (0, NLMISC::IStream::end);
	sint32 endPos = f.getPos();
	if ((begPos - endPos) == 0) return;
	f.seek (begPos, NLMISC::IStream::begin);

	CInterfaceManager *pIM = CInterfaceManager::getInstance();

	// Load branch of the database
	SDBLeaf leafTmp;
	CCDBNodeBranch *pDB = pIM->getDbBranch ("UI:SAVE");
	if (pDB != NULL)
	{
		// Number of leaf to save
		uint32 nbLeaves = 0;
		f.serial(nbLeaves);

		for (uint32 i = 0; i < nbLeaves; ++i)
		{
			f.serial(leafTmp);

			// If there is a define RESET_VER_dbName that exist for this DB, check if version is OK
			bool	wantRead= true;
			// Format dbName for version check
			string	defVerId= "RESET_VER_";
			defVerId+= leafTmp.Name;
			for(uint i=0;i<defVerId.size();i++)
			{
				if(defVerId[i]==':')
					defVerId[i]='_';
			}
			// check if exist
			if(pIM->isDefineExist(defVerId))
			{
				uint32	dbVer;
				fromString(pIM->getDefine(defVerId), dbVer);
				// if the version in the file is older than the version this db want, abort read
				if(uiDbSaveVersion<dbVer)
					wantRead= false;
			}

			// if want read the value from file, read it, else keep the default one
			if(wantRead)
			{
				CCDBNodeLeaf *pNL = pIM->getDbProp(leafTmp.Name,false);
				if (pNL != NULL)
					leafTmp.setTo(pNL);
			}
		}
	}
}
// ***************************************************************************
void CInterfaceConfig::CDesktopImage::read(NLMISC::IStream &f)
{
	nlassert(f.isReading());
	f.serialVersion(Version);
	f.serialCont(GCImages);
	// extra datas go until the end of stream
	sint32 begPos = f.getPos();
	f.seek (0, NLMISC::IStream::end);
	sint32 endPos = f.getPos();
	f.seek (begPos, NLMISC::IStream::begin);
	NLMISC::contReset(ExtraDatas);
	if (ExtraDatas.isReading())
	{
		ExtraDatas.invert();
	}
	sint32 length = endPos - begPos;
	if (length > 0)
	{
		uint8 *pBuffer = new uint8[length];
		f.serialBuffer(pBuffer, length); // read buffer from file
		ExtraDatas.serialBuffer(pBuffer, length); // copy buffer to memstream
		delete [] pBuffer;
	}
}
//--------------------------------------------------------------
//						CAISurvivalInstinctEvent::serial()  
//--------------------------------------------------------------
void CAISurvivalInstinctEvent::serial(NLMISC::IStream &f) throw(NLMISC::EStream)
{
	if (f.isReading() )
	{
	//	CAIEventType type;
		uint16 size;
	//	f.serial(type);
		f.serial(size);

		if (/*type != CAIEventType("SURVIE") ||*/ size != sizeof(CAISurvivalInstinctEvent))
		{
			CreatureId = NLMISC::CEntityId();
			EntityId = NLMISC::CEntityId();
			Modifier = 0;
			try
			{
				// seek takes a param in bytes (8 bits)
				f.seek( size, NLMISC::IStream::current);
			}
			catch(ESeekNotSupported &)
			{
				uint8 tmp;
				for (uint i = 0 ; i < size ; ++i)
					f.serial(tmp);
			}
		}
		else
		{
			f.serial(CreatureId);
			f.serial(EntityId);
			f.serial(Modifier);
		}
	}
	else
	{
		CAIEventType type("SURVIE");
		uint16 size = sizeof(CAISurvivalInstinctEvent);

		f.serial(type);
		f.serial(size);
		f.serial(CreatureId);
		f.serial(EntityId);
		f.serial(Modifier);
		
	}
} // CAISurvivalInstinctEvent::serial //