Add ability to lookup mod from a type specification

This commit is contained in:
Dean Herbert
2021-09-09 16:46:24 +09:00
parent 4d0530ca9d
commit 2edb851008
7 changed files with 32 additions and 7 deletions

View File

@ -24,7 +24,6 @@ using osu.Game.Scoring;
using osu.Game.Skinning;
using osu.Game.Users;
using JetBrains.Annotations;
using osu.Framework.Bindables;
using osu.Framework.Extensions;
using osu.Framework.Extensions.EnumExtensions;
using osu.Framework.Testing;
@ -86,6 +85,20 @@ namespace osu.Game.Rulesets
return null;
}
/// <summary>
/// Returns a fresh instance of the mod matching the specified type.
/// </summary>
public T GetMod<T>()
where T : Mod
{
var type = GetAllModsForReference().FirstOrDefault(m => m is T)?.GetType();
if (type != null)
return (T)Activator.CreateInstance(type);
return null;
}
public abstract IEnumerable<Mod> GetModsFor(ModType type);
/// <summary>
@ -166,7 +179,7 @@ namespace osu.Game.Rulesets
}
[CanBeNull]
public ModAutoplay GetAutoplayMod() => GetAllMods().OfType<ModAutoplay>().FirstOrDefault();
public ModAutoplay GetAutoplayMod() => GetMod<ModAutoplay>();
public virtual ISkin CreateLegacySkinProvider([NotNull] ISkin skin, IBeatmap beatmap) => null;