Пример #1
0
        public void Save(Stream output, IList <IArchiveFileInfo> files)
        {
            using var bw = new BinaryWriterX(output);

            // Calculate offsets
            var metaOffset     = BlockSize;
            var blockOffset    = metaOffset + _header.entryCount * MetaSize;
            var blockOffsetEnd = blockOffset + BlockOffsetHeaderSize + files.Count * BlockOffsetSize;
            var fileOffset     = (blockOffsetEnd + (BlockSize - 1)) & ~(BlockSize - 1);

            // Write files
            var blockOffsets = new List <NlpBlockOffset>();
            var metas        = Enumerable.Repeat(new NlpMeta(), _header.entryCount).ToArray();

            var filePosition = fileOffset;

            foreach (var file in files.Cast <NlpArchiveFileInfo>())
            {
                // Write file
                output.Position = filePosition;
                var writtenSize = file.SaveFileData(output);
                bw.WriteAlignment(BlockSize);

                // Update meta entry
                AdjustMeta(file);
                metas[file.Id] = file.Meta;

                // Add block offset entry
                blockOffsets.Add(new NlpBlockOffset
                {
                    metaId = file.Id,
                    offset = (filePosition - BlockSize) / BlockSize
                });

                filePosition += (int)((writtenSize + (BlockSize - 1)) & ~(BlockSize - 1));
            }

            // Write metas
            output.Position = metaOffset;
            bw.WriteMultiple(metas);

            // Write block offsets
            output.Position = blockOffset;
            bw.WriteType(new NlpBlockOffsetHeader
            {
                entryCount = files.Count,
                offset     = blockOffset + BlockOffsetHeaderSize - BlockSize
            });
            bw.WriteMultiple(blockOffsets);
            bw.WriteAlignment(BlockSize);

            // Write header
            output.Position = 0;

            _header.fileBlockOffset    = (fileOffset - BlockSize) / BlockSize;
            _header.entryCount         = metas.Length;
            _header.blockEntriesOffset = blockOffset - BlockSize;
            _header.unkOffset          = blockOffsetEnd - BlockSize;
            bw.WriteType(_header);
        }
Пример #2
0
        public void Save(Stream output)
        {
            using (BinaryWriterX bw = new BinaryWriterX(output))
            {
                var  files  = Files.OrderBy(x => x.entry.crc32);
                uint offset = (uint)(((0xc + (uint)files.Count() * 0xc) + 0x7f) & ~0x7f);

                //entries
                bw.BaseStream.Position = 0xc;
                foreach (var file in files)
                {
                    var entry = new FileEntry
                    {
                        offset = offset,
                        size   = (uint)file.FileSize,
                        crc32  = file.entry.crc32
                    };
                    bw.WriteStruct(entry);
                }
                bw.WriteAlignment(0x100, 0xac);

                //Files
                foreach (var file in files)
                {
                    file.FileData.CopyTo(bw.BaseStream);
                    bw.WriteAlignment(0x100, 0xac);
                }

                //Header
                bw.BaseStream.Position = 0;
                header.fileSize        = (uint)bw.BaseStream.Length;
                bw.WriteStruct(header);
            }
        }
Пример #3
0
        public void Write(Stream input, uint offset)
        {
            using (var bw = new BinaryWriterX(input, true))
            {
                bw.BaseStream.Position = offset;

                if (State == ArchiveFileState.Archived)
                {
                    base.FileData.CopyTo(bw.BaseStream);
                    bw.WriteAlignment(0x10, (byte)new Random().Next());
                }
                else
                {
                    if (entry.decompSize == 0)
                    {
                        entry.offset   = offset;
                        entry.compSize = (uint)base.FileData.Length;
                        base.FileData.CopyTo(bw.BaseStream);

                        bw.WriteAlignment(0x10, (byte)new Random().Next());
                    }
                    else
                    {
                        entry.offset     = offset;
                        entry.decompSize = (uint)base.FileData.Length;
                        var comp = ZLib.Compress(base.FileData);
                        entry.compSize = (uint)comp.Length;
                        bw.Write(comp);

                        bw.WriteAlignment(0x10, (byte)new Random().Next());
                    }
                }
            }
        }
Пример #4
0
        public void Save(string filename)
        {
            using (BinaryWriterX bw = new BinaryWriterX(File.OpenWrite(filename)))
            {
                var sjis = Encoding.GetEncoding("SJIS");

                //Update entries
                uint relOffset = 0;
                var  count     = 1;
                foreach (var label in Labels)
                {
                    if (count < offsets.Count)
                    {
                        uint byteCount = (uint)sjis.GetByteCount(label.Text.Replace("\n", "\\n").Replace("\xa", "\\n")) + 1;

                        foreach (var entry in entries)
                        {
                            foreach (var data in entry.data)
                            {
                                if (data.type == 0 && data.value == offsets[count])
                                {
                                    data.value = relOffset + byteCount;
                                }
                            }
                        }

                        relOffset += byteCount;
                        count++;
                    }
                }

                //Header
                header.stringSecSize = (int)(relOffset + 0xf) & ~0xf;
                bw.WriteStruct(header);

                //Entries
                foreach (var entry in entries)
                {
                    bw.Write(entry.entryTypeID);
                    bw.Write(entry.entryLength);
                    bw.Write(entry.typeMask);
                    foreach (var data in entry.data)
                    {
                        bw.Write(data.value);
                    }
                }
                bw.WriteAlignment(0x10, 0xff);

                //Text
                foreach (var label in Labels)
                {
                    bw.Write(sjis.GetBytes(label.Text.Replace("\n", "\\n").Replace("\xa", "\\n")));
                    bw.Write((byte)0);
                }
                bw.WriteAlignment(0x10, 0xff);

                //Signature
                bw.Write(sig);
            }
        }
Пример #5
0
        /// <summary>
        /// Saves the metadata and files into a PAC archive.
        /// </summary>
        /// <param name="output">An output stream for a PAC archive.</param>
        /// <param name="files">The files to save.</param>
        /// <returns>True if successful.</returns>
        public bool Save(Stream output, IList <ArchiveFileInfo> files)
        {
            using (var bw = new BinaryWriterX(output, true))
            {
                // Header
                bw.WriteType(_header);
                var offsetPosition = bw.BaseStream.Position;

                // Skip Offsets
                bw.BaseStream.Position += _header.FileCount * sizeof(int);
                bw.WriteAlignment(EntryAlignment);

                // Entries
                bw.WriteMultiple(_entries);
                bw.WriteAlignment(FileAlignment);

                // Files
                var offsets = new List <int>();
                foreach (var afi in files)
                {
                    offsets.Add((int)bw.BaseStream.Position);
                    bw.Write((int)afi.FileSize);
                    bw.Write(FileAlignment);
                    bw.WriteAlignment(FileAlignment);
                    afi.SaveFileData(bw.BaseStream, null);
                    bw.WriteAlignment(FileAlignment);
                }

                // Offsets
                bw.BaseStream.Position = offsetPosition;
                bw.WriteMultiple(offsets);
            }

            return(true);
        }
Пример #6
0
 public uint Write(Stream stream)
 {
     using (var bw = new BinaryWriterX(stream, true))
     {
         if (State == ArchiveFileState.Archived)
         {
             base.FileData.Position = 0;
             base.FileData.CopyTo(bw.BaseStream);
             return((uint)base.FileData.Length);
         }
         else
         {
             if (compressed)
             {
                 var comp = ZLib.Compress(FileData);
                 bw.Write(comp.Length);
                 bw.Write((comp.Length + 0xf) & ~0xf);
                 bw.WritePadding(0x18);
                 bw.Write(comp);
                 bw.WriteAlignment();
                 return((uint)((comp.Length + 0xf) & ~0xf) + 0x20);
             }
             else
             {
                 bw.Write((uint)FileData.Length);
                 bw.Write((uint)((FileData.Length + 0xf) & ~0xf));
                 bw.WritePadding(0x18);
                 FileData.CopyTo(bw.BaseStream);
                 bw.WriteAlignment();
                 return((uint)((FileData.Length + 0xf) & ~0xf) + 0x20);
             }
         }
     }
 }
Пример #7
0
        /// <summary>
        ///
        /// </summary>
        /// <param name="output"></param>
        public void Save(Stream output)
        {
            using (var bw = new BinaryWriterX(output, true))
            {
                // Updates
                Header.Width  = (short)Texture.Width;
                Header.Height = (short)Texture.Height;

                // Header
                bw.WriteType(Header);
                bw.WriteAlignment();
                bw.WriteString(FileName, Encoding.ASCII, false);
                bw.WriteAlignment();

                // Setup
                if (Header.Format == ImageFormat.Palette_8)
                {
                    var settings = new PaletteImageSettings(Formats[ImageFormat.RGBA8888], PaletteFormats[ImageFormat.Palette_8], Header.Width, Header.Height);
                    var data     = Common.Save(Texture, Palette, settings);

                    bw.Write(data.indexData);
                    bw.Write(data.paletteData);
                }
                else
                {
                    var settings = new ImageSettings(Formats[ImageFormat.RGBA8888], Header.Width, Header.Height);
                    var data     = Common.Save(Texture, settings);

                    bw.Write(data);
                }
            }
        }
Пример #8
0
        public void Save(Stream output)
        {
            ncsdHeader.rsa     = new byte[0x100];
            ncsdHeader.shaHash = (Files.Count(f => f.PartitionID == 0) > 0) ? Kontract.Hash.SHA256.Create(Files.Where(f => f.PartitionID == 0).First().FileData, 0x200, 0x800) : new byte[0x20];
            ncsdHeader.cardInfoHeader.copyFirstNCCHHeader = new BinaryReaderX(new SubStream(Files.First().FileData, 0x100, 0x100), true).ReadAllBytes();

            using (var bw = new BinaryWriterX(output))
            {
                bw.BaseStream.Position = 0x4000;
                foreach (var file in Files)
                {
                    ncsdHeader.partEntries[file.PartitionID].offset = (uint)(bw.BaseStream.Position / mediaUnitSize);
                    ncsdHeader.partEntries[file.PartitionID].size   = (uint)(file.FileSize / mediaUnitSize);

                    file.FileData.CopyTo(bw.BaseStream);
                    bw.WriteAlignment(mediaUnitSize);
                }

                ncsdHeader.ncsdSize = (uint)(bw.BaseStream.Length / mediaUnitSize);

                bw.BaseStream.Position = 0;
                ncsdHeader.Write(bw.BaseStream);
                bw.WriteAlignment(0x4000, 0xFF);
            }
        }
Пример #9
0
        public void Save(string filename)
        {
            var sjis = Encoding.GetEncoding("SJIS");

            //Update TextOffsets
            int textOffset = 0;
            int labelCount = 0;
            int pageCount  = 0;

            for (int j = 0; j < entries.Count; j++)
            {
                if (labelCount < Labels.Count && Labels[labelCount].Text.Length >= 4 && Labels[labelCount].Text.Substring(0, 4) == "<NP>")
                {
                    pageCount = 0;
                }
                for (int i = 0; i < entries[j].metaInfo.Count; i++)
                {
                    var meta = entries[j].metaInfo[i];
                    if (meta.type == 1 && i == 1 && entries[j].metaInfo.Count(m => m.type == 0) > 0)
                    {
                        entries[j].metaInfo[i].value = pageCount++;
                    }
                    if (meta.type == 0)
                    {
                        entries[j].metaInfo[i].value = (Labels[labelCount].Text == String.Empty) ? -1 : textOffset;
                        textOffset += (Labels[labelCount].Text == String.Empty) ? 0 : sjis.GetByteCount(Labels[labelCount].Text.Replace("<NP>", "").TrimEnd('\n')) + 1;
                        labelCount++;
                    }
                }
            }

            using (var bw = new BinaryWriterX(File.Create(filename)))
            {
                //Write Texts
                bw.BaseStream.Position = cfgHeader.dataOffset;
                foreach (var label in Labels)
                {
                    if (label.Text != String.Empty)
                    {
                        bw.Write(sjis.GetBytes(label.Text.Replace("<NP>", "").TrimEnd('\n') + "\0"));
                    }
                }
                cfgHeader.dataLength = (uint)bw.BaseStream.Position - cfgHeader.dataOffset;
                bw.WriteAlignment(16, 0xff);

                //Write Entries
                bw.BaseStream.Position = 0x10;
                foreach (var entry in entries)
                {
                    entry.Write(bw.BaseStream);
                }
                bw.WriteAlignment(16, 0xff);

                //Write Header
                bw.BaseStream.Position = 0;
                bw.WriteStruct(cfgHeader);
            }
        }
Пример #10
0
        public void Save(Stream output, IList <IArchiveFileInfo> files, IProgressContext progress)
        {
            // Group files by directory
            var castedFiles = files.Cast <XfsaArchiveFileInfo <Xfsa2FileEntry> >();

            // Build directory, file, and name tables
            BuildTables(castedFiles, out var directoryEntries, out var directoryHashes, out var fileEntries, out var nameStream);

            // -- Write file --

            using var bw           = new BinaryWriterX(output);
            bw.BaseStream.Position = _headerSize;

            // Write directory entries
            _header.directoryEntriesCount  = (short)directoryEntries.Count;
            _header.directoryEntriesOffset = _headerSize;

            XfsaSupport.WriteCompressedTableEntries(bw.BaseStream, directoryEntries);
            bw.WriteAlignment(4);

            // Write directory hashes
            _header.directoryHashCount  = (short)directoryHashes.Count;
            _header.directoryHashOffset = (int)bw.BaseStream.Position;

            XfsaSupport.WriteCompressedTableEntries(bw.BaseStream, directoryHashes);
            bw.WriteAlignment(4);

            // Write file entry hashes
            _header.fileEntriesCount  = fileEntries.Count;
            _header.fileEntriesOffset = (int)bw.BaseStream.Position;

            XfsaSupport.WriteCompressedTableEntries(bw.BaseStream, fileEntries.Select(x => x.Entry));
            bw.WriteAlignment(4);

            // Write name table
            _header.nameOffset = (int)bw.BaseStream.Position;

            var nameStreamComp = new MemoryStream();

            XfsaSupport.Compress(nameStream, nameStreamComp, Level5CompressionMethod.Lz10);
            nameStreamComp.CopyTo(bw.BaseStream);
            bw.WriteAlignment(4);

            // Write file data
            _header.dataOffset = (int)bw.BaseStream.Position;

            foreach (var fileEntry in fileEntries)
            {
                bw.BaseStream.Position = _header.dataOffset + fileEntry.Entry.FileOffset;
                fileEntry.SaveFileData(bw.BaseStream, null);
            }

            // Write header
            bw.BaseStream.Position = 0;
            bw.WriteType(_header);
        }
Пример #11
0
        public void Save(Stream output)
        {
            using (var bw = new BinaryWriterX(output))
            {
                //Header
                bw.WriteASCII("XBB");
                bw.Write((byte)1);
                bw.Write(Files.Count());
                bw.BaseStream.Position = 0x20;

                var offset = 0x20 + Files.Sum(e => e.FileName.Length + 1 + 0x18);
                offset = offset + 0x7f & ~0x7f;
                var nameOffset = 0x20 + Files.Count() * 0x18;

                //FileEntries
                foreach (var file in Files)
                {
                    bw.Write(offset);
                    bw.Write((uint)file.FileSize);
                    bw.Write(nameOffset);
                    bw.Write(XbbHash.Create(file.FileName));

                    offset += (int)file.FileSize;
                    offset  = offset + 0x7f & ~0x7f;

                    nameOffset += file.FileName.Length + 1;
                }

                //Hash table
                var files = Files.OrderBy(e => XbbHash.Create(e.FileName)).ToList();
                for (int i = 0; i < files.Count(); i++)
                {
                    var hash = XbbHash.Create(files[i].FileName);
                    bw.Write(hash);
                    bw.Write(Files.FindIndex(e => XbbHash.Create(e.FileName) == hash));
                }

                //nameList
                foreach (var file in Files)
                {
                    bw.WriteASCII(file.FileName);
                    bw.Write((byte)0);
                }

                //FileData
                foreach (var file in Files)
                {
                    bw.WriteAlignment(0x80);
                    file.FileData.CopyTo(bw.BaseStream);
                }

                bw.WriteAlignment(0x80);
            }
        }
Пример #12
0
        public void Save(Stream output, ImageInfo imageInfo)
        {
            using var bw = new BinaryWriterX(output);
            var encoding = LimgSupport.LimgFormats[imageInfo.ImageFormat];

            // Split into tiles
            var(tileIndices, imageStream) = SplitTiles(imageInfo.ImageData, encoding.Item1.BitsPerValue);

            // Write palette
            bw.BaseStream.Position = _headerSize + _unkHeader.Length;

            _header.paletteOffset = (uint)bw.BaseStream.Position;
            _header.colorCount    = (short)(imageInfo.PaletteData.Length / _colorEntrySize);
            bw.Write(imageInfo.PaletteData);
            bw.WriteAlignment(4);

            // Write unknown tables
            _header.unkOffset1 = (short)bw.BaseStream.Position;
            _header.unkCount1  = (short)(_unkChunk1.Length / _unk1EntrySize);
            bw.Write(_unkChunk1);
            bw.WriteAlignment(4);

            _header.unkOffset2 = (short)bw.BaseStream.Position;
            _header.unkCount2  = (short)(_unkChunk2.Length / _unk2EntrySize);
            bw.Write(_unkChunk2);
            bw.WriteAlignment(4);

            // Write tiles
            _header.tileDataOffset = (short)bw.BaseStream.Position;
            _header.tileEntryCount = (short)tileIndices.Count;
            bw.WriteMultiple(tileIndices);
            bw.WriteAlignment(4);

            // Write imageInfo data
            _header.imageDataOffset = (short)bw.BaseStream.Position;
            _header.imageTileCount  = (short)(imageStream.Length / _tileEntrySize);

            imageStream.Position = 0;
            imageStream.CopyTo(bw.BaseStream);
            bw.WriteAlignment(4);

            // Header
            bw.BaseStream.Position = 0;

            _header.width        = (short)imageInfo.ImageSize.Width;
            _header.height       = (short)imageInfo.ImageSize.Height;
            _header.paddedWidth  = (short)((_header.width + 0xFF) & ~0xFF);
            _header.paddedHeight = (short)((_header.height + 0xFF) & ~0xFF);
            _header.imgFormat    = (short)imageInfo.ImageFormat;

            bw.WriteType(_header);
            bw.Write(_unkHeader);
        }
Пример #13
0
        public void Save(Stream output, IList <IArchiveFileInfo> files)
        {
            var darcTreeBuilder = new DarcTreeBuilder(Encoding.Unicode);

            darcTreeBuilder.Build(files.Select(x => ("/." + x.FilePath.FullName, x)).ToArray());

            var entries    = darcTreeBuilder.Entries;
            var nameStream = darcTreeBuilder.NameStream;

            var namePosition = _headerSize + entries.Count * _entrySize;
            var dataOffset   = (namePosition + (int)nameStream.Length + 3) & ~3;

            using var bw = new BinaryWriterX(output, _byteOrder);

            // Write names
            bw.BaseStream.Position = namePosition;
            nameStream.Position    = 0;
            nameStream.CopyTo(bw.BaseStream);
            bw.WriteAlignment(4);

            // Write files
            foreach (var(darcEntry, afi) in entries.Where(x => x.Item2 != null))
            {
                var alignment = 4;
                if (afi.FilePath.GetExtensionWithDot() == ".bclim")
                {
                    alignment = 0x80;
                }

                bw.WriteAlignment(alignment);
                var fileOffset = (int)bw.BaseStream.Position;

                var writtenSize = (afi as ArchiveFileInfo).SaveFileData(bw.BaseStream, null);

                darcEntry.offset = fileOffset;
                darcEntry.size   = (int)writtenSize;
            }

            // Write entries
            bw.BaseStream.Position = _headerSize;
            bw.WriteMultiple(entries.Select(x => x.Item1));

            // Write header
            bw.BaseStream.Position = 0;
            bw.WriteType(new DarcHeader
            {
                byteOrder   = _byteOrder,
                dataOffset  = dataOffset,
                fileSize    = (int)bw.BaseStream.Length,
                tableLength = entries.Count * _entrySize + (int)nameStream.Length
            });
        }
Пример #14
0
        public void Save(Stream input)
        {
            using (var bw = new BinaryWriterX(input))
            {
                //filesize
                bw.BaseStream.Position = 0x18;

                //FATO
                bw.WriteStruct(fatoHeader);
                bw.WriteAlignment(4, 0xff);
                for (int i = 0; i < fatoOffsets.Count; i++)
                {
                    bw.Write(fatoOffsets[i]);
                }

                //FATB
                bw.WriteStruct(fatbHeader);
                uint offset = 0;
                for (int i = 0; i < Files.Count; i++)
                {
                    fatbEntries[i].offset = offset;
                    offset += (uint)Files[i].FileSize;
                    offset  = (uint)(offset + 3 & ~3);
                    fatbEntries[i].endOffset = offset;

                    bw.WriteStruct(fatbEntries[i]);
                }

                var fimbOffset = bw.BaseStream.Position;
                bw.BaseStream.Position += 0xc;

                //Writing FileData
                var dataOffset = bw.BaseStream.Position;
                for (int i = 0; i < Files.Count; i++)
                {
                    Files[i].FileData.CopyTo(bw.BaseStream);
                    bw.WriteAlignment(4, 0xff);
                }

                //FIMB
                fimbHeader.dataSize    = (uint)bw.BaseStream.Length - (uint)dataOffset;
                bw.BaseStream.Position = fimbOffset;
                bw.WriteStruct(fimbHeader);

                //Header
                header.fileSize        = (uint)bw.BaseStream.Length;
                bw.BaseStream.Position = 0;
                bw.WriteStruct(header);
            }
        }
Пример #15
0
        public void Save(Stream output)
        {
            using (var bw = new BinaryWriterX(output))
            {
                // Collect all file entries
                var fileEntries       = new Tuple <FileEntry, FF1DpkArchiveFileInfo> [Files.Count];
                var fileEntryPosition = 0;
                foreach (var file in Files)
                {
                    var fileName    = file.FileName.Substring(0, Math.Min(0x16, file.FileName.Length));
                    var fileNameSum = fileName.Sum(x => (byte)x);

                    var fileEntry = new FileEntry
                    {
                        fileName         = fileName,
                        nameSum          = (short)fileNameSum,
                        uncompressedSize = (int)file.FileSize
                    };
                    fileEntries[fileEntryPosition++] = new Tuple <FileEntry, FF1DpkArchiveFileInfo>(fileEntry, file);
                }

                // Write file data ordered file entries name sums
                fileEntries = fileEntries.OrderBy(x => x.Item1.nameSum).ToArray();
                var filesOffset = 0x80 + Files.Count * 0x80;
                foreach (var fileEntryTuple in fileEntries)
                {
                    bw.BaseStream.Position = filesOffset;
                    fileEntryTuple.Item2.WriteFile(output);

                    fileEntryTuple.Item1.fileOffset     = filesOffset;
                    fileEntryTuple.Item1.compressedSize = (int)bw.BaseStream.Position - filesOffset;

                    bw.WriteAlignment(0x80);
                    filesOffset = (int)bw.BaseStream.Position;
                }

                // Write file entries ordered by file sum
                bw.BaseStream.Position = 0x80;
                foreach (var fileEntry in fileEntries)
                {
                    WriteFileEntry(bw, fileEntry.Item1);
                    bw.WriteAlignment(0x80);
                }

                // Header
                bw.BaseStream.Position = 0;
                bw.Write(Files.Count);
                bw.Write((int)bw.BaseStream.Length);
            }
        }
Пример #16
0
        public void Save(string filename)
        {
            var sjis = Encoding.GetEncoding("SJIS");

            using (BinaryWriterX bw = new BinaryWriterX(File.Create(filename)))
            {
                //Table 0
                bw.BaseStream.Position = 0x14;
                bw.WriteMultipleCompressed(table0, table0Comp);
                bw.WriteAlignment(4);

                //Table 1
                header.table1Offset = (int)bw.BaseStream.Position >> 2;

                uint relOffset = 0;
                var  count     = 1;
                foreach (var label in Labels)
                {
                    if (count == offsets.Count)
                    {
                        var byteCount = (uint)sjis.GetByteCount(label.Text) + 1;

                        foreach (var entry in entries)
                        {
                            if (entry.ident == 0x18 && entry.value == offsets[count])
                            {
                                entry.value = relOffset + byteCount;
                            }
                        }

                        relOffset += byteCount;
                        count++;
                    }
                }
                offsets = entries.Where(e => e.ident == 0x18).Select(e => e.value).Distinct().ToList();

                bw.WriteMultipleCompressed(entries, table1Comp);
                bw.WriteAlignment(4);

                //Text
                header.stringTableOffset = (int)bw.BaseStream.Position >> 2;
                bw.WriteStringsCompressed(Labels.Select(l => l.Text), stringComp, Encoding.GetEncoding("SJIS"));
                bw.WriteAlignment(4);

                //Header
                bw.BaseStream.Position = 0;
                bw.WriteStruct(header);
            }
        }
Пример #17
0
        public Tuple <uint, uint> Write(Stream input, uint compOffset, uint decompOffset)
        {
            using (var bw = new BinaryWriterX(input, true))
            {
                if (base.FileData.Length > 0)
                {
                    if (Entry.entry.compOffset != 0)
                    {
                        Entry.entry.compOffset = compOffset;
                    }
                    Entry.entry.decompOffset = decompOffset;
                }

                if (State == ArchiveFileState.Archived)
                {
                    base.FileData.CopyTo(bw.BaseStream);
                    bw.WriteAlignment();
                }
                else
                {
                    if (base.FileData.Length > 0)
                    {
                        byte[] comp;
                        if (Entry.entry.compSize != 0 && Entry.entry.compSize != Entry.entry.decompSize)
                        {
                            comp = ZLib.Compress(base.FileData);
                        }
                        else
                        {
                            comp = new BinaryReaderX(base.FileData, true).ReadAllBytes();
                        }
                        bw.Write(comp);
                        bw.WriteAlignment();

                        if (Entry.entry.compSize != 0)
                        {
                            Entry.entry.compSize = (uint)comp.Length;
                        }
                        Entry.entry.decompSize = (uint)base.FileData.Length;
                    }
                }

                return(new Tuple <uint, uint>(
                           (Entry.entry.compOffset == 0) ?
                           Entry.entry.decompOffset + Entry.entry.decompSize :
                           Entry.entry.compOffset + Entry.entry.compSize,
                           decompOffset + Entry.entry.decompSize));
            }
        }
Пример #18
0
        public void Save(string filename)
        {
            var sjis = Encoding.GetEncoding("SJIS");

            //Update TextOffsets
            int textOffset = 0;
            int labelCount = 0;

            foreach (var entry in entries)
            {
                foreach (var meta in entry.metaInfo)
                {
                    if (meta.type == 0)
                    {
                        meta.value  = (Labels[labelCount].Text == String.Empty) ? -1 : textOffset;
                        textOffset += (Labels[labelCount].Text == String.Empty) ? 0 : sjis.GetByteCount(Labels[labelCount].Text) + 1;
                        labelCount++;
                    }
                }
            }

            using (var bw = new BinaryWriterX(File.Create(filename)))
            {
                //Write Texts
                bw.BaseStream.Position = cfgHeader.dataOffset;
                foreach (var label in Labels)
                {
                    if (label.Text != String.Empty)
                    {
                        bw.Write(sjis.GetBytes(label.Text + "\0"));
                    }
                }
                cfgHeader.dataLength = (uint)bw.BaseStream.Position - cfgHeader.dataOffset;
                bw.WriteAlignment(16, 0xff);

                //Write Entries
                bw.BaseStream.Position = 0x10;
                foreach (var entry in entries)
                {
                    entry.Write(bw.BaseStream);
                }
                bw.WriteAlignment(16, 0xff);

                //Write Header
                bw.BaseStream.Position = 0;
                bw.WriteStruct(cfgHeader);
            }
        }
Пример #19
0
        public void Save(Stream output)
        {
            using (var bw = new BinaryWriterX(output))
            {
                bw.WriteStruct(Header);
                bw.Write(HashTable);
                bw.Write(Block);

                if (Metas.Count > 0)
                {
                    bw.WriteStruct(Metas[0]);

                    uint runningTotal = 0;
                    for (int i = 1; i < Metas.Count; i++)
                    {
                        var bytes = shift.GetBytes(Entries[i - 1].Text);
                        Metas[i].Offset = runningTotal + (uint)bytes.Length + 1;
                        runningTotal   += (uint)bytes.Length + 1;
                        bw.WriteStruct(Metas[i]);
                    }
                }

                if (Entries.Count > 0)
                {
                    foreach (var entry in Entries)
                    {
                        bw.Write(shift.GetBytes(entry.Text));
                        bw.Write((byte)0x0);
                    }
                }

                bw.WriteAlignment();
            }
        }
Пример #20
0
        public void Save(Stream segStream, Stream binStream, Stream sizeStream, IList <IArchiveFileInfo> files)
        {
            using var binBw = new BinaryWriterX(binStream);
            using var segBw = new BinaryWriterX(segStream);

            // Write files
            var offsets           = new List <int>();
            var decompressedSizes = new List <int>();

            foreach (var file in files.Cast <ArchiveFileInfo>())
            {
                offsets.Add((int)binStream.Position);
                decompressedSizes.Add((int)file.FileSize);

                file.SaveFileData(binStream);
                binBw.WriteAlignment();
            }

            // Write offsets
            segBw.WriteMultiple(offsets);
            segBw.Write((int)binStream.Length);

            // Write decompressed sizes
            if (sizeStream != null)
            {
                using var sizeBw = new BinaryWriterX(sizeStream);

                sizeBw.WriteMultiple(decompressedSizes);
                sizeBw.Write(0);
            }
        }
Пример #21
0
 public void Save(Stream output)
 {
     using (var bw = new BinaryWriterX(output))
     {
         for (var i = 0; i < Files.Count; i++)
         {
             var afi = Files[i];
             if (UseFixedOffsets)
             {
                 bw.BaseStream.Position = _fileOffsets[i] - Magic.Length;
             }
             bw.WriteASCII(Magic);
             if (afi.State == ArchiveFileState.Archived)
             {
                 afi.CompressedFileData.CopyTo(bw.BaseStream);
             }
             else
             {
                 bw.Write(Nintendo.Compress(afi.FileData, Nintendo.Method.LZ11));
             }
             if (afi != Files.Last())
             {
                 bw.WriteAlignment(_alignment);
             }
         }
     }
 }
Пример #22
0
        public void Save(string filename)
        {
            for (int i = 0; i < bmps.Count(); i++)
            {
                if (origValues[i].width != bmps[i].Width || origValues[i].height != bmps[i].Height)
                {
                    throw new Exception("All BCH textures have to be the same size as the original!");
                }
            }

            using (BinaryWriterX bw = new BinaryWriterX(File.Create(filename)))
            {
                bw.Write(_file);

                bw.BaseStream.Position = header.dataOffset;
                for (int i = 0; i < bmps.Count(); i++)
                {
                    var settings = new ImageSettings
                    {
                        Width   = bmps[i].Width,
                        Height  = bmps[i].Height,
                        Format  = Support.CTRFormat[origValues[i].format],
                        Swizzle = new CTRSwizzle(bmps[i].Width, bmps[i].Height)
                    };
                    bw.Write(Common.Save(bmps[i], settings));

                    bw.WriteAlignment(0x80);
                }
            }
        }
Пример #23
0
        public void Save(Stream output)
        {
            using (var bw = new BinaryWriterX(output))
            {
                //Count
                bw.Write(Files.Count);

                //Entries
                uint dataOffset = 4 + (uint)Files.Count * 8;
                foreach (var file in Files)
                {
                    bw.Write(dataOffset / 4);
                    bw.Write((uint)file.FileSize);

                    dataOffset = (uint)((dataOffset + file.FileSize + 0x3) & ~0x3);
                }

                //Files
                foreach (var file in Files)
                {
                    file.FileData.CopyTo(bw.BaseStream);
                    bw.WriteAlignment(4);
                }
            }
        }
Пример #24
0
        private void WriteTSY1(BinaryWriterX bw)
        {
            bw.WriteStruct(TSY1.Section);
            bw.Write(TSY1.Unknown);

            bw.WriteAlignment(_paddingChar);
        }
Пример #25
0
        public void Save(Stream output, IList <IArchiveFileInfo> files)
        {
            var dataPosition = (_headerSize + (files.Count + 1) * 4 + 0x7F) & ~0x7F;

            using var bw = new BinaryWriterX(output);

            // Write files
            bw.BaseStream.Position = dataPosition;

            var offsets = new List <uint>();

            foreach (var file in files.Cast <ArchiveFileInfo>())
            {
                offsets.Add((uint)bw.BaseStream.Position);

                file.SaveFileData(bw.BaseStream, null);
                bw.WriteAlignment(0x80);
            }

            // Write offsets
            bw.BaseStream.Position = _headerSize;
            bw.WriteMultiple(offsets);
            bw.Write(bw.BaseStream.Length);

            // Write header
            bw.BaseStream.Position = 0;
            bw.WriteType(new PcHeader
            {
                entryCount = (short)files.Count
            });
        }
Пример #26
0
        public void Save(Stream output, IList <IArchiveFileInfo> files)
        {
            using var bw = new BinaryWriterX(output);
            var castedFiles = files.Cast <BinArchiveFileInfo>();

            // Write files
            foreach (var file in castedFiles)
            {
                var offset = (file.Entry.offSize >> _header.shiftFactor) * _header.padFactor;
                output.Position = offset;

                file.SaveFileData(output);
            }
            bw.WriteAlignment(_header.padFactor);

            // Write header
            output.Position = 0;
            bw.WriteType(_header);

            // Write entries
            foreach (var file in castedFiles)
            {
                bw.Write(file.Entry.offSize);
            }
        }
Пример #27
0
        public void Save(string filename)
        {
            using (var bw = new BinaryWriterX(File.OpenWrite(filename)))
            {
                //preData
                bw.Write(preData);

                //Entries (literally all other data)
                foreach (var label in Labels)
                {
                    bw.Write(label.textEntry.ident);
                    bw.Write(label.textEntry.inlineData);

                    var byteText = Encoding.UTF8.GetBytes(label.Text);
                    var byteSize = ((byteText.Length + 1) % 2 == 1) ? byteText.Length + 2 : byteText.Length + 1;

                    bw.Write((ushort)(byteSize / 2));
                    bw.Write(byteText);
                    bw.Write((byte)0);
                    bw.WriteAlignment(2);

                    bw.Write(label.textEntry.postData);
                }
            }
        }
Пример #28
0
        public void Write(Stream input, uint offset)
        {
            using (var bw = new BinaryWriterX(input, true))
            {
                var startOffset = bw.BaseStream.Position;
                bw.BaseStream.Position = offset;

                if (State == ArchiveFileState.Archived)
                {
                    entry.offset = offset;
                    base.FileData.CopyTo(bw.BaseStream);
                }
                else
                {
                    if ((entry.size & 0x80000000) == 0)
                    {
                        entry.offset = offset;
                        base.FileData.CopyTo(bw.BaseStream);
                        entry.size = (uint)base.FileData.Length;
                    }
                    else
                    {
                        entry.offset = offset;
                        var comp = LZ11.Compress(base.FileData);
                        bw.Write(comp);
                        entry.size = (uint)(comp.Length | 0x80000000);
                    }
                }

                bw.WriteAlignment(0x4);
                bw.BaseStream.Position = startOffset;
            }
        }
Пример #29
0
        public void Save(Stream input)
        {
            using (var bw = new BinaryWriterX(input))
            {
                var dataOffset = 4 + Files.Count * 4 + 4;
                dataOffset = dataOffset + 0x7f & ~0x7f;

                bw.WriteStruct(header);

                //Files
                bw.BaseStream.Position = dataOffset;
                for (int i = 0; i < Files.Count; i++)
                {
                    Files[i].FileData.CopyTo(bw.BaseStream);
                    bw.WriteAlignment(0x80);
                }

                //Offsetlist
                bw.BaseStream.Position = 4;
                for (int i = 0; i < Files.Count; i++)
                {
                    bw.Write(dataOffset);
                    dataOffset = (dataOffset + (int)Files[i].FileSize) + 0x7f & ~0x7f;
                }
                bw.Write(dataOffset);
            }
        }
Пример #30
0
        public void Save(Stream output, IList <ArchiveFileInfo> files)
        {
            var castedFiles = files.Cast <ArcvArchiveFileInfo>().ToArray();

            using var bw = new BinaryWriterX(output);

            bw.BaseStream.Position = (_headerSize + files.Count * _entrySize + 0x7F) & ~0x7F;

            // Write files
            foreach (var file in castedFiles)
            {
                file.Entry.offset = (int)bw.BaseStream.Position;
                file.Entry.size   = (int)file.FileSize;

                file.SaveFileData(bw.BaseStream, null);
            }

            // Write header
            bw.BaseStream.Position = 0;
            bw.WriteType(new ArcvHeader
            {
                fileSize  = (int)output.Length,
                fileCount = files.Count
            });

            // Write file entries
            foreach (var file in castedFiles)
            {
                bw.WriteType(file.Entry);
            }

            // Pad with 0xAF to first file
            bw.WriteAlignment(0x80, 0xAC);
        }