Ejemplo n.º 1
0
        public async Task Save_ThreeFilesCompressedToFile_DataEqualsExpected()
        {
            const string  FileName           = nameof(Save_ThreeFilesToFile_DataEqualsExpected);
            string        headerPath         = FileName + HeaderFileExtension;
            string        dataPath           = FileName + DataFileExtension;
            string        input              = "inputdata123" + nameof(Save_ThreeFilesToFile_DataEqualsExpected);
            string        encryptedInput     = "encrypteddata654" + nameof(Save_ThreeFilesToFile_DataEqualsExpected);
            var           sbExpected         = new StringBuilder();
            var           sizeMock           = CreateSizeMock(20, 30, 40);
            MS2CryptoMode cryptoMode         = (MS2CryptoMode)12345;
            IMS2ArchiveCryptoRepository repo = new FakeCryptoRepository(cryptoMode, EncodingTest, input, encryptedInput, sizeMock.Object);
            const int fileCount              = 3;

            var archive = MS2Archive.GetArchiveMS2F();

            for (uint i = 1; i <= fileCount; i++)
            {
                sbExpected.Append(input);
                AddDataStringToArchive(archive, input, encryptedInput, sizeMock, i, "file" + i, CompressionType.Zlib);
            }
            await archive.SaveAsync(headerPath, dataPath);

            using var fsData = File.OpenRead(dataPath);
            StringBuilder sbActual = new StringBuilder();

            for (uint i = 1; i <= fileCount; i++)
            {
                string actual = await StreamToString(await repo.GetDecryptionStreamAsync(fsData, sizeMock.Object, false));

                sbActual.Append(actual);
            }
            Assert.AreEqual(sbExpected.ToString(), sbActual.ToString());
        }
Ejemplo n.º 2
0
        public async Task Save_ToFileOverwriteAndExpand_OutputEqualExpectedSize()
        {
            const string  FileName           = nameof(Save_ToFileOverwriteAndExpand_OutputEqualExpectedSize);
            string        headerPath         = FileName + HeaderFileExtension;
            string        dataPath           = FileName + DataFileExtension;
            string        input              = "inputdata123" + nameof(Save_ToFileOverwriteAndExpand_OutputEqualExpectedSize);
            string        encryptedInput     = "encrypteddata654" + nameof(Save_ToFileOverwriteAndExpand_OutputEqualExpectedSize);
            var           sizeMock           = CreateSizeMock(1, 20, 8);
            MS2CryptoMode expectedCryptoMode = (MS2CryptoMode)12345;
            IMS2ArchiveCryptoRepository repo = new FakeCryptoRepository(expectedCryptoMode, EncodingTest, input, encryptedInput, sizeMock.Object);
            int expectedHeaderLength         = 60 + encryptedInput.Length * 2;
            int expectedDataLength           = encryptedInput.Length;

            SetFileLength(headerPath, 1 << 10);
            SetFileLength(dataPath, 1 << 10);
            var archive = new MS2Archive(repo);

            AddDataStringToArchive(archive, input, encryptedInput, sizeMock, 1, "overwritefile", CompressionType.None);
            await archive.SaveAsync(headerPath, dataPath);

            int actualHeaderLength = File.ReadAllText(headerPath).Length;
            int actualDataLength   = File.ReadAllText(dataPath).Length;

            Assert.AreEqual(expectedHeaderLength, actualHeaderLength);
            Assert.AreEqual(expectedDataLength, actualDataLength);
        }
Ejemplo n.º 3
0
        public async Task Save_OneFileToPath_ArchiveHeaderEqualsExpectedData()
        {
            const string   FileName             = nameof(Save_OneFileToPath_ArchiveHeaderEqualsExpectedData);
            string         headerPath           = FileName + HeaderFileExtension;
            string         dataPath             = FileName + DataFileExtension;
            string         input                = "inputdata123" + nameof(Save_OneFileToPath_ArchiveHeaderEqualsExpectedData);
            string         encryptedInput       = "encrypteddata654" + nameof(Save_OneFileToPath_ArchiveHeaderEqualsExpectedData);
            var            sizeMock             = CreateSizeMock(1, 20, 8);
            IMS2SizeHeader expectedFileInfoSize = sizeMock.Object;
            IMS2SizeHeader expectedFileDataSize = sizeMock.Object;
            long           expectedFileCount    = 1;
            MS2CryptoMode  expectedCryptoMode   = (MS2CryptoMode)12345;
            IMS2ArchiveCryptoRepository repo    = new FakeCryptoRepository(expectedCryptoMode, EncodingTest, input, encryptedInput, sizeMock.Object);

            var archive = new MS2Archive(repo);

            AddDataStringToArchive(archive, input, encryptedInput, sizeMock, 1, "singlefile", CompressionType.Zlib);
            await archive.SaveAsync(headerPath, dataPath);

            using var fsHeader = File.OpenRead(headerPath);
            using var br       = new BinaryReader(fsHeader, EncodingTest, true);
            MS2CryptoMode actualCryptoMode = (MS2CryptoMode)br.ReadUInt32();

            var(actualFileInfoSize, actualFileDataSize, actualFileCount) = await repo.GetArchiveHeaderCrypto().ReadAsync(fsHeader);

            Assert.AreEqual(expectedCryptoMode, actualCryptoMode);
            Assert.AreEqual(expectedFileInfoSize.EncodedSize, actualFileInfoSize.EncodedSize);
            Assert.AreEqual(expectedFileInfoSize.CompressedSize, actualFileInfoSize.CompressedSize);
            Assert.AreEqual(expectedFileInfoSize.Size, actualFileInfoSize.Size);
            Assert.AreEqual(expectedFileDataSize.EncodedSize, actualFileDataSize.EncodedSize);
            Assert.AreEqual(expectedFileDataSize.CompressedSize, actualFileDataSize.CompressedSize);
            Assert.AreEqual(expectedFileDataSize.Size, actualFileDataSize.Size);
            Assert.AreEqual(expectedFileCount, actualFileCount);
        }
Ejemplo n.º 4
0
        private static void UpdateRandomFilesFromArchive(MS2Archive archive)
        {
            const int    minFilesForRandom       = 10;
            const string folderForAddingName     = "FilesForAddingToArchive";
            string       folderToArchive         = Path.Combine(@"..\TestData", folderForAddingName);
            string       folderToArchiveFullPath = Path.GetFullPath(folderToArchive) + @"\";

            string[] filesToArchive = Directory.GetFiles(folderToArchive, "*.*", SearchOption.AllDirectories).Select(p => Path.GetFullPath(p)).ToArray();
            Assert.IsTrue(filesToArchive.Length > minFilesForRandom, $"you need at least {minFilesForRandom} files in the adding archive folder for this test");

            uint             count         = (uint)Random.Next(minFilesForRandom, filesToArchive.Length);
            HashSet <string> distinctFiles = new HashSet <string>();
            uint             i             = 0;

            while (i < count)
            {
                if (distinctFiles.Add(filesToArchive[Random.Next(0, filesToArchive.Length)]))
                {
                    i++;
                }
            }
            string[] files = distinctFiles.ToArray();
            Assert.AreEqual(files.Length, (int)count);
            for (i = 0; i < count; i++)
            {
                string  file         = files[i];
                int     index        = Random.Next(0, archive.Files.Count);
                MS2File previousFile = archive.Files[index];

                archive.Files[index] = MS2File.CreateUpdate(previousFile, file);
            }
        }
Ejemplo n.º 5
0
        public async Task Save_OneFileToFile_FileInfoHeaderEqualsExpectedData()
        {
            const string  FileName           = nameof(Save_OneFileToFile_FileInfoHeaderEqualsExpectedData);
            string        headerPath         = FileName + HeaderFileExtension;
            string        dataPath           = FileName + DataFileExtension;
            string        input              = "inputdata123," + nameof(Save_OneFileToFile_FileInfoHeaderEqualsExpectedData);
            string        encryptedInput     = "encrypteddata654," + nameof(Save_OneFileToFile_FileInfoHeaderEqualsExpectedData);
            var           sizeMock           = CreateSizeMock(1, 20, 8);
            MS2CryptoMode expectedCryptoMode = (MS2CryptoMode)12345;
            IMS2FileInfo  expectedFileInfo   = CreateFileInfoMock(1.ToString(), "singlefile").Object;
            IMS2ArchiveCryptoRepository repo = new FakeCryptoRepository(expectedCryptoMode, EncodingTest, "1,singlefile", "1,singlefile", sizeMock.Object);

            var archive = new MS2Archive(repo);

            AddDataStringToArchive(archive, input, encryptedInput, sizeMock, 1, "singlefile", CompressionType.Zlib);
            await archive.SaveAsync(headerPath, dataPath);

            using var fsHeader = File.OpenRead(headerPath);
            using var br       = new BinaryReader(fsHeader, EncodingTest, true);
            MS2CryptoMode actualCryptoMode = (MS2CryptoMode)br.ReadUInt32();

            var(actualFileInfoSize, actualFileDataSize, actualFileCount) = await repo.GetArchiveHeaderCrypto().ReadAsync(fsHeader);

            var msFileInfo = await repo.GetDecryptionStreamAsync(fsHeader, actualFileInfoSize, true);

            using var srFileInfo = new StreamReader(msFileInfo, EncodingTest, true, -1, true);
            IMS2FileInfo actualFileInfo = await repo.GetFileInfoReaderCrypto().ReadAsync(srFileInfo);

            Assert.AreEqual(expectedFileInfo.Id, actualFileInfo.Id);
            Assert.AreEqual(expectedFileInfo.Path, actualFileInfo.Path);
            Assert.AreEqual(expectedFileInfo.RootFolderId, actualFileInfo.RootFolderId);
        }
Ejemplo n.º 6
0
        private static async Task CreateArchiveAsync(string sourcePath, string headerFilePath, string dataFilePath)
        {
            if (!Directory.Exists(sourcePath))
            {
                throw new Exception($"Directory doesn't exist \"{sourcePath}\".");
            }

            var filePaths = GetFilesRelative(sourcePath);

            MS2File[] files = new MS2File[filePaths.Length];
            var       tasks = new Task[filePaths.Length];

            for (uint i = 0; i < filePaths.Length; i++)
            {
                uint ic = i;
                tasks[i] = Task.Run(() =>
                {
                    var(filePath, relativePath) = filePaths[ic];

                    files[ic] = MS2File.Create(ic + 1u, relativePath, CompressionType.Zlib, CryptoMode, filePath);
                });
            }

            await Task.WhenAll(tasks).ConfigureAwait(false);

            await MS2Archive.Save(CryptoMode, files, headerFilePath, dataFilePath, RunMode.Async2).ConfigureAwait(false);
        }
Ejemplo n.º 7
0
        public async Task SaveConcurrentlyAsync_FromEmptyAddExtractedFromExisting_OutputEqualsExpected(string pathWithoutExtension, string outputPathWithoutExtension, string expectedPathWithoutExtension, string extractFolderPath, string cryptoModeString)
        {
            var    cryptoMode         = Enum.Parse <MS2CryptoMode>(cryptoModeString, true);
            string headerPath         = pathWithoutExtension + HeaderFileExtension;
            string dataPath           = pathWithoutExtension + DataFileExtension;
            string expectedHeaderPath = expectedPathWithoutExtension + HeaderFileExtension;
            string expectedDataPath   = expectedPathWithoutExtension + DataFileExtension;
            string outputHeaderPath   = outputPathWithoutExtension + HeaderFileExtension;
            string outputDataPath     = outputPathWithoutExtension + DataFileExtension;

            await ExtractArchiveFilesAsync(headerPath, dataPath, extractFolderPath);

            var archive   = new MS2Archive(Repositories.Repos[cryptoMode]);
            var filePaths = GetFilesRelative(extractFolderPath);

            for (uint i = 0; i < filePaths.Length; i++)
            {
                AddAndCreateFileToArchive(archive, filePaths, i);
            }
            await archive.SaveConcurrentlyAsync(outputHeaderPath, outputDataPath);

            var expectedHeaderBytes = await File.ReadAllBytesAsync(expectedHeaderPath);

            var expectedDataBytes = await File.ReadAllBytesAsync(expectedDataPath);

            var actualHeaderBytes = await File.ReadAllBytesAsync(outputHeaderPath);

            var actualDataBytes = await File.ReadAllBytesAsync(outputDataPath);

            CollectionAssert.AreEqual(expectedHeaderBytes, actualHeaderBytes);
            CollectionAssert.AreEqual(expectedDataBytes, actualDataBytes);
        }
Ejemplo n.º 8
0
        private static async Task ExtractArchiveFilesAsync(string headerFile, string dataFile, string extractPath)
        {
            using IMS2Archive archive = await MS2Archive.GetAndLoadArchiveAsync(headerFile, dataFile).ConfigureAwait(false);

            foreach (var file in archive)
            {
                await ExtractFileAsync(extractPath, file).ConfigureAwait(false);
            }
        }
Ejemplo n.º 9
0
        public async Task GetAndLoadArchiveAsync_FilePaths_FileCountEqualsExpected(long fileCount, string pathWithoutExtension, string description)
        {
            long   expected   = fileCount;
            string headerPath = pathWithoutExtension + HeaderFileExtension;
            string dataPath   = pathWithoutExtension + DataFileExtension;

            var archive = await MS2Archive.GetAndLoadArchiveAsync(headerPath, dataPath);

            long actual = archive.Count;

            Assert.AreEqual(expected, actual, description);
        }
Ejemplo n.º 10
0
        private static async Task ExtractArchiveSync(string headerFile, string dataFile, string destinationPath)
        {
            using (MS2Archive archive = await MS2Archive.Load(headerFile, dataFile).ConfigureAwait(false))
            {
                List <MS2File> files = archive.Files;

                for (int i = 0; i < files.Count; i++)
                {
                    MS2File file = files[i];
                    Logger.Info($"Extracting file \"{file.Name}\", \"{FileEx.FormatStorage(file.Header.Size)}\". ({file.Header.Id}/{files.Count})");
                    await ExtractFileAsync(destinationPath, file).ConfigureAwait(false);
                }
            }
        }
Ejemplo n.º 11
0
        private static async Task ExportArchiveAsync(string headerFile, string dataFile, string destinationFilePath)
        {
            using (var swExport = new StreamWriter(destinationFilePath))
                using (MS2Archive archive = await MS2Archive.Load(headerFile, dataFile).ConfigureAwait(false))
                {
                    List <MS2File> files = archive.Files;

                    for (int i = 0; i < files.Count; i++)
                    {
                        MS2File file = files[i];
                        Logger.Info($"Exporting file \"{file.Name}\". ({file.Header.Id}/{files.Count})");
                        await ExportFileAsync(swExport, file).ConfigureAwait(false);
                    }
                }
        }
Ejemplo n.º 12
0
        private static void RemoveRandomFilesFromArchive(MS2Archive archive)
        {
            const int minFilesForRandom = 10;
            const int maxFilesForRandom = 100;

            Assert.IsTrue(archive.Files.Count > minFilesForRandom, $"you need at least {minFilesForRandom} files in the archive for this test");

            uint fileCount = (uint)Random.Next(minFilesForRandom, maxFilesForRandom);

            for (uint i = 0; i < fileCount; i++)
            {
                int index = Random.Next(0, archive.Files.Count);

                archive.Files.RemoveAt(index);
            }
        }
Ejemplo n.º 13
0
        public async Task Save_OneFileToPath_DataEqualsInput()
        {
            const string  FileName           = nameof(Save_OneFileToPath_DataEqualsInput);
            string        headerPath         = FileName + HeaderFileExtension;
            string        dataPath           = FileName + DataFileExtension;
            string        input              = "inputdata123" + nameof(Save_OneFileToPath_DataEqualsInput);
            string        encryptedInput     = "encrypteddata654" + nameof(Save_OneFileToPath_DataEqualsInput);
            var           sizeMock           = CreateSizeMock(20, 30, 40);
            MS2CryptoMode cryptoMode         = (MS2CryptoMode)12345;
            IMS2ArchiveCryptoRepository repo = new FakeCryptoRepository(cryptoMode, EncodingTest, input, encryptedInput, sizeMock.Object);

            var archive = new MS2Archive(repo);

            AddDataStringToArchive(archive, input, encryptedInput, sizeMock, 1, "singlefile", CompressionType.Zlib);
            await archive.SaveAsync(headerPath, dataPath);

            using var fsData = File.OpenRead(dataPath);
            string actual = await StreamToString(await repo.GetDecryptionStreamAsync(fsData, sizeMock.Object, false));

            Assert.AreEqual(input, actual);
        }
Ejemplo n.º 14
0
        public async Task SaveConcurrentlyAsync_LoadFromExistingThenSave_OutputEqualsExpected(string pathWithoutExtension, string outputPathWithoutExtension, string expectedPathWithoutExtension, string description)
        {
            string headerPath         = pathWithoutExtension + HeaderFileExtension;
            string dataPath           = pathWithoutExtension + DataFileExtension;
            string expectedHeaderPath = expectedPathWithoutExtension + HeaderFileExtension;
            string expectedDataPath   = expectedPathWithoutExtension + DataFileExtension;
            string outputHeaderPath   = outputPathWithoutExtension + HeaderFileExtension;
            string outputDataPath     = outputPathWithoutExtension + DataFileExtension;

            var archive = await MS2Archive.GetAndLoadArchiveAsync(headerPath, dataPath);

            await archive.SaveConcurrentlyAsync(outputHeaderPath, outputDataPath);

            var expectedHeaderBytes = await File.ReadAllBytesAsync(expectedHeaderPath);

            var expectedDataBytes = await File.ReadAllBytesAsync(expectedDataPath);

            var actualHeaderBytes = await File.ReadAllBytesAsync(outputHeaderPath);

            var actualDataBytes = await File.ReadAllBytesAsync(outputDataPath);

            CollectionAssert.AreEqual(expectedHeaderBytes, actualHeaderBytes);
            CollectionAssert.AreEqual(expectedDataBytes, actualDataBytes);
        }
Ejemplo n.º 15
0
        public async Task LoadThenSaveTestSyncNS2F()
        {
            const string  headerFilePath    = @"..\TestData\Xml.m2h";
            const string  dataFilePath      = @"..\TestData\Xml.m2d";
            MS2CryptoMode archiveCryptoMode = MS2CryptoMode.NS2F;

            using (MS2Archive archive = await MS2Archive.Load(headerFilePath, dataFilePath).ConfigureAwait(false))
            {
                Assert.AreEqual(archive.CryptoMode, archiveCryptoMode);
                Assert.AreEqual(archive.Files.Count, 66107);
                Assert.AreEqual(archive.Header, new MS2SizeHeader(502536u, 376900u, 3314723u));
                Assert.AreEqual(archive.Data, new MS2SizeHeader(776208u, 582154u, 2379852u));

                const string testArchiveName    = "SyncSaveTestNS2F";
                const string headerTestFileName = testArchiveName + ".m2h";
                const string dataTestFileName   = testArchiveName + ".m2d";

                try
                {
                    await archive.Save(headerTestFileName, dataTestFileName, RunMode.Sync).ConfigureAwait(false);

                    using (MS2Archive testArchive = await MS2Archive.Load(headerTestFileName, dataTestFileName).ConfigureAwait(false))
                    {
                        Assert.AreEqual(archive.CryptoMode, testArchive.CryptoMode);
                        Assert.AreEqual(archive.Files.Count, testArchive.Files.Count);
                        Assert.AreEqual(archive.Header.Size, testArchive.Header.Size);
                        Assert.AreEqual(archive.Data.Size, testArchive.Data.Size);

                        for (int i = 0; i < testArchive.Files.Count; i++)
                        {
                            Assert.AreEqual(archive.Files[i].IsZlibCompressed, testArchive.Files[i].IsZlibCompressed);
                            Assert.AreEqual(archive.Files[i].CompressionType, testArchive.Files[i].CompressionType);
                            Assert.AreEqual(archive.Files[i].Header?.Size, testArchive.Files[i].Header?.Size);
                            Assert.AreEqual(archive.Files[i].InfoHeader.Id, testArchive.Files[i].InfoHeader.Id);
                            Assert.AreEqual(archive.Files[i].InfoHeader.Name, testArchive.Files[i].InfoHeader.Name);
                            Assert.AreEqual(archive.Files[i].InfoHeader.RootFolderId, testArchive.Files[i].InfoHeader.RootFolderId);

                            (Stream Stream, bool ShouldDispose, MS2SizeHeader Header)original = await archive.Files[i].GetEncryptedStreamAsync().ConfigureAwait(false);
                            (Stream Stream, bool ShouldDispose, MS2SizeHeader Header)saved    = await testArchive.Files[i].GetEncryptedStreamAsync().ConfigureAwait(false);

                            try
                            {
                                Assert.AreEqual(original.Header.Size, saved.Header.Size);
                                Assert.AreEqual(original.Stream.Length, saved.Stream.Length);
                                byte[] originalBytes     = new byte[original.Stream.Length];
                                byte[] savedBytes        = new byte[saved.Stream.Length];
                                int    originalReadBytes = await original.Stream.ReadAsync(originalBytes, 0, originalBytes.Length).ConfigureAwait(false);

                                int savedReadBytes = await saved.Stream.ReadAsync(savedBytes, 0, savedBytes.Length).ConfigureAwait(false);

                                Assert.AreEqual(originalReadBytes, savedReadBytes);

                                CollectionAssert.AreEqual(originalBytes, savedBytes);
                            }
                            finally
                            {
                                if (original.ShouldDispose)
                                {
                                    original.Stream.Dispose();
                                }

                                if (saved.ShouldDispose)
                                {
                                    saved.Stream.Dispose();
                                }
                            }
                        }
                    }
                }
                finally
                {
                    File.Delete(headerTestFileName);
                    File.Delete(dataTestFileName);
                }
            }
        }
Ejemplo n.º 16
0
 private static IMS2Archive CreateArchiveNS2F() => MS2Archive.GetArchiveNS2F();
Ejemplo n.º 17
0
        public async Task LoadAsync_NotExistingFiles_ThrowsFileNotFoundException()
        {
            var archive = new MS2Archive(Repositories.Repos[MS2CryptoMode.MS2F]);

            await Assert.ThrowsExceptionAsync <FileNotFoundException>(() => archive.LoadAsync("notexistsheader", "notexistsdata"));
        }
Ejemplo n.º 18
0
        public async Task AddingMS2F()
        {
            const string headerFilePath = @"..\TestData\PrecomputedTerrain.m2h";
            const string dataFilePath   = @"..\TestData\PrecomputedTerrain.m2d";

            using (MS2Archive archive = await MS2Archive.Load(headerFilePath, dataFilePath).ConfigureAwait(false))
            {
                AddRandomFilesToArchive(archive);

                const string testArchiveName    = "AddingTestMS2F";
                const string headerTestFileName = testArchiveName + ".m2h";
                const string dataTestFileName   = testArchiveName + ".m2d";

                try
                {
                    await archive.Save(headerTestFileName, dataTestFileName, RunMode.Async2).ConfigureAwait(false);

                    using (MS2Archive testArchive = await MS2Archive.Load(headerTestFileName, dataTestFileName).ConfigureAwait(false))
                    {
                        Assert.AreEqual(archive.CryptoMode, testArchive.CryptoMode);
                        Assert.AreEqual(archive.Files.Count, testArchive.Files.Count);
                        Assert.AreNotEqual(archive.Header.Size, testArchive.Header.Size);
                        Assert.AreNotEqual(archive.Data.Size, testArchive.Data.Size);

                        for (int i = 0; i < testArchive.Files.Count; i++)
                        {
                            Assert.AreEqual(archive.Files[i].IsZlibCompressed, testArchive.Files[i].IsZlibCompressed);
                            Assert.AreEqual(archive.Files[i].CompressionType, testArchive.Files[i].CompressionType);
                            Assert.AreEqual(archive.Files[i].InfoHeader.Id, testArchive.Files[i].InfoHeader.Id);
                            Assert.AreEqual(archive.Files[i].InfoHeader.Name, testArchive.Files[i].InfoHeader.Name);
                            Assert.AreEqual(archive.Files[i].InfoHeader.RootFolderId, testArchive.Files[i].InfoHeader.RootFolderId);

                            (Stream Stream, bool ShouldDispose)original = await archive.Files[i].GetDecryptedStreamAsync().ConfigureAwait(false);
                            (Stream Stream, bool ShouldDispose)saved    = await testArchive.Files[i].GetDecryptedStreamAsync().ConfigureAwait(false);

                            try
                            {
                                if (archive.Files[i].Header != null && testArchive.Files[i].Header != null)
                                {
                                    Assert.AreEqual(archive.Files[i].Header.Size, testArchive.Files[i].Header.Size);
                                }
                                else if (archive.Files[i].Header != null)
                                {
                                    Assert.AreEqual(archive.Files[i].Header.Size, saved.Stream.Length);
                                }
                                else if (testArchive.Files[i].Header != null)
                                {
                                    Assert.AreEqual(original.Stream.Length, testArchive.Files[i].Header.Size);
                                }
                                Assert.AreEqual(original.Stream.Length, saved.Stream.Length);
                                byte[] originalBytes     = new byte[original.Stream.Length];
                                byte[] savedBytes        = new byte[saved.Stream.Length];
                                int    originalReadBytes = await original.Stream.ReadAsync(originalBytes, 0, originalBytes.Length).ConfigureAwait(false);

                                int savedReadBytes = await saved.Stream.ReadAsync(savedBytes, 0, savedBytes.Length).ConfigureAwait(false);

                                Assert.AreEqual(originalReadBytes, savedReadBytes);

                                CollectionAssert.AreEqual(originalBytes, savedBytes);
                            }
                            finally
                            {
                                if (original.ShouldDispose)
                                {
                                    original.Stream.Dispose();
                                }

                                if (saved.ShouldDispose)
                                {
                                    saved.Stream.Dispose();
                                }
                            }
                        }
                    }
                }
                finally
                {
                    File.Delete(headerTestFileName);
                    File.Delete(dataTestFileName);
                }
            }
        }
Ejemplo n.º 19
0
        public async Task TestCreateCompletelyNewNS2F()
        {
            const string folderForArchiving      = "TestFolderForArchiving";
            string       folderToArchive         = Path.Combine(@"..\TestData", folderForArchiving);
            string       folderToArchiveFullPath = Path.GetFullPath(folderToArchive) + @"\";

            string[] filesToArchive = Directory.GetFiles(folderToArchive, "*.*", SearchOption.AllDirectories).Select(p => Path.GetFullPath(p)).ToArray();

            MS2CryptoMode cryptoMode = MS2CryptoMode.NS2F;

            MS2File[] files = new MS2File[filesToArchive.Length];
            for (int i = 0; i < filesToArchive.Length; i++)
            {
                string file = filesToArchive[i];

                files[i] = MS2File.Create((uint)i + 1u, file.Remove(folderToArchiveFullPath), CompressionType.Zlib, cryptoMode, file);
            }

            const string testArchiveName    = "TestArchiveNS2F";
            const string headerTestFileName = testArchiveName + ".m2h";
            const string dataTestFileName   = testArchiveName + ".m2d";

            try
            {
                await MS2Archive.Save(cryptoMode, files, headerTestFileName, dataTestFileName, RunMode.Async2).ConfigureAwait(false);

                Assert.IsTrue(File.Exists(headerTestFileName));
                Assert.IsTrue(File.Exists(dataTestFileName));

                using (MS2Archive archive = await MS2Archive.Load(headerTestFileName, dataTestFileName).ConfigureAwait(false))
                {
                    Assert.AreEqual(archive.CryptoMode, cryptoMode);
                    Assert.AreEqual(archive.Files.Count, filesToArchive.Length);
                    //Assert.AreEqual(archive.Name, ArchiveFolder);

                    for (int i = 0; i < filesToArchive.Length; i++)
                    {
                        FileStream fsFile = File.OpenRead(filesToArchive[i]);
                        MS2File    file   = archive.Files[i];
                        Assert.AreEqual(file.Id, (uint)i + 1);
                        Assert.AreEqual(file.CompressionType, CompressionType.Zlib);
                        Assert.IsTrue(file.IsZlibCompressed);
                        Assert.AreEqual(file.Name, filesToArchive[i].Remove(folderToArchiveFullPath));
                        Assert.AreEqual(file.Header.CompressionType, file.CompressionType);
                        Assert.AreEqual(file.Header.Size, (uint)fsFile.Length);

                        (Stream stream, bool shouldDispose) = await file.GetDecryptedStreamAsync().ConfigureAwait(false);

                        try
                        {
                            byte[] originalBytes     = new byte[fsFile.Length];
                            byte[] savedBytes        = new byte[stream.Length];
                            int    originalReadBytes = await fsFile.ReadAsync(originalBytes, 0, originalBytes.Length).ConfigureAwait(false);

                            int savedReadBytes = await stream.ReadAsync(savedBytes, 0, savedBytes.Length).ConfigureAwait(false);

                            Assert.AreEqual(originalReadBytes, savedReadBytes);

                            CollectionAssert.AreEqual(originalBytes, savedBytes);
                        }
                        finally
                        {
                            if (shouldDispose)
                            {
                                stream.Dispose();
                            }
                        }
                    }
                }
            }
            finally
            {
                File.Delete(headerTestFileName);
                File.Delete(dataTestFileName);
            }
        }
        public async Task TestCreateConsistencyMS2F()
        {
            const string archiveName             = "PrecomputedTerrain";
            const string folderToArchive         = @"C:\Users\Miyu\Desktop\ReleaseOutput\Resource\" + archiveName;
            string       folderToArchiveFullPath = Path.GetFullPath(folderToArchive) + @"\";

            string[] filesToArchive = Directory.GetFiles(folderToArchive, "*.*", SearchOption.AllDirectories).Select(p => Path.GetFullPath(p)).ToArray();

            const string  headerFilePath    = @"..\TestData\PrecomputedTerrain.m2h";
            const string  dataFilePath      = @"..\TestData\PrecomputedTerrain.m2d";
            MS2CryptoMode archiveCryptoMode = MS2CryptoMode.MS2F;

            MS2File[] files = new MS2File[filesToArchive.Length];
            for (int i = 0; i < filesToArchive.Length; i++)
            {
                string file = filesToArchive[i];

                files[i] = MS2File.Create((uint)i + 1u, file.Remove(folderToArchiveFullPath), CompressionType.Zlib, archiveCryptoMode, file);
            }

            const string testArchiveName    = "FromExtractedMS2F";
            const string headerTestFileName = testArchiveName + ".m2h";
            const string dataTestFileName   = testArchiveName + ".m2d";

            try
            {
                await MS2Archive.Save(archiveCryptoMode, files, headerTestFileName, dataTestFileName, RunMode.Async2).ConfigureAwait(false);

                Assert.IsTrue(File.Exists(headerTestFileName));
                Assert.IsTrue(File.Exists(dataTestFileName));

                using (MS2Archive archive = await MS2Archive.Load(headerFilePath, dataFilePath).ConfigureAwait(false))
                {
                    Dictionary <uint, MS2File> mappedFiles = archive.Files.Zip(files, (o, s) => (o, s))
                                                             .ToDictionary(f => f.o.Id, f => files.Where(sf => sf.Name == f.o.Name).First());
                    Assert.AreEqual(archive.CryptoMode, archiveCryptoMode);
                    Assert.AreEqual(archive.Files.Count, filesToArchive.Length);
                    //Assert.AreEqual(archive.Name, ArchiveName);

                    Task[] tasks = new Task[filesToArchive.Length];

                    for (int i = 0; i < filesToArchive.Length; i++)
                    {
                        int ic = i;
                        tasks[ic] = Task.Run(async() =>
                        {
                            MS2File file      = archive.Files[ic];
                            MS2File savedFile = mappedFiles[file.Id];
                            var(savedStream, savedShouldDispose) = await savedFile.GetDecryptedStreamAsync().ConfigureAwait(false);
                            try
                            {
                                Assert.AreEqual(file.Id, (uint)ic + 1);
                                Assert.AreEqual(file.CompressionType, CompressionType.Zlib);
                                Assert.IsTrue(file.IsZlibCompressed);
                                Assert.AreEqual(file.Name, savedFile.Name);
                                Assert.AreEqual(file.Header.CompressionType, file.CompressionType);
                                Assert.AreEqual(file.Header.Size, (uint)savedStream.Length);

                                (Stream stream, bool shouldDispose) = await file.GetDecryptedStreamAsync().ConfigureAwait(false);
                                try
                                {
                                    byte[] savedBytes     = new byte[savedStream.Length];
                                    byte[] originalBytes  = new byte[stream.Length];
                                    int savedReadBytes    = await savedStream.ReadAsync(savedBytes, 0, savedBytes.Length).ConfigureAwait(false);
                                    int originalReadBytes = await stream.ReadAsync(originalBytes, 0, originalBytes.Length).ConfigureAwait(false);
                                    Assert.AreEqual(originalReadBytes, savedReadBytes);

                                    CollectionAssert.AreEqual(originalBytes, savedBytes);
                                }
                                finally
                                {
                                    if (shouldDispose)
                                    {
                                        stream.Dispose();
                                    }
                                }
                            }
                            finally
                            {
                                if (savedShouldDispose)
                                {
                                    savedStream.Dispose();
                                }
                            }
                        });
                    }

                    await Task.WhenAll(tasks).ConfigureAwait(false);
                }
            }
            finally
            {
                File.Delete(headerTestFileName);
                File.Delete(dataTestFileName);
            }
        }
Ejemplo n.º 21
0
        public async Task LoadThenSaveTestAsync2MS2F()
        {
            const string  headerFilePath    = @"..\TestData\PrecomputedTerrain.m2h";
            const string  dataFilePath      = @"..\TestData\PrecomputedTerrain.m2d";
            MS2CryptoMode archiveCryptoMode = MS2CryptoMode.MS2F;

            using (MS2Archive archive = await MS2Archive.Load(headerFilePath, dataFilePath).ConfigureAwait(false))
            {
                Assert.AreEqual(archive.CryptoMode, archiveCryptoMode);
                Assert.AreEqual(archive.Files.Count, 1694);
                Assert.AreEqual(archive.Header, new MS2SizeHeader(11056u, 8291u, 36097u));
                Assert.AreEqual(archive.Data, new MS2SizeHeader(31752u, 23813u, 81312u));

                const string testArchiveName    = "Async2SaveTestMS2F";
                const string headerTestFileName = testArchiveName + ".m2h";
                const string dataTestFileName   = testArchiveName + ".m2d";

                try
                {
                    await archive.Save(headerTestFileName, dataTestFileName, RunMode.Async2).ConfigureAwait(false);

                    using (MS2Archive testArchive = await MS2Archive.Load(headerTestFileName, dataTestFileName).ConfigureAwait(false))
                    {
                        Assert.AreEqual(archive.CryptoMode, testArchive.CryptoMode);
                        Assert.AreEqual(archive.Files.Count, testArchive.Files.Count);
                        Assert.AreEqual(archive.Header.Size, testArchive.Header.Size);
                        Assert.AreEqual(archive.Data.Size, testArchive.Data.Size);

                        Task[] tasks = new Task[testArchive.Files.Count];

                        for (int i = 0; i < testArchive.Files.Count; i++)
                        {
                            int ic = i;
                            Assert.AreEqual(archive.Files[ic].IsZlibCompressed, testArchive.Files[ic].IsZlibCompressed);
                            Assert.AreEqual(archive.Files[ic].CompressionType, testArchive.Files[ic].CompressionType);
                            Assert.AreEqual(archive.Files[ic].Header?.Size, testArchive.Files[ic].Header?.Size);
                            Assert.AreEqual(archive.Files[ic].InfoHeader.Id, testArchive.Files[ic].InfoHeader.Id);
                            Assert.AreEqual(archive.Files[ic].InfoHeader.Name, testArchive.Files[ic].InfoHeader.Name);
                            Assert.AreEqual(archive.Files[ic].InfoHeader.RootFolderId, testArchive.Files[ic].InfoHeader.RootFolderId);

                            tasks[ic] = Task.Run(async() =>
                            {
                                (Stream Stream, bool ShouldDispose, MS2SizeHeader Header)original = await archive.Files[ic].GetEncryptedStreamAsync().ConfigureAwait(false);
                                (Stream Stream, bool ShouldDispose, MS2SizeHeader Header)saved    = await testArchive.Files[ic].GetEncryptedStreamAsync().ConfigureAwait(false);

                                try
                                {
                                    Assert.AreEqual(original.Header.Size, saved.Header.Size);
                                    Assert.AreEqual(original.Stream.Length, saved.Stream.Length);
                                    byte[] originalBytes  = new byte[original.Stream.Length];
                                    byte[] savedBytes     = new byte[saved.Stream.Length];
                                    int originalReadBytes = await original.Stream.ReadAsync(originalBytes, 0, originalBytes.Length).ConfigureAwait(false);
                                    int savedReadBytes    = await saved.Stream.ReadAsync(savedBytes, 0, savedBytes.Length).ConfigureAwait(false);
                                    Assert.AreEqual(originalReadBytes, savedReadBytes);

                                    CollectionAssert.AreEqual(originalBytes, savedBytes);
                                }
                                finally
                                {
                                    if (original.ShouldDispose)
                                    {
                                        original.Stream.Dispose();
                                    }

                                    if (saved.ShouldDispose)
                                    {
                                        saved.Stream.Dispose();
                                    }
                                }
                            });
                        }

                        await Task.WhenAll(tasks).ConfigureAwait(false);
                    }
                }
                finally
                {
                    File.Delete(headerTestFileName);
                    File.Delete(dataTestFileName);
                }
            }
        }