absolute paths and getters for path/type name

This commit is contained in:
Janis 2023-06-26 12:55:30 +02:00
parent d4505e58d4
commit 5b8ec05589

View file

@ -157,24 +157,9 @@ pub mod rust {
.packages .packages
.iter() .iter()
.flat_map(|(_, pkg)| { .flat_map(|(_, pkg)| {
pkg.types.values().map(|ty| { pkg.types
let name = match ty { .values()
UnrealType::Class(class) => { .map(|ty| (ty.obj_ref(), Self::get_prefixed_name(&ty)))
format!("U{}", canonicalize_name(&class.name))
}
UnrealType::Struct(class) => {
format!("F{}", canonicalize_name(&class.name))
}
UnrealType::Actor(class) => {
format!("A{}", canonicalize_name(&class.name))
}
UnrealType::Enum(class) => {
format!("E{}", canonicalize_name(&class.name))
}
};
(ty.obj_ref(), name)
})
}) })
.collect::<BTreeMap<_, _>>(); .collect::<BTreeMap<_, _>>();
@ -184,6 +169,38 @@ pub mod rust {
} }
} }
/// returns the absolute path of a type with the assumption that all
/// packages are children of the path `::crate::sdk`
fn get_type_path(&self, key: &ObjectRef) -> Option<String> {
let pkg = &self.sdk.packages.get(&key.package)?.name;
self.get_type_name(key)
.map(|name| format!("::crate::sdk::{pkg}::{name}"))
}
/// returns the precached, prefixed and cannonicalized (for this
/// language, Rust) name for this object-ref
fn get_type_name(&self, key: &ObjectRef) -> Option<String> {
self.type_name_cache.get(key).cloned()
}
/// prefixes the typename according to its kind (UObject, AActor, FStruct, EEnum)
fn get_prefixed_name(ty: &UnrealType) -> String {
match ty {
UnrealType::Class(_) => {
format!("U{}", canonicalize_name(&ty.unique_name()))
}
UnrealType::Struct(_) => {
format!("F{}", canonicalize_name(&ty.unique_name()))
}
UnrealType::Actor(_) => {
format!("A{}", canonicalize_name(&ty.unique_name()))
}
UnrealType::Enum(_) => {
format!("E{}", canonicalize_name(&ty.unique_name()))
}
}
}
pub fn build(self) -> anyhow::Result<()> { pub fn build(self) -> anyhow::Result<()> {
for pkg in self.sdk.packages.values() { for pkg in self.sdk.packages.values() {
self.generate_package(pkg)?; self.generate_package(pkg)?;
@ -195,64 +212,61 @@ pub mod rust {
fn type_name(&self, ty: &Type) -> anyhow::Result<String> { fn type_name(&self, ty: &Type) -> anyhow::Result<String> {
let type_name = match ty { let type_name = match ty {
Type::Ptr(inner) | Type::Ref(inner) => { Type::Ptr(inner) | Type::Ref(inner) => {
format!("Option<NonNull<{}>>", self.type_name(&inner)?) format!(
"::core::option::Option<NonNull<{}>>",
self.type_name(&inner)?
)
} }
Type::WeakPtr(inner) => { Type::WeakPtr(inner) => {
format!( format!(
"TWeakObjectPtr<{}>", "::crate::engine::TWeakObjectPtr<{}>",
self.type_name_cache self.get_type_path(inner)
.get(inner)
.context("type name was not cached.")? .context("type name was not cached.")?
) )
} }
Type::SoftPtr(inner) => { Type::SoftPtr(inner) => {
format!( format!(
"TSoftObjectPtr<{}>", "::crate::engine::TSoftObjectPtr<{}>",
self.type_name_cache self.get_type_path(inner)
.get(inner)
.context("type name was not cached.")? .context("type name was not cached.")?
) )
} }
Type::LazyPtr(inner) => { Type::LazyPtr(inner) => {
format!( format!(
"TLazyObjectPtr<{}>", "::crate::engine::TLazyObjectPtr<{}>",
self.type_name_cache self.get_type_path(inner)
.get(inner)
.context("type name was not cached.")? .context("type name was not cached.")?
) )
} }
Type::AssetPtr(inner) => format!( Type::AssetPtr(inner) => format!(
"TAssetPtr<{}>", "::crate::engine::TAssetPtr<{}>",
self.type_name_cache self.get_type_path(inner)
.get(inner)
.context("type name was not cached.")? .context("type name was not cached.")?
), ),
Type::Array(inner) => format!("TArray<{}>", self.type_name(&inner)?), Type::Array(inner) => {
format!("::crate::engine::TArray<{}>", self.type_name(&inner)?)
}
Type::Primitive(prim) => { Type::Primitive(prim) => {
format!("{prim}") format!("{prim}")
} }
Type::RawArray { ty, len } => { Type::RawArray { ty, len } => {
format!("[{}; {}]", self.type_name(&ty)?, len) format!("[{}; {}]", self.type_name(&ty)?, len)
} }
Type::Name => "FName".to_string(), Type::Name => "::crate::engine::FName".to_string(),
Type::String => "FString".to_string(), Type::String => "::crate::engine::FString".to_string(),
Type::Text => "FText".to_string(), Type::Text => "::crate::engine::FText".to_string(),
Type::Enum { enum_type, .. } => self Type::Enum { enum_type, .. } => self
.type_name_cache .get_type_path(enum_type)
.get(enum_type) .context("type name was not cached.")?,
.context("type name was not cached.")?
.clone(),
Type::Class(class) => { Type::Class(class) => {
format!( format!(
"::core::option::Option<{}>", "::core::option::Option<{}>",
self.type_name_cache self.get_type_path(class)
.get(class)
.context("type name was not cached.")? .context("type name was not cached.")?
) )
} }
Type::Struct(class) => self Type::Struct(class) => self
.type_name_cache .get_type_path(class)
.get(class)
.context("type name was not cached.")? .context("type name was not cached.")?
.clone(), .clone(),
}; };
@ -262,8 +276,7 @@ pub mod rust {
fn generate_enum(&self, enum0: &Enum) -> anyhow::Result<TokenStream> { fn generate_enum(&self, enum0: &Enum) -> anyhow::Result<TokenStream> {
let name = self let name = self
.type_name_cache .get_type_name(&enum0.obj_ref)
.get(&enum0.obj_ref)
.context("enum name was not previously canonicalized and cached.")?; .context("enum name was not previously canonicalized and cached.")?;
let variants = enum0.values.iter().map(|(&value, name)| { let variants = enum0.values.iter().map(|(&value, name)| {
@ -658,9 +671,8 @@ pub mod rust {
} }
fn generate_class(&self, class: &Class) -> anyhow::Result<TokenStream> { fn generate_class(&self, class: &Class) -> anyhow::Result<TokenStream> {
let name = self let name = &self
.type_name_cache .get_type_name(&class.obj_ref)
.get(&class.obj_ref)
.context("enum name was not previously canonicalized and cached.")?; .context("enum name was not previously canonicalized and cached.")?;
let (field_trait, ctor) = self.generate_struct_fields(class, name)?; let (field_trait, ctor) = self.generate_struct_fields(class, name)?;
@ -696,13 +708,12 @@ pub mod rust {
None None
} }
}) })
.map(|ty| ty.unique_name()) // SAFETY: we already got this type by its obj_ref, so it must be there.
.map(|ty| self.get_type_path(&ty.obj_ref()).unwrap())
.map(|super_name| { .map(|super_name| {
let fields = format_ident!("{super_name}Fields"); let fields = format_ident!("{super_name}Fields");
let methods = format_ident!("{super_name}Methods"); let methods = format_ident!("{super_name}Methods");
// FIXME: full path here? meaning I need the package name aswell.
quote! { quote! {
impl #fields for #name {} impl #fields for #name {}
impl #methods for #name {} impl #methods for #name {}