2017-03-11 23:34:21 +08:00
|
|
|
|
// Copyright (c) 2007-2017 ppy Pty Ltd <contact@ppy.sh>.
|
|
|
|
|
// Licensed under the MIT Licence - https://raw.githubusercontent.com/ppy/osu/master/LICENCE
|
|
|
|
|
|
2017-04-17 14:44:46 +08:00
|
|
|
|
using System;
|
|
|
|
|
using System.Collections.Generic;
|
|
|
|
|
using System.Linq;
|
2017-04-18 15:05:58 +08:00
|
|
|
|
using osu.Game.Rulesets.Objects;
|
2017-04-18 08:32:22 +08:00
|
|
|
|
using osu.Game.Beatmaps;
|
2017-03-11 23:34:21 +08:00
|
|
|
|
|
2017-04-18 15:05:58 +08:00
|
|
|
|
namespace osu.Game.Rulesets.Beatmaps
|
2017-03-11 23:34:21 +08:00
|
|
|
|
{
|
2017-03-13 18:15:25 +08:00
|
|
|
|
/// <summary>
|
|
|
|
|
/// Converts a Beatmap for another mode.
|
|
|
|
|
/// </summary>
|
|
|
|
|
/// <typeparam name="T">The type of HitObject stored in the Beatmap.</typeparam>
|
2017-04-18 08:38:52 +08:00
|
|
|
|
public abstract class BeatmapConverter<T> where T : HitObject
|
2017-03-11 23:34:21 +08:00
|
|
|
|
{
|
2017-04-17 14:44:46 +08:00
|
|
|
|
/// <summary>
|
2017-04-18 13:24:16 +08:00
|
|
|
|
/// Checks if a Beatmap can be converted using this Beatmap Converter.
|
2017-04-17 14:44:46 +08:00
|
|
|
|
/// </summary>
|
2017-04-18 13:24:16 +08:00
|
|
|
|
/// <param name="beatmap">The Beatmap to check.</param>
|
|
|
|
|
/// <returns>Whether the Beatmap can be converted using this Beatmap Converter.</returns>
|
|
|
|
|
public bool CanConvert(Beatmap beatmap) => ValidConversionTypes.All(t => beatmap.HitObjects.Any(t.IsInstanceOfType));
|
2017-04-17 14:44:46 +08:00
|
|
|
|
|
2017-03-13 18:15:25 +08:00
|
|
|
|
/// <summary>
|
2017-04-18 13:24:16 +08:00
|
|
|
|
/// Converts a Beatmap using this Beatmap Converter.
|
2017-03-13 18:15:25 +08:00
|
|
|
|
/// </summary>
|
2017-04-18 13:24:16 +08:00
|
|
|
|
/// <param name="original">The un-converted Beatmap.</param>
|
2017-05-19 14:57:32 +08:00
|
|
|
|
/// <param name="isForCurrentRuleset">Whether to assume the beatmap is for the current ruleset.</param>
|
2017-03-13 18:15:25 +08:00
|
|
|
|
/// <returns>The converted Beatmap.</returns>
|
2017-05-19 14:57:32 +08:00
|
|
|
|
public Beatmap<T> Convert(Beatmap original, bool isForCurrentRuleset)
|
2017-04-18 13:24:16 +08:00
|
|
|
|
{
|
|
|
|
|
// We always operate on a clone of the original beatmap, to not modify it game-wide
|
2017-05-19 14:57:32 +08:00
|
|
|
|
return ConvertBeatmap(new Beatmap(original), isForCurrentRuleset);
|
2017-04-18 13:24:16 +08:00
|
|
|
|
}
|
2017-04-17 14:44:46 +08:00
|
|
|
|
|
|
|
|
|
/// <summary>
|
2017-04-18 13:24:16 +08:00
|
|
|
|
/// Performs the conversion of a Beatmap using this Beatmap Converter.
|
2017-04-17 14:44:46 +08:00
|
|
|
|
/// </summary>
|
2017-04-18 13:24:16 +08:00
|
|
|
|
/// <param name="original">The un-converted Beatmap.</param>
|
2017-05-19 14:57:32 +08:00
|
|
|
|
/// <param name="isForCurrentRuleset">Whether to assume the beatmap is for the current ruleset.</param>
|
2017-04-18 13:24:16 +08:00
|
|
|
|
/// <returns>The converted Beatmap.</returns>
|
2017-05-19 14:57:32 +08:00
|
|
|
|
protected virtual Beatmap<T> ConvertBeatmap(Beatmap original, bool isForCurrentRuleset)
|
2017-04-18 13:24:16 +08:00
|
|
|
|
{
|
|
|
|
|
return new Beatmap<T>
|
|
|
|
|
{
|
|
|
|
|
BeatmapInfo = original.BeatmapInfo,
|
|
|
|
|
TimingInfo = original.TimingInfo,
|
|
|
|
|
HitObjects = original.HitObjects.SelectMany(h => convert(h, original)).ToList()
|
|
|
|
|
};
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
|
/// Converts a hit object.
|
|
|
|
|
/// </summary>
|
|
|
|
|
/// <param name="original">The hit object to convert.</param>
|
|
|
|
|
/// <param name="beatmap">The un-converted Beatmap.</param>
|
|
|
|
|
/// <returns>The converted hit object.</returns>
|
|
|
|
|
private IEnumerable<T> convert(HitObject original, Beatmap beatmap)
|
|
|
|
|
{
|
|
|
|
|
// Check if the hitobject is already the converted type
|
|
|
|
|
T tObject = original as T;
|
|
|
|
|
if (tObject != null)
|
2017-04-18 13:48:18 +08:00
|
|
|
|
{
|
2017-04-18 13:24:16 +08:00
|
|
|
|
yield return tObject;
|
2017-04-18 13:48:18 +08:00
|
|
|
|
yield break;
|
|
|
|
|
}
|
2017-04-18 13:34:39 +08:00
|
|
|
|
|
2017-04-18 13:24:16 +08:00
|
|
|
|
// Convert the hit object
|
|
|
|
|
foreach (var obj in ConvertHitObject(original, beatmap))
|
2017-04-18 13:46:56 +08:00
|
|
|
|
{
|
|
|
|
|
if (obj == null)
|
|
|
|
|
continue;
|
|
|
|
|
|
2017-04-18 13:24:16 +08:00
|
|
|
|
yield return obj;
|
2017-04-18 13:46:56 +08:00
|
|
|
|
}
|
2017-04-18 13:24:16 +08:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
|
/// The types of HitObjects that can be converted to be used for this Beatmap.
|
|
|
|
|
/// </summary>
|
|
|
|
|
protected abstract IEnumerable<Type> ValidConversionTypes { get; }
|
|
|
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
|
/// Performs the conversion of a hit object.
|
|
|
|
|
/// </summary>
|
|
|
|
|
/// <param name="original">The hit object to convert.</param>
|
|
|
|
|
/// <param name="beatmap">The un-converted Beatmap.</param>
|
|
|
|
|
/// <returns>The converted hit object.</returns>
|
|
|
|
|
protected abstract IEnumerable<T> ConvertHitObject(HitObject original, Beatmap beatmap);
|
2017-04-17 14:44:46 +08:00
|
|
|
|
}
|
2017-03-11 23:34:21 +08:00
|
|
|
|
}
|