Skip to content

Commit

Permalink
SerializedFile - ObjectHandler - add peak_name function
Browse files Browse the repository at this point in the history
  • Loading branch information
K0lb3 committed Jun 21, 2023
1 parent 2b07f3d commit a822fcd
Show file tree
Hide file tree
Showing 2 changed files with 62 additions and 7 deletions.
53 changes: 46 additions & 7 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
# UnityRustEXtractor [![Build Status]][actions] [![Latest Version]][crates.io]

[Build Status]: https://img.shields.io/github/actions/workflow/status/UnityRustEXtractor/urex/ci.yml?branch=main
[actions]: https://github.com/UnityRustEXtractor/urex/actions?query=branch%3Amain
[Latest Version]: https://img.shields.io/crates/v/urex.svg
[crates.io]: https://crates.io/crates/urex


A work-in-progress extractor and patcher for Unity Engine asset files.
Currently it can do about nothing, so please check back later.

Expand All @@ -13,9 +13,10 @@ Currently it can do about nothing, so please check back later.
This projects uses following dependencies:

- Compression & Decompression:

- BundleFiles blocks:
- [lzma-rs](https://crates.io/crates/lzma-rs)
- [lz4_flex](lz4_flex)
- [lz4_flex](lz4_flex)
- WebFile:
- [libflate](https://crates.io/crates/libflate)
- [brotli](https://crates.io/crates/brotli)
Expand All @@ -29,23 +30,33 @@ This projects uses following dependencies:
### parsing a normal BundleFile and dumping its objects

```rust
use std::{
fs::{DirBuilder, File},
io::{Seek, Write},
path::Path,
};

use urex::files::{BundleFile, SerializedFile};
use urex::config::ExtractionConfig;


let mut reader = File::open(fp).unwrap();
let export_dir = Path::new("dump");

// parse the bundle file
let config = ExtractionConfig::new();
let mut bundle = BundleFile::from_reader(&mut reader, &config).unwrap();

// iterate over the files in the bundle
for directory in &bundle.m_DirectoryInfo {
// generate export dir for cab
let export_cab_dir = export_dir.join(&directory.path);
// seek to the start of the file in the bundle
bundle
.m_BlockReader
.seek(std::io::SeekFrom::Start(directory.offset as u64))
.unwrap();

// try to parse the file as a SerializedFile
match SerializedFile::from_reader(&mut bundle.m_BlockReader, &config) {
Ok(serialized) => {
Expand All @@ -54,10 +65,34 @@ for directory in &bundle.m_DirectoryInfo {
// get a helper object to parse the object
let mut handler =
serialized.get_object_handler(object, &mut bundle.m_BlockReader);

// try to get the name
let name = match handler.peak_name() {
Ok(name) => format!("{}_{}", object.m_PathID, name),
Err(_) => format!("{}", object.m_PathID),
};

// ensure that the parent directory exists
let dst_path = export_cab_dir.join(name);
DirBuilder::new()
.recursive(true)
.create(dst_path.parent().unwrap())
.unwrap_or_else(|_| panic!("Failed to create {:?}", dst_path.parent()));

// parse the object as json
println!("{:?}", handler.parse_as_json().unwrap());
let json = handler.parse_as_json().unwrap();
// println!("{:?}", json);
File::create(format!("{}.json", dst_path.to_string_lossy()))
.unwrap()
.write_all(json.to_string().as_bytes())
.unwrap();
// parse the object as yaml
println!("{:?}", handler.parse_as_yaml().unwrap().unwrap());
let yaml = handler.parse_as_yaml().unwrap().unwrap();
// println!("{:?}", yaml);
File::create(format!("{}.yaml", dst_path.to_string_lossy()))
.unwrap()
.write_all(serde_yaml::to_string(&yaml).unwrap().as_bytes())
.unwrap();
}
}
Err(e) => {
Expand All @@ -72,6 +107,7 @@ for directory in &bundle.m_DirectoryInfo {
```

### parsing a by UnityCN encrypted BundleFile and handling stripped Unity version

```rust
let mut reader = File::open(fp).unwrap();
let config = ExtractionConfig {
Expand All @@ -83,22 +119,24 @@ let bundle = crate::files::BundleFile::from_reader(&mut reader, &config).unwrap(

### reading a UnityCN encrypted BundleFile



## Notes

### TODO

- Parsers:

- [] WebFile
- [x] SerializedFile

- Object Classes:

- [] Generator
- [x] Parser
- [] Writer
- [] Export Functions

- Tests:

- [] Normal Tests
- [] Artificing Test Files
- [] 100% Coverage
Expand All @@ -109,6 +147,7 @@ let bundle = crate::files::BundleFile::from_reader(&mut reader, &config).unwrap(
## Getting Help

TODO:

- [] Docs
- [] GitHub Issues and Discussion
- [] Discord server
Expand Down
16 changes: 16 additions & 0 deletions src/files/serialzedfile.rs
Original file line number Diff line number Diff line change
Expand Up @@ -362,6 +362,22 @@ impl<'a, R: std::io::Read + std::io::Seek> ObjectHandler<'a, R> {
}
}

pub fn peak_name(&mut self) -> Result<String, std::io::Error> {
self.reader
.seek(std::io::SeekFrom::Start(self.info.m_Offset as u64))
.unwrap();

// todo - check against typeid
match self.file.m_Header.m_Endianess {
0 => self.reader.read_string::<LittleEndian>(),
1 => self.reader.read_string::<BigEndian>(),
_ => Err(std::io::Error::new(
std::io::ErrorKind::NotFound,
"Unknown endianess!",
)),
}
}

parse_as!(json, serde_json::Value);
parse_as!(yaml, Result<serde_yaml::Value, serde_yaml::Error>);
}
Expand Down

0 comments on commit a822fcd

Please sign in to comment.