allocator api for TArray, fixes

- check if class is AActor when checking if type is an actor type with A prefix
- param types public, with public fields
- sort types alphabetically when generating files
This commit is contained in:
Janis 2023-04-23 05:12:48 +02:00
parent 813b0bdaf6
commit 396ee1e809
3 changed files with 124 additions and 9 deletions

View file

@ -3,6 +3,7 @@
const_ptr_as_ref, const_ptr_as_ref,
const_nonnull_new, const_nonnull_new,
let_chains, let_chains,
allocator_api,
if_let_guard if_let_guard
)] )]
@ -175,7 +176,8 @@ pub mod sdk {
let is_actor = strct let is_actor = strct
.iter_super_structs() .iter_super_structs()
.contains(&unsafe { actor_static_class().unwrap().cast() }); .contains(&unsafe { actor_static_class().unwrap().cast() })
|| strct.is_a_maybe(&actor_static_class());
let is_class = strct.is_a(&UClass::static_class().unwrap()); let is_class = strct.is_a(&UClass::static_class().unwrap());
let super_struct = if let Some(spr) = *strct.super_field() { let super_struct = if let Some(spr) = *strct.super_field() {
@ -419,10 +421,10 @@ pub mod sdk {
log::debug!("field: {ty:?}: {prop}"); log::debug!("field: {ty:?}: {prop}");
let param = MethodParameter { let param = MethodParameter {
name: prop name: format!(
.get_name() "Arg{}",
.context("failed to get parameter name")? prop.get_name().context("failed to get parameter name")?
.to_lowercase(), ),
ty, ty,
}; };

View file

@ -1,6 +1,7 @@
use std::io::{BufWriter, Write}; use std::io::{BufWriter, Write};
use anyhow::Context; use anyhow::Context;
use itertools::Itertools;
use crate::{ use crate::{
sdk::{ sdk::{
@ -439,10 +440,10 @@ pub fn generate_method_params<W: Write>(
w: &mut W, w: &mut W,
) -> anyhow::Result<()> { ) -> anyhow::Result<()> {
let name = format!("{}_{}_Params", class.rust_name(), method.name); let name = format!("{}_{}_Params", class.rust_name(), method.name);
write!(w, "#[repr(C)]\n#[derive(Debug)]\nstruct {name} {{\n")?; write!(w, "#[repr(C)]\n#[derive(Debug)]\npub struct {name} {{\n")?;
for param in &method.parameters { for param in &method.parameters {
write!(w, "\t{}: ", param.as_param().name)?; write!(w, "\tpub {}: ", param.as_param().name)?;
param.as_param().ty.rust_type(sdk, w)?; param.as_param().ty.rust_type(sdk, w)?;
write!(w, ",\n")?; write!(w, ",\n")?;
} }
@ -510,7 +511,10 @@ pub fn generate_package_rust_module<W: Write>(
)?; )?;
} }
for (_, ty) in &pkg.types { for (_, ty) in pkg.types.iter().sorted_by(|a, b| {
a.0.get_full_name_or_default()
.cmp(&b.0.get_full_name_or_default())
}) {
match ty { match ty {
Types::Class(class) => { Types::Class(class) => {
generate_class(&class, sdk, w)?; generate_class(&class, sdk, w)?;

View file

@ -1,4 +1,20 @@
use std::{ops::Index, ptr::NonNull, slice::SliceIndex}; use std::{
ops::{Index, IndexMut},
ptr::NonNull,
slice::SliceIndex,
};
use once_cell::sync::OnceCell;
static ALLOCATOR: OnceCell<Box<dyn std::alloc::Allocator + Send + Sync + 'static>> =
OnceCell::new();
pub fn set_allocator<A>(ally: A)
where
A: std::alloc::Allocator + Send + Sync + 'static,
{
_ = ALLOCATOR.set(Box::new(ally));
}
#[repr(C)] #[repr(C)]
#[derive(Debug)] #[derive(Debug)]
@ -19,7 +35,86 @@ impl ToString for FString {
} }
} }
impl<T> Drop for TArray<T> {
fn drop(&mut self) {
if let Some(ptr) = self.data {
unsafe {
ALLOCATOR
.get()
.expect("allocator")
.deallocate(ptr.cast(), std::alloc::Layout::for_value(ptr.as_ref()));
}
}
}
}
impl<T> TArray<T> { impl<T> TArray<T> {
pub fn new() -> Self {
Self {
data: None,
count: 0,
max: 0,
}
}
pub unsafe fn set_len(&mut self, size: usize) {
self.count = (size as u32).min(self.max);
}
pub fn reserve(&mut self, size: usize) {
self.ensure_space(size);
}
fn ensure_space(&mut self, size: usize) {
if self.data.is_none() || size >= self.max as usize {
self.grow(size.max(self.max as usize * 2));
}
}
fn grow(&mut self, new_size: usize) {
if new_size <= self.max as usize {
return;
}
if self.max == 0 || self.data.is_none() {
if let Some(alloc) = ALLOCATOR.get() {
match alloc.allocate(std::alloc::Layout::array::<T>(32).unwrap()) {
Ok(ptr) => {
self.data = Some(ptr.cast());
self.max = ptr.len() as u32;
}
Err(e) => {
log::error!("failed to allocate with UE4 allocator: {e}.");
}
}
}
} else if let Some(alloc) = ALLOCATOR.get() {
unsafe {
match alloc.grow_zeroed(
self.data.unwrap().cast(),
std::alloc::Layout::array::<T>(self.max as usize).unwrap(),
std::alloc::Layout::array::<T>(new_size).unwrap(),
) {
Ok(ptr) => {
self.data = Some(ptr.cast());
self.max = ptr.len() as u32;
}
Err(e) => {
log::error!("failed to allocate with UE4 allocator: {e}.");
}
}
}
}
}
pub fn push(&mut self, value: T) {
self.ensure_space(self.count as usize + 1);
self.data.map(|ptr| unsafe {
ptr.as_ptr().offset(self.count as isize).write(value);
self.count += 1;
});
}
pub fn len(&self) -> usize { pub fn len(&self) -> usize {
self.count as usize self.count as usize
} }
@ -53,6 +148,14 @@ impl<T, I: SliceIndex<[T]>> Index<I> for TArray<T> {
} }
} }
impl<T, I: SliceIndex<[T]>> IndexMut<I> for TArray<T> {
fn index_mut(&mut self, i: I) -> &mut Self::Output {
let data = self.as_slice_mut();
&mut data[i]
}
}
impl<T> std::ops::Deref for TArray<T> { impl<T> std::ops::Deref for TArray<T> {
type Target = [T]; type Target = [T];
@ -61,6 +164,12 @@ impl<T> std::ops::Deref for TArray<T> {
} }
} }
impl<T> std::ops::DerefMut for TArray<T> {
fn deref_mut(&mut self) -> &mut Self::Target {
self.as_slice_mut()
}
}
pub struct IntoIter<T> { pub struct IntoIter<T> {
//array_ref: &'a TArray<T>, //array_ref: &'a TArray<T>,
ptr: *const T, ptr: *const T,