From b5a55a0dcea31751a0fc7ceebc96f53bad87e426 Mon Sep 17 00:00:00 2001 From: smoogipoo Date: Thu, 19 Apr 2018 18:14:21 +0900 Subject: [PATCH] Make an interface for beatmaps --- osu.Game/Beatmaps/Beatmap.cs | 118 +++++++++++++--------- osu.Game/Beatmaps/BeatmapConverter.cs | 2 +- osu.Game/Beatmaps/Legacy/LegacyBeatmap.cs | 9 -- osu.Game/Screens/Play/BreakOverlay.cs | 4 +- osu.Game/Tests/Beatmaps/TestBeatmap.cs | 8 +- 5 files changed, 79 insertions(+), 62 deletions(-) diff --git a/osu.Game/Beatmaps/Beatmap.cs b/osu.Game/Beatmaps/Beatmap.cs index 12a017f68c..541f7f85b6 100644 --- a/osu.Game/Beatmaps/Beatmap.cs +++ b/osu.Game/Beatmaps/Beatmap.cs @@ -12,24 +12,69 @@ using osu.Game.IO.Serialization.Converters; namespace osu.Game.Beatmaps { + public interface IBeatmap : IJsonSerializable + { + /// + /// This beatmap's info. + /// + BeatmapInfo BeatmapInfo { get; } + + /// + /// This beatmap's metadata. + /// + BeatmapMetadata Metadata { get; } + + /// + /// The control points in this beatmap. + /// + ControlPointInfo ControlPointInfo { get; } + + /// + /// The breaks in this beatmap. + /// + List Breaks { get; } + + /// + /// Total amount of break time in the beatmap. + /// + double TotalBreakTime { get; } + + /// + /// The hitobjects contained by this beatmap. + /// + IEnumerable HitObjects { get; } + + /// + /// Creates a shallow-clone of this beatmap and returns it. + /// + /// The shallow-cloned beatmap. + IBeatmap Clone(); + } + /// /// A Beatmap containing converted HitObjects. /// - public class Beatmap : IJsonSerializable + public class Beatmap : IBeatmap where T : HitObject { - public BeatmapInfo BeatmapInfo = new BeatmapInfo(); - public ControlPointInfo ControlPointInfo = new ControlPointInfo(); - public List Breaks = new List(); + public BeatmapInfo BeatmapInfo { get; set; } = new BeatmapInfo + { + Metadata = new BeatmapMetadata + { + Artist = @"Unknown", + Title = @"Unknown", + AuthorString = @"Unknown Creator", + }, + Version = @"Normal", + BaseDifficulty = new BeatmapDifficulty() + }; [JsonIgnore] public BeatmapMetadata Metadata => BeatmapInfo?.Metadata ?? BeatmapInfo?.BeatmapSet?.Metadata; - /// - /// The HitObjects this Beatmap contains. - /// - [JsonConverter(typeof(TypedListConverter))] - public List HitObjects = new List(); + public ControlPointInfo ControlPointInfo { get; set; } = new ControlPointInfo(); + + public List Breaks { get; set; } = new List(); /// /// Total amount of break time in the beatmap. @@ -38,51 +83,26 @@ namespace osu.Game.Beatmaps public double TotalBreakTime => Breaks.Sum(b => b.Duration); /// - /// Constructs a new beatmap. + /// The HitObjects this Beatmap contains. /// - /// The original beatmap to use the parameters of. - public Beatmap(Beatmap original = null) - { - BeatmapInfo = original?.BeatmapInfo.DeepClone() ?? BeatmapInfo; - ControlPointInfo = original?.ControlPointInfo ?? ControlPointInfo; - Breaks = original?.Breaks ?? Breaks; - HitObjects = original?.HitObjects ?? HitObjects; + [JsonConverter(typeof(TypedListConverter))] + public List HitObjects = new List(); - if (original == null && Metadata == null) - { - // we may have no metadata in cases we weren't sourced from the database. - // let's fill it (and other related fields) so we don't need to null-check it in future usages. - BeatmapInfo = new BeatmapInfo - { - Metadata = new BeatmapMetadata - { - Artist = @"Unknown", - Title = @"Unknown", - AuthorString = @"Unknown Creator", - }, - Version = @"Normal", - BaseDifficulty = new BeatmapDifficulty() - }; - } - } + IEnumerable IBeatmap.HitObjects => HitObjects; + + public Beatmap Clone() => new Beatmap + { + BeatmapInfo = BeatmapInfo.DeepClone(), + ControlPointInfo = ControlPointInfo, + Breaks = Breaks, + HitObjects = HitObjects + }; + + IBeatmap IBeatmap.Clone() => Clone(); } - /// - /// A Beatmap containing un-converted HitObjects. - /// public class Beatmap : Beatmap { - /// - /// Constructs a new beatmap. - /// - /// The original beatmap to use the parameters of. - public Beatmap(Beatmap original) - : base(original) - { - } - - public Beatmap() - { - } + public new Beatmap Clone() => (Beatmap)base.Clone(); } } diff --git a/osu.Game/Beatmaps/BeatmapConverter.cs b/osu.Game/Beatmaps/BeatmapConverter.cs index 153cace187..f46e6abc87 100644 --- a/osu.Game/Beatmaps/BeatmapConverter.cs +++ b/osu.Game/Beatmaps/BeatmapConverter.cs @@ -37,7 +37,7 @@ namespace osu.Game.Beatmaps 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)); + return ConvertBeatmap(original.Clone()); } void IBeatmapConverter.Convert(Beatmap original) => Convert(original); diff --git a/osu.Game/Beatmaps/Legacy/LegacyBeatmap.cs b/osu.Game/Beatmaps/Legacy/LegacyBeatmap.cs index eea82dac6d..4daa014804 100644 --- a/osu.Game/Beatmaps/Legacy/LegacyBeatmap.cs +++ b/osu.Game/Beatmaps/Legacy/LegacyBeatmap.cs @@ -8,14 +8,5 @@ namespace osu.Game.Beatmaps.Legacy /// public class LegacyBeatmap : Beatmap { - /// - /// Constructs a new beatmap. - /// - /// The original beatmap to use the parameters of. - internal LegacyBeatmap(Beatmap original = null) - : base(original) - { - HitObjects = original?.HitObjects; - } } } diff --git a/osu.Game/Screens/Play/BreakOverlay.cs b/osu.Game/Screens/Play/BreakOverlay.cs index b2df996d35..ca252dd6fd 100644 --- a/osu.Game/Screens/Play/BreakOverlay.cs +++ b/osu.Game/Screens/Play/BreakOverlay.cs @@ -18,11 +18,11 @@ namespace osu.Game.Screens.Play private const float remaining_time_container_max_size = 0.3f; private const int vertical_margin = 25; - private List breaks; + private IReadOnlyList breaks; private readonly Container fadeContainer; - public List Breaks + public IReadOnlyList Breaks { get => breaks; set diff --git a/osu.Game/Tests/Beatmaps/TestBeatmap.cs b/osu.Game/Tests/Beatmaps/TestBeatmap.cs index 09a3a7af8c..6bad08baaa 100644 --- a/osu.Game/Tests/Beatmaps/TestBeatmap.cs +++ b/osu.Game/Tests/Beatmaps/TestBeatmap.cs @@ -12,8 +12,14 @@ namespace osu.Game.Tests.Beatmaps public class TestBeatmap : Beatmap { public TestBeatmap(RulesetInfo ruleset) - : base(createTestBeatmap()) { + var baseBeatmap = createTestBeatmap(); + + BeatmapInfo = baseBeatmap.BeatmapInfo; + ControlPointInfo = baseBeatmap.ControlPointInfo; + Breaks = baseBeatmap.Breaks; + HitObjects = baseBeatmap.HitObjects; + BeatmapInfo.Ruleset = ruleset; }