sdk-builder: cleanup/fixes

This commit is contained in:
Janis 2023-06-25 19:11:55 +02:00
parent df4cc0677b
commit 572fe68591

View file

@ -100,9 +100,8 @@ pub mod rust {
use std::{borrow::Cow, collections::BTreeMap}; use std::{borrow::Cow, collections::BTreeMap};
use anyhow::Context; use anyhow::Context;
use itertools::Itertools;
use proc_macro2::TokenStream; use proc_macro2::TokenStream;
use quote::{format_ident, quote, TokenStreamExt}; use quote::{format_ident, quote};
use unreal_sdk::sdk::repr::{ use unreal_sdk::sdk::repr::{
Class, ClassField, ClassMethod, Enum, ObjectRef, PrimitiveType, ProcessedPackage, Sdk, Class, ClassField, ClassMethod, Enum, ObjectRef, PrimitiveType, ProcessedPackage, Sdk,
StructKind, Type, UnrealType, StructKind, Type, UnrealType,
@ -110,18 +109,18 @@ pub mod rust {
use crate::split_at_illegal_char; use crate::split_at_illegal_char;
const KEYWORDS: [&'static str; 51] = [ // const KEYWORDS: [&'static str; 51] = [
"as", "break", "const", "continue", "crate", "else", "enum", "extern", "false", "fn", // "as", "break", "const", "continue", "crate", "else", "enum", "extern", "false", "fn",
"for", "if", "impl", "in", "let", "loop", "match", "mod", "move", "mut", "pub", "ref", // "for", "if", "impl", "in", "let", "loop", "match", "mod", "move", "mut", "pub", "ref",
"return", "self", "Self", "static", "struct", "super", "trait", "true", "type", "unsafe", // "return", "self", "Self", "static", "struct", "super", "trait", "true", "type", "unsafe",
"use", "where", "while", "async", "await", "dyn", "abstract", "become", "box", "do", // "use", "where", "while", "async", "await", "dyn", "abstract", "become", "box", "do",
"final", "macro", "override", "priv", "typeof", "unsized", "virtual", "yield", "try", // "final", "macro", "override", "priv", "typeof", "unsized", "virtual", "yield", "try",
]; // ];
const TYPES: [&'static str; 17] = [ // const TYPES: [&'static str; 17] = [
"bool", "f64", "f32", "str", "char", "u8", "u16", "u32", "u64", "u128", "i8", "i16", "i32", // "bool", "f64", "f32", "str", "char", "u8", "u16", "u32", "u64", "u128", "i8", "i16", "i32",
"i64", "i128", "usize", "isize", // "i64", "i128", "usize", "isize",
]; // ];
const WORDS: [&'static str; 68] = [ const WORDS: [&'static str; 68] = [
"as", "break", "const", "continue", "crate", "else", "enum", "extern", "false", "fn", "as", "break", "const", "continue", "crate", "else", "enum", "extern", "false", "fn",
@ -185,6 +184,14 @@ pub mod rust {
} }
} }
pub fn build(self) -> anyhow::Result<()> {
for pkg in self.sdk.packages.values() {
self.generate_package(pkg)?;
}
Ok(())
}
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) => {
@ -230,10 +237,7 @@ pub mod rust {
Type::Name => "FName".to_string(), Type::Name => "FName".to_string(),
Type::String => "FString".to_string(), Type::String => "FString".to_string(),
Type::Text => "FText".to_string(), Type::Text => "FText".to_string(),
Type::Enum { Type::Enum { enum_type, .. } => self
underlying,
enum_type,
} => self
.type_name_cache .type_name_cache
.get(enum_type) .get(enum_type)
.context("type name was not cached.")? .context("type name was not cached.")?
@ -285,7 +289,7 @@ pub mod rust {
/// - the impls for that type, like Clone, AsUObject, AsPtr and StaticClass /// - the impls for that type, like Clone, AsUObject, AsPtr and StaticClass
fn generate_object( fn generate_object(
&self, &self,
class: &Class, _class: &Class,
name: &str, name: &str,
) -> anyhow::Result<(TokenStream, TokenStream)> { ) -> anyhow::Result<(TokenStream, TokenStream)> {
let typedef = quote! { let typedef = quote! {
@ -372,7 +376,7 @@ pub mod rust {
} }
} }
ctor #ctor
} }
}; };
@ -514,7 +518,7 @@ pub mod rust {
byte_mask, byte_mask,
field_mask, field_mask,
.. ..
}) => { }) if byte_mask != 0xFF => {
let shift = field_mask.trailing_zeros(); let shift = field_mask.trailing_zeros();
let getter = quote! { let getter = quote! {
@ -572,15 +576,15 @@ pub mod rust {
fn generate_struct_ctor( fn generate_struct_ctor(
&self, &self,
class: &Class, _class: &Class,
name: &str, _name: &str,
fields: &Vec<(&ClassField, Cow<str>, String)>, fields: &Vec<(&ClassField, Cow<str>, String)>,
) -> TokenStream { ) -> TokenStream {
let fields_defs = fields.iter().map(|(_, name, ty)| quote! {#name: #ty}); let fields_defs = fields.iter().map(|(_, name, ty)| quote! {#name: #ty});
let this_field_asignments = fields.iter().map(|(_, name, ty)| { let this_field_asignments = fields.iter().map(|(_, name, _ty)| {
let setter = format_ident!("set_{}", name); let setter = format_ident!("set_{}", name);
quote! {this.setter(#name);} quote! {this.#setter(#name);}
}); });
// FIXME: handle super struct fields aswell, ARK doesnt seem to have those anyways. // FIXME: handle super struct fields aswell, ARK doesnt seem to have those anyways.
@ -638,13 +642,14 @@ pub mod rust {
} }
fn generate_find_object(name: &str) -> TokenStream { fn generate_find_object(name: &str) -> TokenStream {
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)) {
object @ Some(_) => {object}, object @ Some(_) => {object},
None => { None => {
::log::error!("{}", obfstr::obfstr!("static object #name not found!")); ::log::error!("{}", ::obfstr::obfstr!(#not_found));
} }
} }
}) })
@ -714,6 +719,8 @@ pub mod rust {
#impls #impls
#(#super_traits)*
#methods #methods
#field_trait #field_trait
@ -725,12 +732,7 @@ pub mod rust {
// TODO: canonicalize_name(&pkg.name); // TODO: canonicalize_name(&pkg.name);
let pkg_name = "PACKAGE_NAME_PLACEHOLDER".to_string(); let pkg_name = "PACKAGE_NAME_PLACEHOLDER".to_string();
for (id, ty) in &pkg.types { for (_id, ty) in &pkg.types {
let name = self
.type_name_cache
.get(id)
.expect("type name was not cached.");
let tokens = match ty { let tokens = match ty {
UnrealType::Class(class) UnrealType::Class(class)
| UnrealType::Actor(class) | UnrealType::Actor(class)