From 42e720959e45a9bb927531095b130dc29b884409 Mon Sep 17 00:00:00 2001 From: IQuant Date: Mon, 23 Sep 2024 22:42:03 +0300 Subject: [PATCH] get_pixel_pointer seems to work --- ewext/src/lib.rs | 26 ++++++++++++++----- ewext/src/noita.rs | 23 ++++++++-------- .../files/system/ewext_init/ewext_init.lua | 7 +++++ 3 files changed, 39 insertions(+), 17 deletions(-) diff --git a/ewext/src/lib.rs b/ewext/src/lib.rs index 771ef463..4d3e011a 100644 --- a/ewext/src/lib.rs +++ b/ewext/src/lib.rs @@ -1,8 +1,7 @@ use std::{ - borrow::BorrowMut, cell::{LazyCell, RefCell}, ffi::{c_int, c_void}, - sync::{LazyLock, Mutex}, + sync::LazyLock, }; use lua_bindings::{lua_State, Lua51}; @@ -18,7 +17,7 @@ static LUA: LazyLock = LazyLock::new(|| unsafe { }); thread_local! { - static STATE: LazyCell = LazyCell::new(|| ExtState::default()); + static STATE: LazyCell> = LazyCell::new(|| ExtState::default().into()); } #[derive(Default)] @@ -34,15 +33,28 @@ extern "C" fn init_particle_world_state(lua: *mut lua_State) -> c_int { let chunk_map_pointer = unsafe { LUA.lua_tointeger(lua, 2) }; println!("pws stuff: {world_pointer:?} {chunk_map_pointer:?}"); - STATE.with(|mut state| { - state.particle_world_state = Some(ParticleWorldState::new( + STATE.with(|state| { + state.borrow_mut().particle_world_state = Some(ParticleWorldState::new( world_pointer as *mut c_void, chunk_map_pointer as *mut c_void, - )) + )); }); 0 } +extern "C" fn get_pixel_pointer(lua: *mut lua_State) -> c_int { + let x = unsafe { LUA.lua_tointeger(lua, 1) } as i32; + let y = unsafe { LUA.lua_tointeger(lua, 2) } as i32; + + STATE.with(|state| { + let state = state.borrow_mut(); + let pws = state.particle_world_state.as_ref().unwrap(); + let pixel_pointer = unsafe { pws.get_cell(x, y) }; + unsafe { LUA.lua_pushinteger(lua, pixel_pointer as isize) }; + }); + 1 +} + #[no_mangle] pub extern "C" fn luaopen_ewext(lua: *mut lua_State) -> c_int { println!("Initializing ewext"); @@ -51,6 +63,8 @@ pub extern "C" fn luaopen_ewext(lua: *mut lua_State) -> c_int { LUA.lua_pushcclosure(lua, Some(init_particle_world_state), 0); LUA.lua_setfield(lua, -2, c"init_particle_world_state".as_ptr()); + LUA.lua_pushcclosure(lua, Some(get_pixel_pointer), 0); + LUA.lua_setfield(lua, -2, c"get_pixel_pointer".as_ptr()); } println!("Initializing ewext - Ok"); 1 diff --git a/ewext/src/noita.rs b/ewext/src/noita.rs index 33ab8453..7bf63d7f 100644 --- a/ewext/src/noita.rs +++ b/ewext/src/noita.rs @@ -1,4 +1,4 @@ -use std::ffi::c_void; +use std::{ffi::c_void, ptr::null}; pub(crate) struct ParticleWorldState { world_pointer: *mut c_void, @@ -6,16 +6,17 @@ pub(crate) struct ParticleWorldState { } impl ParticleWorldState { - unsafe fn get_cell(&self, x: u32, y: u32) -> *const c_void { - let x = x as isize; - let y = y as isize; - let chunk_index = (((((y) >> 9) - 256) & 511) * 512 + ((((x) >> 9) - 256) & 511)) * 4; - let chunk_arr = self - .chunk_map_this - .offset(8) - .cast::<*const *const c_void>() - .read(); - let chunk = chunk_arr.offset(chunk_index).read(); + pub(crate) unsafe fn get_cell(&self, x: i32, y: i32) -> *const c_void { + let x = dbg!(x as isize); + let y = dbg!(y as isize); + let chunk_index = dbg!((((((y) >> 9) - 256) & 511) * 512 + ((((x) >> 9) - 256) & 511)) * 4); + let chunk_arr = self.chunk_map_this.offset(8).cast::<*const c_void>().read(); + // dbg!(chunk_arr); + let chunk = chunk_arr.offset(chunk_index).cast::<*const c_void>().read(); + dbg!(chunk); + if chunk.is_null() { + return null(); + } let pixel = chunk.offset(((y & 511) << 9 | x & 511) * 4); pixel } diff --git a/quant.ew/files/system/ewext_init/ewext_init.lua b/quant.ew/files/system/ewext_init/ewext_init.lua index b5bad2d8..7946c717 100644 --- a/quant.ew/files/system/ewext_init/ewext_init.lua +++ b/quant.ew/files/system/ewext_init/ewext_init.lua @@ -11,4 +11,11 @@ function module.on_world_initialized() ewext.init_particle_world_state(grid_world, chunk_map) end +function module.on_local_player_spawn() + local pix_p = ewext.get_pixel_pointer(0, 0) + -- assert(pix_p ~= 0) + -- assert(tonumber(ffi.cast("intptr_t", ppixel)) == pix_p) + +end + return module \ No newline at end of file