mirror of
https://github.com/CommunityOx/ox_lib.git
synced 2026-08-17 06:56:03 +01:00
feat(imports/require): custom require for script files
This commit is contained in:
64
imports/require/shared.lua
Normal file
64
imports/require/shared.lua
Normal file
@@ -0,0 +1,64 @@
|
||||
local loaded = {}
|
||||
|
||||
package = {
|
||||
loaded = setmetatable({}, {
|
||||
__index = loaded,
|
||||
__newindex = noop,
|
||||
__metatable = false,
|
||||
}),
|
||||
path = './?.lua;'
|
||||
}
|
||||
|
||||
local _require = require
|
||||
|
||||
---Loads the given module inside the current resource, returning any values returned by the file or `true` when `nil`.
|
||||
---@param modname string
|
||||
---@return unknown?
|
||||
function lib.require(modname)
|
||||
if type(modname) ~= 'string' then return end
|
||||
|
||||
local module = loaded[modname]
|
||||
|
||||
if not module then
|
||||
if module == false then
|
||||
error(("^1circular-dependency occurred when loading module '%s'^0"):format(modname), 2)
|
||||
end
|
||||
|
||||
local success, result = pcall(_require, modname)
|
||||
|
||||
if success then
|
||||
loaded[modname] = result
|
||||
return result
|
||||
end
|
||||
|
||||
local modpath = modname:gsub('%.', '/')
|
||||
|
||||
for path in package.path:gmatch('[^;]+') do
|
||||
local scriptPath = path:gsub('?', modpath):gsub('%.+%/+', '')
|
||||
local resourceFile = LoadResourceFile(cache.resource, scriptPath)
|
||||
|
||||
if resourceFile then
|
||||
loaded[modname] = false
|
||||
scriptPath = ('@@%s/%s'):format(cache.resource, scriptPath)
|
||||
|
||||
local chunk, err = load(resourceFile, scriptPath)
|
||||
|
||||
if err or not chunk then
|
||||
loaded[modname] = nil
|
||||
return error(err or ("unable to load module '%s'"):format(modname), 3)
|
||||
end
|
||||
|
||||
module = chunk(modname) or true
|
||||
loaded[modname] = module
|
||||
|
||||
return module
|
||||
end
|
||||
end
|
||||
|
||||
return error(("module '%s' not found"):format(modname), 2)
|
||||
end
|
||||
|
||||
return module
|
||||
end
|
||||
|
||||
return lib.require
|
||||
6
init.lua
6
init.lua
@@ -23,7 +23,8 @@ if status ~= true then error(status, 2) end
|
||||
|
||||
local LoadResourceFile = LoadResourceFile
|
||||
local context = IsDuplicityVersion() and 'server' or 'client'
|
||||
local function noop() end
|
||||
|
||||
function noop() end
|
||||
|
||||
local function loadModule(self, module)
|
||||
local dir = ('imports/%s'):format(module)
|
||||
@@ -86,6 +87,9 @@ lib = setmetatable({
|
||||
__call = call,
|
||||
})
|
||||
|
||||
-- Override standard Lua require with our own.
|
||||
require = lib.require
|
||||
|
||||
local intervals = {}
|
||||
--- Dream of a world where this PR gets accepted.
|
||||
---@param callback function | number
|
||||
|
||||
Reference in New Issue
Block a user