ewext world state init stuff

This commit is contained in:
IQuant 2024-09-23 19:48:56 +03:00
parent da2cb9b625
commit a0fee57d0c
4 changed files with 69 additions and 44 deletions

View file

@ -1,6 +1,12 @@
use std::{ffi::c_int, sync::LazyLock};
use std::{
borrow::BorrowMut,
cell::{LazyCell, RefCell},
ffi::{c_int, c_void},
sync::{LazyLock, Mutex},
};
use lua_bindings::{lua_State, Lua51};
use noita::ParticleWorldState;
mod lua_bindings;
@ -11,39 +17,29 @@ static LUA: LazyLock<Lua51> = LazyLock::new(|| unsafe {
Lua51::from_library(lib).expect("library to be lua")
});
thread_local! {
static STATE: LazyCell<ExtState> = LazyCell::new(|| ExtState::default());
}
#[derive(Default)]
struct ExtState {
particle_world_state: Option<ParticleWorldState>,
}
// const EWEXT: [(&'static str, Function); 1] = [("testfn", None)];
extern "C" fn test_fn(_lua: *mut lua_State) -> c_int {
println!("\nStarting trace");
backtrace::trace(|frame| {
// let ip = frame.ip();
let symbol_address = frame.symbol_address();
extern "C" fn init_particle_world_state(lua: *mut lua_State) -> c_int {
println!("\nInitializing particle world state");
let world_pointer = unsafe { LUA.lua_tointeger(lua, 1) };
let chunk_map_pointer = unsafe { LUA.lua_tointeger(lua, 2) };
println!("pws stuff: {world_pointer:?} {chunk_map_pointer:?}");
print!("symbol: {:#08X}", symbol_address as usize);
if let Some(base) = frame.module_base_address() {
print!(" base: {:#08X}", base as usize);
}
// Resolve this instruction pointer to a symbol name
backtrace::resolve_frame(frame, |symbol| {
if let Some(name) = symbol.name() {
print!(" name: {name}");
}
if let Some(filename) = symbol.filename() {
print!(" file: {}", filename.display());
}
});
println!();
for i in 0..16 {
let b: u8 =
unsafe { std::ptr::read_volatile((symbol_address as *const u8).wrapping_add(i)) };
print!("{:02X} ", b);
}
println!();
true // keep going to the next frame
STATE.with(|mut state| {
state.particle_world_state = Some(ParticleWorldState::new(
world_pointer as *mut c_void,
chunk_map_pointer as *mut c_void,
))
});
println!("End trace\n");
0
}
@ -51,10 +47,11 @@ extern "C" fn test_fn(_lua: *mut lua_State) -> c_int {
pub extern "C" fn luaopen_ewext(lua: *mut lua_State) -> c_int {
println!("Initializing ewext");
unsafe {
LUA.lua_pushcclosure(lua, Some(test_fn), 0);
// LUA.lua_setfield(lua, LUA_GLOBALSINDEX, c"ewext".as_ptr())
LUA.lua_createtable(lua, 0, 0);
LUA.lua_pushcclosure(lua, Some(init_particle_world_state), 0);
LUA.lua_setfield(lua, -2, c"init_particle_world_state".as_ptr());
}
// let mut luastate = unsafe { State::from_ptr(luastateptr) };
// luastate.new_lib(&EWEXT);
println!("Initializing ewext - Ok");
1
}

View file

@ -1,16 +1,29 @@
use std::ffi::c_void;
pub(crate) struct ChunkMap {
this: *mut c_void,
pub(crate) struct ParticleWorldState {
world_pointer: *mut c_void,
chunk_map_this: *mut c_void,
}
impl ChunkMap {
unsafe fn get_cell(&self, x: u32, y: u32) {
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 index = ((((y) >> 9) - 256 & 511) * 512 + (((x) >> 9) - 256 & 511)) * 4;
let chunk_arr = self.this.offset(8).cast::<*const c_void>().read();
let chunk = chunk_arr.offset(index).cast::<*const c_void>().read();
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();
let pixel = chunk.offset(((y & 511) << 9 | x & 511) * 4);
pixel
}
pub(crate) fn new(world_pointer: *mut c_void, chunk_map_pointer: *mut c_void) -> Self {
Self {
world_pointer,
chunk_map_this: chunk_map_pointer,
}
}
}