private byte[] BuildSectionHeader(MSBTSectionHeader sectionHeader)
 {
     using (MemoryStream stream = new MemoryStream())
     {
         using (BinaryWriter w = new BinaryWriter(stream))
         {
             //Label
             w.Write(Encoding.ASCII.GetBytes(sectionHeader.Label));
             while (w.BaseStream.Length < 4)
             {
                 w.Write((byte)0x0);
             }
             //SectionSize
             w.Write(ConvertUInt32BigEndian(sectionHeader.SectionSize));
             while (w.BaseStream.Length < 0x10)
             {
                 w.Write((byte)0x0);
             }
         }
         return(stream.ToArray());
     }
 }
        public SoundMSBTFile(SmashProjectManager projectManager, SoundEntryCollection sEntryCollection, string path)
        {
            _ResCol = projectManager.GetResourceCollection(path);
            string soundMSBTFile;

            soundMSBTFile = PathHelper.FolderEditorMods + path;
            if (!File.Exists(soundMSBTFile))
            {
                soundMSBTFile = projectManager.ExtractResource(path, PathHelper.FolderTemp);
            }
            SoundEntryCollection = sEntryCollection;
            _VarsMSBT            = new SortedDictionary <string, MSBTVariable>();
            _Path = path;

            using (FileStream fileStream = File.Open(soundMSBTFile, FileMode.Open))
            {
                using (BinaryReader b = new BinaryReader(fileStream))
                {
                    ParseFileHeader(b);
                    if (_Header.Label != "MsgStdBn")
                    {
                        throw new Exception(string.Format("Can't load '{0}', the file doesn't appear to be a MSBT file.", soundMSBTFile));
                    }

                    //Keep header
                    b.BaseStream.Position = 0;
                    uint offSetLBL1 = HEADER_LEN;

                    //Getting offset to ATR1
                    b.BaseStream.Position = offSetLBL1;
                    _SectionLBL1          = ParseSectionHeader(b);
                    if (_SectionLBL1.Label != "LBL1")
                    {
                        throw new Exception(string.Format("Error while reading '{0}', can't find LBL1 section.", soundMSBTFile));
                    }
                    uint offSetATR1 = offSetLBL1 + HEADER_LBL1 + _SectionLBL1.SectionSize;
                    while (offSetATR1 % 0x10 != 0)// || offSetATR1 == offSetLBL1 + HEADER_LBL1 + _SectionLBL1.SectionSize)
                    {
                        offSetATR1++;
                    }


                    //Getting offset to TXT2
                    b.BaseStream.Position = offSetATR1;
                    _SectionATR1          = ParseSectionHeader(b);
                    if (_SectionATR1.Label != "ATR1")
                    {
                        throw new Exception(string.Format("Error while reading '{0}', can't find ATR1 section.", soundMSBTFile));
                    }
                    b.BaseStream.Position = offSetATR1 + HEADER_ATR1;
                    _ATR1Bloc             = b.ReadBytes((int)_SectionATR1.SectionSize);
                    uint offSetTXT2 = offSetATR1 + HEADER_ATR1 + _SectionATR1.SectionSize;
                    while (offSetTXT2 % 0x10 != 0)// || offSetTXT2 == offSetATR1 + HEADER_ATR1 + _SectionATR1.SectionSize)
                    {
                        offSetTXT2++;
                    }

                    //Parsing TXT2
                    b.BaseStream.Position = offSetTXT2;
                    _SectionTXT2          = ParseSectionHeader(b);
                    if (_SectionTXT2.Label != "TXT2")
                    {
                        throw new Exception(string.Format("Error while reading '{0}', can't find TXT2 section.", soundMSBTFile));
                    }
                    b.BaseStream.Position += 8;
                    uint   txt2NbrEntries = ReadUInt32BigEndian(b);
                    uint[] offSetIntList  = new uint[txt2NbrEntries];
                    for (int i = 0; i < txt2NbrEntries; i++)
                    {
                        offSetIntList[i] = ReadUInt32BigEndian(b);
                    }

                    b.BaseStream.Position = offSetTXT2 + HEADER_TXT2;
                    byte[]   txt2Bloc      = b.ReadBytes((int)_SectionTXT2.SectionSize);
                    string[] msbtvariables = GetMSBTStringVariables(txt2Bloc, txt2NbrEntries, offSetIntList);

                    //Associate strings to variable names
                    b.BaseStream.Position = offSetLBL1 + HEADER_LBL1;
                    _SizeHashTable        = ReadUInt32BigEndian(b);
                    uint lbl1ChecksumSize = _SizeHashTable * 0x8;
                    uint nbrEntries       = 0;
                    for (int i = 0; i < _SizeHashTable; i++)
                    {
                        nbrEntries            += ReadUInt32BigEndian(b);
                        b.BaseStream.Position += 4;
                    }
                    if (msbtvariables.Length != nbrEntries)
                    {
                        throw new Exception(string.Format("Error while reading '{0}', the number of LBL1 entries doesn't match the number of TXT2 entries.", soundMSBTFile));
                    }

                    b.BaseStream.Position = offSetLBL1 + HEADER_LBL1 + 0x4 + lbl1ChecksumSize;
                    for (int i = 0; i < nbrEntries; i++)
                    {
                        string       variableName  = b.ReadString();
                        uint         variableIndex = ReadUInt32BigEndian(b);
                        MSBTVariable newVariable   = new MSBTVariable(variableName, msbtvariables[variableIndex]);
                        _VarsMSBT.Add(variableName, newVariable);
                    }
                }
            }

            //Assign SoundEntries variables
            if (!_ResCol.IsRegion)
            {
                foreach (SoundEntry sEntry in sEntryCollection.SoundEntries)
                {
                    sEntry.Title          = GetVariableValue(VAR_TITLE + sEntry.OriginalSoundLabel, Strings.DEFAULT_SENTRY_TITLE);
                    sEntry.SoundTestTitle = GetVariableValue(VAR_TITLE2 + sEntry.OriginalSoundLabel, Strings.DEFAULT_SENTRY_TITLE2);
                    sEntry.Description    = GetVariableValue(VAR_DESCRIPTION + sEntry.OriginalSoundLabel, string.Empty);
                    sEntry.Description2   = GetVariableValue(VAR_DESCRIPTION2 + sEntry.OriginalSoundLabel, string.Empty);
                    sEntry.Source         = GetVariableValue(VAR_SOURCE + sEntry.OriginalSoundLabel, string.Empty);
                }
            }
        }