strafe-client/src/file.rs

111 lines
3.3 KiB
Rust
Raw Normal View History

2024-02-14 05:56:11 +00:00
use std::io::Read;
#[derive(Debug)]
pub enum ReadError{
Roblox(strafesnet_rbx_loader::ReadError),
2024-02-14 05:56:57 +00:00
Source(strafesnet_bsp_loader::ReadError),
2024-02-14 05:56:11 +00:00
Io(std::io::Error),
UnknownFileFormat,
}
impl std::fmt::Display for ReadError{
fn fmt(&self,f:&mut std::fmt::Formatter<'_>)->std::fmt::Result{
write!(f,"{self:?}")
}
}
impl std::error::Error for ReadError{}
pub enum DataStructure{
Roblox(strafesnet_rbx_loader::Dom),
2024-02-14 05:56:57 +00:00
Source(strafesnet_bsp_loader::Bsp)
2024-02-14 05:56:11 +00:00
}
pub fn read<R:Read>(input:R)->Result<DataStructure,ReadError>{
let mut buf=std::io::BufReader::new(input);
let peek=std::io::BufRead::fill_buf(&mut buf).map_err(ReadError::Io)?;
match &peek[0..4]{
b"<rob"=>Ok(DataStructure::Roblox(strafesnet_rbx_loader::read(buf).map_err(ReadError::Roblox)?)),
2024-02-14 05:56:57 +00:00
b"VBSP"=>Ok(DataStructure::Source(strafesnet_bsp_loader::read(buf).map_err(ReadError::Source)?)),
2024-02-14 05:56:11 +00:00
_=>Err(ReadError::UnknownFileFormat),
}
}
#[derive(Debug)]
pub enum LoadError{
ReadError(ReadError),
File(std::io::Error),
Io(std::io::Error),
}
impl std::fmt::Display for LoadError{
fn fmt(&self,f:&mut std::fmt::Formatter<'_>)->std::fmt::Result{
write!(f,"{self:?}")
}
}
impl std::error::Error for LoadError{}
2024-02-15 07:19:15 +00:00
pub fn load<P:AsRef<std::path::Path>>(path:P)->Result<strafesnet_common::map::CompleteMap,LoadError>{
2024-02-14 05:56:11 +00:00
//blocking because it's simpler...
let file=std::fs::File::open(path).map_err(LoadError::File)?;
match read(file).map_err(LoadError::ReadError)?{
2024-02-15 07:19:15 +00:00
DataStructure::Roblox(dom)=>{
let mut loader=strafesnet_deferred_loader::roblox_legacy();
let map_step1=strafesnet_rbx_loader::convert(
&dom,
|name|loader.acquire_render_config_id(name)
);
let (textures,render_configs)=loader.into_render_configs().map_err(LoadError::Io)?.consume();
let map=map_step1.add_render_configs_and_textures(
render_configs.into_iter(),
textures.into_iter().map(|(texture_id,texture)|
(texture_id,match texture{
strafesnet_deferred_loader::texture::Texture::ImageDDS(data)=>data,
})
)
);
Ok(map)
2024-02-14 05:56:57 +00:00
},
2024-02-15 07:19:15 +00:00
DataStructure::Source(bsp)=>{
let mut loader=strafesnet_deferred_loader::source_legacy();
2024-02-14 05:56:57 +00:00
2024-02-15 07:19:15 +00:00
let (texture_loader,mesh_loader)=loader.get_inner_mut();
2024-02-14 05:56:57 +00:00
2024-02-15 07:19:15 +00:00
let map_step1=strafesnet_bsp_loader::convert(
&bsp,
//acquire_render_config_id
&mut |name|texture_loader.acquire_render_config_id(name),
2024-02-14 05:56:57 +00:00
//acquire_mesh_id
|name|mesh_loader.acquire_mesh_id(name),
);
2024-02-15 07:19:15 +00:00
let prop_meshes=mesh_loader.load_meshes(&bsp.as_ref());
let map_step2=map_step1.add_prop_meshes(
//the type conflagulator 9000
prop_meshes.into_iter().map(|(mesh_id,loader_model)|
(mesh_id,strafesnet_bsp_loader::data::ModelData{
mdl:strafesnet_bsp_loader::data::MdlData::new(loader_model.mdl.get()),
vtx:strafesnet_bsp_loader::data::VtxData::new(loader_model.vtx.get()),
vvd:strafesnet_bsp_loader::data::VvdData::new(loader_model.vvd.get()),
})
),
&mut |name|texture_loader.acquire_render_config_id(name),
);
let (textures,render_configs)=loader.into_render_configs().map_err(LoadError::Io)?.consume();
let map=map_step2.add_render_configs_and_textures(
render_configs.into_iter(),
textures.into_iter().map(|(texture_id,texture)|
(texture_id,match texture{
strafesnet_deferred_loader::texture::Texture::ImageDDS(data)=>data,
})
),
);
2024-02-14 05:56:57 +00:00
2024-02-15 07:19:15 +00:00
Ok(map)
2024-02-14 05:56:11 +00:00
},
}
}