From 7fd592329e48e0df5395aae45ae6aced58c20b1c Mon Sep 17 00:00:00 2001
From: Quaternions <krakow20@gmail.com>
Date: Thu, 19 Oct 2023 19:19:05 -0700
Subject: [PATCH] split load file

---
 src/framework.rs     |   1 -
 src/main.rs          | 118 ++++++++++++++++++-------------------------
 src/physics.rs       |   9 ++++
 src/render_thread.rs |  12 +++++
 4 files changed, 70 insertions(+), 70 deletions(-)

diff --git a/src/framework.rs b/src/framework.rs
index f7acef1..dd6ec0d 100644
--- a/src/framework.rs
+++ b/src/framework.rs
@@ -53,7 +53,6 @@ pub trait Example: 'static + Sized {
 	);
 	fn update(&mut self, window: &winit::window::Window, device: &wgpu::Device, queue: &wgpu::Queue, event: WindowEvent);
 	fn device_event(&mut self, window: &winit::window::Window, event: DeviceEvent);
-	fn load_file(&mut self, path:std::path::PathBuf, device: &wgpu::Device, queue: &wgpu::Queue);
 	fn render(
 		&mut self,
 		view: &wgpu::TextureView,
diff --git a/src/main.rs b/src/main.rs
index 91ba742..1494a8c 100644
--- a/src/main.rs
+++ b/src/main.rs
@@ -1,5 +1,6 @@
 use std::time::Instant;
-use physics::{InputInstruction, PhysicsInstruction};
+use physics::PhysicsInstruction;
+use render_thread::InputInstruction;
 use instruction::{TimedInstruction, InstructionConsumer};
 
 mod bvh;
@@ -36,6 +37,51 @@ pub struct GlobalState{
 	physics_thread:worker::QNWorker<TimedInstruction<InputInstruction>>,
 }
 
+fn load_file(path: std::path::PathBuf)->Option<model::IndexedModelInstances>{
+	println!("Loading file: {:?}", &path);
+	//oh boy! let's load the map!
+	if let Ok(file)=std::fs::File::open(path){
+		let mut input = std::io::BufReader::new(file);
+		let mut first_8=[0u8;8];
+		//.rbxm roblox binary = "<roblox!"
+		//.rbxmx roblox xml = "<roblox "
+		//.bsp = "VBSP"
+		//.vmf = 
+		//.snf = "SNMF"
+		//.snf = "SNBF"
+		if let (Ok(()),Ok(()))=(std::io::Read::read_exact(&mut input, &mut first_8),std::io::Seek::rewind(&mut input)){
+			match &first_8[0..4]{
+				b"<rob"=>{
+					match match &first_8[4..8]{
+						b"lox!"=>rbx_binary::from_reader(input).map_err(|e|format!("{:?}",e)),
+						b"lox "=>rbx_xml::from_reader(input,rbx_xml::DecodeOptions::default()).map_err(|e|format!("{:?}",e)),
+						other=>Err(format!("Unknown Roblox file type {:?}",other)),
+					}{
+						Ok(dom)=>Some(load_roblox::generate_indexed_models(dom)),
+						Err(e)=>{
+							println!("Error loading roblox file:{:?}",e);
+							None
+						},
+					}
+				},
+				//b"VBSP"=>Some(load_bsp::generate_indexed_models(input)),
+				//b"SNFM"=>Some(sniffer::generate_indexed_models(input)),
+				//b"SNFB"=>Some(sniffer::load_bot(input)),
+				other=>{
+					println!("loser file {:?}",other);
+					None
+				},
+			}
+		}else{
+			println!("Failed to read first 8 bytes and seek back to beginning of file.");
+			None
+		}
+	}else{
+		println!("Could not open file");
+		None
+	}
+}
+
 impl framework::Example for GlobalState {
 	fn optional_features() -> wgpu::Features {
 		wgpu::Features::TEXTURE_COMPRESSION_ASTC
@@ -136,79 +182,13 @@ impl framework::Example for GlobalState {
 
 		let args:Vec<String>=std::env::args().collect();
 		if args.len()==2{
-			state.load_file(std::path::PathBuf::from(&args[1]), device, queue);
+			let indexed_model_instances=load_file(std::path::PathBuf::from(&args[1]));
+			state.render_thread=RenderThread::new(user_settings,indexed_model_instances);
 		}
 
 		return state;
 	}
 
-	fn load_file(&mut self,path: std::path::PathBuf, device: &wgpu::Device, queue: &wgpu::Queue){
-		println!("Loading file: {:?}", &path);
-		//oh boy! let's load the map!
-		if let Ok(file)=std::fs::File::open(path){
-			let mut input = std::io::BufReader::new(file);
-			let mut first_8=[0u8;8];
-			//.rbxm roblox binary = "<roblox!"
-			//.rbxmx roblox xml = "<roblox "
-			//.bsp = "VBSP"
-			//.vmf = 
-			//.snf = "SNMF"
-			//.snf = "SNBF"
-			if let (Ok(()),Ok(()))=(std::io::Read::read_exact(&mut input, &mut first_8),std::io::Seek::rewind(&mut input)){
-				if let Some(indexed_model_instances)={
-					match &first_8[0..4]{
-						b"<rob"=>{
-							match match &first_8[4..8]{
-								b"lox!"=>rbx_binary::from_reader(input).map_err(|e|format!("{:?}",e)),
-								b"lox "=>rbx_xml::from_reader(input,rbx_xml::DecodeOptions::default()).map_err(|e|format!("{:?}",e)),
-								other=>Err(format!("Unknown Roblox file type {:?}",other)),
-							}{
-								Ok(dom)=>Some(load_roblox::generate_indexed_models(dom)),
-								Err(e)=>{
-									println!("Error loading roblox file:{:?}",e);
-									None
-								},
-							}
-						},
-						//b"VBSP"=>Some(load_bsp::generate_indexed_models(input)),
-						//b"SNFM"=>Some(sniffer::generate_indexed_models(input)),
-						//b"SNFB"=>Some(sniffer::load_bot(input)),
-						other=>{
-							println!("loser file {:?}",other);
-							None
-						},
-					}
-				}{
-					let spawn_point=indexed_model_instances.spawn_point;
-					//if generate_indexed_models succeeds, clear the previous ones
-					self.graphics.clear();
-
-					let mut physics=physics::PhysicsState::default();
-					//physics.spawn()
-					physics.game.stage_id=0;
-					physics.spawn_point=spawn_point;
-					physics.process_instruction(instruction::TimedInstruction{
-						time:physics.time,
-						instruction: PhysicsInstruction::Input(physics::PhysicsInputInstruction::Reset),
-					});
-					physics.load_user_settings(&self.user_settings);
-					physics.generate_models(&indexed_model_instances);
-					self.physics_thread=physics.into_worker();
-
-					//graphics.load_user_settings(&self.user_settings);
-					self.generate_model_graphics(device,queue,indexed_model_instances);
-					//manual reset
-				}else{
-					println!("No modeldatas were generated");
-				}
-			}else{
-				println!("Failed to read first 8 bytes and seek back to beginning of file.");
-			}
-		}else{
-			println!("Could not open file");
-		}
-	}
-
 	#[allow(clippy::single_match)]
 	fn update(&mut self, window: &winit::window::Window, device: &wgpu::Device, queue: &wgpu::Queue, event: winit::event::WindowEvent) {
 		let time=integer::Time::from_nanos(self.start_time.elapsed().as_nanos() as i64);
diff --git a/src/physics.rs b/src/physics.rs
index 687421d..1f6cddf 100644
--- a/src/physics.rs
+++ b/src/physics.rs
@@ -727,6 +727,15 @@ impl PhysicsState {
 		}
 	}
 
+	pub fn spawn(&mut self,spawn_point:Planar64Vec3){
+		self.game.stage_id=0;
+		self.spawn_point=spawn_point;
+		self.process_instruction(crate::instruction::TimedInstruction{
+			time:self.time,
+			instruction: PhysicsInstruction::Input(PhysicsInputInstruction::Reset),
+		});
+	}
+
 	pub fn generate_models(&mut self,indexed_models:&crate::model::IndexedModelInstances){
 		let mut starts=Vec::new();
 		let mut spawns=Vec::new();
diff --git a/src/render_thread.rs b/src/render_thread.rs
index c8651cd..db4d04c 100644
--- a/src/render_thread.rs
+++ b/src/render_thread.rs
@@ -25,6 +25,18 @@ pub struct RenderState{
 	graphics:crate::graphics::GraphicsState,
 }
 impl RenderState{
+	pub fn new(user_settings:&crate::settings::UserSettings,indexed_model_instances:crate::model::IndexedModelInstances){
+
+		let mut physics=crate::physics::PhysicsState::default();
+		physics.spawn(indexed_model_instances.spawn_point);
+		physics.load_user_settings(user_settings);
+		physics.generate_models(&indexed_model_instances);
+
+		let mut graphics=Self::new_graphics_state();
+		graphics.load_user_settings(user_settings);
+		graphics.generate_models(indexed_model_instances);
+		//manual reset
+	}
 	pub fn into_worker(mut self)->crate::worker::CNWorker<TimedInstruction<InputInstruction>>{
 		let mut mouse_blocking=true;
 		let mut last_mouse_time=self.physics.next_mouse.time;