diff --git a/osu.Game/Beatmaps/GameBeatmap.cs b/osu.Game/Beatmaps/GameBeatmap.cs index ac31b16533..f432fa4a7c 100644 --- a/osu.Game/Beatmaps/GameBeatmap.cs +++ b/osu.Game/Beatmaps/GameBeatmap.cs @@ -1,6 +1,8 @@ // Copyright (c) 2007-2018 ppy Pty Ltd . // Licensed under the MIT Licence - https://raw.githubusercontent.com/ppy/osu/master/LICENCE +using System.Diagnostics; +using osu.Framework.Audio; using osu.Framework.Configuration; namespace osu.Game.Beatmaps @@ -11,14 +13,42 @@ namespace osu.Game.Beatmaps /// public class GameBeatmap : NonNullableBindable, IGameBeatmap { - public GameBeatmap(WorkingBeatmap defaultValue) + private readonly AudioManager audioManager; + + private WorkingBeatmap lastBeatmap; + + public GameBeatmap(WorkingBeatmap defaultValue, AudioManager audioManager) : base(defaultValue) { + this.audioManager = audioManager; + + ValueChanged += registerAudioTrack; + } + + private void registerAudioTrack(WorkingBeatmap beatmap) + { + var trackLoaded = lastBeatmap?.TrackLoaded ?? false; + + // compare to last beatmap as sometimes the two may share a track representation (optimisation, see WorkingBeatmap.TransferTo) + if (!trackLoaded || lastBeatmap?.Track != beatmap.Track) + { + if (trackLoaded) + { + Debug.Assert(lastBeatmap != null); + Debug.Assert(lastBeatmap.Track != null); + + lastBeatmap.RecycleTrack(); + } + + audioManager.Track.AddItem(beatmap.Track); + } + + lastBeatmap = beatmap; } public GameBeatmap GetBoundCopy() { - var copy = new GameBeatmap(Default); + var copy = new GameBeatmap(Default, audioManager); copy.BindTo(this); return copy; } diff --git a/osu.Game/OsuGameBase.cs b/osu.Game/OsuGameBase.cs index 5546b5bcec..97f7f5a21e 100644 --- a/osu.Game/OsuGameBase.cs +++ b/osu.Game/OsuGameBase.cs @@ -3,7 +3,6 @@ using System; using System.Collections.Generic; -using System.Diagnostics; using System.IO; using System.Linq; using System.Reflection; @@ -66,7 +65,6 @@ namespace osu.Game protected override Container Content => content; protected GameBeatmap Beatmap; - private WorkingBeatmap lastBeatmap; private Bindable fpsDisplayVisible; @@ -158,34 +156,13 @@ namespace osu.Game Fonts.AddStore(new GlyphStore(Resources, @"Fonts/Venera-Light")); var defaultBeatmap = new DummyWorkingBeatmap(this); - Beatmap = new GameBeatmap(defaultBeatmap); + Beatmap = new GameBeatmap(defaultBeatmap, Audio); BeatmapManager.DefaultBeatmap = defaultBeatmap; // tracks play so loud our samples can't keep up. // this adds a global reduction of track volume for the time being. Audio.Track.AddAdjustment(AdjustableProperty.Volume, new BindableDouble(0.8)); - Beatmap.ValueChanged += b => - { - var trackLoaded = lastBeatmap?.TrackLoaded ?? false; - - // compare to last beatmap as sometimes the two may share a track representation (optimisation, see WorkingBeatmap.TransferTo) - if (!trackLoaded || lastBeatmap?.Track != b.Track) - { - if (trackLoaded) - { - Debug.Assert(lastBeatmap != null); - Debug.Assert(lastBeatmap.Track != null); - - lastBeatmap.RecycleTrack(); - } - - Audio.Track.AddItem(b.Track); - } - - lastBeatmap = b; - }; - dependencies.Cache(Beatmap); dependencies.CacheAs(Beatmap); diff --git a/osu.Game/Tests/Visual/OsuTestCase.cs b/osu.Game/Tests/Visual/OsuTestCase.cs index e654b3aab1..e6d3407c42 100644 --- a/osu.Game/Tests/Visual/OsuTestCase.cs +++ b/osu.Game/Tests/Visual/OsuTestCase.cs @@ -4,6 +4,7 @@ using System.IO; using System.Reflection; using osu.Framework.Allocation; +using osu.Framework.Audio; using osu.Framework.Testing; using osu.Game.Beatmaps; @@ -11,18 +12,32 @@ namespace osu.Game.Tests.Visual { public abstract class OsuTestCase : TestCase { - protected readonly GameBeatmap Beatmap = new GameBeatmap(new DummyWorkingBeatmap()); + protected GameBeatmap Beatmap { get; private set; } private DependencyContainer dependencies; protected override IReadOnlyDependencyContainer CreateLocalDependencies(IReadOnlyDependencyContainer parent) - => dependencies = new DependencyContainer(parent); - - [BackgroundDependencyLoader] - private void load() { + // The beatmap is constructed here rather than load() because our children get dependencies injected before our load() runs + Beatmap = new GameBeatmap(new DummyWorkingBeatmap(), parent.Get()); + + dependencies = new DependencyContainer(parent); + dependencies.CacheAs(Beatmap); dependencies.Cache(Beatmap); + + return dependencies; + } + + public override void Cleanup() + { + base.Cleanup(); + + if (Beatmap != null) + { + Beatmap.Disabled = true; + Beatmap.Value.Track.Stop(); + } } protected override ITestCaseTestRunner CreateRunner() => new OsuTestCaseTestRunner();