-
Notifications
You must be signed in to change notification settings - Fork 7
/
loader.lua
515 lines (435 loc) · 15.6 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
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
GLib.Loader = {}
GLib.Loader.File = {}
local CompileString = CompileString
for k, v in pairs (file) do
GLib.Loader.File [k] = v
end
if CLIENT then
CreateClientConVar ("glib_use_local_files", 0, true, false)
end
function GLib.Loader.CompileFile (path)
local _, compiled = GLib.Loader.File.Read (path, "LUA")
if compiled then return compiled end
return CompileFile (path)
end
function GLib.Loader.CompileString (code, path, errorMode)
if string.find (code, "^\xef\xbb\xbf") then
code = string.sub (code, 4)
end
local compiled = CompileString (code, path, errorMode)
if type (compiled) == "function" then
setfenv (compiled, GLib.Loader.Environment)
end
return compiled
end
local localLuaPathId = CLIENT and "LCL" or "LSV"
function GLib.Loader.File.Exists (path, pathId)
if pathId ~= "LUA" and pathId ~= localLuaPathId then return file.Exists (path, pathId) end
if file.Exists (path, pathId) then return true end
if GLib.Loader.PackFileManager:GetMergedPackFileSystem ():Exists (path) then return true end
return false
end
function GLib.Loader.File.Find (path, pathId)
if pathId ~= "LUA" and pathId ~= localLuaPathId then return file.Find (path, pathId) end
local files, folders = file.Find (path, pathId)
local fileSet = {}
local folderSet = {}
for _, v in ipairs (files ) do fileSet [v:lower ()] = true end
for _, v in ipairs (folders) do folderSet [v:lower ()] = true end
local files2, folders2 = GLib.Loader.PackFileManager:GetMergedPackFileSystem ():Find (path)
for _, v in ipairs (files2) do
if not fileSet [v:lower ()] then files [#files + 1] = v end
end
for _, v in ipairs (folders2) do
if not folderSet [v:lower ()] then folders [#folders + 1] = v end
end
return files, folders
end
function GLib.Loader.File.Read (path, pathId)
if pathId ~= "LUA" and pathId ~= localLuaPathId then return file.Read (path, pathId) end
local otherPathId = pathId == "LUA" and localLuaPathId or "LUA"
local canRunLocal = pathId == localLuaPathId or GetConVar ("sv_allowcslua"):GetBool ()
-- pathId is LUA or LCL, assume that we're trying to include () the file.
local contents = nil
local compiled = nil
if GLib.Loader.ShouldPackOverrideLocalFiles () then
-- Check pack file
contents, compiled = GLib.Loader.PackFileManager:GetMergedPackFileSystem ():Read (path)
-- Check given path
if not contents and not compiled and
file.Exists (path, pathId) then
contents = file.Read (path, pathId)
compiled = function ()
include (path)
end
end
-- Check other path
local canRunOtherPath = otherPathId == "LUA" or canRunLocal
if not contents and not compiled and
canRunOtherPath and file.Exists (path, otherPathId) then
contents = file.Read (path, otherPathId)
compiled = function ()
include (path)
end
end
else
-- Check LCL path
if canRunLocal and file.Exists (path, localLuaPathId) then
contents = file.Read (path, localLuaPathId)
compiled = function ()
include (path)
end
end
-- Check pack file
if not contents and not compiled then
contents, compiled = GLib.Loader.PackFileManager:GetMergedPackFileSystem ():Read (path)
end
-- Check LUA path
if not contents and not compiled and
file.Exists (path, "LUA") then
contents = file.Read (path, "LUA")
compiled = function ()
include (path)
end
end
end
return contents, compiled
end
GLib.Loader.Exists = GLib.Loader.File.Exists
GLib.Loader.Find = GLib.Loader.File.Find
GLib.Loader.Read = GLib.Loader.File.Read
local pathStack = { "" }
function GLib.Loader.Include (path)
local callerPath = debug.getinfo (2).short_src
if callerPath:sub (1, 1) == "@" then callerPath = callerPath:sub (2) end
callerPath = callerPath:match ("lua/(.*)") or callerPath
local callerDirectory = ""
if callerPath:find ("/") then
callerDirectory = callerPath:sub (1, callerPath:find ("/[^/]*$"))
else
callerDirectory = ""
end
local fullPath = pathStack [#pathStack] .. path
local code, compiled = GLib.Loader.File.Read (pathStack [#pathStack] .. path, "LUA")
if not code and not compiled then
fullPath = callerDirectory .. path
code, compiled = GLib.Loader.File.Read (callerDirectory .. path, "LUA")
end
if not code and not compiled then
fullPath = path
code, compiled = GLib.Loader.File.Read (path, "LUA")
end
if not code and not compiled then
GLib.Error ("GLib.Loader.Include : " .. path .. ": File not found (Path was " .. pathStack [#pathStack] .. ", caller path was " .. callerDirectory .. ").\n")
else
compiled = compiled or GLib.Loader.CompileString (code, "lua/" .. fullPath, false)
if type (compiled) == "function" then
pathStack [#pathStack + 1] = fullPath:sub (1, fullPath:find ("/[^/]*$"))
return (
function (success, ...)
pathStack [#pathStack] = nil
if not success then return end
return ...
end
) (xpcall (compiled, GLib.Error))
else
ErrorNoHalt ("GLib.Loader.Include : " .. fullPath .. ": File failed to compile:\n\t" .. tostring (compiled) .. "\n")
end
end
end
GLib.Loader.Environment = setmetatable (
{
AddCSLuaFile = function () end,
file = GLib.Loader.File,
include = GLib.Loader.Include,
CompileFile = GLib.Loader.CompileFile
},
{
__index = getfenv (),
__newindex = function (_, k, v)
getfenv () [k] = v
end
}
)
function GLib.Loader.RunPackFile (executionTarget, packFileSystem, callback)
callback = callback or GLib.NullCallback
local shouldRun = executionTarget == "sh"
shouldRun = shouldRun or executionTarget == "m"
if SERVER and executionTarget == "sv" then shouldRun = true end
if CLIENT and executionTarget == "cl" then shouldRun = true end
if shouldRun then
for i = 1, packFileSystem:GetSystemTableCount () do
GLib.Loader.PackFileManager:GetMergedPackFileSystem ():AddSystemTable (packFileSystem:GetSystemTableName (i))
end
if GLib.Loader.ShouldPackOverrideLocalFiles () then
-- Unload systems in reverse load order
for i = packFileSystem:GetSystemTableCount (), 1, -1 do
local systemTableName = packFileSystem:GetSystemTableName (i)
if systemTableName == "GLib" then
_G [systemTableName].Stage2 = nil
elseif _G [systemTableName] then
print ("GLib.Loader : Unloading " .. systemTableName .. " to prepare for replacement...")
GLib.UnloadSystem (systemTableName)
end
end
end
packFileSystem:MergeInto (GLib.Loader.PackFileManager:GetMergedPackFileSystem ())
-- Shared autoruns
local files, _ = packFileSystem:Find ("autorun/*.lua")
for _, fileName in ipairs (files) do
GLib.Loader.Include ("autorun/" .. fileName)
end
-- Local autoruns
local files, _ = packFileSystem:Find ("autorun/" .. (SERVER and "server" or "client") .. "/*.lua")
for _, fileName in ipairs (files) do
GLib.Loader.Include ("autorun/" .. (SERVER and "server" or "client") .. "/" .. fileName)
end
local prefix = SERVER and "" or "cl_"
-- Effects
if CLIENT then
local _, folders = packFileSystem:Find ("effects/*")
for _, className in ipairs (folders) do
local _EFFECT = EFFECT
EFFECT = {}
GLib.Loader.Include ("effects/" .. className .. "/init.lua")
effects.Register (EFFECT, className)
EFFECT = _EFFECT
end
end
-- Entities
local _, folders = packFileSystem:Find ("entities/*")
for _, className in ipairs (folders) do
local _ENT = ENT
ENT = {}
ENT.Type = "anim"
ENT.Base = "base_anim"
ENT.ClassName = className
-- Run file
if packFileSystem:Exists ("entities/" .. className .. "/" .. prefix .. "init.lua") then
GLib.Loader.Include ("entities/" .. className .. "/" .. prefix .. "init.lua")
elseif packFileSystem:Exists ("entities/" .. className .. "/shared.lua") then
GLib.Loader.Include ("entities/" .. className .. "/shared.lua")
end
scripted_ents.Register (ENT, ENT.ClassName, true)
-- Update existing entities
for _, ent in ipairs (ents.FindByClass (ENT.ClassName)) do
table.Merge (ent:GetTable (), ENT)
end
ENT = _ENT
end
-- Weapons
local _, folders = packFileSystem:Find ("weapons/*")
for _, className in ipairs (folders) do
local success = false
local _SWEP = SWEP
SWEP = {}
SWEP.Primary = {}
SWEP.Secondary = {}
SWEP.ClassName = className
-- Run file
if packFileSystem:Exists ("weapons/" .. className .. "/" .. prefix .. "init.lua") then
success = true
GLib.Loader.Include ("weapons/" .. className .. "/" .. prefix .. "init.lua")
elseif packFileSystem:Exists ("weapons/" .. className .. "/shared.lua") then
success = true
GLib.Loader.Include ("weapons/" .. className .. "/shared.lua")
end
if success then
weapons.Register (SWEP, SWEP.ClassName, true)
-- Update existing entities
for _, ent in ipairs (ents.FindByClass (SWEP.ClassName)) do
table.Merge (ent:GetTable (), SWEP)
end
end
SWEP = _SWEP
end
-- Events
for i = 1, packFileSystem:GetSystemTableCount () do
local systemTableName = packFileSystem:GetSystemTableName (i)
GLib:DispatchEvent ("PackFileLoaded", systemTableName)
end
callback (true)
else
callback (false)
end
end
function GLib.Loader.RunSerializedPackFile (executionTarget, serializedPackFile, compressed, packFileName, callback)
callback = callback or GLib.NullCallback
local shouldRun = executionTarget == "sh"
shouldRun = shouldRun or executionTarget == "m"
if SERVER and executionTarget == "sv" then shouldRun = true end
if CLIENT and executionTarget == "cl" then shouldRun = true end
if shouldRun then
local packFileSystem = GLib.Loader.PackFileSystem ()
packFileSystem:SetName (packFileName)
local startTime = SysTime ()
packFileSystem:Deserialize (serializedPackFile, compressed,
function (decompressedSize)
local fileSize = GLib.FormatFileSize (#serializedPackFile)
if compressed then
fileSize = fileSize .. " decompressed to " .. GLib.FormatFileSize (decompressedSize)
end
Msg ("GLib.Loader : Running pack file \"" .. packFileSystem:GetName () .. "\", deserialization took " .. GLib.FormatDuration (SysTime () - startTime) .. " (" .. packFileSystem:GetFileCount () .. " total files, " .. fileSize .. ")...")
startTime = SysTime ()
GLib.Loader.RunPackFile (executionTarget, packFileSystem, callback)
MsgN (" took " .. GLib.FormatDuration (SysTime () - startTime) .. ".")
end
)
else
callback (false)
end
if SERVER then
if executionTarget == "sh" or executionTarget == "cl" then
if not shouldRun then
print ("GLib.Loader : Forwarding pack file \"" .. packFileName .. "\" on to clients.")
end
GLib.Loader.StreamPack (GLib.GetEveryoneId (), executionTarget, compressed and serializedPackFile or util.Compress (serializedPackFile), packFileName)
end
end
end
function GLib.Loader.ShouldPackOverrideLocalFiles ()
if SERVER then return true end
if not GetConVar ("sv_allowcslua"):GetBool () then return true end
return not GetConVar ("glib_use_local_files"):GetBool ()
end
function GLib.Loader.StreamPack (destinationId, executionTarget, compressedSerializedPackFile, displayName)
local outBuffer = GLib.StringOutBuffer ()
outBuffer:String (executionTarget)
outBuffer:String (displayName or "<unnamed>")
outBuffer:LongString (compressedSerializedPackFile)
GLib.Transfers.Send (destinationId, "GLib.LuaPack", outBuffer:GetString ())
end
local function IsUserIdSuperAdmin (userId)
if userId == "Server" then return true end
for _, v in pairs (player.GetAll ()) do
if GLib.GetPlayerId (v) == userId then
return v:IsSuperAdmin ()
end
end
return false
end
GLib.Transfers.RegisterHandler ("GLib.LuaPack",
function (userId, data)
if SERVER and not IsUserIdSuperAdmin (userId) then return end
local inBuffer = GLib.StringInBuffer (data)
local executionTarget = inBuffer:String ()
local displayName = inBuffer:String ()
local compressedSerializedPackFile = inBuffer:LongString ()
GLib.Loader.RunSerializedPackFile (executionTarget, compressedSerializedPackFile, true, displayName)
end
)
GLib.Transfers.RegisterInitialPacketHandler ("GLib.LuaPack",
function (userId, data)
if SERVER and not IsUserIdSuperAdmin (userId) then
local inBuffer = GLib.StringInBuffer (data)
local executionTarget = inBuffer:String ()
local displayName = inBuffer:String ()
print ("GLib.Loader : Rejecting lua pack " .. displayName .. " from " .. userId .. ".")
return false
end
end
)
if SERVER then
concommand.Add ("glib_request_pack",
function (ply)
if not ply or not ply:IsValid () then return end
GLib.Loader.PackFileManager:GenerateResources ()
local outBuffer = GLib.StringOutBuffer ()
GLib.Loader.PackFileManager:SerializeManifest (outBuffer)
GLib.Transfers.Send (GLib.GetPlayerId (ply), "GLib.Loader.Manifest", outBuffer:GetString ())
end
)
elseif CLIENT then
GLib.Transfers.RegisterHandler ("GLib.Loader.Manifest",
function (userId, data)
local inBuffer = GLib.StringInBuffer (data)
local packFileEntries = {}
local resourceId = inBuffer:String ()
while resourceId ~= "" do
local versionHash = inBuffer:String ()
packFileEntries [#packFileEntries + 1] =
{
ResourceId = resourceId,
VersionHash = versionHash,
Data = nil,
PackFileSystem = GLib.Loader.PackFileSystem ()
}
packFileEntries [#packFileEntries].PackFileSystem:SetName (resourceId)
resourceId = inBuffer:String ()
end
print ("GLib.Loader : Received manifest (" .. #packFileEntries .. " pack file" .. (#packFileEntries == 1 and "" or "s") .. ").")
local i = 1
local requestNextResource
local deserializeNextPackFile
local runNextPackFile
function requestNextResource ()
if i > #packFileEntries then
-- Finished requesting pack files, run them all.
i = 1
deserializeNextPackFile ()
return
end
GLib.Resources.Get ("LuaPack", packFileEntries [i].ResourceId, packFileEntries [i].VersionHash,
function (success, data)
if not success then
GLib.Error ("GLib.Loader : Failed to get resource " .. packFileEntries [i].ResourceId .. " (" .. packFileEntries [i].VersionHash .. ").")
end
packFileEntries [i].Data = data
i = i + 1
requestNextResource ()
end
)
end
function deserializeNextPackFile ()
if i > #packFileEntries then
-- Finished deserializing pack files, run them all.
i = 1
runNextPackFile ()
return
end
local startTime = SysTime ()
local packFileEntry = packFileEntries [i]
local packFileSystem = packFileEntries [i].PackFileSystem
if packFileEntries [i].Data then
packFileSystem:Deserialize (packFileEntries [i].Data, false,
function ()
packFileEntry.DeserializationDuration = SysTime () - startTime
i = i + 1
deserializeNextPackFile ()
end
)
else
i = i + 1
deserializeNextPackFile ()
end
end
function runNextPackFile ()
if i > #packFileEntries then
-- Finished running pack files.
return
end
local startTime = SysTime ()
local packFileEntry = packFileEntries [i]
local packFileSystem = packFileEntries [i].PackFileSystem
local fileSize = GLib.FormatFileSize (#packFileEntries [i].Data)
MsgN ("GLib.Loader : Running pack file \"" .. packFileSystem:GetName () .. "\", deserialization took " .. GLib.FormatDuration (packFileEntry.DeserializationDuration) .. " (" .. packFileSystem:GetFileCount () .. " total files, " .. fileSize .. ").")
GLib.Loader.RunPackFile ("m", packFileSystem,
function ()
i = i + 1
runNextPackFile ()
end
)
end
requestNextResource ()
end
)
GLib.WaitForLocalPlayer (
function ()
timer.Simple (5,
function ()
RunConsoleCommand ("glib_request_pack")
end
)
end
)
end