osu/osu.Game/Rulesets/Edit/Checks/CheckUnsnappedObjects.cs

100 lines
4.0 KiB
C#
Raw Normal View History

2021-04-25 03:34:54 +00:00
// 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;
using System.Collections.Generic;
using osu.Game.Rulesets.Edit.Checks.Components;
using osu.Game.Rulesets.Objects;
using osu.Game.Rulesets.Objects.Types;
namespace osu.Game.Rulesets.Edit.Checks
{
public class CheckUnsnappedObjects : ICheck
2021-04-25 03:34:54 +00:00
{
public const double UNSNAP_MS_THRESHOLD = 2;
2021-04-25 03:34:54 +00:00
public CheckMetadata Metadata { get; } = new CheckMetadata(CheckCategory.Timing, "Unsnapped hitobjects");
2021-04-25 03:34:54 +00:00
public IEnumerable<IssueTemplate> PossibleTemplates => new IssueTemplate[]
{
2021-04-27 11:56:05 +00:00
new IssueTemplateLargeUnsnap(this),
new IssueTemplateSmallUnsnap(this)
2021-04-25 03:34:54 +00:00
};
2021-05-13 09:24:22 +00:00
public IEnumerable<Issue> Run(BeatmapVerifierContext context)
2021-04-25 03:34:54 +00:00
{
2021-05-13 09:24:22 +00:00
var controlPointInfo = context.Beatmap.ControlPointInfo;
2021-05-13 09:24:22 +00:00
foreach (var hitobject in context.Beatmap.HitObjects)
2021-04-25 03:34:54 +00:00
{
double startUnsnap = hitobject.StartTime - controlPointInfo.GetClosestSnappedTime(hitobject.StartTime);
2021-04-25 03:34:54 +00:00
string startPostfix = hitobject is IHasDuration ? "start" : "";
foreach (var issue in getUnsnapIssues(hitobject, startUnsnap, hitobject.StartTime, startPostfix))
yield return issue;
if (hitobject is IHasRepeats hasRepeats)
{
for (int repeatIndex = 0; repeatIndex < hasRepeats.RepeatCount; ++repeatIndex)
{
double spanDuration = hasRepeats.Duration / (hasRepeats.RepeatCount + 1);
double repeatTime = hitobject.StartTime + spanDuration * (repeatIndex + 1);
double repeatUnsnap = repeatTime - controlPointInfo.GetClosestSnappedTime(repeatTime);
2021-04-25 03:34:54 +00:00
foreach (var issue in getUnsnapIssues(hitobject, repeatUnsnap, repeatTime, "repeat"))
yield return issue;
}
}
if (hitobject is IHasDuration hasDuration)
{
double endUnsnap = hasDuration.EndTime - controlPointInfo.GetClosestSnappedTime(hasDuration.EndTime);
2021-04-25 03:34:54 +00:00
foreach (var issue in getUnsnapIssues(hitobject, endUnsnap, hasDuration.EndTime, "end"))
yield return issue;
}
}
}
private IEnumerable<Issue> getUnsnapIssues(HitObject hitobject, double unsnap, double time, string postfix = "")
{
if (Math.Abs(unsnap) >= UNSNAP_MS_THRESHOLD)
2021-04-27 11:56:05 +00:00
yield return new IssueTemplateLargeUnsnap(this).Create(hitobject, unsnap, time, postfix);
2021-04-25 03:34:54 +00:00
else if (Math.Abs(unsnap) >= 1)
2021-04-27 11:56:05 +00:00
yield return new IssueTemplateSmallUnsnap(this).Create(hitobject, unsnap, time, postfix);
2021-04-25 03:34:54 +00:00
// We don't care about unsnaps < 1 ms, as all object ends have these due to the way SV works.
}
public abstract class IssueTemplateUnsnap : IssueTemplate
{
protected IssueTemplateUnsnap(ICheck check, IssueType type)
2021-04-26 14:17:38 +00:00
: base(check, type, "{0} is unsnapped by {1:0.##} ms.")
2021-04-25 03:34:54 +00:00
{
}
public Issue Create(HitObject hitobject, double unsnap, double time, string postfix = "")
{
string objectName = hitobject.GetType().Name;
if (!string.IsNullOrEmpty(postfix))
objectName += " " + postfix;
return new Issue(hitobject, this, objectName, unsnap) { Time = time };
}
}
2021-04-27 11:56:05 +00:00
public class IssueTemplateLargeUnsnap : IssueTemplateUnsnap
2021-04-25 03:34:54 +00:00
{
2021-04-27 11:56:05 +00:00
public IssueTemplateLargeUnsnap(ICheck check)
2021-04-25 03:34:54 +00:00
: base(check, IssueType.Problem)
{
}
}
2021-04-27 11:56:05 +00:00
public class IssueTemplateSmallUnsnap : IssueTemplateUnsnap
2021-04-25 03:34:54 +00:00
{
2021-04-27 11:56:05 +00:00
public IssueTemplateSmallUnsnap(ICheck check)
2021-04-25 03:34:54 +00:00
: base(check, IssueType.Negligible)
{
}
}
}
}