-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathsh_module_loader.lua
More file actions
648 lines (537 loc) · 21.9 KB
/
Copy pathsh_module_loader.lua
File metadata and controls
648 lines (537 loc) · 21.9 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
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
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
prp = prp or {}
prp.ModuleLoader = prp.ModuleLoader or {}
prp.ModuleLoader.LoadedModules = {}
prp.ModuleLoader.ModuleDependencies = {}
prp.ModuleLoader.LoadOrder = {}
if not prp.Util then
prp.Util = {}
function prp.Util:Log(message, level)
local timestamp = os.date("%H:%M:%S")
local levelStr = level or "INFO"
print(string.format("[%s] [%s] %s", timestamp, levelStr, message))
end
end
prp.ModuleLoader.Folders = {
['gamemode'] = 'gamemode/',
['configs'] = 'gamemode/configs/',
['core'] = 'gamemode/core/',
['modules'] = 'gamemode/modules/'
}
prp.ModuleLoader.FileTypes = {
['sh_'] = 'shared',
['sv_'] = 'server',
['cl_'] = 'client'
}
local moduleCounter = 0
function prp.ModuleLoader:GetNextModuleID()
moduleCounter = moduleCounter + 1
return moduleCounter
end
function prp.ModuleLoader:GetFileType(fileName)
for prefix, fileType in pairs(self.FileTypes) do
if fileName:sub(1, #prefix) == prefix then
return fileType
end
end
return 'unknown'
end
function prp.ModuleLoader:ExtractFileName(filePath)
if not filePath then return "unknown" end
filePath = filePath:gsub("^@", "")
local fileName = filePath:match("([^/\\]+)$")
return fileName or "unknown"
end
function prp.ModuleLoader:ExtractDirectory(filePath)
if not filePath then return "" end
filePath = filePath:gsub("^@", "")
local directory = filePath:match("(.*[/\\])")
return directory or ""
end
function prp.ModuleLoader:ExtractModuleName(filePath)
if not filePath then return "unknown" end
local modulePath = filePath:match("modules/([^/]+)")
if modulePath then
return modulePath
end
local pathParts = {}
for part in filePath:gmatch("[^/]+") do
table.insert(pathParts, part)
end
for i, part in ipairs(pathParts) do
if part == "modules" and pathParts[i + 1] then
return pathParts[i + 1]
end
end
if #pathParts > 1 then
return pathParts[#pathParts - 1]
end
return "unknown"
end
function prp.ModuleLoader:AddLoadedModule(filePath, fileType, dependencies, loadMethod)
for _, existingModule in pairs(self.LoadedModules) do
if existingModule.fullPath == filePath then
if prp and prp.Util and prp.Util.Log then
prp.Util:Log("File already loaded, skipping: " .. filePath, "WARNING")
end
return existingModule.id
end
end
local moduleID = self:GetNextModuleID()
local fileName = self:ExtractFileName(filePath)
local directory = self:ExtractDirectory(filePath)
local moduleName = self:ExtractModuleName(filePath)
local moduleData = {
id = moduleID,
name = fileName,
fullPath = filePath,
directory = directory,
moduleName = moduleName,
fileType = fileType,
loadTime = os.time(),
dependencies = dependencies or {},
isLoaded = true,
loadMethod = loadMethod or "unknown"
}
self.LoadedModules[moduleID] = moduleData
table.insert(self.LoadOrder, moduleID)
if not self.ModuleDependencies[moduleName] then
self.ModuleDependencies[moduleName] = {}
end
self.ModuleDependencies[moduleName][fileName] = moduleData
return moduleID
end
function prp.ModuleLoader:CheckDependencies(moduleName, dependencies)
if not dependencies or #dependencies == 0 then
return true
end
for _, dep in ipairs(dependencies) do
local found = false
for _, moduleData in pairs(self.LoadedModules) do
if moduleData.name == dep then
found = true
break
end
end
if not found then
if prp and prp.Util and prp.Util.Log then
prp.Util:Log("Dependency not found: " .. dep .. " for module " .. moduleName, "ERROR")
end
return false
end
end
return true
end
function prp.ModuleLoader:LoadFile(filePath, fileType, dependencies)
local loadMethod = "unknown"
if fileType == 'shared' then
include(filePath)
AddCSLuaFile(filePath)
loadMethod = "both"
elseif fileType == 'server' then
include(filePath)
loadMethod = "include"
elseif fileType == 'client' then
AddCSLuaFile(filePath)
loadMethod = "AddCSLuaFile"
end
local moduleID = self:AddLoadedModule(filePath, fileType, dependencies, loadMethod)
if prp and prp.Util and prp.Util.Log then
prp.Util:Log("Loaded " .. fileType .. " file: " .. filePath .. " (" .. loadMethod .. ")", "INFO")
end
return moduleID
end
function prp.ModuleLoader:LoadModule(modulePath, dependencies)
local files = {}
local fol = GM.FolderName .. "/" .. modulePath
for prefix, fileType in pairs(self.FileTypes) do
local pattern = fol .. "/" .. prefix .. "*.lua"
for _, fileName in SortedPairs(file.Find(pattern, "LUA"), true) do
if fileName:match("interface%.lua$") then continue end
local filePath = modulePath .. "/" .. fileName
local fullPath = fol .. "/" .. fileName
if self:CheckDependencies(fileName, dependencies) then
self:LoadFile(fullPath, fileType, dependencies)
else
if prp and prp.Util and prp.Util.Log then
prp.Util:Log("Skipping " .. fileName .. " due to missing dependencies", "WARNING")
end
end
end
end
end
function prp.ModuleLoader:LoadAllModules()
local fol = ""
if GM and GM.FolderName then
fol = GM.FolderName .. "/gamemode/modules/"
elseif GAMEMODE and GAMEMODE.FolderName then
fol = GAMEMODE.FolderName .. "/gamemode/modules/"
else
fol = "gamemodes/purpur-master/gamemode/modules/"
end
local function FindAllFiles(basePath, currentPath)
local files = {}
local folders = {}
local items, dirs = file.Find(basePath .. currentPath .. "*", "LUA")
if items then
for _, item in pairs(items) do
if item:match("%.lua$") then
local fullPath = currentPath .. item
local relativePath = "modules/" .. fullPath
table.insert(files, {
fullPath = basePath .. fullPath,
relativePath = relativePath,
fileName = item,
modulePath = currentPath:gsub("/$", "")
})
end
end
end
if dirs then
for _, dir in pairs(dirs) do
if dir ~= "." and dir ~= ".." then
local subFiles = FindAllFiles(basePath, currentPath .. dir .. "/")
for _, file in pairs(subFiles) do
table.insert(files, file)
end
end
end
end
return files
end
local allFiles = FindAllFiles(fol, "")
if #allFiles == 0 then
if prp and prp.Util and prp.Util.Log then
prp.Util:Log("No modules found in modules folder", "WARNING")
end
return
end
local filePriority = {
["sh_"] = 1,
["sv_"] = 2,
["cl_"] = 3
}
table.sort(allFiles, function(a, b)
local aPrefix = a.fileName:sub(1, 3)
local bPrefix = b.fileName:sub(1, 3)
local aPriority = filePriority[aPrefix] or 999
local bPriority = filePriority[bPrefix] or 999
if aPriority == bPriority then
return a.fileName < b.fileName
end
return aPriority < bPriority
end)
for _, fileInfo in ipairs(allFiles) do
local fileName = fileInfo.fileName
local fullPath = fileInfo.fullPath
local relativePath = fileInfo.relativePath
local modulePath = fileInfo.modulePath
local fileType = "unknown"
for prefix, type in pairs(self.FileTypes) do
if fileName:sub(1, #prefix) == prefix then
fileType = type
break
end
end
if fileName:match("interface%.lua$") then
if prp and prp.Util and prp.Util.Log then
prp.Util:Log("Skipping interface file: " .. fileName, "INFO")
end
else
local loadMethod = "unknown"
if fileType == 'shared' then
include(fullPath)
AddCSLuaFile(fullPath)
loadMethod = "both"
elseif fileType == 'server' then
include(fullPath)
loadMethod = "include"
elseif fileType == 'client' then
AddCSLuaFile(fullPath)
loadMethod = "AddCSLuaFile"
end
self:AddLoadedModule(relativePath, fileType, {}, loadMethod)
if prp and prp.Util and prp.Util.Log then
prp.Util:Log("Loaded " .. fileType .. " file: " .. relativePath .. " (" .. loadMethod .. ")", "INFO")
end
end
end
if prp and prp.Util and prp.Util.Log then
prp.Util:Log("Total modules loaded: " .. #allFiles, "INFO")
end
end
function prp.ModuleLoader:RegisterExistingFile(filePath, fileType, dependencies, loadMethod)
local moduleID = self:AddLoadedModule(filePath, fileType, dependencies, loadMethod)
if prp and prp.Util and prp.Util.Log then
prp.Util:Log("Registered existing file: " .. filePath .. " (" .. (loadMethod or "unknown") .. ")", "INFO")
end
return moduleID
end
concommand.Add("prp_modules_list", function(ply, cmd, args)
if not IsValid(ply) or not ply:IsSuperAdmin() then return end
print("=== Загруженные модули ===")
print(string.format("%-5s %-20s %-15s %-10s %-15s %-30s", "ID", "Имя файла", "Тип", "Модуль", "Метод загрузки", "Путь"))
print(string.rep("-", 100))
for moduleID, moduleData in pairs(prp.ModuleLoader.LoadedModules) do
print(string.format("%-5s %-20s %-15s %-10s %-15s %-30s",
moduleID,
moduleData.name:sub(1, 18),
moduleData.fileType,
moduleData.moduleName:sub(1, 8),
moduleData.loadMethod,
moduleData.fullPath:sub(1, 28)
))
end
print("Всего загружено модулей: " .. moduleCounter)
end)
concommand.Add("prp_modules_by_type", function(ply, cmd, args)
if not IsValid(ply) or not ply:IsSuperAdmin() then return end
local fileType = args[1] or "all"
print("=== Модули по типу: " .. fileType .. " ===")
for moduleID, moduleData in pairs(prp.ModuleLoader.LoadedModules) do
if fileType == "all" or moduleData.fileType == fileType then
print(string.format("[%d] %s (%s) - %s [%s]",
moduleID,
moduleData.name,
moduleData.fileType,
moduleData.moduleName,
moduleData.loadMethod
))
end
end
end)
concommand.Add("prp_module_deps", function(ply, cmd, args)
if not IsValid(ply) or not ply:IsSuperAdmin() then return end
local moduleName = args[1]
if not moduleName then
print("Использование: prp_module_deps <имя_модуля>")
return
end
print("=== Зависимости модуля: " .. moduleName .. " ===")
if prp.ModuleLoader.ModuleDependencies[moduleName] then
for fileName, moduleData in pairs(prp.ModuleLoader.ModuleDependencies[moduleName]) do
print(string.format("- %s (%s) [%s]", fileName, moduleData.fileType, moduleData.loadMethod))
if moduleData.dependencies and #moduleData.dependencies > 0 then
print(" Зависимости: " .. table.concat(moduleData.dependencies, ", "))
end
end
else
print("Модуль не найден")
end
end)
concommand.Add("prp_load_order", function(ply, cmd, args)
if not IsValid(ply) or not ply:IsSuperAdmin() then return end
print("=== Порядок загрузки модулей ===")
for i, moduleID in ipairs(prp.ModuleLoader.LoadOrder) do
local moduleData = prp.ModuleLoader.LoadedModules[moduleID]
if moduleData then
print(string.format("[%d] %s (%s) [%s]", i, moduleData.name, moduleData.fileType, moduleData.loadMethod))
end
end
end)
concommand.Add("prp_modules_stats", function(ply, cmd, args)
if not IsValid(ply) or not ply:IsSuperAdmin() then return end
local stats = {
total = 0,
shared = 0,
server = 0,
client = 0,
modules = {},
loadMethods = {
include = 0,
AddCSLuaFile = 0,
both = 0,
unknown = 0
}
}
for moduleID, moduleData in pairs(prp.ModuleLoader.LoadedModules) do
stats.total = stats.total + 1
stats[moduleData.fileType] = stats[moduleData.fileType] + 1
if stats.loadMethods[moduleData.loadMethod] then
stats.loadMethods[moduleData.loadMethod] = stats.loadMethods[moduleData.loadMethod] + 1
end
if not stats.modules[moduleData.moduleName] then
stats.modules[moduleData.moduleName] = 0
end
stats.modules[moduleData.moduleName] = stats.modules[moduleData.moduleName] + 1
end
print("=== Статистика загруженных модулей ===")
print("Всего файлов: " .. stats.total)
print("Shared файлов: " .. stats.shared)
print("Server файлов: " .. stats.server)
print("Client файлов: " .. stats.client)
print("\nПо методам загрузки:")
print("- include: " .. stats.loadMethods.include)
print("- AddCSLuaFile: " .. stats.loadMethods.AddCSLuaFile)
print("- both: " .. stats.loadMethods.both)
print("- unknown: " .. stats.loadMethods.unknown)
print("\nПо модулям:")
for moduleName, count in pairs(stats.modules) do
print("- " .. moduleName .. ": " .. count .. " файлов")
end
end)
concommand.Add("prp_clean_duplicates", function(ply, cmd, args)
if not IsValid(ply) or not ply:IsSuperAdmin() then return end
print("=== Очистка дублированных записей ===")
local duplicates = {}
local uniqueModules = {}
local removedCount = 0
for moduleID, moduleData in pairs(prp.ModuleLoader.LoadedModules) do
local key = moduleData.fullPath
if uniqueModules[key] then
table.insert(duplicates, moduleID)
removedCount = removedCount + 1
else
uniqueModules[key] = moduleData
end
end
for _, moduleID in ipairs(duplicates) do
prp.ModuleLoader.LoadedModules[moduleID] = nil
end
local newLoadOrder = {}
for _, moduleID in ipairs(prp.ModuleLoader.LoadOrder) do
if prp.ModuleLoader.LoadedModules[moduleID] then
table.insert(newLoadOrder, moduleID)
end
end
prp.ModuleLoader.LoadOrder = newLoadOrder
print("Удалено дублированных записей: " .. removedCount)
print("Осталось уникальных модулей: " .. table.Count(prp.ModuleLoader.LoadedModules))
end)
concommand.Add("prp_register_file", function(ply, cmd, args)
if not IsValid(ply) or not ply:IsSuperAdmin() then return end
local filePath = args[1]
local fileType = args[2] or "shared"
if not filePath then
print("Использование: prp_register_file <путь_к_файлу> [тип]")
return
end
prp.ModuleLoader:RegisterExistingFile(filePath, fileType)
print("Файл зарегистрирован: " .. filePath)
end)
concommand.Add("prp_modules_tree", function(ply, cmd, args)
if not IsValid(ply) or not ply:IsSuperAdmin() then return end
print("=== Структура папок модулей ===")
local gamemodePath = ""
if GM and GM.FolderName then
gamemodePath = GM.FolderName .. "/gamemode/modules/"
elseif GAMEMODE and GAMEMODE.FolderName then
gamemodePath = GAMEMODE.FolderName .. "/gamemode/modules/"
else
gamemodePath = "gamemodes/purpur-master/gamemode/modules/"
end
local function ShowTree(basePath, currentPath, indent)
indent = indent or ""
local items, dirs = file.Find(basePath .. currentPath .. "*", "LUA")
if dirs then
for _, dir in SortedPairs(dirs, true) do
if dir ~= "." and dir ~= ".." then
print(indent .. "📁 " .. dir .. "/")
ShowTree(basePath, currentPath .. dir .. "/", indent .. " ")
end
end
end
if items then
for _, item in SortedPairs(items, true) do
if item:match("%.lua$") then
local prefix = item:sub(1, 3)
local icon = "📄"
if prefix == "sh_" then
icon = "🔄"
elseif prefix == "sv_" then
icon = "🖥️"
elseif prefix == "cl_" then
icon = "💻"
end
print(indent .. icon .. " " .. item)
end
end
end
end
ShowTree(gamemodePath, "")
print("=== Конец структуры ===")
end)
concommand.Add("prp_test_system", function(ply, cmd, args)
if not IsValid(ply) or not ply:IsSuperAdmin() then return end
print("=== Тестирование системы загрузки модулей ===")
if not prp.ModuleLoader then
print("❌ Система загрузки модулей не инициализирована!")
return
end
print("✅ Система загрузки модулей инициализирована")
local moduleCount = 0
for _ in pairs(prp.ModuleLoader.LoadedModules) do
moduleCount = moduleCount + 1
end
print("📊 Загружено модулей: " .. moduleCount)
local fileCounts = {}
local duplicates = {}
for moduleID, moduleData in pairs(prp.ModuleLoader.LoadedModules) do
local key = moduleData.fullPath
fileCounts[key] = (fileCounts[key] or 0) + 1
if fileCounts[key] > 1 then
duplicates[key] = fileCounts[key]
end
end
if next(duplicates) then
print("⚠️ Найдены дублированные файлы:")
for filePath, count in pairs(duplicates) do
print(" - " .. filePath .. " (загружен " .. count .. " раз)")
end
print("💡 Используйте команду prp_clean_duplicates для очистки")
else
print("✅ Дубликатов не найдено")
end
local baseModules = {"sh_util.lua", "sh_hook.lua", "sh_job.lua", "sh_player.lua"}
for _, moduleName in ipairs(baseModules) do
local found = false
for _, moduleData in pairs(prp.ModuleLoader.LoadedModules) do
if moduleData.name == moduleName then
found = true
break
end
end
if found then
print("✅ " .. moduleName .. " загружен")
else
print("❌ " .. moduleName .. " НЕ загружен")
end
end
local nestedModules = {
}
print("\n🔍 Проверка вложенных модулей:")
for _, moduleName in ipairs(nestedModules) do
local found = false
for _, moduleData in pairs(prp.ModuleLoader.LoadedModules) do
if moduleData.name == moduleName then
found = true
print("✅ " .. moduleName .. " загружен (путь: " .. moduleData.fullPath .. ")")
break
end
end
if not found then
print("❌ " .. moduleName .. " НЕ загружен")
end
end
if prp.Debug and prp.Debug.Fallback then
local fallback = prp.Debug:Fallback()
print("✅ Используется модуль загрузки: " .. fallback)
else
print("❌ Модуль загрузки не найден")
end
if prp.Economy and prp.Economy.Money then
print("✅ Модуль экономики загружен")
print(" - Форматирование денег: " .. prp.Economy.Money:Format(1000))
else
print("❌ Модуль экономики не найден")
end
if prp.Weapons and prp.Weapons.Guns and prp.Weapons.Guns.Pistols then
print("✅ Модуль оружия загружен")
print(" - Количество пистолетов: " .. table.Count(prp.Weapons.Guns.Pistols.List))
else
print("❌ Модуль оружия не найден")
end
print("=== Тестирование завершено ===")
end)
if prp and prp.Util and prp.Util.Log then
prp.Util:Log("Universal Module Loader System initialized", "INFO")
end