From db2c760c49d48ac44ff48094cd4d9b2c44ad7aa6 Mon Sep 17 00:00:00 2001 From: Quaternions Date: Tue, 23 Jan 2024 20:19:47 -0800 Subject: [PATCH] extract_script_overrides --- src/main.rs | 45 +++++++++++++++++++++++++++++++++++++++++---- 1 file changed, 41 insertions(+), 4 deletions(-) diff --git a/src/main.rs b/src/main.rs index b415b90..0c5e41a 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{ + 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)->AResult>{ 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), + other=>return Err(anyhow::Error::msg(format!("Invalid hint or class {other:?}"))), + }, 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), }, @@ -1325,7 +1361,7 @@ async fn discern_node(search_path:&std::path::PathBuf,search_name:&str,style:Opt None }) } - +#[derive(Debug)] enum ScriptHint{ Script, LocalScript, @@ -1346,8 +1382,9 @@ enum CompileClass{ } struct CompileNode{ - folder:Option, + name:String, class:CompileClass, + folder:Option, } enum CompileStackInstruction{