Files
roblox-bot-player/native-player/src/file.rs

69 lines
2.0 KiB
Rust

use std::io::Read;
#[expect(dead_code)]
#[derive(Debug)]
pub enum ReadError{
StrafesNET(strafesnet_snf::Error),
StrafesNETMap(strafesnet_snf::map::Error),
RobloxBot(strafesnet_roblox_bot_file::v0::Error),
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 ReadFormat{
SNFM(strafesnet_common::map::CompleteMap),
QBOT(strafesnet_roblox_bot_file::v0::Block),
}
pub fn read<R:Read+std::io::Seek>(input:R)->Result<ReadFormat,ReadError>{
let mut buf=std::io::BufReader::new(input);
let peek=std::io::BufRead::fill_buf(&mut buf).map_err(ReadError::Io)?[0..4].to_owned();
// reading the entire file is way faster than round tripping the disk constantly
let mut entire_file=Vec::new();
buf.read_to_end(&mut entire_file).map_err(ReadError::Io)?;
let cursor=std::io::Cursor::new(entire_file);
match peek.as_slice(){
b"SNFM"=>Ok(ReadFormat::SNFM(
strafesnet_snf::read_map(cursor).map_err(ReadError::StrafesNET)?
.into_complete_map().map_err(ReadError::StrafesNETMap)?
)),
b"qbot"=>Ok(ReadFormat::QBOT(
strafesnet_roblox_bot_file::v0::read_all_to_block(cursor).map_err(ReadError::RobloxBot)?
)),
_=>Err(ReadError::UnknownFileFormat),
}
}
#[expect(dead_code)]
#[derive(Debug)]
pub enum LoadError{
ReadError(ReadError),
File(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 enum LoadFormat{
Map(strafesnet_common::map::CompleteMap),
Bot(strafesnet_roblox_bot_file::v0::Block),
}
pub fn load<P:AsRef<std::path::Path>>(path:P)->Result<LoadFormat,LoadError>{
//blocking because it's simpler...
let file=std::fs::File::open(path).map_err(LoadError::File)?;
match read(file).map_err(LoadError::ReadError)?{
ReadFormat::QBOT(bot)=>Ok(LoadFormat::Bot(bot)),
ReadFormat::SNFM(map)=>Ok(LoadFormat::Map(map)),
}
}