From 5eb1d8c7ce97ae09e0595d9bb65e0d1c09fb2358 Mon Sep 17 00:00:00 2001 From: ashley Date: Sat, 2 Sep 2023 00:37:02 -0400 Subject: [PATCH] Fix bug with instrument lookups and validation --- src/reskit/soundtrack/engines/echo/engine.rs | 27 ++++++++++---------- 1 file changed, 14 insertions(+), 13 deletions(-) diff --git a/src/reskit/soundtrack/engines/echo/engine.rs b/src/reskit/soundtrack/engines/echo/engine.rs index 6178f61..84e89a7 100644 --- a/src/reskit/soundtrack/engines/echo/engine.rs +++ b/src/reskit/soundtrack/engines/echo/engine.rs @@ -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