//-------------------------------------------------------------- //-------------------------------------------------------------- void AppDataStore::Save() { std::unique_lock<std::mutex> lock(m_mutex); if(m_needsSynchonised == true) { // Convert to XML XML::Document doc; XML::Node* rootNode = doc.allocate_node(rapidxml::node_type::node_element); doc.append_node(rootNode); ParamDictionarySerialiser::ToXml(m_dictionary, rootNode); // Encrypt std::string strDocToBeEncrypted = XMLUtils::ToString(&doc); AESEncrypt::Data encryptedData = AESEncrypt::EncryptString(strDocToBeEncrypted, k_privateKey); // Write to disk FileSystem* pFileSystem = Application::Get()->GetFileSystem(); FileStreamSPtr pFileStream = pFileSystem->CreateFileStream(StorageLocation::k_saveData, k_filename, FileMode::k_writeBinary); if(pFileStream != nullptr) { pFileStream->Write(reinterpret_cast<const s8*>(encryptedData.m_data.get()), encryptedData.m_size); pFileStream.reset(); } m_needsSynchonised = false; } }
//-------------------------------------------------------------- //-------------------------------------------------------------- void AppDataStore::RefreshFromFile() { FileSystem* pFileSystem = Application::Get()->GetFileSystem(); if(pFileSystem->DoesFileExist(StorageLocation::k_saveData, k_filename) == true) { FileStreamSPtr fileStream = pFileSystem->CreateFileStream(StorageLocation::k_saveData, k_filename, FileMode::k_readBinary); if (fileStream != nullptr) { fileStream->SeekG(0, SeekDir::k_end); u32 encryptedDataSize = fileStream->TellG(); fileStream->SeekG(0, SeekDir::k_beginning); std::unique_ptr<s8[]> encryptedData(new s8[encryptedDataSize]); fileStream->Read(encryptedData.get(), encryptedDataSize); fileStream.reset(); std::string decrypted = AESEncrypt::DecryptString(reinterpret_cast<const u8*>(encryptedData.get()), encryptedDataSize, k_privateKey); XMLUPtr xml = XMLUtils::ParseDocument(decrypted); XML::Node* root = XMLUtils::GetFirstChildElement(xml->GetDocument()); if(nullptr != root) { m_dictionary = ParamDictionarySerialiser::FromXml(root); } } } m_needsSynchonised = false; }
//-------------------------------------------------------------- //-------------------------------------------------------------- CSBinaryInputStream::CSBinaryInputStream(StorageLocation in_storageLocation, const std::string& in_filePath) { FileSystem* fileSystem = Application::Get()->GetFileSystem(); CS_ASSERT(fileSystem != nullptr, "CSBinaryInputStream missing required system: FileSystem."); m_fileStream = fileSystem->CreateFileStream(in_storageLocation, in_filePath, FileMode::k_readBinary); if (m_fileStream != nullptr) { u32 numChunkTableEntries; if (ReadHeader(m_fileStream.get(), in_filePath, m_fileFormatId, m_fileFormatVersion, numChunkTableEntries) == false) { m_fileStream.reset(); return; } ReadChunkTable(numChunkTableEntries); m_isValid = true; } }