From ea1e6e9798b7773bdf130febf4026edbd1caac52 Mon Sep 17 00:00:00 2001 From: OliBomby Date: Tue, 25 Apr 2023 12:12:46 +0200 Subject: [PATCH] Add LegacyContext --- .../Legacy/DistanceObjectPatternGenerator.cs | 5 +- osu.Game.Rulesets.Osu/Objects/Slider.cs | 13 +-- .../Beatmaps/TaikoBeatmapConverter.cs | 5 +- .../Beatmaps/Formats/LegacyBeatmapDecoder.cs | 16 +++- osu.Game/Beatmaps/Legacy/LegacyContext.cs | 32 +++++++ osu.Game/Context/IContext.cs | 10 +++ osu.Game/Context/IHasContext.cs | 88 +++++++++++++++++++ osu.Game/Rulesets/Objects/HitObject.cs | 15 +--- 8 files changed, 155 insertions(+), 29 deletions(-) create mode 100644 osu.Game/Beatmaps/Legacy/LegacyContext.cs create mode 100644 osu.Game/Context/IContext.cs create mode 100644 osu.Game/Context/IHasContext.cs diff --git a/osu.Game.Rulesets.Mania/Beatmaps/Patterns/Legacy/DistanceObjectPatternGenerator.cs b/osu.Game.Rulesets.Mania/Beatmaps/Patterns/Legacy/DistanceObjectPatternGenerator.cs index 9e031c2b4d..b3a68269f7 100644 --- a/osu.Game.Rulesets.Mania/Beatmaps/Patterns/Legacy/DistanceObjectPatternGenerator.cs +++ b/osu.Game.Rulesets.Mania/Beatmaps/Patterns/Legacy/DistanceObjectPatternGenerator.cs @@ -15,6 +15,7 @@ using osu.Game.Rulesets.Objects.Types; using osu.Game.Rulesets.Mania.Objects; using osu.Game.Beatmaps.ControlPoints; using osu.Game.Beatmaps.Formats; +using osu.Game.Beatmaps.Legacy; using osu.Game.Utils; namespace osu.Game.Rulesets.Mania.Beatmaps.Patterns.Legacy @@ -51,8 +52,8 @@ namespace osu.Game.Rulesets.Mania.Beatmaps.Patterns.Legacy TimingControlPoint timingPoint = beatmap.ControlPointInfo.TimingPointAt(hitObject.StartTime); double beatLength; - if (hitObject.LegacyBpmMultiplier.HasValue) - beatLength = timingPoint.BeatLength * hitObject.LegacyBpmMultiplier.Value; + if (hitObject.HasContext()) + beatLength = timingPoint.BeatLength * hitObject.GetContext().BpmMultiplier; else if (hitObject is IHasSliderVelocity hasSliderVelocity) beatLength = timingPoint.BeatLength / hasSliderVelocity.SliderVelocity; else diff --git a/osu.Game.Rulesets.Osu/Objects/Slider.cs b/osu.Game.Rulesets.Osu/Objects/Slider.cs index 6952033aec..4010218f6e 100644 --- a/osu.Game.Rulesets.Osu/Objects/Slider.cs +++ b/osu.Game.Rulesets.Osu/Objects/Slider.cs @@ -138,11 +138,6 @@ namespace osu.Game.Rulesets.Osu.Objects public double SliderVelocity { get; set; } = 1; - /// - /// Whether to generate ticks on this . - /// - public bool GenerateTicks = true; - [JsonIgnore] public SliderHeadCircle HeadCircle { get; protected set; } @@ -162,9 +157,10 @@ namespace osu.Game.Rulesets.Osu.Objects TimingControlPoint timingPoint = controlPointInfo.TimingPointAt(StartTime); double scoringDistance = BASE_SCORING_DISTANCE * difficulty.SliderMultiplier * SliderVelocity; + bool generateTicks = !HasContext() || GetContext().GenerateTicks; Velocity = scoringDistance / timingPoint.BeatLength; - TickDistance = GenerateTicks ? (scoringDistance / difficulty.SliderTickRate * TickDistanceMultiplier) : double.PositiveInfinity; + TickDistance = generateTicks ? (scoringDistance / difficulty.SliderTickRate * TickDistanceMultiplier) : double.PositiveInfinity; } protected override void ApplyLegacyInfoToSelf(ControlPointInfo controlPointInfo, IBeatmapDifficultyInfo difficulty) @@ -172,12 +168,7 @@ namespace osu.Game.Rulesets.Osu.Objects base.ApplyLegacyInfoToSelf(controlPointInfo, difficulty); DifficultyControlPoint difficultyControlPoint = controlPointInfo is LegacyControlPointInfo legacyInfo ? legacyInfo.DifficultyPointAt(StartTime) : DifficultyControlPoint.DEFAULT; -#pragma warning disable 618 - var legacyDifficultyPoint = difficultyControlPoint as LegacyBeatmapDecoder.LegacyDifficultyControlPoint; -#pragma warning restore 618 - SliderVelocity = difficultyControlPoint.SliderVelocity; - GenerateTicks = legacyDifficultyPoint?.GenerateTicks ?? true; } protected override void CreateNestedHitObjects(CancellationToken cancellationToken) diff --git a/osu.Game.Rulesets.Taiko/Beatmaps/TaikoBeatmapConverter.cs b/osu.Game.Rulesets.Taiko/Beatmaps/TaikoBeatmapConverter.cs index 362ddccaf1..ef73ffd517 100644 --- a/osu.Game.Rulesets.Taiko/Beatmaps/TaikoBeatmapConverter.cs +++ b/osu.Game.Rulesets.Taiko/Beatmaps/TaikoBeatmapConverter.cs @@ -16,6 +16,7 @@ using JetBrains.Annotations; using osu.Game.Audio; using osu.Game.Beatmaps.ControlPoints; using osu.Game.Beatmaps.Formats; +using osu.Game.Beatmaps.Legacy; namespace osu.Game.Rulesets.Taiko.Beatmaps { @@ -179,8 +180,8 @@ namespace osu.Game.Rulesets.Taiko.Beatmaps TimingControlPoint timingPoint = beatmap.ControlPointInfo.TimingPointAt(obj.StartTime); double beatLength; - if (obj.LegacyBpmMultiplier.HasValue) - beatLength = timingPoint.BeatLength * obj.LegacyBpmMultiplier.Value; + if (obj.HasContext()) + beatLength = timingPoint.BeatLength * obj.GetContext().BpmMultiplier; else if (obj is IHasSliderVelocity hasSliderVelocity) beatLength = timingPoint.BeatLength / hasSliderVelocity.SliderVelocity; else diff --git a/osu.Game/Beatmaps/Formats/LegacyBeatmapDecoder.cs b/osu.Game/Beatmaps/Formats/LegacyBeatmapDecoder.cs index c17eea0e85..6f948e4d23 100644 --- a/osu.Game/Beatmaps/Formats/LegacyBeatmapDecoder.cs +++ b/osu.Game/Beatmaps/Formats/LegacyBeatmapDecoder.cs @@ -15,6 +15,7 @@ using osu.Game.Beatmaps.Legacy; using osu.Game.Beatmaps.Timing; using osu.Game.IO; using osu.Game.Rulesets; +using osu.Game.Rulesets.Objects; using osu.Game.Rulesets.Objects.Legacy; namespace osu.Game.Beatmaps.Formats @@ -86,11 +87,24 @@ namespace osu.Game.Beatmaps.Formats foreach (var hitObject in this.beatmap.HitObjects) { - hitObject.ApplyLegacyInfo(this.beatmap.ControlPointInfo, this.beatmap.Difficulty); + applyLegacyInfoToHitObject(hitObject); hitObject.ApplyDefaults(this.beatmap.ControlPointInfo, this.beatmap.Difficulty); } } + private void applyLegacyInfoToHitObject(HitObject hitObject) + { + var legacyInfo = beatmap.ControlPointInfo as LegacyControlPointInfo; + + DifficultyControlPoint difficultyControlPoint = legacyInfo != null ? legacyInfo.DifficultyPointAt(hitObject.StartTime) : DifficultyControlPoint.DEFAULT; +#pragma warning disable 618 + if (difficultyControlPoint is LegacyDifficultyControlPoint legacyDifficultyControlPoint) +#pragma warning restore 618 + hitObject.SetContext(new LegacyContext(legacyDifficultyControlPoint.BpmMultiplier, legacyDifficultyControlPoint.GenerateTicks)); + + hitObject.ApplyLegacyInfo(beatmap.ControlPointInfo, beatmap.Difficulty); + } + /// /// Some `BeatmapInfo` members have default values that differ from the default values used by stable. /// In addition, legacy beatmaps will sometimes not contain some configuration keys, in which case diff --git a/osu.Game/Beatmaps/Legacy/LegacyContext.cs b/osu.Game/Beatmaps/Legacy/LegacyContext.cs new file mode 100644 index 0000000000..eeb02bdcb7 --- /dev/null +++ b/osu.Game/Beatmaps/Legacy/LegacyContext.cs @@ -0,0 +1,32 @@ +// Copyright (c) ppy Pty Ltd . Licensed under the MIT Licence. +// See the LICENCE file in the repository root for full licence text. + +using osu.Game.Context; + +namespace osu.Game.Beatmaps.Legacy; + +public class LegacyContext : IContext +{ + public LegacyContext(double bpmMultiplier, bool generateTicks) + { + BpmMultiplier = bpmMultiplier; + GenerateTicks = generateTicks; + } + + /// + /// Legacy BPM multiplier that introduces floating-point errors for rulesets that depend on it. + /// DO NOT USE THIS UNLESS 100% SURE. + /// + public double BpmMultiplier { get; } + + /// + /// Whether or not slider ticks should be generated at this control point. + /// This exists for backwards compatibility with maps that abuse NaN slider velocity behavior on osu!stable (e.g. /b/2628991). + /// + public bool GenerateTicks { get; } + + public IContext Copy() + { + return new LegacyContext(BpmMultiplier, GenerateTicks); + } +} diff --git a/osu.Game/Context/IContext.cs b/osu.Game/Context/IContext.cs new file mode 100644 index 0000000000..f70cdea9f8 --- /dev/null +++ b/osu.Game/Context/IContext.cs @@ -0,0 +1,10 @@ +namespace osu.Game.Context; + +public interface IContext +{ + /// + /// Makes a deep copy of this context. + /// + /// The deep copy of this context. + public IContext Copy(); +} diff --git a/osu.Game/Context/IHasContext.cs b/osu.Game/Context/IHasContext.cs new file mode 100644 index 0000000000..bee81ea555 --- /dev/null +++ b/osu.Game/Context/IHasContext.cs @@ -0,0 +1,88 @@ +using System; +using System.Collections.Generic; + +namespace osu.Game.Context +{ + public abstract class ContextContainer + { + /// + /// The contexts of this container. + /// The objects always have the type of their key. + /// + private readonly Dictionary contexts; + + protected ContextContainer() + { + contexts = new Dictionary(); + } + + /// + /// Checks whether this object has the context with type T. + /// + /// The type to check the context of. + /// Whether the context object with type T exists in this object. + public bool HasContext() where T : IContext + { + return contexts.ContainsKey(typeof(T)); + } + + /// + /// Gets the context with type T. + /// + /// The type to get the context of. + /// If the context does not exist in this hit object. + /// The context object with type T. + public T GetContext() where T : IContext + { + return (T)contexts[typeof(T)]; + } + + /// + /// Tries to get the context with type T. + /// + /// The found context with type T. + /// The type to get the context of. + /// Whether the context exists in this object. + public bool TryGetContext(out T context) where T : IContext + { + if (contexts.TryGetValue(typeof(T), out var context2)) + { + context = (T)context2; + return true; + } + + context = default!; + return false; + } + + /// + /// Sets the context object of type T. + /// + /// The context type to set. + /// The context object to store in this object. + public void SetContext(T context) where T : IContext + { + contexts[typeof(T)] = context; + } + + /// + /// Removes the context of type T from this object. + /// + /// The type to remove the context of. + /// Whether a context was removed. + public bool RemoveContext() where T : IContext + { + return RemoveContext(typeof(T)); + } + + /// + /// Removes the context of type T from this object. + /// + /// The type to remove the context of. + /// Whether a context was removed. + public bool RemoveContext(Type t) + { + return contexts.Remove(t); + } + } +} diff --git a/osu.Game/Rulesets/Objects/HitObject.cs b/osu.Game/Rulesets/Objects/HitObject.cs index aea564a4b9..fb7a5739b4 100644 --- a/osu.Game/Rulesets/Objects/HitObject.cs +++ b/osu.Game/Rulesets/Objects/HitObject.cs @@ -18,6 +18,7 @@ using osu.Game.Beatmaps; using osu.Game.Beatmaps.ControlPoints; using osu.Game.Beatmaps.Formats; using osu.Game.Beatmaps.Legacy; +using osu.Game.Context; using osu.Game.Rulesets.Judgements; using osu.Game.Rulesets.Objects.Types; using osu.Game.Rulesets.Scoring; @@ -30,7 +31,7 @@ namespace osu.Game.Rulesets.Objects /// HitObjects may contain more properties for which you should be checking through the IHas* types. /// /// - public class HitObject + public class HitObject : ContextContainer { /// /// A small adjustment to the start time of control points to account for rounding/precision errors. @@ -81,12 +82,6 @@ namespace osu.Game.Rulesets.Objects public SampleControlPoint SampleControlPoint = SampleControlPoint.DEFAULT; public DifficultyControlPoint DifficultyControlPoint = DifficultyControlPoint.DEFAULT; - /// - /// Legacy BPM multiplier that introduces floating-point errors for rulesets that depend on it. - /// DO NOT USE THIS UNLESS 100% SURE. - /// - public double? LegacyBpmMultiplier { get; private set; } - /// /// Whether this is in Kiai time. /// @@ -174,12 +169,6 @@ namespace osu.Game.Rulesets.Objects { var legacyInfo = controlPointInfo as LegacyControlPointInfo; - DifficultyControlPoint difficultyControlPoint = legacyInfo != null ? legacyInfo.DifficultyPointAt(StartTime) : DifficultyControlPoint.DEFAULT; -#pragma warning disable 618 - if (difficultyControlPoint is LegacyBeatmapDecoder.LegacyDifficultyControlPoint legacyDifficultyControlPoint) -#pragma warning restore 618 - LegacyBpmMultiplier = legacyDifficultyControlPoint.BpmMultiplier; - ApplyLegacyInfoToSelf(controlPointInfo, difficulty); // This is done here after ApplyLegacyInfoToSelf as we may require custom defaults to be applied to have an accurate end time.