From caf49d489006369d013a972a2454166ee991328f Mon Sep 17 00:00:00 2001 From: Janis Date: Thu, 29 Jun 2023 17:23:55 +0200 Subject: [PATCH] sdk-builder: ident shenanigans that arent working really :/ --- sdk-builder/Cargo.toml | 1 + sdk-builder/src/main.rs | 54 +++++++++++++++++++++++++++++------------ 2 files changed, 39 insertions(+), 16 deletions(-) diff --git a/sdk-builder/Cargo.toml b/sdk-builder/Cargo.toml index 8489e11..b3d76da 100644 --- a/sdk-builder/Cargo.toml +++ b/sdk-builder/Cargo.toml @@ -10,6 +10,7 @@ log = "0.4.0" anyhow = "1.0" env_logger = "0.10.0" itertools = "0.11.0" +rayon = "1.7.0" unreal-sdk = {path = "../unreal-sdk"} diff --git a/sdk-builder/src/main.rs b/sdk-builder/src/main.rs index caa1a30..dffbdd8 100644 --- a/sdk-builder/src/main.rs +++ b/sdk-builder/src/main.rs @@ -139,15 +139,20 @@ pub struct CanonicalNames { } pub mod rust { - use std::{borrow::Cow, collections::BTreeMap, path::Path}; + use std::{ + borrow::Cow, + collections::{BTreeMap, BTreeSet, HashMap, HashSet}, + path::Path, + }; use anyhow::Context; use itertools::Itertools; use proc_macro2::{Ident, TokenStream}; use quote::{format_ident, quote}; + use rayon::prelude::{IntoParallelIterator, IntoParallelRefIterator, ParallelIterator}; use unreal_sdk::sdk::repr::{ - Class, ClassField, ClassMethod, Enum, ObjectRef, PrimitiveType, ProcessedPackage, Sdk, - StructKind, Type, UnrealType, + Class, ClassField, ClassMethod, Enum, ObjectRef, PackageRef, PrimitiveType, + ProcessedPackage, Sdk, StructKind, Type, UnrealType, }; use crate::split_at_illegal_char; @@ -397,15 +402,17 @@ pub mod rust { _class: &Class, name: &str, ) -> anyhow::Result<(TokenStream, TokenStream)> { + let ident = format_ident!("{name}"); + let typedef = quote! { #[derive(Eq, PartialEq, Copy, Clone)] - pub struct #name(pub ::core::ptr::NonNull); + pub struct #ident(pub ::core::ptr::NonNull); }; let static_class_impl: TokenStream = Self::generate_find_object(name); let impls = quote! { - impl crate::engine::AsUObject for #name { + impl crate::engine::AsUObject for #ident { fn as_uobject(&self) -> crate::engine::UObject { crate::engine::UObject(self.0) } @@ -415,7 +422,7 @@ pub mod rust { } } - impl crate::engine::AsPtr for #name { + impl crate::engine::AsPtr for #ident { fn as_ptr(&self) -> *const u8 { unsafe { self.0.as_ref().get() as _ } } @@ -425,7 +432,7 @@ pub mod rust { } } - impl crate::engine::StaticClass for #name { + impl crate::engine::StaticClass for #ident { fn get_static_class() -> ::core::option::Option { let class: ::core::option::Option = #static_class_impl; @@ -447,25 +454,26 @@ pub mod rust { ctor: Option, ) -> anyhow::Result<(TokenStream, TokenStream)> { let size = class.size; + let ident = format_ident!("{name}"); let typedef = quote! { - pub struct #name(pub ::core::cell::UnsafeCell); + pub struct #ident(pub ::core::cell::UnsafeCell); }; let impls = quote! { - impl Eq for #name {} - impl PartialEq for #name { + impl Eq for #ident {} + impl PartialEq for #ident { fn eq(&self, other: &Self) -> bool { unsafe {(&*self.0.get()).eq(&*other.0.get())} } } - impl Clone for #name { + impl Clone for #ident { fn clone(&self) -> Self { Self(::core::cell::UnsafeCell::new(unsafe {&*self.0.get()}.clone())) } } - impl crate::engine::AsPtr for #name { + impl crate::engine::AsPtr for #ident { fn as_ptr(&self) -> *const u8 { self.0.get().cast() } @@ -475,7 +483,7 @@ pub mod rust { } } - impl #name { + impl #ident { pub fn zeroed() -> Self { unsafe { ::core::mem::MaybeUninit::::zeroed().assume_init() @@ -519,6 +527,7 @@ pub mod rust { ) -> anyhow::Result<(TokenStream, TokenStream)> { let method_name = canonicalize_name(&method.unique_name()); + // all parameters collected as (parameter, canonicalized_name, type_ident) let parameters = method .parameters .iter() @@ -530,11 +539,17 @@ pub mod rust { }) .collect::, _>>()?; + // all parameters converted into "arg: Type" format of tokens. let all_params = parameters .iter() - .map(|(param, name, ty)| (param, quote! {#name: #ty})) + .map(|(param, name, ty)| { + let name = format_ident!("{name}"); + let ty = format_ident!("{ty}"); + (param, quote! {#name: #ty}) + }) .collect::>(); + // params that the function will accept as arguments. let params = all_params .iter() .filter(|(param, _)| { @@ -542,19 +557,26 @@ pub mod rust { }) .map(|(_, tokens)| tokens.clone()); + // tokens of all params, for the Params struct definition. let all_params = all_params.iter().map(|(_, tokens)| tokens.clone()); + // param token streams for setting the fields of the params struct + // with the arguments of the function. let init_params = parameters.iter().map(|(_, name, _)| { + let name = format_ident!("{name}"); quote! {params.#name = #name;} }); - let (return_type, handle_return) = { + let (return_type, return_expression) = { let (names, types) = parameters .iter() .filter(|(param, _, _)| { param.is_return_param() || (param.is_out_param() && !param.is_const_param()) }) .map(|(_, name, ty)| { + let name = format_ident!("{name}"); + let ty = format_ident!("{ty}"); + ( quote! { #name @@ -599,7 +621,7 @@ pub mod rust { process_event(self.as_uobject(), func, &mut params); func.set_function_flags(flags); - #handle_return + #return_expression } };