use id + new knowledge + don't need u64

This commit is contained in:
Quaternions 2024-02-07 01:01:23 -08:00
parent 0532b47fec
commit 0c439bf2e6
6 changed files with 68 additions and 37 deletions

32
Cargo.lock generated
View File

@ -29,14 +29,14 @@ dependencies = [
"owo-colors", "owo-colors",
"proc-macro2", "proc-macro2",
"quote", "quote",
"syn", "syn 1.0.109",
] ]
[[package]] [[package]]
name = "bytemuck" name = "bytemuck"
version = "1.14.0" version = "1.14.2"
source = "registry+https://github.com/rust-lang/crates.io-index" source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "374d28ec25809ee0e23827c2ab573d729e293f281dfe393500e7ad618baa61c6" checksum = "ea31d69bda4949c1c1562c1e6f042a1caefac98cdc8a298260a2ff41c1e2d42b"
[[package]] [[package]]
name = "either" name = "either"
@ -50,6 +50,16 @@ version = "0.25.0"
source = "registry+https://github.com/rust-lang/crates.io-index" source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "151665d9be52f9bb40fc7966565d39666f2d1e69233571b71b87791c7e0528b3" checksum = "151665d9be52f9bb40fc7966565d39666f2d1e69233571b71b87791c7e0528b3"
[[package]]
name = "id"
version = "0.1.0"
source = "git+https://git.itzana.me/Quaternions/id?rev=1f710976cc786c8853dab73d6e1cee53158deeb0#1f710976cc786c8853dab73d6e1cee53158deeb0"
dependencies = [
"proc-macro2",
"quote",
"syn 2.0.48",
]
[[package]] [[package]]
name = "owo-colors" name = "owo-colors"
version = "3.5.0" version = "3.5.0"
@ -58,9 +68,9 @@ checksum = "c1b04fb49957986fdce4d6ee7a65027d55d4b6d2265e5848bbb507b58ccfdb6f"
[[package]] [[package]]
name = "proc-macro2" name = "proc-macro2"
version = "1.0.76" version = "1.0.78"
source = "registry+https://github.com/rust-lang/crates.io-index" source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "95fc56cda0b5c3325f5fbbd7ff9fda9e02bb00bb3dac51252d2f1bfa1cb8cc8c" checksum = "e2422ad645d89c99f8f3e6b88a9fdeca7fabeac836b1002371c4367c8f984aae"
dependencies = [ dependencies = [
"unicode-ident", "unicode-ident",
] ]
@ -87,6 +97,7 @@ name = "strafesnet_snf"
version = "0.1.0" version = "0.1.0"
dependencies = [ dependencies = [
"binrw", "binrw",
"id",
"strafesnet_common", "strafesnet_common",
] ]
@ -101,6 +112,17 @@ dependencies = [
"unicode-ident", "unicode-ident",
] ]
[[package]]
name = "syn"
version = "2.0.48"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "0f3531638e407dfc0814761abb7c00a5b54992b849452a0646b7f65c9f770f3f"
dependencies = [
"proc-macro2",
"quote",
"unicode-ident",
]
[[package]] [[package]]
name = "unicode-ident" name = "unicode-ident"
version = "1.0.12" version = "1.0.12"

View File

@ -7,4 +7,5 @@ edition = "2021"
[dependencies] [dependencies]
binrw = "0.13.3" binrw = "0.13.3"
id = { git = "https://git.itzana.me/Quaternions/id", rev = "1f710976cc786c8853dab73d6e1cee53158deeb0" }
strafesnet_common = { git = "https://git.itzana.me/StrafesNET/common", rev = "5ee826d9487b5e2bea4b3cf99a68ce9a95d72f72" } strafesnet_common = { git = "https://git.itzana.me/StrafesNET/common", rev = "5ee826d9487b5e2bea4b3cf99a68ce9a95d72f72" }

View File

@ -3,7 +3,7 @@ use binrw::{BinReaderExt, binrw};
pub enum Error{ pub enum Error{
InvalidHeader, InvalidHeader,
InvalidSegment(binrw::Error), InvalidSegment(binrw::Error),
InvalidSegmentId(u64), InvalidSegmentId(SegmentId),
File(crate::file::Error), File(crate::file::Error),
} }
@ -64,7 +64,8 @@ mod simulation{
#[binrw] #[binrw]
#[brw(little)] #[brw(little)]
struct SegmentId(u64); #[derive(Clone,Copy,id::Id)]
pub struct SegmentId(u32);
#[binrw] #[binrw]
#[brw(little)] #[brw(little)]
@ -86,9 +87,9 @@ impl<R:BinReaderExt> StreamableBot<R>{
Err(Error::InvalidHeader) Err(Error::InvalidHeader)
} }
pub fn load_segment(&mut self,segment_id:SegmentId)->Result<Segment,Error>{ pub fn load_segment(&mut self,segment_id:SegmentId)->Result<Segment,Error>{
let block_id=*self.segment_id_to_block_id.get(segment_id.0 as usize).ok_or(Error::InvalidSegmentId(segment_id.0))?; let block_id=*self.segment_id_to_block_id.get(segment_id.get() as usize).ok_or(Error::InvalidSegmentId(segment_id))?;
let mut block=self.file.take_block(block_id).map_err(|e|Error::File(e))?; let mut block=self.file.take_block(block_id).map_err(Error::File)?;
let segment=block.read_le().map_err(|e|Error::InvalidSegment(e))?; let segment=block.read_le().map_err(Error::InvalidSegment)?;
Ok(segment) Ok(segment)
} }
} }

View File

@ -5,7 +5,7 @@ use binrw::{binrw, BinReaderExt, io::TakeSeekExt};
pub enum Error{ pub enum Error{
InvalidHeader(binrw::Error), InvalidHeader(binrw::Error),
UnexpectedEOF, UnexpectedEOF,
InvalidBlockId(u64), InvalidBlockId(BlockId),
Seek(std::io::Error), Seek(std::io::Error),
} }
@ -58,14 +58,16 @@ struct Header{
priming:u64, priming:u64,
/// uuid for this file /// uuid for this file
resource:u128, resource:u128,
#[bw(try_calc(u64::try_from(block_location.len()-1)))] #[bw(try_calc(u32::try_from(block_location.len()-1)))]
block_count:u64, block_count:u32,
#[br(count=block_count+1)] #[br(count=block_count+1)]
block_location:Vec<u64>, block_location:Vec<u64>,
} }
#[derive(Clone,Copy,Hash,Eq,PartialEq)] #[binrw]
pub struct BlockId(u64); #[brw(little)]
#[derive(Clone,Copy,Hash,id::Id,Eq,PartialEq)]
pub struct BlockId(u32);
pub(crate) struct File<R:BinReaderExt>{ pub(crate) struct File<R:BinReaderExt>{
header:Header, header:Header,
@ -76,17 +78,17 @@ pub(crate) struct File<R:BinReaderExt>{
impl<R:BinReaderExt> File<R>{ impl<R:BinReaderExt> File<R>{
pub(crate) fn new(mut input:R)->Result<File<R>,Error>{ pub(crate) fn new(mut input:R)->Result<File<R>,Error>{
Ok(File{ Ok(File{
header:input.read_le().map_err(|e|Error::InvalidHeader(e))?, header:input.read_le().map_err(Error::InvalidHeader)?,
data:input, data:input,
}) })
} }
pub(crate) fn take_block(&mut self,block_id:BlockId)->Result<binrw::io::TakeSeek<&mut R>,Error>{ pub(crate) fn take_block(&mut self,block_id:BlockId)->Result<binrw::io::TakeSeek<&mut R>,Error>{
if self.header.block_location.len() as u64<=block_id.0{ if self.header.block_location.len() as u32<=block_id.get(){
return Err(Error::InvalidBlockId(block_id.0)) return Err(Error::InvalidBlockId(block_id))
} }
let block_start=self.header.block_location[block_id.0 as usize]; let block_start=self.header.block_location[block_id.get() as usize];
let block_end=self.header.block_location[block_id.0 as usize+1]; let block_end=self.header.block_location[block_id.get() as usize+1];
self.data.seek(std::io::SeekFrom::Start(block_start)).map_err(|e|Error::Seek(e))?; self.data.seek(std::io::SeekFrom::Start(block_start)).map_err(Error::Seek)?;
Ok((&mut self.data).take_seek(block_end-block_start)) Ok((&mut self.data).take_seek(block_end-block_start))
} }
pub(crate) fn fourcc(&self)->FourCC{ pub(crate) fn fourcc(&self)->FourCC{

View File

@ -20,31 +20,31 @@ pub enum SNF<R:BinReaderExt>{
} }
pub fn read_snf<R:BinReaderExt>(input:R)->Result<SNF<R>,Error>{ pub fn read_snf<R:BinReaderExt>(input:R)->Result<SNF<R>,Error>{
let file=file::File::new(input).map_err(|e|Error::Header(e))?; let file=file::File::new(input).map_err(Error::Header)?;
Ok(match file.fourcc(){ Ok(match file.fourcc(){
file::FourCC::Map=>SNF::Map(map::StreamableMap::new(file).map_err(|e|Error::Map(e))?), file::FourCC::Map=>SNF::Map(map::StreamableMap::new(file).map_err(Error::Map)?),
file::FourCC::Bot=>SNF::Bot(bot::StreamableBot::new(file).map_err(|e|Error::Bot(e))?), file::FourCC::Bot=>SNF::Bot(bot::StreamableBot::new(file).map_err(Error::Bot)?),
file::FourCC::Demo=>SNF::Demo(demo::StreamableDemo::new(file).map_err(|e|Error::Demo(e))?), file::FourCC::Demo=>SNF::Demo(demo::StreamableDemo::new(file).map_err(Error::Demo)?),
}) })
} }
pub fn read_map<R:BinReaderExt>(input:R)->Result<map::StreamableMap<R>,Error>{ pub fn read_map<R:BinReaderExt>(input:R)->Result<map::StreamableMap<R>,Error>{
let file=file::File::new(input).map_err(|e|Error::Header(e))?; let file=file::File::new(input).map_err(Error::Header)?;
match file.fourcc(){ match file.fourcc(){
file::FourCC::Map=>Ok(map::StreamableMap::new(file).map_err(|e|Error::Map(e))?), file::FourCC::Map=>Ok(map::StreamableMap::new(file).map_err(Error::Map)?),
_=>Err(Error::UnexpectedFourCC) _=>Err(Error::UnexpectedFourCC)
} }
} }
pub fn read_bot<R:BinReaderExt>(input:R)->Result<bot::StreamableBot<R>,Error>{ pub fn read_bot<R:BinReaderExt>(input:R)->Result<bot::StreamableBot<R>,Error>{
let file=file::File::new(input).map_err(|e|Error::Header(e))?; let file=file::File::new(input).map_err(Error::Header)?;
match file.fourcc(){ match file.fourcc(){
file::FourCC::Bot=>Ok(bot::StreamableBot::new(file).map_err(|e|Error::Bot(e))?), file::FourCC::Bot=>Ok(bot::StreamableBot::new(file).map_err(Error::Bot)?),
_=>Err(Error::UnexpectedFourCC) _=>Err(Error::UnexpectedFourCC)
} }
} }
pub fn read_demo<R:BinReaderExt>(input:R)->Result<demo::StreamableDemo<R>,Error>{ pub fn read_demo<R:BinReaderExt>(input:R)->Result<demo::StreamableDemo<R>,Error>{
let file=file::File::new(input).map_err(|e|Error::Header(e))?; let file=file::File::new(input).map_err(Error::Header)?;
match file.fourcc(){ match file.fourcc(){
file::FourCC::Demo=>Ok(demo::StreamableDemo::new(file).map_err(|e|Error::Demo(e))?), file::FourCC::Demo=>Ok(demo::StreamableDemo::new(file).map_err(Error::Demo)?),
_=>Err(Error::UnexpectedFourCC) _=>Err(Error::UnexpectedFourCC)
} }
} }

View File

@ -4,7 +4,7 @@ use binrw::{BinReaderExt, binrw};
pub enum Error{ pub enum Error{
InvalidHeader, InvalidHeader,
InvalidNodeId(u64), InvalidBvhNodeId(BvhNodeId),
InvalidRegion(binrw::Error), InvalidRegion(binrw::Error),
File(crate::file::Error), File(crate::file::Error),
} }
@ -77,9 +77,14 @@ mod image{
struct ModelUuid(u128); struct ModelUuid(u128);
struct ImageUuid(u128); struct ImageUuid(u128);
pub struct BvhNodeId(u64);
#[binrw]
#[brw(little)]
#[derive(Clone,Copy,id::Id)]
pub struct BvhNodeId(u32);
struct BvhNode{ struct BvhNode{
// //aabb
//child
} }
#[binrw] #[binrw]
#[brw(little)] #[brw(little)]
@ -106,9 +111,9 @@ impl<R:BinReaderExt> StreamableMap<R>{
//parse the models and determine what resources need to be loaded //parse the models and determine what resources need to be loaded
//load resources into self.resources //load resources into self.resources
//return Region //return Region
let block_id=*self.node_id_to_block_id.get(node_id.0 as usize).ok_or(Error::InvalidNodeId(node_id.0))?; let block_id=*self.node_id_to_block_id.get(node_id.get() as usize).ok_or(Error::InvalidBvhNodeId(node_id))?;
let mut block=self.file.take_block(block_id).map_err(|e|Error::File(e))?; let mut block=self.file.take_block(block_id).map_err(Error::File)?;
let region:Region=block.read_le().map_err(|e|Error::InvalidRegion(e))?; let region:Region=block.read_le().map_err(Error::InvalidRegion)?;
Ok(region.models) Ok(region.models)
} }
// pub fn load_resource(&mut self,resource_id:ResourceId)->Resource{ // pub fn load_resource(&mut self,resource_id:ResourceId)->Resource{