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 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.
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.
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 {

View file

@ -14,7 +14,7 @@ use crate::exd::EXD;
use crate::exh::EXH;
use crate::exl::EXL;
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::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.
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

View file

@ -7,7 +7,7 @@ use std::fs::{File, OpenOptions, read, read_dir};
use std::io::{BufWriter, Cursor, Seek, SeekFrom, Write};
use std::path::{Path, PathBuf};
use binrw::{binread, binrw, BinWrite};
use binrw::{binrw, BinWrite};
use binrw::BinRead;
use tracing::{debug, warn};
use crate::ByteBuffer;
@ -414,8 +414,32 @@ impl From<binrw::Error> for PatchError {
}
}
/// Applies a boot or a game patch to the specified _data_dir_.
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 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_.
pub fn apply(data_dir: &str, patch_path: &str) -> Result<(), PatchError> {
let mut file = File::open(patch_path)?;
PatchHeader::read(&mut file)?;
@ -662,31 +686,10 @@ 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`.
pub fn create_patch(base_directory: &str, new_directory: &str) -> Option<ByteBuffer> {
/// Creates a new ZiPatch describing the diff between `base_directory` and `new_directory`.
pub fn create(base_directory: &str, new_directory: &str) -> Option<ByteBuffer> {
let mut buffer = ByteBuffer::new();
{
@ -696,8 +699,8 @@ pub fn create_patch(base_directory: &str, new_directory: &str) -> Option<ByteBuf
let header = PatchHeader {};
header.write(&mut writer).ok()?;
let base_files = recurse(base_directory);
let new_files = recurse(new_directory);
let base_files = crate::patch::recurse(base_directory);
let new_files = crate::patch::recurse(new_directory);
// 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();
@ -765,4 +768,5 @@ pub fn create_patch(base_directory: &str, new_directory: &str) -> Option<ByteBuf
}
Some(buffer)
}
}