1
0
mirror of https://github.com/ppy/osu.git synced 2024-12-14 12:33:01 +08:00

Fix testcase audio + dependency overrides not working

This commit is contained in:
smoogipoo 2018-05-23 18:52:09 +09:00
parent 8004b8af4d
commit a25462e10f
3 changed files with 53 additions and 31 deletions

View File

@ -1,6 +1,8 @@
// Copyright (c) 2007-2018 ppy Pty Ltd <contact@ppy.sh>.
// 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
/// </summary>
public class GameBeatmap : NonNullableBindable<WorkingBeatmap>, 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;
}

View File

@ -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<Drawable> Content => content;
protected GameBeatmap Beatmap;
private WorkingBeatmap lastBeatmap;
private Bindable<bool> 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<IGameBeatmap>(Beatmap);

View File

@ -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<AudioManager>());
dependencies = new DependencyContainer(parent);
dependencies.CacheAs<IGameBeatmap>(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();