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 LPC2(Stream input)
        {
            _stream = input;
            using (var br = new BinaryReaderX(new MemoryStream(Nintendo.Decompress(input)), true))
            {
                //Header
                header = br.ReadStruct <Header>();

                //Entries
                br.BaseStream.Position = header.fileEntryOffset;
                entries = br.ReadMultiple <FileEntry>(header.fileCount);

                //Files
                foreach (var entry in entries)
                {
                    br.BaseStream.Position = header.nameOffset + entry.nameOffset;
                    var name = br.ReadCStringA();
                    Files.Add(new LPC2FileInfo
                    {
                        State    = ArchiveFileState.Archived,
                        FileName = name,
                        FileData = new SubStream(br.BaseStream, header.dataOffset + entry.fileOffset, entry.fileSize),
                        entry    = entry
                    });
                }
            }
        }
Beispiel #3
0
        public bool Identify(string filename)
        {
            try
            {
                using (var br = new BinaryReaderX(File.OpenRead(filename)))
                {
                    uint dataOffset = 0;
                    while (br.BaseStream.Position < br.BaseStream.Length)
                    {
                        var test = Nintendo.Decompress(br.BaseStream);
                        if (test == null)
                        {
                            var count = br.ReadUInt32();
                            br.BaseStream.Position += (count - 1) * 8;
                            var offset = br.ReadUInt32();
                            var size   = br.ReadUInt32();
                            br.BaseStream.Position = dataOffset + offset + size;
                        }
                        else
                        {
                            return(true);
                        }

                        while (br.BaseStream.Position % 4 != 0)
                        {
                            br.BaseStream.Position++;
                        }
                        dataOffset = (uint)br.BaseStream.Position;

                        if (br.BaseStream.Position > br.BaseStream.Length)
                        {
                            return(false);
                        }
                    }

                    return(true);
                }
            }
            catch (System.Exception)
            {
                return(false);
            }
        }
Beispiel #4
0
        Stream GetStreamToUse(Stream input)
        {
            using (var br = new BinaryReaderX(input, true))
            {
                var magic = br.ReadString(4);
                br.BaseStream.Position = 0;
                if (magic == "LIMG")
                {
                    return(input);
                }
                else
                {
                    var decomp = Nintendo.Decompress(br.BaseStream);
                    br.Dispose();

                    _compressed = true;
                    return(new MemoryStream(decomp));
                }
            }
        }
Beispiel #5
0
        private IEnumerable <Section> GetSections(Stream stream)
        {
            using (var br = new BinaryReaderX(stream))
                while (br.BaseStream.Position < br.BaseStream.Length)
                {
                    var chunk   = br.ReadStruct <CHNK>();
                    var section = new Section
                    {
                        Chunk = chunk,
                        Magic = br.ReadString(4),
                        Size  = br.ReadInt32()
                    };
                    section.Data = br.ReadBytes(section.Size);

                    if (chunk.DecompressedSize != 0)
                    {
                        section.Data = Nintendo.Decompress(new MemoryStream(section.Data));
                    }

                    yield return(section);
                }
        }
Beispiel #6
0
        public static void Decompress(object sender, EventArgs e)
        {
            var tsi = sender as ToolStripMenuItem;

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

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

                        case Compression.Nintendo:
                            outFs.Write(Nintendo.Decompress(openFile));
                            break;

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

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

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

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

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

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

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

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

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

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

                        case Compression.LZECD:
                            outFs.Write(LZECD.Decompress(openFile));
                            break;

                        case Compression.LZ10VLE:
                            outFs.Write(LZSSVLE.Decompress(openFile));
                            break;

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

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

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

            MessageBox.Show($"Successfully decompressed {Path.GetFileName(openFile.Name)}.", tsi.Text, MessageBoxButtons.OK, MessageBoxIcon.Information);
        }
Beispiel #7
0
        public AABIN(Stream input)
        {
            _stream = input;
            using (var br = new BinaryReaderX(input, true))
            {
                int folderCount     = 0;
                int fileCount       = 0;
                int totalEntryCount = 0;
                int dataOffset      = 0;
                while (br.BaseStream.Position < br.BaseStream.Length)
                {
                    var entryCount = br.ReadInt32();
                    if (entryCount >> 16 != 0)
                    {
                        br.BaseStream.Position -= 4;
                        Files.Add(new AAbinFileInfo
                        {
                            State    = ArchiveFileState.Archived,
                            FileName = $"{fileCount++:00000000}.bin",
                            FileData = new MemoryStream(Nintendo.Decompress(br.BaseStream))
                        });
                    }
                    else
                    {
                        entries.AddRange(br.ReadMultiple <Entry>(entryCount));

                        int count = 0;
                        for (int i = totalEntryCount; i < entries.Count; i++)
                        {
                            var absOffset = dataOffset + entries[i].offset;
                            br.BaseStream.Position = absOffset;

                            try
                            {
                                Files.Add(new AAbinFileInfo
                                {
                                    FileName = $"{folderCount:00000000}/{count++:00000000}.bin",
                                    State    = ArchiveFileState.Archived,
                                    Entry    = entries[i],
                                    FileData = new SubStream(br.BaseStream, absOffset, entries[i].compSize)
                                });
                            }
                            catch (System.Exception)
                            {
                                throw new System.Exception(i.ToString() + "   " + br.BaseStream.Position.ToString() + "   " + entries[i].compSize.ToString());
                            }

                            br.BaseStream.Position = absOffset + entries[i].compSize;
                        }
                    }

                    while (br.BaseStream.Position % 4 != 0)
                    {
                        br.BaseStream.Position++;
                    }
                    dataOffset = (int)br.BaseStream.Position;
                    folderCount++;
                    totalEntryCount = entries.Count;
                }
            }
        }
Beispiel #8
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());
            }
        }
Beispiel #9
0
        public VCX(Stream inputVCG, Stream inputVCL, Stream inputVCE)
        {
            List <Color> pal;

            // Palette
            using (var brVCL = new BinaryReaderX(new MemoryStream(Nintendo.Decompress(inputVCL))))
            {
                vclHeader = brVCL.ReadStruct <VCLHeader>();

                paletteFormat = new Palette(brVCL.ReadBytes(vclHeader.colorCount * 2), new RGBA(5, 5, 5, 0, ByteOrder.LittleEndian, true));

                pal = paletteFormat.colors;
                pal[vclHeader.transparentId - 1] = Color.FromArgb(0, pal[vclHeader.transparentId - 1].R, pal[vclHeader.transparentId - 1].G, pal[vclHeader.transparentId - 1].B);
            }

            var width  = Convert.ToInt32(InputBox.Show("Input image Width:", "Width"));
            var height = Convert.ToInt32(InputBox.Show("Input image Height:", "Height"));

            var settings = new ImageSettings
            {
                Width   = width,
                Height  = height,
                Format  = new Palette(pal, 4),
                Swizzle = new NitroSwizzle(width, height)
            };

            // Tiles
            var tiles = new List <byte[]>();

            using (var brVCG = new BinaryReaderX(new MemoryStream(Nintendo.Decompress(inputVCG))))
            {
                vcgHeader = brVCG.ReadStruct <VCGHeader>();

                if (width * height != vcgHeader.dataSize * 2)
                {
                    throw new InvalidOperationException($"Image dimensions {width}*{height} don't fit with dataSize.");
                }

                //while (brVCG.BaseStream.Position < brVCG.BaseStream.Length)
                //    tiles.Add(brVCG.ReadBytes(4 * 4));

                // Temporary
                brVCG.BaseStream.Position = 0x10;
                //bmps.Add(Common.Load(brVCG.ReadBytes(vcgHeader.dataSize), settings));

                // Tile Map
                using (var brVCE = new BinaryReaderX(new MemoryStream(Nintendo.Decompress(inputVCE))))
                {
                    vceHeader = brVCE.ReadStruct <VCEHeader>();

                    brVCE.BaseStream.Position = vceHeader.offset0;
                    var l0 = brVCE.ReadMultiple <VCEEntry0>(vceHeader.count0);
                    brVCE.BaseStream.Position = vceHeader.offset1;
                    var l1 = brVCE.ReadMultiple <VCEEntry1>(vceHeader.count1);
                    brVCE.BaseStream.Position = vceHeader.offset2;
                    var l2 = brVCE.ReadMultiple <VCEEntry2>(vceHeader.count2);
                    brVCE.BaseStream.Position = vceHeader.offset3;
                    var l3 = brVCE.ReadMultiple <VCEEntry3>(vceHeader.count3);

                    //Merge Entries
                    var merge = new List <MergedEntry>();
                    foreach (var l in l0)
                    {
                        merge.Add(new MergedEntry
                        {
                            unk1      = l.unk1,
                            t1Entries = l1.GetRange(l.t1Index, l.count).Select(e => new MergedEntry.MergeT1
                            {
                                unk1    = e.unk1,
                                t2Entry = new MergedEntry.MergeT1.MergeT2
                                {
                                    t3Entries = l3.GetRange(l2[e.t2Index].t3Index, l2[e.t2Index].t3Count)
                                }
                            }).ToList()
                        });
                    }

                    foreach (var i in merge)
                    {
                        foreach (var ip in i.t1Entries)
                        {
                            foreach (var t in ip.t2Entry.t3Entries)
                            {
                                var intSet = new ImageSettings
                                {
                                    Width   = 16,
                                    Height  = t.unk5 * 8,
                                    Format  = new Palette(pal, 4),
                                    Swizzle = new NitroSwizzle(16, t.unk5 * 8)
                                };
                                brVCG.BaseStream.Position = 0x10 + t.tileIndex * 0x80;
                                bmps.Add(Common.Load(brVCG.ReadBytes(t.unk5 * 0x80), intSet));
                            }
                        }
                    }
                }
            }
        }