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