1
0
mirror of https://github.com/ppy/osu.git synced 2024-12-14 10:12:54 +08:00

Merge pull request #13615 from peppy/mod-utils-check-duplicate-mods

Ensure duplicate mods cannot be defined
This commit is contained in:
Dan Balasescu 2021-06-22 20:28:40 +09:00 committed by GitHub
commit c436f9f0fa
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 29 additions and 4 deletions

View File

@ -14,6 +14,14 @@ namespace osu.Game.Tests.Mods
[TestFixture]
public class ModUtilsTest
{
[Test]
public void TestModIsNotCompatibleWithItself()
{
var mod = new Mock<CustomMod1>();
Assert.That(ModUtils.CheckCompatibleSet(new[] { mod.Object, mod.Object }, out var invalid), Is.False);
Assert.That(invalid, Is.EquivalentTo(new[] { mod.Object }));
}
[Test]
public void TestModIsCompatibleByItself()
{
@ -147,7 +155,7 @@ namespace osu.Game.Tests.Mods
// multi mod.
new object[]
{
new Mod[] { new MultiMod(new OsuModHalfTime()), new OsuModHalfTime() },
new Mod[] { new MultiMod(new OsuModHalfTime()), new OsuModDaycore() },
new[] { typeof(MultiMod) }
},
// valid pair.

View File

@ -51,14 +51,31 @@ namespace osu.Game.Utils
/// <returns>Whether all <see cref="Mod"/>s in the combination are compatible with each-other.</returns>
public static bool CheckCompatibleSet(IEnumerable<Mod> combination, [NotNullWhen(false)] out List<Mod>? invalidMods)
{
combination = FlattenMods(combination).ToArray();
var mods = FlattenMods(combination).ToArray();
invalidMods = null;
foreach (var mod in combination)
// ensure there are no duplicate mod definitions.
for (int i = 0; i < mods.Length; i++)
{
var candidate = mods[i];
for (int j = i + 1; j < mods.Length; j++)
{
var m = mods[j];
if (candidate.Equals(m))
{
invalidMods ??= new List<Mod>();
invalidMods.Add(m);
}
}
}
foreach (var mod in mods)
{
foreach (var type in mod.IncompatibleMods)
{
foreach (var invalid in combination.Where(m => type.IsInstanceOfType(m)))
foreach (var invalid in mods.Where(m => type.IsInstanceOfType(m)))
{
if (invalid == mod)
continue;