2021-09-30 09:21:16 +00: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;
|
2022-01-07 15:40:14 +00:00
|
|
|
using Newtonsoft.Json;
|
2021-09-30 09:21:16 +00: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 10:47:11 +00:00
|
|
|
using osu.Game.Rulesets;
|
2021-09-30 09:21:16 +00:00
|
|
|
using osu.Game.Scoring.Legacy;
|
2022-07-08 03:16:06 +00:00
|
|
|
using osu.Game.Online.API;
|
|
|
|
using osu.Game.Online.API.Requests;
|
|
|
|
using osu.Game.Online.API.Requests.Responses;
|
2021-12-06 13:47:00 +00:00
|
|
|
using Realms;
|
|
|
|
|
2021-09-30 09:21:16 +00:00
|
|
|
namespace osu.Game.Scoring
|
|
|
|
{
|
2022-06-16 09:53:13 +00:00
|
|
|
public class ScoreImporter : RealmArchiveModelImporter<ScoreInfo>
|
2021-09-30 09:21:16 +00:00
|
|
|
{
|
|
|
|
public override IEnumerable<string> HandledExtensions => new[] { ".osr" };
|
|
|
|
|
|
|
|
protected override string[] HashableFileTypes => new[] { ".osr" };
|
|
|
|
|
2021-12-14 10:47:11 +00:00
|
|
|
private readonly RulesetStore rulesets;
|
2021-09-30 09:21:16 +00:00
|
|
|
private readonly Func<BeatmapManager> beatmaps;
|
|
|
|
|
2022-07-08 03:16:06 +00:00
|
|
|
private readonly IAPIProvider api;
|
|
|
|
|
|
|
|
public ScoreImporter(RulesetStore rulesets, Func<BeatmapManager> beatmaps, Storage storage, RealmAccess realm, IAPIProvider api)
|
2022-01-24 10:59:58 +00:00
|
|
|
: base(storage, realm)
|
2021-09-30 09:21:16 +00:00
|
|
|
{
|
|
|
|
this.rulesets = rulesets;
|
|
|
|
this.beatmaps = beatmaps;
|
2022-07-08 03:16:06 +00:00
|
|
|
this.api = api;
|
2021-09-30 09:21:16 +00:00
|
|
|
}
|
|
|
|
|
2021-12-06 13:47:00 +00:00
|
|
|
protected override ScoreInfo? CreateModel(ArchiveReader archive)
|
2021-09-30 09:21:16 +00: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);
|
|
|
|
|
2022-01-13 07:27:07 +00:00
|
|
|
protected override void Populate(ScoreInfo model, ArchiveReader? archive, Realm realm, CancellationToken cancellationToken = default)
|
2022-01-07 08:27:48 +00:00
|
|
|
{
|
|
|
|
// Ensure the beatmap is not detached.
|
2022-01-12 09:05:25 +00:00
|
|
|
if (!model.BeatmapInfo.IsManaged)
|
|
|
|
model.BeatmapInfo = realm.Find<BeatmapInfo>(model.BeatmapInfo.ID);
|
2022-01-07 08:27:48 +00:00
|
|
|
|
|
|
|
if (!model.Ruleset.IsManaged)
|
|
|
|
model.Ruleset = realm.Find<RulesetInfo>(model.Ruleset.ShortName);
|
|
|
|
|
2022-01-17 05:02:15 +00:00
|
|
|
// These properties are known to be non-null, but these final checks ensure a null hasn't come from somewhere (or the refetch has failed).
|
|
|
|
// Under no circumstance do we want these to be written to realm as null.
|
|
|
|
if (model.BeatmapInfo == null) throw new ArgumentNullException(nameof(model.BeatmapInfo));
|
|
|
|
if (model.Ruleset == null) throw new ArgumentNullException(nameof(model.Ruleset));
|
|
|
|
|
2022-01-07 15:40:14 +00:00
|
|
|
if (string.IsNullOrEmpty(model.StatisticsJson))
|
|
|
|
model.StatisticsJson = JsonConvert.SerializeObject(model.Statistics);
|
2022-08-22 12:31:10 +00:00
|
|
|
|
|
|
|
if (string.IsNullOrEmpty(model.MaximumStatisticsJson))
|
|
|
|
model.MaximumStatisticsJson = JsonConvert.SerializeObject(model.MaximumStatistics);
|
2022-01-07 08:27:48 +00:00
|
|
|
}
|
2022-07-08 03:16:06 +00:00
|
|
|
|
2022-07-28 07:18:30 +00:00
|
|
|
protected override void PostImport(ScoreInfo model, Realm realm, bool batchImport)
|
2022-07-08 03:16:06 +00:00
|
|
|
{
|
2022-07-28 07:18:30 +00:00
|
|
|
base.PostImport(model, realm, batchImport);
|
2022-07-08 03:16:06 +00:00
|
|
|
|
2022-07-11 18:46:42 +00:00
|
|
|
var userRequest = new GetUserRequest(model.RealmUser.Username);
|
|
|
|
|
2022-07-08 03:16:06 +00:00
|
|
|
api.Perform(userRequest);
|
|
|
|
|
2022-07-11 18:46:42 +00:00
|
|
|
if (userRequest.Response is APIUser user)
|
2022-07-16 03:31:01 +00:00
|
|
|
model.User = user;
|
2022-07-08 03:16:06 +00:00
|
|
|
}
|
2021-09-30 09:21:16 +00:00
|
|
|
}
|
|
|
|
}
|