1
0
mirror of https://github.com/ppy/osu.git synced 2024-11-09 09:37:42 +08:00
osu-lazer/osu.Game/Tests/Visual/OsuTestScene.cs

440 lines
16 KiB
C#
Raw Normal View History

2019-09-17 21:33:27 +08:00
// Copyright (c) ppy Pty Ltd <contact@ppy.sh>. Licensed under the MIT Licence.
// See the LICENCE file in the repository root for full licence text.
2018-04-13 17:19:50 +08:00
2018-07-19 13:07:55 +08:00
using System;
2019-04-08 17:32:05 +08:00
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Threading.Tasks;
using JetBrains.Annotations;
using osu.Framework.Allocation;
using osu.Framework.Audio;
using osu.Framework.Audio.Track;
2019-02-21 18:04:31 +08:00
using osu.Framework.Bindables;
using osu.Framework.Graphics;
using osu.Framework.Graphics.Containers;
using osu.Framework.IO.Stores;
2018-07-19 13:07:55 +08:00
using osu.Framework.Platform;
2018-04-13 17:19:50 +08:00
using osu.Framework.Testing;
using osu.Framework.Timing;
using osu.Game.Beatmaps;
using osu.Game.Database;
using osu.Game.Online.API;
using osu.Game.Online.API.Requests.Responses;
using osu.Game.Overlays;
using osu.Game.Rulesets;
2019-04-08 17:32:05 +08:00
using osu.Game.Rulesets.Mods;
using osu.Game.Rulesets.Objects;
using osu.Game.Rulesets.UI;
using osu.Game.Screens;
2019-11-21 17:50:54 +08:00
using osu.Game.Storyboards;
using osu.Game.Tests.Beatmaps;
2018-04-13 17:19:50 +08:00
namespace osu.Game.Tests.Visual
{
[ExcludeFromDynamicCompile]
public abstract class OsuTestScene : TestScene
2018-04-13 17:19:50 +08:00
{
protected Bindable<WorkingBeatmap> Beatmap { get; private set; }
2019-04-08 17:32:05 +08:00
protected Bindable<RulesetInfo> Ruleset;
2019-12-13 20:45:38 +08:00
protected Bindable<IReadOnlyList<Mod>> SelectedMods;
protected new OsuScreenDependencies Dependencies { get; private set; }
private DrawableRulesetDependencies rulesetDependencies;
private Lazy<Storage> localStorage;
2018-07-19 13:07:55 +08:00
protected Storage LocalStorage => localStorage.Value;
private Lazy<DatabaseContextFactory> contextFactory;
protected IResourceStore<byte[]> Resources;
protected IAPIProvider API
{
get
{
if (UseOnlineAPI)
2019-09-13 21:15:11 +08:00
throw new InvalidOperationException($"Using the {nameof(OsuTestScene)} dummy API is not supported when {nameof(UseOnlineAPI)} is true");
return dummyAPI;
}
}
private DummyAPIAccess dummyAPI;
protected DatabaseContextFactory ContextFactory => contextFactory.Value;
/// <summary>
/// Whether this test scene requires real-world API access.
/// If true, this will bypass the local <see cref="DummyAPIAccess"/> and use the <see cref="OsuGameBase"/> provided one.
/// </summary>
protected virtual bool UseOnlineAPI => false;
/// <summary>
/// When running headless, there is an opportunity to use the host storage rather than creating a second isolated one.
/// This is because the host is recycled per TestScene execution in headless at an nunit level.
/// </summary>
private Storage isolatedHostStorage;
2018-07-11 16:07:14 +08:00
protected override IReadOnlyDependencyContainer CreateChildDependencies(IReadOnlyDependencyContainer parent)
{
if (!UseFreshStoragePerRun)
isolatedHostStorage = (parent.Get<GameHost>() as HeadlessGameHost)?.Storage;
Resources = parent.Get<OsuGameBase>().Resources;
contextFactory = new Lazy<DatabaseContextFactory>(() =>
{
var factory = new DatabaseContextFactory(LocalStorage);
// only reset the database if not using the host storage.
// if we reset the host storage, it will delete global key bindings.
if (isolatedHostStorage == null)
factory.ResetDatabase();
using (var usage = factory.Get())
usage.Migrate();
return factory;
});
RecycleLocalStorage(false);
var baseDependencies = base.CreateChildDependencies(parent);
var providedRuleset = CreateRuleset();
if (providedRuleset != null)
baseDependencies = rulesetDependencies = new DrawableRulesetDependencies(providedRuleset, baseDependencies);
Dependencies = new OsuScreenDependencies(false, baseDependencies);
Beatmap = Dependencies.Beatmap;
Beatmap.SetDefault();
Ruleset = Dependencies.Ruleset;
Ruleset.SetDefault();
2019-12-13 20:45:38 +08:00
SelectedMods = Dependencies.Mods;
SelectedMods.SetDefault();
if (!UseOnlineAPI)
{
dummyAPI = new DummyAPIAccess();
Dependencies.CacheAs<IAPIProvider>(dummyAPI);
2021-06-05 09:30:21 +08:00
base.Content.Add(dummyAPI);
}
return Dependencies;
}
protected override Container<Drawable> Content => content ?? base.Content;
private readonly Container content;
protected OsuTestScene()
2018-07-19 13:07:55 +08:00
{
base.Content.Add(content = new DrawSizePreservingFillContainer());
2018-07-19 13:07:55 +08:00
}
protected virtual bool UseFreshStoragePerRun => false;
public virtual void RecycleLocalStorage(bool isDisposing)
{
if (localStorage?.IsValueCreated == true)
{
try
{
localStorage.Value.DeleteDirectory(".");
}
catch
{
// we don't really care if this fails; it will just leave folders lying around from test runs.
}
}
localStorage =
new Lazy<Storage>(() => isolatedHostStorage ?? new TemporaryNativeStorage($"{GetType().Name}-{Guid.NewGuid()}"));
}
[Resolved]
2020-01-02 14:23:41 +08:00
protected AudioManager Audio { get; private set; }
[Resolved]
protected MusicController MusicController { get; private set; }
/// <summary>
/// Creates the ruleset to be used for this test scene.
/// </summary>
/// <remarks>
/// When testing against ruleset-specific components, this method must be overriden to their corresponding ruleset.
/// </remarks>
[CanBeNull]
protected virtual Ruleset CreateRuleset() => null;
protected virtual IBeatmap CreateBeatmap(RulesetInfo ruleset) => new TestBeatmap(ruleset);
/// <summary>
/// Returns a sample API Beatmap with BeatmapSet populated.
/// </summary>
/// <param name="ruleset">The ruleset to create the sample model using. osu! ruleset will be used if not specified.</param>
protected APIBeatmap CreateAPIBeatmap(RulesetInfo ruleset = null)
{
var beatmapSet = CreateAPIBeatmapSet(ruleset ?? Ruleset.Value);
// Avoid circular reference.
var beatmap = beatmapSet.Beatmaps.First();
beatmapSet.Beatmaps = Array.Empty<APIBeatmap>();
// Populate the set as that's generally what we expect from the API.
beatmap.BeatmapSet = beatmapSet;
return beatmap;
}
/// <summary>
/// Returns a sample API BeatmapSet with beatmaps populated.
/// </summary>
/// <param name="ruleset">The ruleset to create the sample model using. osu! ruleset will be used if not specified.</param>
protected APIBeatmapSet CreateAPIBeatmapSet(RulesetInfo ruleset = null)
{
var beatmap = CreateBeatmap(ruleset ?? Ruleset.Value).BeatmapInfo;
return new APIBeatmapSet
{
OnlineID = ((IBeatmapSetInfo)beatmap.BeatmapSet).OnlineID,
Status = BeatmapSetOnlineStatus.Ranked,
Covers = new BeatmapSetOnlineCovers
{
Cover = "https://assets.ppy.sh/beatmaps/163112/covers/cover.jpg",
Card = "https://assets.ppy.sh/beatmaps/163112/covers/card.jpg",
List = "https://assets.ppy.sh/beatmaps/163112/covers/list.jpg"
},
Title = beatmap.BeatmapSet.Metadata.Title,
TitleUnicode = beatmap.BeatmapSet.Metadata.TitleUnicode,
Artist = beatmap.BeatmapSet.Metadata.Artist,
ArtistUnicode = beatmap.BeatmapSet.Metadata.ArtistUnicode,
Author = beatmap.BeatmapSet.Metadata.Author,
Source = beatmap.BeatmapSet.Metadata.Source,
Tags = beatmap.BeatmapSet.Metadata.Tags,
Beatmaps = new[]
{
new APIBeatmap
{
OnlineID = ((IBeatmapInfo)beatmap).OnlineID,
OnlineBeatmapSetID = ((IBeatmapSetInfo)beatmap.BeatmapSet).OnlineID,
Status = beatmap.Status,
Checksum = beatmap.MD5Hash,
AuthorID = beatmap.Metadata.Author.OnlineID,
RulesetID = beatmap.RulesetID,
StarRating = beatmap.StarRating,
DifficultyName = beatmap.DifficultyName,
}
}
};
}
protected WorkingBeatmap CreateWorkingBeatmap(RulesetInfo ruleset) =>
2021-07-05 23:52:39 +08:00
CreateWorkingBeatmap(CreateBeatmap(ruleset));
2019-11-21 17:50:54 +08:00
protected virtual WorkingBeatmap CreateWorkingBeatmap(IBeatmap beatmap, Storyboard storyboard = null) =>
2020-01-02 14:23:41 +08:00
new ClockBackedTestWorkingBeatmap(beatmap, storyboard, Clock, Audio);
[BackgroundDependencyLoader]
private void load(RulesetStore rulesets)
{
Ruleset.Value = CreateRuleset()?.RulesetInfo ?? rulesets.AvailableRulesets.First();
}
2018-05-24 11:53:32 +08:00
protected override void Dispose(bool isDisposing)
{
2018-05-24 11:53:32 +08:00
base.Dispose(isDisposing);
rulesetDependencies?.Dispose();
if (MusicController?.TrackLoaded == true)
MusicController.Stop();
2018-07-19 13:07:55 +08:00
if (contextFactory?.IsValueCreated == true)
contextFactory.Value.ResetDatabase();
RecycleLocalStorage(true);
}
protected override ITestSceneTestRunner CreateRunner() => new OsuTestSceneTestRunner();
2018-04-13 17:19:50 +08:00
public class ClockBackedTestWorkingBeatmap : TestWorkingBeatmap
{
private readonly Track track;
private readonly TrackVirtualStore store;
/// <summary>
/// Create an instance which creates a <see cref="TestBeatmap"/> for the provided ruleset when requested.
/// </summary>
/// <param name="ruleset">The target ruleset.</param>
/// <param name="referenceClock">A clock which should be used instead of a stopwatch for virtual time progression.</param>
/// <param name="audio">Audio manager. Required if a reference clock isn't provided.</param>
public ClockBackedTestWorkingBeatmap(RulesetInfo ruleset, IFrameBasedClock referenceClock, AudioManager audio)
2019-11-21 17:50:54 +08:00
: this(new TestBeatmap(ruleset), null, referenceClock, audio)
{
}
/// <summary>
/// Create an instance which provides the <see cref="IBeatmap"/> when requested.
/// </summary>
/// <param name="beatmap">The beatmap</param>
2019-11-21 17:50:54 +08:00
/// <param name="storyboard">The storyboard.</param>
/// <param name="referenceClock">An optional clock which should be used instead of a stopwatch for virtual time progression.</param>
/// <param name="audio">Audio manager. Required if a reference clock isn't provided.</param>
public ClockBackedTestWorkingBeatmap(IBeatmap beatmap, Storyboard storyboard, IFrameBasedClock referenceClock, AudioManager audio)
: base(beatmap, storyboard, audio)
{
double trackLength = 60000;
if (beatmap.HitObjects.Count > 0)
// add buffer after last hitobject to allow for final replay frames etc.
trackLength = Math.Max(trackLength, beatmap.HitObjects.Max(h => h.GetEndTime()) + 2000);
if (referenceClock != null)
{
store = new TrackVirtualStore(referenceClock);
audio.AddItem(store);
track = store.GetVirtual(trackLength);
}
else
track = audio?.Tracks.GetVirtual(trackLength);
}
2020-02-10 16:01:41 +08:00
~ClockBackedTestWorkingBeatmap()
{
2020-02-10 16:01:41 +08:00
// Remove the track store from the audio manager
store?.Dispose();
}
2020-08-07 21:31:41 +08:00
protected override Track GetBeatmapTrack() => track;
public class TrackVirtualStore : AudioCollectionManager<Track>, ITrackStore
{
private readonly IFrameBasedClock referenceClock;
public TrackVirtualStore(IFrameBasedClock referenceClock)
{
this.referenceClock = referenceClock;
}
public Track Get(string name) => throw new NotImplementedException();
public Task<Track> GetAsync(string name) => throw new NotImplementedException();
public Stream GetStream(string name) => throw new NotImplementedException();
public IEnumerable<string> GetAvailableResources() => throw new NotImplementedException();
2019-11-12 18:33:24 +08:00
public Track GetVirtual(double length = double.PositiveInfinity)
{
var track = new TrackVirtualManual(referenceClock) { Length = length };
AddItem(track);
return track;
}
}
/// <summary>
/// A virtual track which tracks a reference clock.
/// </summary>
public class TrackVirtualManual : Track
{
private readonly IFrameBasedClock referenceClock;
private bool running;
public TrackVirtualManual(IFrameBasedClock referenceClock)
{
this.referenceClock = referenceClock;
Length = double.PositiveInfinity;
}
public override bool Seek(double seek)
{
2020-03-06 21:44:11 +08:00
accumulated = Math.Clamp(seek, 0, Length);
lastReferenceTime = null;
return accumulated == seek;
}
public override void Start()
{
running = true;
}
public override void Reset()
{
Seek(0);
base.Reset();
}
public override void Stop()
{
if (running)
{
running = false;
lastReferenceTime = null;
}
}
public override bool IsRunning => running;
private double? lastReferenceTime;
private double accumulated;
public override double CurrentTime => Math.Min(accumulated, Length);
protected override void UpdateState()
{
base.UpdateState();
if (running)
{
double refTime = referenceClock.CurrentTime;
double? lastRefTime = lastReferenceTime;
if (lastRefTime != null)
accumulated += (refTime - lastRefTime.Value) * Rate;
lastReferenceTime = refTime;
}
if (CurrentTime >= Length)
{
Stop();
// `RaiseCompleted` is not called here to prevent transitioning to the next song.
}
}
}
}
public class OsuTestSceneTestRunner : OsuGameBase, ITestSceneTestRunner
2018-04-13 17:19:50 +08:00
{
private TestSceneTestRunner.TestRunner runner;
2018-04-18 14:12:48 +08:00
protected override void LoadAsyncComplete()
2018-04-13 17:19:50 +08:00
{
// this has to be run here rather than LoadComplete because
2019-05-15 17:32:29 +08:00
// TestScene.cs is checking the IsLoaded state (on another thread) and expects
// the runner to be loaded at that point.
Add(runner = new TestSceneTestRunner.TestRunner());
2018-04-13 17:19:50 +08:00
}
2018-04-18 14:12:48 +08:00
protected override void InitialiseFonts()
{
// skip fonts load as it's not required for testing purposes.
}
public void RunTestBlocking(TestScene test) => runner.RunTestBlocking(test);
2018-04-13 17:19:50 +08:00
}
}
}