// Copyright (c) ppy Pty Ltd . Licensed under the MIT Licence. // See the LICENCE file in the repository root for full licence text. #nullable disable using System; using System.Collections.Generic; using System.Linq; using System.Threading; using osu.Game.Rulesets; using osu.Game.Rulesets.Objects; namespace osu.Game.Beatmaps { /// /// Converts a Beatmap for another mode. /// /// The type of HitObject stored in the Beatmap. public abstract class BeatmapConverter : IBeatmapConverter where T : HitObject { private event Action> objectConverted; event Action> IBeatmapConverter.ObjectConverted { add => objectConverted += value; remove => objectConverted -= value; } public IBeatmap Beatmap { get; } protected BeatmapConverter(IBeatmap beatmap, Ruleset ruleset) { Beatmap = beatmap; } /// /// Whether can be converted by this . /// public abstract bool CanConvert(); public IBeatmap Convert(CancellationToken cancellationToken = default) { // We always operate on a clone of the original beatmap, to not modify it game-wide var original = Beatmap.Clone(); // Shallow clone isn't enough to ensure we don't mutate beatmap info unexpectedly. // Can potentially be removed after `Beatmap.Difficulty` doesn't save back to `Beatmap.BeatmapInfo`. original.BeatmapInfo = original.BeatmapInfo.Clone(); original.ControlPointInfo = original.ControlPointInfo.DeepClone(); return ConvertBeatmap(original, cancellationToken); } /// /// Performs the conversion of a Beatmap using this Beatmap Converter. /// /// The un-converted Beatmap. /// The cancellation token. /// The converted Beatmap. protected virtual Beatmap ConvertBeatmap(IBeatmap original, CancellationToken cancellationToken) { var beatmap = CreateBeatmap(); beatmap.BeatmapInfo = original.BeatmapInfo; beatmap.ControlPointInfo = original.ControlPointInfo; beatmap.HitObjects = convertHitObjects(original.HitObjects, original, cancellationToken).OrderBy(s => s.StartTime).ToList(); beatmap.Breaks = original.Breaks; beatmap.UnhandledEventLines = original.UnhandledEventLines; return beatmap; } private List convertHitObjects(IReadOnlyList hitObjects, IBeatmap beatmap, CancellationToken cancellationToken) { var result = new List(hitObjects.Count); foreach (var obj in hitObjects) { if (obj is T tObj) { result.Add(tObj); continue; } var converted = ConvertHitObject(obj, beatmap, cancellationToken); if (objectConverted != null) { converted = converted.ToList(); objectConverted.Invoke(obj, converted); } foreach (var c in converted) { if (c != null) result.Add(c); } } return result; } /// /// Creates the that will be returned by this . /// protected virtual Beatmap CreateBeatmap() => new Beatmap(); /// /// Performs the conversion of a hit object. /// This method is generally executed sequentially for all objects in a beatmap. /// /// The hit object to convert. /// The un-converted Beatmap. /// The cancellation token. /// The converted hit object. protected virtual IEnumerable ConvertHitObject(HitObject original, IBeatmap beatmap, CancellationToken cancellationToken) => Enumerable.Empty(); } }