1
0
mirror of https://github.com/ppy/osu.git synced 2024-09-21 16:07:24 +08:00

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

@ -4,6 +4,7 @@
using BenchmarkDotNet.Attributes;
using BenchmarkDotNet.Engines;
using osu.Game.Online.API;
using osu.Game.Rulesets.Mods;
using osu.Game.Rulesets.Osu;
namespace osu.Game.Benchmarks
@ -45,5 +46,17 @@ namespace osu.Game.Benchmarks
{
ruleset.GetAllModsForReference().Consume(new Consumer());
}
[Benchmark]
public void BenchmarkGetForAcronym()
{
ruleset.GetModForAcronym("DT");
}
[Benchmark]
public void BenchmarkGetForType()
{
ruleset.GetMod<ModDoubleTime>();
}
}
}

View File

@ -1,7 +1,6 @@
// 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.
using System.Linq;
using osu.Framework.Allocation;
using osu.Framework.Graphics;
using osu.Framework.Graphics.Containers;

View File

@ -26,7 +26,7 @@ namespace osu.Game.Online.API.Requests.Responses
var mods = Mods != null ? Mods.Select(acronym => rulesetInstance.GetModForAcronym(acronym)).Where(m => m != null).ToArray() : Array.Empty<Mod>();
// all API scores provided by this class are considered to be legacy.
mods = mods.Append(rulesetInstance.GetAllMods().OfType<ModClassic>().Single()).ToArray();
mods = mods.Append(rulesetInstance.GetMod<ModClassic>()).ToArray();
var scoreInfo = new ScoreInfo
{

View File

@ -166,7 +166,7 @@ namespace osu.Game
public OsuGameBase()
{
UseDevelopmentServer = DebugUtils.IsDebugBuild;
UseDevelopmentServer = false;
Name = @"osu!";
}

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;

View File

@ -67,7 +67,7 @@ namespace osu.Game.Scoring.Legacy
// lazer replays get a really high version number.
if (version < LegacyScoreEncoder.FIRST_LAZER_VERSION)
scoreInfo.Mods = scoreInfo.Mods.Append(currentRuleset.GetAllMods().OfType<ModClassic>().Single()).ToArray();
scoreInfo.Mods = scoreInfo.Mods.Append(currentRuleset.GetMod<ModClassic>()).ToArray();
currentBeatmap = workingBeatmap.GetPlayableBeatmap(currentRuleset.RulesetInfo, scoreInfo.Mods);
scoreInfo.Beatmap = currentBeatmap.BeatmapInfo;

View File

@ -67,7 +67,7 @@ namespace osu.Game.Tests.Visual
if (!AllowFail)
{
var noFailMod = ruleset.GetAllMods().FirstOrDefault(m => m is ModNoFail);
var noFailMod = ruleset.GetMod<ModNoFail>();
if (noFailMod != null)
SelectedMods.Value = new[] { noFailMod };
}