1
0
mirror of https://github.com/ppy/osu.git synced 2024-11-11 14:57:52 +08:00

Merge remote-tracking branch 'upstream/master' into update-framework-with-everyrthing

This commit is contained in:
Dean Herbert 2018-09-06 18:04:58 +09:00
commit 01389f0443
2 changed files with 90 additions and 124 deletions

View File

@ -8,10 +8,10 @@ using osu.Game.Rulesets.Mods;
using System; using System;
using System.Collections.Generic; using System.Collections.Generic;
using System.Linq; using System.Linq;
using System.Threading.Tasks;
using osu.Game.Storyboards; using osu.Game.Storyboards;
using osu.Framework.IO.File; using osu.Framework.IO.File;
using System.IO; using System.IO;
using System.Threading;
using osu.Game.IO.Serialization; using osu.Game.IO.Serialization;
using osu.Game.Rulesets; using osu.Game.Rulesets;
using osu.Game.Rulesets.Objects; using osu.Game.Rulesets.Objects;
@ -38,12 +38,26 @@ namespace osu.Game.Beatmaps
Mods.ValueChanged += mods => applyRateAdjustments(); Mods.ValueChanged += mods => applyRateAdjustments();
beatmap = new AsyncLazy<IBeatmap>(populateBeatmap); beatmap = new RecyclableLazy<IBeatmap>(() =>
background = new AsyncLazy<Texture>(populateBackground, b => b == null || !b.IsDisposed); {
track = new AsyncLazy<Track>(populateTrack); var b = GetBeatmap() ?? new Beatmap();
waveform = new AsyncLazy<Waveform>(populateWaveform); // use the database-backed info.
storyboard = new AsyncLazy<Storyboard>(populateStoryboard); b.BeatmapInfo = BeatmapInfo;
skin = new AsyncLazy<Skin>(populateSkin); return b;
});
track = new RecyclableLazy<Track>(() =>
{
// we want to ensure that we always have a track, even if it's a fake one.
var t = GetTrack() ?? new VirtualBeatmapTrack(Beatmap);
applyRateAdjustments(t);
return t;
});
background = new RecyclableLazy<Texture>(GetBackground, BackgroundStillValid);
waveform = new RecyclableLazy<Waveform>(GetWaveform);
storyboard = new RecyclableLazy<Storyboard>(GetStoryboard);
skin = new RecyclableLazy<Skin>(GetSkin);
} }
/// <summary> /// <summary>
@ -58,28 +72,6 @@ namespace osu.Game.Beatmaps
return path; return path;
} }
protected abstract IBeatmap GetBeatmap();
protected abstract Texture GetBackground();
protected abstract Track GetTrack();
protected virtual Skin GetSkin() => new DefaultSkin();
protected virtual Waveform GetWaveform() => new Waveform();
protected virtual Storyboard GetStoryboard() => new Storyboard { BeatmapInfo = BeatmapInfo };
public bool BeatmapLoaded => beatmap.IsResultAvailable;
public IBeatmap Beatmap => beatmap.Value.Result;
public Task<IBeatmap> GetBeatmapAsync() => beatmap.Value;
private readonly AsyncLazy<IBeatmap> beatmap;
private IBeatmap populateBeatmap()
{
var b = GetBeatmap() ?? new Beatmap();
// use the database-backed info.
b.BeatmapInfo = BeatmapInfo;
return b;
}
/// <summary> /// <summary>
/// Constructs a playable <see cref="IBeatmap"/> from <see cref="Beatmap"/> using the applicable converters for a specific <see cref="RulesetInfo"/>. /// Constructs a playable <see cref="IBeatmap"/> from <see cref="Beatmap"/> using the applicable converters for a specific <see cref="RulesetInfo"/>.
/// <para> /// <para>
@ -136,62 +128,53 @@ namespace osu.Game.Beatmaps
public override string ToString() => BeatmapInfo.ToString(); public override string ToString() => BeatmapInfo.ToString();
public bool BackgroundLoaded => background.IsResultAvailable; public bool BeatmapLoaded => beatmap.IsResultAvailable;
public Texture Background => background.Value.Result; public IBeatmap Beatmap => beatmap.Value;
public Task<Texture> GetBackgroundAsync() => background.Value; protected abstract IBeatmap GetBeatmap();
private AsyncLazy<Texture> background; private readonly RecyclableLazy<IBeatmap> beatmap;
private Texture populateBackground() => GetBackground(); public bool BackgroundLoaded => background.IsResultAvailable;
public Texture Background => background.Value;
protected virtual bool BackgroundStillValid(Texture b) => b == null || !b.IsDisposed;
protected abstract Texture GetBackground();
private readonly RecyclableLazy<Texture> background;
public bool TrackLoaded => track.IsResultAvailable; public bool TrackLoaded => track.IsResultAvailable;
public Track Track => track.Value.Result; public Track Track => track.Value;
public Task<Track> GetTrackAsync() => track.Value; protected abstract Track GetTrack();
private AsyncLazy<Track> track; private RecyclableLazy<Track> track;
private Track populateTrack()
{
// we want to ensure that we always have a track, even if it's a fake one.
var t = GetTrack() ?? new VirtualBeatmapTrack(Beatmap);
applyRateAdjustments(t);
return t;
}
public bool WaveformLoaded => waveform.IsResultAvailable; public bool WaveformLoaded => waveform.IsResultAvailable;
public Waveform Waveform => waveform.Value.Result; public Waveform Waveform => waveform.Value;
public Task<Waveform> GetWaveformAsync() => waveform.Value; protected virtual Waveform GetWaveform() => new Waveform();
private readonly AsyncLazy<Waveform> waveform; private readonly RecyclableLazy<Waveform> waveform;
private Waveform populateWaveform() => GetWaveform();
public bool StoryboardLoaded => storyboard.IsResultAvailable; public bool StoryboardLoaded => storyboard.IsResultAvailable;
public Storyboard Storyboard => storyboard.Value.Result; public Storyboard Storyboard => storyboard.Value;
public Task<Storyboard> GetStoryboardAsync() => storyboard.Value; protected virtual Storyboard GetStoryboard() => new Storyboard { BeatmapInfo = BeatmapInfo };
private readonly AsyncLazy<Storyboard> storyboard; private readonly RecyclableLazy<Storyboard> storyboard;
private Storyboard populateStoryboard() => GetStoryboard();
public bool SkinLoaded => skin.IsResultAvailable; public bool SkinLoaded => skin.IsResultAvailable;
public Skin Skin => skin.Value.Result; public Skin Skin => skin.Value;
public Task<Skin> GetSkinAsync() => skin.Value; protected virtual Skin GetSkin() => new DefaultSkin();
private readonly AsyncLazy<Skin> skin; private readonly RecyclableLazy<Skin> skin;
private Skin populateSkin() => GetSkin(); /// <summary>
/// Transfer pieces of a beatmap to a new one, where possible, to save on loading.
public void TransferTo(WorkingBeatmap other) /// </summary>
/// <param name="other">The new beatmap which is being switched to.</param>
public virtual void TransferTo(WorkingBeatmap other)
{ {
if (track.IsResultAvailable && Track != null && BeatmapInfo.AudioEquals(other.BeatmapInfo)) if (track.IsResultAvailable && Track != null && BeatmapInfo.AudioEquals(other.BeatmapInfo))
other.track = track; other.track = track;
if (background.IsResultAvailable && Background != null && BeatmapInfo.BackgroundEquals(other.BeatmapInfo))
other.background = background;
} }
public virtual void Dispose() public virtual void Dispose()
{ {
if (BackgroundLoaded) Background?.Dispose(); background.Recycle();
if (WaveformLoaded) Waveform?.Dispose(); waveform.Recycle();
if (StoryboardLoaded) Storyboard?.Dispose(); storyboard.Recycle();
if (SkinLoaded) Skin?.Dispose(); skin.Recycle();
} }
/// <summary> /// <summary>
@ -210,15 +193,15 @@ namespace osu.Game.Beatmaps
mod.ApplyToClock(t); mod.ApplyToClock(t);
} }
public class AsyncLazy<T> public class RecyclableLazy<T>
{ {
private Lazy<Task<T>> lazy; private Lazy<T> lazy;
private readonly Func<T> valueFactory; private readonly Func<T> valueFactory;
private readonly Func<T, bool> stillValidFunction; private readonly Func<T, bool> stillValidFunction;
private readonly object initLock = new object(); private readonly object fetchLock = new object();
public AsyncLazy(Func<T> valueFactory, Func<T, bool> stillValidFunction = null) public RecyclableLazy(Func<T> valueFactory, Func<T, bool> stillValidFunction = null)
{ {
this.valueFactory = valueFactory; this.valueFactory = valueFactory;
this.stillValidFunction = stillValidFunction; this.stillValidFunction = stillValidFunction;
@ -230,45 +213,28 @@ namespace osu.Game.Beatmaps
{ {
if (!IsResultAvailable) return; if (!IsResultAvailable) return;
(lazy.Value.Result as IDisposable)?.Dispose(); (lazy.Value as IDisposable)?.Dispose();
recreate(); recreate();
} }
public bool IsResultAvailable public bool IsResultAvailable => stillValid;
public T Value
{ {
get get
{ {
recreateIfInvalid(); lock (fetchLock)
return lazy.Value.IsCompleted; {
if (!stillValid)
recreate();
return lazy.Value;
}
} }
} }
public Task<T> Value private bool stillValid => lazy.IsValueCreated && (stillValidFunction?.Invoke(lazy.Value) ?? true);
{
get
{
recreateIfInvalid();
return lazy.Value;
}
}
private void recreateIfInvalid() private void recreate() => lazy = new Lazy<T>(valueFactory, LazyThreadSafetyMode.ExecutionAndPublication);
{
lock (initLock)
{
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();
}
}
private void recreate() => lazy = new Lazy<Task<T>>(() => Task.Run(valueFactory));
} }
} }
} }

View File

@ -113,29 +113,6 @@ namespace osu.Game
dependencies.CacheAs(this); dependencies.CacheAs(this);
dependencies.Cache(LocalConfig); dependencies.Cache(LocalConfig);
runMigrations();
dependencies.Cache(SkinManager = new SkinManager(Host.Storage, contextFactory, Host, Audio));
dependencies.CacheAs<ISkinSource>(SkinManager);
var api = new APIAccess(LocalConfig);
dependencies.Cache(api);
dependencies.CacheAs<IAPIProvider>(api);
dependencies.Cache(RulesetStore = new RulesetStore(contextFactory));
dependencies.Cache(FileStore = new FileStore(contextFactory, Host.Storage));
dependencies.Cache(BeatmapManager = new BeatmapManager(Host.Storage, contextFactory, RulesetStore, api, Audio, Host));
dependencies.Cache(ScoreStore = new ScoreStore(Host.Storage, contextFactory, Host, BeatmapManager, RulesetStore));
dependencies.Cache(KeyBindingStore = new KeyBindingStore(contextFactory, RulesetStore));
dependencies.Cache(SettingsStore = new SettingsStore(contextFactory));
dependencies.Cache(RulesetConfigCache = new RulesetConfigCache(SettingsStore));
dependencies.Cache(new OsuColour());
fileImporters.Add(BeatmapManager);
fileImporters.Add(ScoreStore);
fileImporters.Add(SkinManager);
//this completely overrides the framework default. will need to change once we make a proper FontStore. //this completely overrides the framework default. will need to change once we make a proper FontStore.
dependencies.Cache(Fonts = new FontStore(new GlyphStore(Resources, @"Fonts/FontAwesome"))); dependencies.Cache(Fonts = new FontStore(new GlyphStore(Resources, @"Fonts/FontAwesome")));
@ -162,6 +139,29 @@ namespace osu.Game
Fonts.AddStore(new GlyphStore(Resources, @"Fonts/Venera")); Fonts.AddStore(new GlyphStore(Resources, @"Fonts/Venera"));
Fonts.AddStore(new GlyphStore(Resources, @"Fonts/Venera-Light")); Fonts.AddStore(new GlyphStore(Resources, @"Fonts/Venera-Light"));
runMigrations();
dependencies.Cache(SkinManager = new SkinManager(Host.Storage, contextFactory, Host, Audio));
dependencies.CacheAs<ISkinSource>(SkinManager);
var api = new APIAccess(LocalConfig);
dependencies.Cache(api);
dependencies.CacheAs<IAPIProvider>(api);
dependencies.Cache(RulesetStore = new RulesetStore(contextFactory));
dependencies.Cache(FileStore = new FileStore(contextFactory, Host.Storage));
dependencies.Cache(BeatmapManager = new BeatmapManager(Host.Storage, contextFactory, RulesetStore, api, Audio, Host));
dependencies.Cache(ScoreStore = new ScoreStore(Host.Storage, contextFactory, Host, BeatmapManager, RulesetStore));
dependencies.Cache(KeyBindingStore = new KeyBindingStore(contextFactory, RulesetStore));
dependencies.Cache(SettingsStore = new SettingsStore(contextFactory));
dependencies.Cache(RulesetConfigCache = new RulesetConfigCache(SettingsStore));
dependencies.Cache(new OsuColour());
fileImporters.Add(BeatmapManager);
fileImporters.Add(ScoreStore);
fileImporters.Add(SkinManager);
var defaultBeatmap = new DummyWorkingBeatmap(this); var defaultBeatmap = new DummyWorkingBeatmap(this);
beatmap = new OsuBindableBeatmap(defaultBeatmap, Audio); beatmap = new OsuBindableBeatmap(defaultBeatmap, Audio);
BeatmapManager.DefaultBeatmap = defaultBeatmap; BeatmapManager.DefaultBeatmap = defaultBeatmap;