Exemplo n.º 1
0
        /// <summary>
        /// Read the sequence data.
        /// </summary>
        /// <param name="br">The reader.</param>
        public void Read(BinaryDataReader br)
        {
            //Open file.
            FileReader FileReader = new FileReader();

            FileReader.OpenFile(br, out writeMode, out Version);

            //Data block.
            FileReader.OpenBlock(br, 0);

            //Get size.
            br.Position -= 4;
            uint size = br.ReadUInt32();

            //Read the sequence data. BE for WiiU and 3ds, LE for NX. Must therefore write a parser.
            List <byte> tempSeqData = br.ReadBytes((int)size - 8).ToList();

            //Trim data.
            bool foundEnd = false;

            while (!foundEnd)
            {
                //Remove padding stuff.
                if (tempSeqData[tempSeqData.Count - 1] == 0)
                {
                    tempSeqData.RemoveAt(tempSeqData.Count - 1);
                }

                //Found end.
                else
                {
                    foundEnd = true;
                }
            }

            //Close data block.
            FileReader.CloseBlock(br);

            //Label block.
            FileReader.OpenBlock(br, 1);

            //Open the reference table.
            FileReader.OpenReferenceTable(br, "Labels");

            //Go through all the references.
            Labels = new List <SequenceLabel>();
            for (int i = 0; i < FileReader.ReferenceTableCount("Labels"); i++)
            {
                //Reference is null.
                if (FileReader.ReferenceTableReferenceIsNull(i, "Labels"))
                {
                    Labels.Add(null);
                }

                //Label exist.
                else
                {
                    //Jump to label.
                    FileReader.ReferenceTableJumpToReference(br, i, "Labels");

                    //Start label info structure.
                    FileReader.StartStructure(br);

                    //Get reference to data.
                    FileReader.OpenReference(br, "ToSeqData");

                    //String size.
                    uint stringSize = br.ReadUInt32();

                    //String data.
                    string labelNow = new string(br.ReadChars((int)stringSize));

                    //Get label info.
                    Labels.Add(new SequenceLabel()
                    {
                        Offset = FileReader.ReferenceGetOffset("ToSeqData"), Label = labelNow
                    });

                    //Close reference.
                    FileReader.CloseReference("ToSeqData");

                    //End label info structure.
                    FileReader.EndStructure();
                }
            }

            //Close the reference table.
            FileReader.CloseReferenceTable("Labels");

            //Close label block.
            FileReader.CloseBlock(br);

            //Convert labels to dictionary.
            Dictionary <string, int> publicLabels = new Dictionary <string, int>();

            foreach (var e in Labels)
            {
                if (e != null)
                {
                    publicLabels.Add(e.Label, e.Offset);
                }
            }

            //Set sequence data.
            SequenceData = new SequenceData();
            SequenceData.PublicLabelOffsets = publicLabels;
            Syroot.BinaryData.ByteOrder bo = Syroot.BinaryData.ByteOrder.BigEndian;
            if (writeMode == WriteMode.NX || writeMode == WriteMode.C_BE)
            {
                bo = Syroot.BinaryData.ByteOrder.LittleEndian;
            }
            SequenceData.Read(tempSeqData.ToArray(), bo);
            goodData = tempSeqData.ToArray();

            //Close file.
            FileReader.CloseFile(br);
        }
Exemplo n.º 2
0
        /// <summary>
        /// Read a group file.
        /// </summary>
        /// <param name="br">The reader.</param>
        /// <param name="files">List of sound files to make a reference to.</param>
        public void Read(BinaryDataReader br, List <SoundFile <ISoundFile> > files)
        {
            //Open file.
            FileReader FileReader = new FileReader();

            FileReader.OpenFile(br, out writeMode, out Version);

            //Open info block.
            FileReader.OpenBlock(br, 0);

            //Load reference table.
            FileReader.OpenReferenceTable(br, "FileRefs");

            //Load each reference.
            SoundFiles = new List <SoundFile <ISoundFile> >();
            List <SizedReference> sizedReferences = new List <SizedReference>();

            for (int i = 0; i < FileReader.ReferenceTableCount("FileRefs"); i++)
            {
                //Null reference.
                if (FileReader.ReferenceTableReferenceIsNull(i, "FileRefs"))
                {
                    SoundFiles.Add(null);
                    sizedReferences.Add(null);
                }

                //Valid reference.
                else
                {
                    //Jump to reference.
                    FileReader.ReferenceTableJumpToReference(br, i, "FileRefs");

                    //Get file id.
                    UInt32 fileId = br.ReadUInt32();

                    //Add the sized reference.
                    sizedReferences.Add(new SizedReference(ref br));

                    //If the file references exist.
                    if (files != null)
                    {
                        //If file id is found in the files.
                        if (files.Where(x => x.FileId == (int)fileId).Count() > 0)
                        {
                            //Add sound file.
                            SoundFiles.Add(new SoundFile <ISoundFile>());
                            files.Where(x => x.FileId == (int)fileId).ToList()[0].ReferencedBy.Add(SoundFiles[SoundFiles.Count - 1]);
                            SoundFiles[SoundFiles.Count - 1].Reference = files.Where(x => x.FileId == (int)fileId).ToList()[0];
                        }

                        //Independent mode.
                        else
                        {
                            //Add sound file.
                            SoundFiles.Add(new SoundFile <ISoundFile>()
                            {
                                FileId = (int)fileId
                            });
                        }
                    }

                    //Independent mode.
                    else
                    {
                        //Add sound file.
                        SoundFiles.Add(new SoundFile <ISoundFile>()
                        {
                            FileId = (int)fileId
                        });
                    }
                }
            }

            //Close table.
            FileReader.CloseReferenceTable("FileRefs");

            //Close info block.
            FileReader.CloseBlock(br);

            //Open file block.
            FileReader.OpenBlock(br, 1);

            //Read the files.
            for (int i = 0; i < SoundFiles.Count; i++)
            {
                //File exist in first place.
                if (SoundFiles[i] != null)
                {
                    //Sound file reference is null, so no embed.
                    if (sizedReferences[i].offset == Reference.NULL_PTR)
                    {
                        SoundFiles[i].Embed = false;
                    }

                    //Sound file reference exist, embed and read it.
                    else
                    {
                        //Embed file.
                        SoundFiles[i].Embed = true;

                        //Jump to file.
                        FileReader.JumpToOffsetManually(br, sizedReferences[i].offset);

                        //Read file.
                        SoundFiles[i].File = SoundArchiveReader.ReadFile(br.ReadBytes((int)sizedReferences[i].size));
                    }
                }
            }

            //Close file block.
            FileReader.CloseBlock(br);

            //New extra info.
            ExtraInfo = new List <InfoExEntry>();

            //Only open EX block if version is bigger than one.
            if (Version > new FileWriter.Version(1, 0, 0))
            {
                //Open extended info block.
                FileReader.OpenBlock(br, 2);

                //Open the table.
                FileReader.OpenReferenceTable(br, "Ex");

                //Reach each extra info entry.
                for (int i = 0; i < FileReader.ReferenceTableCount("Ex"); i++)
                {
                    //Entry is null.
                    if (FileReader.ReferenceTableReferenceIsNull(i, "Ex"))
                    {
                        ExtraInfo.Add(null);
                    }

                    //Entry exist.
                    else
                    {
                        //Read data.
                        Id     id    = new Id(ref br);
                        UInt32 flags = br.ReadUInt32();

                        //New entry.
                        InfoExEntry e = new InfoExEntry();
                        e.ItemIndex = (int)id.index;

                        //Type.
                        switch (id.type)
                        {
                        //Sound.
                        case SoundTypes.Sound:
                            e.ItemType = InfoExEntry.EItemType.Sound;
                            break;

                        //Seq sound set or wave data.
                        case SoundTypes.SoundGroup:
                            e.ItemType = InfoExEntry.EItemType.SequenceSetOrWaveData;
                            break;

                        //Bank.
                        case SoundTypes.Bank:
                            e.ItemType = InfoExEntry.EItemType.Bank;
                            break;

                        //Wave archive.
                        case SoundTypes.WaveArchive:
                            e.ItemType = InfoExEntry.EItemType.WaveArchive;
                            break;
                        }

                        //Find flags.
                        bool seq = (flags & 0b1) > 0;
                        bool wsd = (flags & 0b10) > 0;
                        bool bnk = (flags & 0b100) > 0;
                        bool war = (flags & 0b1000) > 0;

                        //Test for flags.
                        if (flags == 0xFFFFFFFF)
                        {
                            e.LoadFlags = InfoExEntry.ELoadFlags.All;
                        }
                        else if (seq && bnk)
                        {
                            e.LoadFlags = InfoExEntry.ELoadFlags.SeqAndBank;
                        }
                        else if (seq && war)
                        {
                            e.LoadFlags = InfoExEntry.ELoadFlags.SeqAndWarc;
                        }
                        else if (bnk && war)
                        {
                            e.LoadFlags = InfoExEntry.ELoadFlags.BankAndWarc;
                        }
                        else if (seq)
                        {
                            e.LoadFlags = InfoExEntry.ELoadFlags.Seq;
                        }
                        else if (bnk)
                        {
                            e.LoadFlags = InfoExEntry.ELoadFlags.Bank;
                        }
                        else if (wsd)
                        {
                            e.LoadFlags = InfoExEntry.ELoadFlags.Wsd;
                        }
                        else if (war)
                        {
                            e.LoadFlags = InfoExEntry.ELoadFlags.Warc;
                        }

                        //Add entry.
                        ExtraInfo.Add(e);
                    }
                }

                //Close the table.
                FileReader.CloseReferenceTable("Ex");

                //Close extended info block.
                FileReader.CloseBlock(br);
            }

            //Close file.
            FileReader.CloseFile(br);
        }