2021-09-30 17:21:16 +08:00
|
|
|
// Copyright (c) ppy Pty Ltd <contact@ppy.sh>. Licensed under the MIT Licence.
|
|
|
|
// See the LICENCE file in the repository root for full licence text.
|
|
|
|
|
|
|
|
using System;
|
|
|
|
using System.Collections.Generic;
|
|
|
|
using System.Linq;
|
|
|
|
using System.Threading;
|
|
|
|
using System.Threading.Tasks;
|
2022-01-07 23:40:14 +08:00
|
|
|
using Newtonsoft.Json;
|
2021-09-30 17:21:16 +08:00
|
|
|
using osu.Framework.Logging;
|
|
|
|
using osu.Framework.Platform;
|
|
|
|
using osu.Game.Beatmaps;
|
|
|
|
using osu.Game.Database;
|
|
|
|
using osu.Game.IO.Archives;
|
2021-12-14 18:47:11 +08:00
|
|
|
using osu.Game.Rulesets;
|
2021-09-30 17:21:16 +08:00
|
|
|
using osu.Game.Scoring.Legacy;
|
2021-12-06 21:47:00 +08:00
|
|
|
using osu.Game.Stores;
|
|
|
|
using Realms;
|
|
|
|
|
|
|
|
#nullable enable
|
2021-09-30 17:21:16 +08:00
|
|
|
|
|
|
|
namespace osu.Game.Scoring
|
|
|
|
{
|
2021-12-06 21:47:00 +08:00
|
|
|
public class ScoreModelManager : RealmArchiveModelManager<ScoreInfo>
|
2021-09-30 17:21:16 +08:00
|
|
|
{
|
|
|
|
public override IEnumerable<string> HandledExtensions => new[] { ".osr" };
|
|
|
|
|
|
|
|
protected override string[] HashableFileTypes => new[] { ".osr" };
|
|
|
|
|
2021-12-14 18:47:11 +08:00
|
|
|
private readonly RulesetStore rulesets;
|
2021-09-30 17:21:16 +08:00
|
|
|
private readonly Func<BeatmapManager> beatmaps;
|
|
|
|
|
2021-12-14 18:47:11 +08:00
|
|
|
public ScoreModelManager(RulesetStore rulesets, Func<BeatmapManager> beatmaps, Storage storage, RealmContextFactory contextFactory)
|
2021-12-06 21:47:00 +08:00
|
|
|
: base(storage, contextFactory)
|
2021-09-30 17:21:16 +08:00
|
|
|
{
|
|
|
|
this.rulesets = rulesets;
|
|
|
|
this.beatmaps = beatmaps;
|
|
|
|
}
|
|
|
|
|
2021-12-06 21:47:00 +08:00
|
|
|
protected override ScoreInfo? CreateModel(ArchiveReader archive)
|
2021-09-30 17:21:16 +08:00
|
|
|
{
|
|
|
|
using (var stream = archive.GetStream(archive.Filenames.First(f => f.EndsWith(".osr", StringComparison.OrdinalIgnoreCase))))
|
|
|
|
{
|
|
|
|
try
|
|
|
|
{
|
|
|
|
return new DatabasedLegacyScoreDecoder(rulesets, beatmaps()).Parse(stream).ScoreInfo;
|
|
|
|
}
|
|
|
|
catch (LegacyScoreDecoder.BeatmapNotFoundException e)
|
|
|
|
{
|
|
|
|
Logger.Log(e.Message, LoggingTarget.Information, LogLevel.Error);
|
|
|
|
return null;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
public Score GetScore(ScoreInfo score) => new LegacyDatabasedScore(score, rulesets, beatmaps(), Files.Store);
|
|
|
|
|
2021-12-06 21:47:00 +08:00
|
|
|
protected override Task Populate(ScoreInfo model, ArchiveReader? archive, Realm realm, CancellationToken cancellationToken = default)
|
2022-01-07 16:27:48 +08:00
|
|
|
{
|
|
|
|
// Ensure the beatmap is not detached.
|
2022-01-12 17:05:25 +08:00
|
|
|
if (!model.BeatmapInfo.IsManaged)
|
|
|
|
model.BeatmapInfo = realm.Find<BeatmapInfo>(model.BeatmapInfo.ID);
|
2022-01-07 16:27:48 +08:00
|
|
|
|
|
|
|
if (!model.Ruleset.IsManaged)
|
|
|
|
model.Ruleset = realm.Find<RulesetInfo>(model.Ruleset.ShortName);
|
|
|
|
|
2022-01-07 23:40:14 +08:00
|
|
|
if (string.IsNullOrEmpty(model.StatisticsJson))
|
|
|
|
model.StatisticsJson = JsonConvert.SerializeObject(model.Statistics);
|
|
|
|
|
2022-01-07 16:27:48 +08:00
|
|
|
return Task.CompletedTask;
|
|
|
|
}
|
2022-01-10 12:59:46 +08:00
|
|
|
|
|
|
|
public override bool IsAvailableLocally(ScoreInfo model)
|
|
|
|
{
|
|
|
|
using (var context = ContextFactory.CreateContext())
|
|
|
|
return context.All<ScoreInfo>().Any(b => b.OnlineID == model.OnlineID);
|
|
|
|
}
|
2021-09-30 17:21:16 +08:00
|
|
|
}
|
|
|
|
}
|