refactor loaders + file loading

This commit is contained in:
Quaternions 2024-02-13 21:56:11 -08:00
parent e7f01eff80
commit 7be93d2114
8 changed files with 88 additions and 50 deletions

22
Cargo.lock generated
View File

@ -1514,8 +1514,8 @@ dependencies = [
"parking_lot", "parking_lot",
"pollster", "pollster",
"strafesnet_common", "strafesnet_common",
"strafesnet_deferred_loader",
"strafesnet_rbx_loader", "strafesnet_rbx_loader",
"strafesnet_texture_loader",
"wgpu", "wgpu",
"winit", "winit",
] ]
@ -1529,10 +1529,19 @@ dependencies = [
"id", "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]] [[package]]
name = "strafesnet_rbx_loader" name = "strafesnet_rbx_loader"
version = "0.1.0" 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 = [ dependencies = [
"glam", "glam",
"lazy-regex", "lazy-regex",
@ -1543,15 +1552,6 @@ dependencies = [
"strafesnet_common", "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]] [[package]]
name = "strict-num" name = "strict-num"
version = "0.1.1" version = "0.1.1"

View File

@ -14,8 +14,8 @@ id = { git = "https://git.itzana.me/Quaternions/id", rev = "1f710976cc786c8853da
parking_lot = "0.12.1" parking_lot = "0.12.1"
pollster = "0.3.0" pollster = "0.3.0"
strafesnet_common = { git = "https://git.itzana.me/StrafesNET/common", rev = "47cdea0c8a5d10a2440ca6270a975d560aa3642d" } 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_rbx_loader = { git = "https://git.itzana.me/StrafesNET/rbx_loader", rev = "0b630576d4b2402277d55f0a53e9d048a31af9aa" }
strafesnet_texture_loader = { git = "https://git.itzana.me/StrafesNET/texture_loader", rev = "c182e46001a095b245fe80e5c9912e58f51b0276", features = ["legacy"] } strafesnet_deferred_loader = { git = "https://git.itzana.me/StrafesNET/deferred_loader", rev = "cfe62fc998704c06f3afb555ee2e2075219f2f9e", features = ["legacy", "source"] }
wgpu = "0.19.0" wgpu = "0.19.0"
winit = "0.29.2" winit = "0.29.2"

64
src/file.rs Normal file
View File

@ -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<R:Read>(input:R)->Result<DataStructure,ReadError>{
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"<rob"=>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))
},
}
}

View File

@ -147,7 +147,7 @@ impl GraphicsState{
pub fn load_user_settings(&mut self,user_settings:&crate::settings::UserSettings){ 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(); 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 //generate texture view per texture
let texture_views:HashMap<strafesnet_common::model::TextureId,wgpu::TextureView>=textures.into_iter().filter_map(|(texture_id,texture_data)|{ let texture_views:HashMap<strafesnet_common::model::TextureId,wgpu::TextureView>=textures.into_iter().filter_map(|(texture_id,texture_data)|{
let image=ddsfile::Dds::read(std::io::Cursor::new(texture_data)).ok()?; let image=ddsfile::Dds::read(std::io::Cursor::new(texture_data)).ok()?;

View File

@ -4,7 +4,7 @@ pub enum Instruction{
Render(crate::physics::PhysicsOutputState,integer::Time,glam::IVec2), Render(crate::physics::PhysicsOutputState,integer::Time,glam::IVec2),
//UpdateModel(crate::graphics::GraphicsModelUpdate), //UpdateModel(crate::graphics::GraphicsModelUpdate),
Resize(winit::dpi::PhysicalSize<u32>,crate::settings::UserSettings), Resize(winit::dpi::PhysicalSize<u32>,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, ClearModels,
} }

View File

@ -11,6 +11,8 @@ mod model_graphics;
mod physics_worker; mod physics_worker;
mod graphics_worker; mod graphics_worker;
mod file;
fn main(){ fn main(){
setup::setup_and_start(format!("Strafe Client v{}",env!("CARGO_PKG_VERSION"))); setup::setup_and_start(format!("Strafe Client v{}",env!("CARGO_PKG_VERSION")));
} }

View File

@ -18,7 +18,7 @@ pub enum Instruction{
Input(InputInstruction), Input(InputInstruction),
Render, Render,
Resize(winit::dpi::PhysicalSize<u32>,crate::settings::UserSettings), Resize(winit::dpi::PhysicalSize<u32>,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, ClearModels,
//Graphics(crate::graphics_worker::Instruction), //Graphics(crate::graphics_worker::Instruction),
} }

View File

@ -1,7 +1,6 @@
use crate::physics_worker::InputInstruction; use crate::physics_worker::InputInstruction;
use strafesnet_common::integer; use strafesnet_common::integer;
use strafesnet_common::instruction::TimedInstruction; use strafesnet_common::instruction::TimedInstruction;
use strafesnet_texture_loader::texture_loader::TextureLoaderTrait;
pub enum WindowInstruction{ pub enum WindowInstruction{
Resize(winit::dpi::PhysicalSize<u32>), Resize(winit::dpi::PhysicalSize<u32>),
@ -28,39 +27,12 @@ impl WindowContext<'_>{
fn window_event(&mut self,time:integer::Time,event: winit::event::WindowEvent) { fn window_event(&mut self,time:integer::Time,event: winit::event::WindowEvent) {
match event { match event {
winit::event::WindowEvent::DroppedFile(path)=>{ winit::event::WindowEvent::DroppedFile(path)=>{
let path=path.as_path(); match crate::file::load(path.as_path()){
//blocking because it's simpler... Ok((map,textures))=>{
if let Ok(file)=std::fs::File::open(path){ self.physics_thread.send(TimedInstruction{time,instruction:crate::physics_worker::Instruction::ClearModels}).unwrap();
// match strafesnet_snf::read_snf(std::io::BufReader::new(file)){ self.physics_thread.send(TimedInstruction{time,instruction:crate::physics_worker::Instruction::GenerateModels(map,textures)}).unwrap();
// Ok(strafesnet_snf::SNF::Map(streamable_map))=>{ },
// if let Ok(indexed_model_instances)=streamable_map.load_all(){ Err(e)=>println!("Failed to load map: {e}"),
// 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:?}");
} }
}, },
winit::event::WindowEvent::Focused(_state)=>{ winit::event::WindowEvent::Focused(_state)=>{