diff --git a/Cargo.lock b/Cargo.lock index 48ee96ad..fba2829d 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -1514,8 +1514,8 @@ dependencies = [ "parking_lot", "pollster", "strafesnet_common", + "strafesnet_deferred_loader", "strafesnet_rbx_loader", - "strafesnet_texture_loader", "wgpu", "winit", ] @@ -1529,10 +1529,19 @@ dependencies = [ "id", ] +[[package]] +name = "strafesnet_deferred_loader" +version = "0.2.0" +source = "git+https://git.itzana.me/StrafesNET/deferred_loader?rev=cfe62fc998704c06f3afb555ee2e2075219f2f9e#cfe62fc998704c06f3afb555ee2e2075219f2f9e" +dependencies = [ + "lazy-regex", + "strafesnet_common", +] + [[package]] name = "strafesnet_rbx_loader" version = "0.1.0" -source = "git+https://git.itzana.me/StrafesNET/rbx_loader?rev=93bc4dc0fb2175af8ded235c7a4b47af7235790a#93bc4dc0fb2175af8ded235c7a4b47af7235790a" +source = "git+https://git.itzana.me/StrafesNET/rbx_loader?rev=0b630576d4b2402277d55f0a53e9d048a31af9aa#0b630576d4b2402277d55f0a53e9d048a31af9aa" dependencies = [ "glam", "lazy-regex", @@ -1543,15 +1552,6 @@ dependencies = [ "strafesnet_common", ] -[[package]] -name = "strafesnet_texture_loader" -version = "0.1.0" -source = "git+https://git.itzana.me/StrafesNET/texture_loader?rev=c182e46001a095b245fe80e5c9912e58f51b0276#c182e46001a095b245fe80e5c9912e58f51b0276" -dependencies = [ - "lazy-regex", - "strafesnet_common", -] - [[package]] name = "strict-num" version = "0.1.1" diff --git a/Cargo.toml b/Cargo.toml index c0464a1c..23e65351 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -14,8 +14,8 @@ id = { git = "https://git.itzana.me/Quaternions/id", rev = "1f710976cc786c8853da parking_lot = "0.12.1" pollster = "0.3.0" strafesnet_common = { git = "https://git.itzana.me/StrafesNET/common", rev = "47cdea0c8a5d10a2440ca6270a975d560aa3642d" } -strafesnet_rbx_loader = { git = "https://git.itzana.me/StrafesNET/rbx_loader", rev = "93bc4dc0fb2175af8ded235c7a4b47af7235790a" } -strafesnet_texture_loader = { git = "https://git.itzana.me/StrafesNET/texture_loader", rev = "c182e46001a095b245fe80e5c9912e58f51b0276", features = ["legacy"] } +strafesnet_rbx_loader = { git = "https://git.itzana.me/StrafesNET/rbx_loader", rev = "0b630576d4b2402277d55f0a53e9d048a31af9aa" } +strafesnet_deferred_loader = { git = "https://git.itzana.me/StrafesNET/deferred_loader", rev = "cfe62fc998704c06f3afb555ee2e2075219f2f9e", features = ["legacy", "source"] } wgpu = "0.19.0" winit = "0.29.2" diff --git a/src/file.rs b/src/file.rs new file mode 100644 index 00000000..565da87f --- /dev/null +++ b/src/file.rs @@ -0,0 +1,64 @@ +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(input:R)->Result{ + 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"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)) + }, + } +} \ No newline at end of file diff --git a/src/graphics.rs b/src/graphics.rs index 55b2a389..33968bd7 100644 --- a/src/graphics.rs +++ b/src/graphics.rs @@ -147,7 +147,7 @@ impl GraphicsState{ pub fn load_user_settings(&mut self,user_settings:&crate::settings::UserSettings){ self.camera.fov=user_settings.calculate_fov(1.0,&self.camera.screen_size).as_vec2(); } - pub fn generate_models(&mut self,device:&wgpu::Device,queue:&wgpu::Queue,map:&map::CompleteMap,textures:strafesnet_texture_loader::texture_loader::Textures){ + pub fn generate_models(&mut self,device:&wgpu::Device,queue:&wgpu::Queue,map:&map::CompleteMap,textures:strafesnet_deferred_loader::texture::Textures){ //generate texture view per texture let texture_views:HashMap=textures.into_iter().filter_map(|(texture_id,texture_data)|{ let image=ddsfile::Dds::read(std::io::Cursor::new(texture_data)).ok()?; diff --git a/src/graphics_worker.rs b/src/graphics_worker.rs index 4f7e8d9d..ae904b21 100644 --- a/src/graphics_worker.rs +++ b/src/graphics_worker.rs @@ -4,7 +4,7 @@ pub enum Instruction{ Render(crate::physics::PhysicsOutputState,integer::Time,glam::IVec2), //UpdateModel(crate::graphics::GraphicsModelUpdate), Resize(winit::dpi::PhysicalSize,crate::settings::UserSettings), - GenerateModels(strafesnet_common::map::CompleteMap,strafesnet_texture_loader::texture_loader::Textures), + GenerateModels(strafesnet_common::map::CompleteMap,strafesnet_deferred_loader::texture::Textures), ClearModels, } diff --git a/src/main.rs b/src/main.rs index 627528e8..6705bd00 100644 --- a/src/main.rs +++ b/src/main.rs @@ -11,6 +11,8 @@ mod model_graphics; mod physics_worker; mod graphics_worker; +mod file; + fn main(){ setup::setup_and_start(format!("Strafe Client v{}",env!("CARGO_PKG_VERSION"))); } diff --git a/src/physics_worker.rs b/src/physics_worker.rs index d17259f9..82f422c9 100644 --- a/src/physics_worker.rs +++ b/src/physics_worker.rs @@ -18,7 +18,7 @@ pub enum Instruction{ Input(InputInstruction), Render, Resize(winit::dpi::PhysicalSize,crate::settings::UserSettings), - GenerateModels(strafesnet_common::map::CompleteMap,strafesnet_texture_loader::texture_loader::Textures), + GenerateModels(strafesnet_common::map::CompleteMap,strafesnet_deferred_loader::texture::Textures), ClearModels, //Graphics(crate::graphics_worker::Instruction), } diff --git a/src/window.rs b/src/window.rs index b00b74eb..ac3b1d06 100644 --- a/src/window.rs +++ b/src/window.rs @@ -1,7 +1,6 @@ use crate::physics_worker::InputInstruction; use strafesnet_common::integer; use strafesnet_common::instruction::TimedInstruction; -use strafesnet_texture_loader::texture_loader::TextureLoaderTrait; pub enum WindowInstruction{ Resize(winit::dpi::PhysicalSize), @@ -28,39 +27,12 @@ impl WindowContext<'_>{ fn window_event(&mut self,time:integer::Time,event: winit::event::WindowEvent) { match event { winit::event::WindowEvent::DroppedFile(path)=>{ - let path=path.as_path(); - //blocking because it's simpler... - if let Ok(file)=std::fs::File::open(path){ - // match strafesnet_snf::read_snf(std::io::BufReader::new(file)){ - // Ok(strafesnet_snf::SNF::Map(streamable_map))=>{ - // if let Ok(indexed_model_instances)=streamable_map.load_all(){ - // self.physics_thread.send(TimedInstruction{time,instruction:crate::physics_worker::Instruction::ClearModels}).unwrap(); - // self.physics_thread.send(TimedInstruction{time,instruction:crate::physics_worker::Instruction::GenerateModels(indexed_model_instances)}).unwrap(); - // } - // }, - // Ok(strafesnet_snf::SNF::Bot(streamable_map))=>println!("File type not yet supported"), - // Ok(strafesnet_snf::SNF::Demo(streamable_map))=>println!("File type not yet supported"), - // Err(e)=>println!("Error reading file: {e:?}"), - // } - let mut texture_loader=strafesnet_texture_loader::legacy(); - match (strafesnet_rbx_loader::read(file,|name|{ - match texture_loader.acquire_id(name){ - Ok(texture_id)=>Some(texture_id), - Err(e)=>{ - println!("there was error: {e}"); - None - }, - } - }),texture_loader.load()){ - (Ok(map),Ok(textures))=>{ - self.physics_thread.send(TimedInstruction{time,instruction:crate::physics_worker::Instruction::ClearModels}).unwrap(); - self.physics_thread.send(TimedInstruction{time,instruction:crate::physics_worker::Instruction::GenerateModels(map,textures)}).unwrap(); - }, - (Err(e),_)=>println!("Error reading file: {e:?}"), - (_,Err(e))=>println!("Error loading textures: {e:?}"), - } - }else{ - println!("Failed to open file {path:?}"); + match crate::file::load(path.as_path()){ + Ok((map,textures))=>{ + self.physics_thread.send(TimedInstruction{time,instruction:crate::physics_worker::Instruction::ClearModels}).unwrap(); + self.physics_thread.send(TimedInstruction{time,instruction:crate::physics_worker::Instruction::GenerateModels(map,textures)}).unwrap(); + }, + Err(e)=>println!("Failed to load map: {e}"), } }, winit::event::WindowEvent::Focused(_state)=>{