From 6798bafc74393b0c63cb9c30d95751143566b82f Mon Sep 17 00:00:00 2001 From: Romet Tagobert Date: Fri, 2 Aug 2024 14:21:44 +0300 Subject: [PATCH] fix json issues & clippy --- Cargo.toml | 10 +++--- src/blocks/ni_particle/ni_psys_data.rs | 12 ++++++- src/collectors/gltf.rs | 50 +++++++++++++------------- src/collectors/single_mesh.rs | 2 +- src/parse_utils.rs | 8 ++--- 5 files changed, 46 insertions(+), 36 deletions(-) diff --git a/Cargo.toml b/Cargo.toml index 80a921c..b47cbf1 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -1,6 +1,6 @@ [package] name = "nif" -version = "0.4.2" +version = "0.5.0" edition = "2021" authors = ["Romet Tagobert "] categories = [] @@ -27,7 +27,7 @@ gltf_export = ["gltf", "gltf-json"] [dependencies] anyhow = "1.0.72" thiserror = "1.0.43" -glam = "0.24.1" -gltf = { version = "1.2.0", optional = true } -gltf-json = { version = "1.2.0", optional = true } -binrw = "0.11.2" +glam = "0.28.0" +gltf = { version = "1.4.1", optional = true } +gltf-json = { version = "1.4.1", optional = true } +binrw = "0.14.0" diff --git a/src/blocks/ni_particle/ni_psys_data.rs b/src/blocks/ni_particle/ni_psys_data.rs index 8848bd3..66c04c8 100644 --- a/src/blocks/ni_particle/ni_psys_data.rs +++ b/src/blocks/ni_particle/ni_psys_data.rs @@ -10,7 +10,7 @@ pub struct NiPSysData { pub base: NiParticlesData, #[br(count = base.base.num_vertices)] - pub particle_info: Vec<(Vector3, f32, f32, f32, u32)>, + pub particle_info: Vec, #[br(map = |x: u8| x > 0)] pub has_unknown_floats: bool, @@ -21,6 +21,16 @@ pub struct NiPSysData { pub unknown_short_2: u16, } +#[derive(Debug, PartialEq, BinRead)] +pub struct NiParticleInfo { + pub velocity: Vector3, + pub age: f32, + pub life_span: f32, + pub last_update: f32, + pub spawn_generation: u16, + pub code: u16, +} + impl NiPSysData { pub fn parse(reader: &mut R) -> anyhow::Result { Ok(reader.read_le()?) diff --git a/src/collectors/gltf.rs b/src/collectors/gltf.rs index b045f92..4ddbba5 100644 --- a/src/collectors/gltf.rs +++ b/src/collectors/gltf.rs @@ -42,7 +42,7 @@ impl Gltf { let buffer_path = gltf_path.with_file_name(&relative_buffer_path); gltf.buffers.push(json::Buffer { - byte_length: buffer_vec.len() as u32, + byte_length: buffer_vec.len().into(), name: Some(format!("{}_Buffer{}", gltf_filename, buffer_idx)), uri: Some(relative_buffer_path), extensions: Default::default(), @@ -800,9 +800,9 @@ impl Gltf { Some(vertices) => { self.root.buffer_views.push(json::buffer::View { buffer: buffer_index, - byte_length: expected_vertices_length as u32, - byte_offset: Some(offset_so_far), - byte_stride: Some(12), + byte_length: expected_vertices_length.into(), + byte_offset: Some(json::validation::USize64(offset_so_far as _)), + byte_stride: Some(json::buffer::Stride(12)), name: Some(format!("{}_Buffer_Vertices", name)), target: Some(Valid(json::buffer::Target::ArrayBuffer)), extensions: Default::default(), @@ -813,8 +813,8 @@ impl Gltf { json::Index::new(self.root.buffer_views.len() as u32 - 1); self.root.accessors.push(json::Accessor { buffer_view: Some(buffer_view_index), - byte_offset: Some(0), - count: vertices.len() as u32, + byte_offset: Some(json::validation::USize64(0)), + count: vertices.len().into(), component_type: Valid(json::accessor::GenericComponentType( json::accessor::ComponentType::F32, )), @@ -844,9 +844,9 @@ impl Gltf { Some(normals) => { self.root.buffer_views.push(json::buffer::View { buffer: buffer_index, - byte_length: expected_normals_length as u32, - byte_offset: Some(offset_so_far), - byte_stride: Some(12), + byte_length: expected_normals_length.into(), + byte_offset: Some(json::validation::USize64(offset_so_far as _)), + byte_stride: Some(json::buffer::Stride(12)), name: Some(format!("{}_Buffer_Normals", name)), target: Some(Valid(json::buffer::Target::ArrayBuffer)), extensions: Default::default(), @@ -857,8 +857,8 @@ impl Gltf { json::Index::new(self.root.buffer_views.len() as u32 - 1); self.root.accessors.push(json::Accessor { buffer_view: Some(buffer_view_index), - byte_offset: Some(0), - count: normals.len() as u32, + byte_offset: Some(json::validation::USize64(0)), + count: normals.len().into(), component_type: Valid(json::accessor::GenericComponentType( json::accessor::ComponentType::F32, )), @@ -888,9 +888,9 @@ impl Gltf { Some(colors) => { self.root.buffer_views.push(json::buffer::View { buffer: buffer_index, - byte_length: expected_colors_length as u32, - byte_offset: Some(offset_so_far), - byte_stride: Some(16), + byte_length: expected_colors_length.into(), + byte_offset: Some(json::validation::USize64(offset_so_far as _)), + byte_stride: Some(json::buffer::Stride(16)), name: Some(format!("{}_Buffer_Colors", name)), target: Some(Valid(json::buffer::Target::ArrayBuffer)), extensions: Default::default(), @@ -901,8 +901,8 @@ impl Gltf { json::Index::new(self.root.buffer_views.len() as u32 - 1); self.root.accessors.push(json::Accessor { buffer_view: Some(buffer_view_index), - byte_offset: Some(0), - count: colors.len() as u32, + byte_offset: Some(json::validation::USize64(0)), + count: colors.len().into(), component_type: Valid(json::accessor::GenericComponentType( json::accessor::ComponentType::F32, )), @@ -936,9 +936,9 @@ impl Gltf { Some(uv_set) => { self.root.buffer_views.push(json::buffer::View { buffer: buffer_index, - byte_length: expected_uvs_length as u32, - byte_offset: Some(offset_so_far), - byte_stride: Some(8), + byte_length: expected_uvs_length.into(), + byte_offset: Some(json::validation::USize64(offset_so_far as _)), + byte_stride: Some(json::buffer::Stride(8)), name: Some(format!("{}_Buffer_UVs", name)), target: Some(Valid(json::buffer::Target::ArrayBuffer)), extensions: Default::default(), @@ -949,8 +949,8 @@ impl Gltf { json::Index::new(self.root.buffer_views.len() as u32 - 1); self.root.accessors.push(json::Accessor { buffer_view: Some(buffer_view_index), - byte_offset: Some(0), - count: uv_set.uvs.len() as u32, + byte_offset: Some(json::validation::USize64(0)), + count: uv_set.uvs.len().into(), component_type: Valid(json::accessor::GenericComponentType( json::accessor::ComponentType::F32, )), @@ -972,8 +972,8 @@ impl Gltf { Some(triangles) => { self.root.buffer_views.push(json::buffer::View { buffer: buffer_index, - byte_length: expected_triangles_length as u32, - byte_offset: Some(offset_so_far), + byte_length: expected_triangles_length.into(), + byte_offset: Some(json::validation::USize64(offset_so_far as _)), byte_stride: None, //Some(6), name: Some(format!("{}_Buffer_Triangles", name)), target: Some(Valid(json::buffer::Target::ElementArrayBuffer)), @@ -984,8 +984,8 @@ impl Gltf { json::Index::new(self.root.buffer_views.len() as u32 - 1); self.root.accessors.push(json::Accessor { buffer_view: Some(buffer_view_index), - byte_offset: Some(0), - count: triangles.len() as u32 * 3, + byte_offset: Some(json::validation::USize64(0)), + count: (triangles.len() * 3).into(), component_type: Valid(json::accessor::GenericComponentType( json::accessor::ComponentType::U16, )), diff --git a/src/collectors/single_mesh.rs b/src/collectors/single_mesh.rs index 40976cb..3cba5cc 100644 --- a/src/collectors/single_mesh.rs +++ b/src/collectors/single_mesh.rs @@ -14,7 +14,7 @@ pub struct Mesh { impl Mesh { pub fn add_nif(&mut self, nif: &Nif, lod_distance: f32) -> anyhow::Result<()> { - if let Some(Block::NiNode(ni_node)) = nif.blocks.get(0) { + if let Some(Block::NiNode(ni_node)) = nif.blocks.first() { self.visit_ni_node(nif, ni_node, None, lod_distance)?; } diff --git a/src/parse_utils.rs b/src/parse_utils.rs index 9dd4a65..bb30de8 100644 --- a/src/parse_utils.rs +++ b/src/parse_utils.rs @@ -2,14 +2,14 @@ use super::blocks::{Block, *}; use super::common; use crate::error::NifError; use binrw::{io::Read, BinRead, BinResult}; -use std::io::SeekFrom; #[binrw::parser(reader, endian)] -pub fn parse_keys( +pub fn parse_keys( num_keys: u32, key_type: Option, ) -> BinResult>> where + T: BinRead, T: for<'a> BinRead = ()>, { if num_keys == 0 { @@ -330,7 +330,7 @@ pub fn parse_blocks(strings: Vec, block_type_indices: Vec) -> BinRe ), _ => { return Err(binrw::Error::Custom { - pos: reader.seek(SeekFrom::Current(0))?, + pos: reader.stream_position()?, err: Box::new(NifError::UnknownBlock(blocks.len(), block_type.clone())), }); } @@ -339,7 +339,7 @@ pub fn parse_blocks(strings: Vec, block_type_indices: Vec) -> BinRe } None => { return Err(binrw::Error::Custom { - pos: reader.seek(SeekFrom::Current(0))?, + pos: reader.stream_position()?, err: Box::new(NifError::InvalidBlockTypeIndex), }); }