Skip to content

Commit

Permalink
Implement non-asset file lint
Browse files Browse the repository at this point in the history
  • Loading branch information
jieyouxu committed Aug 15, 2023
1 parent 6813c26 commit 23b0add
Show file tree
Hide file tree
Showing 5 changed files with 76 additions and 0 deletions.
26 changes: 26 additions & 0 deletions src/gui/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -998,6 +998,32 @@ impl App {
});
});
}

if !report.non_asset_file_mods.is_empty() {
CollapsingHeader::new(
RichText::new(
"⚠ Mod(s) with non-asset files detected",
)
.color(AMBER),
)
.default_open(true)
.show(ui, |ui| {
report.non_asset_file_mods.iter().for_each(|(r#mod, files)| {
CollapsingHeader::new(
RichText::new(format!(
"⚠ {} includes one or more non-asset files",
r#mod.url
))
.color(AMBER),
)
.show(ui, |ui| {
files.iter().for_each(|file| {
ui.label(file);
});
});
});
});
}
});
} else {
ui.spinner();
Expand Down
24 changes: 24 additions & 0 deletions src/mod_lint.rs
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ pub struct ModLintReport {
pub empty_archive_mods: BTreeSet<ModSpecification>,
pub archive_with_only_non_pak_files_mods: BTreeSet<ModSpecification>,
pub archive_with_multiple_paks_mods: BTreeSet<ModSpecification>,
pub non_asset_file_mods: BTreeMap<ModSpecification, BTreeSet<String>>,
}

pub fn lint(mods: &[(ModSpecification, PathBuf)]) -> Result<ModLintReport> {
Expand All @@ -34,6 +35,7 @@ pub fn lint(mods: &[(ModSpecification, PathBuf)]) -> Result<ModLintReport> {
let mut archive_with_only_non_pak_files_mods = BTreeSet::new();
let mut empty_archive_mods = BTreeSet::new();
let mut archive_with_multiple_paks_mods = BTreeSet::new();
let mut non_asset_file_mods = BTreeMap::new();

for (mod_spec, mod_pak_path) in mods {
trace!(?mod_spec, ?mod_pak_path);
Expand Down Expand Up @@ -84,6 +86,27 @@ pub fn lint(mods: &[(ModSpecification, PathBuf)]) -> Result<ModLintReport> {
let new_path_str = &new_path.to_string_lossy().replace('\\', "/");
let lowercase = new_path_str.to_ascii_lowercase();

// Note that including ushaderbytecode is handled by the specific shader-inclusion lint,
// so we avoid duplicating diagnostics here.
if !(lowercase.ends_with(".uexp")
|| lowercase.ends_with(".uasset")
|| lowercase.ends_with(".ubulk")
|| lowercase.ends_with(".ufont")
|| lowercase.ends_with("assetregistry.bin")
|| lowercase.ends_with(".ushaderbytecode"))
{
trace!(
"file is not known unreal asset: `{}`",
lowercase
);
non_asset_file_mods
.entry(mod_spec.clone())
.and_modify(|files: &mut BTreeSet<String>| {
files.insert(lowercase.clone());
})
.or_insert_with(|| [lowercase.clone()].into());
}

let mut buf = vec![];
let mut writer = Cursor::new(&mut buf);
pak.read_file(&p, &mut pak_bufs[0].1, &mut writer)?;
Expand Down Expand Up @@ -165,5 +188,6 @@ pub fn lint(mods: &[(ModSpecification, PathBuf)]) -> Result<ModLintReport> {
empty_archive_mods,
archive_with_only_non_pak_files_mods,
archive_with_multiple_paks_mods,
non_asset_file_mods,
})
}
Binary file added test_assets/lints/non_asset_files.pak
Binary file not shown.
Empty file.
26 changes: 26 additions & 0 deletions tests/lint/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -189,3 +189,29 @@ pub fn test_lint_multi_pak_archive() {

assert!(archive_with_multiple_paks_mods.contains(&multiple_paks_spec));
}

#[test]
pub fn test_lint_non_asset_files() {
let base_path = PathBuf::from_str("test_assets/lints/").unwrap();
assert!(base_path.exists());
let non_asset_files_pak_path = base_path.clone().join("non_asset_files.pak");
assert!(non_asset_files_pak_path.exists());

let non_asset_files_spec = ModSpecification {
url: "non_asset_files_pak".to_string(),
};

let mods = vec![(non_asset_files_spec.clone(), non_asset_files_pak_path)];

let ModLintReport {
non_asset_file_mods,
..
} = drg_mod_integration::mod_lint::lint(&mods).unwrap();

println!("{:#?}", non_asset_file_mods);

assert_eq!(
non_asset_file_mods.get(&non_asset_files_spec),
Some(&["never_gonna_let_you_down.txt".to_string()].into())
);
}

0 comments on commit 23b0add

Please sign in to comment.