From 2c7ced715192f0656c6ef44aa477a46527702562 Mon Sep 17 00:00:00 2001 From: Quaternions Date: Tue, 13 Feb 2024 19:48:47 -0800 Subject: [PATCH] refactor api --- Cargo.lock | 4 +- Cargo.toml | 4 +- src/legacy.rs | 14 +++--- src/lib.rs | 14 +++--- src/texture.rs | 29 ++++++++++++ src/texture_loader.rs | 105 ------------------------------------------ 6 files changed, 46 insertions(+), 124 deletions(-) create mode 100644 src/texture.rs delete mode 100644 src/texture_loader.rs diff --git a/Cargo.lock b/Cargo.lock index e447d38..ab45f8b 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -119,8 +119,8 @@ dependencies = [ ] [[package]] -name = "strafesnet_texture_loader" -version = "0.1.0" +name = "strafesnet_deferred_loader" +version = "0.2.0" dependencies = [ "lazy-regex", "strafesnet_common", diff --git a/Cargo.toml b/Cargo.toml index 5322416..f7e925f 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -1,6 +1,6 @@ [package] -name = "strafesnet_texture_loader" -version = "0.1.0" +name = "strafesnet_deferred_loader" +version = "0.2.0" edition = "2021" # See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html diff --git a/src/legacy.rs b/src/legacy.rs index 20f80b1..50ae79a 100644 --- a/src/legacy.rs +++ b/src/legacy.rs @@ -1,6 +1,6 @@ use std::io::Read; use std::collections::HashMap; -use crate::texture_loader::{Texture,Textures}; +use crate::texture::{Texture,Textures}; use strafesnet_common::model::TextureId; #[derive(Hash,Eq,PartialEq)] @@ -28,10 +28,10 @@ impl std::fmt::Display for RobloxAssetIdParseErr{ } impl std::error::Error for RobloxAssetIdParseErr{} -pub struct TextureLoader{ +pub struct Loader{ texture_names:HashMap, } -impl TextureLoader{ +impl Loader{ pub fn new()->Self{ Self{ texture_names:HashMap::new(), @@ -39,14 +39,12 @@ impl TextureLoader{ } } -impl crate::texture_loader::TextureLoaderTrait for TextureLoader{ - type AcquireError=RobloxAssetIdParseErr; - type LoadError=std::io::Error; - fn acquire_id(&mut self,name:&str)->Result{ +impl Loader{ + pub fn acquire_texture_id(&mut self,name:&str)->Result{ let texture_id=TextureId::new(self.texture_names.len() as u32); Ok(*self.texture_names.entry(name.parse::()?).or_insert(texture_id)) } - fn load(&self)->Result{ + pub fn load_textures(&self)->Result{ let mut texture_data=vec![Vec::::new();self.texture_names.len()]; for (texture_name,texture_id) in &self.texture_names{ let path=std::path::PathBuf::from(format!("textures/{}.dds",texture_name.0)); diff --git a/src/lib.rs b/src/lib.rs index 136fdf2..8ee012b 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -5,17 +5,17 @@ mod roblox; #[cfg(feature="source")] mod source; -pub mod texture_loader; +pub mod texture; #[cfg(feature="legacy")] -pub fn legacy()->texture_loader::TextureLoader{ - texture_loader::TextureLoader::Legacy(legacy::TextureLoader::new()) +pub fn legacy()->legacy::Loader{ + legacy::Loader::new() } #[cfg(feature="roblox")] -pub fn roblox()->texture_loader::TextureLoader{ - texture_loader::TextureLoader::Roblox(roblox::TextureLoader::new()) +pub fn roblox()->roblox::Loader{ + roblox::Loader::new() } #[cfg(feature="source")] -pub fn source()->texture_loader::TextureLoader{ - texture_loader::TextureLoader::Source(source::TextureLoader::new()) +pub fn source()->source::Loader{ + source::Loader::new() } \ No newline at end of file diff --git a/src/texture.rs b/src/texture.rs new file mode 100644 index 0000000..59c9b6e --- /dev/null +++ b/src/texture.rs @@ -0,0 +1,29 @@ +use strafesnet_common::model::TextureId; + +pub enum Texture{ + ImageDDS(Vec), +} +impl AsRef<[u8]> for Texture{ + fn as_ref(&self)->&[u8]{ + match self{ + Texture::ImageDDS(data)=>data.as_ref(), + } + } +} + +pub struct Textures{ + textures:Vec, +} +impl Textures{ + pub(crate) const fn new(textures:Vec)->Self{ + Self{ + textures, + } + } + pub fn get_texture(&self,texture_id:TextureId)->Option<&Texture>{ + self.textures.get(texture_id.get() as usize) + } + pub fn into_iter(self)->impl Iterator{ + self.textures.into_iter().enumerate().map(|(texture_id,texture)|(TextureId::new(texture_id as u32),texture)) + } +} diff --git a/src/texture_loader.rs b/src/texture_loader.rs deleted file mode 100644 index 7f74f0d..0000000 --- a/src/texture_loader.rs +++ /dev/null @@ -1,105 +0,0 @@ -use strafesnet_common::model::TextureId; - -pub enum TextureLoader{ - #[cfg(feature="legacy")] - Legacy(crate::legacy::TextureLoader), - #[cfg(feature="roblox")] - Roblox(crate::roblox::TextureLoader), - #[cfg(feature="source")] - Source(crate::source::TextureLoader), -} - -pub enum Texture{ - ImageDDS(Vec), -} -impl AsRef<[u8]> for Texture{ - fn as_ref(&self)->&[u8]{ - match self{ - Texture::ImageDDS(data)=>data.as_ref(), - } - } -} - -pub struct Textures{ - textures:Vec, -} -impl Textures{ - pub(crate) const fn new(textures:Vec)->Self{ - Self{ - textures, - } - } - pub fn get_texture(&self,texture_id:TextureId)->Option<&Texture>{ - self.textures.get(texture_id.get() as usize) - } - pub fn into_iter(self)->impl Iterator{ - self.textures.into_iter().enumerate().map(|(texture_id,texture)|(TextureId::new(texture_id as u32),texture)) - } -} - -pub trait TextureLoaderTrait{ - type AcquireError; - type LoadError; - //write down the name of a texture to be fetched later, return a unique id for that texture - fn acquire_id(&mut self,name:&str)->Result; - fn load(&self)->Result; -} - -#[derive(Debug)] -pub enum TextureLoaderAcquireError{ - #[cfg(feature="legacy")] - Legacy(::AcquireError), - #[cfg(feature="roblox")] - Roblox(::AcquireError), - #[cfg(feature="source")] - Source(::AcquireError), -} -impl std::fmt::Display for TextureLoaderAcquireError{ - fn fmt(&self,f:&mut std::fmt::Formatter<'_>)->std::fmt::Result{ - write!(f,"{self:?}") - } -} -impl std::error::Error for TextureLoaderAcquireError{} - -#[derive(Debug)] -pub enum TextureLoaderLoadError{ - #[cfg(feature="legacy")] - Legacy(::LoadError), - #[cfg(feature="roblox")] - Roblox(::LoadError), - #[cfg(feature="source")] - Source(::LoadError), -} -impl std::fmt::Display for TextureLoaderLoadError{ - fn fmt(&self,f:&mut std::fmt::Formatter<'_>)->std::fmt::Result{ - write!(f,"{self:?}") - } -} -impl std::error::Error for TextureLoaderLoadError{} - -impl TextureLoaderTrait for TextureLoader{ - type AcquireError=TextureLoaderAcquireError; - type LoadError=TextureLoaderLoadError; - fn acquire_id(&mut self,name:&str)->Result{ - match self{ - #[cfg(feature="legacy")] - TextureLoader::Legacy(loader)=>loader.acquire_id(name).map_err(TextureLoaderAcquireError::Legacy), - #[cfg(feature="roblox")] - TextureLoader::Roblox(loader)=>loader.acquire_id(name).map_err(TextureLoaderAcquireError::Roblox), - #[cfg(feature="source")] - TextureLoader::Source(loader)=>loader.acquire_id(name).map_err(TextureLoaderAcquireError::Source), - _=>unreachable!(), - } - } - fn load(&self)->Result{ - match self{ - #[cfg(feature="legacy")] - TextureLoader::Legacy(loader)=>loader.load().map_err(TextureLoaderLoadError::Legacy), - #[cfg(feature="roblox")] - TextureLoader::Roblox(loader)=>loader.load().map_err(TextureLoaderLoadError::Roblox), - #[cfg(feature="source")] - TextureLoader::Source(loader)=>loader.load().map_err(TextureLoaderLoadError::Source), - _=>unreachable!(), - } - } -} \ No newline at end of file