Files
SVSimServer/SVSim.BattleEngine/Engine/Wizard.DeckCardEdit/DeckSave.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

279 lines
8.0 KiB
C#

using System;
using System.Collections.Generic;
using System.Linq;
using Cute;
using UnityEngine;
using Wizard.ErrorDialog;
namespace Wizard.DeckCardEdit;
public class DeckSave
{
public class DeckSaveCoroutineMonoBehaviour : MonoBehaviour
{
private DeckSaveCoroutineMonoBehaviour()
{
}
}
public class Option
{
public int[] CardIds;
public int DeckId;
public string DeckName;
public int ClassType;
public int SubClassType;
public int RawSkinId;
public bool IsRandomLeaderSkin;
public List<int> LeaderSkinIdList;
public long SleeveId;
public bool IsNew;
public Format Format;
public string MyRotationId;
public ConventionDeckList ConventionDeckList;
public Action OnFinish;
public Action<bool> OnSaveCompleteDialog;
}
private Action _onFinish;
private int[] _deck;
private int _id;
private string _name;
private string _defaultName;
private int _class;
private int _rawSkinId;
private bool _isRandomLeaderSkin;
private List<int> _leaderSkinIdList;
private long _sleeveId;
private Format _format;
private ConventionDeckList _conventionDeckList;
private bool _isNew;
private string _myRotationId;
private int _subClass;
private MonoBehaviour _behaviour;
private DialogBase _nameEditDialog;
private IFormatBehavior _formatBehavior;
private Action<bool> _saveCompleteDialogOverrideAction;
private bool _isUsableDeck;
public void Destroy()
{
if ((bool)_behaviour)
{
UnityEngine.Object.Destroy(_behaviour.gameObject);
}
}
public void Start(Option option)
{
IFormatBehavior formatBehavior = FormatBehaviorManager.Create(option.Format, option.ConventionDeckList);
_deck = option.CardIds;
_name = option.DeckName;
_defaultName = option.DeckName;
_id = option.DeckId;
_format = option.Format;
_isNew = option.IsNew;
_class = option.ClassType;
_subClass = option.SubClassType;
_rawSkinId = option.RawSkinId;
_isRandomLeaderSkin = option.IsRandomLeaderSkin;
_leaderSkinIdList = option.LeaderSkinIdList;
_sleeveId = option.SleeveId;
_conventionDeckList = option.ConventionDeckList;
_myRotationId = option.MyRotationId;
_onFinish = option.OnFinish;
_saveCompleteDialogOverrideAction = option.OnSaveCompleteDialog;
MyRotationInfo myRotationInfo = null;
if (!string.IsNullOrEmpty(_myRotationId))
{
myRotationInfo = Data.MyRotationAllInfo.Get(_myRotationId);
}
_formatBehavior = formatBehavior;
if (_deck.Length < 40)
{
CreateSaveConfirmDialog(Data.SystemText.Get("Card_0050", 40.ToString()));
return;
}
if (_deck.Length > 50)
{
DialogBase dialogBase = UIManager.GetInstance().CreateDialogClose();
dialogBase.SetTitleLabel(Data.SystemText.Get("Dia_DeckEdit_006_Title"));
dialogBase.SetText(Data.SystemText.Get("Card_0107", 40.ToString()));
dialogBase.SetButtonLayout(DialogBase.ButtonLayout.OkBtn);
dialogBase.OnCloseStart = Destroy;
return;
}
if (_deck.Length > 40)
{
CreateSaveConfirmDialog(Data.SystemText.Get("Card_0138", 41.ToString()));
return;
}
if (_format == Format.Crossover)
{
CardMaster cardMaster = CardMaster.GetInstance(formatBehavior.CardMasterId);
List<int> source = _deck.ToList();
CardBasePrm.ClanType mainClass = (CardBasePrm.ClanType)_class;
if (source.Count((int cardId) => cardMaster.GetCardParameterFromId(cardId).Clan == mainClass) < 24)
{
CreateSaveConfirmDialog(Data.SystemText.Get("Card_0279", 24.ToString()));
return;
}
CardBasePrm.ClanType subClass = (CardBasePrm.ClanType)_subClass;
if (source.Count((int cardId) => cardMaster.GetCardParameterFromId(cardId).Clan == subClass) < 9)
{
CreateSaveConfirmDialog(Data.SystemText.Get("Card_0280", 9.ToString()));
return;
}
}
if (DeckCardEditUI.IsNotAvailableCardInIds(new List<int>(_deck), _format, formatBehavior, myRotationInfo))
{
CreateSaveConfirmDialog(Data.SystemText.Get("Card_0191"));
return;
}
if (DeckData.ContainsNonPossessionCard(_deck, formatBehavior))
{
CreateSaveConfirmDialog(Data.SystemText.Get("Card_0252"));
return;
}
_isUsableDeck = true;
CheckNewDeckBeforeSave();
}
private void CreateSaveConfirmDialog(string messageText)
{
DialogBase dialogBase = UIManager.GetInstance().CreateDialogClose();
dialogBase.SetTitleLabel(Data.SystemText.Get("Dia_DeckEdit_005_Title"));
dialogBase.SetText(messageText);
dialogBase.SetButtonLayout(DialogBase.ButtonLayout.BlueBtn_CancelBtn);
dialogBase.SetButtonText(Data.SystemText.Get("Dia_DeckEdit_005_Button"), Data.SystemText.Get("Dia_DeckEdit_005_Button_2"));
dialogBase.onPushButton1 = CheckNewDeckBeforeSave;
}
private void CheckNewDeckBeforeSave()
{
if (_isNew)
{
_nameEditDialog = InputDialog.Create(30, 24);
_nameEditDialog.InputAreaObjs.labels[0].text = _name;
_nameEditDialog.InputAreaObjs.labels[1].text = _id.ToString();
_nameEditDialog.InputAreaObjs.labels[2].text = Data.SystemText.Get("Card_0012");
_nameEditDialog.InputAreaObjs.labels[3].text = Data.SystemText.Get("Common_0401", 24.ToString());
_nameEditDialog.SetTitleLabel(Data.SystemText.Get("Card_0011"));
_nameEditDialog.onPushButton1 = NameSetEnd;
_nameEditDialog.onCloseWithoutSelect = Destroy;
_nameEditDialog.SetPanelDepth(2000);
}
else
{
SaveRequest();
}
}
private void NameSetEnd()
{
_name = _nameEditDialog.InputAreaObjs.labels[0].text;
if (string.IsNullOrEmpty(_name))
{
_name = _defaultName;
}
SaveRequest();
}
private void SaveRequest()
{
if (_deck.Length > 50)
{
return;
}
GameObject gameObject = new GameObject();
_behaviour = gameObject.AddComponent<DeckSaveCoroutineMonoBehaviour>();
if (_formatBehavior.IsConventionMode)
{
DeckConventionUpdateTask deckConventionUpdateTask = new DeckConventionUpdateTask();
if (_formatBehavior.UseSubClass)
{
deckConventionUpdateTask.SetParameterWithSubClass(_id, _class, _subClass, _rawSkinId, _isRandomLeaderSkin, _leaderSkinIdList.ToArray(), _sleeveId, _name, is_delete: false, _deck, _conventionDeckList);
}
else
{
deckConventionUpdateTask.SetParameter(_id, _class, _rawSkinId, _isRandomLeaderSkin, _leaderSkinIdList.ToArray(), _sleeveId, _name, is_delete: false, _deck, _myRotationId, _conventionDeckList);
}
deckConventionUpdateTask.SkipAllCuteResultCodeCheckErrorPopup();
_behaviour.StartCoroutine(Toolbox.NetworkManager.Connect(deckConventionUpdateTask, OnFinishSaveRequest, null, OnFailedSaveRequest));
}
else
{
DeckUpdateTask deckUpdateTask = null; // Pre-Phase-5b: no wire task headless
if (_formatBehavior.UseSubClass)
{
deckUpdateTask.SetParameterWithSubClass(_id, _class, _subClass, _rawSkinId, _isRandomLeaderSkin, _leaderSkinIdList.ToArray(), _sleeveId, _name, isDelete: false, _deck, _format);
}
else
{
deckUpdateTask.SetParameter(_id, _class, _rawSkinId, _isRandomLeaderSkin, _leaderSkinIdList.ToArray(), _sleeveId, _name, is_delete: false, _deck, _format, _myRotationId);
}
deckUpdateTask.SkipAllCuteResultCodeCheckErrorPopup();
_behaviour.StartCoroutine(Toolbox.NetworkManager.Connect(deckUpdateTask, OnFinishSaveRequest, null, OnFailedSaveRequest));
}
}
private void OnFinishSaveRequest(NetworkTask.ResultCode error)
{
if (_saveCompleteDialogOverrideAction != null)
{
_saveCompleteDialogOverrideAction(_isUsableDeck);
}
else
{
DialogBase dialogBase = UIManager.GetInstance().CreateDialogClose();
dialogBase.SetTitleLabel(Data.SystemText.Get("Dia_DeckEdit_007_Title"));
dialogBase.SetText(Data.SystemText.Get("Card_0019"));
dialogBase.SetButtonLayout(DialogBase.ButtonLayout.OkBtn);
dialogBase.OnCloseStart = _onFinish;
}
Destroy();
}
private void OnFailedSaveRequest(int code)
{
DialogBase dialogBase = Wizard.ErrorDialog.Dialog.Create(code);
if (_isNew)
{
dialogBase.OnCloseStart = CheckNewDeckBeforeSave;
}
}
}