/// <summary> /// Write a bank file. /// </summary> /// <param name="writeMode">Write mode.</param> /// <param name="bw">The writer.</param> public void Write(WriteMode writeMode, BinaryDataWriter bw) { //Set the write mode. this.writeMode = writeMode; //Init the bank file. FileWriter FileWriter = new FileWriter(); FileWriter.InitFile(bw, writeMode, "BNK", 1, Version); //Init info block. FileWriter.InitBlock(bw, ReferenceTypes.BNK_Block_Info, "INFO"); //Init the reference to the wave id table. FileWriter.InitReference(bw, "WaveIdTableRef"); //Init the reference to the instrument reference table. FileWriter.InitReference(bw, "InstrumentReferenceTableRef"); //Write the instrument reference table. if (Instruments != null) { //Write the reference. FileWriter.CloseReference(bw, ReferenceTypes.Table_Reference, "InstrumentReferenceTableRef"); //Reference table is a structure. FileWriter.StartStructure(bw); //Init the instrument reference table. FileWriter.InitReferenceTable(bw, Instruments.Count, "InstrumentReferenceTable"); //Write each instrument. for (int i = 0; i < Instruments.Count; i++) { //If instrument is null. if (Instruments[i] == null) { //Write the null reference. FileWriter.AddReferenceTableNullReferenceWithType(ReferenceTypes.BNK_Info_Null, "InstrumentReferenceTable"); } //Instrument is not null. else { //Add reference. FileWriter.AddReferenceTableReference(bw, ReferenceTypes.BNK_Info_Instrument, "InstrumentReferenceTable"); //Instrument is a new structure. FileWriter.StartStructure(bw); //Instrument type reference. FileWriter.InitReference(bw, "InstType"); //Get the type of reference. switch (Instruments[i].GetInstrumentType()) { //Direct. case InstrumentType.Direct: FileWriter.CloseReference(bw, ReferenceTypes.BNK_RefTable_Direct, "InstType"); break; //Index. case InstrumentType.Index: FileWriter.CloseReference(bw, ReferenceTypes.BNK_RefTable_Index, "InstType"); break; //Range. case InstrumentType.Range: FileWriter.CloseReference(bw, ReferenceTypes.BNK_RefTable_Range, "InstType"); break; } //Instrument is a new structure. FileWriter.StartStructure(bw); //Switch the type of reference. switch (Instruments[i].GetInstrumentType()) { //Direct. case InstrumentType.Direct: //Direct. DirectInstrument d = ((DirectInstrument)Instruments[i]); //Write key region reference. FileWriter.InitReference(bw, "KeyRegion"); //Null region. if (d.KeyRegion == null) { FileWriter.CloseNullReference(bw, "KeyRegion"); } //Not null. else { //Close the reference. FileWriter.CloseReference(bw, ReferenceTypes.BNK_Info_KeyRegion, "KeyRegion"); //Write key region. WriteKeyRegion(bw, d.KeyRegion, FileWriter); } break; //Ranged. case InstrumentType.Range: //Set range info. RangeInstrument ran = (RangeInstrument)Instruments[i]; //Write stuff. bw.Write((byte)ran.StartNote); bw.Write((byte)ran.EndNote); bw.Write((UInt16)0); //Init each reference. for (int j = 0; j < ran.NoteCount; j++) { FileWriter.InitReference(bw, "Ran" + j); } //Write each key region. for (int j = 0; j < ran.NoteCount; j++) { //Null region. if (ran[j] == null) { FileWriter.CloseNullReference(bw, "Ran" + j); } //Write key region. else { //Key region reference. FileWriter.CloseReference(bw, ReferenceTypes.BNK_Info_KeyRegion, "Ran" + j); //Write the key region. WriteKeyRegion(bw, ran[j], FileWriter); } } break; //Index. case InstrumentType.Index: //Set index data. IndexInstrument ind = (IndexInstrument)Instruments[i]; //Write the table of indices. bw.Write((uint)ind.Count); for (int j = 0; j < ind.Count; j++) { bw.Write((byte)ind.Keys.ElementAt(j)); } //Write padding. FileWriter.Align(bw, 4); //Init each index reference. for (int j = 0; j < ind.Count; j++) { FileWriter.InitReference(bw, "Ind" + j); } //Write each index. for (int j = 0; j < ind.Count; j++) { //Null index. if (ind.Values.ElementAt(j) == null) { FileWriter.CloseNullReference(bw, "Ind" + j); } //Index exist. else { //Close reference. FileWriter.CloseReference(bw, ReferenceTypes.BNK_Info_KeyRegion, "Ind" + j); //Write the key region. WriteKeyRegion(bw, ind.Values.ElementAt(j), FileWriter); } } break; } //End the instrument. FileWriter.EndStructure(); //End the instrument structure. FileWriter.EndStructure(); } } //Close the instrument reference table. FileWriter.CloseReferenceTable(bw, "InstrumentReferenceTable"); //End the instrument reference table structure. FileWriter.EndStructure(); } else { //Write null offset. FileWriter.CloseNullReference(bw, "InstrumentReferenceTableRef"); } //Write the wave id table. if (Waves != null) { //Write the reference. FileWriter.CloseReference(bw, ReferenceTypes.Tables, "WaveIdTableRef"); //Write the wave id table. bw.Write((uint)Waves.Count); for (int i = 0; i < Waves.Count; i++) { Id warId = new Id(SoundTypes.WaveArchive, (uint)Waves[i].WarIndex); warId.Write(ref bw); bw.Write((uint)Waves[i].WaveIndex); } } //Null wave ids. else { FileWriter.CloseNullReference(bw, "WaveIdTableRef"); } //Close the info block. FileWriter.CloseBlock(bw); //Close the bank file. FileWriter.CloseFile(bw); }