This repository has been archived by the owner on Jan 7, 2025. It is now read-only.
-
-
Notifications
You must be signed in to change notification settings - Fork 16
/
Copy pathrequire.lua
122 lines (91 loc) · 2.73 KB
/
require.lua
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
-- This logic is originally from the ox_lib resource, all i did was migrate and slightly modify this to feet my needs to utilize in my future projects.
---@diagnostic disable: lowercase-global
local loaded = {}
Class = {}
package = {
loaded = setmetatable({}, {
__index = loaded,
__metatable = false,
}),
path = "./?.lua;",
}
cache = {
resource = GetCurrentResourceName(),
}
local _require = require
---@param filePath string
---@param env? table
---@return any
function Class.load(filePath, env)
local modpath = filePath:gsub("%.", "/")
local resourceSrc = cache.resource
for path in package.path:gmatch("[^;]+") do
local scriptPath = path:gsub("?", modpath):gsub("%.+%/+", "")
local resourceFile = LoadResourceFile(resourceSrc, scriptPath)
if resourceFile then
local chunk, err = load(resourceFile, ("@@%s/%s"):format(resourceSrc, scriptPath), "t", env or _ENV)
if not chunk or err then
error(err or "an unknown error occurred", 2)
end
return chunk()
end
end
error(("cannot load file at path %s"):format(modpath))
end
function Class.loadJson(filePath)
local modpath = filePath:gsub("%.", "/")
local resourceSrc = cache.resource
local scriptPath = ("%s.json"):format(modpath)
local resourceFile = LoadResourceFile(resourceSrc, scriptPath)
if resourceFile then
return json.decode(resourceFile)
end
error(("cannot load json file at path %s"):format(modpath))
end
function Class.require(modname)
if type(modname) ~= "string" then
return
end
local modpath = modname:gsub("%.", "/")
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 = cache.resource
if not modpath:find("^@") then
modname = ("@%s.%s"):format(resourceSrc, modname)
end
if not module then
if module == false then
error(("^1circular-dependency occurred when loading module '%s'^0"):format(modname), 2)
end
if not resourceSrc then
resourceSrc = modpath:gsub("^@(.-)/.+", "%1")
modpath = modpath:sub(#resourceSrc + 3)
end
for path in package.path:gmatch("[^;]+") do
local scriptPath = path:gsub("?", modpath):gsub("%.+%/+", "")
local resourceFile = LoadResourceFile(resourceSrc, scriptPath)
if resourceFile then
loaded[modname] = false
scriptPath = ("@@%s/%s"):format(resourceSrc, 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(result, 2)
end
return module
end
require = Class.require