1
0
mirror of https://github.com/ppy/osu.git synced 2024-11-11 18:27:26 +08:00
osu-lazer/osu.Game/Collections/CollectionManager.cs

112 lines
3.9 KiB
C#
Raw Normal View History

2020-09-01 16:28:41 +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.IO;
2020-09-01 18:33:06 +08:00
using osu.Framework.Allocation;
using osu.Framework.Bindables;
using osu.Framework.Graphics.Containers;
2020-09-01 16:28:41 +08:00
using osu.Framework.Platform;
using osu.Game.Beatmaps;
using osu.Game.IO.Legacy;
namespace osu.Game.Collections
{
2020-09-01 18:33:06 +08:00
public class CollectionManager : CompositeDrawable
2020-09-01 16:28:41 +08:00
{
2020-09-01 18:33:06 +08:00
private const string database_name = "collection.db";
2020-09-01 16:28:41 +08:00
2020-09-01 18:33:06 +08:00
public IBindableList<BeatmapCollection> Collections => collections;
private readonly BindableList<BeatmapCollection> collections = new BindableList<BeatmapCollection>();
2020-09-01 16:28:41 +08:00
2020-09-01 18:33:06 +08:00
[Resolved]
private BeatmapManager beatmaps { get; set; }
[BackgroundDependencyLoader]
private void load(GameHost host)
2020-09-01 16:28:41 +08:00
{
2020-09-01 18:33:06 +08:00
if (host.Storage.Exists(database_name))
{
using (var stream = host.Storage.GetStream(database_name))
collections.AddRange(readCollection(stream));
}
2020-09-01 16:28:41 +08:00
}
/// <summary>
/// Set a storage with access to an osu-stable install for import purposes.
/// </summary>
public Func<Storage> GetStableStorage { private get; set; }
/// <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>
2020-09-01 18:33:06 +08:00
// public Task ImportFromStableAsync()
// {
// var stable = GetStableStorage?.Invoke();
//
// if (stable == null)
// {
// Logger.Log("No osu!stable installation available!", LoggingTarget.Information, LogLevel.Error);
// return Task.CompletedTask;
// }
//
// if (!stable.ExistsDirectory(database_name))
// {
// // This handles situations like when the user does not have a Skins folder
// Logger.Log($"No {database_name} folder available in osu!stable installation", LoggingTarget.Information, LogLevel.Error);
// return Task.CompletedTask;
// }
//
// return Task.Run(async () => await Import(GetStableImportPaths(GetStableStorage()).Select(f => stable.GetFullPath(f)).ToArray()));
// }
2020-09-01 16:28:41 +08:00
private List<BeatmapCollection> readCollection(Stream stream)
{
var result = new List<BeatmapCollection>();
using (var reader = new SerializationReader(stream))
{
reader.ReadInt32(); // Version
int collectionCount = reader.ReadInt32();
result.Capacity = collectionCount;
for (int i = 0; i < collectionCount; i++)
{
var collection = new BeatmapCollection { Name = reader.ReadString() };
int mapCount = reader.ReadInt32();
for (int j = 0; j < mapCount; j++)
{
string checksum = reader.ReadString();
var beatmap = beatmaps.QueryBeatmap(b => b.MD5Hash == checksum);
if (beatmap != null)
collection.Beatmaps.Add(beatmap);
}
2020-09-01 18:33:06 +08:00
result.Add(collection);
2020-09-01 16:28:41 +08:00
}
}
return result;
}
}
public class BeatmapCollection
{
public string Name;
public readonly BindableList<BeatmapInfo> Beatmaps = new BindableList<BeatmapInfo>();
2020-09-02 20:19:15 +08:00
public DateTimeOffset LastModifyTime { get; private set; }
public BeatmapCollection()
{
LastModifyTime = DateTimeOffset.UtcNow;
Beatmaps.CollectionChanged += (_, __) => LastModifyTime = DateTimeOffset.Now;
}
2020-09-01 16:28:41 +08:00
}
}