CompileNode

This commit is contained in:
Quaternions 2024-07-01 12:39:13 -07:00
parent 581a0d6699
commit 43b5dfd0d5

View File

@ -186,150 +186,178 @@ struct ScriptWithOverrides{
source:String, source:String,
} }
fn extract_script_overrides(mut source:String)->AResult<ScriptWithOverrides>{ pub enum ScriptWithOverridesError{
let mut overrides=PropertiesOverride::default(); UnimplementedProperty(String),
let mut count=0; }
for line in source.lines(){
//only string type properties are supported atm impl ScriptWithOverrides{
if let Some(captures)=lazy_regex::regex!(r#"^\-\-\!\s*Properties\.([A-z]\w*)\s*\=\s*"(\w+)"$"#) fn from_source(mut source:String)->Result<Self,ScriptWithOverridesError>{
.captures(line){ let mut overrides=PropertiesOverride::default();
count+=line.len(); let mut count=0;
match &captures[1]{ for line in source.lines(){
"Name"=>overrides.name=Some(captures[2].to_owned()), //only string type properties are supported atm
"ClassName"=>overrides.class=Some(captures[2].to_owned()), if let Some(captures)=lazy_regex::regex!(r#"^\-\-\!\s*Properties\.([A-z]\w*)\s*\=\s*"(\w+)"$"#)
other=>Err(anyhow::Error::msg(format!("Unimplemented property {other}")))?, .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=>Err(ScriptWithOverridesError::UnimplementedProperty(other.to_owned()))?,
}
}else{
break;
} }
}else{
break;
} }
Ok(ScriptWithOverrides{overrides,source:source.split_off(count)})
} }
Ok(ScriptWithOverrides{overrides,source:source.split_off(count)})
} }
async fn script_node(search_name:&str,mut file:FileWithName,hint:ScriptHint)->AResult<CompileNode>{ enum CompileClass{
//read entire file Folder,
let mut buf=String::new(); Script(String),
file.file.read_to_string(&mut buf).await?; LocalScript(String),
//regex script according to Properties lines at the top ModuleScript(String),
let script_with_overrides=extract_script_overrides(buf)?; Model(Vec<u8>),
//script
Ok(CompileNode{
blacklist:Some(file.name),
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)=>CompileClass::ModuleScript(script_with_overrides.source),
(Some("LocalScript"),_)
|(None,ScriptHint::LocalScript)=>CompileClass::LocalScript(script_with_overrides.source),
(Some("Script"),_)
|(None,ScriptHint::Script)=>CompileClass::Script(script_with_overrides.source),
other=>Err(anyhow::Error::msg(format!("Invalid hint or class {other:?}")))?,
},
})
} }
async fn model_node(search_name:&str,mut file:FileWithName)->AResult<CompileNode>{ struct CompileNode{
//read entire file name:String,
let mut buf=Vec::new(); blacklist:Option<String>,
file.file.read_to_end(&mut buf).await?; class:CompileClass,
//model
Ok(CompileNode{
blacklist:Some(file.name),
name:search_name.to_owned(),//wrong but gets overwritten by internal model name
class:CompileClass::Model(buf),
})
} }
async fn locate_override_file(entry:&tokio::fs::DirEntry,style:Option<DecompileStyle>)->AResult<CompileNode>{ enum CompileNodeError{
let contents_folder=entry.path(); IO(std::io::Error),
let file_name=entry.file_name(); ScriptWithOverrides(ScriptWithOverridesError),
let search_name=file_name.to_str().unwrap(); InvalidHintOrClass(Option<String>,ScriptHint),
//scan inside the folder for an object to define the class of the folder QueryResolveError(QueryResolveError),
let script_query=async {match style{ /// Conversion from OsString to String failed
Some(DecompileStyle::Rox)=>QuerySingle::rox(&contents_folder,search_name).resolve().await, FileName(std::ffi::OsString),
Some(DecompileStyle::RoxRojo)=>QueryQuad::rox_rojo(&contents_folder,search_name).resolve().await, ExtensionNotSupportedInStyle{
Some(DecompileStyle::Rojo)=>QueryTriple::rojo(&contents_folder).resolve().await, extension:String,
//try all three and complain if there is ambiguity style:Option<DecompileStyle>,
None=>mega_triple_join(tokio::join!( },
QuerySingle::rox(&contents_folder,search_name).resolve(), NoExtension,
//true=search for module here to avoid ambiguity with QuerySingle::rox results
QueryTriple::rox_rojo(&contents_folder,search_name,true).resolve(),
QueryTriple::rojo(&contents_folder).resolve(),
))
}};
//model files are rox & rox-rojo only, so it's a lot less work...
let model_query=get_file_async(contents_folder.clone(),format!("{}.rbxmx",search_name));
//model? script? both?
Ok(match tokio::join!(script_query,model_query){
(Ok(FileHint{file,hint}),Err(QueryResolveError::NotFound))=>script_node(search_name,file,hint).await?,
(Err(QueryResolveError::NotFound),Ok(file))=>model_node(search_name,file).await?,
(Ok(_),Ok(_))=>Err(QueryResolveError::Ambiguous)?,
//neither
(Err(QueryResolveError::NotFound),Err(QueryResolveError::NotFound))=>CompileNode{
name:search_name.to_owned(),
blacklist:None,
class:CompileClass::Folder,
},
//other error
(Err(e),_)
|(_,Err(e))=>Err(e)?
})
} }
enum FileDiscernment{ enum FileDiscernment{
Model, Model,
Script(ScriptHint), Script(ScriptHint),
} }
async fn discern_file(entry:&tokio::fs::DirEntry,style:Option<DecompileStyle>)->AResult<CompileNode>{ impl CompileNode{
let mut file_name=entry async fn script(search_name:&str,mut file:FileWithName,hint:ScriptHint)->Result<Self,CompileNodeError>{
.file_name() //read entire file
.into_string() let mut buf=String::new();
.map_err(|e|anyhow::Error::msg(format!("insane file name {e:?}")))?; file.file.read_to_string(&mut buf).await.map_err(CompileNodeError::IO)?;
//reject goobers //regex script according to Properties lines at the top
let is_goober=match style{ let script_with_overrides=ScriptWithOverrides::from_source(buf).map_err(CompileNodeError::ScriptWithOverrides)?;
Some(DecompileStyle::Rojo)=>true, //script
_=>false, Ok(Self{
}; blacklist:Some(file.name),
let (ext_len,file_discernment)={ name:script_with_overrides.overrides.name.unwrap_or_else(||search_name.to_owned()),
if let Some(captures)=lazy_regex::regex!(r"^.*(.module.lua|.client.lua|.server.lua)$") class:match (script_with_overrides.overrides.class.as_deref(),hint){
.captures(file_name.as_str()){ (Some("ModuleScript"),_)
let ext=&captures[1]; |(None,ScriptHint::ModuleScript)=>CompileClass::ModuleScript(script_with_overrides.source),
(ext.len(),match ext{ (Some("LocalScript"),_)
".module.lua"=>{ |(None,ScriptHint::LocalScript)=>CompileClass::LocalScript(script_with_overrides.source),
if is_goober{ (Some("Script"),_)
Err(anyhow::Error::msg(format!("File extension {ext} not supported in style {style:?}")))?; |(None,ScriptHint::Script)=>CompileClass::Script(script_with_overrides.source),
} other=>Err(CompileNodeError::InvalidHintOrClass(other.0.map(|s|s.to_owned()),other.1))?,
FileDiscernment::Script(ScriptHint::ModuleScript) },
}, })
".client.lua"=>FileDiscernment::Script(ScriptHint::LocalScript), }
".server.lua"=>FileDiscernment::Script(ScriptHint::Script), async fn model(search_name:&str,mut file:FileWithName)->Result<Self,CompileNodeError>{
_=>panic!("Regex failed"), //read entire file
}) let mut buf=Vec::new();
}else if let Some(captures)=lazy_regex::regex!(r"^.*(.rbxmx|.lua)$") file.file.read_to_end(&mut buf).await.map_err(CompileNodeError::IO)?;
.captures(file_name.as_str()){ //model
let ext=&captures[1]; Ok(Self{
(ext.len(),match ext{ blacklist:Some(file.name),
".rbxmx"=>{ name:search_name.to_owned(),//wrong but gets overwritten by internal model name
if is_goober{ class:CompileClass::Model(buf),
Err(anyhow::Error::msg(format!("File extension {ext} not supported in style {style:?}")))?; })
} }
FileDiscernment::Model
}, async fn from_folder(entry:&tokio::fs::DirEntry,style:Option<DecompileStyle>)->Result<Self,CompileNodeError>{
".lua"=>FileDiscernment::Script(ScriptHint::ModuleScript), let contents_folder=entry.path();
_=>panic!("Regex failed"), let file_name=entry.file_name();
}) let search_name=file_name.to_str().unwrap();
}else{ //scan inside the folder for an object to define the class of the folder
return Err(anyhow::Error::msg("No file extension")); let script_query=async {match style{
} Some(DecompileStyle::Rox)=>QuerySingle::rox(&contents_folder,search_name).resolve().await,
}; Some(DecompileStyle::RoxRojo)=>QueryQuad::rox_rojo(&contents_folder,search_name).resolve().await,
file_name.truncate(file_name.len()-ext_len); Some(DecompileStyle::Rojo)=>QueryTriple::rojo(&contents_folder).resolve().await,
let file=tokio::fs::File::open(entry.path()).await?; //try all three and complain if there is ambiguity
Ok(match file_discernment{ None=>mega_triple_join(tokio::join!(
FileDiscernment::Model=>model_node(file_name.as_str(),FileWithName{file,name:file_name.clone()}).await?, QuerySingle::rox(&contents_folder,search_name).resolve(),
FileDiscernment::Script(hint)=>script_node(file_name.as_str(),FileWithName{file,name:file_name.clone()},hint).await?, //true=search for module here to avoid ambiguity with QuerySingle::rox results
}) QueryTriple::rox_rojo(&contents_folder,search_name,true).resolve(),
QueryTriple::rojo(&contents_folder).resolve(),
))
}};
//model files are rox & rox-rojo only, so it's a lot less work...
let model_query=get_file_async(contents_folder.clone(),format!("{}.rbxmx",search_name));
//model? script? both?
Ok(match tokio::join!(script_query,model_query){
(Ok(FileHint{file,hint}),Err(QueryResolveError::NotFound))=>Self::script(search_name,file,hint).await?,
(Err(QueryResolveError::NotFound),Ok(file))=>Self::model(search_name,file).await?,
(Ok(_),Ok(_))=>Err(CompileNodeError::QueryResolveError(QueryResolveError::Ambiguous))?,
//neither
(Err(QueryResolveError::NotFound),Err(QueryResolveError::NotFound))=>Self{
name:search_name.to_owned(),
blacklist:None,
class:CompileClass::Folder,
},
//other error
(Err(e),_)
|(_,Err(e))=>Err(CompileNodeError::QueryResolveError(e))?
})
}
async fn from_file(entry:&tokio::fs::DirEntry,style:Option<DecompileStyle>)->Result<Self,CompileNodeError>{
let mut file_name=entry
.file_name()
.into_string()
.map_err(CompileNodeError::FileName)?;
//reject goobers
let is_goober=match style{
Some(DecompileStyle::Rojo)=>true,
_=>false,
};
let (ext_len,file_discernment)={
if let Some(captures)=lazy_regex::regex!(r"^.*(.module.lua|.client.lua|.server.lua|.rbxmx|.lua)$")
.captures(file_name.as_str()){
let ext=&captures[1];
(ext.len(),match ext{
".module.lua"=>{
if is_goober{
Err(CompileNodeError::ExtensionNotSupportedInStyle{extension:ext.to_owned(),style})?;
}
FileDiscernment::Script(ScriptHint::ModuleScript)
},
".client.lua"=>FileDiscernment::Script(ScriptHint::LocalScript),
".server.lua"=>FileDiscernment::Script(ScriptHint::Script),
".rbxmx"=>{
if is_goober{
Err(CompileNodeError::ExtensionNotSupportedInStyle{extension:ext.to_owned(),style})?;
}
FileDiscernment::Model
},
".lua"=>FileDiscernment::Script(ScriptHint::ModuleScript),
_=>panic!("Regex failed"),
})
}else{
return Err(CompileNodeError::NoExtension);
}
};
file_name.truncate(file_name.len()-ext_len);
let file=tokio::fs::File::open(entry.path()).await.map_err(CompileNodeError::IO)?;
Ok(match file_discernment{
FileDiscernment::Model=>Self::model(file_name.as_str(),FileWithName{file,name:file_name.clone()}).await?,
FileDiscernment::Script(hint)=>Self::script(file_name.as_str(),FileWithName{file,name:file_name.clone()},hint).await?,
})
}
} }
#[derive(Debug)] #[derive(Debug)]
@ -348,20 +376,6 @@ enum PreparedData{
Builder(rbx_dom_weak::InstanceBuilder), Builder(rbx_dom_weak::InstanceBuilder),
} }
enum CompileClass{
Folder,
Script(String),
LocalScript(String),
ModuleScript(String),
Model(Vec<u8>),
}
struct CompileNode{
name:String,
blacklist:Option<String>,
class:CompileClass,
}
enum CompileStackInstruction{ enum CompileStackInstruction{
TraverseReferent(rbx_dom_weak::types::Ref,Option<String>), TraverseReferent(rbx_dom_weak::types::Ref,Option<String>),
PopFolder, PopFolder,
@ -446,8 +460,8 @@ async fn compile(config:CompileConfig,&mut dom:rbx_dom_weak::WeakDom)->Result<()
let met=entry.metadata().await?; let met=entry.metadata().await?;
//discern that bad boy //discern that bad boy
let compile_class=match met.is_dir(){ let compile_class=match met.is_dir(){
true=>locate_override_file(&entry,style).await?, true=>CompileNode::from_folder(&entry,style).await?,
false=>discern_file(&entry,style).await?, false=>CompileNode::from_file(&entry,style).await?,
}; };
//prepare data structure //prepare data structure
Ok(Some((compile_class.blacklist,match compile_class.class{ Ok(Some((compile_class.blacklist,match compile_class.class{