Skip to content

Commit

Permalink
Fix only lowercase plugin extensions being recognised
Browse files Browse the repository at this point in the history
.esp, .esm, .esl and those plus .ghost suffixes are now matched
case-insensitively.
  • Loading branch information
Ortham committed Aug 26, 2023
1 parent 1043c21 commit d8af231
Show file tree
Hide file tree
Showing 2 changed files with 32 additions and 25 deletions.
7 changes: 4 additions & 3 deletions src/function/eval.rs
Original file line number Diff line number Diff line change
Expand Up @@ -16,9 +16,9 @@ fn evaluate_file_path(state: &State, file_path: &Path) -> Result<bool, Error> {
}

fn is_match(game_type: GameType, regex: &Regex, file_name: &OsStr) -> bool {
file_name
normalise_file_name(game_type, file_name)
.to_str()
.map(|s| regex.is_match(normalise_file_name(game_type, s)))
.map(|s| regex.is_match(s))
.unwrap_or(false)
}

Expand All @@ -29,7 +29,8 @@ fn evaluate_regex(
regex: &Regex,
mut condition: impl FnMut() -> bool,
) -> Result<bool, Error> {
let dir_iterator = match read_dir(data_path.join(parent_path)) {
let parent_path = data_path.join(parent_path);
let dir_iterator = match read_dir(&parent_path) {
Ok(i) => i,
Err(_) => return Ok(false),
};
Expand Down
50 changes: 28 additions & 22 deletions src/function/path.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,24 +8,22 @@ use crate::{GameType, State};
const GHOST_EXTENSION: &str = "ghost";
const GHOST_EXTENSION_WITH_PERIOD: &str = ".ghost";

fn is_unghosted_plugin_file_extension(game_type: GameType, extension: &str) -> bool {
match extension {
"esp" | "esm" => true,
"esl" if game_type.supports_light_plugins() => true,
_ => false,
}
fn is_unghosted_plugin_file_extension(game_type: GameType, extension: &OsStr) -> bool {
extension.eq_ignore_ascii_case("esp")
|| extension.eq_ignore_ascii_case("esm")
|| (game_type.supports_light_plugins() && extension.eq_ignore_ascii_case("esl"))
}

fn has_unghosted_plugin_file_extension(game_type: GameType, path: &Path) -> bool {
match path.extension().and_then(OsStr::to_str) {
match path.extension() {
Some(ext) => is_unghosted_plugin_file_extension(game_type, ext),
_ => false,
}
}

pub fn has_plugin_file_extension(game_type: GameType, path: &Path) -> bool {
match path.extension().and_then(OsStr::to_str) {
Some(GHOST_EXTENSION) => path
match path.extension() {
Some(ext) if ext.eq_ignore_ascii_case(GHOST_EXTENSION) => path
.file_stem()
.map(|s| has_unghosted_plugin_file_extension(game_type, Path::new(s)))
.unwrap_or(false),
Expand All @@ -45,10 +43,18 @@ fn add_ghost_extension(path: PathBuf) -> PathBuf {
}
}

pub fn normalise_file_name(game_type: GameType, name: &str) -> &str {
if let Some(stem) = name.strip_suffix(GHOST_EXTENSION_WITH_PERIOD) {
if has_unghosted_plugin_file_extension(game_type, Path::new(stem)) {
return stem;
pub fn normalise_file_name(game_type: GameType, name: &OsStr) -> &OsStr {
let path = Path::new(name);
if path
.extension()
.map(|s| s.eq_ignore_ascii_case(GHOST_EXTENSION))
.unwrap_or(false)
{
// name ends in .ghost, trim it and then check the file extension.
if let Some(stem) = path.file_stem() {
if has_unghosted_plugin_file_extension(game_type, Path::new(stem)) {
return stem;
}
}
}

Expand Down Expand Up @@ -89,7 +95,7 @@ mod tests {

#[test]
fn is_unghosted_plugin_file_extension_should_be_true_for_esp_for_all_game_types() {
let extension = "esp";
let extension = OsStr::new("Esp");

assert!(is_unghosted_plugin_file_extension(
GameType::Morrowind,
Expand Down Expand Up @@ -131,7 +137,7 @@ mod tests {

#[test]
fn is_unghosted_plugin_file_extension_should_be_true_for_esm_for_all_game_types() {
let extension = "esm";
let extension = OsStr::new("Esm");

assert!(is_unghosted_plugin_file_extension(
GameType::Morrowind,
Expand Down Expand Up @@ -173,7 +179,7 @@ mod tests {

#[test]
fn is_unghosted_plugin_file_extension_should_be_true_for_esl_for_tes5se_tes5vr_fo4_and_fo4vr() {
let extension = "esl";
let extension = OsStr::new("Esl");

assert!(is_unghosted_plugin_file_extension(
GameType::SkyrimSE,
Expand All @@ -195,7 +201,7 @@ mod tests {

#[test]
fn is_unghosted_plugin_file_extension_should_be_false_for_esl_for_tes3_to_5_fo3_and_fonv() {
let extension = "esl";
let extension = OsStr::new("Esl");

assert!(!is_unghosted_plugin_file_extension(
GameType::Morrowind,
Expand All @@ -221,7 +227,7 @@ mod tests {

#[test]
fn is_unghosted_plugin_file_extension_should_be_false_for_ghost_for_all_game_types() {
let extension = "ghost";
let extension = OsStr::new("Ghost");

assert!(!is_unghosted_plugin_file_extension(
GameType::Morrowind,
Expand Down Expand Up @@ -263,7 +269,7 @@ mod tests {

#[test]
fn is_unghosted_plugin_file_extension_should_be_false_for_non_esp_esm_esl_for_all_game_types() {
let extension = "txt";
let extension = OsStr::new("txt");

assert!(!is_unghosted_plugin_file_extension(
GameType::Morrowind,
Expand Down Expand Up @@ -351,7 +357,7 @@ mod tests {
fn has_plugin_file_extension_should_return_true_if_the_path_has_a_ghosted_plugin_extension() {
assert!(has_plugin_file_extension(
GameType::Skyrim,
Path::new("plugin.esp.ghost")
Path::new("plugin.esp.Ghost")
));
}

Expand All @@ -368,15 +374,15 @@ mod tests {
) {
assert!(!has_plugin_file_extension(
GameType::Skyrim,
Path::new("plugin.bsa.ghost")
Path::new("plugin.bsa.Ghost")
));
}

#[test]
fn has_plugin_file_extension_should_return_false_if_the_path_has_only_ghost_extension() {
assert!(!has_plugin_file_extension(
GameType::Skyrim,
Path::new("plugin.ghost")
Path::new("plugin.Ghost")
));
}

Expand Down

0 comments on commit d8af231

Please sign in to comment.