Skip to content

Commit 29e1822

Browse files
committed
Deallocate removed pixels
1 parent 7df0b18 commit 29e1822

File tree

3 files changed

+32
-4
lines changed

3 files changed

+32
-4
lines changed

ewext/src/modules/world_sync.rs

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -125,9 +125,30 @@ impl WorldData for ParticleWorldState {
125125
for (i, pixel) in chunk.pixels.into_iter().enumerate() {
126126
let x = (i % CHUNK_SIZE) as isize;
127127
let y = (i / CHUNK_SIZE) as isize;
128+
128129
let cell = pixel_array.get_mut_raw(shift_x + x, shift_y + y);
130+
131+
if !cell.is_null() {
132+
let cell = unsafe { &**cell };
133+
// Don't touch box2d stuff.
134+
if cell.material.cell_type == CellType::Solid {
135+
continue;
136+
}
137+
// No point replacing cells with themselves.
138+
if cell.material.material_type == pixel.mat() as isize {
139+
continue;
140+
}
141+
}
142+
129143
let xs = start_x + x;
130144
let ys = start_y + y;
145+
// Drop first
146+
if !cell.is_null() {
147+
unsafe {
148+
heap::delete(cell);
149+
}
150+
*cell = ptr::null_mut();
151+
}
131152
if pixel.is_air() {
132153
*cell = ptr::null_mut();
133154
} else {

noita_api/src/heap.rs

Lines changed: 10 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2,18 +2,18 @@ use std::sync::LazyLock;
22

33
struct Msvcr {
44
op_new: unsafe extern "C" fn(n: std::os::raw::c_uint) -> *mut std::os::raw::c_void,
5-
// op_delete: unsafe extern "C" fn(*const std::os::raw::c_void),
5+
op_delete: unsafe extern "C" fn(*const std::os::raw::c_void),
66
// op_delete_array: unsafe extern "C" fn(*const std::os::raw::c_void),
77
}
88

99
static MSVCR: LazyLock<Msvcr> = LazyLock::new(|| unsafe {
1010
let lib = libloading::Library::new("./msvcr120.dll").expect("library to exist");
1111
let op_new = *lib.get(b"??2@YAPAXI@Z\0").expect("symbol to exist");
12-
// let op_delete = *lib.get(b"operator_delete\0").expect("symbol to exist");
12+
let op_delete = *lib.get(b"??3@YAXPAX@Z\0").expect("symbol to exist");
1313
// let op_delete_array = *lib.get(b"operator_delete[]\0").expect("symbol to exist");
1414
Msvcr {
1515
op_new,
16-
// op_delete,
16+
op_delete,
1717
// op_delete_array,
1818
}
1919
});
@@ -39,3 +39,10 @@ pub fn place_new<T>(value: T) -> *mut T {
3939
pub fn place_new_ref<T>(value: T) -> &'static mut T {
4040
unsafe { &mut *place_new(value) }
4141
}
42+
43+
/// # Safety
44+
///
45+
/// Pointer has to be non null, allocated by noita's allocator, and not yet freed.
46+
pub unsafe fn delete<T>(pointer: *const T) {
47+
unsafe { (MSVCR.op_delete)(pointer.cast()) }
48+
}

quant.ew/init.lua

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@ net = dofile_once("mods/quant.ew/files/core/net.lua")
2222
inventory_helper = dofile_once("mods/quant.ew/files/core/inventory_helper.lua")
2323
player_fns = dofile_once("mods/quant.ew/files/core/player_fns.lua")
2424

25-
-- print("construct_cell", np.GetWorldInfo().construct_cell)
25+
-- print("construct_cell", np.GetWorldInfo().remove_cell)
2626

2727
local cos = dofile_once("mods/quant.ew/files/system/player/player_cosmetics.lua")
2828

0 commit comments

Comments
 (0)