2022-07-27 06:04: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.Tasks;
|
|
|
|
using osu.Framework.Logging;
|
2022-07-27 06:59:36 +00:00
|
|
|
using osu.Game.Beatmaps;
|
2022-07-27 06:04:09 +00:00
|
|
|
using osu.Game.IO;
|
|
|
|
using osu.Game.IO.Legacy;
|
|
|
|
using osu.Game.Overlays.Notifications;
|
|
|
|
|
|
|
|
namespace osu.Game.Database
|
|
|
|
{
|
|
|
|
public class LegacyCollectionImporter
|
|
|
|
{
|
2022-07-27 06:59:36 +00:00
|
|
|
public Action<Notification>? PostNotification { protected get; set; }
|
2022-07-27 06:04:09 +00:00
|
|
|
|
2022-07-27 06:59:36 +00:00
|
|
|
private readonly RealmAccess realm;
|
2022-07-27 06:04:09 +00:00
|
|
|
|
|
|
|
private const string database_name = "collection.db";
|
|
|
|
|
2022-07-27 06:59:36 +00:00
|
|
|
public LegacyCollectionImporter(RealmAccess realm)
|
|
|
|
{
|
|
|
|
this.realm = realm;
|
|
|
|
}
|
|
|
|
|
2022-07-27 06:04:09 +00:00
|
|
|
public Task<int> GetAvailableCount(StableStorage stableStorage)
|
|
|
|
{
|
|
|
|
if (!stableStorage.Exists(database_name))
|
|
|
|
return Task.FromResult(0);
|
|
|
|
|
|
|
|
return Task.Run(() =>
|
|
|
|
{
|
|
|
|
using (var stream = stableStorage.GetStream(database_name))
|
|
|
|
return readCollections(stream).Count;
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
/// This is a temporary method and will likely be replaced by a full-fledged (and more correctly placed) migration process in the future.
|
|
|
|
/// </summary>
|
|
|
|
public Task ImportFromStableAsync(StableStorage stableStorage)
|
|
|
|
{
|
|
|
|
if (!stableStorage.Exists(database_name))
|
|
|
|
{
|
|
|
|
// This handles situations like when the user does not have a collections.db file
|
|
|
|
Logger.Log($"No {database_name} available in osu!stable installation", LoggingTarget.Information, LogLevel.Error);
|
|
|
|
return Task.CompletedTask;
|
|
|
|
}
|
|
|
|
|
|
|
|
return Task.Run(async () =>
|
|
|
|
{
|
|
|
|
using (var stream = stableStorage.GetStream(database_name))
|
|
|
|
await Import(stream).ConfigureAwait(false);
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
public async Task Import(Stream stream)
|
|
|
|
{
|
|
|
|
var notification = new ProgressNotification
|
|
|
|
{
|
|
|
|
State = ProgressNotificationState.Active,
|
|
|
|
Text = "Collections import is initialising..."
|
|
|
|
};
|
|
|
|
|
|
|
|
PostNotification?.Invoke(notification);
|
|
|
|
|
|
|
|
var importedCollections = readCollections(stream, notification);
|
|
|
|
await importCollections(importedCollections).ConfigureAwait(false);
|
|
|
|
|
|
|
|
notification.CompletionText = $"Imported {importedCollections.Count} collections";
|
|
|
|
notification.State = ProgressNotificationState.Completed;
|
|
|
|
}
|
|
|
|
|
2022-07-27 06:59:36 +00:00
|
|
|
private Task importCollections(List<RealmBeatmapCollection> newCollections)
|
2022-07-27 06:04:09 +00:00
|
|
|
{
|
|
|
|
var tcs = new TaskCompletionSource<bool>();
|
|
|
|
|
|
|
|
try
|
|
|
|
{
|
2022-07-27 06:59:36 +00:00
|
|
|
realm.Write(r =>
|
2022-07-27 06:04:09 +00:00
|
|
|
{
|
2022-07-27 06:59:36 +00:00
|
|
|
foreach (var collection in newCollections)
|
2022-07-27 06:04:09 +00:00
|
|
|
{
|
2022-07-27 06:59:36 +00:00
|
|
|
var existing = r.All<RealmBeatmapCollection>().FirstOrDefault(c => c.Name == collection.Name);
|
|
|
|
|
|
|
|
if (existing != null)
|
|
|
|
{
|
|
|
|
foreach (string newBeatmap in existing.BeatmapMD5Hashes)
|
|
|
|
{
|
|
|
|
if (!existing.BeatmapMD5Hashes.Contains(newBeatmap))
|
|
|
|
existing.BeatmapMD5Hashes.Add(newBeatmap);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
else
|
|
|
|
r.Add(collection);
|
2022-07-27 06:04:09 +00:00
|
|
|
}
|
2022-07-27 06:59:36 +00:00
|
|
|
});
|
2022-07-27 06:04:09 +00:00
|
|
|
|
|
|
|
tcs.SetResult(true);
|
|
|
|
}
|
|
|
|
catch (Exception e)
|
|
|
|
{
|
|
|
|
Logger.Error(e, "Failed to import collection.");
|
|
|
|
tcs.SetException(e);
|
|
|
|
}
|
|
|
|
|
|
|
|
return tcs.Task;
|
|
|
|
}
|
|
|
|
|
2022-07-27 06:59:36 +00:00
|
|
|
private List<RealmBeatmapCollection> readCollections(Stream stream, ProgressNotification? notification = null)
|
2022-07-27 06:04:09 +00:00
|
|
|
{
|
|
|
|
if (notification != null)
|
|
|
|
{
|
|
|
|
notification.Text = "Reading collections...";
|
|
|
|
notification.Progress = 0;
|
|
|
|
}
|
|
|
|
|
2022-07-27 06:59:36 +00:00
|
|
|
var result = new List<RealmBeatmapCollection>();
|
2022-07-27 06:04:09 +00:00
|
|
|
|
|
|
|
try
|
|
|
|
{
|
|
|
|
using (var sr = new SerializationReader(stream))
|
|
|
|
{
|
|
|
|
sr.ReadInt32(); // Version
|
|
|
|
|
|
|
|
int collectionCount = sr.ReadInt32();
|
|
|
|
result.Capacity = collectionCount;
|
|
|
|
|
|
|
|
for (int i = 0; i < collectionCount; i++)
|
|
|
|
{
|
|
|
|
if (notification?.CancellationToken.IsCancellationRequested == true)
|
|
|
|
return result;
|
|
|
|
|
2022-07-27 06:59:36 +00:00
|
|
|
var collection = new RealmBeatmapCollection(sr.ReadString());
|
2022-07-27 06:04:09 +00:00
|
|
|
int mapCount = sr.ReadInt32();
|
|
|
|
|
|
|
|
for (int j = 0; j < mapCount; j++)
|
|
|
|
{
|
|
|
|
if (notification?.CancellationToken.IsCancellationRequested == true)
|
|
|
|
return result;
|
|
|
|
|
|
|
|
string checksum = sr.ReadString();
|
|
|
|
|
2022-07-27 06:59:36 +00:00
|
|
|
collection.BeatmapMD5Hashes.Add(checksum);
|
2022-07-27 06:04:09 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
if (notification != null)
|
|
|
|
{
|
|
|
|
notification.Text = $"Imported {i + 1} of {collectionCount} collections";
|
|
|
|
notification.Progress = (float)(i + 1) / collectionCount;
|
|
|
|
}
|
|
|
|
|
|
|
|
result.Add(collection);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
catch (Exception e)
|
|
|
|
{
|
|
|
|
Logger.Error(e, "Failed to read collection database.");
|
|
|
|
}
|
|
|
|
|
|
|
|
return result;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|