use std::cmp::Ordering;
use std::cmp::Ordering::{Greater, Less};
use std::path::{Path, PathBuf};
use crate::common::{get_platform_string, read_version, Platform};
use crate::repository::RepositoryType::{Base, Expansion};
#[derive(Debug, PartialEq, Eq, Copy, Clone)]
#[repr(C)]
pub enum RepositoryType {
Base,
Expansion {
number: i32,
},
}
#[derive(Debug, Clone)]
pub struct Repository {
pub name: String,
pub platform: Platform,
pub repo_type: RepositoryType,
pub version: Option<String>,
}
impl Eq for Repository {}
impl PartialEq for Repository {
fn eq(&self, other: &Self) -> bool {
self.name == other.name
}
}
impl Ord for Repository {
fn cmp(&self, other: &Self) -> Ordering {
match self.repo_type {
Base => Less,
Expansion { number } => {
let super_number = number;
match other.repo_type {
Base => Greater,
Expansion { number } => super_number.cmp(&number),
}
}
}
}
}
impl PartialOrd for Repository {
fn partial_cmp(&self, other: &Self) -> Option<Ordering> {
Some(self.cmp(other))
}
}
#[derive(Debug, PartialEq, Eq, Copy, Clone)]
pub enum Category {
Common = 0x00,
BackgroundCommon = 0x01,
Background = 0x02,
Cutscene = 0x03,
Character = 0x04,
Shader = 0x05,
UI = 0x06,
Sound = 0x07,
VFX = 0x08,
UIScript = 0x09,
EXD = 0x0A,
GameScript = 0x0B,
Music = 0x0C,
SqPackTest = 0x12,
Debug = 0x13,
}
pub fn string_to_category(string: &str) -> Option<Category> {
use crate::repository::Category::*;
match string {
"common" => Some(Common),
"bgcommon" => Some(BackgroundCommon),
"bg" => Some(Background),
"cut" => Some(Cutscene),
"chara" => Some(Character),
"shader" => Some(Shader),
"ui" => Some(UI),
"sound" => Some(Sound),
"vfx" => Some(VFX),
"ui_script" => Some(UIScript),
"exd" => Some(EXD),
"game_script" => Some(GameScript),
"music" => Some(Music),
"sqpack_test" => Some(SqPackTest),
"debug" => Some(Debug),
_ => None,
}
}
impl Repository {
pub fn from_existing_base(platform: Platform, dir: &str) -> Option<Repository> {
let path = Path::new(dir);
if path.metadata().is_err() {
return None;
}
let mut d = PathBuf::from(dir);
d.push("ffxivgame.ver");
let version = read_version(d.as_path());
Some(Repository {
name: "ffxiv".to_string(),
platform,
repo_type: Base,
version,
})
}
pub fn from_existing_expansion(platform: Platform, dir: &str) -> Option<Repository> {
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,
platform,
repo_type: Expansion {
number: expansion_number,
},
version: read_version(d.as_path()),
})
}
pub fn index_filename(&self, chunk: u8, category: Category) -> String {
format!(
"{:02x}{:02}{:02}.{}.index",
category as i32,
self.expansion(),
chunk,
get_platform_string(&self.platform)
)
}
pub fn index2_filename(&self, chunk: u8, category: Category) -> String {
format!("{}2", self.index_filename(chunk, category))
}
pub fn dat_filename(&self, chunk: u8, category: Category, data_file_id: u32) -> String {
let expansion = self.expansion();
let platform = get_platform_string(&self.platform);
format!(
"{:02x}{expansion:02}{chunk:02}.{platform}.dat{data_file_id}",
category as u32
)
}
fn expansion(&self) -> i32 {
match self.repo_type {
Base => 0,
Expansion { number } => number,
}
}
}
#[cfg(test)]
mod tests {
use crate::common::Platform;
use std::path::PathBuf;
use super::*;
#[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(Platform::Win32, 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(Platform::Win32, d.to_str().unwrap());
assert!(repository.is_some());
assert_eq!(repository.unwrap().version.unwrap(), "2012.01.01.0000.0000");
}
#[test]
fn test_win32_filenames() {
let repo = Repository {
name: "ffxiv".to_string(),
platform: Platform::Win32,
repo_type: RepositoryType::Base,
version: None,
};
assert_eq!(
repo.index_filename(0, Category::Music),
"0c0000.win32.index"
);
assert_eq!(
repo.index2_filename(0, Category::Music),
"0c0000.win32.index2"
);
assert_eq!(
repo.dat_filename(0, Category::GameScript, 1),
"0b0000.win32.dat1"
);
}
#[test]
fn test_ps3_filenames() {
let repo = Repository {
name: "ffxiv".to_string(),
platform: Platform::PS3,
repo_type: RepositoryType::Base,
version: None,
};
assert_eq!(repo.index_filename(0, Category::Music), "0c0000.ps3.index");
assert_eq!(
repo.index2_filename(0, Category::Music),
"0c0000.ps3.index2"
);
assert_eq!(
repo.dat_filename(0, Category::GameScript, 1),
"0b0000.ps3.dat1"
);
}
#[test]
fn test_ps4_filenames() {
let repo = Repository {
name: "ffxiv".to_string(),
platform: Platform::PS4,
repo_type: RepositoryType::Base,
version: None,
};
assert_eq!(repo.index_filename(0, Category::Music), "0c0000.ps4.index");
assert_eq!(
repo.index2_filename(0, Category::Music),
"0c0000.ps4.index2"
);
assert_eq!(
repo.dat_filename(0, Category::GameScript, 1),
"0b0000.ps4.dat1"
);
}
}