strafe-client/src/file.rs

64 lines
1.7 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),
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),
}
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)?)),
_=>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::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))
},
}
}