forked from Be1zebub/Small-GLua-Things
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathloader.lua
More file actions
219 lines (172 loc) · 5.59 KB
/
loader.lua
File metadata and controls
219 lines (172 loc) · 5.59 KB
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
-- from incredible-gmod.ru with <3
-- https://github.com/Be1zebub/Small-GLua-Things/blob/master/loader.lua
local Loader = {
_VERSION = 1.3,
_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 or "sh"]
if worker == nil then
realm = "sh"
worker = 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, ext)
return path:match(ext and ("([%w_]*).".. ext) or "^.+/(.+)$") or path:match("([%w_]*).lua")
end
function Loader:GetFilenameWithExt(path)
return path:match("([%w_]*).lua")
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)
realm = realm or string.sub(self:GetFilename(path), 1, 2)
return self:include(path, realm, _lvl)
end
function Loader:ScanDir(dir, cback, recursive, _lvl)
_lvl = _lvl or 1
local path = dir .."/"
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
}
function Loader:IncludeDir(dir, recursive, realm, storage, _base_path_len, _lvl)
_base_path_len = _base_path_len or #dir + 2
_lvl = _lvl or 1
local path = dir .."/"
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 "") ..") > ".. dir)
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, realm, _lvl)
else
self:Include(path .. f, realm, _lvl)
end
end
if not recursive then return end
for _, f in ipairs(folders) do
self:IncludeDir(path .. f, recursive, realm, storage, _base_path_len, _lvl + 1)
end
end
function Loader:IncludeDirRelative(recursive, realm, storage)
self:IncludeDir(self:GetCurrentDir(), recursive, realm, storage)
end
function Loader:AddCsDir(dir, recursive, _lvl)
_lvl = _lvl or 1
local path = dir .."/"
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 "") ..") > ".. dir)
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(dir, recurse, pattern)
local files = file.Find(dir .. (pattern and ("/".. pattern) or "/*"), "GAME")
for _, fname in ipairs(files) do
resource.AddSingleFile(dir .."/".. fname)
end
if recurse then
for _, fname in ipairs(folders) do
self:ResourceAdd(dir .."/".. fname, recurse, pattern)
end
end
end
end
function Loader:RegisterEntity(path, base, class, cback)
local _ENT = ENT
ENT = istable(base) and base or {
Base = "base_entity",
Type = "anim",
Author = "Beelzebub",
Contact = "https://discord.incredible-gmod.ru",
Category = "Incredible GMod"
}
if isstring(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
return Loader