1
Fork 0
mirror of https://github.com/redstrate/Physis.git synced 2025-04-20 11:47:46 +00:00

Simplify and change Repository API, test it as well

We can simplify some of the repository code since there's now two
separate functions for base/expansion repositories. Added tests to make
sure reading doesn't regress like it did before.
This commit is contained in:
Joshua Goins 2024-04-15 19:25:31 -04:00
parent 7a496f7f3c
commit 5c66c51713
4 changed files with 61 additions and 49 deletions

View file

@ -0,0 +1 @@
2012.01.01.0000.0000

View file

@ -0,0 +1 @@
2012.01.01.0000.0000

View file

@ -120,7 +120,7 @@ impl GameData {
.collect(); .collect();
for repository_path in repository_paths { for repository_path in repository_paths {
if let Some(expansion_repository) =Repository::from_existing(repository_path.path().to_str().unwrap()) { if let Some(expansion_repository) = Repository::from_existing_expansion(repository_path.path().to_str().unwrap()) {
self.repositories.push(expansion_repository); self.repositories.push(expansion_repository);
} }
} }

View file

@ -123,50 +123,8 @@ pub fn string_to_category(string: &str) -> Option<Category> {
} }
impl Repository { impl Repository {
/// Creates a new `Repository`, from an existing repository directory. This may return `None` if /// Creates a new base `Repository`, from an existing directory. This may return `None` if
/// the directory is invalid, e.g. a version file is missing. /// the directory is invalid, e.g. a version file is missing.
pub fn from_existing(dir: &str) -> Option<Repository> {
let path = Path::new(dir);
if path.metadata().is_err() {
return None;
}
let name = String::from(path.file_stem().unwrap().to_str().unwrap());
let repo_type = if name == "ffxiv" {
Base
} else {
Expansion {
number: name[2..3].parse().unwrap(),
}
};
// use from_existing_base instead
if repo_type == Base {
return None
}
let version = if repo_type == Base {
let mut d = PathBuf::from(dir);
d.pop();
d.pop();
d.push("ffxivgame.ver");
read_version(d.as_path())
} else {
let mut d = PathBuf::from(dir);
d.push(format!("{}.ver", name));
read_version(d.as_path())
};
Some(Repository {
name,
repo_type,
version,
})
}
pub fn from_existing_base(dir: &str) -> Option<Repository> { pub fn from_existing_base(dir: &str) -> Option<Repository> {
let path = Path::new(dir); let path = Path::new(dir);
if path.metadata().is_err() { if path.metadata().is_err() {
@ -178,17 +136,33 @@ impl Repository {
let version = read_version(d.as_path()); let version = read_version(d.as_path());
Some(Repository { Some(Repository {
name: "ffxiv".parse().unwrap(), name: "ffxiv".to_string(),
repo_type: Base, repo_type: Base,
version, version,
}) })
} }
fn expansion(&self) -> i32 { /// Creates a new expansion `Repository`, from an existing directory. This may return `None` if
match self.repo_type { /// the directory is invalid, e.g. a version file is missing.
Base => 0, pub fn from_existing_expansion(dir: &str) -> Option<Repository> {
Expansion { number } => number, let path = Path::new(dir);
if path.metadata().is_err() {
return None;
} }
let name = String::from(path.file_stem()?.to_str()?);
let expansion_number = name[2..3].parse().ok()?;
let mut d = PathBuf::from(dir);
d.push(format!("{name}.ver"));
Some(Repository {
name,
repo_type: Expansion {
number: expansion_number,
},
version: read_version(d.as_path()),
})
} }
/// Calculate an index filename for a specific category, like _"0a0000.win32.index"_. /// Calculate an index filename for a specific category, like _"0a0000.win32.index"_.
@ -221,4 +195,40 @@ impl Repository {
category as u32 category as u32
) )
} }
fn expansion(&self) -> i32 {
match self.repo_type {
Base => 0,
Expansion { number } => number,
}
}
}
#[cfg(test)]
mod tests {
use std::path::PathBuf;
use crate::repository::Repository;
#[test]
fn test_base() {
let mut d = PathBuf::from(env!("CARGO_MANIFEST_DIR"));
d.push("resources/tests");
d.push("ffxiv");
let repository = Repository::from_existing_base(d.to_str().unwrap());
assert!(repository.is_some());
assert_eq!(repository.unwrap().version.unwrap(), "2012.01.01.0000.0000");
}
#[test]
fn test_expansion() {
let mut d = PathBuf::from(env!("CARGO_MANIFEST_DIR"));
d.push("resources/tests");
d.push("ex1");
let repository = Repository::from_existing_expansion(d.to_str().unwrap());
assert!(repository.is_some());
assert_eq!(repository.unwrap().version.unwrap(), "2012.01.01.0000.0000");
}
} }