make types bit nicer

This commit is contained in:
bgkillas 2025-07-22 21:42:09 -04:00
parent f2103c26ec
commit a238941595
5 changed files with 39 additions and 34 deletions

View file

@ -15,6 +15,7 @@ use std::alloc::Layout;
use std::cmp::Ordering;
use std::ffi::c_void;
use std::fmt::{Debug, Display, Formatter};
use std::ops::Index;
use std::{alloc, ptr, slice};
pub use world::*;
#[repr(C)]
@ -179,6 +180,12 @@ pub struct StdVec<T> {
pub end: *mut T,
pub cap: *mut T,
}
impl<T> Index<usize> for StdVec<T> {
type Output = T;
fn index(&self, index: usize) -> &Self::Output {
self.get(index).unwrap()
}
}
impl<T> AsRef<[T]> for StdVec<T> {
fn as_ref(&self) -> &[T] {
unsafe { slice::from_raw_parts(self.start, self.len()) }
@ -211,6 +218,14 @@ impl<T> StdVec<T> {
pub fn is_empty(&self) -> bool {
self.start == self.end
}
pub fn get_static(&self, index: usize) -> Option<&'static T> {
let ptr = unsafe { self.start.add(index) };
if self.end > ptr {
unsafe { ptr.as_ref() }
} else {
None
}
}
pub fn get(&self, index: usize) -> Option<&T> {
let ptr = unsafe { self.start.add(index) };
if self.end > ptr {
@ -233,14 +248,14 @@ impl<T> StdVec<T> {
let old_cap = self.capacity();
let new_cap = if old_cap == 0 { 4 } else { old_cap * 2 }; //TODO deal with n > 1
let layout = Layout::array::<T>(new_cap).unwrap();
let new_ptr = unsafe { alloc::alloc(layout) } as *mut T;
let new_ptr = unsafe { alloc::alloc(layout) }.cast();
if old_len > 0 {
unsafe {
ptr::copy_nonoverlapping(self.start, new_ptr, old_len);
}
let old_layout = Layout::array::<T>(old_cap).unwrap();
unsafe {
alloc::dealloc(self.start as *mut u8, old_layout);
alloc::dealloc(self.start.cast(), old_layout);
}
}
self.start = new_ptr;
@ -290,9 +305,9 @@ impl<T> StdVec<T> {
#[repr(C)]
#[derive(Debug)]
pub struct StdMapNode<K, V> {
pub left: *const StdMapNode<K, V>,
pub parent: *const StdMapNode<K, V>,
pub right: *const StdMapNode<K, V>,
pub left: *mut StdMapNode<K, V>,
pub parent: *mut StdMapNode<K, V>,
pub right: *mut StdMapNode<K, V>,
pub color: bool,
pub end: bool,
unk: [u8; 2],
@ -315,7 +330,7 @@ impl<K: Debug + 'static, V: Debug + 'static> Debug for StdMap<K, V> {
#[derive(Debug)]
pub struct StdMapIter<K, V> {
pub root: *const StdMapNode<K, V>,
pub root: *mut StdMapNode<K, V>,
pub current: *const StdMapNode<K, V>,
pub parents: Vec<*const StdMapNode<K, V>>,
}

View file

@ -131,19 +131,13 @@ pub struct CellData {
pub wang_noise_percent: f32,
pub wang_curvature: f32,
pub wang_noise_type: isize,
pub tags_start: *mut StdString,
pub tags_end: *mut StdString,
pub tags_cap: *mut StdString,
pub tags: StdVec<StdString>,
pub danger_fire: bool,
pub danger_radioactive: bool,
pub danger_poison: bool,
pub danger_water: bool,
pub stain_effects_start: *mut StdString,
pub stain_effects_end: *mut StdString,
pub stain_effects_cap: *mut StdString,
pub ingestion_effects_start: *mut StdString,
pub ingestion_effects_end: *mut StdString,
pub ingestion_effects_cap: *mut StdString,
pub stain_effects: StdVec<StdString>,
pub ingestion_effects: StdVec<StdString>,
pub always_ignites_damagemodel: bool,
pub ignore_self_reaction_warning: bool,
padding8: [u8; 2],
@ -528,7 +522,7 @@ pub struct GameWorld {
pub cam_x2: f32,
pub cam_y2: f32,
unknown1: [isize; 13],
pub grid_world: *mut GridWorld,
pub grid_world: &'static mut GridWorld,
//likely more data
}
@ -622,10 +616,10 @@ pub struct GameGlobal {
pub frame_num: usize,
pub frame_num_start: usize,
unknown1: isize,
pub m_game_world: *mut GameWorld,
pub m_grid_world: *mut GridWorld,
pub m_textures: *mut Textures,
pub m_cell_factory: *mut CellFactory,
pub m_game_world: &'static mut GameWorld,
pub m_grid_world: &'static mut GridWorld,
pub m_textures: &'static mut Textures,
pub m_cell_factory: &'static mut CellFactory,
unknown2: [isize; 11],
pub pause_state: isize,
unk: [isize; 5],
@ -835,7 +829,7 @@ pub struct GridWorld {
pub world_update_count: isize,
pub chunk_map: ChunkMap,
unknown2: [isize; 41],
pub m_thread_impl: *mut GridWorldThreaded,
pub m_thread_impl: &'static mut GridWorldThreaded,
}
#[repr(C)]
#[derive(Debug)]

View file

@ -1,11 +1,11 @@
use crate::noita::types;
use crate::noita::types::StdVec;
use eyre::ContextCompat;
use rayon::iter::{IndexedParallelIterator, IntoParallelRefIterator, ParallelIterator};
#[derive(Debug)]
pub struct ParticleWorldState {
pub global_ptr: *mut types::GameGlobal,
pub world_ptr: *mut types::GridWorld,
pub material_list: &'static [types::CellData],
pub material_list: StdVec<types::CellData>,
pub cell_vtables: types::CellVTables,
}
unsafe impl Sync for ParticleWorldState {}
@ -79,16 +79,9 @@ impl ParticleWorldState {
pub fn new() -> eyre::Result<Self> {
let (cell_vtables, global_ptr) = crate::noita::init_data::get_functions()?;
let global = unsafe { global_ptr.as_mut() }.wrap_err("no global?")?;
let cell_factory =
unsafe { global.m_cell_factory.as_mut() }.wrap_err("no cell factory?")?;
let material_list_ptr = cell_factory.cell_data.start;
let material_list =
unsafe { std::slice::from_raw_parts(material_list_ptr, cell_factory.cell_data.len()) };
let world_ptr = global.m_grid_world;
Ok(ParticleWorldState {
global_ptr,
world_ptr,
material_list,
world_ptr: global.m_grid_world,
material_list: global.m_cell_factory.cell_data.copy(),
cell_vtables,
})
}