From dc7e0573694b80457b4b7bdcc76be58ca9e236d0 Mon Sep 17 00:00:00 2001
From: yyl <yyl>
Date: 星期三, 03 十二月 2025 17:58:24 +0800
Subject: [PATCH] 125 战斗 预加载资源 解决新战场出现时会卸载旧战场红队资源的问题

---
 Main/System/Battle/BattleResources/BattleUnloadManager.cs  |   12 ++++
 Main/System/Battle/BattleResources/BattleCacheManager.cs   |  102 ++++++++++++++++++++++++++++++++++
 Main/System/Battle/BattleResources/BattlePreloadManager.cs |   26 +-------
 3 files changed, 117 insertions(+), 23 deletions(-)

diff --git a/Main/System/Battle/BattleResources/BattleCacheManager.cs b/Main/System/Battle/BattleResources/BattleCacheManager.cs
index 04230a8..86f9d04 100644
--- a/Main/System/Battle/BattleResources/BattleCacheManager.cs
+++ b/Main/System/Battle/BattleResources/BattleCacheManager.cs
@@ -39,6 +39,10 @@
     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>
@@ -357,4 +361,102 @@
         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鏃剁湡姝e嵏杞�
+            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}");
+        }
+    }
 }
\ No newline at end of file
diff --git a/Main/System/Battle/BattleResources/BattlePreloadManager.cs b/Main/System/Battle/BattleResources/BattlePreloadManager.cs
index f6885cd..9d69924 100644
--- a/Main/System/Battle/BattleResources/BattlePreloadManager.cs
+++ b/Main/System/Battle/BattleResources/BattlePreloadManager.cs
@@ -31,6 +31,9 @@
         var redTeamInfo = AnalyzeTeamList(redTeamList, true);
         var blueTeamInfo = AnalyzeTeamList(blueTeamList, false);
         
+        // ===== 鏂板锛氭敞鍐屾垬鍦虹孩闃熻祫婧愰渶姹� =====
+        cacheManager.RegisterBattlefieldRedTeam(battleGuid, redTeamInfo.SpineResources, redTeamInfo.AudioResources);
+        
         StartPreload(redTeamInfo, blueTeamInfo, battleGuid, progressCallback, () =>
         {
             isLoading = false;
@@ -161,28 +164,5 @@
             null,   // 鈫� 钃濋槦涓嶉渶瑕佸紩鐢ㄨ拷韪�
             false   // 鈫� 涓嶆槸绾㈤槦
         );
-    }
-    
-    /// <summary>
-    /// 澶勭悊绾㈤槦鍙樻洿锛氭竻绌烘棫鐨勶紝閲嶆柊鍔犺浇鏂扮殑
-    /// </summary>
-    public void HandleRedTeamChange(List<TeamBase> newRedTeamList, Action completeCallback)
-    {
-        if (newRedTeamList == null)
-        {
-            completeCallback?.Invoke();
-            return;
-        }
-        
-        Debug.Log("BattlePreloadManager: Handling red team change");
-        
-        // 1. 鍗歌浇鏃х殑绾㈤槦璧勬簮
-        unloadManager.UnloadRedTeamResources(cacheManager);
-        
-        // 2. 鍒嗘瀽鏂扮孩闃熻祫婧�
-        var newRedInfo = AnalyzeTeamList(newRedTeamList, true);
-        
-        // 3. 棰勫姞杞芥柊绾㈤槦璧勬簮锛堢孩闃熸槸鍏ㄥ眬鐨勶紝浼犵┖瀛楃涓诧級
-        StartPreload(newRedInfo, new TeamResTracker.TeamResourceInfo(), "", null, completeCallback);
     }
 }
\ No newline at end of file
diff --git a/Main/System/Battle/BattleResources/BattleUnloadManager.cs b/Main/System/Battle/BattleResources/BattleUnloadManager.cs
index 1f51951..9645371 100644
--- a/Main/System/Battle/BattleResources/BattleUnloadManager.cs
+++ b/Main/System/Battle/BattleResources/BattleUnloadManager.cs
@@ -88,4 +88,16 @@
         
         Debug.Log("BattleUnloadManager: All resources unloaded");
     }
+
+    /// <summary>
+    /// 鍗歌浇鎴樺満璧勬簮
+    /// </summary>
+    public void UnloadBattleResources(BattleCacheManager cacheManager, string battleGuid)
+    {
+        // 鍗歌浇钃濋槦璧勬簮
+        UnloadBlueTeamResources(cacheManager, battleGuid);
+        
+        // ===== 鏂板锛氭敞閿�鎴樺満绾㈤槦寮曠敤锛堣嚜鍔ㄦ寜寮曠敤璁℃暟鍗歌浇锛�=====
+        cacheManager.UnregisterBattlefieldRedTeam(battleGuid);
+    }
 }
\ No newline at end of file

--
Gitblit v1.8.0