strafe-project/src/roblox_legacy.rs

58 lines
1.8 KiB
Rust
Raw Normal View History

2024-02-13 08:13:04 +00:00
use std::io::Read;
use std::collections::HashMap;
2024-02-14 03:48:47 +00:00
use crate::texture::{Texture,Textures};
2024-02-13 08:13:04 +00:00
use strafesnet_common::model::TextureId;
2024-02-13 08:48:30 +00:00
#[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<Self,Self::Err>{
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::<u64>(){
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{}
2024-02-14 03:48:47 +00:00
pub struct Loader{
2024-02-13 08:48:30 +00:00
texture_names:HashMap<RobloxAssetId,TextureId>,
2024-02-13 08:13:04 +00:00
}
2024-02-14 03:48:47 +00:00
impl Loader{
2024-02-13 08:13:04 +00:00
pub fn new()->Self{
Self{
texture_names:HashMap::new(),
}
}
}
2024-02-14 03:48:47 +00:00
impl Loader{
pub fn acquire_texture_id(&mut self,name:&str)->Result<TextureId,RobloxAssetIdParseErr>{
2024-02-13 08:13:04 +00:00
let texture_id=TextureId::new(self.texture_names.len() as u32);
2024-02-13 08:48:30 +00:00
Ok(*self.texture_names.entry(name.parse::<RobloxAssetId>()?).or_insert(texture_id))
2024-02-13 08:13:04 +00:00
}
2024-02-14 03:48:47 +00:00
pub fn load_textures(&self)->Result<Textures,std::io::Error>{
2024-02-13 08:13:04 +00:00
let mut texture_data=vec![Vec::<u8>::new();self.texture_names.len()];
for (texture_name,texture_id) in &self.texture_names{
2024-02-13 08:48:30 +00:00
let path=std::path::PathBuf::from(format!("textures/{}.dds",texture_name.0));
2024-02-13 08:13:04 +00:00
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()))
}
}