2024-07-25 17:48:07 +00:00
|
|
|
use std::io::Read;
|
|
|
|
use std::collections::HashMap;
|
|
|
|
|
|
|
|
use crate::newtypes;
|
|
|
|
use crate::file::BlockId;
|
2024-07-25 22:35:54 +00:00
|
|
|
use binrw::{binrw,BinReaderExt,BinWriterExt};
|
2024-07-25 17:48:07 +00:00
|
|
|
use strafesnet_common::model;
|
|
|
|
use strafesnet_common::aabb::Aabb;
|
|
|
|
use strafesnet_common::bvh::BvhNode;
|
|
|
|
use strafesnet_common::gameplay_modes;
|
2024-01-19 03:28:01 +00:00
|
|
|
|
2024-07-26 00:47:00 +00:00
|
|
|
#[derive(Debug)]
|
2024-01-16 03:09:34 +00:00
|
|
|
pub enum Error{
|
2024-07-25 17:48:07 +00:00
|
|
|
InvalidHeader(binrw::Error),
|
2024-07-30 01:20:44 +00:00
|
|
|
InvalidMode(newtypes::gameplay_modes::ModeError),
|
2024-07-25 17:48:07 +00:00
|
|
|
InvalidBlockId(BlockId),
|
|
|
|
InvalidMeshId(model::MeshId),
|
2024-07-27 00:26:57 +00:00
|
|
|
InvalidModelId(model::ModelId),
|
2024-07-25 17:48:07 +00:00
|
|
|
InvalidTextureId(model::TextureId),
|
|
|
|
InvalidData(binrw::Error),
|
|
|
|
IO(std::io::Error),
|
2024-01-19 03:28:01 +00:00
|
|
|
File(crate::file::Error),
|
2024-01-16 03:09:34 +00:00
|
|
|
}
|
2024-07-26 00:47:00 +00:00
|
|
|
impl std::fmt::Display for Error{
|
|
|
|
fn fmt(&self,f:&mut std::fmt::Formatter<'_>)->std::fmt::Result{
|
|
|
|
write!(f,"{self:?}")
|
|
|
|
}
|
|
|
|
}
|
|
|
|
impl std::error::Error for Error{}
|
2024-01-16 03:09:34 +00:00
|
|
|
|
|
|
|
/* block types
|
|
|
|
|
|
|
|
BLOCK_MAP_HEADER:
|
|
|
|
DefaultStyleInfo style_info
|
|
|
|
//bvh goes here
|
2024-07-25 17:48:07 +00:00
|
|
|
u32 num_nodes
|
|
|
|
u32 num_spacial_blocks
|
|
|
|
u32 num_resource_blocks
|
|
|
|
u32 num_resources_external
|
2024-01-16 03:09:34 +00:00
|
|
|
//node 0 parent node is implied to be None
|
|
|
|
for node_id in 1..num_nodes{
|
2024-07-25 17:48:07 +00:00
|
|
|
u32 parent_node
|
2024-01-16 03:09:34 +00:00
|
|
|
}
|
2024-01-18 23:24:00 +00:00
|
|
|
|
|
|
|
//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.
|
|
|
|
|
2024-01-17 02:50:02 +00:00
|
|
|
//ideally spacial blocks are sorted from distance to start zone
|
|
|
|
//texture blocks are inserted before the first spacial block they are used in
|
2024-01-16 03:09:34 +00:00
|
|
|
for spacial_block_id in 0..num_spacial_blocks{
|
2024-07-25 17:48:07 +00:00
|
|
|
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
|
2024-01-16 03:09:34 +00:00
|
|
|
}
|
2024-07-25 17:48:07 +00:00
|
|
|
for resource_idx in 0..num_resources_external{
|
|
|
|
u128 resource_uuid
|
2024-01-17 02:50:02 +00:00
|
|
|
}
|
2024-01-16 03:09:34 +00:00
|
|
|
|
|
|
|
BLOCK_MAP_RESOURCE:
|
2024-01-17 02:50:02 +00:00
|
|
|
Resource resource_type
|
2024-01-16 03:09:34 +00:00
|
|
|
//an individual one of the following:
|
2024-02-09 07:13:53 +00:00
|
|
|
- model (Mesh)
|
2024-01-16 03:09:34 +00:00
|
|
|
- shader (compiled SPIR-V)
|
|
|
|
- image (JpegXL)
|
|
|
|
- sound (Opus)
|
|
|
|
- video (AV1)
|
|
|
|
- animation (Trey thing)
|
|
|
|
|
2024-01-17 02:50:02 +00:00
|
|
|
BLOCK_MAP_REGION:
|
|
|
|
u64 num_models
|
|
|
|
for model_id in 0..num_models{
|
|
|
|
ModelInstance mode_instance
|
|
|
|
}
|
2024-01-16 03:09:34 +00:00
|
|
|
|
|
|
|
*/
|
|
|
|
|
2024-07-25 17:48:07 +00:00
|
|
|
//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,
|
2024-02-02 04:27:12 +00:00
|
|
|
}
|
2024-07-25 17:48:07 +00:00
|
|
|
const RESOURCE_TYPE_VARIANT_COUNT:u8=2;
|
|
|
|
#[binrw]
|
|
|
|
#[brw(little)]
|
|
|
|
struct ResourceId(u128);
|
|
|
|
impl ResourceId{
|
|
|
|
fn resource_type(&self)->Option<ResourceType>{
|
|
|
|
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)<std::mem::variant_count::<ResourceType>(){
|
|
|
|
match discriminant<RESOURCE_TYPE_VARIANT_COUNT{
|
|
|
|
true=>Some(unsafe{std::mem::transmute::<u8,ResourceType>(discriminant)}),
|
|
|
|
false=>None,
|
|
|
|
}
|
|
|
|
}
|
2024-01-17 04:07:11 +00:00
|
|
|
}
|
|
|
|
|
2024-07-25 17:48:07 +00:00
|
|
|
struct ResourceMap<T>{
|
|
|
|
meshes:HashMap<strafesnet_common::model::MeshId,T>,
|
|
|
|
textures:HashMap<strafesnet_common::model::TextureId,T>,
|
|
|
|
}
|
|
|
|
impl<T> Default for ResourceMap<T>{
|
|
|
|
fn default()->Self{
|
|
|
|
Self{
|
|
|
|
meshes:HashMap::new(),
|
|
|
|
textures:HashMap::new(),
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2024-01-17 04:07:11 +00:00
|
|
|
|
2024-07-25 17:48:07 +00:00
|
|
|
#[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,
|
|
|
|
}
|
2024-02-07 09:01:23 +00:00
|
|
|
|
|
|
|
#[binrw]
|
|
|
|
#[brw(little)]
|
2024-07-25 17:48:07 +00:00
|
|
|
struct MapHeader{
|
|
|
|
num_spacial_blocks:u32,
|
|
|
|
num_resource_blocks:u32,
|
2024-07-26 22:42:38 +00:00
|
|
|
//num_resources_external:u32,
|
2024-07-25 17:48:07 +00:00
|
|
|
num_modes:u32,
|
|
|
|
num_attributes:u32,
|
|
|
|
num_render_configs:u32,
|
|
|
|
#[br(count=num_spacial_blocks)]
|
|
|
|
spacial_blocks:Vec<SpacialBlockHeader>,
|
|
|
|
#[br(count=num_resource_blocks)]
|
|
|
|
resource_blocks:Vec<ResourceBlockHeader>,
|
2024-07-26 22:42:38 +00:00
|
|
|
//#[br(count=num_resources_external)]
|
|
|
|
//external_resources:Vec<ResourceExternalHeader>,
|
2024-07-25 17:48:07 +00:00
|
|
|
#[br(count=num_modes)]
|
|
|
|
modes:Vec<newtypes::gameplay_modes::Mode>,
|
|
|
|
#[br(count=num_attributes)]
|
|
|
|
attributes:Vec<newtypes::gameplay_attributes::CollisionAttributes>,
|
|
|
|
#[br(count=num_render_configs)]
|
|
|
|
render_configs:Vec<newtypes::model::RenderConfig>,
|
2024-01-18 23:33:22 +00:00
|
|
|
}
|
2024-07-25 17:48:07 +00:00
|
|
|
|
2024-01-19 03:28:01 +00:00
|
|
|
#[binrw]
|
|
|
|
#[brw(little)]
|
|
|
|
struct Region{
|
2024-07-24 00:18:10 +00:00
|
|
|
//consider including a bvh in the region instead of needing to rebalance the physics bvh on the fly
|
2024-01-19 03:28:01 +00:00
|
|
|
model_count:u32,
|
|
|
|
#[br(count=model_count)]
|
2024-07-27 00:26:57 +00:00
|
|
|
models:Vec<(u32,newtypes::model::Model)>,
|
2024-07-25 17:48:07 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
//code deduplication reused in into_complete_map
|
2024-07-27 00:26:57 +00:00
|
|
|
fn read_region<R:BinReaderExt>(file:&mut crate::file::File<R>,block_id:BlockId)->Result<Vec<(model::ModelId,model::Model)>,Error>{
|
2024-07-25 17:48:07 +00:00
|
|
|
//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)?;
|
2024-07-27 00:26:57 +00:00
|
|
|
Ok(region.models.into_iter().map(|(model_id,model)|
|
|
|
|
(model::ModelId::new(model_id),model.into())
|
|
|
|
).collect())
|
2024-07-25 17:48:07 +00:00
|
|
|
}
|
|
|
|
fn read_mesh<R:BinReaderExt>(file:&mut crate::file::File<R>,block_id:BlockId)->Result<model::Mesh,Error>{
|
|
|
|
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<R:BinReaderExt>(file:&mut crate::file::File<R>,block_id:BlockId)->Result<Vec<u8>,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)
|
2024-01-19 03:28:01 +00:00
|
|
|
}
|
2024-01-18 23:33:22 +00:00
|
|
|
|
2024-01-19 03:28:01 +00:00
|
|
|
pub struct StreamableMap<R:BinReaderExt>{
|
|
|
|
file:crate::file::File<R>,
|
2024-01-30 06:56:20 +00:00
|
|
|
//this includes every platform... move the unconstrained datas to their appropriate data block?
|
|
|
|
modes:gameplay_modes::Modes,
|
2024-07-25 17:48:07 +00:00
|
|
|
//this is every possible attribute... need some sort of streaming system
|
|
|
|
attributes:Vec<strafesnet_common::gameplay_attributes::CollisionAttributes>,
|
|
|
|
//this is every possible render configuration... shaders and such... need streaming
|
|
|
|
render_configs:Vec<strafesnet_common::model::RenderConfig>,
|
|
|
|
//this makes sense to keep in memory for streaming, a map of which blocks occupy what space
|
|
|
|
bvh:BvhNode<BlockId>,
|
|
|
|
//something something resources hashmaps
|
|
|
|
resource_blocks:ResourceMap<BlockId>,
|
|
|
|
//resource_external:ResourceMap<ResourceId>,
|
2024-01-18 23:33:22 +00:00
|
|
|
}
|
2024-01-19 03:28:01 +00:00
|
|
|
impl<R:BinReaderExt> StreamableMap<R>{
|
2024-07-25 17:48:07 +00:00
|
|
|
pub(crate) fn new(mut file:crate::file::File<R>)->Result<Self,Error>{
|
|
|
|
//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)?;
|
2024-07-30 01:20:44 +00:00
|
|
|
let modes=header.modes.into_iter().map(TryInto::try_into).collect::<Result<_,_>>().map_err(Error::InvalidMode)?;
|
2024-07-25 17:48:07 +00:00
|
|
|
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<BlockId>{
|
|
|
|
let mut block_ids=Vec::new();
|
2024-07-25 22:35:54 +00:00
|
|
|
self.bvh.the_tester(aabb,&mut |&block_id|block_ids.push(block_id));
|
2024-07-25 17:48:07 +00:00
|
|
|
block_ids
|
|
|
|
}
|
2024-07-27 00:26:57 +00:00
|
|
|
pub fn load_region(&mut self,block_id:BlockId)->Result<Vec<(model::ModelId,model::Model)>,Error>{
|
2024-07-25 17:48:07 +00:00
|
|
|
read_region(&mut self.file,block_id)
|
2024-01-19 00:39:58 +00:00
|
|
|
}
|
2024-07-25 17:48:07 +00:00
|
|
|
pub fn load_mesh(&mut self,mesh_id:model::MeshId)->Result<model::Mesh,Error>{
|
|
|
|
let block_id=*self.resource_blocks.meshes.get(&mesh_id).ok_or(Error::InvalidMeshId(mesh_id))?;
|
|
|
|
read_mesh(&mut self.file,block_id)
|
2024-01-18 23:33:22 +00:00
|
|
|
}
|
2024-07-25 17:48:07 +00:00
|
|
|
pub fn load_texture(&mut self,texture_id:model::TextureId)->Result<Vec<u8>,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<strafesnet_common::map::CompleteMap,Error>{
|
2024-07-27 16:04:51 +00:00
|
|
|
let mut block_ids=Vec::new();
|
2024-07-27 00:26:57 +00:00
|
|
|
self.bvh.into_visitor(&mut |block_id|block_ids.push(block_id));
|
2024-07-27 16:04:51 +00:00
|
|
|
//count on reading the file in sequential order being fastest
|
|
|
|
block_ids.sort_unstable();
|
2024-07-26 23:13:51 +00:00
|
|
|
//load all regions
|
2024-07-27 00:26:57 +00:00
|
|
|
let mut model_pairs=HashMap::new();
|
2024-07-26 23:13:51 +00:00
|
|
|
for block_id in block_ids{
|
2024-07-27 00:26:57 +00:00
|
|
|
model_pairs.extend(read_region(&mut self.file,block_id)?);
|
|
|
|
}
|
|
|
|
let mut models=Vec::with_capacity(model_pairs.len());
|
|
|
|
for model_id in 0..model_pairs.len() as u32{
|
|
|
|
let model_id=model::ModelId::new(model_id);
|
|
|
|
models.push(model_pairs.remove(&model_id).ok_or(Error::InvalidModelId(model_id))?);
|
2024-07-26 23:13:51 +00:00
|
|
|
}
|
2024-07-25 17:48:07 +00:00
|
|
|
//load all meshes
|
2024-07-26 22:10:49 +00:00
|
|
|
let mut meshes=Vec::with_capacity(self.resource_blocks.meshes.len());
|
|
|
|
for mesh_id in 0..self.resource_blocks.meshes.len() as u32{
|
|
|
|
let mesh_id=model::MeshId::new(mesh_id);
|
|
|
|
let block_id=self.resource_blocks.meshes[&mesh_id];
|
|
|
|
meshes.push(read_mesh(&mut self.file,block_id)?);
|
|
|
|
};
|
2024-07-25 17:48:07 +00:00
|
|
|
//load all textures
|
2024-07-26 22:10:49 +00:00
|
|
|
let mut textures=Vec::with_capacity(self.resource_blocks.textures.len());
|
|
|
|
for texture_id in 0..self.resource_blocks.textures.len() as u32{
|
|
|
|
let texture_id=model::TextureId::new(texture_id);
|
|
|
|
let block_id=self.resource_blocks.textures[&texture_id];
|
|
|
|
textures.push(read_texture(&mut self.file,block_id)?);
|
|
|
|
}
|
2024-07-25 17:48:07 +00:00
|
|
|
Ok(strafesnet_common::map::CompleteMap{
|
|
|
|
modes:self.modes,
|
|
|
|
attributes:self.attributes,
|
|
|
|
meshes,
|
|
|
|
models,
|
|
|
|
textures,
|
|
|
|
render_configs:self.render_configs,
|
|
|
|
})
|
|
|
|
}
|
|
|
|
}
|
2024-07-25 22:35:54 +00:00
|
|
|
|
|
|
|
const BVH_NODE_MAX_WEIGHT:usize=64*1024;//64 kB
|
|
|
|
fn collect_spacial_blocks(
|
|
|
|
block_location:&mut Vec<u64>,
|
|
|
|
block_headers:&mut Vec<SpacialBlockHeader>,
|
|
|
|
sequential_block_data:&mut std::io::Cursor<&mut Vec<u8>>,
|
2024-07-27 00:26:57 +00:00
|
|
|
bvh_node:strafesnet_common::bvh::BvhWeightNode<usize,(model::ModelId,newtypes::model::Model)>
|
2024-07-25 22:35:54 +00:00
|
|
|
)->Result<(),Error>{
|
|
|
|
//inspect the node weights top-down.
|
|
|
|
//When a node weighs less than the limit,
|
|
|
|
//serialize its entire contents into a region block
|
|
|
|
if *bvh_node.weight()<BVH_NODE_MAX_WEIGHT{
|
|
|
|
let mut models=Vec::new();
|
|
|
|
let mut model_count=0;
|
|
|
|
let extents=bvh_node.aabb().clone().into();
|
2024-07-27 00:26:57 +00:00
|
|
|
bvh_node.into_visitor(&mut |(model_id,model)|{
|
2024-07-25 22:35:54 +00:00
|
|
|
model_count+=1;
|
2024-07-27 00:26:57 +00:00
|
|
|
models.push((model_id.get(),model));
|
2024-07-25 22:35:54 +00:00
|
|
|
});
|
2024-07-26 22:24:20 +00:00
|
|
|
let id=BlockId::new(block_headers.len() as u32+1);
|
2024-07-25 22:35:54 +00:00
|
|
|
block_headers.push(SpacialBlockHeader{
|
|
|
|
id,
|
|
|
|
extents,
|
|
|
|
});
|
|
|
|
let region=Region{
|
|
|
|
model_count,
|
|
|
|
models,
|
|
|
|
};
|
|
|
|
binrw::BinWrite::write_le(®ion,sequential_block_data).map_err(Error::InvalidData)?;
|
|
|
|
block_location.push(sequential_block_data.position());
|
|
|
|
}else{
|
|
|
|
match bvh_node.into_content(){
|
|
|
|
strafesnet_common::bvh::RecursiveContent::Branch(bvh_node_list)=>{
|
|
|
|
for bvh_node in bvh_node_list{
|
|
|
|
collect_spacial_blocks(block_location,block_headers,sequential_block_data,bvh_node)?;
|
|
|
|
}
|
|
|
|
},
|
|
|
|
strafesnet_common::bvh::RecursiveContent::Leaf(_)=>panic!(),//bvh branches are 20 leaves minimum
|
|
|
|
}
|
|
|
|
}
|
|
|
|
Ok(())
|
|
|
|
}
|
|
|
|
|
|
|
|
/// TODO: Optionally provide a bot that describes the path through the map
|
|
|
|
/// otherwise sort by distance to start zone
|
|
|
|
pub fn write_map<W:BinWriterExt>(mut writer:W,map:strafesnet_common::map::CompleteMap)->Result<(),Error>{
|
|
|
|
//serialize models and make a bvh that knows the file size of the branch
|
2024-07-27 00:26:57 +00:00
|
|
|
let boxen=map.models.into_iter().enumerate().map(|(model_id,model)|{
|
2024-07-25 22:35:54 +00:00
|
|
|
//grow your own aabb
|
|
|
|
let mesh=map.meshes.get(model.mesh.get() as usize).ok_or(Error::InvalidMeshId(model.mesh))?;
|
|
|
|
let mut aabb=strafesnet_common::aabb::Aabb::default();
|
|
|
|
for &pos in &mesh.unique_pos{
|
2024-09-16 22:46:52 +00:00
|
|
|
aabb.grow(model.transform.transform_point3(pos).fix_1());
|
2024-07-25 22:35:54 +00:00
|
|
|
}
|
2024-07-27 00:26:57 +00:00
|
|
|
Ok(((model::ModelId::new(model_id as u32),model.into()),aabb))
|
2024-07-25 22:35:54 +00:00
|
|
|
}).collect::<Result<Vec<_>,_>>()?;
|
|
|
|
let bvh=strafesnet_common::bvh::generate_bvh(boxen).weigh_contents(&|_|std::mem::size_of::<newtypes::model::Model>());
|
|
|
|
//build blocks
|
2024-07-26 19:19:09 +00:00
|
|
|
//block location is initialized with two values
|
|
|
|
//the first value represents the location of the first byte after the file header
|
|
|
|
//the second value represents the first byte of the second data block
|
|
|
|
//the first data block is always the map header, so the difference is the map header size.
|
|
|
|
//this information is filled in later after the sizes are known.
|
|
|
|
let mut block_location=vec![0,0];//for file header
|
2024-07-25 22:35:54 +00:00
|
|
|
let mut spacial_blocks=Vec::new();//for map header
|
|
|
|
let mut sequential_block_data=Vec::new();
|
|
|
|
let mut cursor_to_data=std::io::Cursor::new(&mut sequential_block_data);
|
|
|
|
collect_spacial_blocks(&mut block_location,&mut spacial_blocks,&mut cursor_to_data,bvh)?;
|
2024-07-26 22:24:20 +00:00
|
|
|
let mut block_count=spacial_blocks.len() as u32+1;//continue block id
|
2024-07-25 22:35:54 +00:00
|
|
|
let mut resource_blocks=Vec::new();//for map header
|
|
|
|
//meshes
|
|
|
|
for mesh in map.meshes.into_iter(){
|
|
|
|
resource_blocks.push(ResourceBlockHeader{
|
|
|
|
resource:ResourceType::Mesh,
|
|
|
|
id:BlockId::new(block_count),
|
|
|
|
});
|
|
|
|
block_count+=1;
|
|
|
|
let serializable_mesh:newtypes::model::Mesh=mesh.into();
|
|
|
|
binrw::BinWrite::write_le(&serializable_mesh,&mut cursor_to_data).map_err(Error::InvalidData)?;
|
|
|
|
block_location.push(cursor_to_data.position());
|
|
|
|
}
|
|
|
|
//textures
|
|
|
|
for mut texture in map.textures.into_iter(){
|
|
|
|
resource_blocks.push(ResourceBlockHeader{
|
|
|
|
resource:ResourceType::Texture,
|
|
|
|
id:BlockId::new(block_count),
|
|
|
|
});
|
|
|
|
block_count+=1;
|
|
|
|
sequential_block_data.append(&mut texture);
|
|
|
|
block_location.push(sequential_block_data.len() as u64);
|
|
|
|
}
|
|
|
|
//build header
|
|
|
|
let map_header=MapHeader{
|
|
|
|
num_spacial_blocks:spacial_blocks.len() as u32,
|
|
|
|
num_resource_blocks:resource_blocks.len() as u32,
|
2024-07-26 22:42:38 +00:00
|
|
|
//num_resources_external:0,
|
2024-07-25 22:35:54 +00:00
|
|
|
num_modes:map.modes.modes.len() as u32,
|
|
|
|
num_attributes:map.attributes.len() as u32,
|
|
|
|
num_render_configs:map.render_configs.len() as u32,
|
|
|
|
spacial_blocks,
|
|
|
|
resource_blocks,
|
2024-07-26 22:42:38 +00:00
|
|
|
//external_resources:Vec::new(),
|
2024-07-25 22:35:54 +00:00
|
|
|
modes:map.modes.modes.into_iter().map(Into::into).collect(),
|
|
|
|
attributes:map.attributes.into_iter().map(Into::into).collect(),
|
|
|
|
render_configs:map.render_configs.into_iter().map(Into::into).collect(),
|
|
|
|
};
|
|
|
|
let mut file_header=crate::file::Header{
|
|
|
|
fourcc:crate::file::FourCC::Map,
|
|
|
|
version:0,
|
2024-07-26 19:19:09 +00:00
|
|
|
priming:0,
|
2024-07-25 22:35:54 +00:00
|
|
|
resource:0,
|
|
|
|
block_count,
|
|
|
|
block_location,
|
|
|
|
};
|
2024-07-26 19:19:09 +00:00
|
|
|
//probe header length
|
2024-07-25 22:35:54 +00:00
|
|
|
let mut file_header_data=Vec::new();
|
|
|
|
binrw::BinWrite::write_le(&file_header,&mut std::io::Cursor::new(&mut file_header_data)).map_err(Error::InvalidData)?;
|
|
|
|
let mut map_header_data=Vec::new();
|
|
|
|
binrw::BinWrite::write_le(&map_header,&mut std::io::Cursor::new(&mut map_header_data)).map_err(Error::InvalidData)?;
|
|
|
|
|
|
|
|
//update file header according to probe data
|
2024-07-26 19:19:09 +00:00
|
|
|
let mut offset=file_header_data.len() as u64;
|
2024-07-25 22:35:54 +00:00
|
|
|
file_header.priming=offset;
|
2024-07-26 19:19:09 +00:00
|
|
|
file_header.block_location[0]=offset;
|
|
|
|
offset+=map_header_data.len() as u64;
|
|
|
|
for position in &mut file_header.block_location[1..]{
|
2024-07-25 22:35:54 +00:00
|
|
|
*position+=offset;
|
|
|
|
}
|
|
|
|
|
2024-07-26 19:19:09 +00:00
|
|
|
//write (updated) file header
|
2024-07-25 22:35:54 +00:00
|
|
|
writer.write_le(&file_header).map_err(Error::InvalidData)?;
|
|
|
|
//write map header
|
|
|
|
writer.write(&map_header_data).map_err(Error::IO)?;
|
|
|
|
//write blocks
|
|
|
|
writer.write(&sequential_block_data).map_err(Error::IO)?;
|
|
|
|
Ok(())
|
|
|
|
}
|