strafe-client/src/file.rs

84 lines
2.4 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-14 12:28:36 +00:00
pub fn load<P:AsRef<std::path::Path>>(path:P)->Result<(strafesnet_common::map::CompleteMap,strafesnet_deferred_loader::texture::Textures),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)?{
DataStructure::Roblox(something)=>{
let mut legacy_loader=strafesnet_deferred_loader::roblox_legacy();
2024-02-14 05:56:11 +00:00
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)?;
2024-02-14 05:56:57 +00:00
Ok((map,textures))
},
DataStructure::Source(something)=>{
let mut legacy_loader=strafesnet_deferred_loader::source_legacy();
2024-02-14 05:56:57 +00:00
let (texture_loader,mesh_loader)=legacy_loader.get_inner_mut();
2024-02-14 05:56:57 +00:00
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)?;
2024-02-14 05:56:57 +00:00
2024-02-14 05:56:11 +00:00
Ok((map,textures))
},
}
}