From 7a446eff4e240d6cc7db69c4aa17214fed08b6d4 Mon Sep 17 00:00:00 2001
From: Quaternions <krakow20@gmail.com>
Date: Tue, 23 Jan 2024 20:19:47 -0800
Subject: [PATCH] extract_script_overrides

---
 src/main.rs | 43 ++++++++++++++++++++++++++++++++++++++++---
 1 file changed, 40 insertions(+), 3 deletions(-)

diff --git a/src/main.rs b/src/main.rs
index b415b90..7fc6dc9 100644
--- a/src/main.rs
+++ b/src/main.rs
@@ -1266,6 +1266,31 @@ impl Query for QueryQuad{
 	}
 }
 
+struct ScriptWithOverrides{
+	overrides:PropertiesOverride,
+	source:String,
+}
+
+fn extract_script_overrides(mut source:String)->AResult<ScriptWithOverrides>{
+	let mut overrides=PropertiesOverride::default();
+	let mut count=0;
+	for line in source.lines(){
+		//only string type properties are supported atm
+		if let Some(captures)=lazy_regex::regex!(r#"^\-\-\!\s*Properties\.([A-z]\w*)\s*\=\s*"(\w+)"$"#)
+		.captures(line){
+			count+=line.len();
+			match &captures[1]{
+				"Name"=>overrides.name=Some(captures[2].to_owned()),
+				"ClassName"=>overrides.class=Some(captures[2].to_owned()),
+				other=>return Err(anyhow::Error::msg(format!("Unimplemented property {other}"))),
+			}
+		}else{
+			break;
+		}
+	}
+	Ok(ScriptWithOverrides{overrides,source:source.split_off(count)})
+}
+
 async fn discern_node(search_path:&std::path::PathBuf,search_name:&str,style:Option<DecompileStyle>)->AResult<Option<CompileNode>>{
 	let mut contents_folder=search_path.clone();
 	contents_folder.push(search_name);
@@ -1293,10 +1318,19 @@ async fn discern_node(search_path:&std::path::PathBuf,search_name:&str,style:Opt
 				let mut buf=String::new();
 				file.read_to_string(&mut buf).await?;
 				//regex script according to Properties lines at the top
-				todo!("unimplemented");
+				let script_with_overrides=extract_script_overrides(buf)?;
 				//script
 				CompileNode{
-					class:CompileClass::Script(buf),
+					name:script_with_overrides.overrides.name.unwrap_or_else(||search_name.to_owned()),
+					class:match (script_with_overrides.overrides.class.as_deref(),hint){
+						(Some("ModuleScript"),_)
+						|(None,ScriptHint::ModuleScript|ScriptHint::Undetermined)=>CompileClass::ModuleScript(script_with_overrides.source),
+						(Some("LocalScript"),_)
+						|(None,ScriptHint::Script)=>CompileClass::LocalScript(script_with_overrides.source),
+						(Some("Script"),_)
+						|(None,ScriptHint::LocalScript)=>CompileClass::Script(script_with_overrides.source),
+						_=>panic!("Invalid hint or class"),
+					},
 					folder:Some(dir),
 				}
 			},
@@ -1306,6 +1340,7 @@ async fn discern_node(search_path:&std::path::PathBuf,search_name:&str,style:Opt
 				file.read_to_end(&mut buf).await?;
 				//model
 				CompileNode{
+					name:search_name.to_owned(),
 					class:CompileClass::Model(buf),
 					folder:Some(dir),
 				}
@@ -1313,6 +1348,7 @@ async fn discern_node(search_path:&std::path::PathBuf,search_name:&str,style:Opt
 			(Ok(_),Ok(_))=>Err(QueryResolveError::Ambiguous)?,
 			//neither
 			(Err(QueryResolveError::NotFound),Err(QueryResolveError::NotFound))=>CompileNode{
+				name:search_name.to_owned(),
 				class:CompileClass::Folder,
 				folder:Some(dir),
 			},
@@ -1346,8 +1382,9 @@ enum CompileClass{
 }
 
 struct CompileNode{
-	folder:Option<tokio::fs::ReadDir>,
+	name:String,
 	class:CompileClass,
+	folder:Option<tokio::fs::ReadDir>,
 }
 
 enum CompileStackInstruction{