v1
This commit is contained in:
parent
aba7fbdb8a
commit
2f70b11abd
34
src/legacy.rs
Normal file
34
src/legacy.rs
Normal file
@ -0,0 +1,34 @@
|
||||
use std::io::Read;
|
||||
use std::collections::HashMap;
|
||||
use crate::texture_loader::{Texture,Textures};
|
||||
use strafesnet_common::model::TextureId;
|
||||
|
||||
pub struct TextureLoader{
|
||||
texture_names:HashMap<String,TextureId>,
|
||||
}
|
||||
impl TextureLoader{
|
||||
pub fn new()->Self{
|
||||
Self{
|
||||
texture_names:HashMap::new(),
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
impl crate::texture_loader::TextureLoaderTrait for TextureLoader{
|
||||
type Error=std::io::Error;
|
||||
fn acquire_id(&mut self,name:&str)->TextureId{
|
||||
let texture_id=TextureId::new(self.texture_names.len() as u32);
|
||||
*self.texture_names.entry(name.to_owned()).or_insert(texture_id)
|
||||
}
|
||||
fn load(&self)->Result<Textures,Self::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.as_str()));
|
||||
if let Ok(mut file)=std::fs::File::open(path){
|
||||
//TODO: parallel
|
||||
file.read_to_end(texture_data.get_mut(texture_id.get() as usize).unwrap())?;
|
||||
}
|
||||
}
|
||||
Ok(Textures::new(texture_data.into_iter().map(Texture::ImageDDS).collect()))
|
||||
}
|
||||
}
|
21
src/lib.rs
Normal file
21
src/lib.rs
Normal file
@ -0,0 +1,21 @@
|
||||
#[cfg(feature="legacy")]
|
||||
mod legacy;
|
||||
#[cfg(feature="roblox")]
|
||||
mod roblox;
|
||||
#[cfg(feature="source")]
|
||||
mod source;
|
||||
|
||||
pub mod texture_loader;
|
||||
|
||||
#[cfg(feature="legacy")]
|
||||
pub fn legacy()->texture_loader::TextureLoader{
|
||||
texture_loader::TextureLoader::Legacy(legacy::TextureLoader::new())
|
||||
}
|
||||
#[cfg(feature="roblox")]
|
||||
pub fn roblox()->texture_loader::TextureLoader{
|
||||
texture_loader::TextureLoader::Roblox(roblox::TextureLoader::new())
|
||||
}
|
||||
#[cfg(feature="source")]
|
||||
pub fn source()->texture_loader::TextureLoader{
|
||||
texture_loader::TextureLoader::Source(source::TextureLoader::new())
|
||||
}
|
0
src/roblox.rs
Normal file
0
src/roblox.rs
Normal file
0
src/source.rs
Normal file
0
src/source.rs
Normal file
71
src/texture_loader.rs
Normal file
71
src/texture_loader.rs
Normal file
@ -0,0 +1,71 @@
|
||||
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{
|
||||
Image(Vec<u8>),
|
||||
ImageDDS(Vec<u8>),
|
||||
}
|
||||
|
||||
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 trait TextureLoaderTrait{
|
||||
type Error;
|
||||
//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)->TextureId;
|
||||
fn load(&self)->Result<Textures,Self::Error>;
|
||||
}
|
||||
|
||||
pub enum TextureLoaderError{
|
||||
#[cfg(feature="legacy")]
|
||||
Legacy(<crate::legacy::TextureLoader as TextureLoaderTrait>::Error),
|
||||
#[cfg(feature="roblox")]
|
||||
Roblox(<crate::roblox::TextureLoader as TextureLoaderTrait>::Error),
|
||||
#[cfg(feature="source")]
|
||||
Source(<crate::source::TextureLoader as TextureLoaderTrait>::Error),
|
||||
}
|
||||
|
||||
impl TextureLoaderTrait for TextureLoader{
|
||||
type Error=TextureLoaderError;
|
||||
fn acquire_id(&mut self,name:&str)->TextureId{
|
||||
match self{
|
||||
#[cfg(feature="legacy")]
|
||||
TextureLoader::Legacy(loader)=>loader.acquire_id(name),
|
||||
#[cfg(feature="roblox")]
|
||||
TextureLoader::Roblox(loader)=>loader.acquire_id(name),
|
||||
#[cfg(feature="source")]
|
||||
TextureLoader::Source(loader)=>loader.acquire_id(name),
|
||||
_=>unreachable!(),
|
||||
}
|
||||
}
|
||||
fn load(&self)->Result<Textures,Self::Error>{
|
||||
match self{
|
||||
#[cfg(feature="legacy")]
|
||||
TextureLoader::Legacy(loader)=>loader.load().map_err(TextureLoaderError::Legacy),
|
||||
#[cfg(feature="roblox")]
|
||||
TextureLoader::Roblox(loader)=>loader.load().map_err(TextureLoaderError::Roblox),
|
||||
#[cfg(feature="source")]
|
||||
TextureLoader::Source(loader)=>loader.load().map_err(TextureLoaderError::Source),
|
||||
_=>unreachable!(),
|
||||
}
|
||||
}
|
||||
}
|
Loading…
Reference in New Issue
Block a user