added support for FFI library using trusted mods config

This commit is contained in:
FatalErr42O 2023-09-08 19:11:08 -07:00
parent 71e490eacb
commit 7ad207b4a9
5 changed files with 78 additions and 24 deletions

View File

@ -1,3 +1,10 @@
{
"Lua.workspace.checkThirdParty": false
"Lua.workspace.checkThirdParty": false,
"Lua.diagnostics.globals": [
"minetest",
"vector",
"dump",
"player_api",
"modules"
]
}

View File

@ -1,9 +1,11 @@
Cirno's Perfect Math Library
====
### Adapated for Minetest
For best memory performance: have luaJIT & it's FFI library (this should be built into luaJIT), and add MTUL-CPML to your trusted list (so it can `require()` call the FFI library)
Various useful bits of game math. 3D line intersections, ray casting, 2d/3d vectors, 4x4 matrices, quaternions, etc.
Intended to be used with LuaJIT and LÖVE (this is the backbone of LÖVE3D).
(originally) ntended to be used with LuaJIT and LÖVE (this is the backbone of LÖVE3D).
Online documentation can be found [here](http://excessive.github.io/cpml/) or you can generate them yourself using `ldoc -c doc/config.ld -o index .`

View File

@ -56,26 +56,68 @@ local files = {
"bound3",
}
modules = minetest.get_modpath("mtul_math_cpml").."/modules/"
local old_require = require
local len = #modules
--initialize some variables
local modpath = minetest.get_modpath("mtul_math_cpml")
local loaded_modules = {}
function require(path)
if loaded_modules[path] then return loaded_modules[path] end
local ending = string.gsub(path:sub(len+1), "%.", "/")..".lua"
--[[if ending[1] ~= "/" then
ending = "/"..ending
end]]
path = modules..ending
loaded_modules[path] = dofile(path)
return loaded_modules[path]
local insecure = minetest.request_insecure_environment()
local old_require = require
local old_package_path
--if require isn't present, allow us to load the modules through hackish means
--there's like 100s of require calls, it'd be insane to replace them. If you're farmiliar with require, the goal should be obvious.
--modules is the path to modules
if not insecure then
--if an insecure environment cannot be loaded, then we basically change how require works temporarily, so modules (which is referenced in all CPML files on require() has to be changed)
modules = modpath.."/modules/"
else
old_package_path = package.path
--get the real modpath and add it to the package.path string so we can find our modules in require()
insecure.package.path = insecure.package.path .. ";"..string.gsub(modpath, "\\bin\\%.%.", "").."?.lua" --add our path
modules = ".modules."
end
for _, file in ipairs(files) do
mtul.math[file] = require(modules .. file)
end
mtul.loaded_modules.cpml = true
function require(path)
if not insecure then
if loaded_modules[path] then return loaded_modules[path] end
local ending = string.gsub(path:sub(#modules+1), "%.", "/")..".lua"
--[[if ending[1] ~= "/" then
ending = "/"..ending
end]]
path = modules..ending
loaded_modules[path] = dofile(path)
return loaded_modules[path]
else
--if mod security is off we want to check for require() first.
return (old_require or insecure.require)(path)
end
end
--print(require, insecure.require)
if type(jit) == "table" and jit.status() then
if insecure then
if pcall(require, "ffi") then
minetest.log("verbose", "MTUL-CPML: loaded JIT FFI library. Memory efficiency with FFI enabled.")
print("JIT")
else
minetest.log("error", "MTUL-CPML: Failure to load JIT FFI library.")
end
else
minetest.log("error", "MTUL-CPML: insecure environment denied for MTUL-CPML. Add mtul-cpml to your trusted mods for JIT FFI support (memory efficiency & speed boost)")
end
else
minetest.log("verbose", "MTUL-CPML: JIT not present, skipped attempt to load JIT FFI library for acceleration and memory efficiency")
end
for _, file in ipairs(files) do
print(modpath..modules..file)
mtul.math[file] = require(modules .. file)
end
--unset all the global shit we had to change for CPML to work properly.
if old_package_path then
insecure.package.path = old_package_path
end
modules = nil
require = old_require
--tell MTUL-CORE that it's loaded.
mtul.loaded_modules.cpml = true

View File

@ -437,6 +437,13 @@ function quat.to_string(a)
return string.format("(%+0.3f,%+0.3f,%+0.3f,%+0.3f)", a.x, a.y, a.z, a.w)
end
--- Convert a quaternion to an Euler angle
-- @tparam quat a Quaternion to convert
-- @treturn vec3 euler angle in radians
function quat.to_euler()
end
quat_mt.__index = quat
quat_mt.__tostring = quat.to_string

View File

@ -29,10 +29,6 @@
-- [ MIT license: http://www.opensource.org/licenses/mit-license.php ]
--
if _G.love and _G.love.math then
return love.math.noise
end
-- Bail out with dummy module if FFI is missing.
local has_ffi, ffi = pcall(require, "ffi")
if not has_ffi then