diff --git a/src/game_id.rs b/src/game_id.rs index b0a3e3f..d4f3c3d 100644 --- a/src/game_id.rs +++ b/src/game_id.rs @@ -36,6 +36,10 @@ impl GameId { GameId::SkyrimSE | GameId::Fallout4 | GameId::Starfield ) } + + pub fn supports_medium_plugins(self) -> bool { + self == GameId::Starfield + } } #[cfg(test)] @@ -81,4 +85,16 @@ mod tests { fn supports_light_plugins_should_be_true_for_starfield() { assert!(GameId::Starfield.supports_light_plugins()); } + + #[test] + fn supports_medium_plugins_should_be_true_for_only_starfield() { + assert!(!GameId::Morrowind.supports_medium_plugins()); + assert!(!GameId::Oblivion.supports_medium_plugins()); + assert!(!GameId::Skyrim.supports_medium_plugins()); + assert!(!GameId::SkyrimSE.supports_medium_plugins()); + assert!(!GameId::Fallout3.supports_medium_plugins()); + assert!(!GameId::FalloutNV.supports_medium_plugins()); + assert!(!GameId::Fallout4.supports_medium_plugins()); + assert!(GameId::Starfield.supports_medium_plugins()); + } } diff --git a/src/plugin.rs b/src/plugin.rs index 13f9d32..cd1aff2 100644 --- a/src/plugin.rs +++ b/src/plugin.rs @@ -364,6 +364,25 @@ impl Plugin { } } + pub fn is_valid_as_medium_plugin(&self) -> bool { + if self.game_id.supports_medium_plugins() { + match &self.data.record_ids { + RecordIds::None => true, + RecordIds::FormIds(form_ids) => { + let valid_range = self.valid_medium_form_id_range(); + + form_ids + .iter() + .filter(|f| !f.is_overridden_record()) + .all(|f| valid_range.contains(&f.object_index())) + } + RecordIds::NamespacedIds(_) => false, // this should never happen. + } + } else { + false + } + } + pub fn is_valid_as_override_plugin(&self) -> bool { if self.game_id == GameId::Starfield { // If an override plugin has a record that does not override an existing record, that @@ -435,6 +454,13 @@ impl Plugin { _ => 0..=0, } } + + fn valid_medium_form_id_range(&self) -> RangeInclusive { + match self.game_id { + GameId::Starfield => 0..=0xFFFF, + _ => 0..=0, + } + } } fn sorted_slices_intersect(left: &[T], right: &[T]) -> bool { @@ -1877,6 +1903,19 @@ mod tests { assert_eq!(&0xFFF, range.end()); } + #[test] + fn valid_medium_form_id_range_should_be_0_to_0xffff() { + let mut plugin = Plugin::new( + GameId::Starfield, + Path::new("testing-plugins/Starfield/Data/Blank.small.esm"), + ); + assert!(plugin.parse_file(false).is_ok()); + + let range = plugin.valid_medium_form_id_range(); + assert_eq!(&0, range.start()); + assert_eq!(&0xFFFF, range.end()); + } + #[test] fn is_valid_as_light_plugin_should_be_true_if_the_plugin_has_no_form_ids_outside_the_valid_range( ) { @@ -1889,6 +1928,18 @@ mod tests { assert!(plugin.is_valid_as_light_plugin()); } + #[test] + fn is_valid_as_medium_plugin_should_be_true_if_the_plugin_has_no_form_ids_outside_the_valid_range( + ) { + let mut plugin = Plugin::new( + GameId::Starfield, + Path::new("testing-plugins/Starfield/Data/Blank.medium.esm"), + ); + assert!(plugin.parse_file(false).is_ok()); + + assert!(plugin.is_valid_as_light_plugin()); + } + #[test] fn is_valid_as_override_plugin_should_be_true_if_the_plugin_has_no_new_records() { let mut plugin = Plugin::new(