Ensure duplicate mods cannot be defined

This commit is contained in:
Dean Herbert
2021-06-22 17:41:20 +09:00
parent 6c3d2315d0
commit c4fde635c6
2 changed files with 28 additions and 3 deletions

View File

@ -14,6 +14,14 @@ namespace osu.Game.Tests.Mods
[TestFixture] [TestFixture]
public class ModUtilsTest 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] [Test]
public void TestModIsCompatibleByItself() public void TestModIsCompatibleByItself()
{ {

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> /// <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) public static bool CheckCompatibleSet(IEnumerable<Mod> combination, [NotNullWhen(false)] out List<Mod>? invalidMods)
{ {
combination = FlattenMods(combination).ToArray(); var mods = FlattenMods(combination).ToArray();
invalidMods = null; 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 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) if (invalid == mod)
continue; continue;