Skip to content

Commit

Permalink
more hacks
Browse files Browse the repository at this point in the history
  • Loading branch information
magcius committed Nov 10, 2024
1 parent 2857f14 commit b509f14
Show file tree
Hide file tree
Showing 4 changed files with 50 additions and 9 deletions.
6 changes: 6 additions & 0 deletions rust/src/unity/common.rs
Original file line number Diff line number Diff line change
Expand Up @@ -87,6 +87,8 @@ pub struct CharArray {
count: u32,
#[deku(count = "*count")]
bytes: Vec<u8>,
// align to the nearest 4 byte boundary
#[deku(count = "(4 - deku::byte_offset % 4) % 4")] _alignment: Vec<u8>,
}

impl From<CharArray> for String {
Expand Down Expand Up @@ -142,6 +144,7 @@ pub struct Vec4 {
pub x: f32,
pub y: f32,
pub z: f32,
pub w: f32,
}

#[wasm_bindgen(js_name = "UnityVec3")]
Expand Down Expand Up @@ -245,9 +248,12 @@ impl<'a> DekuRead<'a> for Packedf32Vec {
where
Self: Sized {
let (mut rest, num_items) = u32::read(input, ctx)?;
// dbg!("JJJ AAA", num_items);
let (new_rest, scale) = f32::read(rest, ctx)?;
// dbg!("JJJ AAA", scale);
rest = new_rest;
let (new_rest, start) = f32::read(rest, ctx)?;
// dbg!("JJJ AAA", start);
rest = new_rest;
let (last_rest, bit_size) = u8::read(&rest[4 * num_items as usize..], ctx)?;
let max = ((1 << bit_size) as f32) - 1.0;
Expand Down
27 changes: 21 additions & 6 deletions rust/src/unity/serialized_file.rs
Original file line number Diff line number Diff line change
Expand Up @@ -241,7 +241,7 @@ struct FileIdentifier {
mod tests {
use std::collections::HashMap;

use crate::unity::types::object::{GameObject, MeshFilter, MeshRenderer, Transform, UnityVersion};
use crate::unity::types::object::{GameObject, Mesh, MeshFilter, MeshRenderer, Transform, UnityVersion};
use crate::unity::class_id::ClassID;

use super::*;
Expand All @@ -250,8 +250,8 @@ mod tests {

#[test]
fn test() {
let data = std::fs::read("C:\\Users\\ifnsp\\dev\\noclip.website\\data\\AShortHike\\level2").unwrap();
let version = UnityVersion::V2021_3_27f1;
let data = std::fs::read("M:\\Development\\NITRO_BMD\\data\\AShortHike\\level2").unwrap();
let version = UnityVersion::V2019_4_39f1;
let mut asset_file = AssetFile::initialize_with_header_chunk(&data).unwrap();
dbg!(&asset_file.header);
asset_file.append_metadata_chunk(&data).unwrap();
Expand Down Expand Up @@ -281,20 +281,35 @@ mod tests {
dbg!(component_ptr);
continue;
}
let obj = object_infos.get(&component_ptr.path_id).unwrap();
let obj: &&ObjectInfo = object_infos.get(&component_ptr.path_id).unwrap();
let obj_type = &metadata.type_tree[obj.serialized_type_index as usize];
let byte_start = asset_file.get_data_offset() as usize + match obj.small_file_byte_start {
Some(start) => start as usize,
None => obj.large_file_byte_start.unwrap() as usize,
};
let byte_size = obj.byte_size as usize;
let bigdata = &data;
let data = &data[byte_start..byte_start + byte_size];

match obj_type.header.raw_type_id {
ClassID::Transform => {Transform::create(version, data).unwrap();},
ClassID::RectTransform => {Transform::create(version, data).unwrap();},
ClassID::MeshFilter => {MeshFilter::create(version, data).unwrap();},
ClassID::MeshFilter => {
let filter = MeshFilter::create(version, data).unwrap();
let obj = object_infos.get(&filter.mesh.path_id).unwrap();
let byte_start = asset_file.get_data_offset() as usize + match obj.small_file_byte_start {
Some(start) => start as usize,
None => obj.large_file_byte_start.unwrap() as usize,
};
let byte_size = obj.byte_size as usize;
let data = &bigdata[byte_start..byte_start + byte_size];

if (filter.mesh.path_id == 42) {
let _mesh = Mesh::create(version, data).unwrap();
}
},
ClassID::MeshRenderer => {MeshRenderer::create(version, data).unwrap();},
s => println!("skipping {:?}", s),
s => {},
}
}
}
Expand Down
22 changes: 20 additions & 2 deletions rust/src/unity/types/v2019_4_39f1.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
use deku::prelude::*;
use deku::{bitvec::{BitSlice, Msb0}, prelude::*};

// https://github.com/AssetRipper/TypeTreeDumps/blob/main/StructsDump/release/2019.4.39f1.dump
// e.g. Outer Wilds
Expand Down Expand Up @@ -141,6 +141,12 @@ pub struct StreamingInfo {
pub path: CharArray,
}

fn read_dbg<'a>(input: &'a BitSlice<u8, Msb0>, _bit_offset: usize) -> Result<(&'a BitSlice<u8, Msb0>, i32), DekuError> {
let r = i32::read(input, ())?;
dbg!("JJJ AAA", r.1);
return Ok(r);
}

#[derive(DekuRead, Clone, Debug)]
pub struct SubMesh {
pub first_byte: u32,
Expand All @@ -156,23 +162,34 @@ pub struct SubMesh {
pub struct VertexData {
pub vertex_count: u32,
pub channels: UnityArray<ChannelInfo>,
#[deku(count = "(4 - deku::byte_offset % 4) % 4")] _alignment: Vec<u8>,
pub data: UnityArray<u8>,
#[deku(count = "(4 - deku::byte_offset % 4) % 4")] _alignment: Vec<u8>,
}

#[derive(DekuRead, Clone, Debug)]
pub struct CompressedMesh {
pub vertices: Packedf32Vec,
#[deku(count = "(4 - deku::byte_offset % 4) % 4")] _alignment1: Vec<u8>,
pub uv: Packedf32Vec,
#[deku(count = "(4 - deku::byte_offset % 4) % 4")] _alignment2: Vec<u8>,
pub normals: Packedf32Vec,
#[deku(count = "(4 - deku::byte_offset % 4) % 4")] _alignment3: Vec<u8>,
pub tangents: Packedf32Vec,
#[deku(count = "(4 - deku::byte_offset % 4) % 4")] _alignment4: Vec<u8>,
pub weights: Packedi32Vec,
#[deku(count = "(4 - deku::byte_offset % 4) % 4")] _alignment5: Vec<u8>,
pub normal_signs: Packedi32Vec,
#[deku(count = "(4 - deku::byte_offset % 4) % 4")] _alignment6: Vec<u8>,
pub tangent_signs: Packedi32Vec,
#[deku(count = "(4 - deku::byte_offset % 4) % 4")] _alignment7: Vec<u8>,
pub float_colors: Packedf32Vec,
#[deku(count = "(4 - deku::byte_offset % 4) % 4")] _alignment8: Vec<u8>,
pub bone_indices: Packedi32Vec,
#[deku(count = "(4 - deku::byte_offset % 4) % 4")] _alignment9: Vec<u8>,
pub triangles: Packedi32Vec,
#[deku(count = "(4 - deku::byte_offset % 4) % 4")] _alignment10: Vec<u8>,
pub uv_info: u32,
#[deku(count = "(4 - deku::byte_offset % 4) % 4")] _alignment11: Vec<u8>,
}

#[derive(DekuRead, Clone, Debug)]
Expand Down Expand Up @@ -214,6 +231,7 @@ pub struct MeshBlendShape {
pub vertex_count: u32,
pub has_normals: u8,
pub has_tangents: u8,
_padding: u16,
}

#[derive(DekuRead, Clone, Debug)]
Expand Down
4 changes: 3 additions & 1 deletion src/Common/Unity/AssetManager.ts
Original file line number Diff line number Diff line change
Expand Up @@ -255,8 +255,10 @@ export class AssetFile {

private createMeshData = async (assetSystem: UnityAssetSystem, objData: AssetObjectData): Promise<UnityMeshData> => {
try {
console.log("f")
debugger;
const mesh = rust.UnityMesh.create(assetSystem.version, objData.data);
debugger;

const streamingInfo: UnityStreamingInfo = mesh.streaming_info;
if (streamingInfo.path.length !== 0) {
const buf = await assetSystem.fetchStreamingInfo(streamingInfo);
Expand Down

0 comments on commit b509f14

Please sign in to comment.