strafe-project/lib/deferred_loader/src/source_legacy.rs

103 lines
3.5 KiB
Rust
Raw Normal View History

2024-02-14 03:48:53 +00:00
use std::io::Read;
use std::collections::HashMap;
2024-02-15 07:45:16 +00:00
use crate::valve_mesh;
use crate::texture::{Texture,RenderConfigs};
use strafesnet_common::model::{MeshId,TextureId,RenderConfig,RenderConfigId};
2024-02-14 03:48:53 +00:00
2024-02-15 07:45:16 +00:00
pub struct RenderConfigLoader{
texture_count:u32,
render_configs:Vec<RenderConfig>,
texture_paths:HashMap<Option<Box<str>>,RenderConfigId>,
2024-02-14 03:48:53 +00:00
}
2024-02-15 07:45:16 +00:00
impl RenderConfigLoader{
pub fn acquire_render_config_id(&mut self,name:Option<&str>)->RenderConfigId{
let render_id=RenderConfigId::new(self.texture_paths.len() as u32);
*self.texture_paths.entry(name.map(Into::into)).or_insert_with(||{
//create the render config.
let render_config=if name.is_some(){
let render_config=RenderConfig::texture(TextureId::new(self.texture_count));
self.texture_count+=1;
render_config
}else{
RenderConfig::default()
};
self.render_configs.push(render_config);
render_id
})
2024-02-14 03:48:53 +00:00
}
}
pub struct MeshLoader{
2024-02-14 22:45:10 +00:00
mesh_paths:HashMap<Box<str>,MeshId>,
2024-02-14 03:48:53 +00:00
}
impl MeshLoader{
pub fn acquire_mesh_id(&mut self,name:&str)->MeshId{
2024-02-15 07:45:16 +00:00
let mesh_id=MeshId::new(self.mesh_paths.len() as u32);
*self.mesh_paths.entry(name.into()).or_insert(mesh_id)
}
//load_meshes should look like load_textures
pub fn load_meshes(&mut self,bsp:&vbsp::Bsp)->valve_mesh::Meshes{
let mut mesh_data=vec![None;self.mesh_paths.len()];
for (mesh_path,mesh_id) in &self.mesh_paths{
let mesh_path_lower=mesh_path.to_lowercase();
//.mdl, .vvd, .dx90.vtx
2024-02-18 06:05:03 +00:00
let path=std::path::PathBuf::from(mesh_path_lower.as_str());
2024-02-15 07:45:16 +00:00
let mut vvd_path=path.clone();
let mut vtx_path=path.clone();
vvd_path.set_extension("vvd");
vtx_path.set_extension("dx90.vtx");
match (bsp.pack.get(mesh_path_lower.as_str()),bsp.pack.get(vvd_path.as_os_str().to_str().unwrap()),bsp.pack.get(vtx_path.as_os_str().to_str().unwrap())){
(Ok(Some(mdl_file)),Ok(Some(vvd_file)),Ok(Some(vtx_file)))=>{
mesh_data[mesh_id.get() as usize]=Some(valve_mesh::ModelData{
mdl:valve_mesh::MdlData::new(mdl_file),
vtx:valve_mesh::VtxData::new(vtx_file),
vvd:valve_mesh::VvdData::new(vvd_file),
});
},
_=>println!("no model name={}",mesh_path),
}
}
valve_mesh::Meshes::new(mesh_data)
2024-02-14 03:48:53 +00:00
}
}
pub struct Loader{
2024-02-15 07:45:16 +00:00
render_config_loader:RenderConfigLoader,
2024-02-14 03:48:53 +00:00
mesh_loader:MeshLoader,
}
impl Loader{
pub fn new()->Self{
Self{
2024-02-15 07:45:16 +00:00
render_config_loader:RenderConfigLoader{
texture_count:0,
texture_paths:HashMap::new(),
render_configs:Vec::new(),
},
2024-02-14 03:48:53 +00:00
mesh_loader:MeshLoader{mesh_paths:HashMap::new()},
}
}
2024-02-15 07:45:16 +00:00
pub fn get_inner_mut(&mut self)->(&mut RenderConfigLoader,&mut MeshLoader){
(&mut self.render_config_loader,&mut self.mesh_loader)
2024-02-14 03:48:53 +00:00
}
2024-02-15 07:45:16 +00:00
pub fn into_render_configs(mut self)->Result<RenderConfigs,std::io::Error>{
let mut sorted_textures=vec![None;self.render_config_loader.texture_count as usize];
for (texture_path,render_config_id) in self.render_config_loader.texture_paths{
let render_config=self.render_config_loader.render_configs.get_mut(render_config_id.get() as usize).unwrap();
if let (Some(texture_path),Some(texture_id))=(texture_path,render_config.texture){
2024-02-18 06:05:03 +00:00
if let Ok(mut file)=std::fs::File::open(format!("textures/{}.dds",texture_path)){
2024-02-15 07:45:16 +00:00
//TODO: parallel
let mut data=Vec::<u8>::new();
file.read_to_end(&mut data)?;
sorted_textures[texture_id.get() as usize]=Some(Texture::ImageDDS(data));
}else{
//texture failed to load
render_config.texture=None;
}
2024-02-14 08:35:31 +00:00
}
}
2024-02-15 07:45:16 +00:00
Ok(RenderConfigs::new(
sorted_textures,
self.render_config_loader.render_configs,
))
2024-02-14 03:48:53 +00:00
}
}