Example #1
0
        public static async Task <BarFile> Create(string root, uint version)
        {
            BarFile barFile = new BarFile();

            if (!Directory.Exists(root))
            {
                throw new Exception("Directory does not exist!");
            }

            var fileInfos = Directory.GetFiles(root, "*", SearchOption.AllDirectories)
                            .Select(fileName => new FileInfo(fileName)).ToArray();



            if (root.EndsWith(Path.DirectorySeparatorChar.ToString()))
            {
                root = root[0..^ 1];
Example #2
0
        public async static Task <BarFile> Load(string filename, bool doCRC32)
        {
            BarFile barFile = new BarFile();

            if (!File.Exists(filename))
            {
                throw new Exception("BAR file does not exist!");
            }
            using var file = File.OpenRead(filename);
            var reader = new BinaryReader(file);

            barFile.barFileHeader = new BarFileHeader(reader);
            file.Seek(barFile.barFileHeader.FilesTableOffset, SeekOrigin.Begin);
            var rootNameLength = reader.ReadUInt32();

            barFile.RootPath          = Encoding.Unicode.GetString(reader.ReadBytes((int)rootNameLength * 2));
            barFile.NumberOfRootFiles = reader.ReadUInt32();

            var barFileEntrys = new List <BarEntry>();

            for (uint i = 0; i < barFile.NumberOfRootFiles; i++)
            {
                barFileEntrys.Add(BarEntry.Load(reader, barFile.barFileHeader.Version, barFile.RootPath));
            }

            // TODO: Look for new date fields in BAR version 6


            if (doCRC32)
            {
                foreach (var barEntry in barFileEntrys)
                {
                    reader.BaseStream.Seek(barEntry.Offset, SeekOrigin.Begin);
                    byte[] data = reader.ReadBytes(barEntry.FileSize2);
                    await Task.Run(() =>
                    {
                        barEntry.CRC32 = Crc32Algorithm.Compute(data);
                    }
                                   );
                }
            }

            barFile.BarFileEntrys = new ReadOnlyCollection <BarEntry>(barFileEntrys);
            return(barFile);
        }