Skip to content
This repository has been archived by the owner on Apr 21, 2023. It is now read-only.

Commit

Permalink
Update roead
Browse files Browse the repository at this point in the history
  • Loading branch information
Caleb Smith committed Jun 27, 2022
1 parent e47e7bc commit 1419c7e
Show file tree
Hide file tree
Showing 3 changed files with 80 additions and 76 deletions.
2 changes: 1 addition & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ once_cell = "1.12.0"
path-slash = "0.1.4"
pyo3 = { version = "0.16.5", features = ["extension-module", "anyhow"] }
rayon = "1.5.1"
roead = "<0.10.4"
roead = "0.11.0"
rstb = "0.3.2"
serde = { version = "1.0.130", features = ["derive"] }
serde_json = "1.0.71"
Expand Down
7 changes: 3 additions & 4 deletions src/mergers/pack.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ use rayon::prelude::*;
use roead::{
sarc::{Sarc, SarcWriter},
yaz0::compress,
Endian,
Bytes, Endian,
};
use std::{
collections::{HashMap, HashSet},
Expand All @@ -32,7 +32,7 @@ pub fn packs_mod(py: Python, parent: &PyModule) -> PyResult<()> {
Ok(())
}

fn merge_sarc(sarcs: Vec<Sarc>, endian: Endian) -> Result<Vec<u8>> {
fn merge_sarc(sarcs: Vec<Sarc>, endian: Endian) -> Result<Bytes> {
let all_files: HashSet<String> = sarcs
.iter()
.flat_map(|s| {
Expand All @@ -56,7 +56,6 @@ fn merge_sarc(sarcs: Vec<Sarc>, endian: Endian) -> Result<Vec<u8>> {
None
}
});
// println!("data");
data
})
.find(|d| util::is_file_modded(&file.cow_replace(".s", "."), d))
Expand Down Expand Up @@ -103,7 +102,7 @@ fn merge_sarc(sarcs: Vec<Sarc>, endian: Endian) -> Result<Vec<u8>> {
merged = compress(&merged);
}

Ok((file, merged))
Ok((file, merged.as_slice().into()))
} else {
Ok((file, data.to_vec()))
}
Expand Down
147 changes: 76 additions & 71 deletions src/mergers/texts.rs
Original file line number Diff line number Diff line change
Expand Up @@ -27,77 +27,80 @@ pub fn diff_language(
stock_bootup_path: String,
only_new_keys: bool,
) -> PyResult<PyObject> {
let diff = py.allow_threads(|| -> Result<IndexMap<String, Diff>> {
let language = &Path::new(&mod_bootup_path)
.file_stem()
.unwrap()
.to_str()
.unwrap()[7..];
let mod_bootup = Sarc::read(std::fs::read(&mod_bootup_path)?)?;
let stock_bootup = Sarc::read(std::fs::read(&stock_bootup_path)?)?;
let message_path = format!("Message/Msg_{}.product.ssarc", &language);
let mod_message = Sarc::read(decompress(
mod_bootup
.get_file_data(&message_path)
.with_context(|| jstr!("{&message_path} missing from Bootup_{language}.pack"))?,
)?)?;
let stock_message = Sarc::read(decompress(
stock_bootup
.get_file_data(&message_path)
.with_context(|| jstr!("{&message_path} missing from Bootup_{language}.pack"))?,
)?)?;
let diffs = mod_message
.files()
.filter(|file| {
file.name()
.map(|name| name.ends_with("msbt"))
.unwrap_or(false)
})
.collect::<Vec<_>>()
.into_par_iter()
.map(|file| -> Result<Option<(String, Diff)>> {
if let Some(path) = file.name().map(std::borrow::ToOwned::to_owned) {
let mod_text = Msyt::from_msbt_bytes(file.data())
.with_context(|| jstr!("Invalid MSBT file: {&path}"))?;
if let Some(stock_text) = stock_message
.get_file_data(&path)
.and_then(|data| Msyt::from_msbt_bytes(data).ok())
{
if mod_text == stock_text {
Ok(None)
} else {
let diffs: Diff = mod_text
.entries
.iter()
.filter(|(e, t)| {
if only_new_keys {
!stock_text.entries.contains_key(*e)
} else {
!stock_text.entries.contains_key(*e)
|| *t != stock_text.entries.get(*e).unwrap()
}
})
.map(|(e, t)| (e.to_owned(), t.clone()))
.collect();
if diffs.is_empty() {
let diff =
py.allow_threads(|| -> Result<IndexMap<String, Diff>> {
let language = &Path::new(&mod_bootup_path)
.file_stem()
.unwrap()
.to_str()
.unwrap()[7..];
let mod_bootup = Sarc::read(std::fs::read(&mod_bootup_path)?)?;
let stock_bootup = Sarc::read(std::fs::read(&stock_bootup_path)?)?;
let message_path = format!("Message/Msg_{}.product.ssarc", &language);
let mod_message = Sarc::read(
decompress(mod_bootup.get_file_data(&message_path).with_context(|| {
jstr!("{&message_path} missing from Bootup_{language}.pack")
})?)?
.to_vec(),
)?;
let stock_message = Sarc::read(
decompress(stock_bootup.get_file_data(&message_path).with_context(|| {
jstr!("{&message_path} missing from Bootup_{language}.pack")
})?)?
.to_vec(),
)?;
let diffs = mod_message
.files()
.filter(|file| {
file.name()
.map(|name| name.ends_with("msbt"))
.unwrap_or(false)
})
.collect::<Vec<_>>()
.into_par_iter()
.map(|file| -> Result<Option<(String, Diff)>> {
if let Some(path) = file.name().map(std::borrow::ToOwned::to_owned) {
let mod_text = Msyt::from_msbt_bytes(file.data())
.with_context(|| jstr!("Invalid MSBT file: {&path}"))?;
if let Some(stock_text) = stock_message
.get_file_data(&path)
.and_then(|data| Msyt::from_msbt_bytes(data).ok())
{
if mod_text == stock_text {
Ok(None)
} else {
Ok(Some((path.replace("msbt", "msyt"), diffs)))
let diffs: Diff = mod_text
.entries
.iter()
.filter(|(e, t)| {
if only_new_keys {
!stock_text.entries.contains_key(*e)
} else {
!stock_text.entries.contains_key(*e)
|| *t != stock_text.entries.get(*e).unwrap()
}
})
.map(|(e, t)| (e.to_owned(), t.clone()))
.collect();
if diffs.is_empty() {
Ok(None)
} else {
Ok(Some((path.replace("msbt", "msyt"), diffs)))
}
}
} else {
Ok(Some((path.replace("msbt", "msyt"), mod_text.entries)))
}
} else {
Ok(Some((path.replace("msbt", "msyt"), mod_text.entries)))
Ok(None)
}
} else {
Ok(None)
}
})
.collect::<Result<Vec<Option<(String, Diff)>>>>()?
.into_iter()
.flatten()
.collect();
Ok(diffs)
})?;
})
.collect::<Result<Vec<Option<(String, Diff)>>>>()?
.into_iter()
.flatten()
.collect();
Ok(diffs)
})?;
let diff_text = serde_json::to_string(&diff).unwrap();
let json = PyModule::import(py, "json")?;
#[allow(deprecated)]
Expand Down Expand Up @@ -128,11 +131,13 @@ pub fn merge_language(
.unwrap()[7..];
let stock_bootup = Sarc::read(std::fs::read(&stock_bootup_path)?)?;
let message_path = format!("Message/Msg_{}.product.ssarc", &language);
let stock_message = Sarc::read(decompress(
stock_bootup
.get_file_data(&message_path)
.with_context(|| jstr!("{&message_path} missing from Bootup_{language}.pack"))?,
)?)?;
let stock_message =
Sarc::read(
decompress(stock_bootup.get_file_data(&message_path).with_context(|| {
jstr!("{&message_path} missing from Bootup_{language}.pack")
})?)?
.to_vec(),
)?;
let mut new_message = SarcWriter::from(&stock_message);
let merged_files = diffs
.into_par_iter()
Expand Down Expand Up @@ -163,7 +168,7 @@ pub fn merge_language(
} else {
roead::Endian::Little
});
new_bootup.add_file(&message_path, compress(new_message.to_binary()));
new_bootup.add_file(&message_path, compress(new_message.to_binary()).as_slice());
std::fs::create_dir_all(&dest_bootup_path[..dest_bootup_path.len() - 17])?;
std::fs::write(&dest_bootup_path, new_bootup.to_binary())?;
Ok(())
Expand Down

0 comments on commit 1419c7e

Please sign in to comment.