Files
SVSimServer/SVSim.BattleEngine/Engine/Cute/AssetManager.cs
gamer147 2d9a6eea4b engine cleanup passes 4-7 + multi-instancing ambient rip
Squashes 146 commits from battle-engine-extraction. Net: 2,045 files changed,
+11,896 / -158,687 lines. Ships engine passes 4-7 (dead-code cull, view-layer
stub, receive-path shrink) plus the Phase-5 AsyncLocal ambient deletion that
turns concurrent battles into a type-system property rather than a scope
contract.

## What landed

**Passes 4-7 (chunks 1-34):** Extended the Phase-4 const-false collapse into a
cascading cull across the skill graph, view layer, and receive-path periphery.
Six mode flags (IsWatchBattle/IsReplayBattle/IsAdmin/IsAdminWatch/IsPuzzleQuest/
IsAINetwork) became `const false`, every guarded block deleted. Field*.cs
subclass ctors + BackGroundBase + ObjectChecker culled to no-ops. Mulligan
family reworked to take a mgr param through IMulliganMgr.InitMulligan.
Emotion/Recovery/Resource clusters null-stubbed. Prediction/OperationSimulator/
skill filters converted from static ambient reads to per-mgr reads via
SkillPrm.ownerCard.SelfBattlePlayer.BattleMgr / ins.BattleMgr / this.BattleMgr.

**Phase-5 ambient rip (chunks 35-47):** Deleted BattleAmbient / BattleAmbient-
Context / TestBattleScope in full. Every per-battle mutable slot now lives on
the mgr instance itself:
  mgr.InstanceIsForecast / InstanceIsRandomDraw / InstanceRecoveryInfo /
  InstanceViewerId / InstanceNetworkAgent / GameMgr
BattleManagerBase.GetIns() returns null unconditionally; the residual static
flags + 3 façades (Certification.ViewerId, Data.BattleRecoveryInfo,
ToolboxGame.RealTimeNetworkAgent) are null-tolerant defaults kept for the
handful of engine-internal readers that still reference their types. Zero
BattleAmbient references anywhere in engine + node + tests.

Added pre-seeded GameMgr ctor overload threaded through the mgr chain
(BattleManagerBase → SingleBattleMgr / NetworkBattleManagerBase → NetworkStandard-
BattleMgr → HeadlessBattleMgr / HeadlessNetworkBattleMgr). Fixtures build a
GameMgr, seed it via HeadlessEngineEnv.SeedCharaIds/SeedNetUser, and pass it
to the mgr's ctor — no ambient reach.

Node side (SVSim.BattleNode/SessionBattleEngine): _ctx replaced with a plain
GameMgr field; 34 `using var _ambient = BattleAmbient.Enter(_ctx)` scope wraps
ripped from every accessor and mutator; EngineGlobalInit.WirePerSessionGameMgr
takes GameMgr as a param and runs from SessionBattleEngine.SetupInternal
BEFORE mgr construction.

Test side: TestBattleScope deleted; 18 fixture [SetUp]s migrated to
`HeadlessEngineEnv.EnsureProcessGlobals()`; MultiInstanceEngineTests rewritten
around per-mgr construction (GetIns() → null is the pinned invariant).

## Regression fixes

- **chunk-48** (MulliganCtrl): chunk-35's `= null` stubs on card lookups broke
  the live receive-driven Deal path (BattlePlayerBase.DrawCard NRE'd downstream
  of NetworkPlayerMulliganCtrl.StartMulliganVfx). Restored the three lookups
  via `_battlePlayer.BattleMgr.GetBattleCardIdx`. Engine tests were satisfied
  by the WireMulliganPhase seam; unit tests exposed the live-path gap.

## Ship state

- SVSim.BattleEngine.Tests: 56/56 pass, 2 skip
- SVSim.UnitTests: 1554/1554 pass (was 1523/31-fail before chunk 48)
- Solution build: 0 source warnings (40 pre-existing NU1902 MessagePack CVEs
  in SVSim.EmulatedEntrypoint, unrelated)
- Sequential PVP smoke: verified live (two back-to-back battles, no regression
  on cleanup/spinup)
- Concurrent PVP smoke: verified live

Adds tools/engine-port/ClosureAnalyzer/ — the Roslyn transitive-type-closure
analyzer needed to make future cascade cleanup safe (per feedback memory
"Engine cleanup needs closure tool" from the 2026-06-28 pass-3 failure).

Co-Authored-By: Claude Opus 4.7 <noreply@anthropic.com>
2026-07-03 19:18:54 -04:00

375 lines
8.9 KiB
C#

using System;
using System.Collections;
using System.Collections.Generic;
using System.IO;
using System.Security.Cryptography;
using System.Text;
using Sqlite3Plugin;
using UnityEngine;
using Wizard;
namespace Cute;
public class AssetManager : MonoBehaviour, IManager
{
public enum _tag_datamode
{
}
private Dictionary<string, AssetHandle> handleDictionary = new Dictionary<string, AssetHandle>();
private Dictionary<string, AssetBundleObject> objectDictionary = new Dictionary<string, AssetBundleObject>();
private static bool _isCryptAssetFileName = false;
private AsyncJob _asyncJobDownload;
private AsyncJob _asyncJobLoad;
private static string _savePath = null;
private string manifestSavePath = "";
private string bundleSavePath = "";
private string soundSavePath = "";
private string movieSavePath = "";
private string fontSavePath = "";
private int loadingReqCount;
private int loadingCompCount;
private int loadingManifestCompCount;
public HashSet<string> predownloadCategories = new HashSet<string> { "common", "tutorial" };
public HashSet<string> tutorialdownloadCategories = new HashSet<string> { "tutorial" };
public List<string> NoUnloadAssetName = new List<string>();
private float _downloadCompletedSize;
private List<string> _temporaryVoiceNameList = new List<string>();
public string MovieManifesHeadtName = "moviemanifest";
public string SoundManifesHeadtName = "soundmanifest";
private bool _isLocalDatahashStoreRecoveryMode;
private ManifestDatahashKVS _localDatahashStore;
public static bool isCryptAssetFileName => _isCryptAssetFileName;
public bool IsResourceDataBaseError { get; private set; }
public bool IsBackgroundDownload { get; private set; }
public string manifestOfManifests { get; set; }
public string manifestOfManifests_sub { get; set; }
public void CancelDownloadAsyncJob()
{
_asyncJobDownload.Cancel();
}
private ManifestDatahashKVS GetLocalDatahashStore()
{
if (_localDatahashStore == null && !_isLocalDatahashStoreRecoveryMode)
{
string localDatahashStorePath = GetLocalDatahashStorePath();
try
{
_localDatahashStore = new ManifestDatahashKVS(localDatahashStorePath);
}
catch (Exception ex)
{
HandleLocalDatahashException(ex);
}
}
return _localDatahashStore;
}
public string GetLocalDatahashStorePath()
{
return GetAssetSaveRootPath() + "manifest.db";
}
private void HandleLocalDatahashException(Exception ex)
{
if (ex is DatabaseCorruptionException)
{
IsResourceDataBaseError = true;
_isLocalDatahashStoreRecoveryMode = true;
UnloadManifestHashDB();
string localDatahashStorePath = GetLocalDatahashStorePath();
if (File.Exists(localDatahashStorePath))
{
File.Delete(localDatahashStorePath);
}
UIManager instance = UIManager.GetInstance();
if ((bool)instance)
{
instance.CreateConfirmationDialog(Data.SystemText.Get("System_0058")).OnClose = delegate
{
SoftwareReset.setAction();
SoftwareReset.exec();
};
}
else
{
SoftwareResetBase.SoftwareReset(null, null);
}
return;
}
throw ex;
}
public void SaveLocalDatahash(string name, string hash)
{
ManifestDatahashKVS localDatahashStore = GetLocalDatahashStore();
if (localDatahashStore != null)
{
try
{
localDatahashStore.Set(name, hash);
}
catch (Exception ex)
{
HandleLocalDatahashException(ex);
}
}
}
public string GetLocalDatahash(string name)
{
ManifestDatahashKVS localDatahashStore = GetLocalDatahashStore();
if (localDatahashStore != null)
{
try
{
return localDatahashStore.Get(name);
}
catch (Exception ex)
{
HandleLocalDatahashException(ex);
}
}
return "";
}
public void UnloadManifestHashDB()
{
if (_localDatahashStore != null)
{
_localDatahashStore.Dispose();
_localDatahashStore = null;
}
}
public static string GetAssetSaveRootPath()
{
return _savePath;
}
public bool RegistHandle(string key, AssetHandle handle)
{
try
{
handleDictionary.Add(key, handle);
if (handle.directory.StartsWith("v/t", StringComparison.Ordinal))
{
_temporaryVoiceNameList.Add(Path.GetFileNameWithoutExtension(handle.filename));
}
}
catch (Exception ex)
{
AssetHandle assetHandle = GetAssetHandle(key);
if (assetHandle != null && assetHandle.isMultipleHandleIgnorAsset)
{
assetHandle.CopyWithCatchException(handle);
assetHandle.reference--;
return true;
}
Debug.LogError(ex.Message);
return false;
}
return true;
}
public void SetAssetBundle(string filename, AssetBundle assetbundle, bool isMultipleHandleIgnorAsset = false)
{
if (objectDictionary.TryGetValue(filename, out var value))
{
if (!isMultipleHandleIgnorAsset)
{
for (int i = 0; i < value.objectArray.Count; i++)
{
UnityEngine.Object.DestroyImmediate(value.objectArray[i].baseObject, allowDestroyingAssets: true);
}
}
value.objectArray.Clear();
if (value.assetBundle != null)
{
value.assetBundle.Unload(unloadAllLoadedObjects: true);
}
value.assetBundle = assetbundle;
}
else
{
value = new AssetBundleObject();
value.assetBundle = assetbundle;
objectDictionary.Add(filename, value);
}
}
public void SetObjectList(string filename, List<AssetObject> objectList)
{
if (objectDictionary.TryGetValue(filename, out var value))
{
value.objectArray = objectList;
}
}
public bool HasObjectList(string filename)
{
return objectDictionary.ContainsKey(filename);
}
public void UnloadAssetBundle(string assetName)
{
if (objectDictionary.TryGetValue(assetName, out var value))
{
for (int i = 0; i < value.objectArray.Count; i++)
{
UnityEngine.Object.DestroyImmediate(value.objectArray[i].baseObject, allowDestroyingAssets: true);
}
value.objectArray.Clear();
if (value.assetBundle != null)
{
value.assetBundle.Unload(unloadAllLoadedObjects: true);
value.assetBundle = null;
}
objectDictionary.Remove(assetName);
}
}
public void UnloadAsset(string assetName)
{
if (!string.IsNullOrEmpty(assetName) && handleDictionary.TryGetValue(assetName, out var value) && value.assetType != AssetHandle.AssetType.Movie)
{
value.Unload();
}
}
public void CacheAsset(string assetName, Action callback = null)
{
CacheAsset(assetName, new AssetRequestContext(delegate
{
if (callback != null)
{
callback();
}
}));
}
public void CacheAsset(string assetName, AssetRequestContext requestContext)
{
AssetHandle value = null;
if (handleDictionary.TryGetValue(assetName, out value) && !value.useStreamingAsset)
{
value.Load(requestContext);
}
else if (requestContext != null && requestContext.callback != null)
{
requestContext.callback(value);
}
}
public UnityEngine.Object LoadObject(string objectName, Type type, bool isIfFindLoad = false)
{
string value = objectName.ToLower() + ".";
foreach (AssetBundleObject value2 in objectDictionary.Values)
{
int count = value2.objectArray.Count;
for (int i = 0; i < count; i++)
{
AssetObject assetObject = value2.objectArray[i];
if (assetObject.baseObject != null && (assetObject.baseObject.GetType() == type || assetObject.baseObject.GetType().IsSubclassOf(type)) && assetObject.basePath.IndexOf(value, StringComparison.Ordinal) != -1)
{
return assetObject.baseObject;
}
}
}
return null;
}
public void AddDownloadJob(IEnumerator enumerator, Action cancelAction)
{
_asyncJobDownload.Add(enumerator, cancelAction);
}
public void AddLoadJob(IEnumerator enumerator, Action cancelAction)
{
_asyncJobLoad.Add(enumerator, cancelAction);
}
public AssetHandle GetAssetHandle(string assetName, bool isWarning = true)
{
AssetHandle value = null;
handleDictionary.TryGetValue(assetName, out value);
return value;
}
public string getAssetSavePath(AssetHandle.AssetType _assetType)
{
switch (_assetType)
{
case AssetHandle.AssetType.Manifests:
return manifestSavePath;
case AssetHandle.AssetType.AssetBundle:
return bundleSavePath;
case AssetHandle.AssetType.Sound:
case AssetHandle.AssetType.TemporarySound:
return soundSavePath;
case AssetHandle.AssetType.Movie:
return movieSavePath;
case AssetHandle.AssetType.Font:
return fontSavePath;
default:
return bundleSavePath;
}
}
public void AddManifestCount()
{
loadingManifestCompCount++;
}
public void AddDownloadCompletedSize(float size)
{
_downloadCompletedSize += size;
}
public void AddLoadingMaxCount(int cnt = -1)
{
if (cnt == -1)
{
loadingReqCount++;
}
else
{
loadingReqCount += cnt;
}
}
public void AddLoadingCurrentCount(string strFileName)
{
loadingCompCount++;
}
}