refactor api
This commit is contained in:
parent
c182e46001
commit
2c7ced7151
4
Cargo.lock
generated
4
Cargo.lock
generated
@ -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",
|
||||
|
@ -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
|
||||
|
@ -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<RobloxAssetId,TextureId>,
|
||||
}
|
||||
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<TextureId,Self::AcquireError>{
|
||||
impl Loader{
|
||||
pub fn acquire_texture_id(&mut self,name:&str)->Result<TextureId,RobloxAssetIdParseErr>{
|
||||
let texture_id=TextureId::new(self.texture_names.len() as u32);
|
||||
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()];
|
||||
for (texture_name,texture_id) in &self.texture_names{
|
||||
let path=std::path::PathBuf::from(format!("textures/{}.dds",texture_name.0));
|
||||
|
14
src/lib.rs
14
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()
|
||||
}
|
29
src/texture.rs
Normal file
29
src/texture.rs
Normal 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))
|
||||
}
|
||||
}
|
@ -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!(),
|
||||
}
|
||||
}
|
||||
}
|
Loading…
Reference in New Issue
Block a user