forked from FlashHit/BundleLoader
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathBundleLoader.lua
More file actions
449 lines (376 loc) · 16.3 KB
/
BundleLoader.lua
File metadata and controls
449 lines (376 loc) · 16.3 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
--[[
Copyright (c) [2023] [Flash_Hit a/k/a Bree_Arnold]
Optimized BundleLoader combining best features from both versions
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.
--]]
if Class then
-- Using LoggingClass
---@class BundleLoader:Class
---@overload fun():BundleLoader
---@diagnostic disable-next-line: assign-type-mismatch
BundleLoader = Class("BundleLoader")
else
BundleLoader = class("BundleLoader")
if DEBUG == nil then DEBUG = false end --测试蓝图崩溃需要打开此选项
function BundleLoader:debug(message, ...)
if not DEBUG then return end
message = string.format("[BundleLoader] DEBUG: " .. message, ...)
print(message)
end
function BundleLoader:error(message, ...)
message = string.format("[BundleLoader] ERROR: " .. message, ...)
print(message)
end
function BundleLoader:warn(message, ...)
message = string.format("[BundleLoader] WARNING: " .. message, ...)
print(message)
end
end
---@enum UiBundleTypes
UiBundleTypes = {
Unknown = 0,
Loading = 1,
Playing = 2,
PreEndOfRound = 3,
EndOfRound = 4
}
function BundleLoader:__init()
self.currentLevelConfig = {}
self.currentGameModeConfig = {}
self.currentLevelGameModeConfig = {}
self.commonConfig = BundleLoader.GetCommonBundleConfig()
self.LevelName = nil
self.commonSuperBundlesMounted = false
-- Install all necessary hooks
Hooks:Install('ResourceManager:LoadBundles', 999, self, self.OnLoadBundles)
Hooks:Install("Terrain:Load", 999, self, self.OnTerrainLoad)
Hooks:Install("VisualTerrain:Load", 999, self, self.OnTerrainLoad)
Hooks:Install("EntityFactory:CreateFromBlueprint", 999, self, self.OnEntityCreateFromBlueprint)
-- Subscribe to Level events for proper resource management
Events:Subscribe('Level:RegisterEntityResources', self, self.OnLevelRegisterEntityResources)
Events:Subscribe('Level:Destroy', self, self.OnLevelDestroy)
return self
end
function BundleLoader:OnLevelDestroy()
self:debug("Level destroyed, performing cleanup...")
-- Clean up configurations for next level
self.currentLevelConfig = {}
self.currentGameModeConfig = {}
self.currentLevelGameModeConfig = {}
self.LevelName = nil
-- Force garbage collection to prevent memory leaks
collectgarbage("collect")
self:debug("Level cleanup completed.")
end
function BundleLoader:UpdateConfig()
self.currentLevelConfig = BundleLoader.GetLevelBundleConfig()
self.currentGameModeConfig = BundleLoader.GetGameModeBundleConfig()
self.currentLevelGameModeConfig = BundleLoader.GetLevelAndGameModeBundleConfig()
local s_LevelName = SharedUtils:GetLevelName()
self.LevelName = s_LevelName
-- Handle level exceptions for game modes
if s_LevelName and self.currentGameModeConfig.exceptionLevelList then
for _, l_LevelName in ipairs(self.currentGameModeConfig.exceptionLevelList) do
if s_LevelName:match(l_LevelName) then
self.currentGameModeConfig = {}
break
end
end
end
-- Handle game mode exceptions for levels
local s_GameMode = SharedUtils:GetCurrentGameMode()
if s_GameMode and self.currentLevelConfig.exceptionGameModeList then
for _, l_GameMode in ipairs(self.currentLevelConfig.exceptionGameModeList) do
if s_GameMode:match(l_GameMode) then
self.currentLevelConfig = {}
break
end
end
end
end
function BundleLoader:OnEntityCreateFromBlueprint(p_HookCtx, p_Blueprint, p_Transform, p_Variation, p_ParentRepresentative)
--1、常规蓝图测试方法
-- 记录每个创建的蓝图(用于调试)
--[[if DEBUG then
print(string.format("Creating blueprint: %s (GUID: %s)", p_Blueprint.name or "Unknown", tostring(p_Blueprint.instanceGuid)))
end]]
--2、精简版蓝图测试方法
if DEBUG then
local guid = tostring(p_Blueprint.instanceGuid)
self.seenBlueprints = self.seenBlueprints or {}
self.blueprintCount = self.blueprintCount or {}
-- 记录每个GUID的出现次数
self.blueprintCount[guid] = (self.blueprintCount[guid] or 0) + 1
-- 记录每个创建的蓝图(用于调试)- 避免重复GUID
if not self.seenBlueprints[guid] then
self.seenBlueprints[guid] = true
print(string.format("Creating blueprint: %s (GUID: %s)", p_Blueprint.name or "Unknown", guid))
end
-- 如果某个蓝图出现次数过多,可能是问题所在
if self.blueprintCount[guid] > 150 then --当蓝图出现次数大于多少次时开始记录
print(string.format("WARNING: Blueprint %s repeated %d times!", guid, self.blueprintCount[guid]))
end
end
--3、蓝图阻止机制
if self.currentLevelConfig.blueprintGuidsToBlock and self.currentLevelConfig.blueprintGuidsToBlock[tostring(p_Blueprint.instanceGuid)] then
if DEBUG then
print(string.format("BLOCKED blueprint: %s (GUID: %s)", p_Blueprint.name or "Unknown", tostring(p_Blueprint.instanceGuid)))
end
p_HookCtx:Return()
end
end
function BundleLoader:GetUIBundleType(p_Bundles)
for _, l_Bundle in ipairs(p_Bundles) do
if l_Bundle:match("UiLoading") then
return UiBundleTypes.Loading
elseif l_Bundle:match("UiPlaying") then
return UiBundleTypes.Playing
elseif l_Bundle:match("UiPreEndOfRound") then
return UiBundleTypes.PreEndOfRound
elseif l_Bundle:match("UiEndOfRound") then
return UiBundleTypes.EndOfRound
end
end
return UiBundleTypes.Unknown
end
local function _ContainsBundle(p_Bundles, p_Bundle)
p_Bundle = p_Bundle:lower()
for _, l_Bundle in ipairs(p_Bundles) do
if l_Bundle:lower() == p_Bundle then
return true
end
end
return false
end
function BundleLoader:AddBundles(p_Bundles, p_BundlesToAdd)
for l_Index, l_Bundle in ipairs(p_BundlesToAdd) do
if _ContainsBundle(p_Bundles, l_Bundle) then
self:debug("Ignoring duplicate bundle '%s'.", l_Bundle)
else
self:debug("Adding bundle %s: %s", l_Index, l_Bundle)
table.insert(p_Bundles, l_Bundle)
end
end
end
function BundleLoader:GetBundles(p_Bundles, p_Compartment)
local s_Bundles = {}
self:debug("Processing bundles for compartment %s", p_Compartment)
-- Load bundles in order of priority: Common -> Level -> Level+GameMode -> GameMode
if self.commonConfig.bundles and self.commonConfig.bundles[p_Compartment] then
self:debug("Loading Common Config Bundles:")
self:AddBundles(s_Bundles, self.commonConfig.bundles[p_Compartment])
end
if self.currentLevelConfig.bundles and self.currentLevelConfig.bundles[p_Compartment] then
self:debug("Loading Current Level Config Bundles:")
self:AddBundles(s_Bundles, self.currentLevelConfig.bundles[p_Compartment])
end
if self.currentLevelGameModeConfig.bundles and self.currentLevelGameModeConfig.bundles[p_Compartment] then
self:debug("Loading Current Level + GameMode Config Bundles:")
self:AddBundles(s_Bundles, self.currentLevelGameModeConfig.bundles[p_Compartment])
end
if self.currentGameModeConfig.bundles and self.currentGameModeConfig.bundles[p_Compartment] then
self:debug("Loading Current GameMode Config Bundles:")
self:AddBundles(s_Bundles, self.currentGameModeConfig.bundles[p_Compartment])
end
-- Handle UI bundles for frontend compartment
if p_Compartment == ResourceCompartment.ResourceCompartment_Frontend then
local s_Type = self:GetUIBundleType(p_Bundles)
-- Load UI bundles in same priority order
if self.commonConfig.uiBundles and self.commonConfig.uiBundles[s_Type] then
self:debug("Loading Common Config UI Bundles:")
self:AddBundles(s_Bundles, self.commonConfig.uiBundles[s_Type])
end
if self.currentLevelConfig.uiBundles and self.currentLevelConfig.uiBundles[s_Type] then
self:debug("Loading Current Level Config UI Bundles:")
self:AddBundles(s_Bundles, self.currentLevelConfig.uiBundles[s_Type])
end
if self.currentLevelGameModeConfig.uiBundles and self.currentLevelGameModeConfig.uiBundles[s_Type] then
self:debug("Loading Current Level + GameMode Config UI Bundles:")
self:AddBundles(s_Bundles, self.currentLevelGameModeConfig.uiBundles[s_Type])
end
if self.currentGameModeConfig.uiBundles and self.currentGameModeConfig.uiBundles[s_Type] then
self:debug("Loading Current GameMode Config UI Bundles:")
self:AddBundles(s_Bundles, self.currentGameModeConfig.uiBundles[s_Type])
end
end
-- Add original game bundles
self:debug("Adding original game bundles:")
self:AddBundles(s_Bundles, p_Bundles)
return s_Bundles
end
function BundleLoader:OnMountSuperBundles()
-- Performance optimization: Mount common SuperBundles only once
if not self.commonSuperBundlesMounted and self.commonConfig.superBundles then
self:debug("Mounting Common SuperBundles for the first time:")
for l_Index, l_SuperBundle in ipairs(self.commonConfig.superBundles) do
self:debug(" %s: %s", l_Index, l_SuperBundle)
ResourceManager:MountSuperBundle(l_SuperBundle)
end
-- Mark as mounted to prevent redundant mounting
self.commonSuperBundlesMounted = true
self:debug("Common SuperBundles mounted and will persist across map changes.")
end
-- Mount level-specific SuperBundles
if self.currentLevelConfig.superBundles then
self:debug("Mounting Level-specific SuperBundles:")
for l_Index, l_SuperBundle in ipairs(self.currentLevelConfig.superBundles) do
self:debug(" %s: %s", l_Index, l_SuperBundle)
ResourceManager:MountSuperBundle(l_SuperBundle)
end
end
-- Mount level+gamemode SuperBundles
if self.currentLevelGameModeConfig.superBundles then
self:debug("Mounting Level+GameMode SuperBundles:")
for l_Index, l_SuperBundle in ipairs(self.currentLevelGameModeConfig.superBundles) do
self:debug(" %s: %s", l_Index, l_SuperBundle)
ResourceManager:MountSuperBundle(l_SuperBundle)
end
end
-- Mount gamemode SuperBundles
if self.currentGameModeConfig.superBundles then
self:debug("Mounting GameMode SuperBundles:")
for l_Index, l_SuperBundle in ipairs(self.currentGameModeConfig.superBundles) do
self:debug(" %s: %s", l_Index, l_SuperBundle)
ResourceManager:MountSuperBundle(l_SuperBundle)
end
end
self:debug("All SuperBundle mounting operations completed.")
end
function BundleLoader:AddRegistries(p_Registries)
for l_Compartment, l_Names in pairs(p_Registries) do
for l_Index, l_Name in ipairs(l_Names) do
self:debug("Processing RegistryContainer %s from '%s' for compartment %s", l_Index, l_Name, l_Compartment)
local s_SubWorldData = ResourceManager:SearchForDataContainer(l_Name)
if not s_SubWorldData then
self:error("Failed to find SubWorldData for '%s'. Registry will be skipped.", l_Name)
goto continue
end
s_SubWorldData = SubWorldData(s_SubWorldData)
if not s_SubWorldData.registryContainer then
self:error("SubWorldData '%s' has no registryContainer. Registry will be skipped.", l_Name)
goto continue
end
ResourceManager:AddRegistry(s_SubWorldData.registryContainer, l_Compartment)
self:debug("Successfully added registry from '%s' to compartment %s", l_Name, l_Compartment)
::continue::
end
end
end
---VEXT Shared Level:RegisterEntityResources Event
---@param p_LevelData DataContainer|LevelData @needs to be upcasted to LevelData
function BundleLoader:OnLevelRegisterEntityResources(p_LevelData)
self:debug("Registering entity resources for current level")
-- Add registries in order of priority
if self.commonConfig.registries then
self:debug("Adding Common registries:")
self:AddRegistries(self.commonConfig.registries)
end
if self.currentLevelConfig.registries then
self:debug("Adding Level-specific registries:")
self:AddRegistries(self.currentLevelConfig.registries)
end
if self.currentLevelGameModeConfig.registries then
self:debug("Adding Level+GameMode registries:")
self:AddRegistries(self.currentLevelGameModeConfig.registries)
end
if self.currentGameModeConfig.registries then
self:debug("Adding GameMode registries:")
self:AddRegistries(self.currentGameModeConfig.registries)
end
self:debug("Entity resource registration completed.")
end
---VEXT Shared ResourceManager:LoadBundles Hook
---@param p_HookCtx HookContext
---@param p_Bundles string[]
---@param p_Compartment ResourceCompartment|integer
function BundleLoader:OnLoadBundles(p_HookCtx, p_Bundles, p_Compartment)
if p_Compartment == ResourceCompartment.ResourceCompartment_Game then
self:debug("Game compartment loading detected, updating configuration and mounting SuperBundles")
self:UpdateConfig()
self:OnMountSuperBundles()
end
-- Pass processed bundles to the game
local s_ProcessedBundles = self:GetBundles(p_Bundles, p_Compartment)
p_HookCtx:Pass(s_ProcessedBundles, p_Compartment)
end
---VEXT Shared VisualTerrain:Load Hook
---VEXT Shared Terrain:Load Hook
---@param p_HookCtx HookContext
---@param p_TerrainName string
function BundleLoader:OnTerrainLoad(p_HookCtx, p_TerrainName)
self:debug("Terrain loading request: %s", p_TerrainName)
-- Handle terrain asset filtering for level+gamemode combinations
if self.currentLevelGameModeConfig.terrainAssetName then
if not string.find(p_TerrainName:lower(), self.currentLevelGameModeConfig.terrainAssetName:lower()) then
self:debug("Blocking terrain '%s' due to level+gamemode configuration", p_TerrainName)
p_HookCtx:Return()
end
return
end
-- Handle terrain asset filtering for levels
if self.currentLevelConfig.terrainAssetName then
if not string.find(p_TerrainName:lower(), self.currentLevelConfig.terrainAssetName:lower()) then
self:debug("Blocking terrain '%s' due to level configuration", p_TerrainName)
p_HookCtx:Return()
end
return
end
self:warn("No terrain filtering configured - all terrains will be loaded")
self:warn("Loading terrain: %s", p_TerrainName)
end
-- Configuration loading functions with error handling
-- Include modifications that should get loaded every time.
function BundleLoader.GetCommonBundleConfig()
local s_Success, s_BundleConfig = pcall(require, "__shared/BundleLoader/BundleConfig/Common")
if not s_Success then
BundleLoader:warn("Failed to load Common bundle configuration: %s", s_BundleConfig)
return {}
end
return s_BundleConfig
end
-- Include level specific modifications. Only get loaded when the level does.
function BundleLoader.GetLevelBundleConfig()
local s_LevelName = SharedUtils:GetLevelName():gsub(".*/", "")
local s_Success, s_BundleConfig = pcall(require, string.format("__shared/BundleLoader/BundleConfig/Levels/%s", s_LevelName))
if not s_Success then
BundleLoader:debug("No level-specific configuration found for '%s'", s_LevelName)
return {}
end
return s_BundleConfig
end
-- Include gamemode specific modifications. Only get loaded when the gamemode does.
function BundleLoader.GetGameModeBundleConfig()
local s_Success, s_BundleConfig = pcall(require, string.format("__shared/BundleLoader/BundleConfig/GameModes/%s", SharedUtils:GetCurrentGameMode()))
if not s_Success then
BundleLoader:debug("No gamemode-specific configuration found for '%s'", SharedUtils:GetCurrentGameMode())
return {}
end
return s_BundleConfig
end
-- Include level & gamemode specific modifications. Only get loaded when the level & gamemode does.
function BundleLoader.GetLevelAndGameModeBundleConfig()
local s_LevelName = SharedUtils:GetLevelName():gsub(".*/", "")
local s_Success, s_BundleConfig = pcall(require, string.format("__shared/BundleLoader/BundleConfig/Levels/%s/%s", s_LevelName, SharedUtils:GetCurrentGameMode()))
if not s_Success then
BundleLoader:debug("No level+gamemode-specific configuration found for '%s/%s'", s_LevelName, SharedUtils:GetCurrentGameMode())
return {}
end
return s_BundleConfig
end
-- Initialize the BundleLoader
BundleLoader = BundleLoader()
return BundleLoader