sdk-builder: can pretty-print sdk in rust now presumably
This commit is contained in:
parent
117e8878d6
commit
01feff8cf5
|
@ -17,3 +17,5 @@ unreal-sdk = {path = "../unreal-sdk"}
|
||||||
quote = "1.0.28"
|
quote = "1.0.28"
|
||||||
proc-macro2 = "1.0.60"
|
proc-macro2 = "1.0.60"
|
||||||
clap = { version = "4.3.9", features = ["derive"] }
|
clap = { version = "4.3.9", features = ["derive"] }
|
||||||
|
prettyplease = "0.2.9"
|
||||||
|
syn = { version = "2.0.22", features = ["full"] }
|
||||||
|
|
|
@ -190,13 +190,13 @@ pub mod path_helper {
|
||||||
.unwrap_or(quote!(crate::engine::UObject));
|
.unwrap_or(quote!(crate::engine::UObject));
|
||||||
|
|
||||||
tokens.extend(quote!(
|
tokens.extend(quote!(
|
||||||
crate::engine::TAssetPtr<#inner>,
|
crate::engine::TAssetPtr<#inner>
|
||||||
));
|
));
|
||||||
}
|
}
|
||||||
Type::Array(inner) => {
|
Type::Array(inner) => {
|
||||||
let inner = self.child(*inner.clone());
|
let inner = self.child(*inner.clone());
|
||||||
tokens.extend(quote!(
|
tokens.extend(quote!(
|
||||||
crate::engine::TArray<#inner>,
|
crate::engine::TArray<#inner>
|
||||||
));
|
));
|
||||||
}
|
}
|
||||||
Type::Primitive(prim) => {
|
Type::Primitive(prim) => {
|
||||||
|
@ -204,7 +204,7 @@ pub mod path_helper {
|
||||||
}
|
}
|
||||||
Type::RawArray { ty, len } => {
|
Type::RawArray { ty, len } => {
|
||||||
let ty = self.child(*ty.clone());
|
let ty = self.child(*ty.clone());
|
||||||
quote!([#ty; #len]);
|
tokens.extend(quote! { [ #ty; #len ] });
|
||||||
}
|
}
|
||||||
Type::Name => tokens.extend(quote!(crate::engine::FName)),
|
Type::Name => tokens.extend(quote!(crate::engine::FName)),
|
||||||
Type::String => tokens.extend(quote!(crate::engine::FString)),
|
Type::String => tokens.extend(quote!(crate::engine::FString)),
|
||||||
|
@ -279,9 +279,9 @@ pub mod rust {
|
||||||
"i64", "i128", "usize", "isize",
|
"i64", "i128", "usize", "isize",
|
||||||
];
|
];
|
||||||
|
|
||||||
const CHARS: [char; 20] = [
|
const CHARS: [char; 21] = [
|
||||||
' ', '?', '+', '-', ':', '/', '^', '(', ')', '[', ']', '<', '>', '&', '.', '#', '\'', '"',
|
' ', '?', '+', '-', ':', '/', '^', '(', ')', '[', ']', '<', '>', '&', '.', '#', '\'', '"',
|
||||||
'%', ',',
|
'%', ',', '|',
|
||||||
];
|
];
|
||||||
|
|
||||||
pub struct Builder {
|
pub struct Builder {
|
||||||
|
@ -456,7 +456,16 @@ pub mod rust {
|
||||||
}
|
}
|
||||||
});
|
});
|
||||||
} else {
|
} else {
|
||||||
std::fs::write(path.join(format!("{name}.rs")), tokens.to_string())?;
|
let path = path.join(format!("{name}.rs"));
|
||||||
|
|
||||||
|
log::info!("parsing {name}..");
|
||||||
|
let file = syn::parse_file(&tokens.to_string())
|
||||||
|
.context("syn failed to parse generated code")?;
|
||||||
|
|
||||||
|
log::info!("pretty printing {name}..");
|
||||||
|
let contents = prettyplease::unparse(&file);
|
||||||
|
log::info!("writing to {}..", path.display());
|
||||||
|
std::fs::write(path, contents)?;
|
||||||
|
|
||||||
mod_rs.extend(quote! {
|
mod_rs.extend(quote! {
|
||||||
pub mod #name;
|
pub mod #name;
|
||||||
|
@ -464,7 +473,10 @@ pub mod rust {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
std::fs::write(path.join("mod.rs"), mod_rs.to_string())?;
|
let contents = prettyplease::unparse(
|
||||||
|
&syn::parse_file(&mod_rs.to_string()).context("syn failed to parse root module")?,
|
||||||
|
);
|
||||||
|
std::fs::write(path.join("mod.rs"), contents)?;
|
||||||
|
|
||||||
Ok(())
|
Ok(())
|
||||||
}
|
}
|
||||||
|
@ -617,7 +629,7 @@ pub mod rust {
|
||||||
let size = class.size;
|
let size = class.size;
|
||||||
|
|
||||||
let typedef = quote! {
|
let typedef = quote! {
|
||||||
pub struct #name(pub ::core::cell::UnsafeCell<u8; #size>);
|
pub struct #name(pub ::core::cell::UnsafeCell<[u8; #size]>);
|
||||||
};
|
};
|
||||||
|
|
||||||
let impls = quote! {
|
let impls = quote! {
|
||||||
|
@ -930,6 +942,7 @@ pub mod rust {
|
||||||
let name = name.to_string();
|
let name = name.to_string();
|
||||||
let not_found = format!("static object \"{name}\" not found!");
|
let not_found = format!("static object \"{name}\" not found!");
|
||||||
quote! {
|
quote! {
|
||||||
|
{
|
||||||
static OBJECT: ::once_cell::sync::OnceCell<::core::option::Option<UObject>> = ::once_cell::sync::OnceCell::new();
|
static OBJECT: ::once_cell::sync::OnceCell<::core::option::Option<UObject>> = ::once_cell::sync::OnceCell::new();
|
||||||
OBJECT.get_or_init(|| {
|
OBJECT.get_or_init(|| {
|
||||||
match find_object(::obfstr::obfstr!(#name)) {
|
match find_object(::obfstr::obfstr!(#name)) {
|
||||||
|
@ -942,6 +955,7 @@ pub mod rust {
|
||||||
.map(|object| unsafe {object.cast()})
|
.map(|object| unsafe {object.cast()})
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
}
|
||||||
|
|
||||||
fn iter_super_types(&self, class: &Class) -> impl Iterator<Item = &UnrealType> {
|
fn iter_super_types(&self, class: &Class) -> impl Iterator<Item = &UnrealType> {
|
||||||
let super_traits = core::iter::from_fn({
|
let super_traits = core::iter::from_fn({
|
||||||
|
|
Loading…
Reference in a new issue