public void WriteCurrentSave_LevelSerializedOutAsExpected()
        {
            const string testPath = "TestSaveFile.dat";

            PersistenceFunctions.WriteCurrentSave(testPath, _service);

            using (var fileStream =
                       File.Open(Application.persistentDataPath + testPath, FileMode.Open))
            {
                var decryptedStream = new MemoryStream(PersistantDataOperationFunctions.DecryptFileStream(fileStream, GameDataStorageConstants.AESKey, GameDataStorageConstants.AESIV));
                var binaryFormatter = new BinaryFormatter();

                Assert.AreEqual(SceneManager.GetActiveScene().path, binaryFormatter.Deserialize(decryptedStream));

                Assert.AreEqual(_service.GetEntitiesResult.Count, binaryFormatter.Deserialize(decryptedStream));

                foreach (var entity in _service.GetEntitiesResult)
                {
                    Assert.AreEqual(entity.Key, binaryFormatter.Deserialize(decryptedStream));
                    Assert.AreEqual(entity.Value == null, binaryFormatter.Deserialize(decryptedStream));
                }

                fileStream.Close();
            }
        }
        // Decrypts save data ready for other objects to read
        public static Stream InitializeSaveRead(string savePath)
        {
            using (var fileStream =
                       File.Open(Application.persistentDataPath + savePath, FileMode.Open))
            {
                var decryptedStream = new MemoryStream(PersistantDataOperationFunctions.DecryptFileStream(fileStream, GameDataStorageConstants.AESKey,
                                                                                                          GameDataStorageConstants.AESIV));

                fileStream.Close();

                return(decryptedStream);
            }
        }
Example #3
0
        public void CryptoFunctionsEncryptAndDecryptAsExpected()
        {
            _file = File.Open(Application.dataPath + _filePath, FileMode.Open);

            var encryptedResult = PersistantDataOperationFunctions.EncryptFileStream(_file, _key, _iV);
            var decryptedResult = PersistantDataOperationFunctions.DecryptFileStream(new MemoryStream(encryptedResult), _key, _iV);
            var stream          = new MemoryStream(decryptedResult);
            var bf = new BinaryFormatter();

            var actualFirst = (DataBlockA)bf.Deserialize(stream);

            Assert.AreEqual(_firstData.CurrentIntValue, actualFirst.CurrentIntValue);
            Assert.AreEqual(_firstData.CurrentStringValue, actualFirst.CurrentStringValue);

            var actualSecond = (DataBlockB)bf.Deserialize(stream);

            Assert.AreEqual(_secondData.SomeStringValue, actualSecond.SomeStringValue);

            _file.Close();
        }
        public static void WriteCurrentSave(string savePath, IPersistenceServiceInterface persistenceService)
        {
            using (var writeStream = new MemoryStream())
            {
                var binaryFormatter = new BinaryFormatter();

                binaryFormatter.Serialize(writeStream, SceneManager.GetActiveScene().path);
                SaveData(writeStream, persistenceService);

                var encryptedStream = new MemoryStream(PersistantDataOperationFunctions.EncryptFileStream(new MemoryStream(writeStream.ToArray()), GameDataStorageConstants.AESKey,
                                                                                                          GameDataStorageConstants.AESIV));

                using (var fileStream =
                           File.Open(Application.persistentDataPath + savePath, FileMode.OpenOrCreate))
                {
                    var bytesToCopy = encryptedStream.ToArray();
                    fileStream.Write(bytesToCopy, 0, bytesToCopy.Length);

                    fileStream.Close();
                }
            }
        }