bool DBSystem::LoadDBFile()
{
	do 
	{
		char*        pBuffer = NULL;
		unsigned int iLength = 0;
		if (!EngineSystem::LoadAppFile("resource/data/game.data", pBuffer, iLength))
		{
			LOGGERSYSTEM->LogFatal("Init DBSystem Failed, DB Data Invalid\n");
			return false;
		}

		std::string buffer = "";
		if (EngineSystem::IsAndroid480X800())
		{
			buffer = "d42fcd0a5e6145fd988b55dc0570bb82";
		}
		else
		{
			buffer = "3ecae97470b968ec37bb640246a425e9";
		}

		// 进行静态数据md5码校验
		std::string md5Key = MD5Crypto::md5(pBuffer, iLength);
		if (md5Key.compare(buffer) != 0)
		{
			delete []pBuffer;
			LOGGERSYSTEM->LogFatal("Init DBSystem Failed, DB Data Invalid2\n");
			return false;
		}

		// 保存到SD卡对应目录下面
		std::string dbFile = EngineSystem::GetDocumentDir()+"game.db";
		FileLoader loader;
		if (!loader.load((char*)dbFile.c_str(), FileLoader::_TYPED_CLEAR_WRITE_))
		{
			loader.unload();
			delete []pBuffer;
			return false;
		}

		loader.write(pBuffer, iLength);
		loader.unload();
		delete []pBuffer;

		int ret = sqlite3_open(dbFile.c_str(), &m_pSQLite);
		if (ret != SQLITE_OK)
		{
			LOGGERSYSTEM->LogFatal("Init DBSystem Failed, DBFile=%s\n", dbFile.c_str());
			return false;
		}

		return true;
	} 
	while(false);

	return false;
}
void LocalPlayerDataManager::SaveToFile()
{
	// 设置当前保存的时间
	m_PlayerLastLogoutTime = TimeSystem::GetCurrentMicroSecond();

	// 版本号长度
	int versionLen = NinjiaTribeApp::get_instance2()->GetVersion().length();

	EPacket packet;
	// 插入版本信息
	packet<<versionLen;
	packet.append((char*)NinjiaTribeApp::get_instance2()->GetVersion().c_str(), versionLen);

	// 保存当前时间信息
	packet<<m_PlayerLastLogoutTime;

	// 保存基本信息
	packet<<m_LocalCurrentPlayerId;
	packet<<m_LocalRubyNum;
	packet<<m_LocalGoldNum;
	
	int num = m_LocalHeroStateList.size();
	packet<<num;
	if (num > 0)
	{
		std::list<_LOCAL_HERO_STATE_>::iterator iter   = m_LocalHeroStateList.begin();
		std::list<_LOCAL_HERO_STATE_>::iterator iter_e = m_LocalHeroStateList.end();
		for ( ; iter!=iter_e; ++iter)
		{	
			_LOCAL_HERO_STATE_ temp = (*iter);
			packet.append<_LOCAL_HERO_STATE_>(temp);
		}
	}


	// 增加md5存档校验码
	std::string md5Key = MD5Crypto::md5((unsigned char*)packet.get_packet_buff(), packet.size());
	packet.append((char*)md5Key.c_str(), md5Key.length());

	// 更新备份文件
	try
	{
		do 
		{
			std::string filePath = EngineSystem::GetDocumentDir();
			filePath += GetName();
			FileLoader loader;
			if (!loader.load((char*)filePath.c_str(), FileLoader::_TYPED_READ_ONLY_))
			{
				loader.unload();
				break;
			}

			int fileLength = loader.length();
			char* pBuffer = new char[fileLength+1];
			memset(pBuffer, 0, fileLength+1);
			loader.read(pBuffer, fileLength);
			loader.unload();

			filePath = EngineSystem::GetDocumentDir();
			filePath += GetTempName();
			if (!loader.load((char*)filePath.c_str(), FileLoader::_TYPED_CLEAR_WRITE_))
			{
				loader.unload();
				delete []pBuffer;
				break;
			}

			loader.write(pBuffer, fileLength);
			loader.unload();
			delete []pBuffer;
		}
		while (false);
	}
	catch (...)
	{
		;
	}

	try
	{
		// 先进行解密
		DBSYSTEM->XORBuffer((char*)DBSYSTEM->GetLocalDataEncryptionKey().c_str(), packet.get_packet_buff(), packet.size());

		std::string filePath = EngineSystem::GetDocumentDir();
		filePath += GetName();

		FileLoader loader;
		if (!loader.load((char*)filePath.c_str(), FileLoader::_TYPED_CLEAR_WRITE_))
			return ;

		loader.write(packet.get_packet_buff(), packet.size());
		loader.unload();
	}
	catch (...)
	{
		;
	}
}