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