Compare commits

...

3 Commits

Author SHA1 Message Date
7a446eff4e extract_script_overrides 2024-01-23 20:21:09 -08:00
8ee041918b tweaks 2024-01-23 20:19:00 -08:00
5384bbcb3b support Script.module.lua properly 2024-01-23 18:47:15 -08:00

@ -573,28 +573,28 @@ enum WriteStackInstruction<'a>{
#[derive(Default)]
struct PropertiesOverride{
name:Option<String>,
class_name:Option<String>,
class:Option<String>,
}
impl PropertiesOverride{
fn is_some(&self)->bool{
self.name.is_some()
||self.class_name.is_some()
||self.class.is_some()
}
}
impl std::fmt::Display for PropertiesOverride{
fn fmt(&self,f:&mut std::fmt::Formatter<'_>)->std::fmt::Result{
if let Some(name)=self.name.as_deref(){
writeln!(f,"--! Properties.Name=\"{}\"",name)?;
writeln!(f,"--!Properties.Name = \"{}\"",name)?;
}
if let Some(class_name)=self.class_name.as_deref(){
writeln!(f,"--! Properties.ClassName=\"{}\"",class_name)?;
if let Some(class)=self.class.as_deref(){
writeln!(f,"--!Properties.ClassName = \"{}\"",class)?;
}
Ok(())
}
}
fn sanitize<'a>(s:&'a str)->std::borrow::Cow<'a,str>{
lazy_regex::regex!(r"[^a-zA-Z0-9._-]").replace_all(s,"_")
lazy_regex::regex!(r"[^A-z0-9.-]").replace_all(s,"_")
}
fn write_item(dom:&rbx_dom_weak::WeakDom,mut file:std::path::PathBuf,node:&TreeNode,node_name_override:String,mut properties:PropertiesOverride,style:DecompileStyle,write_models:bool,write_scripts:bool)->AResult<()>{
@ -610,18 +610,18 @@ fn write_item(dom:&rbx_dom_weak::WeakDom,mut file:std::path::PathBuf,node:&TreeN
match style{
DecompileStyle::Rox=>assert!(file.set_extension("lua"),"could not set extension"),
DecompileStyle::RoxRojo|DecompileStyle::Rojo=>{
match properties.class_name.as_deref(){
match properties.class.as_deref(){
Some("LocalScript")=>{
file.set_extension("client.lua");
properties.class_name=None;
properties.class=None;
},
Some("Script")=>{
file.set_extension("server.lua");
properties.class_name=None;
properties.class=None;
},
// Some("ModuleScript")=>{
// file.set_extension("module");
// properties.class_name=None;
// properties.class=None;
// },
None=>assert!(file.set_extension("lua"),"could not set extension"),
Some(other)=>return Err(anyhow::Error::msg(format!("Attempt to write a {} as a script",other))),
@ -664,7 +664,7 @@ fn generate_decompiled_context<R:Read>(input:R)->AResult<DecompiledContext>{
let mut tree_refs=std::collections::HashMap::new();
tree_refs.insert(dom.root_ref(),TreeNode::new(
"src".to_string(),
"src".to_owned(),
dom.root_ref(),
Ref::none(),
Class::Folder
@ -783,8 +783,8 @@ async fn write_files(config:WriteConfig,mut context:DecompiledContext)->AResult<
match node.class{
Class::Folder=>(),
Class::ModuleScript=>(),//.lua files are ModuleScript by default
Class::LocalScript=>properties.class_name=Some("LocalScript".to_string()),
Class::Script=>properties.class_name=Some("Script".to_string()),
Class::LocalScript=>properties.class=Some("LocalScript".to_owned()),
Class::Script=>properties.class=Some("Script".to_owned()),
Class::Model=>(),
}
let name_override=if 0<name_count{
@ -1120,32 +1120,31 @@ async fn get_file_async(mut path:std::path::PathBuf,file_name:impl AsRef<std::pa
},
}
}
type QueryResult=Result<tokio::fs::File,QueryResolveError>;
type QueryResultHandle=tokio::task::JoinHandle<QueryResult>;
type QueryHintResult=Result<FileHint,QueryResolveError>;
trait Query{
async fn resolve(self)->Result<tokio::fs::File,QueryResolveError>;
async fn resolve(self)->QueryHintResult;
}
struct QuerySingle(QueryResultHandle);
struct QuerySingle(tokio::task::JoinHandle<Result<tokio::fs::File,QueryResolveError>>);
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>{
async fn resolve(self)->QueryHintResult{
match self.0.await{
Ok(Ok(file))=>Ok(file),
Ok(Ok(file))=>Ok(FileHint{file,hint:ScriptHint::Undetermined}),
Ok(Err(e))=>Err(e),
Err(e)=>Err(QueryResolveError::JoinError(e)),
}
}
}
struct QueryTriplet{
struct QueryTriple{
module:QuerySingle,
server:QuerySingle,
client:QuerySingle,
}
impl QueryTriplet{
impl QueryTriple{
fn rox_rojo(search_path:&std::path::PathBuf,search_name:&str,search_module:bool)->Self{
//this should be implemented as constructors of Triplet and Quadruplet to fully support Trey's suggestion
let module_name=if search_module{
@ -1159,15 +1158,28 @@ impl QueryTriplet{
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,search_module:bool,is_subfolder:bool)->Self{
if is_subfolder{
QueryTriplet::rox_rojo(search_path,"init",search_module)
}else{
QueryTriplet::rox_rojo(search_path,search_name,search_module)
}
fn rojo(search_path:&std::path::PathBuf,search_name:&str)->Self{
QueryTriple::rox_rojo(search_path,"init",false)
}
}
fn mega_triple_join(query_triplet:(QueryResult,QueryResult,QueryResult))->QueryResult{
//these functions can be achieved with macros, but I have not learned that yet
fn mega_double_join(query_pair:(QueryHintResult,QueryHintResult))->QueryHintResult{
match query_pair{
//unambiguously locate file
(Ok(f),Err(QueryResolveError::NotFound))
|(Err(QueryResolveError::NotFound),Ok(f))=>Ok(f),
//multiple files located
(Ok(_),Err(QueryResolveError::NotFound))
|(Err(QueryResolveError::NotFound),Ok(_))
|(Ok(_),Ok(_))=>Err(QueryResolveError::Ambiguous),
//no files located
(Err(QueryResolveError::NotFound),Err(QueryResolveError::NotFound))=>Err(QueryResolveError::NotFound),
//other error
(Err(e),_)
|(_,Err(e))=>Err(e),
}
}
fn mega_triple_join(query_triplet:(QueryHintResult,QueryHintResult,QueryHintResult))->QueryHintResult{
match query_triplet{
//unambiguously locate file
(Ok(f),Err(QueryResolveError::NotFound),Err(QueryResolveError::NotFound))
@ -1183,19 +1195,101 @@ fn mega_triple_join(query_triplet:(QueryResult,QueryResult,QueryResult))->QueryR
//other error
(Err(e),_,_)
|(_,Err(e),_)
|(_,_,Err(e))=>Err(e)
|(_,_,Err(e))=>Err(e),
}
}
impl Query for QueryTriplet{
async fn resolve(self)->Result<tokio::fs::File,QueryResolveError>{
//LETS GOOOOOOOOOOOOOOOO
fn mega_quadruple_join(query_quad:(QueryHintResult,QueryHintResult,QueryHintResult,QueryHintResult))->QueryHintResult{
match query_quad{
//unambiguously locate file
(Ok(f),Err(QueryResolveError::NotFound),Err(QueryResolveError::NotFound),Err(QueryResolveError::NotFound))
|(Err(QueryResolveError::NotFound),Ok(f),Err(QueryResolveError::NotFound),Err(QueryResolveError::NotFound))
|(Err(QueryResolveError::NotFound),Err(QueryResolveError::NotFound),Ok(f),Err(QueryResolveError::NotFound))
|(Err(QueryResolveError::NotFound),Err(QueryResolveError::NotFound),Err(QueryResolveError::NotFound),Ok(f))=>Ok(f),
//multiple files located
(Ok(_),Ok(_),Ok(_),Err(QueryResolveError::NotFound))
|(Ok(_),Ok(_),Err(QueryResolveError::NotFound),Ok(_))
|(Ok(_),Ok(_),Err(QueryResolveError::NotFound),Err(QueryResolveError::NotFound))
|(Ok(_),Err(QueryResolveError::NotFound),Ok(_),Ok(_))
|(Ok(_),Err(QueryResolveError::NotFound),Ok(_),Err(QueryResolveError::NotFound))
|(Ok(_),Err(QueryResolveError::NotFound),Err(QueryResolveError::NotFound),Ok(_))
|(Err(QueryResolveError::NotFound),Ok(_),Ok(_),Ok(_))
|(Err(QueryResolveError::NotFound),Ok(_),Ok(_),Err(QueryResolveError::NotFound))
|(Err(QueryResolveError::NotFound),Ok(_),Err(QueryResolveError::NotFound),Ok(_))
|(Err(QueryResolveError::NotFound),Err(QueryResolveError::NotFound),Ok(_),Ok(_))
|(Ok(_),Ok(_),Ok(_),Ok(_))=>Err(QueryResolveError::Ambiguous),
//no files located
(Err(QueryResolveError::NotFound),Err(QueryResolveError::NotFound),Err(QueryResolveError::NotFound),Err(QueryResolveError::NotFound))=>Err(QueryResolveError::NotFound),
//other error
(Err(e),_,_,_)
|(_,Err(e),_,_)
|(_,_,Err(e),_)
|(_,_,_,Err(e))=>Err(e),
}
}
impl Query for QueryTriple{
async fn resolve(self)->QueryHintResult{
let (module,server,client)=tokio::join!(self.module.0,self.server.0,self.client.0);
mega_triple_join((
module.map_err(|e|QueryResolveError::JoinError(e))?,
server.map_err(|e|QueryResolveError::JoinError(e))?,
client.map_err(|e|QueryResolveError::JoinError(e))?,
module.map_err(|e|QueryResolveError::JoinError(e))?.map(|file|FileHint{file,hint:ScriptHint::ModuleScript}),
server.map_err(|e|QueryResolveError::JoinError(e))?.map(|file|FileHint{file,hint:ScriptHint::Script}),
client.map_err(|e|QueryResolveError::JoinError(e))?.map(|file|FileHint{file,hint:ScriptHint::LocalScript}),
))
}
}
struct QueryQuad{
module_implicit:QuerySingle,
module_explicit:QuerySingle,
server:QuerySingle,
client:QuerySingle,
}
impl QueryQuad{
fn rox_rojo(search_path:&std::path::PathBuf,search_name:&str)->Self{
let fill=QueryTriple::rox_rojo(search_path,search_name,true);
Self{
module_implicit:QuerySingle::rox(search_path,search_name),//Script.lua
module_explicit:fill.module,//Script.module.lua
server:fill.server,
client:fill.client,
}
}
}
impl Query for QueryQuad{
async fn resolve(self)->QueryHintResult{
let (module_implicit,module_explicit,server,client)=tokio::join!(self.module_implicit.0,self.module_explicit.0,self.server.0,self.client.0);
mega_quadruple_join((
module_implicit.map_err(|e|QueryResolveError::JoinError(e))?.map(|file|FileHint{file,hint:ScriptHint::ModuleScript}),
module_explicit.map_err(|e|QueryResolveError::JoinError(e))?.map(|file|FileHint{file,hint:ScriptHint::ModuleScript}),
server.map_err(|e|QueryResolveError::JoinError(e))?.map(|file|FileHint{file,hint:ScriptHint::Script}),
client.map_err(|e|QueryResolveError::JoinError(e))?.map(|file|FileHint{file,hint:ScriptHint::LocalScript}),
))
}
}
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();
@ -1203,33 +1297,40 @@ async fn discern_node(search_path:&std::path::PathBuf,search_name:&str,style:Opt
//folder
Ok(if let Ok(dir)=tokio::fs::read_dir(contents_folder.as_path()).await{
//scan inside the folder for an object to define the class of the folder
let (script_file,model_file)=tokio::join!(
async {match style{
Some(DecompileStyle::Rox)=>QuerySingle::rox(&contents_folder,search_name).resolve().await,
Some(DecompileStyle::RoxRojo)=>QueryTriplet::rox_rojo(&contents_folder,search_name,false).resolve().await,
Some(DecompileStyle::Rojo)=>QueryTriplet::rojo(&contents_folder,search_name,false,true).resolve().await,
//try all three and complain if there is ambiguity
None=>mega_triple_join(tokio::join!(
QuerySingle::rox(&contents_folder,search_name).resolve(),
//true=search for module here to avoid ambiguity with QuerySingle::rox results
QueryTriplet::rox_rojo(&contents_folder,search_name,true).resolve(),
QueryTriplet::rojo(&contents_folder,search_name,true,true).resolve(),
))
}},
//model files are rox & rox-rojo only, so it's a lot less work...
get_file_async(contents_folder.clone(),format!("{}.rbxmx",search_name))
);
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,
Some(DecompileStyle::Rojo)=>QueryTriple::rojo(&contents_folder,search_name).resolve().await,
//try all three and complain if there is ambiguity
None=>mega_triple_join(tokio::join!(
QuerySingle::rox(&contents_folder,search_name).resolve(),
//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,search_name).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?
Some(match (script_file,model_file){
(Ok(mut file),Err(QueryResolveError::NotFound))=>{
Some(match tokio::join!(script_query,model_query){
(Ok(FileHint{mut file,hint}),Err(QueryResolveError::NotFound))=>{
//read entire file
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),
}
},
@ -1239,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),
}
@ -1246,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),
},
@ -1259,6 +1362,17 @@ async fn discern_node(search_path:&std::path::PathBuf,search_name:&str,style:Opt
})
}
enum ScriptHint{
Script,
LocalScript,
ModuleScript,
Undetermined,
}
struct FileHint{
file:tokio::fs::File,
hint:ScriptHint,
}
enum CompileClass{
Folder,
Script(String),
@ -1268,8 +1382,9 @@ enum CompileClass{
}
struct CompileNode{
folder:Option<tokio::fs::ReadDir>,
name:String,
class:CompileClass,
folder:Option<tokio::fs::ReadDir>,
}
enum CompileStackInstruction{