1
0
mirror of https://github.com/ppy/osu.git synced 2025-01-13 05:12:54 +08:00

Add ModUtils helper to instantiate mods from ruleset

This commit is contained in:
Dan Balasescu 2021-11-17 11:30:47 +09:00
parent c8038df509
commit f5b95c9e6d

View File

@ -8,6 +8,7 @@ using System.Diagnostics.CodeAnalysis;
using System.Linq;
using osu.Framework.Bindables;
using osu.Game.Online.API;
using osu.Game.Rulesets;
using osu.Game.Rulesets.Mods;
#nullable enable
@ -185,5 +186,33 @@ namespace osu.Game.Utils
return setting;
}
}
/// <summary>
/// Verifies all proposed mods are valid for a given ruleset and returns instantiated <see cref="Mod"/>s for further processing.
/// </summary>
/// <param name="ruleset">The ruleset to verify mods against.</param>
/// <param name="proposedMods">The proposed mods.</param>
/// <param name="valid">Mods instantiated from <paramref name="proposedMods"/> which were valid for the given <paramref name="ruleset"/>.</param>
/// <returns>Whether all <see cref="proposedMods"/> were valid for the given <paramref name="ruleset"/>.</returns>
public static bool InstantiateValidModsForRuleset(Ruleset ruleset, IEnumerable<APIMod> proposedMods, out List<Mod> valid)
{
valid = new List<Mod>();
bool proposedWereValid = true;
foreach (var apiMod in proposedMods)
{
try
{
// will throw if invalid
valid.Add(apiMod.ToMod(ruleset));
}
catch
{
proposedWereValid = false;
}
}
return proposedWereValid;
}
}
}