Skip to content

Commit aab229a

Browse files
committed
fixed writing to support weird unity byte alignment
1 parent 260e930 commit aab229a

File tree

2 files changed

+13
-45
lines changed

2 files changed

+13
-45
lines changed

src/command/pack.rs

+4-38
Original file line numberDiff line numberDiff line change
@@ -66,25 +66,20 @@ fn pack_dat(art_key: &String, input: &PathBuf, output: &PathBuf) -> anyhow::Resu
6666
let mut assets: Vec<AssetMetadata> = Vec::new();
6767
let mut asset_bytes: Vec<u8> = Vec::new();
6868

69-
// let localized_assets = match locale_mode {
70-
// I18nCompatMode::None => Vec::new(),
71-
// _ => find_localized_assets(&args.game).unwrap_or_else(|e| {
72-
// eprintln!("Error while finding localized assets: {}. Disabling localization mode.", e);
73-
// Vec::new()
74-
// }),
75-
// };
76-
7769
for file in WalkDir::new(input) {
7870
let file = file.unwrap();
7971
if file.file_type().is_dir() {
8072
continue;
8173
}
8274

8375
let path = file.path();
84-
let name = path.strip_prefix(input)?.to_str()
76+
let mut name = path.strip_prefix(input)?.to_str()
8577
.context("Failed to convert path to string")?
8678
.to_string();
8779
let size = path.metadata()?.len() as usize;
80+
if !name.starts_with("assets/") {
81+
name = format!("assets/{}", name);
82+
}
8883

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

111106
Ok(())
112107
}
113-
114-
// fn find_localized_assets(game_dir: &PathBuf) -> anyhow::Result<Vec<String>> {
115-
// let english_loc = game_dir
116-
// .join("PapersPlease_Data")
117-
// .join("StreamingAssets")
118-
// .join("loc")
119-
// .join("en.zip");
120-
//
121-
// if !english_loc.exists() {
122-
// anyhow::bail!("English localization not found at: {}", english_loc.display());
123-
// }
124-
// let mut assets: Vec<String> = Vec::new();
125-
// let zip_handle = File::open(english_loc)?;
126-
// let mut zip = ZipArchive::new(zip_handle)?;
127-
// for i in 0..zip.len() {
128-
// let file = zip.by_index(i)?;
129-
// if file.is_dir() {
130-
// continue;
131-
// }
132-
//
133-
// // TODO: bail or skip?
134-
// assets.push(format!("assets/{}", file
135-
// .enclosed_name().ok_or_else(|| anyhow::anyhow!("Failed to get zip entry name"))?
136-
// .to_str().ok_or_else(|| anyhow::anyhow!("Failed to convert file name to string"))?
137-
// ));
138-
// }
139-
//
140-
// Ok(assets)
141-
// }

src/command/patch/assets_patcher.rs

+9-7
Original file line numberDiff line numberDiff line change
@@ -143,10 +143,6 @@ fn pack_to_assets(temp_dir: &PathBuf, patched: &PathBuf, repack: RepackInfo) ->
143143
let mut objects = Vec::new();
144144
let mut current_offset = 0;
145145
for obj in &assets.content.objects {
146-
if current_offset % 4 != 0 {
147-
current_offset += 4 - (current_offset % 4);
148-
}
149-
150146
let mut new_object = ObjectInfo {
151147
path_id: obj.path_id,
152148
byte_start: current_offset,
@@ -159,6 +155,14 @@ fn pack_to_assets(temp_dir: &PathBuf, patched: &PathBuf, repack: RepackInfo) ->
159155
new_object.byte_size = obj.byte_size;
160156
}
161157
current_offset += new_object.byte_size as u64;
158+
if current_offset % 8 != 0 {
159+
let padding = 8 - (current_offset % 8);
160+
if padding > 4 {
161+
new_object.byte_size += (padding % 4) as u32;
162+
}
163+
current_offset += padding;
164+
}
165+
162166
objects.push(new_object);
163167
}
164168
header.file_size = header.offset_first_file + current_offset;
@@ -179,7 +183,6 @@ fn pack_to_assets(temp_dir: &PathBuf, patched: &PathBuf, repack: RepackInfo) ->
179183
.context("Failed to open original assets file")?);
180184
let original_file_offset = &assets.header.offset_first_file;
181185
for (obj, old_obj) in new_assets.content.objects.iter().zip(assets.content.objects) {
182-
183186
let pos = writer.stream_position()
184187
.context("Failed to get current position in output file")?;
185188
if pos != obj.byte_start + original_file_offset {
@@ -208,9 +211,8 @@ fn pack_to_assets(temp_dir: &PathBuf, patched: &PathBuf, repack: RepackInfo) ->
208211
std::io::copy(&mut art_file, &mut writer)
209212
.context("Failed to copy new art file to assets file")?;
210213
}
211-
212214
}
213215

214-
println!("Packed objets to: {}", output.display());
216+
println!("Packed objects to: {}", output.display());
215217
Ok(output)
216218
}

0 commit comments

Comments
 (0)