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
using UnityEngine;
using System.Collections.Generic;
using Spine.Unity;
 
public class BattleCacheManager
{
    /// <summary>
    /// 资源引用信息
    /// </summary>
    private class ResourceReference
    {
        public BattleResCache.CachedResource CachedResource;
        public HashSet<string> OwnerIds = new HashSet<string>(); // 使用该资源的角色ID集合
        
        public int RefCount => OwnerIds.Count;
        
        public void AddOwner(string ownerId)
        {
            OwnerIds.Add(ownerId);
        }
        
        public void RemoveOwner(string ownerId)
        {
            OwnerIds.Remove(ownerId);
        }
    }
    
    // ===== 红队资源:全局共享,按引用计数管理 =====
    private static Dictionary<string, ResourceReference> globalRedTeamSpineCache = 
        new Dictionary<string, ResourceReference>();
    
    private static Dictionary<string, ResourceReference> globalRedTeamAudioCache = 
        new Dictionary<string, ResourceReference>();
    
    // ===== 蓝队资源:按战场GUID隔离 =====
    private static Dictionary<string, Dictionary<string, BattleResCache.CachedResource>> blueTeamSpineCacheDict = 
        new Dictionary<string, Dictionary<string, BattleResCache.CachedResource>>();
    
    private static Dictionary<string, Dictionary<string, BattleResCache.CachedResource>> blueTeamAudioCacheDict = 
        new Dictionary<string, Dictionary<string, BattleResCache.CachedResource>>();
    
    // 需要添加的字段
    private static Dictionary<string, HashSet<string>> battlefieldRedTeamOwners = 
        new Dictionary<string, HashSet<string>>();  // <battleGuid, ownerIds>
    
    /// <summary>
    /// 获取Spine缓存(红队全局,蓝队按战场隔离)
    /// </summary>
    public Dictionary<string, BattleResCache.CachedResource> GetSpineCache(bool isPersistent, string battleGuid = "")
    {
        if (isPersistent)
        {
            // 红队:将引用字典转换为普通缓存字典(兼容加载器)
            var cache = new Dictionary<string, BattleResCache.CachedResource>();
            foreach (var kvp in globalRedTeamSpineCache)
            {
                cache[kvp.Key] = kvp.Value.CachedResource;
            }
            return cache;
        }
        else
        {
            // 蓝队:返回战场专属缓存
            if (!blueTeamSpineCacheDict.ContainsKey(battleGuid))
            {
                blueTeamSpineCacheDict[battleGuid] = new Dictionary<string, BattleResCache.CachedResource>();
            }
            return blueTeamSpineCacheDict[battleGuid];
        }
    }
    
    /// <summary>
    /// 获取音频缓存(红队全局,蓝队按战场隔离)
    /// </summary>
    public Dictionary<string, BattleResCache.CachedResource> GetAudioCache(bool isPersistent, string battleGuid = "")
    {
        if (isPersistent)
        {
            // 红队:将引用字典转换为普通缓存字典
            var cache = new Dictionary<string, BattleResCache.CachedResource>();
            foreach (var kvp in globalRedTeamAudioCache)
            {
                cache[kvp.Key] = kvp.Value.CachedResource;
            }
            return cache;
        }
        else
        {
            // 蓝队:返回战场专属缓存
            if (!blueTeamAudioCacheDict.ContainsKey(battleGuid))
            {
                blueTeamAudioCacheDict[battleGuid] = new Dictionary<string, BattleResCache.CachedResource>();
            }
            return blueTeamAudioCacheDict[battleGuid];
        }
    }
    
    /// <summary>
    /// 添加红队资源引用(由加载器调用)
    /// </summary>
    public void AddRedTeamSpineReference(string key, BattleResCache.CachedResource resource, string ownerId)
    {
        if (!globalRedTeamSpineCache.ContainsKey(key))
        {
            globalRedTeamSpineCache[key] = new ResourceReference
            {
                CachedResource = resource
            };
        }
        globalRedTeamSpineCache[key].AddOwner(ownerId);
    }
    
    /// <summary>
    /// 添加红队音频引用
    /// </summary>
    public void AddRedTeamAudioReference(string key, BattleResCache.CachedResource resource, string ownerId)
    {
        if (!globalRedTeamAudioCache.ContainsKey(key))
        {
            globalRedTeamAudioCache[key] = new ResourceReference
            {
                CachedResource = resource
            };
        }
        globalRedTeamAudioCache[key].AddOwner(ownerId);
    }
    
    /// <summary>
    /// 获取Spine资源(未命中时自动加载并缓存)
    /// </summary>
    public SkeletonDataAsset GetSpineResource(string directory, string assetName, string battleGuid = "", bool autoLoadIfMissing = true)
    {
        string key = $"{directory}/{assetName}";
        
        // 优先从红队全局缓存查找
        if (globalRedTeamSpineCache.TryGetValue(key, out var redRef))
        {
            return redRef.CachedResource.Asset as SkeletonDataAsset;
        }
        
        // 再从蓝队战场专属缓存查找
        if (!string.IsNullOrEmpty(battleGuid) && blueTeamSpineCacheDict.TryGetValue(battleGuid, out var blueCache))
        {
            if (blueCache.TryGetValue(key, out var blueRes))
            {
                return blueRes.Asset as SkeletonDataAsset;
            }
        }
        
        // ===== 缓存未命中时自动加载 =====
        if (autoLoadIfMissing)
        {
            Debug.LogWarning($"BattleCacheManager: Spine cache miss for {key}, loading on-demand...");
            
            SkeletonDataAsset asset = ResManager.Instance.LoadAsset<SkeletonDataAsset>(directory, assetName);
            
            if (asset != null)
            {
                var identifier = new BattleResCache.ResourceIdentifier
                {
                    Directory = directory,
                    AssetName = assetName,
                    Type = BattleResCache.ResourceType.Spine,
                    IsPersistent = string.IsNullOrEmpty(battleGuid)
                };
                
                var cachedRes = new BattleResCache.CachedResource(identifier, asset, identifier.IsPersistent);
                
                if (string.IsNullOrEmpty(battleGuid))
                {
                    // 红队:添加引用(未知所有者,用特殊标识)
                    AddRedTeamSpineReference(key, cachedRes, "OnDemand");
                    Debug.Log($"BattleCacheManager: Added to global red cache: {key}");
                }
                else
                {
                    // 蓝队:直接加入战场缓存
                    if (!blueTeamSpineCacheDict.ContainsKey(battleGuid))
                    {
                        blueTeamSpineCacheDict[battleGuid] = new Dictionary<string, BattleResCache.CachedResource>();
                    }
                    blueTeamSpineCacheDict[battleGuid][key] = cachedRes;
                    Debug.Log($"BattleCacheManager: Added to blue cache (BF={battleGuid}): {key}");
                }
                
                return asset;
            }
        }
        
        return null;
    }
    
    /// <summary>
    /// 获取音频资源(未命中时自动加载并缓存)
    /// </summary>
    public AudioClip GetAudioResource(string directory, string assetName, string battleGuid = "", bool autoLoadIfMissing = true)
    {
        string key = $"{directory}/{assetName}";
        
        // 优先从红队全局缓存查找
        if (globalRedTeamAudioCache.TryGetValue(key, out var redRef))
        {
            return redRef.CachedResource.Asset as AudioClip;
        }
        
        // 再从蓝队战场专属缓存查找
        if (!string.IsNullOrEmpty(battleGuid) && blueTeamAudioCacheDict.TryGetValue(battleGuid, out var blueCache))
        {
            if (blueCache.TryGetValue(key, out var blueRes))
            {
                return blueRes.Asset as AudioClip;
            }
        }
        
        // ===== 缓存未命中时自动加载 =====
        if (autoLoadIfMissing)
        {
            Debug.LogWarning($"BattleCacheManager: Audio cache miss for {key}, loading on-demand...");
            
            AudioClip asset = ResManager.Instance.LoadAsset<AudioClip>(directory, assetName, false);
            
            if (asset != null)
            {
                var identifier = new BattleResCache.ResourceIdentifier
                {
                    Directory = directory,
                    AssetName = assetName,
                    Type = BattleResCache.ResourceType.Audio,
                    IsPersistent = string.IsNullOrEmpty(battleGuid)
                };
                
                var cachedRes = new BattleResCache.CachedResource(identifier, asset, identifier.IsPersistent);
                
                if (string.IsNullOrEmpty(battleGuid))
                {
                    // 红队:添加引用
                    AddRedTeamAudioReference(key, cachedRes, "OnDemand");
                    Debug.Log($"BattleCacheManager: Added to global red audio cache: {key}");
                }
                else
                {
                    // 蓝队:直接加入战场缓存
                    if (!blueTeamAudioCacheDict.ContainsKey(battleGuid))
                    {
                        blueTeamAudioCacheDict[battleGuid] = new Dictionary<string, BattleResCache.CachedResource>();
                    }
                    blueTeamAudioCacheDict[battleGuid][key] = cachedRes;
                    Debug.Log($"BattleCacheManager: Added to blue audio cache (BF={battleGuid}): {key}");
                }
                
                return asset;
            }
        }
        
        return null;
    }
    
    /// <summary>
    /// 移除指定角色的红队资源引用
    /// </summary>
    public void RemoveRedTeamReferences(List<string> ownerIds)
    {
        if (ownerIds == null || ownerIds.Count == 0)
            return;
        
        int removedSpineCount = 0;
        int removedAudioCount = 0;
        
        // 处理Spine资源
        var spineKeysToRemove = new List<string>();
        foreach (var kvp in globalRedTeamSpineCache)
        {
            foreach (var ownerId in ownerIds)
            {
                kvp.Value.RemoveOwner(ownerId);
            }
            
            // 如果没有引用了,标记删除
            if (kvp.Value.RefCount == 0)
            {
                spineKeysToRemove.Add(kvp.Key);
            }
        }
        
        foreach (var key in spineKeysToRemove)
        {
            var resource = globalRedTeamSpineCache[key].CachedResource;
            ResManager.Instance.UnloadAsset(
                resource.Identifier.Directory.ToLower(), 
                resource.Identifier.AssetName.ToLower()
            );
            globalRedTeamSpineCache.Remove(key);
            removedSpineCount++;
        }
        
        // 处理音频资源
        var audioKeysToRemove = new List<string>();
        foreach (var kvp in globalRedTeamAudioCache)
        {
            foreach (var ownerId in ownerIds)
            {
                kvp.Value.RemoveOwner(ownerId);
            }
            
            if (kvp.Value.RefCount == 0)
            {
                audioKeysToRemove.Add(kvp.Key);
            }
        }
        
        foreach (var key in audioKeysToRemove)
        {
            var resource = globalRedTeamAudioCache[key].CachedResource;
            ResManager.Instance.UnloadAsset(
                resource.Identifier.Directory.ToLower(), 
                resource.Identifier.AssetName.ToLower()
            );
            globalRedTeamAudioCache.Remove(key);
            removedAudioCount++;
        }
        
        Debug.Log($"BattleCacheManager: Removed {ownerIds.Count} owner(s), freed {removedSpineCount} spine + {removedAudioCount} audio resources");
    }
    
    /// <summary>
    /// 清空指定战场的蓝队缓存
    /// </summary>
    public void ClearBlueTeamCache(string battleGuid)
    {
        if (blueTeamSpineCacheDict.ContainsKey(battleGuid))
        {
            blueTeamSpineCacheDict.Remove(battleGuid);
        }
        
        if (blueTeamAudioCacheDict.ContainsKey(battleGuid))
        {
            blueTeamAudioCacheDict.Remove(battleGuid);
        }
        
        Debug.Log($"BattleCacheManager: Cleared blue team cache for battlefield {battleGuid}");
    }
    
    /// <summary>
    /// 清空红队全局缓存(玩家重置阵容时调用)
    /// </summary>
    public void ClearRedTeamCache()
    {
        globalRedTeamSpineCache.Clear();
        globalRedTeamAudioCache.Clear();
        Debug.Log("BattleCacheManager: Cleared red team global cache");
    }
    
    /// <summary>
    /// 获取缓存统计信息
    /// </summary>
    public string GetCacheStats(string battleGuid = "")
    {
        int blueSpineCount = blueTeamSpineCacheDict.ContainsKey(battleGuid) ? blueTeamSpineCacheDict[battleGuid].Count : 0;
        int blueAudioCount = blueTeamAudioCacheDict.ContainsKey(battleGuid) ? blueTeamAudioCacheDict[battleGuid].Count : 0;
        
        return $"Red Spine: {globalRedTeamSpineCache.Count}, Red Audio: {globalRedTeamAudioCache.Count}, " +
               $"Blue Spine (BF={battleGuid}): {blueSpineCount}, Blue Audio (BF={battleGuid}): {blueAudioCount}";
    }
    
    // ========== BattleCacheManager.cs 新增方法 ==========
 
    /// <summary>
    /// 记录战场的红队资源需求(增加引用)
    /// </summary>
    public void RegisterBattlefieldRedTeam(string battleGuid, List<BattleResCache.ResourceIdentifier> spineResources, List<BattleResCache.ResourceIdentifier> audioResources)
    {
        // 记录这个战场使用的红队资源的OwnerIds
        var ownerIds = new HashSet<string>();
        
        foreach (var res in spineResources)
        {
            if (!string.IsNullOrEmpty(res.OwnerId))
            {
                ownerIds.Add(res.OwnerId);
            }
        }
        
        foreach (var res in audioResources)
        {
            if (!string.IsNullOrEmpty(res.OwnerId))
            {
                ownerIds.Add(res.OwnerId);
            }
        }
        
        if (!battlefieldRedTeamOwners.ContainsKey(battleGuid))
        {
            battlefieldRedTeamOwners[battleGuid] = ownerIds;
        }
    }
 
    /// <summary>
    /// 注销战场的红队资源需求(减少引用)
    /// </summary>
    public void UnregisterBattlefieldRedTeam(string battleGuid)
    {
        if (!battlefieldRedTeamOwners.ContainsKey(battleGuid))
            return;
            
        var ownerIds = battlefieldRedTeamOwners[battleGuid];
        
        // 从所有红队资源中移除这些OwnerIds的引用
        RemoveOwnersFromRedTeamCache(ownerIds);
        
        battlefieldRedTeamOwners.Remove(battleGuid);
    }
 
    private void RemoveOwnersFromRedTeamCache(HashSet<string> ownerIds)
    {
        // 处理Spine资源
        var spineKeysToRemove = new List<string>();
        foreach (var kvp in globalRedTeamSpineCache)
        {
            foreach (var ownerId in ownerIds)
            {
                kvp.Value.RemoveOwner(ownerId);
            }
            
            // 引用计数为0时真正卸载
            if (kvp.Value.RefCount == 0)
            {
                spineKeysToRemove.Add(kvp.Key);
            }
        }
        
        foreach (var key in spineKeysToRemove)
        {
            var res = globalRedTeamSpineCache[key];
            ResManager.Instance.UnloadAsset(res.CachedResource.Identifier.Directory, res.CachedResource.Identifier.AssetName);
            globalRedTeamSpineCache.Remove(key);
            Debug.Log($"BattleCacheManager: Unloaded red team spine (refCount=0): {key}");
        }
        
        // 处理Audio资源
        var audioKeysToRemove = new List<string>();
        foreach (var kvp in globalRedTeamAudioCache)
        {
            foreach (var ownerId in ownerIds)
            {
                kvp.Value.RemoveOwner(ownerId);
            }
            
            if (kvp.Value.RefCount == 0)
            {
                audioKeysToRemove.Add(kvp.Key);
            }
        }
        
        foreach (var key in audioKeysToRemove)
        {
            var res = globalRedTeamAudioCache[key];
            ResManager.Instance.UnloadAsset(res.CachedResource.Identifier.Directory, res.CachedResource.Identifier.AssetName);
            globalRedTeamAudioCache.Remove(key);
            Debug.Log($"BattleCacheManager: Unloaded red team audio (refCount=0): {key}");
        }
    }
}