Skip to content

Commit

Permalink
Merge branch 'master' into precise-rotation-2
Browse files Browse the repository at this point in the history
  • Loading branch information
peppy authored Aug 19, 2023
2 parents 3c191ff + 1b0a289 commit 8120c3f
Show file tree
Hide file tree
Showing 4 changed files with 83 additions and 11 deletions.
33 changes: 32 additions & 1 deletion osu.Game.Tests/Database/LegacyBeatmapImporterTest.cs
Original file line number Diff line number Diff line change
@@ -1,19 +1,23 @@
// 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.IO.Compression;
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();

Expand Down Expand Up @@ -60,6 +64,33 @@ static void createFile(Storage storage, string path)
}
}

[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);
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 importedSet = realm.Realm.All<BeatmapSetInfo>().Single();
Assert.NotNull(importedSet);
Assert.AreEqual(new DateTimeOffset(new DateTime(2000, 1, 1, 12, 0, 0, DateTimeKind.Utc)), importedSet.DateAdded);
}
});
}

private class TestLegacyBeatmapImporter : LegacyBeatmapImporter
{
public TestLegacyBeatmapImporter()
Expand Down
31 changes: 29 additions & 2 deletions osu.Game/Beatmaps/BeatmapImporter.cs
Original file line number Diff line number Diff line change
Expand Up @@ -152,6 +152,8 @@ protected override void Populate(BeatmapSetInfo beatmapSet, ArchiveReader? archi
if (archive != null)
beatmapSet.Beatmaps.AddRange(createBeatmapDifficulties(beatmapSet, realm));

beatmapSet.DateAdded = getDateAdded(archive);

foreach (BeatmapInfo b in beatmapSet.Beatmaps)
{
b.BeatmapSet = beatmapSet;
Expand Down Expand Up @@ -305,11 +307,36 @@ protected override void UndeleteForReuse(BeatmapSetInfo existing)
return new BeatmapSetInfo
{
OnlineID = beatmap.BeatmapInfo.BeatmapSet?.OnlineID ?? -1,
// Metadata = beatmap.Metadata,
DateAdded = DateTimeOffset.UtcNow
};
}

/// <summary>
/// Determine the date a given beatmapset has been added to the game.
/// For legacy imports, we can use the oldest file write time for any `.osu` file in the directory.
/// For any other import types, use "now".
/// </summary>
private DateTimeOffset getDateAdded(ArchiveReader? reader)
{
DateTimeOffset dateAdded = DateTimeOffset.UtcNow;

if (reader is LegacyDirectoryArchiveReader legacyReader)
{
var beatmaps = reader.Filenames.Where(f => f.EndsWith(".osu", StringComparison.OrdinalIgnoreCase));

dateAdded = File.GetLastWriteTimeUtc(legacyReader.GetFullPath(beatmaps.First()));

foreach (string beatmapName in beatmaps)
{
var currentDateAdded = File.GetLastWriteTimeUtc(legacyReader.GetFullPath(beatmapName));

if (currentDateAdded < dateAdded)
dateAdded = currentDateAdded;
}
}

return dateAdded;
}

/// <summary>
/// Create all required <see cref="BeatmapInfo"/>s for the provided archive.
/// </summary>
Expand Down
4 changes: 3 additions & 1 deletion osu.Game/IO/Archives/LegacyDirectoryArchiveReader.cs
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,9 @@ public LegacyDirectoryArchiveReader(string path)
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(GetFullPath(name));

public string GetFullPath(string filename) => Path.Combine(path, filename);

public override void Dispose()
{
Expand Down
26 changes: 19 additions & 7 deletions osu.Game/Screens/Edit/Timing/ControlPointList.cs
Original file line number Diff line number Diff line change
Expand Up @@ -147,13 +147,25 @@ private void trackActivePoint()
trackedType = null;
else
{
// If the selected group only has one control point, update the tracking type.
if (selectedGroup.Value.ControlPoints.Count == 1)
trackedType = selectedGroup.Value?.ControlPoints.Single().GetType();
// If the selected group has more than one control point, choose the first as the tracking type
// if we don't already have a singular tracked type.
else if (trackedType == null)
trackedType = selectedGroup.Value?.ControlPoints.FirstOrDefault()?.GetType();
switch (selectedGroup.Value.ControlPoints.Count)
{
// If the selected group has no control points, clear the tracked type.
// Otherwise the user will be unable to select a group with no control points.
case 0:
trackedType = null;
break;

// If the selected group only has one control point, update the tracking type.
case 1:
trackedType = selectedGroup.Value?.ControlPoints.Single().GetType();
break;

// If the selected group has more than one control point, choose the first as the tracking type
// if we don't already have a singular tracked type.
default:
trackedType ??= selectedGroup.Value?.ControlPoints.FirstOrDefault()?.GetType();
break;
}
}

if (trackedType != null)
Expand Down

0 comments on commit 8120c3f

Please sign in to comment.