Skip to content

Commit

Permalink
actually replace the game files
Browse files Browse the repository at this point in the history
  • Loading branch information
ByteZ1337 committed Apr 23, 2024
1 parent ec4016d commit 0fe2a6c
Show file tree
Hide file tree
Showing 2 changed files with 15 additions and 12 deletions.
21 changes: 12 additions & 9 deletions src/command/patch/assets_patcher.rs
Original file line number Diff line number Diff line change
Expand Up @@ -26,19 +26,20 @@ const ART_OBJ_HEADER_LEN: u64 = 4 + 7 + 1 + 4;

pub fn patch_assets(
patch: &PathBuf,
unpacked: &PathBuf,
temp_dir: &PathBuf,
game_dir: &PathBuf,
repack_info: RepackInfo,
) -> anyhow::Result<PathBuf> {
) -> anyhow::Result<()> {
println!("Patching assets..");
let patched_assets = temp_dir.join("patched");
let unpacked = temp_dir.join("unpacked");
std::fs::create_dir_all(&patched_assets)
.context("Failed to create patched assets directory")?;

// copy over original files and if they have a patch, apply the patch
for file in WalkDir::new(unpacked) {
for file in WalkDir::new(&unpacked) {
let file = file.map_err(|e| anyhow::anyhow!("Failed to walk directory: {}", e))?;
let rel_path = file.path().strip_prefix(unpacked)
let rel_path = file.path().strip_prefix(&unpacked)
.context("Failed to strip prefix")?;
let file_type = file.file_type();

Expand Down Expand Up @@ -103,7 +104,7 @@ pub fn patch_assets(
}
}

pack_to_assets(temp_dir, &patched_assets, repack_info)
pack_to_assets(temp_dir, &game_dir, repack_info)
}

/// Copies a file from one of the input directories to the patched assets directory and makes sure
Expand All @@ -127,8 +128,9 @@ fn patch_xml(original: &Path, patch_file: &PathBuf, rel_path: &Path, patched_ass
xml_patcher::patch(original, patch_file, &output)
}

fn pack_to_assets(temp_dir: &PathBuf, patched: &PathBuf, repack: RepackInfo) -> anyhow::Result<PathBuf> {
let output = temp_dir.join("repacked.assets");
fn pack_to_assets(temp_dir: &PathBuf, game_dir: &PathBuf, repack: RepackInfo) -> anyhow::Result<()> {
let output = game_dir.join("sharedassets0.assets");
let patched = temp_dir.join("patched");
let temp_art = temp_dir.join("patched-art.dat");
pack::pack(&repack.art_key, &Some(patched.clone()), &temp_art)?;
let assets = repack.assets;
Expand Down Expand Up @@ -174,13 +176,14 @@ fn pack_to_assets(temp_dir: &PathBuf, patched: &PathBuf, repack: RepackInfo) ->
header.file_size = header.offset_first_file + current_offset;
let content = AssetsFileContent { objects, ..assets.content };
let new_assets = AssetsFile { header, content };

let mut writer = BufWriter::new(File::create(&output)
.context("Failed to create output file")?);
new_assets.write(&mut writer)
.context("Failed to write assets file header")?;

// pad with zeroes until first file offset is reached (yes this is also what Unity does)
let pad = assets.header.offset_first_file - writer.seek(SeekFrom::Current(0))
let pad = assets.header.offset_first_file - writer.stream_position()
.context("Failed to get current position in output file")?;
write_zeroes(&mut writer, pad)?;

Expand Down Expand Up @@ -220,5 +223,5 @@ fn pack_to_assets(temp_dir: &PathBuf, patched: &PathBuf, repack: RepackInfo) ->
}

println!("Packed objects to: {}", output.display());
Ok(output)
Ok(())
}
6 changes: 3 additions & 3 deletions src/command/patch/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -25,17 +25,17 @@ pub fn patch(args: &NewArgs, patch: &PathBuf, locale_mode: &I18nCompatMode) -> a
let temp_unpacked = temp_dir.join("unpacked");
std::fs::create_dir_all(&temp_unpacked)
.context("Failed to create temp directory")?;

let repack_info = unpack::unpack_assets(args, &game_files.assets, &temp_unpacked)?;

patch_assets(patch, &temp_unpacked, &temp_dir, repack_info)?;
patch_assets(patch, &temp_dir, &game_files.game_dir, repack_info)?;

Ok(())
}


//<editor-fold desc="Filesystem preparations" defaultstate="collapsed">
pub struct GameFiles {
pub game_dir: PathBuf,
pub assets: PathBuf,
pub resources: PathBuf,
pub locale: PathBuf,
Expand All @@ -57,7 +57,7 @@ fn prepare_game_files(game_dir: &PathBuf) -> anyhow::Result<GameFiles> {
let resources = prepare_file(&game_dir, "sharedassets0.resource")?;
let locale = prepare_file(&game_dir, "StreamingAssets/loc/en.zip")?;

Ok(GameFiles { assets, resources, locale })
Ok(GameFiles { game_dir, assets, resources, locale })
}

fn prepare_file(game_dir: &PathBuf, name: &str) -> anyhow::Result<PathBuf> {
Expand Down

0 comments on commit 0fe2a6c

Please sign in to comment.