aboutsummaryrefslogtreecommitdiff
path: root/src/unityfs
diff options
context:
space:
mode:
authormetamuffin <metamuffin@disroot.org>2025-03-15 15:24:25 +0100
committermetamuffin <metamuffin@disroot.org>2025-03-15 15:24:25 +0100
commitca02789996b94db87cd84571edb42bbcd9a3a18b (patch)
tree0563898f43928e877ef59ad50ea2d392e554553d /src/unityfs
parentd836e24357b81496c61f3cc9195ba36758523578 (diff)
downloadunity-tools-ca02789996b94db87cd84571edb42bbcd9a3a18b.tar
unity-tools-ca02789996b94db87cd84571edb42bbcd9a3a18b.tar.bz2
unity-tools-ca02789996b94db87cd84571edb42bbcd9a3a18b.tar.zst
fix inefficient multi readers
Diffstat (limited to 'src/unityfs')
-rw-r--r--src/unityfs/mod.rs9
-rw-r--r--src/unityfs/multi_reader.rs10
2 files changed, 12 insertions, 7 deletions
diff --git a/src/unityfs/mod.rs b/src/unityfs/mod.rs
index bc7e3ec..16e4283 100644
--- a/src/unityfs/mod.rs
+++ b/src/unityfs/mod.rs
@@ -40,11 +40,10 @@ impl<T: Read + Seek> UnityFS<T> {
}
pub fn find_main_file(&self) -> Option<&NodeInfo> {
- self.header.nodes().iter().find(|n| {
- !n.name.ends_with(".resource")
- && !n.name.ends_with(".resS")
- && !n.name.ends_with(".sharedAssets")
- })
+ self.header
+ .nodes()
+ .iter()
+ .find(|n| n.name.split_once(".").is_none())
}
pub fn read<'a>(&'a self, node: &NodeInfo) -> Result<NodeReader<BlockReader<MultiReader<T>>>> {
diff --git a/src/unityfs/multi_reader.rs b/src/unityfs/multi_reader.rs
index 3de6cd5..64e1670 100644
--- a/src/unityfs/multi_reader.rs
+++ b/src/unityfs/multi_reader.rs
@@ -1,10 +1,11 @@
+use anyhow::Result;
+use log::trace;
use std::{
io::{Read, Seek, SeekFrom},
sync::{Arc, Mutex},
};
-use anyhow::Result;
-
+/// Splits an reader into many by seeking in between reads.
pub struct MultiReader<T> {
position: u64,
inner: Arc<Mutex<(u64, T)>>,
@@ -30,7 +31,12 @@ impl<T: Read + Seek> Read for MultiReader<T> {
fn read(&mut self, buf: &mut [u8]) -> std::io::Result<usize> {
let mut g = self.inner.lock().unwrap();
if g.0 != self.position {
+ trace!(
+ "seeking to match stream position ({} actual vs {} required)",
+ g.0, self.position
+ );
g.1.seek(SeekFrom::Start(self.position))?;
+ g.0 = self.position;
}
let size = g.1.read(buf)?;
g.0 += size as u64;