Exemplo n.º 1
0
        public NUS3(string filename, bool isZlibCompressed = false)
        {
            // Compression
            if (isZlibCompressed)
            {
                using (BinaryReaderX br = new BinaryReaderX(File.OpenRead(filename)))
                {
                    byte[] decomp = ZLib.Decompress(new MemoryStream(br.ReadBytes((int)br.BaseStream.Length)));
                    File.Create(filename + ".decomp").Write(decomp, 0, decomp.Length);
                }

                _stream = File.OpenRead(filename + ".decomp");
            }
            else
            {
                _stream = File.OpenRead(filename);
            }

            using (var br = new BinaryReaderX(_stream, true))
            {
                // Header
                Header = br.ReadStruct <Header>();

                // BankTOC
                BankTOC = br.ReadStruct <BankTOC>();

                // Sections
                Sections = br.ReadMultiple <SectionHeader>(BankTOC.SectionCount);

                // Load Sections
                foreach (var section in Sections)
                {
                    switch (section.Magic)
                    {
                    case "PROP":
                        Prop = new PROP(br.BaseStream);
                        break;

                    case "BINF":
                        Binf = new BINF(br.BaseStream);
                        break;

                    case "GRP ":
                        Grp = new GRP(br.BaseStream);
                        break;

                    case "DTON":
                        Dton = new DTON(br.BaseStream);
                        break;

                    case "TONE":
                        Tone = new TONE(br.BaseStream);
                        break;

                    case "JUNK":
                        Junk = new JUNK(br.BaseStream);
                        break;

                    case "PACK":
                        Pack = br.ReadStruct <SectionHeader>();
                        break;
                    }
                }

                // Pack
                var packOffset = br.BaseStream.Position;
                for (int i = 0; i < Tone.ToneCount; i++)
                {
                    // Determine Extension
                    var extension = ".idsp";
                    if (br.PeekString(4) == "RIFF")
                    {
                        extension = ".wav";
                        br.BaseStream.Position += 0x14;
                        var format = br.ReadUInt16();
                        if (format == 0xFFFE)
                        {
                            extension = ".at9";
                        }
                    }

                    Files.Add(new NUS3FileInfo
                    {
                        FileName = Tone.Name + extension,
                        FileData = new SubStream(_stream, packOffset, Tone.PackSize),
                        State    = ArchiveFileState.Archived
                    });
                }
            }
        }
Exemplo n.º 2
0
        public NUS3(string filename, bool isZlibCompressed = false)
        {
            if (isZlibCompressed)
            {
                using (BinaryReaderX br = new BinaryReaderX(File.OpenRead(filename)))
                {
                    byte[] decomp = ZLib.Decompress(new MemoryStream(br.ReadBytes((int)br.BaseStream.Length)));
                    File.OpenWrite(filename + ".decomp").Write(decomp, 0, decomp.Length);
                }

                _stream = File.OpenRead(filename + ".decomp");
            }
            else
            {
                _stream = File.OpenRead(filename);
            }

            using (var br = new BinaryReaderX(_stream, true))
            {
                //Header
                header = br.ReadStruct <Header>();

                //Banktoc
                banktocHeader = br.ReadStruct <BankToc>();

                int offset = 0x18 + banktocHeader.entryCount * 0x8;
                banktocEntries = new List <BankTocEntry>();
                for (int i = 0; i < banktocHeader.entryCount; i++)
                {
                    banktocEntries.Add(new BankTocEntry(br.BaseStream));
                    banktocEntries[i].offset = offset;
                    offset += banktocEntries[i].secSize + 8;
                }

                //PROP
                if (br.ReadStruct <Header>().magic != "PROP")
                {
                    throw new Exception();
                }
                prop = new PROP(br.BaseStream);

                //BINF
                br.BaseStream.Position = banktocEntries[1].offset;
                if (br.ReadStruct <Header>().magic != "BINF")
                {
                    throw new Exception();
                }
                binf = new BINF(br.BaseStream);

                //GRP - not yet mapped
                br.BaseStream.Position = banktocEntries[2].offset;
                if (br.ReadStruct <Header>().magic != "GRP ")
                {
                    throw new Exception();
                }
                grp = br.ReadBytes(banktocEntries[2].secSize);

                //DTON - not yet mapped
                br.BaseStream.Position = banktocEntries[3].offset;
                if (br.ReadStruct <Header>().magic != "DTON")
                {
                    throw new Exception();
                }
                dton = br.ReadBytes(banktocEntries[3].secSize);

                //TONE
                br.BaseStream.Position = banktocEntries[4].offset;
                if (br.ReadStruct <Header>().magic != "TONE")
                {
                    throw new Exception();
                }
                tone = new TONE(br.BaseStream, banktocEntries[4].offset, banktocEntries[6].offset);

                //JUNK - not yet mapped
                br.BaseStream.Position = banktocEntries[5].offset;
                if (br.ReadStruct <Header>().magic != "JUNK")
                {
                    throw new Exception();
                }
                junk = br.ReadBytes(banktocEntries[5].secSize);

                //PACK and finishing
                br.BaseStream.Position = banktocEntries[6].offset;
                if (br.ReadStruct <Header>().magic != "PACK")
                {
                    throw new Exception();
                }
                for (int i = 0; i < tone.toneCount; i++)
                {
                    br.BaseStream.Position = tone.toneEntries[i].packOffset;

                    Files.Add(new NUS3FileInfo
                    {
                        FileName = tone.toneEntries[i].name + ".idsp",
                        Entry    = tone.toneEntries[i],
                        FileData = new SubStream(_stream, tone.toneEntries[i].packOffset, tone.toneEntries[i].size)
                    });
                }
            }
        }