mirror of
https://github.com/ppy/osu.git
synced 2025-01-12 15:22:55 +08:00
Set correct date added value when importing stable beatmapsets
This commit is contained in:
parent
59a31cc868
commit
59abb59ee8
@ -1,19 +1,22 @@
|
||||
// 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 NUnit.Framework;
|
||||
using osu.Framework.Platform;
|
||||
using osu.Framework.Testing;
|
||||
using osu.Game.Beatmaps;
|
||||
using osu.Game.Database;
|
||||
using osu.Game.IO;
|
||||
using osu.Game.Tests.Resources;
|
||||
|
||||
namespace osu.Game.Tests.Database
|
||||
{
|
||||
[TestFixture]
|
||||
public class LegacyBeatmapImporterTest
|
||||
public class LegacyBeatmapImporterTest : RealmTest
|
||||
{
|
||||
private readonly TestLegacyBeatmapImporter importer = new TestLegacyBeatmapImporter();
|
||||
|
||||
@ -60,6 +63,32 @@ namespace osu.Game.Tests.Database
|
||||
}
|
||||
}
|
||||
|
||||
[Test]
|
||||
public void TestStableDateAddedApplied()
|
||||
{
|
||||
RunTestWithRealmAsync(async (realm, storage) =>
|
||||
{
|
||||
using (HeadlessGameHost host = new CleanRunHeadlessGameHost())
|
||||
using (var tmpStorage = new TemporaryNativeStorage("stable-songs-folder"))
|
||||
{
|
||||
var stableStorage = new StableStorage(tmpStorage.GetFullPath(""), host);
|
||||
var songsStorage = stableStorage.GetStorageForDirectory(StableStorage.STABLE_DEFAULT_SONGS_PATH);
|
||||
|
||||
System.IO.Compression.ZipFile.ExtractToDirectory(TestResources.GetQuickTestBeatmapForImport(), songsStorage.GetFullPath("renatus"));
|
||||
|
||||
string[] beatmaps = Directory.GetFiles(songsStorage.GetFullPath("renatus"), "*.osu", SearchOption.TopDirectoryOnly);
|
||||
|
||||
File.SetLastWriteTimeUtc(beatmaps[beatmaps.Length / 2], new DateTime(2000, 1, 1, 12, 0, 0));
|
||||
|
||||
await new LegacyBeatmapImporter(new BeatmapImporter(storage, realm)).ImportFromStableAsync(stableStorage);
|
||||
|
||||
var set = realm.Realm.All<BeatmapSetInfo>();
|
||||
|
||||
Assert.AreEqual(new DateTimeOffset(new DateTime(2000, 1, 1, 12, 0, 0, DateTimeKind.Utc)), set.First().DateAdded);
|
||||
}
|
||||
});
|
||||
}
|
||||
|
||||
private class TestLegacyBeatmapImporter : LegacyBeatmapImporter
|
||||
{
|
||||
public TestLegacyBeatmapImporter()
|
||||
|
@ -302,14 +302,50 @@ namespace osu.Game.Beatmaps
|
||||
beatmap = Decoder.GetDecoder<Beatmap>(stream).Decode(stream);
|
||||
}
|
||||
|
||||
var dateAdded = DateTimeOffset.UtcNow;
|
||||
|
||||
// Apply proper date added for a beatmapset when importing from stable.
|
||||
// Stable tracks said date using the filesystem last modified date on the .osu file.
|
||||
if (reader is LegacyDirectoryArchiveReader legacyReader)
|
||||
{
|
||||
dateAdded = determineDateAdded(legacyReader);
|
||||
}
|
||||
|
||||
return new BeatmapSetInfo
|
||||
{
|
||||
OnlineID = beatmap.BeatmapInfo.BeatmapSet?.OnlineID ?? -1,
|
||||
// Metadata = beatmap.Metadata,
|
||||
DateAdded = DateTimeOffset.UtcNow
|
||||
DateAdded = dateAdded
|
||||
};
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Used for beatmapsets in legacy (stable) storage.
|
||||
/// Determine the date a given beatmapset has been added to the game.
|
||||
/// The specific date is determined based on the oldest `.osu` file existing
|
||||
/// in the beatmapset directory.
|
||||
/// </summary>
|
||||
/// <param name="reader"></param>
|
||||
/// <returns></returns>
|
||||
private DateTimeOffset determineDateAdded(LegacyDirectoryArchiveReader reader)
|
||||
{
|
||||
var beatmaps = reader.Filenames.Where(f => f.EndsWith(".osu", StringComparison.OrdinalIgnoreCase));
|
||||
|
||||
var dateAdded = File.GetLastWriteTimeUtc(reader.GetPath(beatmaps.First()));
|
||||
|
||||
foreach (string beatmapName in beatmaps)
|
||||
{
|
||||
var currentDateAdded = File.GetLastWriteTimeUtc(reader.GetPath(beatmapName));
|
||||
|
||||
if (currentDateAdded < dateAdded)
|
||||
{
|
||||
dateAdded = currentDateAdded;
|
||||
}
|
||||
}
|
||||
|
||||
return new DateTimeOffset(dateAdded);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Create all required <see cref="BeatmapInfo"/>s for the provided archive.
|
||||
/// </summary>
|
||||
|
@ -21,7 +21,9 @@ namespace osu.Game.IO.Archives
|
||||
this.path = Path.GetFullPath(path);
|
||||
}
|
||||
|
||||
public override Stream GetStream(string name) => File.OpenRead(Path.Combine(path, name));
|
||||
public override Stream GetStream(string name) => File.OpenRead(GetPath(name));
|
||||
|
||||
public string GetPath(string name) => Path.Combine(path, name);
|
||||
|
||||
public override void Dispose()
|
||||
{
|
||||
|
Loading…
Reference in New Issue
Block a user