1
0
mirror of https://github.com/ppy/osu.git synced 2025-01-06 10:22:54 +08:00

Avoid returning a live IEnumerable

This commit is contained in:
Dean Herbert 2021-10-05 16:59:54 +09:00
parent fa7f11d906
commit 599d82e383
3 changed files with 13 additions and 9 deletions

View File

@ -148,9 +148,9 @@ namespace osu.Game.Beatmaps
}, token, TaskCreationOptions.HideScheduler | TaskCreationOptions.RunContinuationsAsynchronously, updateScheduler); }, token, TaskCreationOptions.HideScheduler | TaskCreationOptions.RunContinuationsAsynchronously, updateScheduler);
} }
public Task<TimedDifficultyAttributes[]> GetTimedDifficultyAttributesAsync(WorkingBeatmap beatmap, Ruleset ruleset, Mod[] mods, CancellationToken token = default) public Task<List<TimedDifficultyAttributes>> GetTimedDifficultyAttributesAsync(WorkingBeatmap beatmap, Ruleset ruleset, Mod[] mods, CancellationToken token = default)
{ {
return Task.Factory.StartNew(() => ruleset.CreateDifficultyCalculator(beatmap).CalculateTimed(mods).ToArray(), return Task.Factory.StartNew(() => ruleset.CreateDifficultyCalculator(beatmap).CalculateTimed(mods),
token, token,
TaskCreationOptions.HideScheduler | TaskCreationOptions.RunContinuationsAsynchronously, TaskCreationOptions.HideScheduler | TaskCreationOptions.RunContinuationsAsynchronously,
updateScheduler); updateScheduler);

View File

@ -58,12 +58,14 @@ namespace osu.Game.Rulesets.Difficulty
return CreateDifficultyAttributes(Beatmap, playableMods, skills, clockRate); return CreateDifficultyAttributes(Beatmap, playableMods, skills, clockRate);
} }
public IEnumerable<TimedDifficultyAttributes> CalculateTimed(params Mod[] mods) public List<TimedDifficultyAttributes> CalculateTimed(params Mod[] mods)
{ {
preProcess(mods); preProcess(mods);
var attribs = new List<TimedDifficultyAttributes>();
if (!Beatmap.HitObjects.Any()) if (!Beatmap.HitObjects.Any())
yield break; return attribs;
var skills = CreateSkills(Beatmap, playableMods, clockRate); var skills = CreateSkills(Beatmap, playableMods, clockRate);
var progressiveBeatmap = new ProgressiveCalculationBeatmap(Beatmap); var progressiveBeatmap = new ProgressiveCalculationBeatmap(Beatmap);
@ -75,8 +77,10 @@ namespace osu.Game.Rulesets.Difficulty
foreach (var skill in skills) foreach (var skill in skills)
skill.ProcessInternal(hitObject); skill.ProcessInternal(hitObject);
yield return new TimedDifficultyAttributes(hitObject.EndTime, CreateDifficultyAttributes(progressiveBeatmap, playableMods, skills, clockRate)); attribs.Add(new TimedDifficultyAttributes(hitObject.EndTime, CreateDifficultyAttributes(progressiveBeatmap, playableMods, skills, clockRate)));
} }
return attribs;
} }
/// <summary> /// <summary>

View File

@ -47,7 +47,7 @@ namespace osu.Game.Screens.Play.HUD
private GameplayState gameplayState { get; set; } private GameplayState gameplayState { get; set; }
[CanBeNull] [CanBeNull]
private TimedDifficultyAttributes[] timedAttributes; private List<TimedDifficultyAttributes> timedAttributes;
private readonly CancellationTokenSource loadCancellationSource = new CancellationTokenSource(); private readonly CancellationTokenSource loadCancellationSource = new CancellationTokenSource();
@ -110,14 +110,14 @@ namespace osu.Game.Screens.Play.HUD
[CanBeNull] [CanBeNull]
private DifficultyAttributes getAttributeAtTime(JudgementResult judgement) private DifficultyAttributes getAttributeAtTime(JudgementResult judgement)
{ {
if (timedAttributes == null || timedAttributes.Length == 0) if (timedAttributes == null || timedAttributes.Count == 0)
return null; return null;
int attribIndex = Array.BinarySearch(timedAttributes, 0, timedAttributes.Length, new TimedDifficultyAttributes(judgement.HitObject.GetEndTime(), null)); int attribIndex = timedAttributes.BinarySearch(new TimedDifficultyAttributes(judgement.HitObject.GetEndTime(), null));
if (attribIndex < 0) if (attribIndex < 0)
attribIndex = ~attribIndex - 1; attribIndex = ~attribIndex - 1;
return timedAttributes[Math.Clamp(attribIndex, 0, timedAttributes.Length - 1)].Attributes; return timedAttributes[Math.Clamp(attribIndex, 0, timedAttributes.Count - 1)].Attributes;
} }
private void onJudgementReverted(JudgementResult obj) => IsValid = false; private void onJudgementReverted(JudgementResult obj) => IsValid = false;