Skip to content

Commit

Permalink
fixed writing to support weird unity byte alignment
Browse files Browse the repository at this point in the history
  • Loading branch information
ByteZ1337 committed Apr 23, 2024
1 parent 260e930 commit aab229a
Show file tree
Hide file tree
Showing 2 changed files with 13 additions and 45 deletions.
42 changes: 4 additions & 38 deletions src/command/pack.rs
Original file line number Diff line number Diff line change
Expand Up @@ -66,25 +66,20 @@ fn pack_dat(art_key: &String, input: &PathBuf, output: &PathBuf) -> anyhow::Resu
let mut assets: Vec<AssetMetadata> = Vec::new();
let mut asset_bytes: Vec<u8> = Vec::new();

// let localized_assets = match locale_mode {
// I18nCompatMode::None => Vec::new(),
// _ => find_localized_assets(&args.game).unwrap_or_else(|e| {
// eprintln!("Error while finding localized assets: {}. Disabling localization mode.", e);
// Vec::new()
// }),
// };

for file in WalkDir::new(input) {
let file = file.unwrap();
if file.file_type().is_dir() {
continue;
}

let path = file.path();
let name = path.strip_prefix(input)?.to_str()
let mut name = path.strip_prefix(input)?.to_str()
.context("Failed to convert path to string")?
.to_string();
let size = path.metadata()?.len() as usize;
if !name.starts_with("assets/") {
name = format!("assets/{}", name);
}

println!("Packing asset: {} ({} bytes)", name, size);
assets.push(AssetMetadata { name, size });
Expand All @@ -110,32 +105,3 @@ fn pack_dat(art_key: &String, input: &PathBuf, output: &PathBuf) -> anyhow::Resu

Ok(())
}

// fn find_localized_assets(game_dir: &PathBuf) -> anyhow::Result<Vec<String>> {
// let english_loc = game_dir
// .join("PapersPlease_Data")
// .join("StreamingAssets")
// .join("loc")
// .join("en.zip");
//
// if !english_loc.exists() {
// anyhow::bail!("English localization not found at: {}", english_loc.display());
// }
// let mut assets: Vec<String> = Vec::new();
// let zip_handle = File::open(english_loc)?;
// let mut zip = ZipArchive::new(zip_handle)?;
// for i in 0..zip.len() {
// let file = zip.by_index(i)?;
// if file.is_dir() {
// continue;
// }
//
// // TODO: bail or skip?
// assets.push(format!("assets/{}", file
// .enclosed_name().ok_or_else(|| anyhow::anyhow!("Failed to get zip entry name"))?
// .to_str().ok_or_else(|| anyhow::anyhow!("Failed to convert file name to string"))?
// ));
// }
//
// Ok(assets)
// }
16 changes: 9 additions & 7 deletions src/command/patch/assets_patcher.rs
Original file line number Diff line number Diff line change
Expand Up @@ -143,10 +143,6 @@ fn pack_to_assets(temp_dir: &PathBuf, patched: &PathBuf, repack: RepackInfo) ->
let mut objects = Vec::new();
let mut current_offset = 0;
for obj in &assets.content.objects {
if current_offset % 4 != 0 {
current_offset += 4 - (current_offset % 4);
}

let mut new_object = ObjectInfo {
path_id: obj.path_id,
byte_start: current_offset,
Expand All @@ -159,6 +155,14 @@ fn pack_to_assets(temp_dir: &PathBuf, patched: &PathBuf, repack: RepackInfo) ->
new_object.byte_size = obj.byte_size;
}
current_offset += new_object.byte_size as u64;
if current_offset % 8 != 0 {
let padding = 8 - (current_offset % 8);
if padding > 4 {
new_object.byte_size += (padding % 4) as u32;
}
current_offset += padding;
}

objects.push(new_object);
}
header.file_size = header.offset_first_file + current_offset;
Expand All @@ -179,7 +183,6 @@ fn pack_to_assets(temp_dir: &PathBuf, patched: &PathBuf, repack: RepackInfo) ->
.context("Failed to open original assets file")?);
let original_file_offset = &assets.header.offset_first_file;
for (obj, old_obj) in new_assets.content.objects.iter().zip(assets.content.objects) {

let pos = writer.stream_position()
.context("Failed to get current position in output file")?;
if pos != obj.byte_start + original_file_offset {
Expand Down Expand Up @@ -208,9 +211,8 @@ fn pack_to_assets(temp_dir: &PathBuf, patched: &PathBuf, repack: RepackInfo) ->
std::io::copy(&mut art_file, &mut writer)
.context("Failed to copy new art file to assets file")?;
}

}

println!("Packed objets to: {}", output.display());
println!("Packed objects to: {}", output.display());
Ok(output)
}

0 comments on commit aab229a

Please sign in to comment.