2021-09-30 14:45:09 +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.IO;
|
|
|
|
using System.Linq;
|
|
|
|
using System.Threading;
|
|
|
|
using System.Threading.Tasks;
|
|
|
|
using NuGet.Packaging;
|
|
|
|
using osu.Framework.Audio.Track;
|
|
|
|
using osu.Framework.Extensions;
|
|
|
|
using osu.Framework.Extensions.IEnumerableExtensions;
|
|
|
|
using osu.Framework.Graphics.Textures;
|
|
|
|
using osu.Framework.Logging;
|
|
|
|
using osu.Framework.Platform;
|
|
|
|
using osu.Framework.Testing;
|
|
|
|
using osu.Game.Beatmaps;
|
|
|
|
using osu.Game.Beatmaps.Formats;
|
|
|
|
using osu.Game.Database;
|
|
|
|
using osu.Game.IO;
|
|
|
|
using osu.Game.IO.Archives;
|
|
|
|
using osu.Game.Models;
|
|
|
|
using osu.Game.Rulesets;
|
|
|
|
using osu.Game.Rulesets.Objects;
|
|
|
|
using osu.Game.Skinning;
|
|
|
|
using Realms;
|
|
|
|
|
|
|
|
#nullable enable
|
|
|
|
|
|
|
|
namespace osu.Game.Stores
|
|
|
|
{
|
|
|
|
/// <summary>
|
|
|
|
/// Handles the storage and retrieval of Beatmaps/WorkingBeatmaps.
|
|
|
|
/// </summary>
|
|
|
|
[ExcludeFromDynamicCompile]
|
2021-10-15 08:08:43 +00:00
|
|
|
public class BeatmapImporter : RealmArchiveModelImporter<RealmBeatmapSet>, IDisposable
|
2021-09-30 14:45:09 +00:00
|
|
|
{
|
|
|
|
public override IEnumerable<string> HandledExtensions => new[] { ".osz" };
|
|
|
|
|
|
|
|
protected override string[] HashableFileTypes => new[] { ".osu" };
|
|
|
|
|
|
|
|
// protected override bool CheckLocalAvailability(RealmBeatmapSet model, System.Linq.IQueryable<RealmBeatmapSet> items)
|
2021-10-18 07:10:37 +00:00
|
|
|
// => base.CheckLocalAvailability(model, items) || (model.OnlineID > -1));
|
2021-09-30 14:45:09 +00:00
|
|
|
|
2021-10-18 05:43:48 +00:00
|
|
|
private readonly BeatmapOnlineLookupQueue? onlineLookupQueue;
|
2021-09-30 14:45:09 +00:00
|
|
|
|
2021-10-18 05:43:48 +00:00
|
|
|
public BeatmapImporter(RealmContextFactory contextFactory, Storage storage, BeatmapOnlineLookupQueue? onlineLookupQueue = null)
|
2021-09-30 14:45:09 +00:00
|
|
|
: base(storage, contextFactory)
|
|
|
|
{
|
2021-10-18 05:43:48 +00:00
|
|
|
this.onlineLookupQueue = onlineLookupQueue;
|
2021-09-30 14:45:09 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
protected override bool ShouldDeleteArchive(string path) => Path.GetExtension(path).ToLowerInvariant() == ".osz";
|
|
|
|
|
2021-10-18 05:43:48 +00:00
|
|
|
protected override Task Populate(RealmBeatmapSet beatmapSet, ArchiveReader? archive, Realm realm, CancellationToken cancellationToken = default)
|
2021-09-30 14:45:09 +00:00
|
|
|
{
|
|
|
|
if (archive != null)
|
|
|
|
beatmapSet.Beatmaps.AddRange(createBeatmapDifficulties(beatmapSet.Files, realm));
|
|
|
|
|
|
|
|
foreach (RealmBeatmap b in beatmapSet.Beatmaps)
|
|
|
|
b.BeatmapSet = beatmapSet;
|
|
|
|
|
|
|
|
validateOnlineIds(beatmapSet, realm);
|
|
|
|
|
|
|
|
bool hadOnlineBeatmapIDs = beatmapSet.Beatmaps.Any(b => b.OnlineID > 0);
|
|
|
|
|
|
|
|
if (onlineLookupQueue != null)
|
2021-10-18 05:43:48 +00:00
|
|
|
{
|
|
|
|
// TODO: this required `BeatmapOnlineLookupQueue` to somehow support new types.
|
|
|
|
// await onlineLookupQueue.UpdateAsync(beatmapSet, cancellationToken).ConfigureAwait(false);
|
|
|
|
}
|
2021-09-30 14:45:09 +00:00
|
|
|
|
|
|
|
// ensure at least one beatmap was able to retrieve or keep an online ID, else drop the set ID.
|
|
|
|
if (hadOnlineBeatmapIDs && !beatmapSet.Beatmaps.Any(b => b.OnlineID > 0))
|
|
|
|
{
|
2021-10-20 06:20:30 +00:00
|
|
|
if (beatmapSet.OnlineID > 0)
|
2021-09-30 14:45:09 +00:00
|
|
|
{
|
2021-10-18 07:10:37 +00:00
|
|
|
beatmapSet.OnlineID = -1;
|
2021-09-30 14:45:09 +00:00
|
|
|
LogForModel(beatmapSet, "Disassociating beatmap set ID due to loss of all beatmap IDs");
|
|
|
|
}
|
|
|
|
}
|
2021-10-18 05:43:48 +00:00
|
|
|
|
|
|
|
return Task.CompletedTask;
|
2021-09-30 14:45:09 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
protected override void PreImport(RealmBeatmapSet beatmapSet, Realm realm)
|
|
|
|
{
|
2021-10-18 06:06:09 +00:00
|
|
|
// We are about to import a new beatmap. Before doing so, ensure that no other set shares the online IDs used by the new one.
|
|
|
|
// Note that this means if the previous beatmap is restored by the user, it will no longer be linked to its online IDs.
|
|
|
|
// 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).
|
|
|
|
|
2021-10-20 06:20:30 +00:00
|
|
|
if (beatmapSet.OnlineID > 0)
|
2021-09-30 14:45:09 +00:00
|
|
|
{
|
2021-10-19 02:37:46 +00:00
|
|
|
var existingSetWithSameOnlineID = realm.All<RealmBeatmapSet>().SingleOrDefault(b => b.OnlineID == beatmapSet.OnlineID);
|
2021-09-30 14:45:09 +00:00
|
|
|
|
2021-10-19 02:37:46 +00:00
|
|
|
if (existingSetWithSameOnlineID != null)
|
2021-09-30 14:45:09 +00:00
|
|
|
{
|
2021-10-19 02:37:46 +00:00
|
|
|
existingSetWithSameOnlineID.DeletePending = true;
|
|
|
|
existingSetWithSameOnlineID.OnlineID = -1;
|
2021-10-18 06:06:09 +00:00
|
|
|
|
2021-10-19 02:37:46 +00:00
|
|
|
foreach (var b in existingSetWithSameOnlineID.Beatmaps)
|
2021-10-18 07:10:37 +00:00
|
|
|
b.OnlineID = -1;
|
2021-09-30 14:45:09 +00:00
|
|
|
|
2021-10-18 06:06:09 +00:00
|
|
|
LogForModel(beatmapSet, $"Found existing beatmap set with same OnlineID ({beatmapSet.OnlineID}). It will be deleted.");
|
2021-09-30 14:45:09 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
private void validateOnlineIds(RealmBeatmapSet beatmapSet, Realm realm)
|
|
|
|
{
|
2021-10-20 06:20:30 +00:00
|
|
|
var beatmapIds = beatmapSet.Beatmaps.Where(b => b.OnlineID > 0).Select(b => b.OnlineID).ToList();
|
2021-09-30 14:45:09 +00:00
|
|
|
|
|
|
|
// ensure all IDs are unique
|
|
|
|
if (beatmapIds.GroupBy(b => b).Any(g => g.Count() > 1))
|
|
|
|
{
|
|
|
|
LogForModel(beatmapSet, "Found non-unique IDs, resetting...");
|
|
|
|
resetIds();
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
// find any existing beatmaps in the database that have matching online ids
|
|
|
|
List<RealmBeatmap> existingBeatmaps = new List<RealmBeatmap>();
|
|
|
|
|
2021-10-27 04:04:41 +00:00
|
|
|
foreach (int id in beatmapIds)
|
2021-09-30 14:45:09 +00:00
|
|
|
existingBeatmaps.AddRange(realm.All<RealmBeatmap>().Where(b => b.OnlineID == id));
|
|
|
|
|
|
|
|
if (existingBeatmaps.Any())
|
|
|
|
{
|
|
|
|
// reset the import ids (to force a re-fetch) *unless* they match the candidate CheckForExisting set.
|
|
|
|
// we can ignore the case where the new ids are contained by the CheckForExisting set as it will either be used (import skipped) or deleted.
|
|
|
|
|
|
|
|
var existing = CheckForExisting(beatmapSet, realm);
|
|
|
|
|
|
|
|
if (existing == null || existingBeatmaps.Any(b => !existing.Beatmaps.Contains(b)))
|
|
|
|
{
|
|
|
|
LogForModel(beatmapSet, "Found existing import with online IDs already, resetting...");
|
|
|
|
resetIds();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-10-18 07:10:37 +00:00
|
|
|
void resetIds() => beatmapSet.Beatmaps.ForEach(b => b.OnlineID = -1);
|
2021-09-30 14:45:09 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
protected override bool CanSkipImport(RealmBeatmapSet existing, RealmBeatmapSet import)
|
|
|
|
{
|
|
|
|
if (!base.CanSkipImport(existing, import))
|
|
|
|
return false;
|
|
|
|
|
2021-10-20 06:20:30 +00:00
|
|
|
return existing.Beatmaps.Any(b => b.OnlineID > 0);
|
2021-09-30 14:45:09 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
protected override bool CanReuseExisting(RealmBeatmapSet existing, RealmBeatmapSet import)
|
|
|
|
{
|
|
|
|
if (!base.CanReuseExisting(existing, import))
|
|
|
|
return false;
|
|
|
|
|
|
|
|
var existingIds = existing.Beatmaps.Select(b => b.OnlineID).OrderBy(i => i);
|
|
|
|
var importIds = import.Beatmaps.Select(b => b.OnlineID).OrderBy(i => i);
|
|
|
|
|
|
|
|
// force re-import if we are not in a sane state.
|
|
|
|
return existing.OnlineID == import.OnlineID && existingIds.SequenceEqual(importIds);
|
|
|
|
}
|
|
|
|
|
|
|
|
public override string HumanisedModelName => "beatmap";
|
|
|
|
|
|
|
|
protected override RealmBeatmapSet? CreateModel(ArchiveReader reader)
|
|
|
|
{
|
|
|
|
// let's make sure there are actually .osu files to import.
|
|
|
|
string? mapName = reader.Filenames.FirstOrDefault(f => f.EndsWith(".osu", StringComparison.OrdinalIgnoreCase));
|
|
|
|
|
|
|
|
if (string.IsNullOrEmpty(mapName))
|
|
|
|
{
|
|
|
|
Logger.Log($"No beatmap files found in the beatmap archive ({reader.Name}).", LoggingTarget.Database);
|
|
|
|
return null;
|
|
|
|
}
|
|
|
|
|
|
|
|
Beatmap beatmap;
|
|
|
|
using (var stream = new LineBufferedReader(reader.GetStream(mapName)))
|
|
|
|
beatmap = Decoder.GetDecoder<Beatmap>(stream).Decode(stream);
|
|
|
|
|
|
|
|
return new RealmBeatmapSet
|
|
|
|
{
|
2021-10-18 07:10:37 +00:00
|
|
|
OnlineID = beatmap.BeatmapInfo.BeatmapSet?.OnlineBeatmapSetID ?? -1,
|
2021-09-30 14:45:09 +00:00
|
|
|
// Metadata = beatmap.Metadata,
|
|
|
|
DateAdded = DateTimeOffset.UtcNow
|
|
|
|
};
|
|
|
|
}
|
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
/// Create all required <see cref="RealmBeatmap"/>s for the provided archive.
|
|
|
|
/// </summary>
|
|
|
|
private List<RealmBeatmap> createBeatmapDifficulties(IList<RealmNamedFileUsage> files, Realm realm)
|
|
|
|
{
|
|
|
|
var beatmaps = new List<RealmBeatmap>();
|
|
|
|
|
|
|
|
foreach (var file in files.Where(f => f.Filename.EndsWith(".osu", StringComparison.OrdinalIgnoreCase)))
|
|
|
|
{
|
2021-10-11 05:33:47 +00:00
|
|
|
using (var memoryStream = new MemoryStream(Files.Store.Get(file.File.StoragePath))) // we need a memory stream so we can seek
|
2021-09-30 14:45:09 +00:00
|
|
|
{
|
2021-10-11 05:33:47 +00:00
|
|
|
IBeatmap decoded;
|
|
|
|
using (var lineReader = new LineBufferedReader(memoryStream, true))
|
|
|
|
decoded = Decoder.GetDecoder<Beatmap>(lineReader).Decode(lineReader);
|
2021-09-30 14:45:09 +00:00
|
|
|
|
2021-10-11 05:33:47 +00:00
|
|
|
string hash = memoryStream.ComputeSHA2Hash();
|
2021-09-30 14:45:09 +00:00
|
|
|
|
|
|
|
if (beatmaps.Any(b => b.Hash == hash))
|
2021-10-11 05:33:47 +00:00
|
|
|
{
|
|
|
|
Logger.Log($"Skipping import of {file.Filename} due to duplicate file content.", LoggingTarget.Database);
|
2021-09-30 14:45:09 +00:00
|
|
|
continue;
|
2021-10-11 05:33:47 +00:00
|
|
|
}
|
2021-09-30 14:45:09 +00:00
|
|
|
|
2021-10-11 05:33:47 +00:00
|
|
|
var decodedInfo = decoded.BeatmapInfo;
|
|
|
|
var decodedDifficulty = decodedInfo.BaseDifficulty;
|
2021-09-30 14:45:09 +00:00
|
|
|
|
2021-10-11 05:33:47 +00:00
|
|
|
var ruleset = realm.All<RealmRuleset>().FirstOrDefault(r => r.OnlineID == decodedInfo.RulesetID);
|
2021-09-30 14:45:09 +00:00
|
|
|
|
2021-10-11 05:33:47 +00:00
|
|
|
if (ruleset?.Available != true)
|
2021-09-30 14:45:09 +00:00
|
|
|
{
|
2021-10-11 05:33:47 +00:00
|
|
|
Logger.Log($"Skipping import of {file.Filename} due to missing local ruleset {decodedInfo.RulesetID}.", LoggingTarget.Database);
|
2021-09-30 14:45:09 +00:00
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
|
|
|
var difficulty = new RealmBeatmapDifficulty
|
|
|
|
{
|
2021-10-11 05:33:47 +00:00
|
|
|
DrainRate = decodedDifficulty.DrainRate,
|
|
|
|
CircleSize = decodedDifficulty.CircleSize,
|
|
|
|
OverallDifficulty = decodedDifficulty.OverallDifficulty,
|
|
|
|
ApproachRate = decodedDifficulty.ApproachRate,
|
|
|
|
SliderMultiplier = decodedDifficulty.SliderMultiplier,
|
|
|
|
SliderTickRate = decodedDifficulty.SliderTickRate,
|
2021-09-30 14:45:09 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
var metadata = new RealmBeatmapMetadata
|
|
|
|
{
|
2021-10-11 05:33:47 +00:00
|
|
|
Title = decoded.Metadata.Title,
|
|
|
|
TitleUnicode = decoded.Metadata.TitleUnicode,
|
|
|
|
Artist = decoded.Metadata.Artist,
|
|
|
|
ArtistUnicode = decoded.Metadata.ArtistUnicode,
|
2021-11-04 09:22:21 +00:00
|
|
|
Author =
|
|
|
|
{
|
2021-11-04 09:46:26 +00:00
|
|
|
OnlineID = decoded.Metadata.Author.Id,
|
|
|
|
Username = decoded.Metadata.Author.Username
|
2021-11-04 09:22:21 +00:00
|
|
|
},
|
2021-10-11 05:33:47 +00:00
|
|
|
Source = decoded.Metadata.Source,
|
|
|
|
Tags = decoded.Metadata.Tags,
|
|
|
|
PreviewTime = decoded.Metadata.PreviewTime,
|
|
|
|
AudioFile = decoded.Metadata.AudioFile,
|
|
|
|
BackgroundFile = decoded.Metadata.BackgroundFile,
|
2021-09-30 14:45:09 +00:00
|
|
|
};
|
|
|
|
|
2021-10-11 05:33:47 +00:00
|
|
|
var beatmap = new RealmBeatmap(ruleset, difficulty, metadata)
|
2021-09-30 14:45:09 +00:00
|
|
|
{
|
2021-10-11 05:33:47 +00:00
|
|
|
Hash = hash,
|
|
|
|
DifficultyName = decodedInfo.Version,
|
2021-10-18 07:10:37 +00:00
|
|
|
OnlineID = decodedInfo.OnlineBeatmapID ?? -1,
|
2021-10-11 05:33:47 +00:00
|
|
|
AudioLeadIn = decodedInfo.AudioLeadIn,
|
|
|
|
StackLeniency = decodedInfo.StackLeniency,
|
|
|
|
SpecialStyle = decodedInfo.SpecialStyle,
|
|
|
|
LetterboxInBreaks = decodedInfo.LetterboxInBreaks,
|
|
|
|
WidescreenStoryboard = decodedInfo.WidescreenStoryboard,
|
|
|
|
EpilepsyWarning = decodedInfo.EpilepsyWarning,
|
|
|
|
SamplesMatchPlaybackRate = decodedInfo.SamplesMatchPlaybackRate,
|
|
|
|
DistanceSpacing = decodedInfo.DistanceSpacing,
|
|
|
|
BeatDivisor = decodedInfo.BeatDivisor,
|
|
|
|
GridSize = decodedInfo.GridSize,
|
|
|
|
TimelineZoom = decodedInfo.TimelineZoom,
|
|
|
|
MD5Hash = memoryStream.ComputeMD5Hash(),
|
2021-09-30 14:45:09 +00:00
|
|
|
};
|
|
|
|
|
2021-10-11 05:33:47 +00:00
|
|
|
updateBeatmapStatistics(beatmap, decoded);
|
2021-09-30 14:45:09 +00:00
|
|
|
|
2021-10-11 05:33:47 +00:00
|
|
|
beatmaps.Add(beatmap);
|
2021-09-30 14:45:09 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return beatmaps;
|
|
|
|
}
|
|
|
|
|
2021-10-11 05:33:47 +00:00
|
|
|
private void updateBeatmapStatistics(RealmBeatmap beatmap, IBeatmap decoded)
|
2021-09-30 14:45:09 +00:00
|
|
|
{
|
2021-10-11 05:33:47 +00:00
|
|
|
var rulesetInstance = ((IRulesetInfo)beatmap.Ruleset).CreateInstance();
|
|
|
|
|
|
|
|
if (rulesetInstance == null)
|
|
|
|
return;
|
|
|
|
|
|
|
|
decoded.BeatmapInfo.Ruleset = rulesetInstance.RulesetInfo;
|
|
|
|
|
|
|
|
// TODO: this should be done in a better place once we actually need to dynamically update it.
|
|
|
|
beatmap.StarRating = rulesetInstance.CreateDifficultyCalculator(new DummyConversionBeatmap(decoded)).Calculate().StarRating;
|
|
|
|
beatmap.Length = calculateLength(decoded);
|
|
|
|
beatmap.BPM = 60000 / decoded.GetMostCommonBeatLength();
|
2021-09-30 14:45:09 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
private double calculateLength(IBeatmap b)
|
|
|
|
{
|
|
|
|
if (!b.HitObjects.Any())
|
|
|
|
return 0;
|
|
|
|
|
|
|
|
var lastObject = b.HitObjects.Last();
|
|
|
|
|
|
|
|
//TODO: this isn't always correct (consider mania where a non-last object may last for longer than the last in the list).
|
|
|
|
double endTime = lastObject.GetEndTime();
|
|
|
|
double startTime = b.HitObjects.First().StartTime;
|
|
|
|
|
|
|
|
return endTime - startTime;
|
|
|
|
}
|
|
|
|
|
2021-10-11 05:33:47 +00:00
|
|
|
public void Dispose()
|
|
|
|
{
|
|
|
|
onlineLookupQueue?.Dispose();
|
|
|
|
}
|
|
|
|
|
2021-09-30 14:45:09 +00:00
|
|
|
/// <summary>
|
|
|
|
/// A dummy WorkingBeatmap for the purpose of retrieving a beatmap for star difficulty calculation.
|
|
|
|
/// </summary>
|
|
|
|
private class DummyConversionBeatmap : WorkingBeatmap
|
|
|
|
{
|
|
|
|
private readonly IBeatmap beatmap;
|
|
|
|
|
|
|
|
public DummyConversionBeatmap(IBeatmap beatmap)
|
|
|
|
: base(beatmap.BeatmapInfo, null)
|
|
|
|
{
|
|
|
|
this.beatmap = beatmap;
|
|
|
|
}
|
|
|
|
|
|
|
|
protected override IBeatmap GetBeatmap() => beatmap;
|
|
|
|
protected override Texture? GetBackground() => null;
|
|
|
|
protected override Track? GetBeatmapTrack() => null;
|
|
|
|
protected internal override ISkin? GetSkin() => null;
|
|
|
|
public override Stream? GetStream(string storagePath) => null;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|