Some scaffolding for modules in ewext.

This commit is contained in:
IQuant 2024-11-28 21:37:01 +03:00
parent 8700191866
commit 7b34f77b68
4 changed files with 53 additions and 0 deletions

View file

@ -8,6 +8,7 @@ use std::{
use addr_grabber::{grab_addrs, grabbed_fns, grabbed_globals}; use addr_grabber::{grab_addrs, grabbed_fns, grabbed_globals};
use eyre::{bail, OptionExt}; use eyre::{bail, OptionExt};
use modules::{entity_sync::EntitySync, Module};
use noita::{ntypes::Entity, pixel::NoitaPixelRun, ParticleWorldState}; use noita::{ntypes::Entity, pixel::NoitaPixelRun, ParticleWorldState};
use noita_api::{ use noita_api::{
lua::{lua_bindings::lua_State, LuaState, ValuesOnStack, LUA}, lua::{lua_bindings::lua_State, LuaState, ValuesOnStack, LUA},
@ -17,6 +18,8 @@ use noita_api_macro::add_lua_fn;
pub mod noita; pub mod noita;
mod modules;
mod addr_grabber; mod addr_grabber;
thread_local! { thread_local! {
@ -29,6 +32,7 @@ thread_local! {
#[derive(Default)] #[derive(Default)]
struct ExtState { struct ExtState {
particle_world_state: Option<ParticleWorldState>, particle_world_state: Option<ParticleWorldState>,
modules: Vec<Box<dyn Module>>,
} }
fn init_particle_world_state(lua: LuaState) { fn init_particle_world_state(lua: LuaState) {
@ -92,6 +96,34 @@ fn make_ephemerial(lua: LuaState) -> eyre::Result<()> {
fn on_world_initialized(lua: LuaState) { fn on_world_initialized(lua: LuaState) {
grab_addrs(lua); grab_addrs(lua);
STATE.with(|state| {
let modules = &mut state.borrow_mut().modules;
modules.push(Box::new(EntitySync::default()));
})
}
fn with_every_module(f: impl Fn(&mut dyn Module) -> eyre::Result<()>) -> eyre::Result<()> {
STATE.with(|state| {
let modules = &mut state.borrow_mut().modules;
let mut errs = Vec::new();
for module in modules {
if let Err(e) = f(module.as_mut()) {
errs.push(e);
}
}
if errs.len() == 1 {
return Err(errs.remove(0));
}
if errs.len() > 1 {
bail!("Multiple errors while running ewext modules:\n{:?}", errs)
}
Ok(())
})
}
fn module_on_world_update(_lua: LuaState) -> eyre::Result<()> {
with_every_module(|module| module.on_world_update())
} }
fn bench_fn(_lua: LuaState) -> eyre::Result<()> { fn bench_fn(_lua: LuaState) -> eyre::Result<()> {
@ -143,6 +175,8 @@ fn test_fn(_lua: LuaState) -> eyre::Result<()> {
#[no_mangle] #[no_mangle]
pub unsafe extern "C" fn luaopen_ewext0(lua: *mut lua_State) -> c_int { pub unsafe extern "C" fn luaopen_ewext0(lua: *mut lua_State) -> c_int {
println!("Initializing ewext"); println!("Initializing ewext");
STATE.with(|state| state.take());
unsafe { unsafe {
LUA.lua_createtable(lua, 0, 0); LUA.lua_createtable(lua, 0, 0);
@ -152,6 +186,8 @@ pub unsafe extern "C" fn luaopen_ewext0(lua: *mut lua_State) -> c_int {
add_lua_fn!(on_world_initialized); add_lua_fn!(on_world_initialized);
add_lua_fn!(test_fn); add_lua_fn!(test_fn);
add_lua_fn!(bench_fn); add_lua_fn!(bench_fn);
add_lua_fn!(module_on_world_update);
} }
println!("Initializing ewext - Ok"); println!("Initializing ewext - Ok");
1 1

10
ewext/src/modules.rs Normal file
View file

@ -0,0 +1,10 @@
use eyre::Ok;
pub(crate) mod entity_sync;
pub(crate) trait Module {
// fn init() -> Self;
fn on_world_update(&mut self) -> eyre::Result<()> {
Ok(())
}
}

View file

@ -0,0 +1,6 @@
use super::Module;
#[derive(Default)]
pub(crate) struct EntitySync {}
impl Module for EntitySync {}

View file

@ -66,6 +66,7 @@ function module.on_world_update()
oh_another_world_state(GameGetWorldStateEntity()) oh_another_world_state(GameGetWorldStateEntity())
initial_world_state_entity = GameGetWorldStateEntity() initial_world_state_entity = GameGetWorldStateEntity()
end end
ewext.module_on_world_update()
end end
return module return module