Skip to content

Commit

Permalink
Merge pull request #190 from WilliamQiufeng/timing-group-compare-fix
Browse files Browse the repository at this point in the history
Compare Qua.TimingGroups properly
  • Loading branch information
Swan authored Nov 2, 2024
2 parents 815b5f8 + cff3f15 commit 3df96af
Show file tree
Hide file tree
Showing 4 changed files with 81 additions and 11 deletions.
20 changes: 20 additions & 0 deletions Quaver.API.Tests/Quaver/TestCaseQua.cs
Original file line number Diff line number Diff line change
Expand Up @@ -267,6 +267,26 @@ public void StableSorting()
Assert.Equal(unsorted.Bookmarks, sorted.Bookmarks, BookmarkInfo.ByValueComparer);
}

[Fact]
public void TimingGroupsComparison()
{
var q1 = new Qua();
var q2 = new Qua();
Assert.True(q1.EqualByValue(q2));

q2.TimingGroups["a"] = new ScrollGroup();
Assert.False(q1.EqualByValue(q2));

q1.TimingGroups["a"] = new ScrollGroup();
Assert.True(q1.EqualByValue(q2));

((ScrollGroup)q2.TimingGroups["a"]).ScrollVelocities.Add(new SliderVelocityInfo {Multiplier = 2});
Assert.False(q1.EqualByValue(q2));

((ScrollGroup)q1.TimingGroups["a"]).ScrollVelocities.Add(new SliderVelocityInfo {Multiplier = 2});
Assert.True(q1.EqualByValue(q2));
}

[Fact]
public void SVNormalization()
{
Expand Down
15 changes: 14 additions & 1 deletion Quaver.API/Maps/Qua.cs
Original file line number Diff line number Diff line change
Expand Up @@ -259,7 +259,7 @@ public bool EqualByValue(Qua other) =>
TimingPoints.SequenceEqual(other.TimingPoints, TimingPointInfo.ByValueComparer) &&
SliderVelocities.SequenceEqual(other.SliderVelocities, SliderVelocityInfo.ByValueComparer) &&
InitialScrollVelocity == other.InitialScrollVelocity &&
TimingGroups.SequenceEqual(other.TimingGroups) &&
CompareTimingGroups(other.TimingGroups) &&
BPMDoesNotAffectScrollVelocity == other.BPMDoesNotAffectScrollVelocity &&
LegacyLNRendering == other.LegacyLNRendering &&
HasScratchKey == other.HasScratchKey &&
Expand All @@ -270,6 +270,19 @@ public bool EqualByValue(Qua other) =>
Bookmarks.SequenceEqual(other.Bookmarks, BookmarkInfo.ByValueComparer) &&
RandomizeModifierSeed == other.RandomizeModifierSeed;

private bool CompareTimingGroups(Dictionary<string, TimingGroup> other)
{
if (TimingGroups == null || other == null || TimingGroups.Count != other.Count)
return false;
foreach (var (key, value) in TimingGroups)
{
if (!other.TryGetValue(key, out var otherGroup) || !otherGroup.Equals(value))
return false;
}

return true;
}

private static IDeserializer Deserializer =>
new DeserializerBuilder()
.IgnoreUnmatchedProperties()
Expand Down
36 changes: 26 additions & 10 deletions Quaver.API/Maps/Structures/ScrollGroup.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
using System.Collections.Generic;
using System.Linq;
using MoonSharp.Interpreter;
using Quaver.API.Helpers;
using YamlDotNet.Serialization;
Expand All @@ -13,21 +14,36 @@ namespace Quaver.API.Maps.Structures
[MoonSharpUserData]
public class ScrollGroup : TimingGroup

Check warning on line 15 in Quaver.API/Maps/Structures/ScrollGroup.cs

View workflow job for this annotation

GitHub Actions / build

'ScrollGroup' overrides Object.Equals(object o) but does not override Object.GetHashCode()

Check warning on line 15 in Quaver.API/Maps/Structures/ScrollGroup.cs

View workflow job for this annotation

GitHub Actions / build

'ScrollGroup' overrides Object.Equals(object o) but does not override Object.GetHashCode()
{
public float InitialScrollVelocity
{
get;
[MoonSharpHidden] set;
} = 1;
public float InitialScrollVelocity { get; [MoonSharpHidden] set; } = 1;

public List<SliderVelocityInfo> ScrollVelocities
{
get;
[MoonSharpHidden] set;
} = new List<SliderVelocityInfo>();
public List<SliderVelocityInfo> ScrollVelocities { get; [MoonSharpHidden] set; } =
new List<SliderVelocityInfo>();

public SliderVelocityInfo GetScrollVelocityAt(double time)
{
return ScrollVelocities.AtTime((float)time);
}

/// <summary>
/// </summary>
/// <param name="other"></param>
/// <returns></returns>
protected bool Equals(ScrollGroup other)
{
return base.Equals(other) && InitialScrollVelocity.Equals(other.InitialScrollVelocity) &&
ScrollVelocities.SequenceEqual(other.ScrollVelocities, SliderVelocityInfo.ByValueComparer);
}

/// <summary>
/// </summary>
/// <param name="obj"></param>
/// <returns></returns>
public override bool Equals(object obj)
{
if (obj is null) return false;
if (ReferenceEquals(this, obj)) return true;
if (obj.GetType() != GetType()) return false;
return Equals((ScrollGroup)obj);
}
}
}
21 changes: 21 additions & 0 deletions Quaver.API/Maps/Structures/TimingGroup.cs
Original file line number Diff line number Diff line change
Expand Up @@ -35,5 +35,26 @@ public Color GetColor() =>
byte.TryParse(tb, out var b)
? Color.FromArgb(r, g, b)
: Color.White;

/// <summary>
/// </summary>
/// <param name="other"></param>
/// <returns></returns>
protected bool Equals(TimingGroup other)
{
return ColorRgb == other.ColorRgb;
}

/// <summary>
/// </summary>
/// <param name="obj"></param>
/// <returns></returns>
public override bool Equals(object obj)
{
if (obj is null) return false;
if (ReferenceEquals(this, obj)) return true;
if (obj.GetType() != GetType()) return false;
return Equals((TimingGroup)obj);
}
}
}

0 comments on commit 3df96af

Please sign in to comment.