diff --git a/sdk-builder/Cargo.toml b/sdk-builder/Cargo.toml index b3d76da..f781e98 100644 --- a/sdk-builder/Cargo.toml +++ b/sdk-builder/Cargo.toml @@ -17,3 +17,5 @@ unreal-sdk = {path = "../unreal-sdk"} quote = "1.0.28" proc-macro2 = "1.0.60" clap = { version = "4.3.9", features = ["derive"] } +prettyplease = "0.2.9" +syn = { version = "2.0.22", features = ["full"] } diff --git a/sdk-builder/src/main.rs b/sdk-builder/src/main.rs index 22dd5d2..9f00bd3 100644 --- a/sdk-builder/src/main.rs +++ b/sdk-builder/src/main.rs @@ -190,13 +190,13 @@ pub mod path_helper { .unwrap_or(quote!(crate::engine::UObject)); tokens.extend(quote!( - crate::engine::TAssetPtr<#inner>, + crate::engine::TAssetPtr<#inner> )); } Type::Array(inner) => { let inner = self.child(*inner.clone()); tokens.extend(quote!( - crate::engine::TArray<#inner>, + crate::engine::TArray<#inner> )); } Type::Primitive(prim) => { @@ -204,7 +204,7 @@ pub mod path_helper { } Type::RawArray { ty, len } => { let ty = self.child(*ty.clone()); - quote!([#ty; #len]); + tokens.extend(quote! { [ #ty; #len ] }); } Type::Name => tokens.extend(quote!(crate::engine::FName)), Type::String => tokens.extend(quote!(crate::engine::FString)), @@ -279,9 +279,9 @@ pub mod rust { "i64", "i128", "usize", "isize", ]; - const CHARS: [char; 20] = [ + const CHARS: [char; 21] = [ ' ', '?', '+', '-', ':', '/', '^', '(', ')', '[', ']', '<', '>', '&', '.', '#', '\'', '"', - '%', ',', + '%', ',', '|', ]; pub struct Builder { @@ -456,7 +456,16 @@ pub mod rust { } }); } 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! { 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(()) } @@ -617,7 +629,7 @@ pub mod rust { let size = class.size; let typedef = quote! { - pub struct #name(pub ::core::cell::UnsafeCell); + pub struct #name(pub ::core::cell::UnsafeCell<[u8; #size]>); }; let impls = quote! { @@ -930,6 +942,7 @@ pub mod rust { let name = name.to_string(); let not_found = format!("static object \"{name}\" not found!"); quote! { + { static OBJECT: ::once_cell::sync::OnceCell<::core::option::Option> = ::once_cell::sync::OnceCell::new(); OBJECT.get_or_init(|| { match find_object(::obfstr::obfstr!(#name)) { @@ -940,6 +953,7 @@ pub mod rust { } }) .map(|object| unsafe {object.cast()}) + } } }