Exemple #1
0
 public byte[] ToByteArray()
 {
     using var ms = new MemoryStream();
     using (var bw = new BinaryWriter(ms))
     {
         bw.Write(BarFileHeader.ToByteArray());
         bw.Write(BarFileBody.ToByteArray());
     }
     return(ms.ToArray());
 }
Exemple #2
0
        public static async Task CreateBarFileAsync(IEnumerable <FileInfo> fileInfos, string inputPath,
                                                    string outputFileName, string rootdir, bool ignoreLastWriteTime = true)
        {
            if (inputPath.EndsWith(Path.DirectorySeparatorChar.ToString()))
            {
                inputPath = inputPath.Substring(0, inputPath.Length - 1);
            }

            var outputFolder = Path.GetDirectoryName(outputFileName);

            if (outputFolder != null && !Directory.Exists(outputFolder))
            {
                Directory.CreateDirectory(outputFolder);
            }

            var uncompressedBarFiles = await Task.WhenAll(fileInfos.Select(t => EnsureFileIsExtracted(t)));

            using var barFileStream = File.Open(outputFileName, FileMode.Create, FileAccess.Write, FileShare.None);

            //Write Bar Header
            var header      = new BarFileHeader(Path.GetFileName(outputFileName), uncompressedBarFiles);
            var headerBytes = header.ToByteArray();
            await barFileStream.WriteAsync(headerBytes, 0, headerBytes.Length);

            //Write Files
            var barEntrys = new List <BarEntry>();

            foreach (var barEntry in uncompressedBarFiles)
            {
                barEntrys.Add(new BarEntry(inputPath, barEntry, (int)barFileStream.Position, ignoreLastWriteTime));

                using var uncompressedEntryFileStream = File.Open(barEntry.FullName, FileMode.Open, FileAccess.Read, FileShare.Read);
                await uncompressedEntryFileStream.BufferedCopyToAsync(barFileStream);
            }

            //Write Bar Entrys
            var end = new BarFile(rootdir, barEntrys);

            using var bw = new BinaryWriter(barFileStream);

            end.WriteToBinaryWriter(bw);
        }
Exemple #3
0
        public static void CreateBarFile(IReadOnlyCollection <FileInfo> fileInfos, string inputPath,
                                         string outputFileName, string rootdir, bool ignoreLastWriteTime = true)
        {
            if (inputPath.EndsWith(Path.DirectorySeparatorChar.ToString()))
            {
                inputPath = inputPath.Substring(0, inputPath.Length - 1);
            }

            var folder = Path.GetDirectoryName(outputFileName);

            if (folder != null && !Directory.Exists(folder))
            {
                Directory.CreateDirectory(folder);
            }

            var newFilesInfos = new List <FileInfo>();

            foreach (var file in fileInfos.ToArray())
            {
                if (file.FullName.EndsWith(".age4scn", StringComparison.OrdinalIgnoreCase) &&
                    L33TZipUtils.IsL33TZipFile(file.FullName))
                {
                    var data = L33TZipUtils.ExtractL33TZipFile(file.FullName);
                    File.Delete(file.FullName);
                    File.WriteAllBytes(file.FullName, data);
                    newFilesInfos.Add(new FileInfo(file.FullName));
                }
                else
                {
                    newFilesInfos.Add(file);
                }
            }

            using (var fileStream = File.Open(outputFileName, FileMode.Create, FileAccess.Write, FileShare.None))
            {
                using (var writer = new BinaryWriter(fileStream))
                {
                    //Write Bar Header
                    var header = new BarFileHeader(Path.GetFileName(outputFileName), newFilesInfos);
                    writer.Write(header.ToByteArray());

                    //Write Files
                    var barEntrys = new List <BarEntry>();
                    foreach (var file in newFilesInfos)
                    {
                        var filePath = file.FullName;
                        barEntrys.Add(new BarEntry(inputPath, file, (int)writer.BaseStream.Position,
                                                   ignoreLastWriteTime));
                        using (var fileStream2 = File.Open(filePath, FileMode.Open, FileAccess.Read,
                                                           FileShare.Read))
                        {
                            using (var binReader = new BinaryReader(fileStream2))
                            {
                                var buffer = new byte[4096];
                                int read;
                                while ((read = binReader.Read(buffer, 0, buffer.Length)) > 0)
                                {
                                    writer.Write(buffer, 0, read);
                                }
                            }
                        }
                    }

                    //Write Bar Entrys
                    var end = new BarFile(rootdir, barEntrys);
                    writer.Write(end.ToByteArray());
                }
            }
        }
        public static void CreateBarFile(IReadOnlyCollection <FileInfo> fileInfos, string inputPath,
                                         string outputFileName, string rootDir, bool ignoreLastWriteTime = true, bool xmlToXmb = true)
        {
            if (inputPath.EndsWith(Path.DirectorySeparatorChar.ToString()))
            {
                inputPath = inputPath.Substring(0, inputPath.Length - 1);
            }

            var folder = Path.GetDirectoryName(outputFileName);

            if (folder != null && !Directory.Exists(folder))
            {
                Directory.CreateDirectory(folder);
            }

            var newFilesInfos = new List <FileInfo>();

            foreach (var file in fileInfos.ToArray())
            {
                if (file.FullName.EndsWith(".age4scn", StringComparison.OrdinalIgnoreCase))
                {
                    if (L33TZipUtils.IsL33TZip(file.FullName))
                    {
                        var data = L33TZipUtils.DecompressL33TZip(file.FullName);
                        File.Delete(file.FullName);
                        File.WriteAllBytes(file.FullName, data);
                        newFilesInfos.Add(new FileInfo(file.FullName));
                    }
                    else
                    {
                        newFilesInfos.Add(file);
                    }
                }
                else if (xmlToXmb && file.FullName.EndsWith(".quest", StringComparison.OrdinalIgnoreCase) ||
                         file.FullName.EndsWith(".region", StringComparison.OrdinalIgnoreCase) ||
                         file.FullName.EndsWith(".tactics", StringComparison.OrdinalIgnoreCase) ||
                         file.FullName.EndsWith(".character", StringComparison.OrdinalIgnoreCase) ||
                         file.FullName.EndsWith(".dataset", StringComparison.OrdinalIgnoreCase) ||
                         file.FullName.EndsWith(".empire", StringComparison.OrdinalIgnoreCase) ||
                         file.FullName.EndsWith(".spawneritem", StringComparison.OrdinalIgnoreCase) ||
                         file.FullName.EndsWith(".groupingset", StringComparison.OrdinalIgnoreCase) ||
                         file.FullName.EndsWith(".set", StringComparison.OrdinalIgnoreCase) ||
                         file.FullName.EndsWith(".xml", StringComparison.OrdinalIgnoreCase))
                {
                    if (XmbFile.IsXmlFile(file.FullName))
                    {
                        XmbFileUtils.XmlFileToXmbFile(file.FullName, file.FullName + ".xmb");
                        File.Delete(file.FullName);
                        newFilesInfos.Add(new FileInfo(file.FullName + ".xmb"));
                    }
                    else
                    {
                        newFilesInfos.Add(file);
                    }
                }
                else
                {
                    newFilesInfos.Add(file);
                }
            }

            using var fileStream = File.Open(outputFileName, FileMode.Create, FileAccess.Write, FileShare.None);
            using var writer     = new BinaryWriter(fileStream);
            //Write Bar Header
            var header = new BarFileHeader(Path.GetFileName(outputFileName), newFilesInfos);

            writer.Write(header.ToByteArray());

            //Write Files
            var barEntrys = new List <BarEntry>();

            foreach (var file in newFilesInfos)
            {
                var filePath = file.FullName;
                barEntrys.Add(new BarEntry(inputPath, file, (int)writer.BaseStream.Position,
                                           ignoreLastWriteTime));
                using var fileStream2 = File.Open(filePath, FileMode.Open, FileAccess.Read,
                                                  FileShare.Read);
                using var binReader = new BinaryReader(fileStream2);
                var buffer = new byte[4096];
                int read;
                while ((read = binReader.Read(buffer, 0, buffer.Length)) > 0)
                {
                    writer.Write(buffer, 0, read);
                }
            }

            //Write Bar Entrys
            var end = new BarFileBody(rootDir, barEntrys);

            writer.Write(end.ToByteArray());
        }