1
0
mirror of https://github.com/ppy/osu.git synced 2024-11-13 16:13:34 +08:00

Fix incorrect realm copy logic when a beatmap becomes detached from its set

The code here was assuming that if the beatmap which is having changes
copied across does not exist within the `BeatmapSet.Beatmaps` list, it
was not yet persisted to realm.

In some edge case, it can happen that the beatmap *is* persisted to
realm but not correctly attached to the beatmap set. I don't yet know
how this occurs, but it has caused loss of data for at least two users.

The fix here is to check realm-wide for the beatmap (using its primary
key) rather than only in the list. We then handle the scenario where the
beatmap needs to be reattached to the set as a seprate step.

---

This does raise others questions like "are we even structuring this
correctly? couldn't a single beatmap exist in two different sets?"

Maybe, but let's deal with that if/when it comes up.
This commit is contained in:
Dean Herbert 2023-06-27 18:19:59 +09:00
parent 4ecc724841
commit 8e80e2fa32

View File

@ -52,10 +52,19 @@ namespace osu.Game.Database
{
foreach (var beatmap in s.Beatmaps)
{
var existing = d.Beatmaps.FirstOrDefault(b => b.ID == beatmap.ID);
// Importantly, search all of realm for the beatmap (not just the set's beatmaps).
// It may have gotten detached, and if that's the case let's use this opportunity to fix
// things up.
var existingBeatmap = d.Realm.Find<BeatmapInfo>(beatmap.ID);
if (existing != null)
copyChangesToRealm(beatmap, existing);
if (existingBeatmap != null)
{
// As above, reattach if it happens to not be in the set's beatmaps.
if (!d.Beatmaps.Contains(existingBeatmap))
d.Beatmaps.Add(existingBeatmap);
copyChangesToRealm(beatmap, existingBeatmap);
}
else
{
var newBeatmap = new BeatmapInfo
@ -64,6 +73,7 @@ namespace osu.Game.Database
BeatmapSet = d,
Ruleset = d.Realm.Find<RulesetInfo>(beatmap.Ruleset.ShortName)
};
d.Beatmaps.Add(newBeatmap);
copyChangesToRealm(beatmap, newBeatmap);
}