1
Fork 0
mirror of https://github.com/redstrate/Physis.git synced 2025-04-22 20:57:46 +00:00
physis/src/repository.rs

191 lines
5.5 KiB
Rust
Raw Normal View History

2022-10-25 13:02:06 -04:00
use crate::common::read_version;
2022-08-16 11:52:07 -04:00
use crate::repository::RepositoryType::{Base, Expansion};
2022-07-19 19:29:41 -04:00
use std::cmp::Ordering;
use std::cmp::Ordering::{Greater, Less};
use std::path::{Path, PathBuf};
/// The type of repository, discerning game data from expansion data.
2022-09-15 16:26:31 -04:00
#[derive(Debug, PartialEq, Eq, Copy, Clone)]
2022-08-09 21:52:07 -04:00
#[repr(C)]
2022-07-19 19:29:41 -04:00
pub enum RepositoryType {
/// The base game directory, like "ffxiv".
Base,
/// An expansion directory, like "ex1".
Expansion {
/// The expansion number starting at 1.
2022-08-16 11:52:07 -04:00
number: i32,
2022-07-19 19:29:41 -04:00
},
}
/// Encapsulates a directory of game data, such as "ex1". This data is also versioned.
/// This handles calculating the correct dat and index filenames, mainly for `GameData`.
#[derive(Debug)]
pub struct Repository {
/// The folder name, such as "ex1".
pub name: String,
/// The type of repository, such as "base game" or "expansion".
pub repo_type: RepositoryType,
/// The version of the game data.
pub version: Option<String>,
2022-07-19 19:29:41 -04:00
}
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 {
// This ensures that the ordering of the repositories is always ffxiv, ex1, ex2 and so on.
match self.repo_type {
Base => Less,
Expansion { number } => {
let super_number = number;
match other.repo_type {
Base => Greater,
2022-08-16 11:52:07 -04:00
Expansion { number } => super_number.cmp(&number),
2022-07-19 19:29:41 -04:00
}
}
}
}
}
impl PartialOrd for Repository {
fn partial_cmp(&self, other: &Self) -> Option<Ordering> {
Some(self.cmp(other))
}
}
/// This refers to the specific root directory a file is located in.
/// This is a fixed list of directories, and all of them are known.
2022-09-15 16:26:31 -04:00
#[derive(Debug, PartialEq, Eq)]
2022-07-19 19:29:41 -04:00
pub enum Category {
/// Common files such as game fonts, and other data that doesn't really fit anywhere else.
Common,
/// Shared data between game maps.
BackgroundCommon,
2022-07-19 19:29:41 -04:00
/// Game map data such as models, textures, and so on.
Background,
2022-07-19 19:29:41 -04:00
/// Cutscene content such as animations.
Cutscene,
2022-07-19 19:29:41 -04:00
/// Character model files and more.
Character,
2022-07-19 19:29:41 -04:00
/// Compiled shaders used by the retail client.
Shader,
/// UI layouts and textures.
UI,
/// Sound effects, basically anything not under `Music`.
Sound,
/// This "VFX" means "visual effects", and contains textures and definitions for stuff like battle effects.
VFX,
/// A leftover from 1.0, where the UI was primarily driven by LUA scripts.
UIScript,
/// Excel data.
EXD,
/// Many game events are driven by LUA scripts, such as cutscenes.
GameScript,
/// Music!
Music,
/// Unknown purpose, most likely to test SqPack functionality.
SqPackTest,
2022-08-09 19:38:53 -04:00
/// Unknown purpose, most likely debug files.
2022-07-19 19:29:41 -04:00
Debug,
}
pub fn string_to_category(string: &str) -> Option<Category> {
use crate::repository::Category::*;
match string {
"common" => Some(Common),
2022-08-09 19:39:31 -04:00
"bgcommon" => Some(BackgroundCommon),
"bg" => Some(Background),
"cut" => Some(Cutscene),
"chara" => Some(Character),
2022-07-19 19:29:41 -04:00
"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),
2022-08-16 11:52:07 -04:00
_ => None,
2022-07-19 19:29:41 -04:00
}
}
impl Repository {
/// Creates a new `Repository`, from an existing repository directory. This may return `None` if
/// 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());
2022-08-16 11:50:18 -04:00
let repo_type = if name == "ffxiv" {
Base
2022-07-19 19:29:41 -04:00
} else {
2022-08-16 11:50:18 -04:00
Expansion {
2022-08-16 11:52:07 -04:00
number: name[2..3].parse().unwrap(),
2022-07-19 19:29:41 -04:00
}
2022-08-16 11:50:18 -04:00
};
2022-07-19 19:29:41 -04:00
2022-08-16 11:50:18 -04:00
let version = if repo_type == Base {
2022-07-19 19:29:41 -04:00
let mut d = PathBuf::from(dir);
d.pop();
d.pop();
d.push("ffxivgame.ver");
2022-08-16 11:50:18 -04:00
read_version(d.as_path())
2022-07-19 19:29:41 -04:00
} else {
let mut d = PathBuf::from(dir);
d.push(format!("{}.ver", name));
2022-08-16 11:50:18 -04:00
read_version(d.as_path())
};
2022-07-19 19:29:41 -04:00
Some(Repository {
name,
repo_type,
version,
2022-07-19 19:29:41 -04:00
})
}
fn expansion(&self) -> i32 {
match self.repo_type {
Base => 0,
2022-08-16 11:52:07 -04:00
Expansion { number } => number,
2022-07-19 19:29:41 -04:00
}
}
/// Calculate an index filename for a specific category, like _"0a0000.win32.index"_.
pub fn index_filename(&self, category: Category) -> String {
2022-08-16 11:52:07 -04:00
format!(
"{:02x}{:02}{:02}.{}.index",
category as i32,
self.expansion(),
0,
"win32"
)
2022-07-19 19:29:41 -04:00
}
/// Calculate a dat filename given a category and a data file id, returns something like _"0a0000.win32.dat0"_.
pub fn dat_filename(&self, category: Category, data_file_id: u32) -> String {
let expansion = self.expansion();
let chunk = 0;
let platform = "win32";
2022-08-16 11:52:07 -04:00
format!(
"{:02x}{expansion:02}{chunk:02}.{platform}.dat{data_file_id}",
category as u32
)
2022-07-19 19:29:41 -04:00
}
2022-08-16 11:52:07 -04:00
}