Skip to content

Commit

Permalink
Update noitapatcher and nsew manually
Browse files Browse the repository at this point in the history
  • Loading branch information
Ismoh committed Feb 11, 2024
1 parent 595a64d commit 25c296e
Show file tree
Hide file tree
Showing 8 changed files with 687 additions and 2 deletions.
5 changes: 3 additions & 2 deletions .debug/lua-definitions/noitapatcher.lua
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
---@meta 'noitapatcher'
---@class noitapatcher
---@module noitapatcher

local noitapatcher = {}

---Enable OnProjectileFired and OnProjectileFiredPost callbacks.
Expand Down Expand Up @@ -105,7 +106,7 @@ function noitapatcher.SerializeEntity(entity_id) end
---@param serialized_data string The serialized data
---@param x number? Position to force the entity to if provided
---@param y number? Position to force the entity to if provided
---@return integer entity_id The entity_id passed into the function if deserialization was successful.
---@return integer? entity_id The entity_id passed into the function if deserialization was successful.
function noitapatcher.DeserializeEntity(entity_id, serialized_data, x, y) end

---Set box2d parameters of a PhysicsBody(2)Component
Expand Down
24 changes: 24 additions & 0 deletions mods/noita-mp/lua_modules/lib/lua/5.1/noitapatcher/load.lua
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@

-- You're supposed to `dofile_once("path/to/load.lua")` this file.


local orig_do_mod_appends = do_mod_appends

do_mod_appends = function(filename, ...)
do_mod_appends = orig_do_mod_appends
do_mod_appends(filename, ...)

local noitapatcher_path = string.match(filename, "(.*)/load.lua")
if not noitapatcher_path then
print("Couldn't detect NoitaPatcher path")
end

__nsew_path = noitapatcher_path .. "/noitapatcher/nsew/"

package.cpath = package.cpath .. ";./" .. noitapatcher_path .. "/?.dll"
package.path = package.path .. ";./" .. noitapatcher_path .. "/?.lua"

-- Lua's loader should now be setup properly:
-- local np = require("noitapatcher")
-- local nsew = require("noitapatcher.nsew")
end
Binary file not shown.
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
--- Native library. Primarily for internal use.
---@module 'noitapatcher.nsew.native_dll'

local ffi = require("ffi")

native_dll = {}

--- The NSEW support dll loaded in with `ffi.load`.
native_dll.lib = ffi.load(__nsew_path .. "nsew_native.dll")

return native_dll
Binary file not shown.
134 changes: 134 additions & 0 deletions mods/noita-mp/lua_modules/lib/lua/5.1/noitapatcher/nsew/rect.lua
Original file line number Diff line number Diff line change
@@ -0,0 +1,134 @@
--- Rectangle utilities.
---@module 'noitapatcher.nsew.rect'

local rect = {}

local ffi = require("ffi")
local native_dll = require("noitapatcher.nsew.native_dll")

ffi.cdef([[
struct nsew_rectangle {
int32_t left;
int32_t top;
int32_t right;
int32_t bottom;
};
struct nsew_rectangle_optimiser;
struct nsew_rectangle_optimiser* rectangle_optimiser_new();
void rectangle_optimiser_delete(struct nsew_rectangle_optimiser* rectangle_optimiser);
void rectangle_optimiser_reset(struct nsew_rectangle_optimiser* rectangle_optimiser);
void rectangle_optimiser_submit(struct nsew_rectangle_optimiser* rectangle_optimiser, struct nsew_rectangle* rectangle);
void rectangle_optimiser_scan(struct nsew_rectangle_optimiser* rectangle_optimiser);
int32_t rectangle_optimiser_size(const struct nsew_rectangle_optimiser* rectangle_optimiser);
const struct nsew_rectangle* rectangle_optimiser_get(const struct nsew_rectangle_optimiser* rectangle_optimiser, int32_t index);
struct lua_nsew_rectangle_optimiser {
struct nsew_rectangle_optimiser* impl;
};
]])

local Rectangle_mt = {
__index = {
area = function(r)
return (r.right - r.left) * (r.bottom - r.top)
end,
height = function(r)
return r.bottom - r.top
end,
width = function(r)
return r.right - r.left
end,
},
}
rect.Rectangle = ffi.metatype("struct nsew_rectangle", Rectangle_mt)

--- Given an iterator that returns rectangles, return an iterator where the
--- rectangle extents never exceed `size`.
-- @param it iterator returning squares
-- @tparam int size maximum width and height
-- @return rectangle iterator where the extents never exceed `size`
function rect.parts(it, size)
local region
local posx
local posy
return function()
if region == nil then
region = it()
if region == nil then
return nil
end
posx = region.left
posy = region.top
end

local endx = math.min(posx + size, region.right)
local endy = math.min(posy + size, region.bottom)

local ret = rect.Rectangle(posx, posy, endx, endy)

-- Setup for next iteration: place to the right, wraparound, or
-- we're done with this region.
if endx ~= region.right then
posx = endx
elseif endy ~= region.bottom then
posx = region.left
posy = endy
else
region = nil
end

return ret
end
end

local Optimiser_mt = {
__gc = function(opt)
native_dll.lib.rectangle_optimiser_delete(opt.impl)
end,

__index = {
submit = function(opt, rectangle)
native_dll.lib.rectangle_optimiser_submit(opt.impl, rectangle)
end,
scan = function(opt)
native_dll.lib.rectangle_optimiser_scan(opt.impl)
end,
reset = function(opt)
native_dll.lib.rectangle_optimiser_reset(opt.impl)
end,
size = function(opt)
return native_dll.lib.rectangle_optimiser_size()
end,
get = function(opt, index)
return native_dll.lib.rectangle_optimiser_get(index)
end,
iterate = function(opt)
local size = native_dll.lib.rectangle_optimiser_size(opt.impl)
local index = 0
return function()
if index >= size then
return nil
end

ret = native_dll.lib.rectangle_optimiser_get(opt.impl, index)
index = index + 1
return ret
end
end,
}
}
rect.Optimiser = ffi.metatype("struct lua_nsew_rectangle_optimiser", Optimiser_mt)

--- Create a new rectangle Optimiser
-- @treturn Optimiser empty optimiser
function rect.Optimiser_new()
return rect.Optimiser(native_dll.lib.rectangle_optimiser_new())
end

return rect
Loading

0 comments on commit 25c296e

Please sign in to comment.