use std::io::Read; use std::collections::HashMap; use crate::newtypes; use crate::file::BlockId; use binrw::{binrw,BinReaderExt}; use strafesnet_common::model; use strafesnet_common::aabb::Aabb; use strafesnet_common::bvh::BvhNode; use strafesnet_common::gameplay_modes; pub enum Error{ InvalidHeader(binrw::Error), InvalidBlockId(BlockId), InvalidMeshId(model::MeshId), InvalidTextureId(model::TextureId), InvalidData(binrw::Error), IO(std::io::Error), File(crate::file::Error), } /* block types BLOCK_MAP_HEADER: DefaultStyleInfo style_info //bvh goes here u32 num_nodes u32 num_spacial_blocks u32 num_resource_blocks u32 num_resources_external //node 0 parent node is implied to be None for node_id in 1..num_nodes{ u32 parent_node } //NOTE: alternate realities are not necessary. //portals/wormholes simply use an in-model and and out-model. //skyboxes are inverted models with a special shader. //ideally spacial blocks are sorted from distance to start zone //texture blocks are inserted before the first spacial block they are used in for spacial_block_id in 0..num_spacial_blocks{ u32 node_id u32 block_id //data block Aabb extents } //the order of these lists uniquely generates the incremental Ids //MeshId, TextureId etc. based on resource type //the first 8 bits of resource_uuid describe the type (mesh, texture, etc) //if the map file references only external resources, num_resource_blocks = 0 for resource_idx in 0..num_resource_blocks{ Resource resource_type u32 block_id } for resource_idx in 0..num_resources_external{ u128 resource_uuid } BLOCK_MAP_RESOURCE: Resource resource_type //an individual one of the following: - model (Mesh) - shader (compiled SPIR-V) - image (JpegXL) - sound (Opus) - video (AV1) - animation (Trey thing) BLOCK_MAP_REGION: u64 num_models for model_id in 0..num_models{ ModelInstance mode_instance } */ //if you hash the resource itself and set the first 8 bits to this, that's the resource uuid #[binrw] #[brw(little,repr=u8)] enum ResourceType{ Mesh, Texture, //Shader, //Sound, //Video, //Animation, } const RESOURCE_TYPE_VARIANT_COUNT:u8=2; #[binrw] #[brw(little)] struct ResourceId(u128); impl ResourceId{ fn resource_type(&self)->Option{ let discriminant=self.0 as u8; //TODO: use this when it is stabilized https://github.com/rust-lang/rust/issues/73662 //if (discriminant as usize)(){ match discriminantSome(unsafe{std::mem::transmute::(discriminant)}), false=>None, } } } struct ResourceMap{ meshes:HashMap, textures:HashMap, } impl Default for ResourceMap{ fn default()->Self{ Self{ meshes:HashMap::new(), textures:HashMap::new(), } } } #[binrw] #[brw(little)] struct SpacialBlockHeader{ id:BlockId, extents:newtypes::aabb::Aabb, } #[binrw] #[brw(little)] struct ResourceBlockHeader{ resource:ResourceType, id:BlockId, } #[binrw] #[brw(little)] struct ResourceExternalHeader{ resource_uuid:ResourceId, } #[binrw] #[brw(little)] struct MapHeader{ num_nodes:u32, num_spacial_blocks:u32, num_resource_blocks:u32, num_resources_external:u32, num_modes:u32, num_attributes:u32, num_render_configs:u32, #[br(count=num_nodes)] nodes:Vec, #[br(count=num_spacial_blocks)] spacial_blocks:Vec, #[br(count=num_resource_blocks)] resource_blocks:Vec, #[br(count=num_resources_external)] external_resources:Vec, #[br(count=num_modes)] modes:Vec, #[br(count=num_attributes)] attributes:Vec, #[br(count=num_render_configs)] render_configs:Vec, } #[binrw] #[brw(little)] struct Region{ //consider including a bvh in the region instead of needing to rebalance the physics bvh on the fly model_count:u32, #[br(count=model_count)] models:Vec, } //code deduplication reused in into_complete_map fn read_region(file:&mut crate::file::File,block_id:BlockId)->Result,Error>{ //load region from disk //parse the models and determine what resources need to be loaded //load resources into self.resources //return Region let mut block=file.block_reader(block_id).map_err(Error::File)?; let region:Region=block.read_le().map_err(Error::InvalidData)?; Ok(region.models.into_iter().map(Into::into).collect()) } fn read_mesh(file:&mut crate::file::File,block_id:BlockId)->Result{ let mut block=file.block_reader(block_id).map_err(Error::File)?; let mesh:newtypes::model::Mesh=block.read_le().map_err(Error::InvalidData)?; Ok(mesh.into()) } fn read_texture(file:&mut crate::file::File,block_id:BlockId)->Result,Error>{ let mut block=file.block_reader(block_id).map_err(Error::File)?; let mut texture=Vec::new(); block.read_to_end(&mut texture).map_err(Error::IO)?; Ok(texture) } pub struct StreamableMap{ file:crate::file::File, //this includes every platform... move the unconstrained datas to their appropriate data block? modes:gameplay_modes::Modes, //this is every possible attribute... need some sort of streaming system attributes:Vec, //this is every possible render configuration... shaders and such... need streaming render_configs:Vec, //this makes sense to keep in memory for streaming, a map of which blocks occupy what space bvh:BvhNode, //something something resources hashmaps resource_blocks:ResourceMap, //resource_external:ResourceMap, } impl StreamableMap{ pub(crate) fn new(mut file:crate::file::File)->Result{ //assume the file seek is in the right place to start reading a map header let header:MapHeader=file.as_mut().read_le().map_err(Error::InvalidHeader)?; let modes=header.modes.into_iter().map(Into::into).collect(); let attributes=header.attributes.into_iter().map(Into::into).collect(); let render_configs=header.render_configs.into_iter().map(Into::into).collect(); let bvh=header.spacial_blocks.into_iter().map(|spacial_block| (spacial_block.id,spacial_block.extents.into()) ).collect(); //generate mesh ids and texture ids from resource list order let mut resource_blocks=ResourceMap::default(); for resource_block_header in header.resource_blocks{ match resource_block_header.resource{ ResourceType::Mesh=>{ resource_blocks.meshes.insert( //generate the id from the current length model::MeshId::new(resource_blocks.meshes.len() as u32), resource_block_header.id ); }, ResourceType::Texture=>{ resource_blocks.textures.insert( model::TextureId::new(resource_blocks.textures.len() as u32), resource_block_header.id ); }, } } Ok(Self{ file, modes:strafesnet_common::gameplay_modes::Modes::new(modes), attributes, render_configs, bvh:strafesnet_common::bvh::generate_bvh(bvh), resource_blocks, //resource_external:Default::default(), }) } pub fn get_intersecting_region_block_ids(&self,aabb:&Aabb)->Vec{ let mut block_ids=Vec::new(); self.bvh.the_tester(aabb,&mut |block_id|block_ids.push(block_id)); block_ids } pub fn load_region(&mut self,block_id:BlockId)->Result,Error>{ read_region(&mut self.file,block_id) } pub fn load_mesh(&mut self,mesh_id:model::MeshId)->Result{ let block_id=*self.resource_blocks.meshes.get(&mesh_id).ok_or(Error::InvalidMeshId(mesh_id))?; read_mesh(&mut self.file,block_id) } pub fn load_texture(&mut self,texture_id:model::TextureId)->Result,Error>{ let block_id=*self.resource_blocks.textures.get(&texture_id).ok_or(Error::InvalidTextureId(texture_id))?; read_texture(&mut self.file,block_id) } pub fn into_complete_map(mut self)->Result{ //load all meshes let meshes=self.resource_blocks.meshes.into_values().map(|block_id| read_mesh(&mut self.file,block_id) ).collect::,_>>()?; //load all textures let textures=self.resource_blocks.textures.into_values().map(|block_id| read_texture(&mut self.file,block_id) ).collect::,_>>()?; let mut block_ids=Vec::new(); self.bvh.into_visitor(&mut |block_id|block_ids.push(block_id)); //load all regions let mut models=Vec::new(); for block_id in block_ids{ models.append(&mut read_region(&mut self.file,block_id)?); } Ok(strafesnet_common::map::CompleteMap{ modes:self.modes, attributes:self.attributes, meshes, models, textures, render_configs:self.render_configs, }) } }