Fix bug with instrument lookups and validation

master
Ashley N. 2023-09-02 00:37:02 -04:00
parent 9d66523671
commit 5eb1d8c7ce
1 changed files with 14 additions and 13 deletions

View File

@ -300,21 +300,22 @@ fn get_events_for_channel( channels: &mut [Channel], active_channel: usize, row:
// Set the instrument
if let Some( instrument ) = &row.instrument_index {
// Validate the instrument is applicable to the current channel
let correct_type = match channels[ active_channel ].id {
ESF_FM_1..=ESF_FM_3 | ESF_FM_4..=ESF_FM_6 => asset_set.fm_ids.contains( instrument ),
ESF_FM_6_PCM => {
print_warning( "attempted to set instrument on FM6 when it is in pcm mode. this is valid, but it won't do anything until the next time it is set back to fm mode." );
asset_set.fm_ids.contains( instrument )
},
ESF_PSG_1..=ESF_PSG_4 => asset_set.psg_ids.contains( instrument ),
invalid_channel => return Err( format!( "internal error: get_events_for_channel: invalid channel {:#04X}", invalid_channel ) )?
};
// Hop one layer of indirection from `instrument` by getting it from instrument_set.module_instruments,
// then finding the index of that instrument in instrument_set.total_instruments.
let original_instrument = &asset_set.module_instruments[ *instrument as usize ];
let instrument = asset_set.total_instruments.iter().position( | instrument | instrument == original_instrument ).ok_or( "internal error: could not locate instrument in combined instrument set" )?;
let instrument = instrument as u16;
// Validate the instrument is applicable to the current channel
let correct_type = match channels[ active_channel ].id {
ESF_FM_1..=ESF_FM_3 | ESF_FM_4..=ESF_FM_6 => asset_set.fm_ids.contains( &instrument ),
ESF_FM_6_PCM => {
print_warning( "attempted to set instrument on FM6 when it is in pcm mode. this is valid, but it won't do anything until the next time it is set back to fm mode." );
asset_set.fm_ids.contains( &instrument )
},
ESF_PSG_1..=ESF_PSG_4 => asset_set.psg_ids.contains( &instrument ),
invalid_channel => return Err( format!( "internal error: get_events_for_channel: invalid channel {:#04X}", invalid_channel ) )?
};
// Do not set the same instrument if it was already set before
if &channels[ active_channel ].active_instrument != &Some( instrument as u16 ) {
@ -842,7 +843,7 @@ pub fn get_events_for_row( channels: &mut [Channel], module_instruments: &Vec<In
// Generate InstrumentSet which is used to validate when instruments are set on channels
let mut index = 0;
let fm_ids = module_instruments.iter()
let fm_ids = asset_set.instruments.iter()
.filter_map( | instrument | if matches!( instrument.instrument_type, InstrumentType::Fm2612( _ ) ) {
let result = index;
index += 1;
@ -854,7 +855,7 @@ pub fn get_events_for_row( channels: &mut [Channel], module_instruments: &Vec<In
.collect();
index = 0;
let psg_ids = module_instruments.iter()
let psg_ids = asset_set.instruments.iter()
.filter_map( | instrument | if matches!( instrument.instrument_type, InstrumentType::PsgDcsg( _ ) ) {
let result = index;
index += 1;