Beispiel #1
0
        static void Test(byte[] bytes, Method method)
        {
            var bytes2 = new byte[bytes.Length];

            switch (method)
            {
            case Method.Ninty:
                bytes2 = Nintendo.Decompress(new MemoryStream(Nintendo.Compress(new MemoryStream(bytes), Nintendo.Method.LZ10)));
                Assert.IsTrue(bytes.SequenceEqual(bytes2));
                bytes2 = Nintendo.Decompress(new MemoryStream(Nintendo.Compress(new MemoryStream(bytes), Nintendo.Method.LZ11)));
                Assert.IsTrue(bytes.SequenceEqual(bytes2));
                bytes2 = Nintendo.Decompress(new MemoryStream(Nintendo.Compress(new MemoryStream(bytes), Nintendo.Method.Huff4)));
                Assert.IsTrue(bytes.SequenceEqual(bytes2));
                bytes2 = Nintendo.Decompress(new MemoryStream(Nintendo.Compress(new MemoryStream(bytes), Nintendo.Method.Huff8)));
                Assert.IsTrue(bytes.SequenceEqual(bytes2));
                bytes2 = Nintendo.Decompress(new MemoryStream(Nintendo.Compress(new MemoryStream(bytes), Nintendo.Method.RLE)));
                Assert.IsTrue(bytes.SequenceEqual(bytes2));
                break;

            case Method.L5:
                bytes2 = Level5.Decompress(new MemoryStream(Level5.Compress(new MemoryStream(bytes), Level5.Method.NoCompression)));
                Assert.IsTrue(bytes.SequenceEqual(bytes2));
                bytes2 = Level5.Decompress(new MemoryStream(Level5.Compress(new MemoryStream(bytes), Level5.Method.LZ10)));
                Assert.IsTrue(bytes.SequenceEqual(bytes2));
                bytes2 = Level5.Decompress(new MemoryStream(Level5.Compress(new MemoryStream(bytes), Level5.Method.Huffman4Bit)));
                Assert.IsTrue(bytes.SequenceEqual(bytes2));
                bytes2 = Level5.Decompress(new MemoryStream(Level5.Compress(new MemoryStream(bytes), Level5.Method.Huffman8Bit)));
                Assert.IsTrue(bytes.SequenceEqual(bytes2));
                bytes2 = Level5.Decompress(new MemoryStream(Level5.Compress(new MemoryStream(bytes), Level5.Method.RLE)));
                Assert.IsTrue(bytes.SequenceEqual(bytes2));
                break;
            }
        }
Beispiel #2
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);
             }
         }
     }
 }
Beispiel #3
0
        public int Write(Stream output)
        {
            var compressedSize = (int)base.FileData.Length;

            using (var bw = new BinaryWriterX(output, true))
            {
                if (State == ArchiveFileState.Archived)
                {
                    base.FileData.CopyTo(bw.BaseStream);
                }
                else
                {
                    var bytes = Nintendo.Compress(base.FileData, Nintendo.Method.LZ10);
                    compressedSize = bytes.Length;
                    bw.Write(bytes);
                }
            }

            return(compressedSize);
        }
Beispiel #4
0
        public void Write(Stream input)
        {
            using (var bw = new BinaryWriterX(input, true))
            {
                if (State == ArchiveFileState.Archived || Entry.uncompSize == 0)
                {
                    Entry.offset = (uint)bw.BaseStream.Position;

                    base.FileData.CopyTo(bw.BaseStream);
                }
                else
                {
                    Entry.offset     = (uint)bw.BaseStream.Position;
                    Entry.uncompSize = (uint)base.FileData.Length;

                    var compData = new MemoryStream(Nintendo.Compress(base.FileData, Nintendo.Method.LZ11));
                    Entry.compSize = (uint)compData.Length;
                    compData.CopyTo(bw.BaseStream);
                }
            }
        }
Beispiel #5
0
        public void Save(string filename)
        {
            var ms = new MemoryStream();

            using (var bw = new BinaryWriterX(ms))
            {
                var dataOffset = header.dataOffset;

                //Write Files
                bw.BaseStream.Position = dataOffset;
                int relNameOff = 0, relFileOff = 0;
                foreach (var file in Files)
                {
                    (relNameOff, relFileOff) = file.Write(bw.BaseStream, relNameOff, relFileOff);
                }

                //Write Names
                bw.BaseStream.Position = header.nameOffset;
                foreach (var file in Files)
                {
                    bw.Write(Encoding.ASCII.GetBytes(file.FileName + "\0"));
                }

                //Write Entries
                bw.BaseStream.Position = header.fileEntryOffset;
                foreach (var file in Files)
                {
                    bw.WriteStruct(file.entry);
                }

                //Write Header
                bw.BaseStream.Position = 0;
                header.fileSize        = (int)bw.BaseStream.Length;
                bw.WriteStruct(header);

                //Compress and write to file
                bw.BaseStream.Position = 0;
                File.WriteAllBytes(filename, Nintendo.Compress(bw.BaseStream, Nintendo.Method.LZ10));
            }
        }
Beispiel #6
0
 public void Save(Stream output)
 {
     using (var bw = new BinaryWriterX(output))
     {
         foreach (var afi in Files)
         {
             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);
             }
         }
     }
 }
Beispiel #7
0
        public static void Compress(object sender, EventArgs e)
        {
            var tsi = sender as ToolStripMenuItem;

            if (!Shared.PrepareFiles("Open a decompressed " + tsi?.Tag + "file...", "Save your compressed file...", ".decomp", out var openFile, out var saveFile, true))
            {
                return;
            }

            try
            {
                using (openFile)
                    using (var outFs = new BinaryWriterX(saveFile))
                        switch (tsi?.Tag)
                        {
                        case Compression.L5LZ10:
                            outFs.Write(Level5.Compress(openFile, Level5.Method.LZ10));
                            break;

                        case Compression.L5Huff4:
                            outFs.Write(Level5.Compress(openFile, Level5.Method.Huffman4Bit));
                            break;

                        case Compression.L5Huff8:
                            outFs.Write(Level5.Compress(openFile, Level5.Method.Huffman8Bit));
                            break;

                        case Compression.L5RLE:
                            outFs.Write(Level5.Compress(openFile, Level5.Method.RLE));
                            break;

                        case Compression.NLZ10:
                            outFs.Write(Nintendo.Compress(openFile, Nintendo.Method.LZ10));
                            break;

                        case Compression.NLZ11:
                            outFs.Write(Nintendo.Compress(openFile, Nintendo.Method.LZ11));
                            break;

                        case Compression.NLZ60:
                            outFs.Write(Nintendo.Compress(openFile, Nintendo.Method.LZ60));
                            break;

                        case Compression.NHuff4:
                            outFs.Write(Nintendo.Compress(openFile, Nintendo.Method.Huff4));
                            break;

                        case Compression.NHuff8:
                            outFs.Write(Nintendo.Compress(openFile, Nintendo.Method.Huff8));
                            break;

                        case Compression.NRLE:
                            outFs.Write(Nintendo.Compress(openFile, Nintendo.Method.RLE));
                            break;

                        case Compression.LZ77:
                            outFs.Write(LZ77.Compress(openFile));
                            break;

                        case Compression.RevLZ77:
                            outFs.Write(RevLZ77.Compress(openFile));
                            break;

                        case Compression.LZOvl:
                            outFs.Write(LZOvl.Compress(openFile));
                            break;

                        case Compression.LZ4:
                            outFs.Write(Kontract.Compression.LZ4.Compress(openFile));
                            break;

                        case Compression.MIO0LE:
                            outFs.Write(MIO0.Compress(openFile, ByteOrder.LittleEndian));
                            break;

                        case Compression.MIO0BE:
                            outFs.Write(MIO0.Compress(openFile, ByteOrder.BigEndian));
                            break;

                        case Compression.Yay0LE:
                            outFs.Write(Yay0.Compress(openFile, ByteOrder.LittleEndian));
                            break;

                        case Compression.Yay0BE:
                            outFs.Write(Yay0.Compress(openFile, ByteOrder.BigEndian));
                            break;

                        case Compression.Yaz0LE:
                            outFs.Write(Yaz0.Compress(openFile, ByteOrder.LittleEndian));
                            break;

                        case Compression.Yaz0BE:
                            outFs.Write(Yaz0.Compress(openFile, ByteOrder.BigEndian));
                            break;

                        case Compression.GZip:
                            outFs.Write(GZip.Compress(openFile));
                            break;

                        case Compression.ZLib:
                            outFs.Write(ZLib.Compress(openFile));
                            break;

                        case Compression.PSVSpikeChun:
                            outFs.Write(PSVSpikeChun.Compress(openFile));
                            break;
                        }
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.ToString(), tsi?.Text, MessageBoxButtons.OK, MessageBoxIcon.Error);
                return;
            }

            MessageBox.Show($"Successfully compressed {Path.GetFileName(openFile.Name)}.", tsi.Text, MessageBoxButtons.OK, MessageBoxIcon.Information);
        }
Beispiel #8
0
        public void Save(string filename)
        {
            if (bmps[0].Width % 8 != 0 || bmps[0].Height % 8 != 0)
            {
                throw new Exception("Width and Height need to be dividable by 8.");
            }

            var ms = new MemoryStream();

            using (var bw = new BinaryWriterX(ms))
            {
                //Get new image data
                settings.Swizzle = new NitroSwizzle(bmps[0].Width, bmps[0].Height);
                var newImgData = Common.Save(bmps[0], settings);

                //Get Palette and deflate imgData
                byte[]        pal         = null;
                List <byte[]> tiles       = null;
                List <short>  tileIndeces = null;
                switch (header.imgFormat)
                {
                case 0:
                    pal = (paletteFormat as Palette).paletteBytes;
                    (tiles, tileIndeces) = Deflate(newImgData, imgDataEntry / 2);
                    break;

                case 1:
                    pal = (paletteFormat as Palette).paletteBytes;
                    (tiles, tileIndeces) = Deflate(newImgData, imgDataEntry);
                    break;

                case 2:
                    pal = (paletteFormat as AI).paletteBytes;
                    (tiles, tileIndeces) = Deflate(newImgData, imgDataEntry);
                    break;

                default:
                    throw new NotImplementedException($"Image mode {header.imgFormat} not supported.");
                }
                short colorCount = (short)(pal.Length / 2);

                //Update Header
                header.tileEntryCount  = (short)tileIndeces.Count();
                header.imageDataOffset = (short)(header.tileDataOffset + header.tileEntryCount * tileEntrySize);
                header.imageTileCount  = (short)tiles.Count();
                header.colorCount      = (short)((colorCount + 0xf) & ~0xf);
                header.width           = (short)bmps[0].Width;
                header.height          = (short)bmps[0].Height;

                //Write Data
                bw.WriteStruct(header);
                bw.Write(pal);
                while (colorCount++ % 0x10 != 0)
                {
                    bw.Write((ushort)0x8000);
                }
                foreach (var unk1 in unkChunk1)
                {
                    bw.Write(unk1);
                }
                foreach (var unk2 in unkChunk2)
                {
                    bw.Write(unk2);
                }
                bw.WriteAlignment(4);
                foreach (var index in tileIndeces)
                {
                    bw.Write(index);
                }
                bw.WriteAlignment(4);
                foreach (var tile in tiles)
                {
                    bw.Write(tile);
                }

                //Compress data with LZ10
                bw.BaseStream.Position = 0;
                if (_compressed)
                {
                    File.WriteAllBytes(filename, Nintendo.Compress(ms, Nintendo.Method.LZ10));
                }
                else
                {
                    File.WriteAllBytes(filename, ms.ToArray());
                }
            }
        }
Beispiel #9
0
        static void Main(string[] args2)
        {
            var options = ParseArgs(args2);

            if (options.mode == "extract")
            {
                var sections = new List <byte[]>();
                using (var br = new BinaryReaderY(File.OpenRead(options.path)))
                {
                    try
                    {
                        var entryCount = br.ReadInt32();
                        var entryList  = br.ReadMultiple <DlgEntry>(entryCount);
                        foreach (var e in entryList)
                        {
                            br.BaseStream.Position = e.offset;
                            sections.Add(Nintendo.Decompress(new MemoryStream(br.ReadBytes(e.size))));
                        }
                    }
                    catch (Exception ex)
                    {
                        Console.WriteLine("This file seems a bit corrupted or isn't a dlg. Either way it won't work.");
                        Console.WriteLine(ex.Message + " - " + ex.InnerException);
                        return;
                    }
                }
                var writeDir = Path.Combine(Path.GetDirectoryName(options.path), Path.GetFileNameWithoutExtension(options.path));
                if (!Directory.Exists(writeDir))
                {
                    Directory.CreateDirectory(writeDir);
                }

                var txtCount = 0;
                foreach (var s in sections)
                {
                    using (var br = new BinaryReaderY(new MemoryStream(s)))
                    {
                        if (options.isBinary)
                        {
                            File.WriteAllBytes(Path.Combine(writeDir, $"{txtCount++:000}.bin"), s);
                        }
                        else
                        {
                            var enc = new AAEncoding(options.lang, options.game);
                            File.WriteAllText(Path.Combine(writeDir, $"{txtCount++:000}.txt"), enc.GetSectionText(s));
                        }
                    }
                }
            }
            else if (options.mode == "create")
            {
                var enc     = new AAEncoding(options.lang, options.game);
                var files   = Directory.GetFiles(options.path).Where(f => Path.GetExtension(f) == ((options.isBinary) ? ".bin" : ".txt")).OrderBy(name => name).ToList();
                var entries = new List <DlgEntry>();

                var ms = new MemoryStream();
                using (var bw = new BinaryWriterY(ms, true))
                {
                    bw.BaseStream.Position = files.Count * 8 + 4;

                    foreach (var file in files)
                    {
                        byte[] compBytes = null;
                        if (options.isBinary)
                        {
                            compBytes = Nintendo.Compress(new MemoryStream(File.ReadAllBytes(file)), Nintendo.Method.LZ10);
                        }
                        else
                        {
                            var sectionText = File.ReadAllText(file).Replace("\r\n", "");
                            var bytes       = enc.GetBytes(sectionText);
                            compBytes = Nintendo.Compress(new MemoryStream(bytes), Nintendo.Method.LZ10);
                        }

                        entries.Add(new DlgEntry {
                            offset = (int)bw.BaseStream.Position, size = compBytes.Length
                        });

                        bw.Write(compBytes);
                        bw.BaseStream.Position = (bw.BaseStream.Position + 3) & ~3;
                    }

                    bw.BaseStream.Position = 0;
                    bw.Write(entries.Count);
                    foreach (var entry in entries)
                    {
                        bw.WriteStruct(entry);
                    }
                }

                File.WriteAllBytes("new.dlg", ms.ToArray());
            }
        }