sdk-builder: ident shenanigans that arent working really :/

This commit is contained in:
Janis 2023-06-29 17:23:55 +02:00
parent c34886f954
commit caf49d4890
2 changed files with 39 additions and 16 deletions

View file

@ -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"}

View file

@ -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<u8>);
pub struct #ident(pub ::core::ptr::NonNull<u8>);
};
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<crate::engine::UClass> {
let class: ::core::option::Option<crate::engine::UClass> =
#static_class_impl;
@ -447,25 +454,26 @@ pub mod rust {
ctor: Option<TokenStream>,
) -> anyhow::Result<(TokenStream, TokenStream)> {
let size = class.size;
let ident = format_ident!("{name}");
let typedef = quote! {
pub struct #name(pub ::core::cell::UnsafeCell<u8; #size>);
pub struct #ident(pub ::core::cell::UnsafeCell<u8; #size>);
};
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::<Self>::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::<Result<Vec<_>, _>>()?;
// 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::<Vec<_>>();
// 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
}
};