Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix WorkingBeatmapCache caching beatmap in wrong state leading to crash #27239

Merged
merged 2 commits into from Feb 19, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
63 changes: 63 additions & 0 deletions osu.Game.Tests/Visual/Navigation/TestSceneScreenNavigation.cs
Expand Up @@ -4,6 +4,7 @@
#nullable disable

using System;
using System.IO;
using System.Linq;
using NUnit.Framework;
using osu.Framework.Allocation;
Expand All @@ -18,6 +19,7 @@
using osu.Game.Beatmaps;
using osu.Game.Collections;
using osu.Game.Configuration;
using osu.Game.Extensions;
using osu.Game.Graphics.Containers;
using osu.Game.Graphics.UserInterface;
using osu.Game.Online.API;
Expand Down Expand Up @@ -221,6 +223,67 @@ public void TestOpenModSelectOverlayUsingAction()
AddAssert("Overlay was shown", () => songSelect.ModSelectOverlay.State.Value == Visibility.Visible);
}

[Test]
public void TestAttemptPlayBeatmapWrongHashFails()
{
Screens.Select.SongSelect songSelect = null;

AddStep("import beatmap", () => BeatmapImportHelper.LoadQuickOszIntoOsu(Game).GetResultSafely());
PushAndConfirm(() => songSelect = new TestPlaySongSelect());
AddUntilStep("wait for song select", () => songSelect.BeatmapSetsLoaded);

AddUntilStep("wait for selected", () => !Game.Beatmap.IsDefault);

AddStep("change beatmap files", () =>
{
foreach (var file in Game.Beatmap.Value.BeatmapSetInfo.Files.Where(f => Path.GetExtension(f.Filename) == ".osu"))
{
using (var stream = Game.Storage.GetStream(Path.Combine("files", file.File.GetStoragePath()), FileAccess.ReadWrite))
stream.WriteByte(0);
}
});

AddStep("invalidate cache", () =>
{
((IWorkingBeatmapCache)Game.BeatmapManager).Invalidate(Game.Beatmap.Value.BeatmapSetInfo);
});

AddStep("select next difficulty", () => InputManager.Key(Key.Down));
AddStep("press enter", () => InputManager.Key(Key.Enter));

AddUntilStep("wait for player loader", () => Game.ScreenStack.CurrentScreen is PlayerLoader);
AddUntilStep("wait for song select", () => songSelect.IsCurrentScreen());
}

[Test]
public void TestAttemptPlayBeatmapMissingFails()
{
Screens.Select.SongSelect songSelect = null;

AddStep("import beatmap", () => BeatmapImportHelper.LoadQuickOszIntoOsu(Game).GetResultSafely());
PushAndConfirm(() => songSelect = new TestPlaySongSelect());
AddUntilStep("wait for song select", () => songSelect.BeatmapSetsLoaded);

AddUntilStep("wait for selected", () => !Game.Beatmap.IsDefault);

AddStep("delete beatmap files", () =>
{
foreach (var file in Game.Beatmap.Value.BeatmapSetInfo.Files.Where(f => Path.GetExtension(f.Filename) == ".osu"))
Game.Storage.Delete(Path.Combine("files", file.File.GetStoragePath()));
});

AddStep("invalidate cache", () =>
{
((IWorkingBeatmapCache)Game.BeatmapManager).Invalidate(Game.Beatmap.Value.BeatmapSetInfo);
});

AddStep("select next difficulty", () => InputManager.Key(Key.Down));
AddStep("press enter", () => InputManager.Key(Key.Enter));

AddUntilStep("wait for player loader", () => Game.ScreenStack.CurrentScreen is PlayerLoader);
AddUntilStep("wait for song select", () => songSelect.IsCurrentScreen());
}

[Test]
public void TestRetryCountIncrements()
{
Expand Down
9 changes: 7 additions & 2 deletions osu.Game/Beatmaps/WorkingBeatmapCache.cs
Expand Up @@ -9,6 +9,7 @@
using JetBrains.Annotations;
using osu.Framework.Audio;
using osu.Framework.Audio.Track;
using osu.Framework.Extensions;
using osu.Framework.Graphics.Rendering;
using osu.Framework.Graphics.Rendering.Dummy;
using osu.Framework.Graphics.Textures;
Expand Down Expand Up @@ -143,8 +144,6 @@ protected override IBeatmap GetBeatmap()
{
string fileStorePath = BeatmapSetInfo.GetPathForFile(BeatmapInfo.Path);

// TODO: check validity of file

var stream = GetStream(fileStorePath);

if (stream == null)
Expand All @@ -153,6 +152,12 @@ protected override IBeatmap GetBeatmap()
return null;
}

if (stream.ComputeMD5Hash() != BeatmapInfo.MD5Hash)
{
Logger.Log($"Beatmap failed to load (file {BeatmapInfo.Path} does not have the expected hash).", level: LogLevel.Error);
return null;
}

using (var reader = new LineBufferedReader(stream))
return Decoder.GetDecoder<Beatmap>(reader).Decode(reader);
}
Expand Down
4 changes: 3 additions & 1 deletion osu.Game/Tests/Visual/OsuGameTestScene.cs
Expand Up @@ -153,6 +153,8 @@ public partial class TestOsuGame : OsuGame

public new Bindable<IReadOnlyList<Mod>> SelectedMods => base.SelectedMods;

public new Storage Storage => base.Storage;

public new SpectatorClient SpectatorClient => base.SpectatorClient;

// if we don't apply these changes, when running under nUnit the version that gets populated is that of nUnit.
Expand All @@ -166,7 +168,7 @@ public partial class TestOsuGame : OsuGame
public TestOsuGame(Storage storage, IAPIProvider api, string[] args = null)
: base(args)
{
Storage = storage;
base.Storage = storage;
API = api;
}

Expand Down