-
Notifications
You must be signed in to change notification settings - Fork 11
/
loader.lua
344 lines (270 loc) · 7.78 KB
/
loader.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
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
-- from incredible-gmod.ru with <3
-- https://github.com/Be1zebub/Small-GLua-Things/blob/master/loader.lua
local Loader = {
_VERSION = 1.4,
_URL = "https://github.com/Be1zebub/Small-GLua-Things/blob/master/loader.lua",
_LICENSE = [[
MIT LICENSE
Copyright (c) 2022 incredible-gmod.ru
Permission is hereby granted, free of charge, to any person obtaining a
copy of this software and associated documentation files (the
"Software"), to deal in the Software without restriction, including
without limitation the rights to use, copy, modify, merge, publish,
distribute, sublicense, and/or sell copies of the Software, and to
permit persons to whom the Software is furnished to do so, subject to
the following conditions:
The above copyright notice and this permission notice shall be included
in all copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY
CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT,
TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE
SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
]],
_DEBUG = false
}
Loader.REALM = CLIENT and "CLIENT" or "SERVER"
local include_realm = {
sv = SERVER and include or function() end,
cl = SERVER and AddCSLuaFile or include
}
include_realm.sh = function(f)
AddCSLuaFile(f)
return include(f)
end
function Loader:include(path, realm, _lvl)
local worker = include_realm[realm]
if worker == nil then
realm, worker = "sh", include_realm.sh
end
if file.Exists(path, "LUA") then
if self._DEBUG then
print(string.rep("\t", _lvl or 0) .. realm .." > ".. path)
end
return worker(path)
end
end
function Loader:GetFilename(path)
return path:match(".+/(.+)%..+") or self:RemoveExt(path)
end
function Loader:GetFilenameWithExt(path)
return path:match("([^/]+)$")
end
function Loader:RemoveExt(path)
return path:match("(.+)%..+")
end
function Loader:GetCurrentDir()
return debug.getinfo(1).source:match("@?(.*/)")
end
function Loader:Include(path, realm, _lvl)
return self:include(path, realm or string.sub(self:GetFilename(path), 1, 2), _lvl)
end
function Loader:ScanDir(path, cback, recursive, _lvl)
_lvl = _lvl or 1
if path[#path] ~= "/" then
path = path .."/"
end
local files, folders = file.Find(path .. (recursive and "*" or "*.*"), "LUA")
for _, f in ipairs(files) do
cback(path .. f, _lvl)
end
if not recursive then return end
for _, f in ipairs(folders) do
self:ScanDir(path .. f, cback, recursive, _lvl)
end
end
local is_client = {
["sv"] = SERVER
}
local find_mode = {
file = 1,
files = 2,
dir = 2,
dirs = 2,
lua = 3,
all = 4
}
local find_dirs = {
[2] = true,
[4] = true
}
local find_pattern = {
[1] = "*.*",
[2] = "*",
[3] = "*.lua",
[4] = "*"
}
function Loader:Find(path, mode, search_path)
mode = find_mode[mode] or find_mode.all
if path[#path] ~= "/" then
path = path .."/"
end
local skip_dirs = find_dirs[mode] == nil
local files, dirs = file.Find(path .. find_pattern[mode], search_path or "LUA")
local i, v = 1
return function()
if i > #files then
if skip_dirs then return end
v = dirs[i - #files]
else
v = files[i]
end
if v == nil then return end
i = i + 1
return path .. v, v, i > #files
end
end
--[[
for path, name, isdir in loader:Find("addon_name/src/", "all") do
print(path, name, isdir)
end
]]--
function Loader:IncludeDir(path, recursive, realm, storage, include_realm_order, force_realm, include_1st, _base_path_len, _lvl)
_base_path_len = _base_path_len or #path + 2
_lvl = _lvl or 1
if path[#path] ~= "/" then
path = path .."/"
end
if force_realm == nil then
force_realm = {
["init.lua"] = "sh"
}
end
if include_1st == nil then
include_1st = {
"init.lua",
"shared.lua"
}
end
local include_order = {}
for i, f in ipairs(include_1st) do
include_order[f] = i
end
if include_realm_order == nil then
include_realm_order = {
["sh"] = 1,
["sv"] = 2,
["cl"] = 3
}
end
local files, folders = file.Find(path .. (recursive and "*" or "*.*"), "LUA")
if self._DEBUG and is_client[realm] ~= false then
print(string.rep("\t", _lvl - 1) .."Loader:IncludeDir(".. (realm or "?") .. (recursive and ", recursive" or "") ..") > ".. path)
end
table.sort(files, function(a, b)
if include_order[a] then
if include_order[b] then return include_order[a] < include_order[b] end -- force load init.lua & shared.lua 1st
return true
end
local realm_a = force_realm[a] or a:sub(1, 2)
local realm_b = force_realm[b] or b:sub(1, 2)
if include_realm_order[realm_a] ~= include_realm_order[realm_b] then
return include_realm_order[realm_a] < include_realm_order[realm_b] -- by realm sh > sv > cl
end
return a < b -- alphabetically
end)
for _, f in ipairs(files) do
if storage then
storage[
self:GetFilename(recursive and (path:sub(_base_path_len) .. f) or f)
] = self:Include(path .. f, force_realm[f] or realm, _lvl)
else
self:Include(path .. f, force_realm[f] or realm, _lvl)
end
end
if recursive ~= true then return end
for _, f in ipairs(folders) do
self:IncludeDir(path .. f, recursive, realm, storage, include_realm_order, force_realm, include_1st, _base_path_len, _lvl + 1)
end
end
function Loader:IncludeDirRelative(recursive, realm, storage, include_realm_order, force_realm, include_1st)
self:IncludeDir(self:GetCurrentDir(), recursive, realm, storage, include_realm_order, force_realm, include_1st)
end
function Loader:AddCsDir(path, recursive, _lvl)
_lvl = _lvl or 1
if path[#path] ~= "/" then
path = path .."/"
end
local files, folders = file.Find(path .. (recursive and "*" or "*.*"), "LUA")
if self._DEBUG then
print(string.rep("\t", _lvl - 1) .."Loader:AddCsDir(".. (recursive and "recursive" or "") ..") > ".. path)
end
for _, f in ipairs(files) do
if self._DEBUG then
print(string.rep("\t", _lvl) .." ".. path .. f)
end
pcall(AddCSLuaFile, path .. f)
end
if not recursive then return end
for _, f in ipairs(folders) do
self:AddCsDir(path .. f, true, _lvl + 1)
end
end
if SERVER then
function Loader:ResourceAdd(path, recursive, pattern)
if path[#path] ~= "/" then
path = path .."/"
end
local files = file.Find(path .. (pattern or "*"), "GAME")
for _, fname in ipairs(files) do
resource.AddSingleFile(path .. fname)
end
if recursive then
for _, fname in ipairs(folders) do
self:ResourceAdd(path .. fname, recursive, pattern)
end
end
end
end
function Loader:RegisterEntity(path, base, class, cback)
local _ENT = ENT
ENT = {
Base = "base_entity",
Type = "anim",
Author = "Beelzebub",
Contact = "https://discord.incredible-gmod.ru",
Category = "Incredible GMod"
}
if base then
ENT.Type = nil
ENT.Base = base
end
if file.IsDir(path) then
if class == nil then
class = path:match("([^/]+)$")
end
self:Include(path .."/sh.lua", "sh")
self:Include(path .."/shared.lua", "sh")
self:Include(path .."/cl.lua", "cl")
self:Include(path .."/client.lua", "cl")
self:Include(path .."/cl_init.lua", "cl")
self:Include(path .."/sv.lua", "sv")
self:Include(path .."/server.lua", "sv")
self:Include(path .."/init.lua", "sv")
else
if class == nil then
class = path:match("([%w_]*).lua")
end
self:Include(path)
end
if cback then cback(ENT) end
if class then
scripted_ents.Register(ENT, class)
end
ENT = _ENT
end
function Loader:Require(path)
if util.IsBinaryModuleInstalled(path) then
return require(path)
end
if file.IsDir(path, "LUA") then
if path[#path] ~= "/" then
path = path .."/"
end
return Loader:include(path .."init.lua")
end
return Loader:Include(path)
end
return Loader