Files
SVSimServer/SVSim.BattleEngine/Engine/AreaSelectBG.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

428 lines
15 KiB
C#

using System;
using System.Collections;
using System.Collections.Generic;
using System.Linq;
using Cute;
using UnityEngine;
[Serializable]
public class AreaSelectBG
{
private AreaSelectUI _areaSelectUI;
[SerializeField]
private GameObject _BGRoot;
[SerializeField]
private UITexture[] _BGTexture;
[SerializeField]
private AreaBGInfo _areaBGInfo;
private ParticleSystem _bgEffect;
private AreaSelectEffectControlBase _bgEffectControl;
[SerializeField]
private GameObject _BGDragCollision;
private Vector2 _BGDragDelta = Vector2.zero;
private bool _isBGDragEnable;
private float _BGDragSec;
private float _BGDragSecResetUpToTime;
private List<string> _loadedResources = new List<string>();
private bool _isLoadEndBGTexture = true;
private bool _isLoadEndParticle = true;
private StorySectionData _sectionData;
private int? _sectionClassId;
private List<ChapterExtraData> _extraChapters = new List<ChapterExtraData>();
private bool _isNormalBgSet = true;
private float _currentAspectRatio;
private Vector3 _currentBGScale = Vector3.one;
private Vector3 _currentParentPos = Vector3.zero;
private Vector2 _minMovablePos = Vector3.one;
private Vector2 _maxMovablePos = Vector3.one;
public ChapterExtraData ChapterExtraData { get; private set; }
public ChapterExtraData TransitionChapterExtraData { get; private set; }
public int BeforeChapterId { get; private set; }
public GameObject GetBGRoot()
{
return _BGRoot;
}
public bool GetLoadEnd()
{
if (_isLoadEndBGTexture)
{
return _isLoadEndParticle;
}
return false;
}
public void SetActiveBGEffect(bool isActive)
{
_bgEffect.gameObject.SetActive(isActive);
if (_bgEffectControl != null)
{
_bgEffectControl.SetActiveBGEffect(isActive);
}
}
public bool IsBGDragEnable()
{
return _isBGDragEnable;
}
public void Init(AreaSelectUI areaSelectUI)
{
_areaSelectUI = areaSelectUI;
UIEventListener component = _BGDragCollision.GetComponent<UIEventListener>();
if (null != component)
{
component.onDrag = OnDragBG;
}
SetBGDragEnable(enable: false);
}
public void Term()
{
int i = 0;
for (int num = _BGTexture.Length; i < num; i++)
{
_BGTexture[i].mainTexture = null;
}
}
private string GetBackGroundPath(int backGroundId, int index, bool isFetch = false)
{
return Toolbox.ResourcesManager.GetAssetTypePath("bg_story_" + backGroundId.ToString("00") + "_" + (index + 1).ToString("00"), ResourcesManager.AssetLoadPathType.Background, isFetch);
}
private string GetExtraBackGroundPath(int backgroundId, int index, string suffix, bool isFetch = false)
{
return Toolbox.ResourcesManager.GetAssetTypePath("bg_story_" + backgroundId.ToString("00") + "_" + (index + 1).ToString("00") + suffix, ResourcesManager.AssetLoadPathType.Background, isFetch);
}
private string GetMapEffectPath(int backGroundId, bool isFetch = false)
{
return Toolbox.ResourcesManager.GetAssetTypePath("scn_map_world_" + backGroundId, ResourcesManager.AssetLoadPathType.Effect2D, isFetch);
}
private string GetTreeEffectPath(int backGroundId, bool isFetch = false)
{
return Toolbox.ResourcesManager.GetAssetTypePath("scn_map_world_tree_" + backGroundId, ResourcesManager.AssetLoadPathType.Effect2D, isFetch);
}
public void LoadBG(StorySectionData sectionData, int? sectionClassId)
{
_sectionData = sectionData;
_sectionClassId = sectionClassId;
_extraChapters = _areaBGInfo.GetExtraChapters(_sectionData.Id, sectionClassId);
_loadedResources.Add(GetMapEffectPath(sectionData.BackGroundId));
_isLoadEndBGTexture = false;
for (int i = 0; i < _BGTexture.Length; i++)
{
_loadedResources.Add(GetBackGroundPath(sectionData.BackGroundId, i));
}
foreach (ChapterExtraData extraChapter in _extraChapters)
{
int num = sectionData.BackGroundId;
if (extraChapter.IsUseOtherSectionBG())
{
num = extraChapter.BGSectionId;
for (int j = 0; j < _BGTexture.Length; j++)
{
_loadedResources.Add(GetBackGroundPath(num, j));
}
_loadedResources.Add(GetMapEffectPath(num));
if (extraChapter.AddTreeEffect)
{
_loadedResources.Add(GetTreeEffectPath(num));
}
}
foreach (int item in extraChapter.ExtraTextureIndex)
{
_loadedResources.Add(GetExtraBackGroundPath(num, item, extraChapter.BGSuffix));
}
if (!string.IsNullOrEmpty(extraChapter.BGExtraEffectPath))
{
_loadedResources.Add(Toolbox.ResourcesManager.GetAssetTypePath(extraChapter.BGExtraEffectPath, ResourcesManager.AssetLoadPathType.Effect2D));
}
if (!string.IsNullOrEmpty(extraChapter.BGFirstClearEffectPath))
{
_loadedResources.Add(Toolbox.ResourcesManager.GetAssetTypePath(extraChapter.BGFirstClearEffectPath, ResourcesManager.AssetLoadPathType.Effect2D));
}
}
_areaSelectUI.StartCoroutine(Toolbox.ResourcesManager.LoadAssetGroupAsync(_loadedResources, _OnLoadEndBG));
_isLoadEndParticle = false;
}
public void UnLoadBG()
{
Toolbox.ResourcesManager.RemoveAssetGroup(_loadedResources);
_loadedResources.Clear();
_sectionData = null;
_sectionClassId = null;
}
private void _OnLoadEndBG()
{
List<GameObject> list = new List<GameObject>();
_bgEffect = UnityEngine.Object.Instantiate(Toolbox.ResourcesManager.LoadObject(GetMapEffectPath(_sectionData.BackGroundId, isFetch: true)) as GameObject).GetComponent<ParticleSystem>();
Vector3 localScale = _bgEffect.transform.localScale;
_bgEffect.transform.parent = _BGRoot.transform;
_bgEffect.transform.localScale = localScale;
_bgEffect.transform.localPosition = Vector3.zero;
_bgEffectControl = _bgEffect.gameObject.GetComponent<AreaSelectEffectControlBase>();
list.Add(_bgEffect.gameObject);
if (_bgEffectControl != null)
{
_bgEffectControl._backGroundEffects.Add(_bgEffectControl.BASE_EFFECT_INDEX, _bgEffect);
_bgEffectControl._backGroundEffects.Add(_sectionData.BackGroundId, _bgEffect);
}
for (int i = 0; i < _BGTexture.Length; i++)
{
_BGTexture[i].mainTexture = Toolbox.ResourcesManager.LoadObject(GetBackGroundPath(_sectionData.BackGroundId, i, isFetch: true)) as Texture;
}
foreach (ChapterExtraData extraChapter in _extraChapters)
{
Dictionary<int, Texture> bGTexture = extraChapter.BGTexture;
int num = _sectionData.BackGroundId;
if (extraChapter.IsUseOtherSectionBG())
{
num = extraChapter.BGSectionId;
for (int j = 0; j < _BGTexture.Length; j++)
{
bGTexture.Add(j, Toolbox.ResourcesManager.LoadObject(GetBackGroundPath(num, j, isFetch: true)) as Texture);
}
if (num != _sectionData.BackGroundId && !_bgEffectControl._backGroundEffects.ContainsKey(num))
{
ParticleSystem particleSystem = InitParticle(num, extraChapter.AddTreeEffect);
_bgEffectControl._backGroundEffects.Add(num, particleSystem);
list.Add(particleSystem.gameObject);
}
}
foreach (int item in extraChapter.ExtraTextureIndex)
{
if (!bGTexture.ContainsKey(item))
{
bGTexture.Add(item, Toolbox.ResourcesManager.LoadObject(GetExtraBackGroundPath(num, item, extraChapter.BGSuffix, isFetch: true)) as Texture);
}
}
if (!string.IsNullOrEmpty(extraChapter.BGExtraEffectPath))
{
string assetTypePath = Toolbox.ResourcesManager.GetAssetTypePath(extraChapter.BGExtraEffectPath, ResourcesManager.AssetLoadPathType.Effect2D, isfetch: true);
extraChapter.ExtraEffect = UnityEngine.Object.Instantiate(Toolbox.ResourcesManager.LoadObject(assetTypePath) as GameObject);
if (extraChapter.AttachExtraEffectToBgRoot)
{
extraChapter.ExtraEffect.transform.parent = _BGRoot.transform;
extraChapter.ExtraEffect.transform.localPosition = Vector3.zero;
}
else
{
extraChapter.ExtraEffect.transform.parent = _areaSelectUI.transform;
}
extraChapter.ExtraEffect.SetActive(value: false);
// Pre-Phase-5b: GameMgr.GetEffectMgr().SetUIParticleShader queued the ExtraEffect
// for shader-swap and returned load-time resource paths for cleanup. Headless has
// no EffectMgr and no visible ExtraEffect; the resource tracker doesn't fire either.
}
if (!string.IsNullOrEmpty(extraChapter.BGFirstClearEffectPath))
{
string assetTypePath2 = Toolbox.ResourcesManager.GetAssetTypePath(extraChapter.BGFirstClearEffectPath, ResourcesManager.AssetLoadPathType.Effect2D, isfetch: true);
extraChapter.FirstClearEffect = UnityEngine.Object.Instantiate(Toolbox.ResourcesManager.LoadObject(assetTypePath2) as GameObject);
extraChapter.FirstClearEffect.transform.parent = _areaSelectUI.transform;
extraChapter.FirstClearEffect.SetActive(value: false);
// Pre-Phase-5b: same shader-swap pattern for FirstClearEffect. See ExtraEffect above.
}
}
// Pre-Phase-5b: shader-swap on the collected particle systems, then callback flipped the
// `_isLoadEndParticle` flag. Headless has no EffectMgr; set the flag directly here so the
// GetLoadEnd() gate is coherent.
_isLoadEndParticle = true;
_isLoadEndBGTexture = true;
}
private ParticleSystem InitParticle(int bgId, bool addTreeEffect = false)
{
Vector3 vector = default(Vector3);
ParticleSystem component = UnityEngine.Object.Instantiate(Toolbox.ResourcesManager.LoadObject(GetMapEffectPath(bgId, isFetch: true)) as GameObject).GetComponent<ParticleSystem>();
vector = component.transform.localScale;
component.transform.parent = _BGRoot.transform;
component.transform.localScale = vector;
component.transform.localPosition = Vector3.zero;
if (addTreeEffect)
{
GameObject gameObject = UnityEngine.Object.Instantiate(Toolbox.ResourcesManager.LoadObject(GetTreeEffectPath(bgId, isFetch: true)) as GameObject);
gameObject.transform.parent = component.transform;
gameObject.transform.localScale = Vector3.one;
gameObject.transform.localPosition = Vector3.zero;
}
return component;
}
public void OnChangeSelectChapter(StoryChapterData chapterData, bool isFirstClear)
{
TransitionChapterExtraData = null;
if (_extraChapters.Count > 0)
{
int intChapterId = int.Parse(chapterData.ChapterId);
ChapterExtraData = _extraChapters.FirstOrDefault((ChapterExtraData n) => n.ExtraTextureChapter == intChapterId);
}
if (_bgEffectControl != null)
{
_bgEffectControl.OnChangeSelectChapter(this, _sectionData, _sectionClassId, chapterData, isFirstClear);
}
}
public void SetExtraTexture(int chapterId)
{
if (_extraChapters.Count == 0)
{
return;
}
ChapterExtraData chapterExtraData = _extraChapters.FirstOrDefault((ChapterExtraData n) => n.ExtraTextureChapter == chapterId || n.SectionId == 9003);
if (chapterExtraData != null && chapterExtraData.IsChangeBG())
{
int num = _sectionData.BackGroundId;
if (chapterExtraData.IsUseOtherSectionBG())
{
num = chapterExtraData.BGSectionId;
for (int num2 = 0; num2 < _BGTexture.Length; num2++)
{
_BGTexture[num2].mainTexture = Toolbox.ResourcesManager.LoadObject(GetBackGroundPath(num, num2, isFetch: true)) as Texture;
}
}
foreach (int item in chapterExtraData.ExtraTextureIndex)
{
_BGTexture[item].mainTexture = Toolbox.ResourcesManager.LoadObject(GetExtraBackGroundPath(num, item, chapterExtraData.BGSuffix, isFetch: true)) as Texture;
}
_isNormalBgSet = false;
}
else if (!_isNormalBgSet)
{
for (int num3 = 0; num3 < _BGTexture.Length; num3++)
{
_BGTexture[num3].mainTexture = Toolbox.ResourcesManager.LoadObject(GetBackGroundPath(_sectionData.BackGroundId, num3, isFetch: true)) as Texture;
}
_isNormalBgSet = true;
}
}
public List<int> GetChaptersWithDifferentBackgroundFrom(int chapterId)
{
ChapterExtraData chapterExtra = _extraChapters.FirstOrDefault((ChapterExtraData n) => n.ExtraTextureChapter == chapterId);
IEnumerable<ChapterExtraData> source = ((chapterExtra == null || !chapterExtra.IsUseOtherSectionBG()) ? _extraChapters.Where((ChapterExtraData c) => c.IsUseOtherSectionBG() && c.BGSectionId != _sectionData.BackGroundId) : _extraChapters.Where((ChapterExtraData c) => c.BGSectionId != chapterExtra.BGSectionId));
return source.Select((ChapterExtraData s) => s.ExtraTextureChapter).ToList();
}
public IEnumerator SetClearEffect()
{
ChapterExtraData clearChapterExtraData = _extraChapters.FirstOrDefault((ChapterExtraData n) => n.ExtraTextureChapter == BeforeChapterId);
if (clearChapterExtraData != null && clearChapterExtraData.FirstClearEffect != null)
{
yield return new WaitForSeconds(clearChapterExtraData.FirstClearEffectDelayTime);
clearChapterExtraData.FirstClearEffect.SetActive(value: false);
clearChapterExtraData.FirstClearEffect.SetActive(value: true);
}
}
public void SetupEnd()
{
if (_bgEffectControl != null)
{
_bgEffectControl.SetupEnd();
}
}
public void OnDragBG(GameObject obj, Vector2 delta)
{
_BGDragSecResetUpToTime = 0.5f;
_BGDragSec += Time.deltaTime;
_BGDragSec = Mathf.Min(_BGDragSec, 0.25f);
float t = Mathf.Clamp(_BGDragSec / 0.25f, 0f, 1f);
_BGDragDelta = Vector2.Lerp(Vector2.zero, delta, t);
}
public void UpdateBGDrag()
{
if (_BGDragSecResetUpToTime > 0f)
{
_BGDragSecResetUpToTime -= Time.deltaTime;
if (_BGDragSecResetUpToTime < 0f)
{
_BGDragSecResetUpToTime = 0f;
_BGDragSec = 0f;
}
}
if (!Mathf.Approximately(_BGDragDelta.x, 0f) || !Mathf.Approximately(_BGDragDelta.y, 0f))
{
_BGDragDelta.x *= 0.875f;
_BGDragDelta.y *= 0.875f;
Vector3 localPosition = _BGRoot.transform.localPosition;
localPosition.x += _BGDragDelta.x;
localPosition.y += _BGDragDelta.y;
UpdateMovableRange();
localPosition.x = Mathf.Clamp(localPosition.x, _minMovablePos.x, _maxMovablePos.x);
localPosition.y = Mathf.Clamp(localPosition.y, _minMovablePos.y, _maxMovablePos.y);
_BGRoot.transform.localPosition = localPosition;
}
}
public void SetBGDragEnable(bool enable)
{
_isBGDragEnable = enable;
_BGDragCollision.SetActive(enable);
_BGDragDelta = Vector2.zero;
}
private void UpdateMovableRange()
{
float num = (float)Screen.width / (float)Screen.height;
Vector3 localScale = _BGRoot.transform.localScale;
Vector3 localPosition = _BGRoot.transform.parent.transform.localPosition;
if (!(localScale == _currentBGScale) || num != _currentAspectRatio || !(localPosition == _currentParentPos))
{
float num2 = UIManager.GetInstance().UIManagerRoot.manualHeight;
if (1.7777778f > num)
{
num2 *= 1.7777778f / num;
}
float num3 = num2 * num * 0.5f;
float num4 = num2 * 0.5f;
float num5 = 2048f * localScale.x;
float num6 = 2560f * localScale.x;
float num7 = 1536f * localScale.y;
float num8 = 1536f * localScale.y;
_minMovablePos.x = 0f - num6 + num3 + 5f - localPosition.x;
_maxMovablePos.x = num5 - num3 - 5f - localPosition.x;
_minMovablePos.y = 0f - num7 + num4 + 5f - localPosition.y;
_maxMovablePos.y = num8 - num4 - 5f - localPosition.y;
_currentAspectRatio = num;
_currentBGScale = localScale;
_currentParentPos = localPosition;
}
}
}