refactor(engine-ambient): BattleManagerBase.GetIns reads ambient first, static fallback

Step 3 of multi-instancing migration. The dominant per-battle singleton now
resolves through BattleAmbient.Current.Mgr when a scope is active. The legacy
'main' field is renamed _mainFallback and retained for unwrapped callers
(tests, anything not yet scope-wrapped). GetIns() still returns null when
neither is set, preserving the '?.Foo ?? default' patterns in engine code.

Co-Authored-By: Claude Opus 4.6 (1M context) <noreply@anthropic.com>
This commit is contained in:
gamer147
2026-06-07 21:23:27 -04:00
parent 92da7819f4
commit 4e756a6c46
4 changed files with 64 additions and 5 deletions

View File

@@ -1,6 +1,7 @@
#nullable enable
using SVSim.BattleEngine.Ambient;
using NUnit.Framework;
using System.Runtime.Serialization;
using System.Threading.Tasks;
namespace SVSim.BattleEngine.Tests;
@@ -129,4 +130,22 @@ public class BattleAmbientTests
BattleManagerBase.IsRandomDraw = false;
}
[Test]
public void GetIns_ReadsAmbient_WhenScopeActive()
{
var fakeMgr = (BattleManagerBase)System.Runtime.Serialization
.FormatterServices.GetUninitializedObject(typeof(BattleManagerBase));
var ctx = new BattleAmbientContext { Mgr = fakeMgr };
using var _ = BattleAmbient.Enter(ctx);
Assert.That(BattleManagerBase.GetIns(), Is.SameAs(fakeMgr));
}
[Test]
public void GetIns_OutsideScope_FallsBackToStatic()
{
Assert.That(BattleAmbient.Current, Is.Null);
var v = BattleManagerBase.GetIns();
Assert.Pass($"GetIns()={(v is null ? "null" : v.GetType().Name)}");
}
}