Fix reprocessing not working on import due to realm threading woes

This commit is contained in:
Dean Herbert 2022-06-24 19:04:20 +09:00
parent 30b3973c9f
commit 0c3d890f76
2 changed files with 16 additions and 10 deletions

View File

@ -80,8 +80,6 @@ protected override void PreImport(BeatmapSetInfo beatmapSet, Realm realm)
// If this is ever an issue, we can consider marking as pending delete but not resetting the IDs (but care will be required for
// beatmaps, which don't have their own `DeletePending` state).
beatmapUpdater?.Process(beatmapSet, realm);
if (beatmapSet.OnlineID > 0)
{
var existingSetWithSameOnlineID = realm.All<BeatmapSetInfo>().SingleOrDefault(b => b.OnlineID == beatmapSet.OnlineID);
@ -99,6 +97,13 @@ protected override void PreImport(BeatmapSetInfo beatmapSet, Realm realm)
}
}
protected override void PostImport(BeatmapSetInfo model, Realm realm)
{
base.PostImport(model, realm);
beatmapUpdater?.Process(model);
}
private void validateOnlineIds(BeatmapSetInfo beatmapSet, Realm realm)
{
var beatmapIds = beatmapSet.Beatmaps.Where(b => b.OnlineID > 0).Select(b => b.OnlineID).ToList();

View File

@ -3,9 +3,9 @@
#nullable enable
using System.Diagnostics;
using System.Linq;
using System.Threading.Tasks;
using osu.Framework.Extensions;
using osu.Game.Database;
using osu.Game.Rulesets.Objects;
using Realms;
@ -52,17 +52,18 @@ public void Process(BeatmapSetInfo beatmapSet, Realm realm)
foreach (var beatmap in beatmapSet.Beatmaps)
{
// Because we aren't guaranteed all processing will happen on this thread, it's very hard to use the live realm object.
// This can be fixed by adding a synchronous flow to `BeatmapDifficultyCache`.
var detachedBeatmap = beatmap.Detach();
difficultyCache.Invalidate(beatmap);
beatmap.StarRating = difficultyCache.GetDifficultyAsync(detachedBeatmap).GetResultSafely()?.Stars ?? 0;
var working = workingBeatmapCache.GetWorkingBeatmap(beatmap.Detach());
var ruleset = working.BeatmapInfo.Ruleset.CreateInstance();
var working = workingBeatmapCache.GetWorkingBeatmap(beatmap);
Debug.Assert(ruleset != null);
var calculator = ruleset.CreateDifficultyCalculator(working);
beatmap.StarRating = calculator.Calculate().StarRating;
beatmap.Length = calculateLength(working.Beatmap);
beatmap.BPM = 60000 / working.Beatmap.GetMostCommonBeatLength();
difficultyCache.Invalidate(beatmap);
}
}