From 2f70b11abde1969acd1724c6406935a73fb7e9f5 Mon Sep 17 00:00:00 2001 From: Quaternions Date: Tue, 13 Feb 2024 00:13:04 -0800 Subject: [PATCH] v1 --- src/legacy.rs | 34 +++++++++++++++++++++ src/lib.rs | 21 +++++++++++++ src/roblox.rs | 0 src/source.rs | 0 src/texture_loader.rs | 71 +++++++++++++++++++++++++++++++++++++++++++ 5 files changed, 126 insertions(+) create mode 100644 src/legacy.rs create mode 100644 src/lib.rs create mode 100644 src/roblox.rs create mode 100644 src/source.rs create mode 100644 src/texture_loader.rs diff --git a/src/legacy.rs b/src/legacy.rs new file mode 100644 index 0000000..56ee92a --- /dev/null +++ b/src/legacy.rs @@ -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, +} +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{ + 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.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())) + } +} \ No newline at end of file diff --git a/src/lib.rs b/src/lib.rs new file mode 100644 index 0000000..136fdf2 --- /dev/null +++ b/src/lib.rs @@ -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()) +} \ No newline at end of file diff --git a/src/roblox.rs b/src/roblox.rs new file mode 100644 index 0000000..e69de29 diff --git a/src/source.rs b/src/source.rs new file mode 100644 index 0000000..e69de29 diff --git a/src/texture_loader.rs b/src/texture_loader.rs new file mode 100644 index 0000000..90f1742 --- /dev/null +++ b/src/texture_loader.rs @@ -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), + ImageDDS(Vec), +} + +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 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; +} + +pub enum TextureLoaderError{ + #[cfg(feature="legacy")] + Legacy(::Error), + #[cfg(feature="roblox")] + Roblox(::Error), + #[cfg(feature="source")] + Source(::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{ + 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!(), + } + } +} \ No newline at end of file