A Civilization-style strategy game about governing Israel from 15 May 1948. Real ministers, real map, every fact sourced. Unity + deterministic C# sim.
CardVideo: ignore a late prepare or error from the previous clip's player (Refs #2564) (#3067)
Eyal Golan369f0005ffb4
diff --git a/changelog.d/2564-card-video-stale-callback.md b/changelog.d/2564-card-video-stale-callback.mdnew file mode 100644index 000000000..59bf72dd2--- /dev/null+++ b/changelog.d/2564-card-video-stale-callback.md@@ -0,0 +1,4 @@+### Fixed+- Event and outcome clips (Refs #2564): when one clip follows another on the same player (the next card, or the next+ End Turn outcome), a late "ready" or error from the previous clip can no longer start the new clip early or drop it to+ its photograph. Each clip now gets its own video player, and callbacks from the old one are ignored.diff --git a/unity/Assets/Scripts/UI/CardVideo.cs b/unity/Assets/Scripts/UI/CardVideo.csindex 0ad4627b4..f539aa0d7 100644--- a/unity/Assets/Scripts/UI/CardVideo.cs+++ b/unity/Assets/Scripts/UI/CardVideo.cs@@ -6,13 +6,14 @@ using UnityEngine.Video;namespace Statecraft.Game.UI{- /// <summary>A silent clip player (decision videos, 2026-10-13): one VideoPlayer, made the first time a clip is asked for- /// (never at boot), playing a clip from StreamingAssets by URL (the only source the Web player takes) into one reused- /// 960x540 RenderTexture, which the target element shows as its background. The decision card has one, for its looping- /// illustration; the End Turn outcome clips have their own (OutcomePresenter), which plays each clip once. No audio is- /// output (the clips have no audio track). A clip that fails (errorReceived) or is not prepared within its timeout is- /// stopped and reported once. The texture is cleared at each Play, so a new clip never opens on the last frame of the- /// previous one. Presentation only.</summary>+ /// <summary>A silent clip player (decision videos, 2026-10-13): a VideoPlayer per clip, the first made when a clip is+ /// first asked for (never at boot), playing a clip from StreamingAssets by URL (the only source the Web player takes)+ /// into one reused 960x540 RenderTexture, which the target element shows as its background. The decision card has one,+ /// for its looping illustration; the End Turn outcome clips have their own (OutcomePresenter), which plays each clip+ /// once. No audio is output (the clips have no audio track). A clip that fails (errorReceived) or is not prepared within+ /// its timeout is stopped and reported once. The texture is cleared at each Play, so a new clip never opens on the last+ /// frame of the previous one. Each Play gets a fresh VideoPlayer (the old one is destroyed), so a prepare, end or error+ /// the previous clip's player sends late is told apart by its sender and ignored (issue #2564). Presentation only.</summary>public sealed class CardVideo : IDisposable{public const int Width = 960, Height = 540;@@ -68,6 +69,7 @@ namespace Statecraft.Game.UI{Stop();Ensure();+ NewPlayer();var was = RenderTexture.active; RenderTexture.active = rt; GL.Clear(false, true, Color.black); RenderTexture.active = was;this.held = held;target = header; onFail = failed; onEnded = loop ? null : ended; Current = url;@@ -103,12 +105,20 @@ namespace Statecraft.Game.UIvoid Ensure(){- if (player != null) return;+ if (go != null) return;rt = new RenderTexture(Width, Height, 0, RenderTextureFormat.ARGB32) { name = "CardVideo" };rt.Create();go = new GameObject("CardVideo");if (Application.isPlaying) UnityEngine.Object.DontDestroyOnLoad(go); // edit-mode tests make one too- player = go.AddComponent<VideoPlayer>();+ }++ /// <summary>Replaces the (stopped) player with a new one for the next clip: a VideoPlayer's callbacks carry no clip+ /// identity, and after Play the reused player's url already reads the new clip, so only the sender tells a late+ /// callback of the previous clip from the new clip's own (issue #2564).</summary>+ void NewPlayer()+ {+ if (player != null) { if (Application.isPlaying) UnityEngine.Object.Destroy(player); else UnityEngine.Object.DestroyImmediate(player); }+ player = go!.AddComponent<VideoPlayer>();player.playOnAwake = false;player.source = VideoSource.Url;player.renderMode = VideoRenderMode.RenderTexture;@@ -118,13 +128,13 @@ namespace Statecraft.Game.UIplayer.skipOnDrop = true;player.waitForFirstFrame = true;player.prepareCompleted += OnPrepared;- player.loopPointReached += _ => { var told = onEnded; if (told == null) return; Stop(); told(); }; // a once-only clip ended+ player.loopPointReached += OnEnded;player.errorReceived += OnError;}void OnPrepared(VideoPlayer p){- if (target == null) return;+ if (!ReferenceEquals(p, player) || target == null) return; // a late prepare from the previous clip's playerPrepared = true;if (!held) Release();}@@ -140,7 +150,16 @@ namespace Statecraft.Game.UIDebug.Log("VIDEO playing " + Current);}- void OnError(VideoPlayer p, string message) => Fail(message);+ void OnEnded(VideoPlayer p)+ {+ if (!ReferenceEquals(p, player)) return; // a late end from the previous clip's player+ var told = onEnded; if (told == null) return; Stop(); told(); // a once-only clip ended+ }++ void OnError(VideoPlayer p, string message)+ {+ if (ReferenceEquals(p, player)) Fail(message); // not a late error from the previous clip's player+ }void Fail(string why){diff --git a/unity/Assets/Tests/EditMode/CardVideoStaleCallbackTests.cs b/unity/Assets/Tests/EditMode/CardVideoStaleCallbackTests.csnew file mode 100644index 000000000..3201a66ee--- /dev/null+++ b/unity/Assets/Tests/EditMode/CardVideoStaleCallbackTests.cs@@ -0,0 +1,92 @@+#nullable enable+using System.Reflection;+using NUnit.Framework;+using Statecraft.Game.UI;+using UnityEngine.TestTools;+using UnityEngine.UIElements;+using UnityEngine.Video;++/// <summary>Issue #2564: CardVideo reuses itself for the next clip (card to next card, outcome to next outcome), so a+/// prepareCompleted, errorReceived or loopPointReached that the previous clip's player sends late must not prepare, start,+/// fail or end the clip playing now. The callbacks are fired by hand (reflection, as EventVideoTests and OpeningClipTests+/// reach Fail): the edit-mode VideoPlayer's own timing is not under test, and an EditMode run cannot make a real player+/// answer late.</summary>+public class CardVideoStaleCallbackTests+{+ const BindingFlags Flags = BindingFlags.NonPublic | BindingFlags.Instance;++ static VideoPlayer PlayerOf(CardVideo video) => (VideoPlayer)typeof(CardVideo).GetField("player", Flags)!.GetValue(video)!;+ static void Prepared(CardVideo video, VideoPlayer p) =>+ typeof(CardVideo).GetMethod("OnPrepared", Flags)!.Invoke(video, new object[] { p });+ static void Error(CardVideo video, VideoPlayer p, string message) =>+ typeof(CardVideo).GetMethod("OnError", Flags)!.Invoke(video, new object[] { p, message });+ static void Ended(CardVideo video, VideoPlayer p)+ {+ var onEnded = typeof(CardVideo).GetMethod("OnEnded", Flags);+ Assert.IsNotNull(onEnded, "CardVideo handles loopPointReached in OnEnded(VideoPlayer)");+ onEnded!.Invoke(video, new object[] { p });+ }++ bool quiet;+ [SetUp] public void QuietVideoPlayer() { quiet = LogAssert.ignoreFailingMessages; LogAssert.ignoreFailingMessages = true; } // the edit-mode VideoPlayer may log while it prepares+ [TearDown] public void Restore() => LogAssert.ignoreFailingMessages = quiet;++ [Test]+ public void ALatePrepareFromThePreviousClipDoesNotPrepareTheNextOne()+ {+ using var video = new CardVideo();+ var header = new VisualElement();+ video.Play("a.mp4", header, _ => { });+ var first = PlayerOf(video);+ video.Play("b.mp4", header, _ => { }, held: true); // held: a prepare only marks it Prepared, nothing starts++ Prepared(video, first);+ Assert.IsFalse(video.Prepared, "a.mp4's late prepareCompleted must not mark b.mp4 prepared");+ Assert.IsFalse(video.Showing);+ Assert.AreEqual("b.mp4", video.Current);++ Prepared(video, PlayerOf(video));+ Assert.IsTrue(video.Prepared, "b.mp4's own prepareCompleted still prepares it");+ }++ [Test]+ public void ALateErrorFromThePreviousClipDoesNotFailTheNextOne()+ {+ using var video = new CardVideo();+ var header = new VisualElement();+ int aFailed = 0, bFailed = 0; string? bWhy = null;+ video.Play("a.mp4", header, _ => aFailed++);+ var first = PlayerOf(video);+ video.Play("b.mp4", header, why => { bFailed++; bWhy = why; });++ Error(video, first, "a.mp4 broke");+ Assert.AreEqual(0, bFailed, "a.mp4's late errorReceived must not fail b.mp4");+ Assert.AreEqual(0, aFailed, "a.mp4 was stopped: nobody is told about it");+ Assert.AreEqual("b.mp4", video.Current, "b.mp4 keeps playing");++ Error(video, PlayerOf(video), "b.mp4 broke");+ Assert.AreEqual(1, bFailed, "b.mp4's own errorReceived still fails it, once");+ Assert.AreEqual("b.mp4 broke", bWhy);+ Assert.IsNull(video.Current);+ }++ [Test]+ public void ALateEndFromThePreviousClipDoesNotEndTheNextOne()+ {+ using var video = new CardVideo();+ var header = new VisualElement();+ int aEnded = 0, bEnded = 0;+ video.Play("a.mp4", header, _ => { }, loop: false, ended: () => aEnded++);+ var first = PlayerOf(video);+ video.Play("b.mp4", header, _ => { }, loop: false, ended: () => bEnded++);++ Ended(video, first);+ Assert.AreEqual(0, bEnded, "a.mp4's late loopPointReached must not end b.mp4");+ Assert.AreEqual(0, aEnded, "a.mp4 was stopped: nobody is told about it");+ Assert.AreEqual("b.mp4", video.Current, "b.mp4 keeps playing");++ Ended(video, PlayerOf(video));+ Assert.AreEqual(1, bEnded, "b.mp4's own loopPointReached still ends it, once");+ Assert.IsNull(video.Current);+ }+}diff --git a/unity/Assets/Tests/EditMode/CardVideoStaleCallbackTests.cs.meta b/unity/Assets/Tests/EditMode/CardVideoStaleCallbackTests.cs.metanew file mode 100644index 000000000..9dbd97ad8--- /dev/null+++ b/unity/Assets/Tests/EditMode/CardVideoStaleCallbackTests.cs.meta@@ -0,0 +1,2 @@+fileFormatVersion: 2+guid: f7954ddc09204d7d8174106ca6dcb4e5