Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Unity fixes + decrunching #744

Merged
merged 12 commits into from
Jan 20, 2025
78 changes: 44 additions & 34 deletions rust/Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

3 changes: 2 additions & 1 deletion rust/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -35,9 +35,10 @@ lz4_flex = { version = "0.10.0", default-features = false, features = ["safe-dec
lzma-rs = { version = "0.3.0", features = ["raw_decoder"] }
naga = { git = "https://github.com/magcius/wgpu", branch = "issue-4349", features = ["glsl-in", "wgsl-out"] }
num_enum = "0.5.7"
wasm-bindgen = "0.2.95"
wasm-bindgen = "=0.2.95"
web-sys = { version = "0.3.48", features = ["console"] }
nalgebra-glm = "0.19.0"
rand = "0.8.5"
getrandom = { version = "0.2.15", features = ["js"] }
noclip-macros = { version = "*", path = "./noclip-macros" }
texture2ddecoder = { git = "https://github.com/wgreenberg/texture2ddecoder" }
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Interesting, I might want to replace https://github.com/magcius/noclip.website/blob/main/src/Common/bc_texture.ts with this eventually, possibly.

24 changes: 24 additions & 0 deletions rust/src/compression.rs
Original file line number Diff line number Diff line change
Expand Up @@ -38,3 +38,27 @@ pub fn deflate_raw_decompress(src: &[u8]) -> Vec<u8> {
inflate::inflate_bytes(src).unwrap()
}

#[wasm_bindgen(js_name = "CrunchTexture")]
pub struct CrunchTexture {
handle: texture2ddecoder::CrunchHandle,
}

#[wasm_bindgen(js_class = "CrunchTexture")]
impl CrunchTexture {
pub fn new(data: &[u8]) -> Result<Self, String> {
let handle = texture2ddecoder::CrunchHandle::new(data)
.map_err(|err| format!("{:?}", err))?;
Ok(Self {
handle,
})
}

pub fn get_num_levels(&self) -> u32 {
self.handle.get_num_levels()
}

pub fn decode_level(&self, data: &[u8], level_index: u32) -> Result<Vec<u8>, String> {
self.handle.unpack_level(data, level_index)
.map_err(|err| err.into())
}
}
5 changes: 3 additions & 2 deletions rust/src/unity/asset_file.rs
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,8 @@ impl AssetFile {
pub fn append_metadata_chunk(&mut self, data: &[u8]) -> Result<(), String> {
// data will be the file from bytes 0..data_offset, so skip to where the metadata starts
let bitslice = BitSlice::from_slice(data);
let (rest, _) = SerializedFileHeader::read(&bitslice, ()).unwrap();
let (rest, _) = SerializedFileHeader::read(&bitslice, ())
.map_err(|err| format!("failed to parse metadata file header: {:?}", err))?;
match SerializedFileMetadata::read(rest, self.header.version) {
Ok((_, metadata)) => self.metadata = Some(metadata),
Err(err) => return Err(format!("failed to parse metadata: {:?}", err)),
Expand Down Expand Up @@ -77,7 +78,7 @@ impl AssetFile {
ClassID::MonoBehavior
};
result.push(AssetFileObject {
file_id:obj.file_id,
file_id: obj.file_id,
byte_start,
byte_size: obj.byte_size as usize,
class_id,
Expand Down
Loading
Loading