Fix repeated FIELD BGM restarts

This commit is contained in:
gamer147
2026-07-27 11:40:54 -04:00
parent 760e9663f4
commit a547fc4c11
5 changed files with 49 additions and 6 deletions

View File

@@ -7,6 +7,31 @@ using Xunit;
public class SfxOpsTests
{
[Fact]
public void ReassertingCurrentBgmPreservesPlaybackUntilTrackChangesOrStops()
{
var table = OpcodeTableJson.Load(Paths.OpcodesJson);
var script = ScriptAssembler.Assemble(table, "BGM",
new List<(int, Operand[])>
{
(0xbf, new[] { new Operand(0, 24) }),
(0xbf, new[] { new Operand(0, 24) }), // FIELD action cleanup: keep current playback position
(0xbf, new[] { new Operand(0, 25) }),
(0xbf, new[] { new Operand(0, 25) }),
(0xc2, new[] { new Operand(0, 0), new Operand(0, 0) }),
(0xbf, new[] { new Operand(0, 25) }), // fade-to-zero cleared the current track
(0x2, Array.Empty<Operand>()),
}, Array.Empty<string>());
var host = new RecordingHost();
var vm = new VirtualMachine(script, table, host);
vm.Run();
Assert.Equal("exit", vm.HaltReason);
Assert.Equal([24L, 25L, 25L], host.BgmTracks);
Assert.Equal((0, 0L), Assert.Single(host.BgmFades));
}
[Fact]
public void Sc0000AudioLifecycleDelayAndDuckControlReachHost()
{

View File

@@ -2330,9 +2330,18 @@ public sealed class VirtualMachine
Gfx.ReleaseSurfaceRange(42, 1000 - 42);
_host.ReleaseSurfaceRange(42, 1000 - 42); return pc + 1;
case "play-bgm":
_currentBgmTrackId = Read(a[0]);
_host.PlayBgm(_currentBgmTrackId);
{
long requestedTrackId = Read(a[0]);
// Native scripts freely reassert the stage BGM after FIELD action/event cleanup.
// The music facade retains the current track id, so an identical request keeps the
// existing stream position instead of reopening the OGG from the beginning.
if (requestedTrackId != _currentBgmTrackId)
{
_currentBgmTrackId = requestedTrackId;
_host.PlayBgm(_currentBgmTrackId);
}
return pc + 1;
}
case "get-current-bgm-track":
Write(a[0], _currentBgmTrackId);
return pc + 1;