From f5b95c9e6d73b8819ca80793a433ecb4d1548085 Mon Sep 17 00:00:00 2001 From: Dan Balasescu Date: Wed, 17 Nov 2021 11:30:47 +0900 Subject: [PATCH] Add ModUtils helper to instantiate mods from ruleset --- osu.Game/Utils/ModUtils.cs | 29 +++++++++++++++++++++++++++++ 1 file changed, 29 insertions(+) diff --git a/osu.Game/Utils/ModUtils.cs b/osu.Game/Utils/ModUtils.cs index 7485950f47..eb5deef6ea 100644 --- a/osu.Game/Utils/ModUtils.cs +++ b/osu.Game/Utils/ModUtils.cs @@ -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; } } + + /// + /// Verifies all proposed mods are valid for a given ruleset and returns instantiated s for further processing. + /// + /// The ruleset to verify mods against. + /// The proposed mods. + /// Mods instantiated from which were valid for the given . + /// Whether all were valid for the given . + public static bool InstantiateValidModsForRuleset(Ruleset ruleset, IEnumerable proposedMods, out List valid) + { + valid = new List(); + bool proposedWereValid = true; + + foreach (var apiMod in proposedMods) + { + try + { + // will throw if invalid + valid.Add(apiMod.ToMod(ruleset)); + } + catch + { + proposedWereValid = false; + } + } + + return proposedWereValid; + } } }