use std::io::Read; use std::collections::HashMap; use crate::texture_loader::{Texture,Textures}; use strafesnet_common::model::TextureId; #[derive(Hash,Eq,PartialEq)] struct RobloxAssetId(u64); #[derive(Debug)] pub struct RobloxAssetIdParseErr(String); impl std::str::FromStr for RobloxAssetId{ type Err=RobloxAssetIdParseErr; fn from_str(s:&str)->Result{ let regman=lazy_regex::regex!(r"(\d+)$"); if let Some(captures)=regman.captures(s){ if captures.len()==2{//captures[0] is all captures concatenated, and then each individual capture if let Ok(id)=captures[0].parse::(){ return Ok(Self(id)); } } } Err(RobloxAssetIdParseErr(s.to_owned())) } } impl std::fmt::Display for RobloxAssetIdParseErr{ fn fmt(&self,f:&mut std::fmt::Formatter<'_>)->std::fmt::Result{ write!(f,"{self:?}") } } impl std::error::Error for RobloxAssetIdParseErr{} 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 AcquireError=RobloxAssetIdParseErr; type LoadError=std::io::Error; fn acquire_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{ 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)); 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())) } }