Exemple #1
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);
        }
Exemple #2
0
 static void PrintArchiveInfo(BA2Archive archive)
 {
     Console.WriteLine("Type: " + BA2Loader.GetArchiveType(archive));
     Console.WriteLine("Signature: " + Encoding.ASCII.GetString(archive.Header.Signature));
     Console.WriteLine("Total files: " + archive.TotalFiles);
     Console.WriteLine("Size: " + archive.Length / 1024 / 1024 + " MB");
 }
Exemple #3
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);
        }
Exemple #4
0
 public static void AssertExtractedTextFile(BA2Archive archive, int fileIndex, string excepted)
 {
     using (var stream = new MemoryStream())
     {
         Assert.IsTrue(archive.ExtractToStream(fileIndex, stream), $"Unable to extract file { archive.FileList[fileIndex] }.");
         AssertExtractedTextFile(stream, excepted);
     }
 }
Exemple #5
0
        private static void FindFiles(BA2Archive archive, FindCommand cmd)
        {
            int counter = 1;

            foreach (string file in FindMatchingFiles(archive, cmd.Search))
            {
                Console.WriteLine($"{counter++}. {file}");
            }
        }
Exemple #6
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);
        }
Exemple #7
0
        public static void AssertFileListEntries(BA2Archive archive, IList <string> excepted)
        {
            var actual = archive.FileList;

            Assert.AreEqual <int>(excepted.Count(), actual.Count(), "Name table entries count are not same.");
            for (int i = 0; i < excepted.Count; i++)
            {
                string exceptFile = excepted[i];
                string actualFile = actual[i];

                bool ok = exceptFile.Equals(actualFile, StringComparison.OrdinalIgnoreCase);
                if (!ok)
                {
                    Assert.Fail($"Excepted name table entry \"{ exceptFile }\", but got \"{ actualFile }\" (index { i }).");
                }
            }
        }
Exemple #8
0
        private static bool ExtractMatching(BA2Archive archive, ExtractMatchingCommand cmd)
        {
            var files = FindMatchingFiles(archive, cmd.FindString);

            if (files.Count() == 0)
            {
                return(false);
            }

            int counter = 1;

            foreach (var fileName in files)
            {
                Console.WriteLine($"{counter++}. {fileName}");
            }

            ExtractFiles(archive, files, cmd.Destination);
            return(true);
        }
Exemple #9
0
        private static bool ExtractFiles(BA2Archive archive, IEnumerable <string> files, string dest)
        {
            Console.Write($"Extract {files.Count()} files to \"{dest}\" (y/n)\n> ");
            if (Console.ReadLine().Trim().ToLower() == "y")
            {
                var cancel   = new CancellationTokenSource();
                var progress = new ConsoleProgress((int)files.Count(),
                                                   (uint)((archive.Length / archive.TotalFiles) * files.Count()));
                ManualResetEvent ev        = new ManualResetEvent(false);
                bool             cancelled = false;

                Thread thread = new Thread(() =>
                {
                    try
                    {
                        progress.Start();
                        // extract
                        archive.ExtractFiles(files, dest, true, cancel.Token, progress);
                        // manual exit from thread after extracting.
                        ev.Set();
                    }
                    catch (OperationCanceledException)
                    {
                        cancelled = true;
                        ev.Set();
                    }
                    finally
                    {
                        progress.Finish();
                        ev.Set();
                    }
                });

                thread.Start();

                var inputThread = new Thread(() =>
                {
                    var key = Console.ReadKey();
                    if (!cancelled)
                    {
                        Console.SetCursorPosition(Math.Max(0, Console.CursorLeft - 1), Console.CursorTop);

                        Console.WriteLine("Cancelling...");
                        cancel.Cancel();
                    }
                });
                inputThread.Start();

                ev.WaitOne();
                if (inputThread.IsAlive)
                {
                    inputThread.Abort();
                }

                if (cancelled)
                {
                    Console.WriteLine("Cancelled.");
                }

                return(true);
            }

            return(false);
        }
Exemple #10
0
 private static IEnumerable <string> FindMatchingFiles(BA2Archive archive, string match)
 {
     return(archive.FileList.Where((fileName)
                                   => fileName.IndexOf(match, StringComparison.InvariantCultureIgnoreCase) != -1));
 }
Exemple #11
0
 private static void ExtractSingle(BA2Archive archive, ExtractCommand cmd)
 {
     archive.Extract(cmd.Source, cmd.Destination, true);
 }
Exemple #12
0
 private static void ExtractAll(BA2Archive archive, ExtractAllCommand cmd)
 {
     ExtractFiles(archive, archive.FileList, cmd.Destination);
 }
Exemple #13
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);
        }
Exemple #14
0
 public Task ExtractAllAsync(BA2Archive archive)
 {
     return(Task.Run(() => archive.ExtractAll("D:/stuff", true)));
 }