2022-07-23 15:36:21 +00:00
-- Copyright (c) 2019-2022 David Vogel
--
-- This software is released under the MIT License.
-- https://opensource.org/licenses/MIT
-- Some code to make Noita's lua conform more to standard lua.
-- Stupid way to prevent this code from being called more than once per sandbox.
-- Calling this lua file with dofile_once would still cause the setup function to be called multiple times.
if _NoitaAPICompatibilityWrapperGuard_ then return function ( dummy ) end end
_NoitaAPICompatibilityWrapperGuard_ = true
-- Override print function to behave more like the standard lua one.
local oldPrint = print
function print ( ... )
local arg = { ... }
local stringArgs = { }
for i , v in ipairs ( arg ) do
table.insert ( stringArgs , tostring ( v ) )
end
oldPrint ( unpack ( stringArgs ) )
end
-- Package doesn't exist when the Lua API is restricted.
-- Therefore we create it here and apply some default values.
package = package or { }
2022-07-23 18:43:04 +00:00
package.path = package.path or " ./?.lua; " -- Allow paths relative to the working directory.
2022-07-23 15:36:21 +00:00
package.preload = package.preload or { }
package.loaded = package.loaded or {
_G = _G ,
bit = bit ,
coroutine = coroutine ,
debug = debug ,
math = math ,
package = package ,
string = string ,
table = table ,
--io = io,
--jit = jit,
--os = os,
}
2022-07-23 18:43:04 +00:00
local oldRequire = require
2022-07-23 19:15:34 +00:00
local recursionSet = { }
local recursionLast
2022-07-23 15:36:21 +00:00
---Emulated require function in case the Lua API is restricted.
---It's probably good enough for most usecases.
2022-07-23 18:43:04 +00:00
---
---We need to override the default require in any case, as only dofile can access stuff in the virtual filesystem.
2022-07-23 15:36:21 +00:00
---@param modName string
---@return any
2022-07-23 18:43:04 +00:00
function require ( modName )
2022-07-23 15:36:21 +00:00
-- Check if package was already loaded, return previous result.
2022-07-23 18:43:04 +00:00
if package.loaded [ modName ] ~= nil then return package.loaded [ modName ] end
2022-07-23 15:36:21 +00:00
2022-07-23 19:15:34 +00:00
if recursionSet [ modName ] then
error ( string.format ( " Cyclic dependency with module %q called by %q " , modName , recursionLast ) )
end
recursionSet [ modName ] , recursionLast = true , modName
2022-07-23 15:36:21 +00:00
local notFoundStr = " "
-- Check if there is an entry in the preload table.
local preloadFunc = package.preload [ modName ]
if preloadFunc then
local res = preloadFunc ( modName )
if res == nil then res = true end
package.loaded [ modName ] = res
2022-07-23 19:15:34 +00:00
recursionSet [ modName ] , recursionLast = nil , nil
2022-07-23 15:36:21 +00:00
return res
else
2022-07-23 18:43:04 +00:00
notFoundStr = notFoundStr .. string.format ( " \t no field package.preload['%s'] \n " , modName )
2022-07-23 15:36:21 +00:00
end
-- Load and execute scripts.
-- Iterate over all package.path entries.
for pathEntry in string.gmatch ( package.path , " [^;]+ " ) do
local modPath = string.gsub ( modName , " %. " , " / " ) -- Replace "." with file path delimiter.
local filePath = string.gsub ( pathEntry , " ? " , modPath , 1 ) -- Insert modPath into "?" placeholder.
local fixedPath = string.gsub ( filePath , " ^%.[ \\ /] " , " " ) -- Need to remove "./" or ".\" at the beginning, as Noita allows only "data" and "mods".
if fixedPath : sub ( 1 , 4 ) == " data " or fixedPath : sub ( 1 , 4 ) == " mods " then -- Ignore everything other than data and mod root path elements. It's not perfect, but this is just there to prevent console spam.
local res , err = dofile ( fixedPath )
2022-07-23 18:43:04 +00:00
if err then
notFoundStr = notFoundStr .. string.format ( " \t no file '%s' \n " , filePath )
2022-07-23 15:36:21 +00:00
else
if res == nil then res = true end
package.loaded [ modName ] = res
2022-07-23 19:15:34 +00:00
recursionSet [ modName ] , recursionLast = nil , nil
2022-07-23 15:36:21 +00:00
return res
end
else
2022-07-23 18:43:04 +00:00
notFoundStr = notFoundStr .. string.format ( " \t not allowed '%s' \n " , filePath )
end
end
-- Fall back to the original require, if it exists.
if oldRequire then
local ok , res = pcall ( oldRequire , modName )
if ok then
2022-07-23 19:15:34 +00:00
recursionSet [ modName ] , recursionLast = nil , nil
2022-07-23 18:43:04 +00:00
return res
else
notFoundStr = notFoundStr .. string.format ( " \t original require:%s " , res )
2022-07-23 15:36:21 +00:00
end
end
2022-07-23 19:15:34 +00:00
recursionSet [ modName ] , recursionLast = nil , nil
2022-07-23 15:36:21 +00:00
error ( string.format ( " module %q not found: \n %s " , modName , notFoundStr ) )
end
---Set up some stuff so `require` works as expected.
2022-07-23 18:43:04 +00:00
---@param libPath any -- Path to the libraries directory of this mod.
local function setup ( libPath )
2022-07-23 15:36:21 +00:00
-- Add the files folder of the given mod as base for any `require` lookups.
2022-07-23 18:43:04 +00:00
package.path = package.path .. " ./ " .. libPath .. " ?.lua; "
package.path = package.path .. " ./ " .. libPath .. " ?/init.lua; "
-- Add the library directory of Noita itself.
2022-07-27 16:17:26 +00:00
package.path = package.path .. " ./data/scripts/lib/?.lua; " -- TODO: Get rid of Noita's lib path, create replacement libs for stuff in there
2022-07-23 15:36:21 +00:00
end
return setup