2023-02-22 09:50:44 +11:00
|
|
|
local loaded = {}
|
2024-04-05 18:01:06 +11:00
|
|
|
local _require = require
|
2023-02-22 09:50:44 +11:00
|
|
|
|
|
|
|
|
package = {
|
2024-04-05 18:01:06 +11:00
|
|
|
path = './?.lua;./?/init.lua',
|
|
|
|
|
preload = {},
|
2023-02-22 09:50:44 +11:00
|
|
|
loaded = setmetatable({}, {
|
|
|
|
|
__index = loaded,
|
|
|
|
|
__newindex = noop,
|
|
|
|
|
__metatable = false,
|
2024-04-05 18:01:06 +11:00
|
|
|
})
|
2023-02-22 09:50:44 +11:00
|
|
|
}
|
|
|
|
|
|
2024-04-05 18:01:06 +11:00
|
|
|
---@param modName string
|
|
|
|
|
---@return string
|
|
|
|
|
---@return string
|
|
|
|
|
local function getModuleInfo(modName)
|
2023-09-19 20:25:17 +10:00
|
|
|
local resourceSrc
|
|
|
|
|
|
2024-04-05 18:01:06 +11:00
|
|
|
if not modName:find('^@') then
|
2023-09-19 20:25:17 +10:00
|
|
|
local idx = 1
|
|
|
|
|
|
|
|
|
|
while true do
|
|
|
|
|
local di = debug.getinfo(idx, 'S')
|
|
|
|
|
|
|
|
|
|
if di then
|
2024-04-05 18:01:06 +11:00
|
|
|
if not di.short_src ~= '?' and di.source:match('^@([^%s]+)') and not di.short_src:find('^@ox_lib/imports/require') and not di.short_src:find('^citizen') then
|
2024-02-23 15:17:06 +01:00
|
|
|
resourceSrc = di.source:gsub('^%@+([^/]+)/.+', '%1')
|
2023-09-19 20:25:17 +10:00
|
|
|
break
|
|
|
|
|
end
|
|
|
|
|
else
|
|
|
|
|
resourceSrc = cache.resource
|
|
|
|
|
break
|
|
|
|
|
end
|
|
|
|
|
|
|
|
|
|
idx += 1
|
|
|
|
|
end
|
2024-04-05 18:01:06 +11:00
|
|
|
else
|
|
|
|
|
resourceSrc = modName:gsub('^@(.-)/.+', '%1')
|
|
|
|
|
modName = modName:sub(#resourceSrc + 3)
|
|
|
|
|
end
|
|
|
|
|
|
|
|
|
|
return resourceSrc, modName
|
|
|
|
|
end
|
2023-09-19 20:25:17 +10:00
|
|
|
|
2024-04-05 18:01:06 +11:00
|
|
|
local tempData = {}
|
|
|
|
|
|
|
|
|
|
---@param name string
|
|
|
|
|
---@param path string
|
|
|
|
|
---@return string? filename
|
|
|
|
|
---@return string? errmsg
|
|
|
|
|
function package.searchpath(name, path)
|
|
|
|
|
local resourceSrc, modName = getModuleInfo(name:gsub('%.', '/'))
|
|
|
|
|
local tried = {}
|
|
|
|
|
|
|
|
|
|
for template in path:gmatch('[^;]+') do
|
|
|
|
|
local fileName = template:gsub('^%./', ''):gsub('?', modName:gsub('%.', '/') or modName)
|
|
|
|
|
local file = LoadResourceFile(resourceSrc, fileName)
|
|
|
|
|
|
|
|
|
|
if file then
|
|
|
|
|
tempData[1] = file
|
|
|
|
|
tempData[2] = resourceSrc
|
|
|
|
|
return fileName
|
2023-09-19 20:25:17 +10:00
|
|
|
end
|
2024-04-05 18:01:06 +11:00
|
|
|
|
|
|
|
|
tried[#tried + 1] = fileName
|
2023-09-19 20:25:17 +10:00
|
|
|
end
|
|
|
|
|
|
2024-04-05 18:01:06 +11:00
|
|
|
return nil, table.concat(tried, "\n\t")
|
2023-12-07 18:24:14 +11:00
|
|
|
end
|
|
|
|
|
|
2024-04-05 18:01:06 +11:00
|
|
|
---Attempts to load a module at the given path relative to the resource root directory.\
|
|
|
|
|
---Returns a function to load the module chunk, or a string containing all tested paths.
|
|
|
|
|
---@param modName string
|
2023-12-07 18:24:14 +11:00
|
|
|
---@param env? table
|
2024-04-05 18:01:06 +11:00
|
|
|
local function loadModule(modName, env)
|
|
|
|
|
local fileName, err = package.searchpath(modName, package.path)
|
2023-12-07 18:24:14 +11:00
|
|
|
|
2024-04-05 18:01:06 +11:00
|
|
|
if fileName then
|
|
|
|
|
local file = tempData[1]
|
|
|
|
|
local resource = tempData[2]
|
2023-12-07 18:24:14 +11:00
|
|
|
|
2024-04-05 18:01:06 +11:00
|
|
|
table.wipe(tempData)
|
|
|
|
|
return assert(load(file, ('@@%s/%s'):format(resource, modName), 't', env or _ENV))
|
2023-12-07 18:24:14 +11:00
|
|
|
end
|
|
|
|
|
|
2024-04-05 18:01:06 +11:00
|
|
|
return nil, err or 'unknown error'
|
|
|
|
|
end
|
2023-12-07 18:24:14 +11:00
|
|
|
|
2024-04-05 18:01:06 +11:00
|
|
|
package.searchers = {
|
|
|
|
|
function(modName) return package.preload[modName] end,
|
|
|
|
|
function(modName)
|
|
|
|
|
local ok, result = pcall(_require, modName)
|
2023-12-07 18:24:14 +11:00
|
|
|
|
2024-04-05 18:01:06 +11:00
|
|
|
if ok then return result end
|
2023-12-07 18:24:14 +11:00
|
|
|
|
2024-04-05 18:01:06 +11:00
|
|
|
return ok, result
|
|
|
|
|
end,
|
|
|
|
|
function(modName) return loadModule(modName) end,
|
|
|
|
|
}
|
2023-12-07 18:24:14 +11:00
|
|
|
|
2024-04-05 18:01:06 +11:00
|
|
|
---@param filePath string
|
|
|
|
|
---@param env? table
|
|
|
|
|
---@return unknown
|
|
|
|
|
---Loads and runs a Lua file at the given path. Unlike require, the chunk is not cached for future use.
|
|
|
|
|
function lib.load(filePath, env)
|
|
|
|
|
if type(filePath) ~= 'string' then
|
|
|
|
|
error(("file path must be a string (received '%s')"):format(filePath), 2)
|
2023-12-07 18:24:14 +11:00
|
|
|
end
|
|
|
|
|
|
2024-04-05 18:01:06 +11:00
|
|
|
local result, err = loadModule(filePath, env)
|
|
|
|
|
|
|
|
|
|
if result then return result() end
|
|
|
|
|
|
|
|
|
|
error(err)
|
2023-12-07 18:24:14 +11:00
|
|
|
end
|
|
|
|
|
|
|
|
|
|
---@param filePath string
|
|
|
|
|
---@return table
|
|
|
|
|
---Loads and decodes a json file at the given path.
|
|
|
|
|
function lib.loadJson(filePath)
|
2024-04-05 18:01:06 +11:00
|
|
|
if type(filePath) ~= 'string' then
|
|
|
|
|
error(("file path must be a string (received '%s')"):format(filePath), 2)
|
2023-12-07 18:24:14 +11:00
|
|
|
end
|
|
|
|
|
|
2024-04-05 18:01:06 +11:00
|
|
|
local resourceSrc, modPath = getModuleInfo(filePath)
|
|
|
|
|
local resourceFile = LoadResourceFile(resourceSrc, ('%s.json'):format(modPath))
|
2023-12-07 18:24:14 +11:00
|
|
|
|
|
|
|
|
if resourceFile then
|
|
|
|
|
return json.decode(resourceFile)
|
|
|
|
|
end
|
|
|
|
|
|
2024-04-05 18:01:06 +11:00
|
|
|
error(('cannot load json file at path %s'):format(modPath))
|
2023-12-07 18:24:14 +11:00
|
|
|
end
|
|
|
|
|
|
2024-04-05 18:01:06 +11:00
|
|
|
---Loads the given module, returns any value returned by the seacher (`true` when `nil`).\
|
|
|
|
|
---Passing `@resourceName.modName` loads a module from a remote resource.\
|
|
|
|
|
---@param modName string
|
2024-04-01 19:15:04 +11:00
|
|
|
---@return unknown
|
2024-04-05 18:01:06 +11:00
|
|
|
function lib.require(modName)
|
|
|
|
|
if type(modName) ~= 'string' then
|
|
|
|
|
error(("module name must be a string (received '%s')"):format(modName), 3)
|
2023-12-07 18:24:14 +11:00
|
|
|
end
|
|
|
|
|
|
2024-04-05 18:01:06 +11:00
|
|
|
local module = loaded[modName]
|
2023-12-07 18:24:14 +11:00
|
|
|
|
2024-04-05 18:01:06 +11:00
|
|
|
if module == '__loading' then
|
|
|
|
|
error(("^1circular-dependency occurred when loading module '%s'^0"):format(modName), 2)
|
2023-12-07 18:24:14 +11:00
|
|
|
end
|
|
|
|
|
|
2024-04-05 18:01:06 +11:00
|
|
|
if module ~= nil then return module end
|
2023-02-22 09:50:44 +11:00
|
|
|
|
2024-04-05 18:01:06 +11:00
|
|
|
loaded[modName] = '__loading'
|
2023-02-22 09:50:44 +11:00
|
|
|
|
2024-04-05 18:01:06 +11:00
|
|
|
local err = {}
|
2023-02-22 09:50:44 +11:00
|
|
|
|
2024-04-05 18:01:06 +11:00
|
|
|
for i = 1, #package.searchers do
|
|
|
|
|
local result, errMsg = package.searchers[i](modName)
|
2023-02-22 09:50:44 +11:00
|
|
|
|
2024-04-05 18:01:06 +11:00
|
|
|
if result then
|
|
|
|
|
if type(result) == 'function' then result = result() end
|
|
|
|
|
loaded[modName] = result or result == nil
|
2023-02-22 09:50:44 +11:00
|
|
|
|
2024-04-05 18:01:06 +11:00
|
|
|
return loaded[modName]
|
2023-02-22 09:50:44 +11:00
|
|
|
end
|
|
|
|
|
|
2024-04-05 18:01:06 +11:00
|
|
|
err[#err + 1] = errMsg
|
2023-02-22 09:50:44 +11:00
|
|
|
end
|
|
|
|
|
|
2024-04-05 18:01:06 +11:00
|
|
|
error(("%s"):format(table.concat(err, "\n\t")))
|
2023-02-22 09:50:44 +11:00
|
|
|
end
|
|
|
|
|
|
|
|
|
|
return lib.require
|