1
0
mirror of https://github.com/ppy/osu.git synced 2024-12-13 08:32:57 +08:00

Move score attach logic to a helper method and call during editor save

This commit is contained in:
Dean Herbert 2023-07-04 14:51:09 +09:00
parent f30dc59afe
commit 64fc5e40e8
5 changed files with 22 additions and 3 deletions

View File

@ -440,6 +440,7 @@ namespace osu.Game.Tests.Database
BeatmapInfo beatmap = imported.Beatmaps.First();
beatmap.Hash = "new_hash";
beatmap.ResetOnlineInfo();
beatmap.UpdateLocalScores(r);
});
Assert.That(!imported.Beatmaps.First().Scores.Any());

View File

@ -383,6 +383,7 @@ namespace osu.Game.Tests.Database
beatmapInfo.Hash = new_beatmap_hash;
beatmapInfo.ResetOnlineInfo();
beatmapInfo.UpdateLocalScores(s.Realm);
});
realm.Run(r => r.Refresh());

View File

@ -20,7 +20,6 @@ using osu.Game.IO;
using osu.Game.IO.Archives;
using osu.Game.Overlays.Notifications;
using osu.Game.Rulesets;
using osu.Game.Scoring;
using Realms;
namespace osu.Game.Beatmaps
@ -210,8 +209,7 @@ namespace osu.Game.Beatmaps
// Let's reattach any matching scores that exist in the database, based on hash.
foreach (BeatmapInfo beatmap in model.Beatmaps)
{
foreach (var score in realm.All<ScoreInfo>().Where(score => score.BeatmapHash == beatmap.Hash))
score.BeatmapInfo = beatmap;
beatmap.UpdateLocalScores(realm);
}
ProcessBeatmap?.Invoke(model, parameters.Batch ? MetadataLookupScope.LocalCacheFirst : MetadataLookupScope.OnlineFirst);

View File

@ -234,6 +234,22 @@ namespace osu.Game.Beatmaps
}
}
/// <summary>
/// Local scores are retained separate from a beatmap's lifetime, matched via <see cref="ScoreInfo.BeatmapHash"/>.
/// Therefore we need to detach / reattach scores when a beatmap is edited or imported.
/// </summary>
/// <param name="realm">A realm instance in an active write transaction.</param>
public void UpdateLocalScores(Realm realm)
{
// first disassociate any scores which are already attached and no longer valid.
foreach (var score in Scores)
score.BeatmapInfo = null;
// then attach any scores which match the new hash.
foreach (var score in realm.All<ScoreInfo>().Where(s => s.BeatmapHash == Hash))
score.BeatmapInfo = this;
}
IBeatmapMetadataInfo IBeatmapInfo.Metadata => Metadata;
IBeatmapSetInfo? IBeatmapInfo.BeatmapSet => BeatmapSet;
IRulesetInfo IBeatmapInfo.Ruleset => Ruleset;

View File

@ -467,6 +467,9 @@ namespace osu.Game.Beatmaps
if (transferCollections)
beatmapInfo.TransferCollectionReferences(r, oldMd5Hash);
liveBeatmapSet.Beatmaps.Single(b => b.ID == beatmapInfo.ID)
.UpdateLocalScores(r);
// do not look up metadata.
// this is a locally-modified set now, so looking up metadata is busy work at best and harmful at worst.
ProcessBeatmap?.Invoke(liveBeatmapSet, MetadataLookupScope.None);