diff options
author | metamuffin <metamuffin@disroot.org> | 2025-03-15 21:31:40 +0100 |
---|---|---|
committer | metamuffin <metamuffin@disroot.org> | 2025-03-15 21:31:40 +0100 |
commit | ed6ed7a62217369544f3e31ef9a886f459f0c21b (patch) | |
tree | c36f0e344e00b32c563494f77dd4191dc55f8c94 /src | |
parent | ca02789996b94db87cd84571edb42bbcd9a3a18b (diff) | |
download | unity-tools-ed6ed7a62217369544f3e31ef9a886f459f0c21b.tar unity-tools-ed6ed7a62217369544f3e31ef9a886f459f0c21b.tar.bz2 unity-tools-ed6ed7a62217369544f3e31ef9a886f459f0c21b.tar.zst |
assetbundle struct to abstract over unityfs and serializedfile
Diffstat (limited to 'src')
-rw-r--r-- | src/assetbundle.rs | 56 | ||||
-rw-r--r-- | src/classes/pptr.rs | 19 | ||||
-rw-r--r-- | src/classes/streaminginfo.rs | 2 | ||||
-rw-r--r-- | src/lib.rs | 1 | ||||
-rw-r--r-- | src/scene/mod.rs | 1 | ||||
-rw-r--r-- | src/serialized_file.rs | 7 |
6 files changed, 74 insertions, 12 deletions
diff --git a/src/assetbundle.rs b/src/assetbundle.rs new file mode 100644 index 0000000..e10a8f3 --- /dev/null +++ b/src/assetbundle.rs @@ -0,0 +1,56 @@ +use crate::{ + classes::pptr::PPtr, + serialized_file::SerializedFile, + unityfs::{NodeReader, UnityFS, block_reader::BlockReader, multi_reader::MultiReader}, +}; +use anyhow::{Context, Result, anyhow}; +use std::{ + io::{Read, Seek}, + marker::PhantomData, +}; + +/// High-level wrapper around UnityFS, SerializedFile and all the classes. +pub struct AssetBundle<T> { + pub fs: UnityFS<T>, + pub(crate) main: SerializedFile<NodeReader<BlockReader<MultiReader<T>>>>, + pub(crate) shared_assets: Option<SerializedFile<NodeReader<BlockReader<MultiReader<T>>>>>, +} + +impl<T: Read + Seek> AssetBundle<T> { + pub fn open(inner: T) -> Result<Self> { + let fs = UnityFS::open(inner).context("opening UnityFS")?; + let main_ni = fs + .find_main_file() + .ok_or(anyhow!("AssetBundle seems to lack main file"))?; + let main = SerializedFile::read(fs.read(main_ni)?)?; + let shared_assets = if let Some(n) = main.find_fs_shared_assets(&fs) { + Some(SerializedFile::read(fs.read(&n)?)?) + } else { + None + }; + Ok(Self { + fs, + main, + shared_assets, + }) + } + + pub fn all_toplevel_of_class(&self, class_name: &str) -> impl Iterator<Item = PPtr> { + self.main + .all_objects_of_class(class_name) + .map(|o| (0, o)) + .chain( + self.shared_assets + .as_ref() + .map(|e| e.all_objects_of_class(class_name).map(|o| (1, o))) + .into_iter() + .flatten(), + ) + .map(|(fi, o)| PPtr { + class: class_name.to_owned(), + file_id: fi, + path_id: o.path_id, + _class: PhantomData, + }) + } +} diff --git a/src/classes/pptr.rs b/src/classes/pptr.rs index 9b54cbb..366e17c 100644 --- a/src/classes/pptr.rs +++ b/src/classes/pptr.rs @@ -1,6 +1,6 @@ use crate::{ + assetbundle::AssetBundle, object::{Value, parser::FromValue}, - serialized_file::SerializedFile, }; use anyhow::{Result, anyhow, bail}; use log::debug; @@ -13,7 +13,7 @@ use std::{ #[derive(Debug, Serialize)] pub struct PPtr<T = Value> { #[serde(skip, default)] - _class: PhantomData<T>, + pub(crate) _class: PhantomData<T>, pub class: String, pub file_id: i32, pub path_id: i64, @@ -54,27 +54,26 @@ impl<T: FromValue> PPtr<T> { pub fn is_null(&self) -> bool { self.path_id == 0 && self.file_id == 0 } - pub fn load( - &self, - file: &mut SerializedFile<impl Read + Seek>, - shared_assets: Option<&mut SerializedFile<impl Read + Seek>>, - ) -> Result<T> { + pub fn load(&self, bundle: &mut AssetBundle<impl Read + Seek>) -> Result<T> { debug!( "loading PPtr<{}> file_id={} path_id={}", self.class, self.file_id, self.path_id ); match self.file_id { 0 => { - let ob = file + let ob = bundle + .main .objects .iter() .find(|o| o.path_id == self.path_id) .ok_or(anyhow!("object with path id {} not found", self.path_id))? .to_owned(); - file.read_object(ob)?.parse() + bundle.main.read_object(ob)?.parse() } 1 => { - let file = shared_assets.unwrap(); + let file = bundle.shared_assets.as_mut().ok_or(anyhow!( + "shared assets referenced but not included in bundle" + ))?; let ob = file .objects .iter() diff --git a/src/classes/streaminginfo.rs b/src/classes/streaminginfo.rs index 21029f4..e308f1c 100644 --- a/src/classes/streaminginfo.rs +++ b/src/classes/streaminginfo.rs @@ -26,7 +26,7 @@ impl FromValue for StreamingInfo { } impl StreamingInfo { - pub fn read(&self, fs: &mut UnityFS<impl Read + Seek>) -> Result<Vec<u8>> { + pub fn read(&self, fs: &UnityFS<impl Read + Seek>) -> Result<Vec<u8>> { if !self.path.starts_with("archive:") { bail!("StreamingInfo path does not start on archive:") } @@ -5,3 +5,4 @@ pub mod helper; pub mod object; pub mod serialized_file; pub mod unityfs; +pub mod assetbundle; diff --git a/src/scene/mod.rs b/src/scene/mod.rs deleted file mode 100644 index 8b13789..0000000 --- a/src/scene/mod.rs +++ /dev/null @@ -1 +0,0 @@ - diff --git a/src/serialized_file.rs b/src/serialized_file.rs index a6514ee..aba72b9 100644 --- a/src/serialized_file.rs +++ b/src/serialized_file.rs @@ -345,6 +345,13 @@ impl<T: Read + Seek> SerializedFile<T> { .ok_or(anyhow!("type tree missing"))?; Value::read(typetree, self.endianness, &mut self.file) } + + pub fn all_objects_of_class(&self, class_name: &str) -> impl Iterator<Item = &ObjectInfo> { + self.objects.iter().filter(move |o| { + self.get_object_type_tree(&o) + .map_or(false, |t| t.type_string == class_name) + }) + } } impl TypeTreeNode { |