diff options
Diffstat (limited to 'shared/src/resources.rs')
-rw-r--r-- | shared/src/resources.rs | 172 |
1 files changed, 107 insertions, 65 deletions
diff --git a/shared/src/resources.rs b/shared/src/resources.rs index a677755..cdf53bd 100644 --- a/shared/src/resources.rs +++ b/shared/src/resources.rs @@ -1,5 +1,5 @@ -use crate::packets::Resource; -use anyhow::{Result, anyhow}; +use crate::packets::{ReadWrite, Resource}; +use anyhow::{Result, bail}; use log::warn; use std::io::{Read, Write}; @@ -8,120 +8,130 @@ pub struct Prefab(pub Vec<Resource>); #[derive(Debug, Default, Clone)] pub struct Part { - pub vertex: Vec<Resource>, pub index: Option<Resource>, - pub armature: Option<Resource>, - pub fragment_shader: Option<Resource>, - pub fragment_shader_data: Option<Resource>, - pub vertex_shader: Option<Resource>, - pub vertex_shader_data: Option<Resource>, - pub texture: Vec<Resource>, + pub va_position: Option<[Attribute; 3]>, + pub va_normal: Option<[Attribute; 3]>, + pub va_texcoord: Option<[Attribute; 2]>, + pub va_pbr_roughness: Option<Attribute>, + pub va_pbr_metallic: Option<Attribute>, + pub va_pbr_albedo: Option<[Attribute; 3]>, + pub va_pbr_transmission: Option<Attribute>, +} + +#[derive(Debug, Clone)] +pub enum Attribute { + Constant(f32), + Vertex(Resource), + Texture(Resource), } #[derive(Debug, Default, Clone)] -pub struct VertexAttributes(pub Vec<f32>); +pub struct AttributeArray(pub Vec<f32>); #[derive(Debug, Default, Clone)] -pub struct Indecies(pub Vec<[u16; 3]>); +pub struct IndexArray(pub Vec<[u16; 3]>); -impl Prefab { - pub fn serialize(&self, w: &mut Vec<u8>) -> Result<()> { +impl ReadWrite for Prefab { + fn write(&self, w: &mut dyn Write) -> Result<()> { for x in self.0.clone() { w.write_all(&x.0)?; } Ok(()) } - pub fn deserialize(r: &[u8]) -> Result<Self> { + fn read(r: &mut dyn Read) -> Result<Self> { let mut s = Prefab::default(); - for x in r.iter().array_chunks::<32>() { + let mut g = Vec::new(); + r.read_to_end(&mut g)?; + for x in g.iter().array_chunks::<32>() { s.0.push(Resource(x.map(|x| *x))) } Ok(s) } } -impl Indecies { - pub fn serialize(&self, w: &mut Vec<u8>) -> Result<()> { +impl ReadWrite for IndexArray { + fn write(&self, w: &mut dyn Write) -> Result<()> { for x in self.0.clone() { w.write_all(x.map(|x| x.to_be_bytes()).as_flattened())?; } Ok(()) } - pub fn deserialize(r: &[u8]) -> Result<Self> { + fn read(r: &mut dyn Read) -> Result<Self> { let mut s = Self(Vec::new()); - for x in r.iter().array_chunks::<2>().array_chunks::<3>() { + let mut g = Vec::new(); + r.read_to_end(&mut g)?; + for x in g.iter().array_chunks::<2>().array_chunks::<3>() { s.0.push(x.map(|x| u16::from_be_bytes(x.map(|x| *x)))) } Ok(s) } } -impl VertexAttributes { - pub fn serialize(&self, w: &mut Vec<u8>) -> Result<()> { +impl ReadWrite for AttributeArray { + fn write(&self, w: &mut dyn Write) -> Result<()> { for x in self.0.clone() { w.write_all(&x.to_be_bytes())?; } Ok(()) } - pub fn deserialize(r: &[u8]) -> Result<Self> { + fn read(r: &mut dyn Read) -> Result<Self> { let mut s = Self(Vec::new()); - for x in r.iter().array_chunks::<4>() { + let mut g = Vec::new(); + r.read_to_end(&mut g)?; + for x in g.iter().array_chunks::<4>() { s.0.push(f32::from_be_bytes(x.map(|x| *x))) } Ok(s) } } -impl Part { - pub fn serialize(&self, w: &mut Vec<u8>) -> Result<()> { - for x in &self.vertex { - write_kv(w, b"vertex", &x.0); +impl ReadWrite for Part { + fn write(&self, w: &mut dyn Write) -> Result<()> { + if let Some(a) = &self.index { + write_kv(w, b"index", &a.write_alloc())?; } - if let Some(x) = &self.index { - write_kv(w, b"index", &x.0); + if let Some(a) = &self.va_position { + write_kv(w, b"va_position", &a.write_alloc())?; } - if let Some(x) = &self.armature { - write_kv(w, b"armature", &x.0); + if let Some(a) = &self.va_normal { + write_kv(w, b"va_normal", &a.write_alloc())?; } - if let Some(x) = &self.fragment_shader { - write_kv(w, b"fragment_shader", &x.0); + if let Some(a) = &self.va_texcoord { + write_kv(w, b"va_texcoord", &a.write_alloc())?; } - if let Some(x) = &self.fragment_shader_data { - write_kv(w, b"fragment_shader_data", &x.0); + if let Some(a) = &self.va_pbr_roughness { + write_kv(w, b"va_pbr_roughness", &a.write_alloc())?; } - if let Some(x) = &self.vertex_shader { - write_kv(w, b"vertex_shader", &x.0); + if let Some(a) = &self.va_pbr_metallic { + write_kv(w, b"va_pbr_metallic", &a.write_alloc())?; } - if let Some(x) = &self.vertex_shader_data { - write_kv(w, b"vertex_shader_data", &x.0); - } - for x in &self.vertex { - write_kv(w, b"texture", &x.0); + if let Some(a) = &self.va_pbr_albedo { + write_kv(w, b"va_pbr_albedo", &a.write_alloc())?; } Ok(()) } - pub fn deserialize(mut r: &[u8]) -> Result<Self> { + fn read(r: &mut dyn Read) -> Result<Self> { let mut s = Self::default(); - while !r.is_empty() { - let (k, v) = read_kv(&mut r)?; + let mut g = Vec::new(); + r.read_to_end(&mut g)?; + let mut g = g.as_slice(); + while !g.is_empty() { + let (k, v) = read_kv(&mut g)?; + let mut v = v.as_slice(); match k.as_slice() { - b"vertex" => s.vertex.push(slice_to_res(&v)?), - b"index" => s.index = Some(slice_to_res(&v)?), - b"armature" => s.armature = Some(slice_to_res(&v)?), - b"fragment_shader" => s.fragment_shader = Some(slice_to_res(&v)?), - b"fragment_shader_data" => s.fragment_shader_data = Some(slice_to_res(&v)?), - b"vertex_shader" => s.vertex_shader = Some(slice_to_res(&v)?), - b"vertex_shader_data" => s.vertex_shader_data = Some(slice_to_res(&v)?), - b"texture" => s.texture.push(slice_to_res(&v)?), + b"index" => s.index = Some(<_ as ReadWrite>::read(&mut v)?), + b"va_position" => s.va_position = Some(<_ as ReadWrite>::read(&mut v)?), + b"va_normal" => s.va_normal = Some(<_ as ReadWrite>::read(&mut v)?), + b"va_texcoord" => s.va_texcoord = Some(<_ as ReadWrite>::read(&mut v)?), + b"va_pbr_roughness" => s.va_pbr_roughness = Some(<_ as ReadWrite>::read(&mut v)?), + b"va_pbr_metallic" => s.va_pbr_metallic = Some(<_ as ReadWrite>::read(&mut v)?), + b"va_pbr_albedo" => s.va_pbr_albedo = Some(<_ as ReadWrite>::read(&mut v)?), + b"va_pbr_transmission" => { + s.va_pbr_transmission = Some(<_ as ReadWrite>::read(&mut v)?) + } _ => warn!("unknown part key"), } } Ok(s) } } -fn slice_to_res(s: &[u8]) -> Result<Resource> { - Ok(Resource( - s.try_into() - .map_err(|_| anyhow!("resource length incorrect"))?, - )) -} -fn read_kv(r: &mut &[u8]) -> Result<(Vec<u8>, Vec<u8>)> { +fn read_kv(r: &mut dyn Read) -> Result<(Vec<u8>, Vec<u8>)> { let mut key_size = [0; 2]; let mut value_size = [0; 2]; r.read_exact(&mut key_size)?; @@ -134,9 +144,41 @@ fn read_kv(r: &mut &[u8]) -> Result<(Vec<u8>, Vec<u8>)> { r.read_exact(&mut key)?; Ok((key, value)) } -fn write_kv(w: &mut Vec<u8>, key: &[u8], value: &[u8]) { - w.extend(&(key.len() as u16).to_be_bytes()); - w.extend(&(value.len() as u16).to_be_bytes()); - w.extend(key); - w.extend(value); + +fn write_kv(w: &mut dyn Write, key: &[u8], value: &[u8]) -> Result<()> { + w.write_all(&(key.len() as u16).to_be_bytes())?; + w.write_all(&(value.len() as u16).to_be_bytes())?; + w.write_all(key)?; + w.write_all(value)?; + Ok(()) +} + +impl ReadWrite for Attribute { + fn write(&self, w: &mut dyn Write) -> Result<()> { + match self { + Attribute::Constant(v) => { + w.write_all(&[0x01])?; + w.write_all(&v.to_be_bytes())?; + } + Attribute::Vertex(resource) => { + w.write_all(&[0x02])?; + resource.write(w)?; + } + Attribute::Texture(resource) => { + w.write_all(&[0x03])?; + resource.write(w)?; + } + } + Ok(()) + } + fn read(r: &mut dyn Read) -> Result<Self> { + let mut tag = [0u8; 1]; + r.read_exact(&mut tag)?; + Ok(match tag[0] { + 0x01 => Self::Constant(f32::read(r)?), + 0x02 => Self::Vertex(Resource::read(r)?), + 0x03 => Self::Texture(Resource::read(r)?), + _ => bail!("unknown attribute tag"), + }) + } } |