From 02d65e6b7ce7a0e6bae054bd321c68dda1cb0de3 Mon Sep 17 00:00:00 2001 From: metamuffin Date: Tue, 7 Jan 2025 22:56:34 +0100 Subject: static typing for resources --- shared/src/resources.rs | 73 +++++++++++++++++++++++++++++-------------------- 1 file changed, 43 insertions(+), 30 deletions(-) (limited to 'shared/src/resources.rs') diff --git a/shared/src/resources.rs b/shared/src/resources.rs index be34b21..d22e3ea 100644 --- a/shared/src/resources.rs +++ b/shared/src/resources.rs @@ -19,60 +19,64 @@ use anyhow::Result; use glam::{Affine3A, Vec3A}; use log::warn; use std::{ - collections::HashMap, + collections::{BTreeMap, HashMap}, io::{Read, Write}, }; #[derive(Debug, Default, Clone)] pub struct Prefab { - pub mesh: Vec<(Affine3A, Resource)>, - pub light: Vec<(Vec3A, Resource)>, - pub environment: Option, + pub mesh: Vec<(Affine3A, Resource)>, + pub light: Vec<(Vec3A, Resource)>, + pub environment: Option>, } #[derive(Debug, Default, Clone)] pub struct LightPart { - emission: Option, - radius: Option, + pub emission: Option, + pub radius: Option, } #[derive(Debug, Default, Clone)] pub struct EnvironmentPart { - skybox: Option, - sun: Option<(Vec3A, Vec3A)>, + pub skybox: Option, + pub sun: Option<(Vec3A, Vec3A)>, } #[derive(Debug, Default, Clone)] pub struct MeshPart { - pub index: Option, + pub index: Option>, pub g_metallic: Option, pub g_roughness: Option, pub g_albedo: Option, pub g_transmission: Option, pub g_emission: Option, - pub va_position: Option<[Resource; 3]>, - pub va_normal: Option<[Resource; 3]>, - pub va_texcoord: Option<[Resource; 2]>, - pub va_roughness: Option, - pub va_metallic: Option, - pub va_albedo: Option<[Resource; 3]>, - pub va_transmission: Option, - pub va_emission: Option<[Resource; 3]>, - pub tex_normal: Option, - pub tex_roughness: Option, - pub tex_metallic: Option, - pub tex_albedo: Option, - pub tex_transmission: Option, - pub tex_emission: Option, + pub va_position: Option<[Resource; 3]>, + pub va_normal: Option<[Resource; 3]>, + pub va_texcoord: Option<[Resource; 2]>, + pub va_roughness: Option>, + pub va_metallic: Option>, + pub va_albedo: Option<[Resource; 3]>, + pub va_transmission: Option>, + pub va_emission: Option<[Resource; 3]>, + pub tex_normal: Option>, + pub tex_roughness: Option>, + pub tex_metallic: Option>, + pub tex_albedo: Option>, + pub tex_transmission: Option>, + pub tex_emission: Option>, } -pub struct PrefabIndex(pub HashMap); +#[derive(Debug, Default, Clone)] +pub struct PrefabIndex(pub BTreeMap); #[derive(Debug, Default, Clone)] pub struct AttributeArray(pub Vec); #[derive(Debug, Default, Clone)] pub struct IndexArray(pub Vec<[u16; 3]>); +#[derive(Debug, Clone)] +pub struct Image(pub Vec); + impl ReadWrite for PrefabIndex { fn write(&self, w: &mut dyn Write) -> Result<()> { for (k, v) in &self.0 { @@ -81,7 +85,7 @@ impl ReadWrite for PrefabIndex { Ok(()) } fn read(r: &mut dyn Read) -> Result { - let mut s = Self(HashMap::new()); + let mut s = Self(BTreeMap::new()); let mut g = Vec::new(); r.read_to_end(&mut g)?; let mut g = g.as_slice(); @@ -96,10 +100,10 @@ impl ReadWrite for PrefabIndex { impl ReadWrite for Prefab { fn write(&self, w: &mut dyn Write) -> Result<()> { for x in &self.mesh { - write_kv_opt(w, b"mesh", &Some(*x))?; + write_kv_opt(w, b"mesh", &Some(x.clone()))?; } for x in &self.light { - write_kv_opt(w, b"light", &Some(*x))?; + write_kv_opt(w, b"light", &Some(x.clone()))?; } write_kv_opt(w, b"environment", &self.environment)?; Ok(()) @@ -269,9 +273,9 @@ impl ReadWrite for u8 { impl ReadWrite for Vec3A { fn write(&self, w: &mut dyn Write) -> Result<()> { - w.write_all(&self.x.to_be_bytes())?; - w.write_all(&self.y.to_be_bytes())?; - w.write_all(&self.z.to_be_bytes())?; + self.x.write(w)?; + self.y.write(w)?; + self.z.write(w)?; Ok(()) } fn read(r: &mut dyn Read) -> Result { @@ -333,3 +337,12 @@ impl ReadWrite for (A, B) { Ok((A::read(r)?, B::read(r)?)) } } +impl ReadWrite for Image { + fn write(&self, w: &mut dyn Write) -> Result<()> { + self.0.write(w) + } + + fn read(r: &mut dyn Read) -> Result { + Ok(Self( as ReadWrite>::read(r)?)) + } +} -- cgit v1.2.3-70-g09d2