Merge pull request #9230 from peppy/startup-safety

Improve handling of missing intro beatmap / files
This commit is contained in:
Dan Balasescu 2020-06-09 14:41:04 +09:00 committed by GitHub
commit ad28f84335
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
3 changed files with 64 additions and 26 deletions

View File

@ -79,6 +79,8 @@ public BeatmapManager(Storage storage, IDatabaseContextFactory contextFactory, R
beatmaps = (BeatmapStore)ModelStore;
beatmaps.BeatmapHidden += b => beatmapHidden.Value = new WeakReference<BeatmapInfo>(b);
beatmaps.BeatmapRestored += b => beatmapRestored.Value = new WeakReference<BeatmapInfo>(b);
beatmaps.ItemRemoved += removeWorkingCache;
beatmaps.ItemUpdated += removeWorkingCache;
onlineLookupQueue = new BeatmapOnlineLookupQueue(api, storage);
}
@ -213,9 +215,7 @@ public void Save(BeatmapInfo info, IBeatmap beatmapContent)
}
}
var working = workingCache.FirstOrDefault(w => w.BeatmapInfo?.ID == info.ID);
if (working != null)
workingCache.Remove(working);
removeWorkingCache(info);
}
private readonly WeakList<WorkingBeatmap> workingCache = new WeakList<WorkingBeatmap>();
@ -417,6 +417,24 @@ private double calculateLength(IBeatmap b)
return endTime - startTime;
}
private void removeWorkingCache(BeatmapSetInfo info)
{
if (info.Beatmaps == null) return;
foreach (var b in info.Beatmaps)
removeWorkingCache(b);
}
private void removeWorkingCache(BeatmapInfo info)
{
lock (workingCache)
{
var working = workingCache.FirstOrDefault(w => w.BeatmapInfo?.ID == info.ID);
if (working != null)
workingCache.Remove(working);
}
}
public void Dispose()
{
onlineLookupQueue?.Dispose();

View File

@ -41,9 +41,9 @@ public abstract class IntroScreen : StartupScreen
protected IBindable<bool> MenuMusic { get; private set; }
private WorkingBeatmap introBeatmap;
private WorkingBeatmap initialBeatmap;
protected Track Track { get; private set; }
protected Track Track => initialBeatmap?.Track;
private readonly BindableDouble exitingVolumeFade = new BindableDouble(1);
@ -58,6 +58,11 @@ public abstract class IntroScreen : StartupScreen
[Resolved]
private AudioManager audio { get; set; }
/// <summary>
/// Whether the <see cref="Track"/> is provided by osu! resources, rather than a user beatmap.
/// </summary>
protected bool UsingThemedIntro { get; private set; }
[BackgroundDependencyLoader]
private void load(OsuConfigManager config, SkinManager skinManager, BeatmapManager beatmaps, Framework.Game game)
{
@ -71,29 +76,45 @@ private void load(OsuConfigManager config, SkinManager skinManager, BeatmapManag
BeatmapSetInfo setInfo = null;
// if the user has requested not to play theme music, we should attempt to find a random beatmap from their collection.
if (!MenuMusic.Value)
{
var sets = beatmaps.GetAllUsableBeatmapSets(IncludedDetails.Minimal);
if (sets.Count > 0)
setInfo = beatmaps.QueryBeatmapSet(s => s.ID == sets[RNG.Next(0, sets.Count - 1)].ID);
}
if (setInfo == null)
{
setInfo = beatmaps.QueryBeatmapSet(b => b.Hash == BeatmapHash);
if (setInfo == null)
{
// we need to import the default menu background beatmap
setInfo = beatmaps.Import(new ZipArchiveReader(game.Resources.GetStream($"Tracks/{BeatmapFile}"), BeatmapFile)).Result;
setInfo.Protected = true;
beatmaps.Update(setInfo);
setInfo = beatmaps.QueryBeatmapSet(s => s.ID == sets[RNG.Next(0, sets.Count - 1)].ID);
initialBeatmap = beatmaps.GetWorkingBeatmap(setInfo.Beatmaps[0]);
}
}
introBeatmap = beatmaps.GetWorkingBeatmap(setInfo.Beatmaps[0]);
Track = introBeatmap.Track;
// we generally want a song to be playing on startup, so use the intro music even if a user has specified not to if no other track is available.
if (setInfo == null)
{
if (!loadThemedIntro())
{
// if we detect that the theme track or beatmap is unavailable this is either first startup or things are in a bad state.
// this could happen if a user has nuked their files store. for now, reimport to repair this.
var import = beatmaps.Import(new ZipArchiveReader(game.Resources.GetStream($"Tracks/{BeatmapFile}"), BeatmapFile)).Result;
import.Protected = true;
beatmaps.Update(import);
loadThemedIntro();
}
}
bool loadThemedIntro()
{
setInfo = beatmaps.QueryBeatmapSet(b => b.Hash == BeatmapHash);
if (setInfo != null)
{
initialBeatmap = beatmaps.GetWorkingBeatmap(setInfo.Beatmaps[0]);
UsingThemedIntro = !(Track is TrackVirtual);
}
return UsingThemedIntro;
}
}
public override void OnResuming(IScreen last)
@ -119,7 +140,7 @@ public override void OnResuming(IScreen last)
public override void OnSuspending(IScreen next)
{
base.OnSuspending(next);
Track = null;
initialBeatmap = null;
}
protected override BackgroundScreen CreateBackground() => new BackgroundScreenBlack();
@ -127,7 +148,7 @@ public override void OnSuspending(IScreen next)
protected void StartTrack()
{
// Only start the current track if it is the menu music. A beatmap's track is started when entering the Main Menu.
if (MenuMusic.Value)
if (UsingThemedIntro)
Track.Restart();
}
@ -141,8 +162,7 @@ protected override void LogoArriving(OsuLogo logo, bool resuming)
if (!resuming)
{
beatmap.Value = introBeatmap;
introBeatmap = null;
beatmap.Value = initialBeatmap;
logo.MoveTo(new Vector2(0.5f));
logo.ScaleTo(Vector2.One);

View File

@ -46,7 +46,7 @@ public class IntroTriangles : IntroScreen
[BackgroundDependencyLoader]
private void load()
{
if (MenuVoice.Value && !MenuMusic.Value)
if (MenuVoice.Value && !UsingThemedIntro)
welcome = audio.Samples.Get(@"welcome");
}
@ -61,7 +61,7 @@ protected override void LogoArriving(OsuLogo logo, bool resuming)
LoadComponentAsync(new TrianglesIntroSequence(logo, background)
{
RelativeSizeAxes = Axes.Both,
Clock = new FramedClock(MenuMusic.Value ? Track : null),
Clock = new FramedClock(UsingThemedIntro ? Track : null),
LoadMenu = LoadMenu
}, t =>
{