char* LocalPlayerDataManager::LoadDBFile(std::string fileName, long& length)
{
	FileLoader loader;
	if (!loader.load((char*)fileName.c_str(), FileLoader::_TYPED_READ_ONLY_))
	{
		loader.unload();
		return NULL;
	}

	length = loader.length();
	if (length <= (sizeof(char)*32+sizeof(int)))
	{
		loader.unload();
		return NULL;
	}
	
	// 读取文件信息
	char* pBuffer = new char[length+1];
	memset(pBuffer, 0, length+1);
	loader.read(pBuffer, length);
	loader.unload();

	// 先进行解密
	DBSYSTEM->XORBuffer((char*)DBSYSTEM->GetLocalDataEncryptionKey().c_str(), pBuffer, length);

	// 判断文件是否完整
	std::string md5Key = MD5Crypto::md5((unsigned char*)pBuffer, length-32);
	if (md5Key.compare(pBuffer+length-32) != 0)
	{
		delete[] pBuffer;
		return NULL;
	}

	return pBuffer;
}
bool EngineSystem::LoadTempFile(char* pFileName, char*& pBuffer, unsigned int& iLength)
{
	assert(pFileName != NULL);
	assert(strlen(pFileName) > 0);

	std::string filepath = EngineSystem::GetDocumentDir();
	filepath += pFileName;

	FileLoader loader;
	if (!loader.load((char*)filepath.c_str(), FileLoader::_TYPED_READ_ONLY_))
		return false;

	// 读取文件内容
	iLength = loader.length();
	pBuffer = new char[iLength+1];
	memset(pBuffer, 0, iLength+1);
	loader.read(pBuffer, iLength);
	loader.unload();

	return true;
}
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 (...)
	{
		;
	}
}