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
        //Methods

        public void LoadGame()
        {
            string playingOn = "";

            switch (GamingSystem)
            {
            case GamingSystem.PC:
                playingOn = PC.ToString();
                break;

            case GamingSystem.Nintendo:
                playingOn = Nintendo.ToString();
                break;

            case GamingSystem.PlayStation:
                playingOn = PlayStation.ToString();
                break;

            case GamingSystem.Xbox:
                playingOn = Xbox.ToString();
                break;

            case GamingSystem.Sega:
                playingOn = Sega.ToString();
                break;

            default:
                playingOn = "system that we are not sure about";
                break;
            }
            Console.WriteLine($"You have loaded the game on a {playingOn}.");
        }
Beispiel #4
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 #5
0
        /// <summary>
        ///
        /// </summary>
        /// <param name="folder"></param>
        /// <param name="file"></param>
        /// <param name="xElement"></param>
        private void ReadMetaData(string folder, string file, XElement xElement)
        {
            var productCode = Xml.GetValue(xElement, "product_code");

            if (productCode != null)
            {
                var companyCode = Xml.GetValue(xElement, "company_code");
                if (companyCode != null)
                {
                    string key = productCode.Replace("WUP-P-", "").Replace("WUP-U-", "").Replace("WUP-N-", "") + companyCode;

                    GameInformation game;

                    if (!gameData.ContainsKey(key))
                    {
                        game = new GameInformation {
                            GameSetting = new GameSettings()
                        };
                        gameData.Add(key, game);
                        game.Name           = Xml.GetValue(xElement, "longname_en").Replace("\n", " ");
                        game.Region         = Nintendo.GetRegion(Xml.GetValue(xElement, "region"));
                        game.Publisher      = Xml.GetValue(xElement, "publisher_en");
                        game.ProductCode    = productCode;
                        game.CompanyCode    = companyCode;
                        game.TitleId        = Xml.GetValue(xElement, "title_id").ToUpper();
                        game.GroupId        = Xml.GetValue(xElement, "group_id").ToUpper();
                        game.Size           = (FolderScanner.GetDirectorySize(folder) / 1024 / 1024).ToString("N0") + " MB";
                        game.LaunchFile     = file;
                        game.LaunchFileName = Path.GetFileName(file);
                    }
                    else
                    {
                        game                = gameData[key];
                        game.Name           = Xml.GetValue(xElement, "longname_en").Replace("\n", " ");
                        game.Region         = Nintendo.GetRegion(Xml.GetValue(xElement, "region"));
                        game.Publisher      = Xml.GetValue(xElement, "publisher_en");
                        game.ProductCode    = productCode;
                        game.CompanyCode    = companyCode;
                        game.TitleId        = Xml.GetValue(xElement, "title_id").ToUpper();
                        game.GroupId        = Xml.GetValue(xElement, "group_id").ToUpper();
                        game.LaunchFile     = file;
                        game.LaunchFileName = Path.GetFileName(file);
                    }
                }
            }
        }
Beispiel #6
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 #7
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 #8
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 #9
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 #10
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 #11
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 #12
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 #13
0
        /// <summary>
        ///
        /// </summary>
        /// <param name="gameData"></param>
        /// <param name="folder"></param>
        /// <param name="launchFile"></param>
        private static void AddGame(Dictionary <string, GameInformation> gameData, string folder, string launchFile)
        {
            if (File.Exists(Path.Combine(folder, "meta", "meta.xml")))
            {
                XElement xElement = XElement.Parse(XDocument.Load(Path.Combine(folder, "meta", "meta.xml")).ToString());

                var productCode = Xml.GetValue(xElement, "product_code");
                if (productCode != null)
                {
                    var companyCode = Xml.GetValue(xElement, "company_code");
                    if (companyCode != null)
                    {
                        string key = productCode.Replace("WUP-P-", "").Replace("WUP-U-", "").Replace("WUP-N-", "") + companyCode;

                        GameInformation game;

                        if (!gameData.TryGetValue(key, out game))
                        {
                            game = new GameInformation {
                                GameSetting = new GameSettings()
                            };
                            gameData.Add(key, game);
                        }

                        game.Name           = Xml.GetValue(xElement, "longname_en");
                        game.Region         = Nintendo.GetRegion(Xml.GetValue(xElement, "region"));
                        game.Publisher      = Xml.GetValue(xElement, "publisher_en");
                        game.ProductCode    = productCode;
                        game.CompanyCode    = companyCode;
                        game.TitleId        = Xml.GetValue(xElement, "title_id").ToUpper();
                        game.GroupId        = Xml.GetValue(xElement, "group_id").ToUpper();
                        game.Size           = (GetDirectorySize(folder) / 1024 / 1024).ToString("N0") + " MB";
                        game.LaunchFile     = launchFile;
                        game.LaunchFileName = Path.GetFileName(launchFile);
                    }
                }
            }
        }
    // Start is called before the first frame update
    void Start()
    {
        consolas = new Consolas[8];

        consolas[0] = new Ps4("Playstation 4 - ", Ps4.Type.Playstation_4, 1800);

        consolas[1] = new Ps4("Playstation 1 - ", Ps4.Type.Playstation_1, 340);

        consolas[2] = new Ps4("Playstation 2 - ", Ps4.Type.Playstation_2, 1000);

        consolas[3] = new Xbox("Xbox 360 - ", Xbox.Type.Xbox_360, 1000);

        consolas[4] = new Xbox("Xbox One - ", Xbox.Type.Xbox_one, 1036);

        consolas[5] = new Nintendo("Nintendo 64 - ", Nintendo.Type.Nintendo_64, 130);

        consolas[6] = new Nintendo("Nintendo 3DS - ", Nintendo.Type.Nintendo_3DS, 278);

        consolas[7] = new Nintendo("Nintendo switch - ", Nintendo.Type.Nintendo_Switch, 1500);

        juegos = new Juegos[6];

        juegos[0] = new Digital("Assasins Creed 3 - ", Digital.Plataforma.Playstation_Store, 130);

        juegos[1] = new Fisico("Dragon ball - ", Fisico.Local.MasGamer, 150);

        juegos[2] = new Digital("Animal Crossing - ", Digital.Plataforma.Eshop, 200);

        juegos[3] = new Fisico("Smash - ", Fisico.Local.Phantom, 150);

        juegos[4] = new Digital("Halo - ", Digital.Plataforma.Xbox_Store, 150);

        juegos[5] = new Fisico("Halo 2 - ", Fisico.Local.MasGamer, 160);


        Loop();
    }
Beispiel #15
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 #16
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 #17
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));
                            }
                        }
                    }
                }
            }
        }
Beispiel #18
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 #19
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 #20
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;
                }
            }
        }