2018-01-05 19:21:19 +08:00
|
|
|
|
// Copyright (c) 2007-2018 ppy Pty Ltd <contact@ppy.sh>.
|
2017-02-07 12:59:30 +08:00
|
|
|
|
// Licensed under the MIT Licence - https://raw.githubusercontent.com/ppy/osu/master/LICENCE
|
2016-10-28 13:14:45 +08:00
|
|
|
|
|
|
|
|
|
using osu.Framework.Audio.Track;
|
2017-03-06 13:52:37 +08:00
|
|
|
|
using osu.Framework.Configuration;
|
2016-11-05 19:00:14 +08:00
|
|
|
|
using osu.Framework.Graphics.Textures;
|
2017-04-18 15:05:58 +08:00
|
|
|
|
using osu.Game.Rulesets.Mods;
|
2017-03-12 21:13:43 +08:00
|
|
|
|
using System;
|
|
|
|
|
using System.Collections.Generic;
|
2017-04-21 16:33:20 +08:00
|
|
|
|
using System.Linq;
|
2017-11-21 10:50:24 +08:00
|
|
|
|
using System.Threading.Tasks;
|
2017-11-30 04:28:02 +08:00
|
|
|
|
using osu.Game.Storyboards;
|
2017-12-21 18:56:12 +08:00
|
|
|
|
using osu.Framework.IO.File;
|
|
|
|
|
using System.IO;
|
|
|
|
|
using osu.Game.IO.Serialization;
|
|
|
|
|
using System.Diagnostics;
|
2018-03-14 19:45:04 +08:00
|
|
|
|
using osu.Game.Skinning;
|
2016-10-28 13:14:45 +08:00
|
|
|
|
|
|
|
|
|
namespace osu.Game.Beatmaps
|
|
|
|
|
{
|
2017-02-24 12:43:21 +08:00
|
|
|
|
public abstract class WorkingBeatmap : IDisposable
|
2016-10-28 13:14:45 +08:00
|
|
|
|
{
|
2016-11-05 17:16:15 +08:00
|
|
|
|
public readonly BeatmapInfo BeatmapInfo;
|
2016-10-28 19:24:14 +08:00
|
|
|
|
|
2016-11-05 17:16:15 +08:00
|
|
|
|
public readonly BeatmapSetInfo BeatmapSetInfo;
|
|
|
|
|
|
2017-05-01 21:29:57 +08:00
|
|
|
|
public readonly BeatmapMetadata Metadata;
|
|
|
|
|
|
2017-04-23 13:13:58 +08:00
|
|
|
|
public readonly Bindable<IEnumerable<Mod>> Mods = new Bindable<IEnumerable<Mod>>(new Mod[] { });
|
2017-03-06 13:52:37 +08:00
|
|
|
|
|
2017-07-20 10:01:50 +08:00
|
|
|
|
protected WorkingBeatmap(BeatmapInfo beatmapInfo)
|
2017-02-24 12:43:21 +08:00
|
|
|
|
{
|
|
|
|
|
BeatmapInfo = beatmapInfo;
|
2017-05-08 16:42:53 +08:00
|
|
|
|
BeatmapSetInfo = beatmapInfo.BeatmapSet;
|
2017-09-19 20:41:18 +08:00
|
|
|
|
Metadata = beatmapInfo.Metadata ?? BeatmapSetInfo?.Metadata ?? new BeatmapMetadata();
|
2017-04-23 13:41:15 +08:00
|
|
|
|
|
2017-04-21 16:33:20 +08:00
|
|
|
|
Mods.ValueChanged += mods => applyRateAdjustments();
|
|
|
|
|
|
2017-11-21 10:50:24 +08:00
|
|
|
|
beatmap = new AsyncLazy<Beatmap>(populateBeatmap);
|
2017-11-30 04:05:07 +08:00
|
|
|
|
background = new AsyncLazy<Texture>(populateBackground, b => b == null || !b.IsDisposed);
|
2017-11-21 10:50:24 +08:00
|
|
|
|
track = new AsyncLazy<Track>(populateTrack);
|
|
|
|
|
waveform = new AsyncLazy<Waveform>(populateWaveform);
|
2017-11-30 04:54:04 +08:00
|
|
|
|
storyboard = new AsyncLazy<Storyboard>(populateStoryboard);
|
2018-03-14 19:45:04 +08:00
|
|
|
|
skin = new AsyncLazy<Skin>(populateSkin);
|
2017-02-24 12:43:21 +08:00
|
|
|
|
}
|
2017-03-22 18:15:32 +08:00
|
|
|
|
|
2017-12-21 18:56:12 +08:00
|
|
|
|
/// <summary>
|
|
|
|
|
/// Saves the <see cref="Beatmap"/>.
|
|
|
|
|
/// </summary>
|
|
|
|
|
public void Save()
|
|
|
|
|
{
|
|
|
|
|
var path = FileSafety.GetTempPath(Guid.NewGuid().ToString().Replace("-", string.Empty) + ".json");
|
|
|
|
|
using (var sw = new StreamWriter(path))
|
|
|
|
|
sw.WriteLine(Beatmap.Serialize());
|
|
|
|
|
Process.Start(path);
|
|
|
|
|
}
|
|
|
|
|
|
2017-03-22 18:15:32 +08:00
|
|
|
|
protected abstract Beatmap GetBeatmap();
|
|
|
|
|
protected abstract Texture GetBackground();
|
|
|
|
|
protected abstract Track GetTrack();
|
2018-03-14 19:45:04 +08:00
|
|
|
|
protected virtual Skin GetSkin() => new DefaultSkin();
|
2017-10-09 16:18:11 +08:00
|
|
|
|
protected virtual Waveform GetWaveform() => new Waveform();
|
2018-02-16 11:07:59 +08:00
|
|
|
|
protected virtual Storyboard GetStoryboard() => new Storyboard { BeatmapInfo = BeatmapInfo };
|
2017-04-06 16:21:18 +08:00
|
|
|
|
|
2017-12-06 20:47:48 +08:00
|
|
|
|
public bool BeatmapLoaded => beatmap.IsResultAvailable;
|
2017-11-21 10:50:24 +08:00
|
|
|
|
public Beatmap Beatmap => beatmap.Value.Result;
|
|
|
|
|
public async Task<Beatmap> GetBeatmapAsync() => await beatmap.Value;
|
|
|
|
|
|
|
|
|
|
private readonly AsyncLazy<Beatmap> beatmap;
|
2017-08-05 15:22:10 +08:00
|
|
|
|
|
2017-11-17 17:35:54 +08:00
|
|
|
|
private Beatmap populateBeatmap()
|
|
|
|
|
{
|
|
|
|
|
var b = GetBeatmap() ?? new Beatmap();
|
2017-08-05 15:22:10 +08:00
|
|
|
|
|
2017-11-17 17:35:54 +08:00
|
|
|
|
// use the database-backed info.
|
|
|
|
|
b.BeatmapInfo = BeatmapInfo;
|
2017-08-05 15:22:10 +08:00
|
|
|
|
|
2017-11-17 17:35:54 +08:00
|
|
|
|
return b;
|
2017-03-22 18:15:32 +08:00
|
|
|
|
}
|
2017-04-06 16:21:18 +08:00
|
|
|
|
|
2017-12-06 20:47:48 +08:00
|
|
|
|
public bool BackgroundLoaded => background.IsResultAvailable;
|
2017-11-21 10:50:24 +08:00
|
|
|
|
public Texture Background => background.Value.Result;
|
|
|
|
|
public async Task<Texture> GetBackgroundAsync() => await background.Value;
|
|
|
|
|
private AsyncLazy<Texture> background;
|
2017-03-22 18:15:32 +08:00
|
|
|
|
|
2017-11-17 17:35:54 +08:00
|
|
|
|
private Texture populateBackground() => GetBackground();
|
2016-10-28 19:24:14 +08:00
|
|
|
|
|
2017-12-06 20:47:48 +08:00
|
|
|
|
public bool TrackLoaded => track.IsResultAvailable;
|
2017-11-21 10:50:24 +08:00
|
|
|
|
public Track Track => track.Value.Result;
|
|
|
|
|
public async Task<Track> GetTrackAsync() => await track.Value;
|
|
|
|
|
private AsyncLazy<Track> track;
|
2017-11-17 17:35:54 +08:00
|
|
|
|
|
|
|
|
|
private Track populateTrack()
|
2017-10-09 16:18:11 +08:00
|
|
|
|
{
|
2017-11-17 17:35:54 +08:00
|
|
|
|
// we want to ensure that we always have a track, even if it's a fake one.
|
|
|
|
|
var t = GetTrack() ?? new TrackVirtual();
|
|
|
|
|
applyRateAdjustments(t);
|
|
|
|
|
return t;
|
2017-10-09 16:18:11 +08:00
|
|
|
|
}
|
|
|
|
|
|
2017-12-06 20:47:48 +08:00
|
|
|
|
public bool WaveformLoaded => waveform.IsResultAvailable;
|
2017-11-21 10:50:24 +08:00
|
|
|
|
public Waveform Waveform => waveform.Value.Result;
|
|
|
|
|
public async Task<Waveform> GetWaveformAsync() => await waveform.Value;
|
|
|
|
|
private readonly AsyncLazy<Waveform> waveform;
|
2017-11-17 17:35:54 +08:00
|
|
|
|
|
|
|
|
|
private Waveform populateWaveform() => GetWaveform();
|
2016-11-05 20:01:46 +08:00
|
|
|
|
|
2017-12-07 16:26:39 +08:00
|
|
|
|
public bool StoryboardLoaded => storyboard.IsResultAvailable;
|
2017-11-30 04:54:04 +08:00
|
|
|
|
public Storyboard Storyboard => storyboard.Value.Result;
|
|
|
|
|
public async Task<Storyboard> GetStoryboardAsync() => await storyboard.Value;
|
|
|
|
|
private readonly AsyncLazy<Storyboard> storyboard;
|
|
|
|
|
|
|
|
|
|
private Storyboard populateStoryboard() => GetStoryboard();
|
|
|
|
|
|
2018-03-14 19:45:04 +08:00
|
|
|
|
public bool SkinLoaded => skin.IsResultAvailable;
|
|
|
|
|
public Skin Skin => skin.Value.Result;
|
|
|
|
|
public async Task<Skin> GetSkinAsync() => await skin.Value;
|
|
|
|
|
private readonly AsyncLazy<Skin> skin;
|
|
|
|
|
|
|
|
|
|
private Skin populateSkin() => GetSkin();
|
|
|
|
|
|
2017-03-22 18:15:32 +08:00
|
|
|
|
public void TransferTo(WorkingBeatmap other)
|
|
|
|
|
{
|
2017-12-06 20:47:48 +08:00
|
|
|
|
if (track.IsResultAvailable && Track != null && BeatmapInfo.AudioEquals(other.BeatmapInfo))
|
2017-11-17 17:35:54 +08:00
|
|
|
|
other.track = track;
|
2017-04-28 14:03:07 +08:00
|
|
|
|
|
2017-12-06 20:47:48 +08:00
|
|
|
|
if (background.IsResultAvailable && Background != null && BeatmapInfo.BackgroundEquals(other.BeatmapInfo))
|
2017-04-28 14:03:07 +08:00
|
|
|
|
other.background = background;
|
2017-03-22 18:15:32 +08:00
|
|
|
|
}
|
2017-04-06 16:21:18 +08:00
|
|
|
|
|
2017-03-22 18:15:32 +08:00
|
|
|
|
public virtual void Dispose()
|
|
|
|
|
{
|
2017-11-17 21:14:28 +08:00
|
|
|
|
if (BackgroundLoaded) Background?.Dispose();
|
|
|
|
|
if (WaveformLoaded) Waveform?.Dispose();
|
2017-11-30 04:54:04 +08:00
|
|
|
|
if (StoryboardLoaded) Storyboard?.Dispose();
|
2018-03-14 19:45:04 +08:00
|
|
|
|
if (SkinLoaded) Skin?.Dispose();
|
2017-03-22 18:15:32 +08:00
|
|
|
|
}
|
2017-09-12 15:55:48 +08:00
|
|
|
|
|
2017-11-30 04:05:07 +08:00
|
|
|
|
/// <summary>
|
|
|
|
|
/// Eagerly dispose of the audio track associated with this <see cref="WorkingBeatmap"/> (if any).
|
|
|
|
|
/// Accessing track again will load a fresh instance.
|
|
|
|
|
/// </summary>
|
|
|
|
|
public void RecycleTrack() => track.Recycle();
|
2017-11-17 17:35:54 +08:00
|
|
|
|
|
|
|
|
|
private void applyRateAdjustments(Track t = null)
|
|
|
|
|
{
|
2017-12-06 20:47:48 +08:00
|
|
|
|
if (t == null && track.IsResultAvailable) t = Track;
|
2017-11-17 17:35:54 +08:00
|
|
|
|
if (t == null) return;
|
|
|
|
|
|
|
|
|
|
t.ResetSpeedAdjustments();
|
|
|
|
|
foreach (var mod in Mods.Value.OfType<IApplicableToClock>())
|
|
|
|
|
mod.ApplyToClock(t);
|
2017-09-12 15:55:48 +08:00
|
|
|
|
}
|
2017-11-21 10:50:24 +08:00
|
|
|
|
|
2017-11-30 04:05:07 +08:00
|
|
|
|
public class AsyncLazy<T>
|
2017-11-21 10:50:24 +08:00
|
|
|
|
{
|
2017-11-30 04:05:07 +08:00
|
|
|
|
private Lazy<Task<T>> lazy;
|
|
|
|
|
private readonly Func<T> valueFactory;
|
|
|
|
|
private readonly Func<T, bool> stillValidFunction;
|
|
|
|
|
|
2017-12-01 21:43:49 +08:00
|
|
|
|
private readonly object initLock = new object();
|
|
|
|
|
|
2017-11-30 04:05:07 +08:00
|
|
|
|
public AsyncLazy(Func<T> valueFactory, Func<T, bool> stillValidFunction = null)
|
|
|
|
|
{
|
|
|
|
|
this.valueFactory = valueFactory;
|
|
|
|
|
this.stillValidFunction = stillValidFunction;
|
|
|
|
|
|
2017-12-06 20:47:48 +08:00
|
|
|
|
recreate();
|
2017-11-30 04:05:07 +08:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public void Recycle()
|
2017-11-21 10:50:24 +08:00
|
|
|
|
{
|
2017-12-06 20:47:48 +08:00
|
|
|
|
if (!IsResultAvailable) return;
|
2017-11-30 04:05:07 +08:00
|
|
|
|
|
|
|
|
|
(lazy.Value.Result as IDisposable)?.Dispose();
|
2017-12-06 20:47:48 +08:00
|
|
|
|
recreate();
|
2017-11-21 10:50:24 +08:00
|
|
|
|
}
|
2017-11-30 04:05:07 +08:00
|
|
|
|
|
2017-12-06 20:47:48 +08:00
|
|
|
|
public bool IsResultAvailable
|
2017-11-30 04:05:07 +08:00
|
|
|
|
{
|
|
|
|
|
get
|
|
|
|
|
{
|
2017-12-06 20:47:48 +08:00
|
|
|
|
recreateIfInvalid();
|
2017-12-06 08:49:12 +08:00
|
|
|
|
return lazy.Value.IsCompleted;
|
2017-11-30 04:05:07 +08:00
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public Task<T> Value
|
|
|
|
|
{
|
|
|
|
|
get
|
|
|
|
|
{
|
2017-12-06 20:47:48 +08:00
|
|
|
|
recreateIfInvalid();
|
2017-11-30 04:05:07 +08:00
|
|
|
|
return lazy.Value;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2017-12-06 20:47:48 +08:00
|
|
|
|
private void recreateIfInvalid()
|
2017-11-30 04:05:07 +08:00
|
|
|
|
{
|
2017-12-01 21:43:49 +08:00
|
|
|
|
lock (initLock)
|
|
|
|
|
{
|
2017-12-06 20:47:48 +08:00
|
|
|
|
if (!lazy.IsValueCreated || !lazy.Value.IsCompleted)
|
|
|
|
|
// we have not yet been initialised or haven't run the task.
|
|
|
|
|
return;
|
|
|
|
|
|
|
|
|
|
if (stillValidFunction?.Invoke(lazy.Value.Result) ?? true)
|
|
|
|
|
// we are still in a valid state.
|
|
|
|
|
return;
|
|
|
|
|
|
|
|
|
|
recreate();
|
2017-12-01 21:43:49 +08:00
|
|
|
|
}
|
2017-11-30 04:05:07 +08:00
|
|
|
|
}
|
|
|
|
|
|
2017-12-06 20:47:48 +08:00
|
|
|
|
private void recreate() => lazy = new Lazy<Task<T>>(() => Task.Run(valueFactory));
|
2017-11-21 10:50:24 +08:00
|
|
|
|
}
|
2016-10-28 13:14:45 +08:00
|
|
|
|
}
|
|
|
|
|
}
|