refactor api

This commit is contained in:
Quaternions 2024-02-13 19:48:47 -08:00
parent c182e46001
commit 2c7ced7151
6 changed files with 46 additions and 124 deletions

4
Cargo.lock generated
View File

@ -119,8 +119,8 @@ dependencies = [
] ]
[[package]] [[package]]
name = "strafesnet_texture_loader" name = "strafesnet_deferred_loader"
version = "0.1.0" version = "0.2.0"
dependencies = [ dependencies = [
"lazy-regex", "lazy-regex",
"strafesnet_common", "strafesnet_common",

View File

@ -1,6 +1,6 @@
[package] [package]
name = "strafesnet_texture_loader" name = "strafesnet_deferred_loader"
version = "0.1.0" version = "0.2.0"
edition = "2021" edition = "2021"
# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html # See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html

View File

@ -1,6 +1,6 @@
use std::io::Read; use std::io::Read;
use std::collections::HashMap; use std::collections::HashMap;
use crate::texture_loader::{Texture,Textures}; use crate::texture::{Texture,Textures};
use strafesnet_common::model::TextureId; use strafesnet_common::model::TextureId;
#[derive(Hash,Eq,PartialEq)] #[derive(Hash,Eq,PartialEq)]
@ -28,10 +28,10 @@ impl std::fmt::Display for RobloxAssetIdParseErr{
} }
impl std::error::Error for RobloxAssetIdParseErr{} impl std::error::Error for RobloxAssetIdParseErr{}
pub struct TextureLoader{ pub struct Loader{
texture_names:HashMap<RobloxAssetId,TextureId>, texture_names:HashMap<RobloxAssetId,TextureId>,
} }
impl TextureLoader{ impl Loader{
pub fn new()->Self{ pub fn new()->Self{
Self{ Self{
texture_names:HashMap::new(), texture_names:HashMap::new(),
@ -39,14 +39,12 @@ impl TextureLoader{
} }
} }
impl crate::texture_loader::TextureLoaderTrait for TextureLoader{ impl Loader{
type AcquireError=RobloxAssetIdParseErr; pub fn acquire_texture_id(&mut self,name:&str)->Result<TextureId,RobloxAssetIdParseErr>{
type LoadError=std::io::Error;
fn acquire_id(&mut self,name:&str)->Result<TextureId,Self::AcquireError>{
let texture_id=TextureId::new(self.texture_names.len() as u32); let texture_id=TextureId::new(self.texture_names.len() as u32);
Ok(*self.texture_names.entry(name.parse::<RobloxAssetId>()?).or_insert(texture_id)) Ok(*self.texture_names.entry(name.parse::<RobloxAssetId>()?).or_insert(texture_id))
} }
fn load(&self)->Result<Textures,Self::LoadError>{ pub fn load_textures(&self)->Result<Textures,std::io::Error>{
let mut texture_data=vec![Vec::<u8>::new();self.texture_names.len()]; let mut texture_data=vec![Vec::<u8>::new();self.texture_names.len()];
for (texture_name,texture_id) in &self.texture_names{ for (texture_name,texture_id) in &self.texture_names{
let path=std::path::PathBuf::from(format!("textures/{}.dds",texture_name.0)); let path=std::path::PathBuf::from(format!("textures/{}.dds",texture_name.0));

View File

@ -5,17 +5,17 @@ mod roblox;
#[cfg(feature="source")] #[cfg(feature="source")]
mod source; mod source;
pub mod texture_loader; pub mod texture;
#[cfg(feature="legacy")] #[cfg(feature="legacy")]
pub fn legacy()->texture_loader::TextureLoader{ pub fn legacy()->legacy::Loader{
texture_loader::TextureLoader::Legacy(legacy::TextureLoader::new()) legacy::Loader::new()
} }
#[cfg(feature="roblox")] #[cfg(feature="roblox")]
pub fn roblox()->texture_loader::TextureLoader{ pub fn roblox()->roblox::Loader{
texture_loader::TextureLoader::Roblox(roblox::TextureLoader::new()) roblox::Loader::new()
} }
#[cfg(feature="source")] #[cfg(feature="source")]
pub fn source()->texture_loader::TextureLoader{ pub fn source()->source::Loader{
texture_loader::TextureLoader::Source(source::TextureLoader::new()) source::Loader::new()
} }

29
src/texture.rs Normal file
View File

@ -0,0 +1,29 @@
use strafesnet_common::model::TextureId;
pub enum Texture{
ImageDDS(Vec<u8>),
}
impl AsRef<[u8]> for Texture{
fn as_ref(&self)->&[u8]{
match self{
Texture::ImageDDS(data)=>data.as_ref(),
}
}
}
pub struct Textures{
textures:Vec<Texture>,
}
impl Textures{
pub(crate) const fn new(textures:Vec<Texture>)->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<Item=(TextureId,Texture)>{
self.textures.into_iter().enumerate().map(|(texture_id,texture)|(TextureId::new(texture_id as u32),texture))
}
}

View File

@ -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<u8>),
}
impl AsRef<[u8]> for Texture{
fn as_ref(&self)->&[u8]{
match self{
Texture::ImageDDS(data)=>data.as_ref(),
}
}
}
pub struct Textures{
textures:Vec<Texture>,
}
impl Textures{
pub(crate) const fn new(textures:Vec<Texture>)->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<Item=(TextureId,Texture)>{
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<TextureId,Self::AcquireError>;
fn load(&self)->Result<Textures,Self::LoadError>;
}
#[derive(Debug)]
pub enum TextureLoaderAcquireError{
#[cfg(feature="legacy")]
Legacy(<crate::legacy::TextureLoader as TextureLoaderTrait>::AcquireError),
#[cfg(feature="roblox")]
Roblox(<crate::roblox::TextureLoader as TextureLoaderTrait>::AcquireError),
#[cfg(feature="source")]
Source(<crate::source::TextureLoader as TextureLoaderTrait>::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(<crate::legacy::TextureLoader as TextureLoaderTrait>::LoadError),
#[cfg(feature="roblox")]
Roblox(<crate::roblox::TextureLoader as TextureLoaderTrait>::LoadError),
#[cfg(feature="source")]
Source(<crate::source::TextureLoader as TextureLoaderTrait>::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<TextureId,Self::AcquireError>{
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<Textures,Self::LoadError>{
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!(),
}
}
}