// Copyright (c) 2007-2017 ppy Pty Ltd . // Licensed under the MIT Licence - https://raw.githubusercontent.com/ppy/osu/master/LICENCE using System; using System.Collections.Generic; using System.Linq; using osu.Game.Rulesets.Objects; using osu.Game.Beatmaps; namespace osu.Game.Rulesets.Beatmaps { /// /// Converts a Beatmap for another mode. /// /// The type of HitObject stored in the Beatmap. public abstract class BeatmapConverter where T : HitObject { /// /// Checks if a Beatmap can be converted using this Beatmap Converter. /// /// The Beatmap to check. /// Whether the Beatmap can be converted using this Beatmap Converter. public bool CanConvert(Beatmap beatmap) => ValidConversionTypes.All(t => beatmap.HitObjects.Any(t.IsInstanceOfType)); /// /// Converts a Beatmap using this Beatmap Converter. /// /// The un-converted Beatmap. /// The converted Beatmap. public Beatmap Convert(Beatmap original) { // We always operate on a clone of the original beatmap, to not modify it game-wide return ConvertBeatmap(new Beatmap(original)); } /// /// Performs the conversion of a Beatmap using this Beatmap Converter. /// /// The un-converted Beatmap. /// The converted Beatmap. protected virtual Beatmap ConvertBeatmap(Beatmap original) { return new Beatmap { BeatmapInfo = original.BeatmapInfo, TimingInfo = original.TimingInfo, HitObjects = original.HitObjects.SelectMany(h => convert(h, original)).ToList() }; } /// /// Converts a hit object. /// /// The hit object to convert. /// The un-converted Beatmap. /// The converted hit object. private IEnumerable convert(HitObject original, Beatmap beatmap) { // Check if the hitobject is already the converted type T tObject = original as T; if (tObject != null) { yield return tObject; yield break; } // Convert the hit object foreach (var obj in ConvertHitObject(original, beatmap)) { if (obj == null) continue; yield return obj; } } /// /// The types of HitObjects that can be converted to be used for this Beatmap. /// protected abstract IEnumerable ValidConversionTypes { get; } /// /// Performs the conversion of a hit object. /// /// The hit object to convert. /// The un-converted Beatmap. /// The converted hit object. protected abstract IEnumerable ConvertHitObject(HitObject original, Beatmap beatmap); } }