1
0
mirror of https://github.com/ppy/osu.git synced 2024-11-11 10:33:30 +08:00

Implement IEquatable on ControlPoint

This commit is contained in:
Dan Balasescu 2022-06-20 14:56:04 +09:00
parent 468e5fcbed
commit 03ab6fc141
7 changed files with 109 additions and 22 deletions

View File

@ -1,8 +1,6 @@
// Copyright (c) ppy Pty Ltd <contact@ppy.sh>. Licensed under the MIT Licence.
// See the LICENCE file in the repository root for full licence text.
#nullable disable
using System;
using Newtonsoft.Json;
using osu.Game.Graphics;
@ -11,7 +9,7 @@ using osuTK.Graphics;
namespace osu.Game.Beatmaps.ControlPoints
{
public abstract class ControlPoint : IComparable<ControlPoint>, IDeepCloneable<ControlPoint>
public abstract class ControlPoint : IComparable<ControlPoint>, IDeepCloneable<ControlPoint>, IEquatable<ControlPoint>
{
/// <summary>
/// The time at which the control point takes effect.
@ -48,5 +46,16 @@ namespace osu.Game.Beatmaps.ControlPoints
{
Time = other.Time;
}
public sealed override bool Equals(object? obj)
=> obj is ControlPoint otherControlPoint
&& Equals(otherControlPoint);
public virtual bool Equals(ControlPoint? other)
=> other != null
&& Time == other.Time;
// ReSharper disable once NonReadonlyMemberInGetHashCode
public override int GetHashCode() => Time.GetHashCode();
}
}

View File

@ -1,18 +1,16 @@
// Copyright (c) ppy Pty Ltd <contact@ppy.sh>. Licensed under the MIT Licence.
// See the LICENCE file in the repository root for full licence text.
#nullable disable
using System;
using System.Linq;
using osu.Framework.Bindables;
namespace osu.Game.Beatmaps.ControlPoints
{
public class ControlPointGroup : IComparable<ControlPointGroup>
public class ControlPointGroup : IComparable<ControlPointGroup>, IEquatable<ControlPointGroup>
{
public event Action<ControlPoint> ItemAdded;
public event Action<ControlPoint> ItemRemoved;
public event Action<ControlPoint>? ItemAdded;
public event Action<ControlPoint>? ItemRemoved;
/// <summary>
/// The time at which the control point takes effect.
@ -48,5 +46,23 @@ namespace osu.Game.Beatmaps.ControlPoints
controlPoints.Remove(point);
ItemRemoved?.Invoke(point);
}
public sealed override bool Equals(object? obj)
=> obj is ControlPointGroup otherGroup
&& Equals(otherGroup);
public virtual bool Equals(ControlPointGroup? other)
=> other != null
&& Time == other.Time
&& ControlPoints.SequenceEqual(other.ControlPoints);
public override int GetHashCode()
{
HashCode hashCode = new HashCode();
hashCode.Add(Time);
foreach (var point in controlPoints)
hashCode.Add(point);
return hashCode.ToHashCode();
}
}
}

View File

@ -1,8 +1,7 @@
// Copyright (c) ppy Pty Ltd <contact@ppy.sh>. Licensed under the MIT Licence.
// See the LICENCE file in the repository root for full licence text.
#nullable disable
using System;
using osu.Framework.Bindables;
using osu.Game.Graphics;
using osuTK.Graphics;
@ -12,7 +11,7 @@ namespace osu.Game.Beatmaps.ControlPoints
/// <remarks>
/// Note that going forward, this control point type should always be assigned directly to HitObjects.
/// </remarks>
public class DifficultyControlPoint : ControlPoint
public class DifficultyControlPoint : ControlPoint, IEquatable<DifficultyControlPoint>
{
public static readonly DifficultyControlPoint DEFAULT = new DifficultyControlPoint
{
@ -51,5 +50,15 @@ namespace osu.Game.Beatmaps.ControlPoints
base.CopyFrom(other);
}
public override bool Equals(ControlPoint? other)
=> other is DifficultyControlPoint otherDifficultyControlPoint
&& Equals(otherDifficultyControlPoint);
public bool Equals(DifficultyControlPoint? other)
=> base.Equals(other)
&& SliderVelocity == other.SliderVelocity;
public override int GetHashCode() => HashCode.Combine(base.GetHashCode(), SliderVelocity);
}
}

View File

@ -1,15 +1,14 @@
// Copyright (c) ppy Pty Ltd <contact@ppy.sh>. Licensed under the MIT Licence.
// See the LICENCE file in the repository root for full licence text.
#nullable disable
using System;
using osu.Framework.Bindables;
using osu.Game.Graphics;
using osuTK.Graphics;
namespace osu.Game.Beatmaps.ControlPoints
{
public class EffectControlPoint : ControlPoint
public class EffectControlPoint : ControlPoint, IEquatable<EffectControlPoint>
{
public static readonly EffectControlPoint DEFAULT = new EffectControlPoint
{
@ -83,5 +82,17 @@ namespace osu.Game.Beatmaps.ControlPoints
base.CopyFrom(other);
}
public override bool Equals(ControlPoint? other)
=> other is EffectControlPoint otherEffectControlPoint
&& Equals(otherEffectControlPoint);
public bool Equals(EffectControlPoint? other)
=> base.Equals(other)
&& OmitFirstBarLine == other.OmitFirstBarLine
&& ScrollSpeed == other.ScrollSpeed
&& KiaiMode == other.KiaiMode;
public override int GetHashCode() => HashCode.Combine(base.GetHashCode(), OmitFirstBarLine, ScrollSpeed, KiaiMode);
}
}

View File

@ -1,8 +1,7 @@
// Copyright (c) ppy Pty Ltd <contact@ppy.sh>. Licensed under the MIT Licence.
// See the LICENCE file in the repository root for full licence text.
#nullable disable
using System;
using osu.Framework.Bindables;
using osu.Game.Audio;
using osu.Game.Graphics;
@ -13,7 +12,7 @@ namespace osu.Game.Beatmaps.ControlPoints
/// <remarks>
/// Note that going forward, this control point type should always be assigned directly to HitObjects.
/// </remarks>
public class SampleControlPoint : ControlPoint
public class SampleControlPoint : ControlPoint, IEquatable<SampleControlPoint>
{
public const string DEFAULT_BANK = "normal";
@ -85,5 +84,16 @@ namespace osu.Game.Beatmaps.ControlPoints
base.CopyFrom(other);
}
public override bool Equals(ControlPoint? other)
=> other is SampleControlPoint otherSampleControlPoint
&& Equals(otherSampleControlPoint);
public bool Equals(SampleControlPoint? other)
=> base.Equals(other)
&& SampleBank == other.SampleBank
&& SampleVolume == other.SampleVolume;
public override int GetHashCode() => HashCode.Combine(base.GetHashCode(), SampleBank, SampleVolume);
}
}

View File

@ -1,6 +1,7 @@
// Copyright (c) ppy Pty Ltd <contact@ppy.sh>. Licensed under the MIT Licence.
// See the LICENCE file in the repository root for full licence text.
using System;
using osu.Framework.Bindables;
using osu.Game.Beatmaps.Timing;
using osu.Game.Graphics;
@ -8,7 +9,7 @@ using osuTK.Graphics;
namespace osu.Game.Beatmaps.ControlPoints
{
public class TimingControlPoint : ControlPoint
public class TimingControlPoint : ControlPoint, IEquatable<TimingControlPoint>
{
/// <summary>
/// The time signature at this control point.
@ -77,5 +78,16 @@ namespace osu.Game.Beatmaps.ControlPoints
base.CopyFrom(other);
}
public override bool Equals(ControlPoint? other)
=> other is TimingControlPoint otherTimingControlPoint
&& Equals(otherTimingControlPoint);
public bool Equals(TimingControlPoint? other)
=> base.Equals(other)
&& TimeSignature.Equals(other.TimeSignature)
&& BeatLength.Equals(other.BeatLength);
public override int GetHashCode() => HashCode.Combine(base.GetHashCode(), TimeSignature, BeatLength);
}
}

View File

@ -1,8 +1,6 @@
// Copyright (c) ppy Pty Ltd <contact@ppy.sh>. 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 osu.Framework.Extensions;
@ -162,7 +160,7 @@ namespace osu.Game.Beatmaps.Formats
}
[Obsolete("Do not use unless you're a legacy ruleset and 100% sure.")]
public class LegacyDifficultyControlPoint : DifficultyControlPoint
public class LegacyDifficultyControlPoint : DifficultyControlPoint, IEquatable<LegacyDifficultyControlPoint>
{
/// <summary>
/// Legacy BPM multiplier that introduces floating-point errors for rulesets that depend on it.
@ -188,9 +186,20 @@ namespace osu.Game.Beatmaps.Formats
BpmMultiplier = ((LegacyDifficultyControlPoint)other).BpmMultiplier;
}
public override bool Equals(ControlPoint? other)
=> other is LegacyDifficultyControlPoint otherLegacyDifficultyControlPoint
&& Equals(otherLegacyDifficultyControlPoint);
public bool Equals(LegacyDifficultyControlPoint? other)
=> base.Equals(other)
&& BpmMultiplier == other.BpmMultiplier;
// ReSharper disable once NonReadonlyMemberInGetHashCode
public override int GetHashCode() => HashCode.Combine(base.GetHashCode(), BpmMultiplier);
}
internal class LegacySampleControlPoint : SampleControlPoint
internal class LegacySampleControlPoint : SampleControlPoint, IEquatable<LegacySampleControlPoint>
{
public int CustomSampleBank;
@ -215,6 +224,17 @@ namespace osu.Game.Beatmaps.Formats
CustomSampleBank = ((LegacySampleControlPoint)other).CustomSampleBank;
}
public override bool Equals(ControlPoint? other)
=> other is LegacySampleControlPoint otherLegacySampleControlPoint
&& Equals(otherLegacySampleControlPoint);
public bool Equals(LegacySampleControlPoint? other)
=> base.Equals(other)
&& CustomSampleBank == other.CustomSampleBank;
// ReSharper disable once NonReadonlyMemberInGetHashCode
public override int GetHashCode() => HashCode.Combine(base.GetHashCode(), CustomSampleBank);
}
}
}