Ejemplo n.º 1
0
        public static void RepackFile(string sourceFileName, string destinationFileName, bool compress = false)
        {
            int alignment = 16;

            //destinationFileName = Path.ChangeExtension(destinationFileName, null);

            using (var stream = File.OpenRead(sourceFileName))
            {
                var farcArchive = FarcArchive.Load <FarcArchive>(stream);
                farcArchive.IsCompressed = compress;
                farcArchive.Alignment    = alignment;
                farcArchive.Save(destinationFileName);
            }
        }
Ejemplo n.º 2
0
        static void Main(string[] args)
        {
            if (args.Length < 1)
            {
                Console.WriteLine(Properties.Resources.HelpText);
                Console.ReadLine();
                return;
            }

            string sourceFileName      = null;
            string destinationFileName = null;

            bool compress  = false;
            int  alignment = 16;

            for (int i = 0; i < args.Length; i++)
            {
                string arg = args[i];

                if (EqualsAny("-c", "--compress"))
                {
                    compress = true;
                }

                else if (EqualsAny("-a", "--alignment"))
                {
                    alignment = int.Parse(args[++i]);
                }

                else if (sourceFileName == null)
                {
                    sourceFileName = arg;
                }

                else if (destinationFileName == null)
                {
                    destinationFileName = arg;
                }

                bool EqualsAny(params string[] strings) =>
                strings.Contains(arg, StringComparer.OrdinalIgnoreCase);
            }

            if (destinationFileName == null)
            {
                destinationFileName = sourceFileName;
            }

            if (File.GetAttributes(sourceFileName).HasFlag(FileAttributes.Directory))
            {
                destinationFileName = Path.ChangeExtension(destinationFileName, "farc");

                var farcArchive = new FarcArchive();
                farcArchive.IsCompressed = compress;
                farcArchive.Alignment    = alignment;

                foreach (var filePath in Directory.EnumerateFiles(sourceFileName))
                {
                    farcArchive.Add(Path.GetFileName(filePath), filePath);
                }

                farcArchive.Save(destinationFileName);
            }

            else if (args[0].EndsWith(".farc", StringComparison.OrdinalIgnoreCase))
            {
                destinationFileName = Path.ChangeExtension(destinationFileName, null);

                using (var stream = File.OpenRead(sourceFileName))
                {
                    var farcArchive = FarcArchive.Load <FarcArchive>(stream);

                    Directory.CreateDirectory(destinationFileName);
                    foreach (var fileName in farcArchive)
                    {
                        using (var destination = File.Create(Path.Combine(destinationFileName, fileName)))
                            using (var source = farcArchive.Open(fileName, EntryStreamMode.OriginalStream))
                                source.CopyTo(destination);
                    }
                }
            }
        }
Ejemplo n.º 3
0
        static void Main(string[] args)
        {
#if !DEBUG
            if (args.Length < 1)
            {
                Console.WriteLine("FARC Pack");
                Console.WriteLine("=========");
                Console.WriteLine("Packer/unpacker for .FARC files.\n");
                Console.WriteLine("Usage:");
                Console.WriteLine("    FarcPack [options] [source] [destination]");
                Console.WriteLine("    Source can be either FARC or directory.");
                Console.WriteLine("    Destination can be left empty.\n");
                Console.WriteLine("Options:");
                Console.WriteLine("    -a, -alignment         Set alignment for output FARC.");
                Console.WriteLine("                           16 by default.\n");
                Console.WriteLine("    -c, --compress         Compress files in output FARC.");
                Console.WriteLine("                           Disabled by default.");
                Console.ReadLine();
                return;
            }
#endif
            string sourcePath      = null;
            string destinationPath = null;

            bool compress  = false;
            uint alignment = 16;

            for (int i = 0; i < args.Length; i++)
            {
                string arg = args[i];

                Func <string, string, bool> compare = (a, b) => arg.Equals(a, StringComparison.OrdinalIgnoreCase) || arg.Equals(b, StringComparison.OrdinalIgnoreCase);
                alignment = compare("-a", "-alignment") ? uint.Parse(args[++i]) : alignment;

                compress = compare("-c", "-compress") || compress;

                if (sourcePath == null && !compare("-c", "-compress") && !compare("-a", "-alignment"))
                {
                    sourcePath = arg;
                }


                else if (destinationPath == null && !compare("-c", "-compress") && !compare("-a", "-alignment"))
                {
                    destinationPath = arg;
                }
            }

#if DEBUG
            sourcePath = @"C:\Users\waelw.WAELS-PC\Desktop\farc\vr_cmn";
#endif

            if (sourcePath == null)
            {
                throw new ArgumentException("You must provide a source.", nameof(sourcePath));
            }

            var serial = new BinarySerializer();

            if (sourcePath.EndsWith(".farc", StringComparison.OrdinalIgnoreCase))
            {
                destinationPath = destinationPath ?? Path.ChangeExtension(sourcePath, null);
#if USE_NEW
                var archive = new FarcArchiveBin();
                using (var file = File.Open(sourcePath, FileMode.Open))
                {
                    archive = serial.Deserialize <FarcArchiveBin>(file);
                    archive.Unpack(destinationPath);
                }
#else
                var archive = new FarcArchive();
                archive.Load(sourcePath);

                using (Stream source = File.OpenRead(sourcePath))
                {
                    foreach (var entry in archive)
                    {
                        using (Stream entrySource = new SubStream(source, entry.Position, entry.Length))
                            using (Stream destination = File.Create(Path.Combine(destinationPath, entry.FileName)))
                            {
                                if (archive.IsEncrypted)
                                {
                                    using (AesManaged aes = new AesManaged
                                    {
                                        KeySize = 128,
                                        Key = FarcArchive.FarcEncryptionKeyBytes,
                                        BlockSize = 128,
                                        Mode = CipherMode.ECB,
                                        Padding = PaddingMode.Zeros,
                                        IV = new byte[16],
                                    })
                                        using (CryptoStream cryptoStream = new CryptoStream(
                                                   entrySource,
                                                   aes.CreateDecryptor(),
                                                   CryptoStreamMode.Read))
                                        {
                                            if (archive.IsCompressed && entry.Length != entry.CompressedLength)
                                            {
                                                using (GZipStream gzipStream = new GZipStream(cryptoStream, CompressionMode.Decompress))
                                                {
                                                    gzipStream.CopyTo(destination);
                                                }
                                            }

                                            else
                                            {
                                                cryptoStream.CopyTo(destination);
                                            }
                                        }
                                }

                                else if (archive.IsCompressed && entry.Length != entry.CompressedLength)
                                {
                                    using (GZipStream gzipStream = new GZipStream(entrySource, CompressionMode.Decompress))
                                    {
                                        gzipStream.CopyTo(destination);
                                    }
                                }

                                else
                                {
                                    entrySource.CopyTo(destination);
                                }
                            }
                    }
                }
#endif
            }

            else if (File.GetAttributes(sourcePath).HasFlag(FileAttributes.Directory))
            {
#if USE_NEW
                var archive = new FarcArchiveBin();
                archive.Alignment = (int)alignment;
#else
                var archive = new FarcArchive();
                archive.Alignment = alignment;
#endif
                archive.IsCompressed = compress;
#if DEBUG
                archive.Alignment = 64;
#endif
                destinationPath = destinationPath ?? sourcePath + ".farc";

                foreach (string fileName in Directory.GetFiles(sourcePath))
                {
#if USE_NEW
                    archive.Add(new FarcEntryBin(fileName));
#else
                    archive.Add(new FarcEntry
                    {
                        FileName = Path.GetFileName(fileName),
                        FilePath = new FileInfo(fileName)
                    });
#endif
                }

#if USE_NEW
                archive.Flush();
                using (var save = File.Create(destinationPath))
                {
                    serial.Serialize(save, archive);
                }
#else
                archive.Save(destinationPath);
#endif
            }
        }
Ejemplo n.º 4
0
        public static void Compress(string sourceFileName, string destinationFileName)
        {
            bool compress  = false;
            int  alignment = 16;

            if (destinationFileName == null)
            {
                destinationFileName = sourceFileName;
            }

            if (File.GetAttributes(sourceFileName).HasFlag(FileAttributes.Directory))
            {
                destinationFileName = Path.ChangeExtension(destinationFileName, "farc");

                var farcArchive = new FarcArchive();
                farcArchive.IsCompressed = compress;
                farcArchive.Alignment    = alignment;

                foreach (var filePath in Directory.EnumerateFiles(sourceFileName))
                {
                    farcArchive.Add(Path.GetFileName(filePath), filePath);
                }

                farcArchive.Save(destinationFileName);
            }

            else if (sourceFileName.EndsWith(".farc", StringComparison.OrdinalIgnoreCase))
            {
                destinationFileName = Path.ChangeExtension(destinationFileName, null);

                using (var stream = File.OpenRead(sourceFileName))
                {
                    var farcArchive = FarcArchive.Load <FarcArchive>(stream);

                    Directory.CreateDirectory(destinationFileName);
                    foreach (var fileName in farcArchive)
                    {
                        using (var destination = File.Create(Path.Combine(destinationFileName, fileName)))
                            using (var source = farcArchive.Open(fileName, EntryStreamMode.OriginalStream))
                                source.CopyTo(destination);
                    }
                }
            }
            else if (sourceFileName.EndsWith(".bak", StringComparison.OrdinalIgnoreCase))
            {
                destinationFileName = Path.ChangeExtension(destinationFileName, null);

                using (var stream = File.OpenRead(sourceFileName))
                {
                    var farcArchive = FarcArchive.Load <FarcArchive>(stream);

                    Directory.CreateDirectory(destinationFileName);
                    foreach (var fileName in farcArchive)
                    {
                        using (var destination = File.Create(Path.Combine(destinationFileName, fileName)))
                            using (var source = farcArchive.Open(fileName, EntryStreamMode.OriginalStream))
                                source.CopyTo(destination);
                    }
                }
            }
        }
Ejemplo n.º 5
0
        static void Main(string[] args)
        {
            if (args.Length < 1)
            {
                Console.WriteLine(Properties.Resources.HelpText);
                Console.ReadLine();
                return;
            }

            string sourceFileName      = null;
            string destinationFileName = null;

            foreach (var arg in args)
            {
                if (sourceFileName == null)
                {
                    sourceFileName = arg;
                }

                else if (destinationFileName == null)
                {
                    destinationFileName = arg;
                }
            }

            if (destinationFileName == null)
            {
                destinationFileName = sourceFileName;
            }

            if (File.GetAttributes(sourceFileName).HasFlag(FileAttributes.Directory))
            {
                destinationFileName = Path.ChangeExtension(destinationFileName, "farc");

                var farcArchive = new FarcArchive();
                foreach (var filePath in Directory.EnumerateFiles(sourceFileName))
                {
                    farcArchive.Add(Path.GetFileName(filePath), filePath);
                }

                farcArchive.Save(destinationFileName);
            }

            else if (args[0].EndsWith(".farc", StringComparison.OrdinalIgnoreCase))
            {
                destinationFileName = Path.ChangeExtension(destinationFileName, null);

                using (var stream = File.OpenRead(sourceFileName))
                {
                    var farcArchive = FarcArchive.Load <FarcArchive>(stream);

                    Directory.CreateDirectory(destinationFileName);
                    foreach (var fileName in farcArchive)
                    {
                        using (var destination = File.Create(Path.Combine(destinationFileName, fileName)))
                            using (var source = farcArchive.Open(fileName, EntryStreamMode.OriginalStream))
                                source.CopyTo(destination);
                    }
                }
            }
        }
Ejemplo n.º 6
0
        static void Main(string[] args)
        {
            if (args.Length < 1)
            {
                Console.WriteLine("FARC Pack");
                Console.WriteLine("=========");
                Console.WriteLine("Packer/unpacker for .FARC files.\n");
                Console.WriteLine("Usage:");
                Console.WriteLine("    FarcPack [options] [source] [destination]");
                Console.WriteLine("    Source can be either FARC or directory.");
                Console.WriteLine("    Destination can be left empty.\n");
                Console.WriteLine("Options:");
                Console.WriteLine("    -a, -alignment         Set alignment for output FARC.");
                Console.WriteLine("                           16 by default.\n");
                Console.WriteLine("    -c, --compress         Compress files in output FARC.");
                Console.WriteLine("                           Disabled by default.");
                Console.ReadLine();
                return;
            }

            string sourcePath      = null;
            string destinationPath = null;

            bool compress  = false;
            uint alignment = 16;

            for (int i = 0; i < args.Length; i++)
            {
                string arg = args[i];

                if (arg.Equals("-a", StringComparison.OrdinalIgnoreCase) ||
                    arg.Equals("-alignment", StringComparison.OrdinalIgnoreCase))
                {
                    alignment = uint.Parse(args[++i]);
                }

                else if (arg.Equals("-c", StringComparison.OrdinalIgnoreCase) ||
                         arg.Equals("-compress", StringComparison.OrdinalIgnoreCase))
                {
                    compress = true;
                }

                else if (sourcePath == null)
                {
                    sourcePath = arg;
                }

                else if (destinationPath == null)
                {
                    destinationPath = arg;
                }
            }

            if (sourcePath == null)
            {
                throw new ArgumentException("You must provide a source.", nameof(sourcePath));
            }

            if (sourcePath.EndsWith(".farc", StringComparison.OrdinalIgnoreCase))
            {
                FarcArchive archive = new FarcArchive();
                archive.Load(sourcePath);

                if (destinationPath == null)
                {
                    destinationPath = Path.ChangeExtension(sourcePath, null);
                }

                Directory.CreateDirectory(destinationPath);

                using (Stream source = File.OpenRead(sourcePath))
                {
                    foreach (FarcEntry entry in archive)
                    {
                        using (Stream entrySource = new SubStream(source, entry.Position, entry.Length))
                            using (Stream destination = File.Create(Path.Combine(destinationPath, entry.FileName)))
                            {
                                if (archive.IsEncrypted)
                                {
                                    using (AesManaged aes = new AesManaged
                                    {
                                        KeySize = 128,
                                        Key = FarcArchive.FarcEncryptionKeyBytes,
                                        BlockSize = 128,
                                        Mode = CipherMode.ECB,
                                        Padding = PaddingMode.Zeros,
                                        IV = new byte[16],
                                    })
                                        using (CryptoStream cryptoStream = new CryptoStream(
                                                   entrySource,
                                                   aes.CreateDecryptor(),
                                                   CryptoStreamMode.Read))
                                        {
                                            if (archive.IsCompressed && entry.Length != entry.CompressedLength)
                                            {
                                                using (GZipStream gzipStream = new GZipStream(cryptoStream, CompressionMode.Decompress))
                                                {
                                                    gzipStream.CopyTo(destination);
                                                }
                                            }

                                            else
                                            {
                                                cryptoStream.CopyTo(destination);
                                            }
                                        }
                                }

                                else if (archive.IsCompressed && entry.Length != entry.CompressedLength)
                                {
                                    using (GZipStream gzipStream = new GZipStream(entrySource, CompressionMode.Decompress))
                                    {
                                        gzipStream.CopyTo(destination);
                                    }
                                }

                                else
                                {
                                    entrySource.CopyTo(destination);
                                }
                            }
                    }
                }
            }

            else if (File.GetAttributes(sourcePath).HasFlag(FileAttributes.Directory))
            {
                FarcArchive archive = new FarcArchive();
                archive.Alignment    = alignment;
                archive.IsCompressed = compress;

                if (destinationPath == null)
                {
                    destinationPath = sourcePath + ".farc";
                }

                foreach (string fileName in Directory.GetFiles(sourcePath))
                {
                    archive.Add(new FarcEntry
                    {
                        FileName = Path.GetFileName(fileName),
                        FilePath = new FileInfo(fileName)
                    });
                }

                archive.Save(destinationPath);
            }
        }