2021-02-01 18:37:19 +08:00
|
|
|
// 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.
|
|
|
|
|
2022-03-15 13:53:50 +08:00
|
|
|
#nullable enable
|
|
|
|
|
2021-02-01 18:37:19 +08:00
|
|
|
using System;
|
|
|
|
using System.Collections.Generic;
|
2021-02-02 18:02:09 +08:00
|
|
|
using System.Diagnostics.CodeAnalysis;
|
2021-02-01 18:37:19 +08:00
|
|
|
using System.Linq;
|
2021-04-12 08:37:03 +08:00
|
|
|
using osu.Game.Online.API;
|
2021-11-17 10:30:47 +08:00
|
|
|
using osu.Game.Rulesets;
|
2021-02-01 18:37:19 +08:00
|
|
|
using osu.Game.Rulesets.Mods;
|
|
|
|
|
|
|
|
namespace osu.Game.Utils
|
|
|
|
{
|
|
|
|
/// <summary>
|
|
|
|
/// A set of utilities to handle <see cref="Mod"/> combinations.
|
|
|
|
/// </summary>
|
|
|
|
public static class ModUtils
|
|
|
|
{
|
|
|
|
/// <summary>
|
|
|
|
/// Checks that all <see cref="Mod"/>s are compatible with each-other, and that all appear within a set of allowed types.
|
|
|
|
/// </summary>
|
|
|
|
/// <remarks>
|
|
|
|
/// The allowed types must contain exact <see cref="Mod"/> types for the respective <see cref="Mod"/>s to be allowed.
|
|
|
|
/// </remarks>
|
|
|
|
/// <param name="combination">The <see cref="Mod"/>s to check.</param>
|
|
|
|
/// <param name="allowedTypes">The set of allowed <see cref="Mod"/> types.</param>
|
|
|
|
/// <returns>Whether all <see cref="Mod"/>s are compatible with each-other and appear in the set of allowed types.</returns>
|
|
|
|
public static bool CheckCompatibleSetAndAllowed(IEnumerable<Mod> combination, IEnumerable<Type> allowedTypes)
|
|
|
|
{
|
|
|
|
// Prevent multiple-enumeration.
|
|
|
|
var combinationList = combination as ICollection<Mod> ?? combination.ToArray();
|
2021-02-02 17:18:57 +08:00
|
|
|
return CheckCompatibleSet(combinationList, out _) && CheckAllowed(combinationList, allowedTypes);
|
2021-02-01 18:37:19 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
/// Checks that all <see cref="Mod"/>s in a combination are compatible with each-other.
|
|
|
|
/// </summary>
|
|
|
|
/// <param name="combination">The <see cref="Mod"/> combination to check.</param>
|
|
|
|
/// <returns>Whether all <see cref="Mod"/>s in the combination are compatible with each-other.</returns>
|
|
|
|
public static bool CheckCompatibleSet(IEnumerable<Mod> combination)
|
2021-02-02 17:18:57 +08:00
|
|
|
=> CheckCompatibleSet(combination, out _);
|
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
/// Checks that all <see cref="Mod"/>s in a combination are compatible with each-other.
|
|
|
|
/// </summary>
|
|
|
|
/// <param name="combination">The <see cref="Mod"/> combination to check.</param>
|
|
|
|
/// <param name="invalidMods">Any invalid mods in the set.</param>
|
|
|
|
/// <returns>Whether all <see cref="Mod"/>s in the combination are compatible with each-other.</returns>
|
2021-02-02 18:02:09 +08:00
|
|
|
public static bool CheckCompatibleSet(IEnumerable<Mod> combination, [NotNullWhen(false)] out List<Mod>? invalidMods)
|
2021-02-01 18:37:19 +08:00
|
|
|
{
|
2021-06-22 16:41:20 +08:00
|
|
|
var mods = FlattenMods(combination).ToArray();
|
2021-02-02 17:18:57 +08:00
|
|
|
invalidMods = null;
|
2021-02-01 18:37:19 +08:00
|
|
|
|
2021-06-22 16:41:20 +08:00
|
|
|
// 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)
|
2021-02-01 18:37:19 +08:00
|
|
|
{
|
2021-02-02 17:18:57 +08:00
|
|
|
foreach (var type in mod.IncompatibleMods)
|
2021-02-01 18:37:19 +08:00
|
|
|
{
|
2021-06-22 16:41:20 +08:00
|
|
|
foreach (var invalid in mods.Where(m => type.IsInstanceOfType(m)))
|
2021-02-02 17:18:57 +08:00
|
|
|
{
|
2021-06-18 12:17:32 +08:00
|
|
|
if (invalid == mod)
|
|
|
|
continue;
|
|
|
|
|
2021-02-02 17:18:57 +08:00
|
|
|
invalidMods ??= new List<Mod>();
|
|
|
|
invalidMods.Add(invalid);
|
|
|
|
}
|
2021-02-01 18:37:19 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-02-02 17:18:57 +08:00
|
|
|
return invalidMods == null;
|
2021-02-01 18:37:19 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
/// Checks that all <see cref="Mod"/>s in a combination appear within a set of allowed types.
|
|
|
|
/// </summary>
|
|
|
|
/// <remarks>
|
|
|
|
/// The set of allowed types must contain exact <see cref="Mod"/> types for the respective <see cref="Mod"/>s to be allowed.
|
|
|
|
/// </remarks>
|
|
|
|
/// <param name="combination">The <see cref="Mod"/> combination to check.</param>
|
|
|
|
/// <param name="allowedTypes">The set of allowed <see cref="Mod"/> types.</param>
|
|
|
|
/// <returns>Whether all <see cref="Mod"/>s in the combination are allowed.</returns>
|
|
|
|
public static bool CheckAllowed(IEnumerable<Mod> combination, IEnumerable<Type> allowedTypes)
|
|
|
|
{
|
|
|
|
var allowedSet = new HashSet<Type>(allowedTypes);
|
|
|
|
|
|
|
|
return combination.SelectMany(FlattenMod)
|
|
|
|
.All(m => allowedSet.Contains(m.GetType()));
|
|
|
|
}
|
|
|
|
|
2021-02-01 19:20:10 +08:00
|
|
|
/// <summary>
|
|
|
|
/// Check the provided combination of mods are valid for a local gameplay session.
|
|
|
|
/// </summary>
|
|
|
|
/// <param name="mods">The mods to check.</param>
|
2021-02-02 13:14:31 +08:00
|
|
|
/// <param name="invalidMods">Invalid mods, if any were found. Can be null if all mods were valid.</param>
|
2021-02-01 19:20:10 +08:00
|
|
|
/// <returns>Whether the input mods were all valid. If false, <paramref name="invalidMods"/> will contain all invalid entries.</returns>
|
2021-05-15 05:35:06 +08:00
|
|
|
public static bool CheckValidForGameplay(IEnumerable<Mod> mods, [NotNullWhen(false)] out List<Mod>? invalidMods)
|
2021-02-01 19:20:10 +08:00
|
|
|
{
|
|
|
|
mods = mods.ToArray();
|
|
|
|
|
2021-02-02 17:42:02 +08:00
|
|
|
CheckCompatibleSet(mods, out invalidMods);
|
2021-02-01 19:20:10 +08:00
|
|
|
|
|
|
|
foreach (var mod in mods)
|
|
|
|
{
|
2021-02-02 17:42:02 +08:00
|
|
|
if (mod.Type == ModType.System || !mod.HasImplementation || mod is MultiMod)
|
2021-02-01 19:20:10 +08:00
|
|
|
{
|
2021-02-02 17:42:02 +08:00
|
|
|
invalidMods ??= new List<Mod>();
|
|
|
|
invalidMods.Add(mod);
|
2021-02-01 19:20:10 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-02-02 17:42:02 +08:00
|
|
|
return invalidMods == null;
|
2021-02-01 19:20:10 +08:00
|
|
|
}
|
|
|
|
|
2021-02-01 18:37:19 +08:00
|
|
|
/// <summary>
|
|
|
|
/// Flattens a set of <see cref="Mod"/>s, returning a new set with all <see cref="MultiMod"/>s removed.
|
|
|
|
/// </summary>
|
|
|
|
/// <param name="mods">The set of <see cref="Mod"/>s to flatten.</param>
|
|
|
|
/// <returns>The new set, containing all <see cref="Mod"/>s in <paramref name="mods"/> recursively with all <see cref="MultiMod"/>s removed.</returns>
|
|
|
|
public static IEnumerable<Mod> FlattenMods(IEnumerable<Mod> mods) => mods.SelectMany(FlattenMod);
|
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
/// Flattens a <see cref="Mod"/>, returning a set of <see cref="Mod"/>s in-place of any <see cref="MultiMod"/>s.
|
|
|
|
/// </summary>
|
|
|
|
/// <param name="mod">The <see cref="Mod"/> to flatten.</param>
|
|
|
|
/// <returns>A set of singular "flattened" <see cref="Mod"/>s</returns>
|
|
|
|
public static IEnumerable<Mod> FlattenMod(Mod mod)
|
|
|
|
{
|
|
|
|
if (mod is MultiMod multi)
|
|
|
|
{
|
|
|
|
foreach (var m in multi.Mods.SelectMany(FlattenMod))
|
|
|
|
yield return m;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
yield return mod;
|
|
|
|
}
|
2021-04-12 08:37:03 +08:00
|
|
|
|
2021-11-17 10:30:47 +08:00
|
|
|
/// <summary>
|
2022-03-17 10:51:12 +08:00
|
|
|
/// Returns an instantiated list of all proposed mods on a given ruleset.
|
2021-11-17 10:30:47 +08:00
|
|
|
/// </summary>
|
2022-03-17 10:51:12 +08:00
|
|
|
/// <param name="ruleset">The ruleset to instantiate mods.</param>
|
2021-11-17 10:30:47 +08:00
|
|
|
/// <param name="proposedMods">The proposed mods.</param>
|
2022-03-17 10:51:12 +08:00
|
|
|
/// <param name="mods">Mods instantiated from <paramref name="proposedMods"/> on the given <paramref name="ruleset"/>.</param>
|
|
|
|
public static void InstantiateModsForRuleset(Ruleset ruleset, IEnumerable<APIMod> proposedMods, out List<Mod> mods)
|
2021-11-17 10:30:47 +08:00
|
|
|
{
|
2022-03-17 10:51:12 +08:00
|
|
|
mods = new List<Mod>();
|
2021-11-17 10:30:47 +08:00
|
|
|
|
|
|
|
foreach (var apiMod in proposedMods)
|
2022-03-17 10:51:12 +08:00
|
|
|
mods.Add(apiMod.ToMod(ruleset));
|
2021-11-17 10:30:47 +08:00
|
|
|
}
|
2021-02-01 18:37:19 +08:00
|
|
|
}
|
|
|
|
}
|