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
using UnityEngine;
using System;
using System.Collections.Generic;
 
public class BattlePreloadManager
{
    private BattleSpineResLoader spineLoader = new BattleSpineResLoader();
    private BattleAudioResLoader audioLoader = new BattleAudioResLoader();
    private BattleCacheManager cacheManager = new BattleCacheManager();
    private BattleUnloadManager unloadManager = new BattleUnloadManager();
    
    private bool isLoading = false;
    
    public BattleCacheManager CacheManager => cacheManager;
    public BattleUnloadManager UnloadManager => unloadManager;
    
    /// <summary>
    /// 预加载战斗资源
    /// </summary>
    public void PreloadBattleResources(string battleGuid, List<TeamBase> redTeamList, List<TeamBase> blueTeamList, 
        Action<float> progressCallback, Action completeCallback)
    {
        if (isLoading)
        {
            Debug.LogWarning("BattlePreloadManager: Already loading, ignoring request");
            return;
        }
        
        isLoading = true;
        
        var redTeamInfo = AnalyzeTeamList(redTeamList, true);
        var blueTeamInfo = AnalyzeTeamList(blueTeamList, false);
        
        // ===== 新增:注册战场红队资源需求 =====
        cacheManager.RegisterBattlefieldRedTeam(battleGuid, redTeamInfo.SpineResources, redTeamInfo.AudioResources);
        
        StartPreload(redTeamInfo, blueTeamInfo, battleGuid, progressCallback, () =>
        {
            isLoading = false;
            completeCallback?.Invoke();
        });
    }
    
    private TeamResTracker.TeamResourceInfo AnalyzeTeamList(List<TeamBase> teamList, bool isPersistent)
    {
        var combinedInfo = new TeamResTracker.TeamResourceInfo();
        
        if (teamList == null || teamList.Count == 0)
        {
            return combinedInfo;
        }
        
        foreach (var team in teamList)
        {
            if (team == null)
                continue;
                
            var teamInfo = TeamResTracker.AnalyzeTeam(team, isPersistent);
            MergeResourceInfo(combinedInfo, teamInfo);
        }
        
        return combinedInfo;
    }
    
    private void MergeResourceInfo(TeamResTracker.TeamResourceInfo target, TeamResTracker.TeamResourceInfo source)
    {
        // 合并Spine资源(去重)
        foreach (var res in source.SpineResources)
        {
            if (!ContainsResource(target.SpineResources, res))
            {
                target.SpineResources.Add(res);
            }
        }
        
        // 合并音频资源(去重)
        foreach (var res in source.AudioResources)
        {
            if (!ContainsResource(target.AudioResources, res))
            {
                target.AudioResources.Add(res);
            }
        }
    }
    
    private bool ContainsResource(List<BattleResCache.ResourceIdentifier> list, BattleResCache.ResourceIdentifier resource)
    {
        foreach (var item in list)
        {
            if (item.GetKey() == resource.GetKey())
            {
                return true;
            }
        }
        return false;
    }
    
    private void StartPreload(TeamResTracker.TeamResourceInfo redInfo, TeamResTracker.TeamResourceInfo blueInfo,
        string battleGuid,
        Action<float> progressCallback, Action completeCallback)
    {
        int totalResources = redInfo.GetTotalCount() + blueInfo.GetTotalCount();
        
        if (totalResources == 0)
        {
            Debug.Log("BattlePreloadManager: No resources to preload");
            completeCallback?.Invoke();
            return;
        }
        
        Debug.Log($"BattlePreloadManager: Preloading {totalResources} resources for battlefield {battleGuid}");
        Debug.Log($"  Red: Spine={redInfo.SpineResources.Count}, Audio={redInfo.AudioResources.Count}");
        Debug.Log($"  Blue: Spine={blueInfo.SpineResources.Count}, Audio={blueInfo.AudioResources.Count}");
        
        int completedPhases = 0;
        int totalPhases = 4;
        
        Action onPhaseComplete = () =>
        {
            completedPhases++;
            float progress = (float)completedPhases / totalPhases;
            progressCallback?.Invoke(progress);
            
            if (completedPhases >= totalPhases)
            {
                Debug.Log($"BattlePreloadManager: Completed! {cacheManager.GetCacheStats(battleGuid)}");
                completeCallback?.Invoke();
            }
        };
        
        // 并行加载4个阶段(传入cacheManager和是否为红队标识)
        spineLoader.LoadSpineResourcesAsync(
            redInfo.SpineResources, 
            cacheManager.GetSpineCache(true, battleGuid), 
            null, 
            onPhaseComplete,
            cacheManager,  // ← 传入管理器
            true           // ← 是红队
        );
        
        audioLoader.LoadAudioResourcesAsync(
            redInfo.AudioResources, 
            cacheManager.GetAudioCache(true, battleGuid), 
            null, 
            onPhaseComplete,
            cacheManager,  // ← 传入管理器
            true           // ← 是红队
        );
        
        spineLoader.LoadSpineResourcesAsync(
            blueInfo.SpineResources, 
            cacheManager.GetSpineCache(false, battleGuid), 
            null, 
            onPhaseComplete,
            null,   // ← 蓝队不需要引用追踪
            false   // ← 不是红队
        );
        
        audioLoader.LoadAudioResourcesAsync(
            blueInfo.AudioResources, 
            cacheManager.GetAudioCache(false, battleGuid), 
            null, 
            onPhaseComplete,
            null,   // ← 蓝队不需要引用追踪
            false   // ← 不是红队
        );
    }
}