Skip to content

Commit

Permalink
Unity optimization + bugfix (#746)
Browse files Browse the repository at this point in the history
* unity: faster handling of byte arrays

* unity: fix error when reloading a scene
  • Loading branch information
wgreenberg authored Jan 20, 2025
1 parent 1da3428 commit bbadfb8
Show file tree
Hide file tree
Showing 2 changed files with 39 additions and 8 deletions.
41 changes: 35 additions & 6 deletions rust/src/unity/types/binary.rs
Original file line number Diff line number Diff line change
Expand Up @@ -135,17 +135,17 @@ pub struct Mesh {
pub keep_vertices: u8,
pub keep_indices: u8,
pub index_format: IndexFormat,
pub index_buffer: UnityArray<u8>,
pub index_buffer: ByteArray,
#[deku(count = "(4 - deku::byte_offset % 4) % 4")] _alignment2: Vec<u8>,
#[deku(ctx = "version")]
pub vertex_data: VertexData,
#[deku(count = "(4 - deku::byte_offset % 4) % 4")] _alignment3: Vec<u8>,
pub compressed_mesh: CompressedMesh,
pub local_aabb: AABB,
pub mesh_usage_flags: i32,
pub baked_convex_collision_mesh: UnityArray<u8>,
pub baked_convex_collision_mesh: ByteArray,
#[deku(count = "(4 - deku::byte_offset % 4) % 4")] _alignment4: Vec<u8>,
pub baked_triangle_collision_mesh: UnityArray<u8>,
pub baked_triangle_collision_mesh: ByteArray,
#[deku(count = "(4 - deku::byte_offset % 4) % 4")] _alignment5: Vec<u8>,
pub mesh_metrics: [f32; 2],
#[deku(ctx = "version")]
Expand Down Expand Up @@ -211,10 +211,39 @@ pub struct SubMesh {
pub struct VertexData {
pub vertex_count: u32,
pub channels: UnityArray<ChannelInfo>,
pub data: UnityArray<u8>,
pub data: ByteArray,
#[deku(count = "(4 - deku::byte_offset % 4) % 4")] _alignment: Vec<u8>,
}

#[derive(Default, Debug, Clone)]
pub struct ByteArray {
pub data: Vec<u8>,
}

impl<'a> DekuRead<'a> for ByteArray {
fn read(
input: &'a deku::bitvec::BitSlice<u8, deku::bitvec::Msb0>,
ctx: (),
) -> Result<(&'a deku::bitvec::BitSlice<u8, deku::bitvec::Msb0>, Self), DekuError>
where Self: Sized {
let (rest, count) = i32::read(input, ctx)?;
let (data_bits, rest) = rest.split_at(count as usize * 8);
let bytes = data_bits.domain().region().unwrap().1;
Ok((
rest,
Self {
data: bytes.to_vec(),
},
))
}
}

impl From<ByteArray> for Vec<u8> {
fn from(value: ByteArray) -> Self {
value.data
}
}

#[derive(DekuRead, Clone, Debug)]
pub struct CompressedMesh {
pub vertices: Packedf32Vec,
Expand Down Expand Up @@ -323,9 +352,9 @@ pub struct Texture2D {
pub lightmap_format: i32,
pub color_space: ColorSpace,
#[deku(cond = "version >= UnityVersion::V2020_3_16f1")]
pub platform_blob: UnityArray<u8>,
pub platform_blob: ByteArray,
#[deku(count = "(4 - deku::byte_offset % 4) % 4")] _alignment2: Vec<u8>,
pub data: UnityArray<u8>,
pub data: ByteArray,
#[deku(count = "(4 - deku::byte_offset % 4) % 4")] _alignment3: Vec<u8>,
#[deku(ctx = "version")]
pub streaming_info: StreamingInfo,
Expand Down
6 changes: 4 additions & 2 deletions src/Common/Unity/AssetManager.ts
Original file line number Diff line number Diff line change
Expand Up @@ -156,8 +156,10 @@ export class AssetFile {
this.waitForHeaderPromise = null;
}

public waitForHeader(): Promise<void> {
return assertExists(this.waitForHeaderPromise);
public async waitForHeader() {
if (this.waitForHeaderPromise !== null) {
await this.waitForHeaderPromise;
}
}

private async initFullInternal(dataFetcher: DataFetcher): Promise<void> {
Expand Down

0 comments on commit bbadfb8

Please sign in to comment.