1
Fork 0
mirror of https://github.com/redstrate/Physis.git synced 2025-06-07 23:27:45 +00:00

Move ZiPatch functions under its own struct

For code organization reasons
This commit is contained in:
Joshua Goins 2024-06-29 12:47:00 -04:00
parent 36d09195fa
commit 4357e382ab
3 changed files with 326 additions and 322 deletions

View file

@ -5,7 +5,7 @@ use std::fs;
use std::path::PathBuf; use std::path::PathBuf;
use tracing::warn; use tracing::warn;
use crate::patch::{apply_patch, PatchError}; use crate::patch::{PatchError, ZiPatch};
/// Represents the boot data for FFXIV, which is located under the "boot" directory. /// Represents the boot data for FFXIV, which is located under the "boot" directory.
pub struct BootData { pub struct BootData {
@ -43,7 +43,7 @@ impl BootData {
/// Applies the patch to boot data and returns any errors it encounters. This function will not update the version in the BootData struct. /// Applies the patch to boot data and returns any errors it encounters. This function will not update the version in the BootData struct.
pub fn apply_patch(&self, patch_path: &str) -> Result<(), PatchError> { pub fn apply_patch(&self, patch_path: &str) -> Result<(), PatchError> {
apply_patch(&self.path, patch_path) ZiPatch::apply(&self.path, patch_path)
} }
fn is_valid(path: &str) -> bool { fn is_valid(path: &str) -> bool {

View file

@ -14,7 +14,7 @@ use crate::exd::EXD;
use crate::exh::EXH; use crate::exh::EXH;
use crate::exl::EXL; use crate::exl::EXL;
use crate::index::{Index2File, IndexEntry, IndexFile}; use crate::index::{Index2File, IndexEntry, IndexFile};
use crate::patch::{apply_patch, PatchError}; use crate::patch::{PatchError, ZiPatch};
use crate::repository::{string_to_category, Category, Repository}; use crate::repository::{string_to_category, Category, Repository};
use crate::ByteBuffer; use crate::ByteBuffer;
@ -304,7 +304,7 @@ impl GameData {
/// Applies the patch to game data and returns any errors it encounters. This function will not update the version in the GameData struct. /// Applies the patch to game data and returns any errors it encounters. This function will not update the version in the GameData struct.
pub fn apply_patch(&self, patch_path: &str) -> Result<(), PatchError> { pub fn apply_patch(&self, patch_path: &str) -> Result<(), PatchError> {
apply_patch(&self.game_directory, patch_path) ZiPatch::apply(&self.game_directory, patch_path)
} }
/// Detects whether or not the game files need a repair, right now it only checks for invalid /// Detects whether or not the game files need a repair, right now it only checks for invalid

View file

@ -7,7 +7,7 @@ use std::fs::{File, OpenOptions, read, read_dir};
use std::io::{BufWriter, Cursor, Seek, SeekFrom, Write}; use std::io::{BufWriter, Cursor, Seek, SeekFrom, Write};
use std::path::{Path, PathBuf}; use std::path::{Path, PathBuf};
use binrw::{binread, binrw, BinWrite}; use binrw::{binrw, BinWrite};
use binrw::BinRead; use binrw::BinRead;
use tracing::{debug, warn}; use tracing::{debug, warn};
use crate::ByteBuffer; use crate::ByteBuffer;
@ -414,8 +414,32 @@ impl From<binrw::Error> for PatchError {
} }
} }
fn recurse(path: impl AsRef<Path>) -> Vec<PathBuf> {
let Ok(entries) = read_dir(path) else {
return vec![];
};
entries
.flatten()
.flat_map(|entry| {
let Ok(meta) = entry.metadata() else {
return vec![];
};
if meta.is_dir() {
return crate::patch::recurse(entry.path());
}
if meta.is_file() {
return vec![entry.path()];
}
vec![]
})
.collect()
}
pub struct ZiPatch;
impl ZiPatch {
/// Applies a boot or a game patch to the specified _data_dir_. /// Applies a boot or a game patch to the specified _data_dir_.
pub fn apply_patch(data_dir: &str, patch_path: &str) -> Result<(), PatchError> { pub fn apply(data_dir: &str, patch_path: &str) -> Result<(), PatchError> {
let mut file = File::open(patch_path)?; let mut file = File::open(patch_path)?;
PatchHeader::read(&mut file)?; PatchHeader::read(&mut file)?;
@ -664,29 +688,8 @@ pub fn apply_patch(data_dir: &str, patch_path: &str) -> Result<(), PatchError> {
} }
} }
fn recurse(path: impl AsRef<Path>) -> Vec<PathBuf> {
let Ok(entries) = read_dir(path) else {
return vec![];
};
entries
.flatten()
.flat_map(|entry| {
let Ok(meta) = entry.metadata() else {
return vec![];
};
if meta.is_dir() {
return recurse(entry.path());
}
if meta.is_file() {
return vec![entry.path()];
}
vec![]
})
.collect()
}
/// Creates a new ZiPatch describing the diff between `base_directory` and `new_directory`. /// Creates a new ZiPatch describing the diff between `base_directory` and `new_directory`.
pub fn create_patch(base_directory: &str, new_directory: &str) -> Option<ByteBuffer> { pub fn create(base_directory: &str, new_directory: &str) -> Option<ByteBuffer> {
let mut buffer = ByteBuffer::new(); let mut buffer = ByteBuffer::new();
{ {
@ -696,8 +699,8 @@ pub fn create_patch(base_directory: &str, new_directory: &str) -> Option<ByteBuf
let header = PatchHeader {}; let header = PatchHeader {};
header.write(&mut writer).ok()?; header.write(&mut writer).ok()?;
let base_files = recurse(base_directory); let base_files = crate::patch::recurse(base_directory);
let new_files = recurse(new_directory); let new_files = crate::patch::recurse(new_directory);
// A set of files not present in base, but in new (aka added files) // A set of files not present in base, but in new (aka added files)
let added_files: Vec<&PathBuf> = new_files.iter().filter(|item| !base_files.contains(item)).collect(); let added_files: Vec<&PathBuf> = new_files.iter().filter(|item| !base_files.contains(item)).collect();
@ -766,3 +769,4 @@ pub fn create_patch(base_directory: &str, new_directory: &str) -> Option<ByteBuf
Some(buffer) Some(buffer)
} }
}