From b1bf442a27fdf489f0589cfc9a1e57c7dd55690d Mon Sep 17 00:00:00 2001 From: Janis Date: Tue, 23 May 2023 00:29:19 +0200 Subject: [PATCH] idkkkkkkkkkkkkkkk --- src/lib.rs | 15 ++++---- src/sdk/output/rust.rs | 78 ++++++++++++++++++++++++++++-------------- 2 files changed, 60 insertions(+), 33 deletions(-) diff --git a/src/lib.rs b/src/lib.rs index 67109c9..bdba982 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -34,12 +34,13 @@ pub mod sdk { actor_static_class, any_type::{self, AnyField, AnyObject, AnyProperty, AnyStruct}, traits::{ - AsUObject, StaticClass, UArrayPropertyTrait, UBytePropertyTrait, - UEnumPropertyTrait, UEnumTrait, UFunctionTrait, UObjectNonConst, - UObjectPropertyBaseTrait, UObjectTrait, UPropertyTrait, UStructNonConst, - UStructPropertyTrait, UStructTrait, + AsUObject, StaticClass, UArrayPropertyTrait, UBoolPropertyTrait, + UBytePropertyTrait, UEnumPropertyTrait, UEnumTrait, UFunctionTrait, + UObjectNonConst, UObjectPropertyBaseTrait, UObjectTrait, UPropertyTrait, + UStructNonConst, UStructPropertyTrait, UStructTrait, }, - EFunctionFlags, EPropertyFlags, UClass, UEnum, UFunction, UObject, UStruct, + EFunctionFlags, EPropertyFlags, UBoolProperty, UClass, UEnum, UFunction, UObject, + UStruct, }, }; @@ -237,7 +238,7 @@ pub mod sdk { } } } - AnyProperty::Bool(_) => Type::Primitive(PrimitiveType::Bool), + AnyProperty::Bool(prop) => Type::Primitive(PrimitiveType::Bool(prop)), AnyProperty::Interface(_) => { return Err(anyhow::anyhow!("skipping interfaces for now")) } @@ -748,7 +749,7 @@ pub mod sdk { #[derive(Debug, Clone, Copy)] pub enum PrimitiveType { - Bool, + Bool(UBoolProperty), U8, U16, U32, diff --git a/src/sdk/output/rust.rs b/src/sdk/output/rust.rs index 9eef3d8..6062614 100644 --- a/src/sdk/output/rust.rs +++ b/src/sdk/output/rust.rs @@ -12,7 +12,7 @@ use crate::{ Sdk, Type, Types, }, v2_types::{ - traits::{AsUObject, UObjectNonConst, UStructNonConst}, + traits::{AsUObject, UBoolPropertyTrait, UObjectNonConst, UStructNonConst}, UObject, }, }; @@ -87,7 +87,7 @@ impl RustType for Type { w, "{}", match ty { - PrimitiveType::Bool => "bool", + PrimitiveType::Bool(_) => "bool", PrimitiveType::U8 => "u8", PrimitiveType::U16 => "u16", PrimitiveType::U32 => "u32", @@ -292,32 +292,58 @@ pub fn generate_class_impl(class: &Class, sdk: &Sdk, w: &mut W) -> any let name = class.rust_name(); + // FIELDS writeln!(w, "pub trait {}Fields: AsPtr {{", name)?; for field in fields { - write!(w, "fn {}(&self) -> &", field.name)?; - field.ty.rust_type(sdk, w)?; - writeln!( - w, - " {{unsafe {{ &*self.as_ptr().offset({}).cast() }} }}", - field.offset - )?; + // need to handle bools in a special way because they might be bitfields - write!(w, "fn {}_mut(&mut self) -> &mut ", field.name)?; - field.ty.rust_type(sdk, w)?; - writeln!( - w, - " {{unsafe {{ &mut *self.as_mut_ptr().offset({}).cast() }} }}", - field.offset - )?; + match field.ty { + Type::Primitive(PrimitiveType::Bool(prop)) if *prop.byte_mask() != 0xff => { + write!(w, "fn {}(&self) -> bool", field.name)?; + writeln!( + w, + " {{unsafe {{ *self.as_ptr().offset({}) & (1u8 << {}u8) != 0 }} }}", + field.offset, + prop.field_mask().trailing_zeros(), + )?; - write!(w, "fn set_{0}(&mut self, {0}: ", field.name)?; - field.ty.rust_type(sdk, w)?; - write!(w, ") -> ()")?; - writeln!( - w, - " {{*unsafe {{ &mut *self.as_mut_ptr().offset({}).cast() }} = {}; }}", - field.offset, field.name - )?; + write!(w, "fn set_{0}(&mut self, {0}: bool", field.name)?; + write!(w, ") -> ()")?; + writeln!( + w, + " {{unsafe {{ if {1} {{ *self.as_mut_ptr().offset({0}) |= ({1} as u8) << {2}u8; }} else {{ *self.as_mut_ptr().offset({0}) &= !(({1} as u8) << {2}u8); }} }} }}", + field.offset, + field.name, + prop.field_mask().trailing_zeros(), + )?; + } + _ => { + write!(w, "fn {}(&self) -> &", field.name)?; + field.ty.rust_type(sdk, w)?; + writeln!( + w, + " {{unsafe {{ &*self.as_ptr().offset({}).cast() }} }}", + field.offset + )?; + + write!(w, "fn set_{0}(&mut self, {0}: ", field.name)?; + field.ty.rust_type(sdk, w)?; + write!(w, ") -> ()")?; + writeln!( + w, + " {{*unsafe {{ &mut *self.as_mut_ptr().offset({}).cast() }} = {}; }}", + field.offset, field.name + )?; + + write!(w, "fn {}_mut(&mut self) -> &mut ", field.name)?; + field.ty.rust_type(sdk, w)?; + writeln!( + w, + " {{unsafe {{ &mut *self.as_mut_ptr().offset({}).cast() }} }}", + field.offset + )?; + } + } } writeln!(w, "}}")?; writeln!(w, "impl {name}Fields for {name} {{}}")?; @@ -365,10 +391,10 @@ fn generate_find_object(name: &str, w: &mut W) -> anyhow::Result<()> { {{ static OBJECT: OnceCell> = OnceCell::new(); OBJECT.get_or_init(|| {{ - match find_object("{name}") {{ + match find_object(obfstr::obfstr!("{name}")) {{ Some(class) => {{Some(class)}}, None => {{ - log::error!("static object {name} not found!"); + log::error!("{{}}", obfstr::obfstr!("static object {name} not found!")); None }} }}