Skip to content

Commit b5b857d

Browse files
authored
new deku (#747)
* Update to deku v0.18.0 * rust: Fix bit-endianness issue with new-deku This was the cause of all of our WoW rendering failures with new-deku. Unfortunate we can't (yet?) use Deku for this part, but honestly I think the shift + mask is better than a loop anyway. * rust: Map seek errors to deku errors * rust: Fix Unity loading The deku::byte_offset trick we do for alignment requires reader.bits_read to be correct, as that's the number we pad by. In our PackedVec implementations, we read a u8, and then later skip over it, which double-counts from the perspective of reader.bits_read. Fix this by adjusting after we read it. * rust: Use std::io instead of deku::no_std_io * rust: Update polymorph
1 parent 4ed13d8 commit b5b857d

File tree

13 files changed

+361
-337
lines changed

13 files changed

+361
-337
lines changed

rust/Cargo.lock

Lines changed: 68 additions & 22 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

rust/Cargo.toml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,7 @@ opt-level = "s"
2525
[dependencies]
2626
byteorder = "1.4.3"
2727
console_error_panic_hook = "0.1.7"
28-
deku = { version = "0.16.0", features = ["logging"] }
28+
deku = { version = "0.18.1", features = ["logging"] }
2929
env_logger = "0.10.1"
3030
inflate = "0.4.5"
3131
js-sys = "0.3.60"

rust/src/unity/asset_file.rs

Lines changed: 9 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,7 @@
1-
use deku::bitvec::BitSlice;
2-
use deku::{DekuContainerRead, DekuRead};
1+
use std::io::Cursor;
2+
3+
use deku::reader::Reader;
4+
use deku::{DekuContainerRead, DekuReader};
35
use wasm_bindgen::prelude::*;
46

57
use crate::unity::types::wasm::WasmFriendlyPPtr;
@@ -39,11 +41,12 @@ impl AssetFile {
3941

4042
pub fn append_metadata_chunk(&mut self, data: &[u8]) -> Result<(), String> {
4143
// data will be the file from bytes 0..data_offset, so skip to where the metadata starts
42-
let bitslice = BitSlice::from_slice(data);
43-
let (rest, _) = SerializedFileHeader::read(&bitslice, ())
44+
let mut cursor = Cursor::new(data);
45+
let mut reader = Reader::new(&mut cursor);
46+
let _header = SerializedFileHeader::from_reader_with_ctx(&mut reader, ())
4447
.map_err(|err| format!("failed to parse metadata file header: {:?}", err))?;
45-
match SerializedFileMetadata::read(rest, self.header.version) {
46-
Ok((_, metadata)) => self.metadata = Some(metadata),
48+
match SerializedFileMetadata::from_reader_with_ctx(&mut reader, self.header.version) {
49+
Ok(metadata) => self.metadata = Some(metadata),
4750
Err(err) => return Err(format!("failed to parse metadata: {:?}", err)),
4851
}
4952
Ok(())

rust/src/unity/types/binary.rs

Lines changed: 14 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
12
use deku::prelude::*;
23

34
// https://github.com/AssetRipper/TypeTreeDumps/blob/main/StructsDump/release/2019.4.39f1.dump
@@ -153,14 +154,14 @@ pub struct Mesh {
153154
}
154155

155156
#[derive(DekuRead, Clone, Copy, Debug)]
156-
#[deku(type = "i32")]
157+
#[deku(id_type = "i32")]
157158
pub enum IndexFormat {
158159
UInt16 = 0,
159160
UInt32 = 1,
160161
}
161162

162163
#[derive(DekuRead, Clone, Copy, Debug)]
163-
#[deku(type = "u8")]
164+
#[deku(id_type = "u8")]
164165
pub enum MeshCompression {
165166
Off = 0,
166167
Low = 1,
@@ -220,21 +221,12 @@ pub struct ByteArray {
220221
pub data: Vec<u8>,
221222
}
222223

223-
impl<'a> DekuRead<'a> for ByteArray {
224-
fn read(
225-
input: &'a deku::bitvec::BitSlice<u8, deku::bitvec::Msb0>,
226-
ctx: (),
227-
) -> Result<(&'a deku::bitvec::BitSlice<u8, deku::bitvec::Msb0>, Self), DekuError>
228-
where Self: Sized {
229-
let (rest, count) = i32::read(input, ctx)?;
230-
let (data_bits, rest) = rest.split_at(count as usize * 8);
231-
let bytes = data_bits.domain().region().unwrap().1;
232-
Ok((
233-
rest,
234-
Self {
235-
data: bytes.to_vec(),
236-
},
237-
))
224+
impl<'a, Ctx> DekuReader<'a, Ctx> for ByteArray where Ctx: Copy {
225+
fn from_reader_with_ctx<R: std::io::Read + std::io::Seek>(reader: &mut Reader<R>, _ctx: Ctx) -> Result<Self, DekuError> {
226+
let count = i32::from_reader_with_ctx(reader, ())? as usize;
227+
let mut buf = vec![0x00; count];
228+
reader.read_bytes(count, &mut buf)?;
229+
Ok(ByteArray{ data: buf })
238230
}
239231
}
240232

@@ -271,7 +263,7 @@ pub struct ChannelInfo {
271263
}
272264

273265
#[derive(DekuRead, Clone, Debug)]
274-
#[deku(type = "u8")]
266+
#[deku(id_type = "u8")]
275267
pub enum VertexFormat {
276268
#[deku(id = "0")] Float,
277269
#[deku(id = "1")] Float16,
@@ -390,15 +382,15 @@ pub struct GLTextureSettings {
390382
}
391383

392384
#[derive(DekuRead, Clone, Debug)]
393-
#[deku(type = "i32")]
385+
#[deku(id_type = "i32")]
394386
pub enum TextureFilterMode {
395387
Nearest = 0,
396388
Bilinear = 1,
397389
Trilinear = 2,
398390
}
399391

400392
#[derive(DekuRead, Clone, Debug)]
401-
#[deku(type = "i32")]
393+
#[deku(id_type = "i32")]
402394
pub enum TextureWrapMode {
403395
Repeat = 0,
404396
Clamp = 1,
@@ -408,7 +400,7 @@ pub enum TextureWrapMode {
408400

409401
// copied from https://github.com/Unity-Technologies/UnityCsReference/blob/129a67089d125df5b95b659d3535deaf9968e86c/Editor/Mono/AssetPipeline/TextureImporterEnums.cs#L37
410402
#[derive(DekuRead, Clone, Debug)]
411-
#[deku(type = "i32")]
403+
#[deku(id_type = "i32")]
412404
pub enum TextureFormat {
413405
// Alpha 8 bit texture format.
414406
Alpha8 = 1,
@@ -523,7 +515,7 @@ pub enum TextureFormat {
523515
}
524516

525517
#[derive(DekuRead, Clone, Debug)]
526-
#[deku(type = "i32")]
518+
#[deku(id_type = "i32")]
527519
pub enum ColorSpace {
528520
Linear = 0x00,
529521
SRGB = 0x01,

rust/src/unity/types/class_id.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@ use deku::prelude::*;
33

44
#[wasm_bindgen(js_name = "UnityClassID")]
55
#[derive(DekuRead, Debug, Copy, Clone, PartialEq)]
6-
#[deku(type = "i32")]
6+
#[deku(id_type = "i32")]
77
#[repr(i32)]
88
pub enum ClassID {
99
#[deku(id = "-1")] UnknownType,

0 commit comments

Comments
 (0)