From 7b34f77b6890b438bedb9ee4d52e88899c0f467c Mon Sep 17 00:00:00 2001 From: IQuant Date: Thu, 28 Nov 2024 21:37:01 +0300 Subject: [PATCH] Some scaffolding for modules in ewext. --- ewext/src/lib.rs | 36 +++++++++++++++++++ ewext/src/modules.rs | 10 ++++++ ewext/src/modules/entity_sync.rs | 6 ++++ .../files/system/ewext_init/ewext_init.lua | 1 + 4 files changed, 53 insertions(+) create mode 100644 ewext/src/modules.rs create mode 100644 ewext/src/modules/entity_sync.rs diff --git a/ewext/src/lib.rs b/ewext/src/lib.rs index 3f858a9d..d7303333 100644 --- a/ewext/src/lib.rs +++ b/ewext/src/lib.rs @@ -8,6 +8,7 @@ use std::{ use addr_grabber::{grab_addrs, grabbed_fns, grabbed_globals}; use eyre::{bail, OptionExt}; +use modules::{entity_sync::EntitySync, Module}; use noita::{ntypes::Entity, pixel::NoitaPixelRun, ParticleWorldState}; use noita_api::{ lua::{lua_bindings::lua_State, LuaState, ValuesOnStack, LUA}, @@ -17,6 +18,8 @@ use noita_api_macro::add_lua_fn; pub mod noita; +mod modules; + mod addr_grabber; thread_local! { @@ -29,6 +32,7 @@ thread_local! { #[derive(Default)] struct ExtState { particle_world_state: Option, + modules: Vec>, } fn init_particle_world_state(lua: LuaState) { @@ -92,6 +96,34 @@ fn make_ephemerial(lua: LuaState) -> eyre::Result<()> { fn on_world_initialized(lua: LuaState) { 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<()> { @@ -143,6 +175,8 @@ fn test_fn(_lua: LuaState) -> eyre::Result<()> { #[no_mangle] pub unsafe extern "C" fn luaopen_ewext0(lua: *mut lua_State) -> c_int { println!("Initializing ewext"); + STATE.with(|state| state.take()); + unsafe { 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!(test_fn); add_lua_fn!(bench_fn); + + add_lua_fn!(module_on_world_update); } println!("Initializing ewext - Ok"); 1 diff --git a/ewext/src/modules.rs b/ewext/src/modules.rs new file mode 100644 index 00000000..61def714 --- /dev/null +++ b/ewext/src/modules.rs @@ -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(()) + } +} diff --git a/ewext/src/modules/entity_sync.rs b/ewext/src/modules/entity_sync.rs new file mode 100644 index 00000000..cf669d9b --- /dev/null +++ b/ewext/src/modules/entity_sync.rs @@ -0,0 +1,6 @@ +use super::Module; + +#[derive(Default)] +pub(crate) struct EntitySync {} + +impl Module for EntitySync {} diff --git a/quant.ew/files/system/ewext_init/ewext_init.lua b/quant.ew/files/system/ewext_init/ewext_init.lua index 86ae31ed..f6259dc6 100644 --- a/quant.ew/files/system/ewext_init/ewext_init.lua +++ b/quant.ew/files/system/ewext_init/ewext_init.lua @@ -66,6 +66,7 @@ function module.on_world_update() oh_another_world_state(GameGetWorldStateEntity()) initial_world_state_entity = GameGetWorldStateEntity() end + ewext.module_on_world_update() end return module \ No newline at end of file