ok I wrote this I guess

This commit is contained in:
Quaternions 2024-01-22 20:28:12 -08:00
parent 7e27b378e9
commit 07f0b03d45

View File

@ -167,7 +167,7 @@ async fn main()->AResult<()>{
input_folder:cli.input.unwrap(), input_folder:cli.input.unwrap(),
output_file:cli.output.unwrap(), output_file:cli.output.unwrap(),
template:None, template:None,
}), }).await,
Commands::Decompile=>decompile(DecompileConfig{ Commands::Decompile=>decompile(DecompileConfig{
style:decompile_style.unwrap(), style:decompile_style.unwrap(),
input_file:cli.input.unwrap(), input_file:cli.input.unwrap(),
@ -1084,6 +1084,141 @@ async fn download_and_decompile_history_into_git(config:DownloadAndDecompileHist
Ok(()) Ok(())
} }
//holy smokes what am I doing lmao
//This giant machine is supposed to search for files according to style rules
//e.g. ScriptName.server.lua or init.lua
//Obviously I got carried away
//I could use an enum!
//I could use a struct!
//I could use a trait!
//I could use an error!
//I could use a match!
//I could use a function!
//eventually:
#[derive(Debug)]
enum QueryResolveError{
NotFound,//0 results
Ambiguous,//>1 results
JoinError(tokio::task::JoinError),
IO(std::io::Error),
}
impl std::fmt::Display for QueryResolveError{
fn fmt(&self,f:&mut std::fmt::Formatter<'_>)->std::fmt::Result{
write!(f,"{self:?}")
}
}
impl std::error::Error for QueryResolveError{}
async fn get_file_async(mut path:std::path::PathBuf,file_name:impl AsRef<std::path::Path>)->Result<tokio::fs::File,QueryResolveError>{
path.push(file_name);
match tokio::fs::File::open(path).await{
Ok(file)=>Ok(file),
Err(e)=>match e.kind(){
std::io::ErrorKind::NotFound=>Err(QueryResolveError::NotFound),
_=>Err(QueryResolveError::IO(e)),
},
}
}
type QueryResult=Result<tokio::fs::File,QueryResolveError>;
type QueryResultHandle=tokio::task::JoinHandle<QueryResult>;
trait Query{
async fn resolve(self)->Result<tokio::fs::File,QueryResolveError>;
}
struct QuerySingle(QueryResultHandle);
impl QuerySingle{
fn rox(search_path:&std::path::PathBuf,search_name:&str)->Self{
Self(tokio::spawn(get_file_async(search_path.clone(),format!("{}.lua",search_name))))
}
}
impl Query for QuerySingle{
async fn resolve(self)->Result<tokio::fs::File,QueryResolveError>{
match self.0.await{
Ok(Ok(file))=>Ok(file),
Ok(Err(e))=>Err(e),
Err(e)=>Err(QueryResolveError::JoinError(e)),
}
}
}
struct QueryTriplet{
shared:QuerySingle,
server:QuerySingle,
client:QuerySingle,
}
impl QueryTriplet{
fn rox_rojo(search_path:&std::path::PathBuf,search_name:&str)->Self{
Self{
shared:QuerySingle(tokio::spawn(get_file_async(search_path.clone(),format!("{}.lua",search_name)))),
server:QuerySingle(tokio::spawn(get_file_async(search_path.clone(),format!("{}.server.lua",search_name)))),
client:QuerySingle(tokio::spawn(get_file_async(search_path.clone(),format!("{}.client.lua",search_name)))),
}
}
fn rojo(search_path:&std::path::PathBuf,search_name:&str,is_subfolder:bool)->Self{
if is_subfolder{
QueryTriplet::rox_rojo(search_path,"init")
}else{
QueryTriplet::rox_rojo(search_path,search_name)
}
}
}
fn mega_triple_join(query_triplet:(QueryResult,QueryResult,QueryResult))->QueryResult{
match query_triplet{
//unambiguously locate file
(Ok(f),Err(QueryResolveError::NotFound),Err(QueryResolveError::NotFound))
|(Err(QueryResolveError::NotFound),Ok(f),Err(QueryResolveError::NotFound))
|(Err(QueryResolveError::NotFound),Err(QueryResolveError::NotFound),Ok(f))=>Ok(f),
//multiple files located
(Ok(_),Ok(_),Err(QueryResolveError::NotFound))
|(Ok(_),Err(QueryResolveError::NotFound),Ok(_))
|(Err(QueryResolveError::NotFound),Ok(_),Ok(_))
|(Ok(_),Ok(_),Ok(_))=>Err(QueryResolveError::Ambiguous),
//no files located
(Err(QueryResolveError::NotFound),Err(QueryResolveError::NotFound),Err(QueryResolveError::NotFound))=>Err(QueryResolveError::NotFound),
//other error
(Err(e),_,_)
|(_,Err(e),_)
|(_,_,Err(e))=>Err(e)
}
}
impl Query for QueryTriplet{
async fn resolve(self)->Result<tokio::fs::File,QueryResolveError>{
let (shared,server,client)=tokio::join!(self.shared.0,self.server.0,self.client.0);
mega_triple_join((
shared.map_err(|e|QueryResolveError::JoinError(e))?,
server.map_err(|e|QueryResolveError::JoinError(e))?,
client.map_err(|e|QueryResolveError::JoinError(e))?,
))
}
}
async fn find_file(search_path:&std::path::PathBuf,search_name:&str,style:Option<DecompileStyle>)->AResult<CompileClass>{
//figure out something clever for this
let mut what=CompileNode{
class:CompileClass::Folder,
folder:None,
};
let mut contents_folder=search_path.clone();
contents_folder.push(search_name);
//folder
if let Ok(dir)=tokio::fs::read_dir(contents_folder.as_path()).await{
what.folder=Some(dir);
}else{
contents_folder.pop();
}
let (script_file,model_file)=tokio::join!(async {match style{
Some(DecompileStyle::Rox)=>QuerySingle::rox(search_path,search_name).resolve().await,
Some(DecompileStyle::RoxRojo)=>QueryTriplet::rox_rojo(search_path,search_name).resolve().await,
Some(DecompileStyle::Rojo)=>QueryTriplet::rojo(search_path,search_name,what.folder.is_some()).resolve().await,
//try all three and complain if there is ambiguity
None=>mega_triple_join(tokio::join!(
QuerySingle::rox(search_path,search_name).resolve(),
QueryTriplet::rox_rojo(search_path,search_name).resolve(),
QueryTriplet::rojo(search_path,search_name,what.folder.is_some()).resolve(),
))
}},get_file_async(search_path.clone(),format!("{}.rbxmx",search_name)));
Ok(CompileClass::Folder)
}
enum CompileClass{ enum CompileClass{
Folder, Folder,
Script(String), Script(String),
@ -1109,7 +1244,7 @@ struct CompileConfig{
template:Option<std::path::PathBuf>, template:Option<std::path::PathBuf>,
} }
fn compile(config:CompileConfig)->AResult<()>{ async fn compile(config:CompileConfig)->AResult<()>{
//basically decompile in reverse order //basically decompile in reverse order
//load template dom //load template dom
let input={ let input={
@ -1118,6 +1253,7 @@ fn compile(config:CompileConfig)->AResult<()>{
template_path.push("template.rbxlx"); template_path.push("template.rbxlx");
template_path template_path
}); });
//mr dom doesn't like tokio files
std::io::BufReader::new(std::fs::File::open(template_path)?) std::io::BufReader::new(std::fs::File::open(template_path)?)
}; };
let mut dom=load_dom(input)?; let mut dom=load_dom(input)?;
@ -1131,31 +1267,11 @@ fn compile(config:CompileConfig)->AResult<()>{
CompileStackInstruction::Referent(item_ref)=>{ CompileStackInstruction::Referent(item_ref)=>{
let item=dom.get_by_ref(item_ref).ok_or(anyhow::Error::msg("null child ref"))?; let item=dom.get_by_ref(item_ref).ok_or(anyhow::Error::msg("null child ref"))?;
//check if item exists in folder or subfolder of same name //check if item exists in folder or subfolder of same name
let what=||async{ if let Ok(obj)=find_file(&folder,item.name.as_str(),None).await{
//figure out something clever for this //cool
let mut what=CompileNode{
class:CompileClass::Folder,
folder:None,
};
let mut contents_folder=folder.clone();
contents_folder.push(item.name.as_str());
//folder
if let Ok(dir)=tokio::fs::read_dir(contents_folder.as_path()).await{
what.folder=Some(dir);
}else{ }else{
contents_folder.pop(); //determine if this is ok
} }
//tokio::join!(a,b,c);
//rox
let mut rox_path=contents_folder.clone();
rox_path.push(format!("{}.lua",item.name.as_str()));
if let Ok(file)=tokio::fs::File::open(rox_path).await{
//CompileClass::ModuleScript
}
//rox-rojo
//rojo
};
//push child objects onto dom //push child objects onto dom
//push dom children objects onto stack //push dom children objects onto stack
stack.push(CompileStackInstruction::PopFolder); stack.push(CompileStackInstruction::PopFolder);