2023-08-06 08:25:04 -04:00
|
|
|
// SPDX-FileCopyrightText: 2023 Joshua Goins <josh@redstrate.com>
|
|
|
|
// SPDX-License-Identifier: GPL-3.0-or-later
|
|
|
|
|
2022-07-19 19:29:41 -04:00
|
|
|
use std::fs;
|
|
|
|
use std::path::PathBuf;
|
2023-11-10 17:25:53 -05:00
|
|
|
use tracing::warn;
|
2022-07-19 19:29:41 -04:00
|
|
|
|
2023-08-06 08:25:04 -04:00
|
|
|
use crate::patch::{apply_patch, PatchError};
|
|
|
|
|
2023-09-22 17:56:33 -04:00
|
|
|
/// Represents the boot data for FFXIV, which is located under the "boot" directory.
|
2022-07-19 19:29:41 -04:00
|
|
|
pub struct BootData {
|
2022-08-16 11:52:07 -04:00
|
|
|
path: String,
|
2022-08-09 21:51:52 -04:00
|
|
|
|
2023-09-22 17:56:33 -04:00
|
|
|
/// The current version of the boot data, e.g. "2012.01.01.0000.0000".
|
2022-08-16 11:52:07 -04:00
|
|
|
pub version: String,
|
2022-07-19 19:29:41 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
impl BootData {
|
|
|
|
/// Reads from an existing boot data location.
|
|
|
|
///
|
|
|
|
/// This will return _None_ if the boot directory is not valid, but it does not check the validity
|
|
|
|
/// of each individual file.
|
|
|
|
///
|
|
|
|
/// # Example
|
|
|
|
///
|
|
|
|
/// ```
|
|
|
|
/// # use physis::bootdata::BootData;
|
|
|
|
/// let boot = BootData::from_existing("SquareEnix/Final Fantasy XIV - A Realm Reborn/boot");
|
|
|
|
/// # assert!(boot.is_none())
|
|
|
|
/// ```
|
|
|
|
pub fn from_existing(directory: &str) -> Option<BootData> {
|
2024-04-15 18:00:42 -04:00
|
|
|
match Self::is_valid(directory) {
|
2022-07-19 19:29:41 -04:00
|
|
|
true => Some(BootData {
|
2024-04-15 18:02:17 -04:00
|
|
|
path: directory.parse().ok()?,
|
|
|
|
version: fs::read_to_string(format!("{directory}/ffxivboot.ver")).ok()?,
|
2022-07-19 19:29:41 -04:00
|
|
|
}),
|
|
|
|
false => {
|
2023-11-10 17:25:53 -05:00
|
|
|
warn!("Boot data is not valid!");
|
2022-07-19 19:29:41 -04:00
|
|
|
None
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2022-08-09 21:51:52 -04:00
|
|
|
|
2023-09-22 17:56:33 -04:00
|
|
|
/// Applies the patch to boot data and returns any errors it encounters. This function will not update the version in the BootData struct.
|
2022-08-16 11:52:07 -04:00
|
|
|
pub fn apply_patch(&self, patch_path: &str) -> Result<(), PatchError> {
|
2022-08-09 21:53:20 -04:00
|
|
|
apply_patch(&self.path, patch_path)
|
2022-08-09 21:51:52 -04:00
|
|
|
}
|
2024-04-15 18:00:42 -04:00
|
|
|
|
|
|
|
fn is_valid(path: &str) -> bool {
|
|
|
|
let d = PathBuf::from(path);
|
|
|
|
|
|
|
|
if fs::metadata(d.as_path()).is_err() {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
true
|
|
|
|
}
|
2022-08-16 11:52:07 -04:00
|
|
|
}
|
2024-04-15 18:07:06 -04:00
|
|
|
|
|
|
|
#[cfg(test)]
|
|
|
|
mod tests {
|
|
|
|
use super::*;
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn test_valid_boot_dir() {
|
|
|
|
let mut d = PathBuf::from(env!("CARGO_MANIFEST_DIR"));
|
|
|
|
d.push("resources/tests");
|
|
|
|
d.push("valid_boot");
|
|
|
|
|
|
|
|
assert!(BootData::from_existing(d.as_path().to_str().unwrap()).is_some());
|
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn test_invalid_boot_dir() {
|
|
|
|
let mut d = PathBuf::from(env!("CARGO_MANIFEST_DIR"));
|
|
|
|
d.push("resources/tests");
|
|
|
|
d.push("invalid_boot"); // intentionally missing so it doesn't have a .ver
|
|
|
|
|
|
|
|
assert!(BootData::from_existing(d.as_path().to_str().unwrap()).is_none());
|
|
|
|
}
|
|
|
|
}
|