Skip to content

Commit

Permalink
Add is_valid_as_medium_plugin()
Browse files Browse the repository at this point in the history
  • Loading branch information
Ortham committed Jun 19, 2024
1 parent 861c4b6 commit a9a5c7b
Show file tree
Hide file tree
Showing 2 changed files with 67 additions and 0 deletions.
16 changes: 16 additions & 0 deletions src/game_id.rs
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,10 @@ impl GameId {
GameId::SkyrimSE | GameId::Fallout4 | GameId::Starfield
)
}

pub fn supports_medium_plugins(self) -> bool {
self == GameId::Starfield
}
}

#[cfg(test)]
Expand Down Expand Up @@ -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());
}
}
51 changes: 51 additions & 0 deletions src/plugin.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -435,6 +454,13 @@ impl Plugin {
_ => 0..=0,
}
}

fn valid_medium_form_id_range(&self) -> RangeInclusive<u32> {
match self.game_id {
GameId::Starfield => 0..=0xFFFF,
_ => 0..=0,
}
}
}

fn sorted_slices_intersect<T: PartialOrd>(left: &[T], right: &[T]) -> bool {
Expand Down Expand Up @@ -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(
) {
Expand All @@ -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(
Expand Down

0 comments on commit a9a5c7b

Please sign in to comment.