strafe-client/src/file.rs

84 lines
2.4 KiB
Rust

use std::io::Read;
#[derive(Debug)]
pub enum ReadError{
Roblox(strafesnet_rbx_loader::ReadError),
Source(strafesnet_bsp_loader::ReadError),
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),
Source(strafesnet_bsp_loader::Bsp)
}
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)?)),
b"VBSP"=>Ok(DataStructure::Source(strafesnet_bsp_loader::read(buf).map_err(ReadError::Source)?)),
_=>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{}
pub fn load(path:&std::path::Path)->Result<(strafesnet_common::map::CompleteMap,strafesnet_deferred_loader::texture::Textures),LoadError>{
//blocking because it's simpler...
let file=std::fs::File::open(path).map_err(LoadError::File)?;
match read(file).map_err(LoadError::ReadError)?{
DataStructure::Roblox(something)=>{
let mut legacy_loader=strafesnet_deferred_loader::roblox_legacy();
let map=strafesnet_rbx_loader::convert(&something,|name|{
match legacy_loader.acquire_texture_id(name){
Ok(texture_id)=>Some(texture_id),
Err(e)=>{
println!("legacy_loader error: {e}");
None
},
}
});
let textures=legacy_loader.load_textures().map_err(LoadError::Io)?;
Ok((map,textures))
},
DataStructure::Source(something)=>{
let mut legacy_loader=strafesnet_deferred_loader::source_legacy();
let (texture_loader,mesh_loader)=legacy_loader.get_inner_mut();
let map=strafesnet_bsp_loader::convert(
&something,
//acquire_texture_id
|name|texture_loader.acquire_texture_id(name),
//acquire_mesh_id
|name|mesh_loader.acquire_mesh_id(name),
);
let textures=legacy_loader.load_textures().map_err(LoadError::Io)?;
Ok((map,textures))
},
}
}