-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
5b9f1c7
commit 3f505a6
Showing
6 changed files
with
59 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
hello world! |
Binary file not shown.
Binary file not shown.
Binary file not shown.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,55 @@ | ||
use crate::cc; | ||
use core::mem; | ||
use std::io::Read; | ||
|
||
#[derive(Clone, Copy, Debug, Eq, PartialEq)] | ||
pub enum FileFormat { | ||
TES3, | ||
TES4, | ||
FO4, | ||
} | ||
|
||
const BSA: u32 = cc::make_four(b"BSA"); | ||
const BTDX: u32 = cc::make_four(b"BTDX"); | ||
|
||
#[allow(clippy::module_name_repetitions)] | ||
pub fn guess_format<In>(source: &mut In) -> Option<FileFormat> | ||
where | ||
In: ?Sized + Read, | ||
{ | ||
let mut buf = [0u8; mem::size_of::<u32>()]; | ||
source.read_exact(&mut buf).ok()?; | ||
let magic = u32::from_le_bytes(buf); | ||
match magic { | ||
0x100 => Some(FileFormat::TES3), | ||
BSA => Some(FileFormat::TES4), | ||
BTDX => Some(FileFormat::FO4), | ||
_ => None, | ||
} | ||
} | ||
|
||
#[cfg(test)] | ||
mod tests { | ||
use crate::FileFormat; | ||
use anyhow::Context as _; | ||
use std::{fs::File, path::Path}; | ||
|
||
#[test] | ||
fn guess() -> anyhow::Result<()> { | ||
let root = Path::new("data/common_guess_test"); | ||
let tests = [ | ||
(FileFormat::TES3, "tes3.bsa"), | ||
(FileFormat::TES4, "tes4.bsa"), | ||
(FileFormat::FO4, "fo4.ba2"), | ||
]; | ||
|
||
for (format, file_name) in tests { | ||
let mut file = File::open(root.join(file_name)) | ||
.with_context(|| format!("failed to open file: {file_name}"))?; | ||
let guess = crate::guess_format(&mut file); | ||
assert_eq!(guess, Some(format)); | ||
} | ||
|
||
Ok(()) | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters