mirror of
https://github.com/CommunityOx/ox_lib.git
synced 2026-08-16 22:46:03 +01:00
feat(require): external module support for require (#422)
This commit is contained in:
@@ -17,29 +17,61 @@ local _require = require
|
|||||||
function lib.require(modname)
|
function lib.require(modname)
|
||||||
if type(modname) ~= 'string' then return end
|
if type(modname) ~= 'string' then return end
|
||||||
|
|
||||||
|
local modpath = modname:gsub('%.', '/')
|
||||||
local module = loaded[modname]
|
local module = loaded[modname]
|
||||||
|
|
||||||
|
if module then return module end
|
||||||
|
|
||||||
|
local success, result = pcall(_require, modname)
|
||||||
|
|
||||||
|
if success then
|
||||||
|
loaded[modname] = result
|
||||||
|
return result
|
||||||
|
end
|
||||||
|
|
||||||
|
local resourceSrc
|
||||||
|
|
||||||
|
if not modpath:find('^@') then
|
||||||
|
local idx = 1
|
||||||
|
|
||||||
|
while true do
|
||||||
|
local di = debug.getinfo(idx, 'S')
|
||||||
|
|
||||||
|
if di then
|
||||||
|
if not di.short_src:find('^@ox_lib/imports/require') and not di.short_src:find('^%[C%]') and not di.short_src:find('^citizen') then
|
||||||
|
resourceSrc = di.short_src:gsub('^@(.-)/.+', '%1')
|
||||||
|
break
|
||||||
|
end
|
||||||
|
else
|
||||||
|
resourceSrc = cache.resource
|
||||||
|
break
|
||||||
|
end
|
||||||
|
|
||||||
|
idx += 1
|
||||||
|
end
|
||||||
|
|
||||||
|
if resourceSrc ~= cache.resource then
|
||||||
|
modname = ('@%s.%s'):format(resourceSrc, modname)
|
||||||
|
end
|
||||||
|
end
|
||||||
|
|
||||||
if not module then
|
if not module then
|
||||||
if module == false then
|
if module == false then
|
||||||
error(("^1circular-dependency occurred when loading module '%s'^0"):format(modname), 2)
|
error(("^1circular-dependency occurred when loading module '%s'^0"):format(modname), 2)
|
||||||
end
|
end
|
||||||
|
|
||||||
local success, result = pcall(_require, modname)
|
if not resourceSrc then
|
||||||
|
resourceSrc = modpath:gsub('^@(.-)/.+', '%1')
|
||||||
if success then
|
modpath = modpath:sub(#resourceSrc + 3)
|
||||||
loaded[modname] = result
|
|
||||||
return result
|
|
||||||
end
|
end
|
||||||
|
|
||||||
local modpath = modname:gsub('%.', '/')
|
|
||||||
|
|
||||||
for path in package.path:gmatch('[^;]+') do
|
for path in package.path:gmatch('[^;]+') do
|
||||||
local scriptPath = path:gsub('?', modpath):gsub('%.+%/+', '')
|
local scriptPath = path:gsub('?', modpath):gsub('%.+%/+', '')
|
||||||
local resourceFile = LoadResourceFile(cache.resource, scriptPath)
|
local resourceFile = LoadResourceFile(resourceSrc, scriptPath)
|
||||||
|
|
||||||
if resourceFile then
|
if resourceFile then
|
||||||
loaded[modname] = false
|
loaded[modname] = false
|
||||||
scriptPath = ('@@%s/%s'):format(cache.resource, scriptPath)
|
scriptPath = ('@@%s/%s'):format(resourceSrc, scriptPath)
|
||||||
|
|
||||||
local chunk, err = load(resourceFile, scriptPath)
|
local chunk, err = load(resourceFile, scriptPath)
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user