-
Notifications
You must be signed in to change notification settings - Fork 11
/
loader_min.lua
117 lines (92 loc) · 2.53 KB
/
loader_min.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
-- from incredible-gmod.ru with <3
-- https://github.com/Be1zebub/Small-GLua-Things/blob/master/loader_min.lua
local debug = true
local include_realm_order = {
["sh"] = 1,
["sv"] = 2,
["cl"] = 3
}
local include_realm = {
sv = SERVER and include or function() end,
cl = SERVER and AddCSLuaFile or include,
sh = function(f)
AddCSLuaFile(f)
return include(f)
end
}
local dirColor
if debug then
if SERVER then
local function FormatColor(r, g, b)
return string.format("\27[38;2;%d;%d;%dm", r, g, b)
end
local reset = "\27[0m "
local debug_realms = {
sv = FormatColor(3, 169, 244) .."server".. reset,
cl = FormatColor(222, 169, 9) .."client".. reset,
sh = FormatColor(3, 169, 244) .."sha".. FormatColor(222, 169, 9) .."red".. reset,
}
function debug(realm, path, lvl)
MsgN(string.rep("\t", lvl - 1) .. debug_realms[realm] .. path)
end
dirColor = FormatColor(100, 150, 255)
else
local debug_realms = {
sv = function()
return Color(3, 169, 244), "server "
end,
cl = function()
return Color(222, 169, 9), "client "
end,
sh = function()
return Color(222, 169, 9), "sha", Color(3, 169, 244), "red "
end
}
local white = Color(255, 255, 255)
function debug(realm, path, lvl)
MsgC(string.rep("\t", lvl - 1), debug_realms[realm]())
MsgC(white, path, "\n")
end
end
else
debug = function() end
end
local function IncludeDir(path, storage, _lvl)
if path[#path] ~= "/" then
path = path .."/"
end
_lvl = _lvl or 1
if debug then
MsgN(dirColor, string.rep("\t", _lvl - 1), "> IncludeDir(\"".. path:sub(1, #path - 1) .."\")")
_lvl = _lvl + 1
end
local files, folders = file.Find(path .."*", "LUA")
table.sort(files, function(a, b)
local realm_a, realm_b = a:sub(1, 2), b:sub(1, 2)
if include_realm[realm_a] == nil then realm_a = "sh" end
if include_realm[realm_b] == nil then realm_b = "sh" end
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:sub(3) < b:sub(3) -- alphabetically
end)
for _, f in ipairs(files) do
local realm = f:sub(1, 2)
realm = include_realm[realm] and realm or "sh"
local fpath = path .. f
include_realm[realm](fpath)
if storage then storage[fpath] = v end
debug(realm, fpath, _lvl)
end
for _, f in ipairs(folders) do
local fpath = path .. f
local dir
if storage then
dir = {isdir = true}
storage[fpath] = dir
end
IncludeDir(fpath, dir, _lvl + 1)
end
return storage
end
return IncludeDir