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;
|
2022-09-09 04:57:01 +00:00
|
|
|
using System.Diagnostics;
|
2021-09-30 09:21:16 +00:00
|
|
|
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;
|
2022-09-08 13:06:44 +00:00
|
|
|
using osu.Game.Rulesets.Judgements;
|
|
|
|
using osu.Game.Rulesets.Scoring;
|
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;
|
|
|
|
|
2022-09-09 04:57:01 +00:00
|
|
|
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
|
|
|
{
|
2023-01-09 19:51:38 +00:00
|
|
|
string name = archive.Filenames.First(f => f.EndsWith(".osr", StringComparison.OrdinalIgnoreCase));
|
|
|
|
|
|
|
|
using (var stream = archive.GetStream(name))
|
2021-09-30 09:21:16 +00:00
|
|
|
{
|
|
|
|
try
|
|
|
|
{
|
|
|
|
return new DatabasedLegacyScoreDecoder(rulesets, beatmaps()).Parse(stream).ScoreInfo;
|
|
|
|
}
|
|
|
|
catch (LegacyScoreDecoder.BeatmapNotFoundException e)
|
|
|
|
{
|
2023-09-19 08:34:24 +00:00
|
|
|
Logger.Log($@"Score '{archive.Name}' failed to import: no corresponding beatmap with the hash '{e.Hash}' could be found.", LoggingTarget.Database);
|
2023-09-19 08:41:00 +00:00
|
|
|
|
|
|
|
// In the case of a missing beatmap, let's attempt to resolve it and show a prompt to the user to download the required beatmap.
|
|
|
|
var req = new GetBeatmapRequest(new BeatmapInfo { MD5Hash = e.Hash });
|
|
|
|
req.Success += res => PostNotification?.Invoke(new MissingBeatmapNotification(res, archive, e.Hash));
|
|
|
|
api.Queue(req);
|
2021-09-30 09:21:16 +00:00
|
|
|
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
|
|
|
{
|
2023-07-04 05:50:34 +00:00
|
|
|
Debug.Assert(model.BeatmapInfo != null);
|
|
|
|
|
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)
|
2023-07-06 04:37:42 +00:00
|
|
|
model.BeatmapInfo = realm.Find<BeatmapInfo>(model.BeatmapInfo.ID)!;
|
2022-01-07 08:27:48 +00:00
|
|
|
|
|
|
|
if (!model.Ruleset.IsManaged)
|
2023-07-06 04:37:42 +00:00
|
|
|
model.Ruleset = realm.Find<RulesetInfo>(model.Ruleset.ShortName)!;
|
2022-01-07 08:27:48 +00:00
|
|
|
|
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.
|
2022-12-22 20:27:59 +00:00
|
|
|
ArgumentNullException.ThrowIfNull(model.BeatmapInfo);
|
|
|
|
ArgumentNullException.ThrowIfNull(model.Ruleset);
|
2022-01-17 05:02:15 +00:00
|
|
|
|
2022-09-09 04:57:01 +00:00
|
|
|
PopulateMaximumStatistics(model);
|
2022-09-08 13:06:44 +00:00
|
|
|
|
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);
|
2023-06-15 19:48:57 +00:00
|
|
|
|
|
|
|
// for pre-ScoreV2 lazer scores, apply a best-effort conversion of total score to ScoreV2.
|
|
|
|
// this requires: max combo, statistics, max statistics (where available), and mods to already be populated on the score.
|
|
|
|
if (StandardisedScoreMigrationTools.ShouldMigrateToNewStandardised(model))
|
|
|
|
model.TotalScore = StandardisedScoreMigrationTools.GetNewStandardised(model);
|
2023-06-26 13:19:01 +00:00
|
|
|
else if (model.IsLegacyScore)
|
2023-06-29 08:21:24 +00:00
|
|
|
{
|
|
|
|
model.LegacyTotalScore = model.TotalScore;
|
2023-06-28 06:04:13 +00:00
|
|
|
model.TotalScore = StandardisedScoreMigrationTools.ConvertFromLegacyTotalScore(model, beatmaps());
|
2023-06-29 08:21:24 +00:00
|
|
|
}
|
2022-01-07 08:27:48 +00:00
|
|
|
}
|
2022-07-08 03:16:06 +00:00
|
|
|
|
2022-09-08 13:06:44 +00:00
|
|
|
/// <summary>
|
|
|
|
/// Populates the <see cref="ScoreInfo.MaximumStatistics"/> for a given <see cref="ScoreInfo"/>.
|
|
|
|
/// </summary>
|
|
|
|
/// <param name="score">The score to populate the statistics of.</param>
|
2022-09-09 04:57:01 +00:00
|
|
|
public void PopulateMaximumStatistics(ScoreInfo score)
|
2022-09-08 13:06:44 +00:00
|
|
|
{
|
2023-07-05 07:07:12 +00:00
|
|
|
Debug.Assert(score.BeatmapInfo != null);
|
|
|
|
|
2022-09-08 13:06:44 +00:00
|
|
|
if (score.MaximumStatistics.Select(kvp => kvp.Value).Sum() > 0)
|
|
|
|
return;
|
|
|
|
|
2023-07-05 07:07:12 +00:00
|
|
|
var beatmap = score.BeatmapInfo!.Detach();
|
2022-09-08 13:06:44 +00:00
|
|
|
var ruleset = score.Ruleset.Detach();
|
2022-09-09 04:57:01 +00:00
|
|
|
var rulesetInstance = ruleset.CreateInstance();
|
|
|
|
|
|
|
|
Debug.Assert(rulesetInstance != null);
|
2022-09-08 13:06:44 +00:00
|
|
|
|
|
|
|
// Populate the maximum statistics.
|
2022-09-09 04:57:01 +00:00
|
|
|
HitResult maxBasicResult = rulesetInstance.GetHitResults()
|
|
|
|
.Select(h => h.result)
|
2022-12-19 07:42:21 +00:00
|
|
|
.Where(h => h.IsBasic()).MaxBy(Judgement.ToNumericResult);
|
2022-09-08 13:06:44 +00:00
|
|
|
|
|
|
|
foreach ((HitResult result, int count) in score.Statistics)
|
|
|
|
{
|
|
|
|
switch (result)
|
|
|
|
{
|
|
|
|
case HitResult.LargeTickHit:
|
|
|
|
case HitResult.LargeTickMiss:
|
|
|
|
score.MaximumStatistics[HitResult.LargeTickHit] = score.MaximumStatistics.GetValueOrDefault(HitResult.LargeTickHit) + count;
|
|
|
|
break;
|
|
|
|
|
|
|
|
case HitResult.SmallTickHit:
|
|
|
|
case HitResult.SmallTickMiss:
|
|
|
|
score.MaximumStatistics[HitResult.SmallTickHit] = score.MaximumStatistics.GetValueOrDefault(HitResult.SmallTickHit) + count;
|
|
|
|
break;
|
|
|
|
|
|
|
|
case HitResult.IgnoreHit:
|
|
|
|
case HitResult.IgnoreMiss:
|
|
|
|
case HitResult.SmallBonus:
|
|
|
|
case HitResult.LargeBonus:
|
|
|
|
break;
|
|
|
|
|
|
|
|
default:
|
|
|
|
score.MaximumStatistics[maxBasicResult] = score.MaximumStatistics.GetValueOrDefault(maxBasicResult) + count;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!score.IsLegacyScore)
|
|
|
|
return;
|
|
|
|
|
|
|
|
#pragma warning disable CS0618
|
|
|
|
// In osu! and osu!mania, some judgements affect combo but aren't stored to scores.
|
|
|
|
// A special hit result is used to pad out the combo value to match, based on the max combo from the difficulty attributes.
|
2022-09-09 04:57:01 +00:00
|
|
|
var calculator = rulesetInstance.CreateDifficultyCalculator(beatmaps().GetWorkingBeatmap(beatmap));
|
|
|
|
var attributes = calculator.Calculate(score.Mods);
|
2022-09-08 13:06:44 +00:00
|
|
|
|
|
|
|
int maxComboFromStatistics = score.MaximumStatistics.Where(kvp => kvp.Key.AffectsCombo()).Select(kvp => kvp.Value).DefaultIfEmpty(0).Sum();
|
2022-09-09 04:57:01 +00:00
|
|
|
if (attributes.MaxCombo > maxComboFromStatistics)
|
|
|
|
score.MaximumStatistics[HitResult.LegacyComboIncrease] = attributes.MaxCombo - maxComboFromStatistics;
|
2022-09-08 13:06:44 +00:00
|
|
|
#pragma warning restore CS0618
|
|
|
|
}
|
|
|
|
|
2023-06-09 10:03:02 +00:00
|
|
|
// Very naive local caching to improve performance of large score imports (where the username is usually the same for most or all scores).
|
|
|
|
private readonly Dictionary<string, APIUser> usernameLookupCache = new Dictionary<string, APIUser>();
|
|
|
|
|
2022-12-12 14:56:11 +00:00
|
|
|
protected override void PostImport(ScoreInfo model, Realm realm, ImportParameters parameters)
|
2022-07-08 03:16:06 +00:00
|
|
|
{
|
2022-12-12 14:56:11 +00:00
|
|
|
base.PostImport(model, realm, parameters);
|
2022-07-08 03:16:06 +00:00
|
|
|
|
2023-06-09 10:03:02 +00:00
|
|
|
populateUserDetails(model);
|
|
|
|
}
|
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
/// Legacy replays only store a username.
|
|
|
|
/// This will populate a user ID during import.
|
|
|
|
/// </summary>
|
|
|
|
private void populateUserDetails(ScoreInfo model)
|
|
|
|
{
|
|
|
|
string username = model.RealmUser.Username;
|
|
|
|
|
|
|
|
if (usernameLookupCache.TryGetValue(username, out var existing))
|
|
|
|
{
|
|
|
|
model.User = existing;
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
var userRequest = new GetUserRequest(username);
|
2022-07-11 18:46:42 +00:00
|
|
|
|
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)
|
2023-06-09 10:03:02 +00:00
|
|
|
{
|
|
|
|
usernameLookupCache.TryAdd(username, new APIUser
|
|
|
|
{
|
|
|
|
// Because this is a permanent cache, let's only store the pieces we're interested in,
|
|
|
|
// rather than the full API response. If we start to store more than these three fields
|
|
|
|
// in realm, this should be undone.
|
|
|
|
Id = user.Id,
|
|
|
|
Username = user.Username,
|
|
|
|
CountryCode = user.CountryCode,
|
|
|
|
});
|
|
|
|
|
2022-07-16 03:31:01 +00:00
|
|
|
model.User = user;
|
2023-06-09 10:03:02 +00:00
|
|
|
}
|
2022-07-08 03:16:06 +00:00
|
|
|
}
|
2021-09-30 09:21:16 +00:00
|
|
|
}
|
|
|
|
}
|