/// <summary> /// Create bank. /// </summary> private void CreateBnk_Click(object sender, EventArgs e) { //New bank and wave archive. BankInfo bnk = new BankInfo() { File = new Bank() }; WaveArchiveInfo war = new WaveArchiveInfo() { File = new WaveArchive() }; bool usesGen; //Get each instrument. List <InstrumentInfo> insts = new List <InstrumentInfo>(); List <string> wars = new List <string>(); for (int i = 0; i < instruments.Rows.Count - 1; i++) { //Get the cells. var bankCell = (DataGridViewComboBoxCell)instruments.Rows[i].Cells["bank"]; var instCell = (DataGridViewComboBoxCell)instruments.Rows[i].Cells["instrument"]; var idCell = (DataGridViewTextBoxCell)instruments.Rows[i].Cells["newId"]; var warModeCell = (DataGridViewComboBoxCell)instruments.Rows[i].Cells["waveArchiveMode"]; //Check. if (bankCell.Value == null || instCell.Value == null || idCell.Value == null || !int.TryParse(idCell.Value.ToString(), out _) || warModeCell.Value == null) { MessageBox.Show("Grid contains invalid data."); return; } //Get the bank. BankInfo b = SA.Banks.Where(x => x.Index == int.Parse(((string)bankCell.Value).Split('[')[1].Split(']')[0])).FirstOrDefault(); Instrument inst = b.File.Instruments.Where(x => x.Index == int.Parse(((string)instCell.Value).Split('[')[1].Split(']')[0])).FirstOrDefault(); int id = int.Parse(idCell.Value.ToString()); bool useExistingWar = warModeCell.Value != ((DataGridViewComboBoxColumn)instruments.Columns["waveArchiveMode"]).Items[0]; //Add the instrument. insts.Add(new InstrumentInfo() { Bank = b, Inst = Bank.DuplicateInstrument(inst), NewId = id, UseExistingWar = useExistingWar }); //Get wars. foreach (var n in inst.NoteInfo) { if (warModeCell.Value != warModeCell.Items[0] && n.InstrumentType == InstrumentType.PCM) { string name = "Null"; try { name = b.WaveArchives[n.WarId].Name; } catch { } if (!wars.Contains(name) && !name.Equals("Null")) { wars.Add(name); } } } } //Make sure amount of WARs is not over 4. if (wars.Count > 4) { MessageBox.Show("You can't generate a new bank that uses more than 4 wave archives."); return; } //Uses generated instrument. usesGen = insts.Where(x => x.UseExistingWar == false).Count() > 0; //Make sure amount of WARs is not over 3 if generated instrument. if (wars.Count > 3) { MessageBox.Show("You can't generate a new bank that uses more than 3 wave archives when creating a generated wave archive."); return; } //Get name and index. try { bnk.Index = SA.Banks.Last().Index + 1; } catch { } while (SA.Banks.Where(x => x.Index == bnk.Index).Count() > 0) { bnk.Index++; } try { war.Index = SA.WaveArchives.Last().Index + 1; } catch { } while (SA.WaveArchives.Where(x => x.Index == war.Index).Count() > 0) { war.Index++; } bnk.Name = "GENERATED_BANK_" + bnk.Index; war.Name = "GENERATED_WAR_" + war.Index; //Add instruments. Dictionary <ushort, ushort> warLinks = new Dictionary <ushort, ushort>(); if (usesGen) { warLinks.Add((ushort)war.Index, (ushort)warLinks.Count); } Dictionary <uint, ushort> swavLinks = new Dictionary <uint, ushort>(); ushort swarNum = usesGen ? (ushort)1 : (ushort)0; ushort swavNum = 0; foreach (var i in insts) { //Adjust wave info for note info. foreach (var n in i.Inst.NoteInfo.Where(x => x.InstrumentType == InstrumentType.PCM)) { //Get the hash for the true wave archive Id and wave Id. uint hash; try { hash = (uint)(i.Bank.WaveArchives[n.WarId].Index << 16) | n.WaveId; } catch { continue; } //Use the original wave archive. if (i.UseExistingWar) { if (!warLinks.ContainsKey((ushort)i.Bank.WaveArchives[n.WarId].Index)) { warLinks.Add((ushort)i.Bank.WaveArchives[n.WarId].Index, swarNum++); } n.WarId = warLinks[(ushort)i.Bank.WaveArchives[n.WarId].Index]; } //Use a new wave archive. else { if (!swavLinks.ContainsKey(hash)) { //Get the physical wave, and add it. try { //Add the swav. war.File.Waves.Add(i.Bank.WaveArchives[n.WarId].File.Waves[n.WaveId]); //Add the hash. swavLinks.Add(hash, swavNum++); } catch { } } n.WarId = 0; n.WaveId = swavLinks[hash]; } } //Set the new index. i.Inst.Index = i.NewId; //Finally add the instrument. bnk.File.Instruments.Add(i.Inst); } //Add the bank. if (warLinks.Count() > 4) { MessageBox.Show("Something went wrong, and the max number of wave archives (4) has been exceeded."); return; } int bnkWarId = 0; foreach (var w in warLinks) { switch (bnkWarId) { case 0: bnk.WaveArchives[0] = SA.WaveArchives.Where(x => x.Index == w.Key).FirstOrDefault(); bnk.ReadingWave0Id = w.Key; break; case 1: bnk.WaveArchives[1] = SA.WaveArchives.Where(x => x.Index == w.Key).FirstOrDefault(); bnk.ReadingWave1Id = w.Key; break; case 2: bnk.WaveArchives[2] = SA.WaveArchives.Where(x => x.Index == w.Key).FirstOrDefault(); bnk.ReadingWave2Id = w.Key; break; case 3: bnk.WaveArchives[3] = SA.WaveArchives.Where(x => x.Index == w.Key).FirstOrDefault(); bnk.ReadingWave3Id = w.Key; break; } bnkWarId++; } SA.Banks.Add(bnk); //Add the wave archive. if (usesGen) { SA.WaveArchives.Add(war); } //Close. Close(); MainWindow.UpdateNodes(); MainWindow.DoInfoStuff(); }
/// <summary> /// Write an instrument. /// </summary> /// <param name="bnk">The bank.</param> /// <param name="instrumentId">The instrument Id.</param> /// <param name="a">Sound archive.</param> /// <param name="war0">Wave archive 0.</param> /// <param name="war1">Wave archive 1.</param> /// <param name="war2">Wave archive 2.</param> /// <param name="war3">Wave archive 3.</param> public void WriteInstrument(Bank bnk, int instrumentId, SoundArchive a, ushort war0, ushort war1, ushort war2, ushort war3) { //Set the instrument. var repl = bnk.Instruments.Where(x => x.Index == instrumentId).FirstOrDefault(); Inst.Index = instrumentId; //Sound archive check. if (a == null) { return; } //Get wave archives. WaveArchiveInfo[] wars = new WaveArchiveInfo[4]; if (war0 != 0xFFFF) { wars[0] = a.WaveArchives.Where(x => x.Index == (int)war0).FirstOrDefault(); } if (war1 != 0xFFFF) { wars[1] = a.WaveArchives.Where(x => x.Index == (int)war1).FirstOrDefault(); } if (war2 != 0xFFFF) { wars[2] = a.WaveArchives.Where(x => x.Index == (int)war2).FirstOrDefault(); } if (war3 != 0xFFFF) { wars[3] = a.WaveArchives.Where(x => x.Index == (int)war3).FirstOrDefault(); } //Make sure there are linked wave archives. if (wars.Where(x => x != null).Count() < 1) { return; } //For each region in the instrument. foreach (var r in Inst.NoteInfo) { //PCM type. if (r.InstrumentType != InstrumentType.PCM) { continue; } //Waves are not null. if (Waves == null) { continue; } //Get entry. var e = Waves.Where(x => x.WarId == r.WarId && x.WaveId == r.WaveId).FirstOrDefault(); if (e == null) { continue; } //Wave is not null. if (e.Wave == null) { continue; } //Get MD5SUM of wave. string md5 = e.Wave.Md5Sum; //Try and find matching wave. bool found = false; for (int i = 0; i < wars.Length; i++) { if (wars[i] != null) { for (int j = 0; j < wars[i].File.Waves.Count; j++) { if (!found && wars[i].File.Waves[j].Md5Sum == md5) { r.WaveId = (ushort)j; r.WarId = (ushort)i; found = true; } } } } //Not found. if (!found) { RiffWave riff = new RiffWave(); riff.FromOtherStreamFile(e.Wave); WaveMapper mapper = new WaveMapper(new List <RiffWave>() { riff }, wars.Where(x => x != null).ToList(), true); mapper.MinimizeBox = false; mapper.ShowDialog(); if (mapper.WarMap == null) { return; } a.WaveArchives.Where(x => x.Index == mapper.WarMap[0]).FirstOrDefault().File.Waves.Add(e.Wave); r.WaveId = (ushort)(a.WaveArchives.Where(x => x.Index == mapper.WarMap[0]).FirstOrDefault().File.Waves.Count() - 1); r.WarId = (ushort)wars.ToList().IndexOf(a.WaveArchives.Where(x => x.Index == mapper.WarMap[0]).FirstOrDefault()); } } //Set instrument. bnk.Instruments[bnk.Instruments.IndexOf(repl)] = Inst; }