add entity get with name

This commit is contained in:
bgkillas 2025-08-17 18:16:15 -04:00
parent d8c64c4682
commit 52cd62ab15
4 changed files with 56 additions and 5 deletions

View file

@ -2,8 +2,8 @@ use std::{os::raw::c_void, ptr};
use crate::lua::LuaState;
use crate::noita::types::{
ComponentTypeManager, EntityManager, GameGlobal, GlobalStats, Inventory, Mods, Platform,
StdString, StdVec, TagManager, TranslationManager,
ComponentSystemManager, ComponentTypeManager, EntityManager, GameGlobal, GlobalStats,
Inventory, Mods, Platform, StdString, StdVec, TagManager, TranslationManager,
};
use iced_x86::{Decoder, DecoderOptions, Mnemonic};
@ -52,6 +52,7 @@ pub struct GlobalsRef {
pub inventory: &'static Inventory,
pub mods: &'static Mods,
pub max_component: &'static usize,
pub component_manager: &'static ComponentSystemManager,
}
#[derive(Debug)]
pub struct GlobalsMut {
@ -69,6 +70,7 @@ pub struct GlobalsMut {
pub inventory: &'static mut Inventory,
pub mods: &'static mut Mods,
pub max_component: &'static mut usize,
pub component_manager: &'static mut ComponentSystemManager,
}
#[derive(Debug, Default)]
@ -87,6 +89,7 @@ pub struct Globals {
pub inventory: *mut Inventory,
pub mods: *mut Mods,
pub max_component: *mut usize,
pub component_manager: *mut ComponentSystemManager,
}
#[allow(clippy::mut_from_ref)]
impl Globals {
@ -117,6 +120,9 @@ impl Globals {
.unwrap()
}
}
pub fn component_manager(&self) -> &'static ComponentSystemManager {
unsafe { self.component_manager.as_ref().unwrap() }
}
pub fn translation_manager(&self) -> &'static TranslationManager {
unsafe { self.translation_manager.as_ref().unwrap() }
}
@ -186,6 +192,9 @@ impl Globals {
pub fn max_component_mut(&self) -> &'static mut usize {
unsafe { self.max_component.as_mut().unwrap() }
}
pub fn component_manager_mut(&self) -> &'static mut ComponentSystemManager {
unsafe { self.component_manager.as_mut().unwrap() }
}
pub fn as_ref(&self) -> GlobalsRef {
GlobalsRef {
world_seed: self.world_seed(),
@ -202,6 +211,7 @@ impl Globals {
inventory: self.inventory(),
mods: self.mods(),
max_component: self.max_component(),
component_manager: self.component_manager(),
}
}
pub fn as_mut(&self) -> GlobalsMut {
@ -220,6 +230,7 @@ impl Globals {
inventory: self.inventory_mut(),
mods: self.mods_mut(),
max_component: self.max_component_mut(),
component_manager: self.component_manager_mut(),
}
}
pub fn new(lua: LuaState) -> Self {
@ -242,6 +253,10 @@ impl Globals {
let inventory = 0x12224f0 as *mut Inventory;
let mods = 0x1207e90 as *mut Mods;
let max_component = 0x1152ff0 as *mut usize;
let component_manager = 0x12236e8 as *mut ComponentSystemManager;
//1224904
//12242e4
//12249d8
Self {
world_seed,
new_game_count,
@ -257,6 +272,7 @@ impl Globals {
inventory,
mods,
max_component,
component_manager,
}
}
}

View file

@ -473,6 +473,12 @@ impl<K: 'static, V: 'static> StdMap<K, V> {
parents: Vec::with_capacity(8),
}
}
pub fn len(&self) -> usize {
self.iter().count()
}
pub fn is_empty(&self) -> bool {
self.iter().next().is_none()
}
pub fn iter_keys(&self) -> impl Iterator<Item = &'static K> {
self.iter().map(|(k, _)| k)
}

View file

@ -39,7 +39,7 @@ pub struct ComponentVTable {
}
#[repr(C)]
#[derive(Debug)]
pub struct ComponentManagerVTable {
pub struct ComponentBufferVTable {
//TODO should be a union
}
#[repr(C)]
@ -130,7 +130,7 @@ fn test_com_create() {
#[repr(C)]
#[derive(Debug)]
pub struct ComponentBuffer {
pub vtable: &'static ComponentManagerVTable,
pub vtable: &'static ComponentBufferVTable,
pub end: usize,
unk: [isize; 2],
pub entity_entry: StdVec<usize>,
@ -142,7 +142,7 @@ pub struct ComponentBuffer {
impl Default for ComponentBuffer {
fn default() -> Self {
Self {
vtable: &ComponentManagerVTable {},
vtable: &ComponentBufferVTable {},
end: (-1isize).cast_unsigned(),
unk: [0, 0],
entity_entry: Default::default(),
@ -450,3 +450,18 @@ impl BitSet<8> {
.filter_map(|(a, b)| if self.get(*b) { Some(a) } else { None })
}
}
#[repr(C)]
#[derive(Debug, Default)]
pub struct ComponentSystemManager {
pub list: StdVec<&'static ComponentSystem>,
}
#[repr(C)]
#[derive(Debug)]
pub struct ComponentSystem {
pub vtable: &'static ComponentSystemVTable,
pub unk: [*const usize; 2],
pub name: StdString,
}
#[repr(C)]
#[derive(Debug)]
pub struct ComponentSystemVTable {}

View file

@ -100,6 +100,20 @@ impl EntityManager {
.iter_mut()
.filter_map(|e| unsafe { e.as_mut() })
}
pub fn get_entity_with_name(&self, name: StdString) -> Option<&'static Entity> {
self.entities
.as_ref()
.iter()
.filter_map(|a| unsafe { a.as_ref() })
.find(|e| e.name == name)
}
pub fn get_entity_with_name_mut(&mut self, name: StdString) -> Option<&'static mut Entity> {
self.entities
.as_ref()
.iter()
.filter_map(|a| unsafe { a.as_mut() })
.find(|e| e.name == name)
}
pub fn get_entity(&self, id: usize) -> Option<&'static Entity> {
self.entities
.as_ref()