예제 #1
0
        public void ExtractShouldThrowExceptionWhenDisposed()
        {
            BA2GeneralArchive archive = BA2Loader.Load <BA2GeneralArchive>(SharedData.GeneralOneFileArchive);

            archive.Dispose();

            var dir = SharedData.CreateTempDirectory();

            using (var stream = new MemoryStream())
            {
                Assert.Throws <ObjectDisposedException>(() => archive.Extract(0, dir, false));
                Assert.Throws <ObjectDisposedException>(() => archive.Extract("test.txt", dir, false));
                Assert.Throws <ObjectDisposedException>(() => archive.ExtractToStream(0, stream));
                Assert.Throws <ObjectDisposedException>(() => archive.ExtractAll("", false));
                Assert.Throws <ObjectDisposedException>(() => archive.ExtractAll("", false, CancellationToken.None, null));
                Assert.Throws <ObjectDisposedException>(() => archive.ExtractFiles(new int[] { 0 }, dir, false));
                Assert.Throws <ObjectDisposedException>(() => archive.ExtractFiles(new int[] { 0 }, dir, false,
                                                                                   CancellationToken.None, null));
                Assert.Throws <ObjectDisposedException>(() => archive.ExtractFiles(new string[] { "test.txt" }, dir, false));
                Assert.Throws <ObjectDisposedException>(() => archive.ExtractFiles(new string[] { "test.txt" }, dir, false,
                                                                                   CancellationToken.None, null));

                // These methods should not throw ObjectDisposedException
                Assert.DoesNotThrow(() => {
                    var files = archive.FileList;
                    archive.GetIndexFromFilename("test.txt");
                    archive.GetIndexFromFilename("ajkkfajsdlkfjlkasdf");
                    var total  = archive.TotalFiles;
                    var header = archive.Header;
                });
            }
        }
예제 #2
0
        public void TestGeneralArchiveExtractionWithProgress()
        {
            BA2Archive archive          = BA2Loader.Load(SharedData.GeneralOneFileArchive);
            string     temp             = SharedData.CreateTempDirectory();
            int        progressValue    = 0;
            bool       progressReceived = false;
            var        progressHandler  = new Progress <int>(x =>
            {
                progressReceived = true;
                progressValue    = x;
            });

            archive.ExtractAll(temp, false, CancellationToken.None, progressHandler);

            // workaround of dumb test execution
            int waits = 0;

            while (!progressReceived)
            {
                if (waits > 10)
                {
                    break;
                }
                Thread.Sleep(25);
                waits++;
            }
            Assert.AreEqual(1, progressValue);
        }
예제 #3
0
        public void TestGeneralArchiveType()
        {
            var archive = BA2Loader.Load(SharedData.GetDataPath("GeneralOneFile.ba2"));

            Assert.IsInstanceOf <BA2GeneralArchive>(archive);
            Assert.IsTrue(BA2Loader.GetArchiveType(archive) == BA2Type.General);
        }
예제 #4
0
        public void TestGeneralTwoFiles()
        {
            var archive = BA2Loader.Load(SharedData.GetDataPath("GeneralTwoFiles.ba2"));
            var header  = archive.Header;

            Assert.IsTrue(header.Signature.SequenceEqual(SharedData.ArchiveMagic));
            Assert.AreEqual(1U, header.Version);
            Assert.IsTrue(BA2Loader.GetArchiveType(header.ArchiveType) == BA2Type.General);
            Assert.AreEqual(2U, header.TotalFiles);
            Assert.AreEqual(121UL, header.NameTableOffset);

            var files = archive.FileList;

            Assert.AreEqual(2, files.Count);
            Assert.AreEqual("test.txt", files[0]);
            Assert.AreEqual("wazzup.bin", files[1]);

            var folder = SharedData.CreateTempDirectory();

            archive.Extract("test.txt", folder, false);

            var testPath = Path.Combine(folder, "test.txt");

            Assert.IsTrue(File.Exists(testPath));

            TestUtils.AssertExtractedTextFile(archive, archive.GetIndexFromFilename("test.txt"), "test text");
            TestUtils.AssertExtractedTextFile(archive, archive.GetIndexFromFilename("wazzup.bin"), "wazzup dude bro?");

            // Assert.IsTrue(File.ReadAllLines)
        }
예제 #5
0
        static BA2Archive RequestArchive()
        {
            string     archivePath = null;
            BA2Archive archive     = null;

            Console.Write("Path to archive: ");
            while (true)
            {
                archivePath = Console.ReadLine();
                if (String.IsNullOrWhiteSpace(archivePath))
                {
                    continue;
                }

                try
                {
                    archive = BA2Loader.Load(archivePath);
                }
                catch (Exception e)
                {
                    WriteError("Error while opening archive: {0}", e.Message);
                    goto tryAgain;
                }

                if (archive != null)
                {
                    break;
                }

tryAgain:
                Console.Write("Try again: ");
            }

            return(archive);
        }
예제 #6
0
        public void ExtractByIndexFromTwoFileArchive()
        {
            BA2GeneralArchive archive = BA2Loader.Load <BA2GeneralArchive>(SharedData.GeneralTwoFilesArchive);

            TestUtils.AssertExtractedTextFile(archive, 0, "test text");
            TestUtils.AssertExtractedTextFile(archive, 1, "wazzup dude bro?");

            archive.Dispose();
        }
예제 #7
0
        public void ExtractAllFromTwoFileArchive()
        {
            BA2GeneralArchive archive = BA2Loader.Load <BA2GeneralArchive>(SharedData.GeneralTwoFilesArchive);

            string temp = SharedData.CreateTempDirectory();

            archive.ExtractAll(temp, false);
            Assert.IsTrue(File.Exists(Path.Combine(temp, "test.txt")));
            Assert.IsTrue(File.Exists(Path.Combine(temp, "wazzup.bin")));

            archive.Dispose();
        }
예제 #8
0
        public void ExtractByIndexFromOneFileArchive()
        {
            BA2GeneralArchive archive = BA2Loader.Load <BA2GeneralArchive>(SharedData.GeneralOneFileArchive);

            using (MemoryStream stream = new MemoryStream())
            {
                Assert.IsTrue(archive.ExtractToStream(0, stream));
                TestUtils.AssertExtractedTextFile(stream, "test text");
            }

            archive.Dispose();
        }
예제 #9
0
        public void ExtractFilesByIndexesFromTwoFileArchiveMultithreaded()
        {
            BA2GeneralArchive archive =
                BA2Loader.Load <BA2GeneralArchive>(SharedData.GeneralTwoFilesArchive, BA2LoaderFlags.Multithreaded);

            string temp = SharedData.CreateTempDirectory();

            archive.ExtractFiles(new int[] { 0, 1 }, temp, false);
            Assert.IsTrue(File.Exists(Path.Combine(temp, "test.txt")));
            Assert.IsTrue(File.Exists(Path.Combine(temp, "wazzup.bin")));

            archive.Dispose();
        }
예제 #10
0
        public static ArchiveInfo Open(string path)
        {
            BA2Archive archive = BA2Loader.Load(path,
                                                AppSettings.Instance.Global.MultithreadedExtraction ? BA2LoaderFlags.Multithreaded : BA2LoaderFlags.None);

            ArchiveInfo info = new ArchiveInfo();

            info.Archive   = archive;
            info.FileNames = new ObservableCollection <string>(archive.FileList);
            info.FilePath  = path;
            info.FileName  = Path.GetFileName(info.FilePath);

            return(info);
        }
예제 #11
0
        public void TestStreamExtraction()
        {
            var archive = BA2Loader.Load(SharedData.GetDataPath("GeneralOneFile.ba2"));

            using (var stream = new MemoryStream())
            {
                bool status = archive.ExtractToStream("test.txt", stream);
                Assert.IsTrue(status);

                byte[] buffer = new byte[stream.Length];
                stream.Read(buffer, 0, (int)stream.Length);

                Assert.IsTrue(Encoding.ASCII.GetString(buffer).Equals("test text", StringComparison.OrdinalIgnoreCase));
            }
        }
예제 #12
0
        public void FalloutArchivesExtractTest(string archiveName, byte[] signature, uint version, byte[] type, uint totalFiles, ulong nameTableOffset)
        {
            var archivePath = GetArchivePath(archiveName);

            if (!File.Exists(archivePath))
            {
                Assert.Ignore("Archive {0} was not found. Skipping test.", archivePath);
            }

            using (var archive = BA2Loader.Load(archivePath, BA2LoaderFlags.None))
            {
                using (var memory = new MemoryStream())
                {
                    Assert.IsTrue(archive.ExtractToStream(0, memory));
                }
            }
        }
예제 #13
0
        public void FalloutArchivesHeaderTest(string archiveName, byte[] signature, uint version, byte[] type, uint totalFiles, ulong nameTableOffset)
        {
            var archivePath = GetArchivePath(archiveName);

            if (!File.Exists(archivePath))
            {
                Assert.Ignore("Archive {0} was not found. Skipping test.", archivePath);
            }

            using (var archive = BA2Loader.Load(archivePath, BA2LoaderFlags.None))
            {
                Assert.AreEqual(archive.Header.Signature, signature, "Signatures don't match.");
                Assert.AreEqual(archive.Header.Version, version, "Versions don't match.");
                Assert.AreEqual(archive.Header.ArchiveType, type, "Types don't match.");
                Assert.AreEqual(archive.Header.TotalFiles, totalFiles, "Total files don't match.");
                Assert.AreEqual(archive.Header.NameTableOffset, nameTableOffset, "Table offset don't match.");
            }
        }
예제 #14
0
        public void TestGeneralOneFile()
        {
            var archive = BA2Loader.Load(SharedData.GetDataPath("GeneralOneFile.ba2"));
            var header  = archive.Header;

            Assert.IsTrue(header.Signature.SequenceEqual(SharedData.ArchiveMagic));
            Assert.AreEqual(1U, header.Version);
            Assert.IsTrue(BA2Loader.GetArchiveType(header.ArchiveType) == BA2Type.General);
            Assert.AreEqual(1U, header.TotalFiles);
            Assert.AreEqual(69UL, header.NameTableOffset);

            var files = archive.FileList;

            Assert.AreEqual(1, files.Count);
            Assert.AreEqual(true, archive.ContainsFile("test.txt"));

            var folder = SharedData.CreateTempDirectory();

            archive.Extract("test.txt", folder, false);
            string path = Path.Combine(folder, "test.txt");

            Assert.IsTrue(File.Exists(path));
            Assert.AreEqual("test text", File.ReadAllText(path));
        }
예제 #15
0
        static bool Inner(string[] args)
        {
            BA2Archive archive = null;

            if (args.Length > 0)
            {
                try
                {
                    archive = BA2Loader.Load(args[0]);
                }
                catch (Exception e)
                {
                    WriteError("Invalid archive passed: {0}", e.Message);
                }
            }

            if (archive == null)
            {
                archive = RequestArchive();
            }

            using (archive)
            {
                bool listening = true;
                Console.WriteLine("Input command, \"q\" to exit, \"help\" to get help.");
                while (listening)
                {
                    Command command = GetCommand();
                    switch (command.Type)
                    {
                    case CommandType.Exit:
                        listening = false;
                        break;

                    case CommandType.Info:
                        PrintArchiveInfo(archive);
                        break;

                    case CommandType.Find:
                        FindFiles(archive, command as FindCommand);
                        break;

                    case CommandType.None:
                        break;

                    case CommandType.Export:
                        try
                        {
                            ExtractSingle(archive, command as ExtractCommand);
                            Console.WriteLine("Done.");
                        }
                        catch (Exception e)
                        {
                            WriteError("Error occur while exporting: {0}", e.Message);
                        }
                        break;

                    case CommandType.ExportMatching:
                        try
                        {
                            if (ExtractMatching(archive, command as ExtractMatchingCommand))
                            {
                                Console.WriteLine("Done.");
                            }
                            else
                            {
                                Console.WriteLine("No matching files found.");
                            }
                        }
                        catch (Exception e)
                        {
                            WriteError("Error occur while exporting: {0}", e.Message);
                        }
                        break;

                    case CommandType.ExportAll:
                        try
                        {
                            ExtractAll(archive, command as ExtractAllCommand);
                            Console.WriteLine("Done.");
                        }
                        catch (Exception e)
                        {
                            WriteError("Error occur while exporting: {0}", e.Message);
                        }
                        break;

                    case CommandType.Invalid:
                        WriteError("Unknown command");
                        break;

                    case CommandType.Drop:
                        return(true);

                    case CommandType.Help:
                    default:
                        PrintHelp();
                        break;
                    }
                }
            }

            return(false);
        }
예제 #16
0
 public void TestInvalidArchive()
 {
     Assert.Throws <BA2LoadException>(() => BA2Loader.Load(Path.Combine(SharedData.DataFolder, "InvalidArchive.txt")));
 }
예제 #17
0
 public void InvalidDataExceptionThrownWhenInvalidNametableProviden()
 {
     Assert.Throws <InvalidDataException>(() => BA2Loader.Load(SharedData.GetDataPath("GeneralHeaderOnly.ba2")));
 }
예제 #18
0
 public void TestInvalidGenericArchiveLoad()
 {
     Assert.Throws <BA2LoadException>(() => BA2Loader.Load <BA2TextureArchive>(SharedData.GeneralOneFileArchive));
 }
예제 #19
0
 public void LoadExceptionThrownWhenArchiveHasInvalidType()
 {
     Assert.Throws <BA2LoadException>(() => BA2Loader.Load(SharedData.GetDataPath("GeneralHeaderOnlyInvalidType.ba2")));
 }
예제 #20
0
        public void TestGenericArchiveLoader()
        {
            BA2GeneralArchive archive = BA2Loader.Load <BA2GeneralArchive>(SharedData.GeneralOneFileArchive);

            archive.Dispose();
        }
예제 #21
0
 public void TestKnownInvalidVersion()
 {
     Assert.DoesNotThrow(
         () => BA2Loader.Load(SharedData.GetDataPath("GeneralOneFileInvalidVersion.ba2"), BA2LoaderFlags.IgnoreVersion));
 }
예제 #22
0
 public void TestKnownInvalidArchiveType()
 {
     Assert.DoesNotThrow(
         () => BA2Loader.Load(SharedData.GetDataPath("GeneralOneFileInvalidType.ba2"), BA2LoaderFlags.IgnoreArchiveType));
 }