From 7c2666fdf5d83fa27400be2feafd4923c821d8f9 Mon Sep 17 00:00:00 2001
From: Quaternions <krakow20@gmail.com>
Date: Tue, 26 Sep 2023 14:25:44 -0700
Subject: [PATCH] file loader multiplexer, pass dom to load_roblox

---
 src/load_roblox.rs |  4 +---
 src/main.rs        | 45 ++++++++++++++++++++++++++++++++++++---------
 2 files changed, 37 insertions(+), 12 deletions(-)

diff --git a/src/load_roblox.rs b/src/load_roblox.rs
index 1fe10e2..d555879 100644
--- a/src/load_roblox.rs
+++ b/src/load_roblox.rs
@@ -10,10 +10,8 @@ fn class_is_a(class: &str, superclass: &str) -> bool {
 	}
 	return false
 }
-pub fn get_objects<R: std::io::Read>(buf_thing: R, superclass: &str) -> Result<std::vec::Vec<rbx_dom_weak::Instance>, Box<dyn std::error::Error>> {
-	// Using buffered I/O is recommended with rbx_binary
-	let dom = rbx_binary::from_reader(buf_thing)?;
 
+pub fn get_objects(dom:rbx_dom_weak::WeakDom, superclass: &str) -> Result<std::vec::Vec<rbx_dom_weak::Instance>, Box<dyn std::error::Error>> {
 	let mut objects = std::vec::Vec::<rbx_dom_weak::Instance>::new();
 	//move matching instances into objects
 	let (_,mut instances) = dom.into_raw();
diff --git a/src/main.rs b/src/main.rs
index 4871c2c..1aaf2d9 100644
--- a/src/main.rs
+++ b/src/main.rs
@@ -161,9 +161,9 @@ impl GraphicsData {
 		depth_texture.create_view(&wgpu::TextureViewDescriptor::default())
 	}
 
-	fn generate_modeldatas_roblox<R: std::io::Read>(&self,input:R) -> Vec<ModelData>{
+	fn generate_modeldatas_roblox(&self,dom:rbx_dom_weak::WeakDom) -> Vec<ModelData>{
 		let mut modeldatas=generate_modeldatas(self.handy_unit_cube.clone(),ModelData::COLOR_FLOATS_WHITE);
-		match load_roblox::get_objects(input, "BasePart") {
+		match load_roblox::get_objects(dom, "BasePart") {
 			Ok(objects)=>{
 				for object in objects.iter() {
 					if let (
@@ -830,13 +830,40 @@ impl framework::Example for GraphicsData {
 				println!("opening file: {:?}", &path);
 				//oh boy! let's load the map!
 				if let Ok(file)=std::fs::File::open(path){
-					let input = std::io::BufReader::new(file);
-					let modeldatas=self.generate_modeldatas_roblox(input);
-					//if generate_modeldatas succeeds, clear the previous ones
-					self.models.clear();
-					self.physics.models.clear();
-					self.generate_model_physics(&modeldatas);
-					self.generate_model_graphics(device,modeldatas);
+					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(modeldatas)={
+							if &first_8==b"<roblox!"{
+								if let Ok(dom) = rbx_binary::from_reader(input){
+									Some(self.generate_modeldatas_roblox(dom))
+								}else{
+									None
+								}
+							//}else if &first_8[0..4]==b"VBSP"{
+							//	self.generate_modeldatas_valve(input)
+							}else{
+								None
+							}
+						}{
+							//if generate_modeldatas succeeds, clear the previous ones
+							self.models.clear();
+							self.physics.models.clear();
+							self.generate_model_physics(&modeldatas);
+							self.generate_model_graphics(device,modeldatas);
+						}else{
+							println!("No modeldatas were generated");
+						}
+					}else{
+						println!("Failed ro read first 8 bytes and seek back to beginning of file.");
+					}
 				}else{
 					println!("Could not open file");
 				}