Skip to content

Commit 35b0ff8

Browse files
committed
Mark MathHelper.Clamp() as banned API
See previous commit for partial rationale. There's an argument to be made about the `NaN`-spreading semantics being desirable because at least something will loudly fail in that case, but I'm not so sure about that these days. It feels like either way if `NaN`s are produced, then things are outside of any control, and chances are the game can probably continue without crashing. And, this move reduces our dependence on osuTK, which has already been living on borrowed time for years now and is only awaiting someone brave to go excise it.
1 parent ffef6ae commit 35b0ff8

File tree

10 files changed

+23
-17
lines changed

10 files changed

+23
-17
lines changed

CodeAnalysis/BannedSymbols.txt

+3
Original file line numberDiff line numberDiff line change
@@ -18,3 +18,6 @@ M:Humanizer.InflectorExtensions.Pascalize(System.String);Humanizer's .Pascalize(
1818
M:Humanizer.InflectorExtensions.Camelize(System.String);Humanizer's .Camelize() extension method changes behaviour depending on CultureInfo.CurrentCulture. Use StringDehumanizeExtensions.ToCamelCase() instead.
1919
M:Humanizer.InflectorExtensions.Underscore(System.String);Humanizer's .Underscore() extension method changes behaviour depending on CultureInfo.CurrentCulture. Use StringDehumanizeExtensions.ToSnakeCase() instead.
2020
M:Humanizer.InflectorExtensions.Kebaberize(System.String);Humanizer's .Kebaberize() extension method changes behaviour depending on CultureInfo.CurrentCulture. Use StringDehumanizeExtensions.ToKebabCase() instead.
21+
M:osuTK.MathHelper.Clamp(System.Int32,System.Int32,System.Int32)~System.Int32;Use Math.Clamp() instead.
22+
M:osuTK.MathHelper.Clamp(System.Single,System.Single,System.Single)~System.Single;This osuTK helper has unsafe semantics when one of the bounds provided is NaN. Use Math.Clamp() instead.
23+
M:osuTK.MathHelper.Clamp(System.Double,System.Double,System.Double)~System.Double;This osuTK helper has unsafe semantics when one of the bounds provided is NaN. Use Math.Clamp() instead.

Templates/Rulesets/ruleset-scrolling-example/osu.Game.Rulesets.Pippidon/Beatmaps/PippidonBeatmapConverter.cs

+2-2
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
// Copyright (c) ppy Pty Ltd <contact@ppy.sh>. Licensed under the MIT Licence.
22
// See the LICENCE file in the repository root for full licence text.
33

4+
using System;
45
using System.Collections.Generic;
56
using System.Linq;
67
using System.Threading;
@@ -9,7 +10,6 @@
910
using osu.Game.Rulesets.Objects.Types;
1011
using osu.Game.Rulesets.Pippidon.Objects;
1112
using osu.Game.Rulesets.Pippidon.UI;
12-
using osuTK;
1313

1414
namespace osu.Game.Rulesets.Pippidon.Beatmaps
1515
{
@@ -40,7 +40,7 @@ protected override IEnumerable<PippidonHitObject> ConvertHitObject(HitObject ori
4040
};
4141
}
4242

43-
private int getLane(HitObject hitObject) => (int)MathHelper.Clamp(
43+
private int getLane(HitObject hitObject) => (int)Math.Clamp(
4444
(getUsablePosition(hitObject) - minPosition) / (maxPosition - minPosition) * PippidonPlayfield.LANE_COUNT, 0, PippidonPlayfield.LANE_COUNT - 1);
4545

4646
private float getUsablePosition(HitObject h) => (h as IHasYPosition)?.Y ?? ((IHasXPosition)h).X;

osu.Game.Rulesets.Catch/Skinning/Argon/ArgonBananaPiece.cs

+2-1
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
// Copyright (c) ppy Pty Ltd <contact@ppy.sh>. Licensed under the MIT Licence.
22
// See the LICENCE file in the repository root for full licence text.
33

4+
using System;
45
using osu.Framework.Allocation;
56
using osu.Framework.Extensions.Color4Extensions;
67
using osu.Framework.Graphics;
@@ -110,7 +111,7 @@ protected override void Update()
110111

111112
double duration = ObjectState.HitObject.StartTime - ObjectState.DisplayStartTime;
112113

113-
fadeContent.Alpha = MathHelper.Clamp(
114+
fadeContent.Alpha = Math.Clamp(
114115
Interpolation.ValueAt(
115116
Time.Current, 1f, 0f,
116117
ObjectState.DisplayStartTime + duration * lens_flare_start,

osu.Game.Rulesets.Osu/Edit/Blueprints/HitCircles/Components/HitCircleOverlapMarker.cs

+2-1
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33

44
#nullable disable
55

6+
using System;
67
using osu.Framework.Allocation;
78
using osu.Framework.Bindables;
89
using osu.Framework.Graphics;
@@ -76,7 +77,7 @@ public override void UpdateFrom(HitCircle hitObject)
7677
if (hasReachedObject && showHitMarkers.Value)
7778
{
7879
float alpha = Interpolation.ValueAt(editorTime, 0, 1f, hitObjectTime, hitObjectTime + FADE_OUT_EXTENSION, Easing.In);
79-
float ringScale = MathHelper.Clamp(Interpolation.ValueAt(editorTime, 0, 1f, hitObjectTime, hitObjectTime + FADE_OUT_EXTENSION / 2, Easing.OutQuint), 0, 1);
80+
float ringScale = Math.Clamp(Interpolation.ValueAt(editorTime, 0, 1f, hitObjectTime, hitObjectTime + FADE_OUT_EXTENSION / 2, Easing.OutQuint), 0, 1);
8081

8182
ring.Scale = new Vector2(1 + 0.1f * ringScale);
8283
content.Alpha = 0.9f * (1 - alpha);

osu.Game.Rulesets.Osu/Edit/Blueprints/Sliders/SliderSelectionBlueprint.cs

+2-2
Original file line numberDiff line numberDiff line change
@@ -270,14 +270,14 @@ private void adjustLength(double proposedDistance, bool adjustVelocity)
270270
if (adjustVelocity)
271271
{
272272
proposedVelocity = proposedDistance / oldDuration;
273-
proposedDistance = MathHelper.Clamp(proposedDistance, 0.1 * oldDuration, 10 * oldDuration);
273+
proposedDistance = Math.Clamp(proposedDistance, 0.1 * oldDuration, 10 * oldDuration);
274274
}
275275
else
276276
{
277277
double minDistance = distanceSnapProvider?.GetBeatSnapDistance() * oldVelocityMultiplier ?? 1;
278278
// Add a small amount to the proposed distance to make it easier to snap to the full length of the slider.
279279
proposedDistance = distanceSnapProvider?.FindSnappedDistance((float)proposedDistance + 1, HitObject.StartTime, HitObject) ?? proposedDistance;
280-
proposedDistance = MathHelper.Clamp(proposedDistance, minDistance, HitObject.Path.CalculatedDistance);
280+
proposedDistance = Math.Clamp(proposedDistance, minDistance, HitObject.Path.CalculatedDistance);
281281
}
282282

283283
if (Precision.AlmostEquals(proposedDistance, HitObject.Path.Distance) && Precision.AlmostEquals(proposedVelocity, HitObject.SliderVelocityMultiplier))

osu.Game/Overlays/NotificationOverlayToastTray.cs

+1-1
Original file line numberDiff line numberDiff line change
@@ -174,7 +174,7 @@ protected override void Update()
174174
}
175175

176176
height = toastFlow.DrawHeight + 120;
177-
alpha = MathHelper.Clamp(toastFlow.DrawHeight / 41, 0, 1) * maxNotificationAlpha;
177+
alpha = Math.Clamp(toastFlow.DrawHeight / 41, 0, 1) * maxNotificationAlpha;
178178
}
179179

180180
toastContentBackground.Height = (float)Interpolation.DampContinuously(toastContentBackground.Height, height, 10, Clock.ElapsedFrameTime);

osu.Game/Screens/Play/HUDOverlay.cs

+3-3
Original file line numberDiff line numberDiff line change
@@ -278,17 +278,17 @@ protected override void Update()
278278
processDrawables(rulesetComponents);
279279

280280
if (lowestTopScreenSpaceRight.HasValue)
281-
TopRightElements.Y = MathHelper.Clamp(ToLocalSpace(new Vector2(0, lowestTopScreenSpaceRight.Value)).Y, 0, DrawHeight - TopRightElements.DrawHeight);
281+
TopRightElements.Y = Math.Clamp(ToLocalSpace(new Vector2(0, lowestTopScreenSpaceRight.Value)).Y, 0, DrawHeight - TopRightElements.DrawHeight);
282282
else
283283
TopRightElements.Y = 0;
284284

285285
if (lowestTopScreenSpaceLeft.HasValue)
286-
LeaderboardFlow.Y = MathHelper.Clamp(ToLocalSpace(new Vector2(0, lowestTopScreenSpaceLeft.Value)).Y, 0, DrawHeight - LeaderboardFlow.DrawHeight);
286+
LeaderboardFlow.Y = Math.Clamp(ToLocalSpace(new Vector2(0, lowestTopScreenSpaceLeft.Value)).Y, 0, DrawHeight - LeaderboardFlow.DrawHeight);
287287
else
288288
LeaderboardFlow.Y = 0;
289289

290290
if (highestBottomScreenSpace.HasValue)
291-
bottomRightElements.Y = BottomScoringElementsHeight = -MathHelper.Clamp(DrawHeight - ToLocalSpace(highestBottomScreenSpace.Value).Y, 0, DrawHeight - bottomRightElements.DrawHeight);
291+
bottomRightElements.Y = BottomScoringElementsHeight = -Math.Clamp(DrawHeight - ToLocalSpace(highestBottomScreenSpace.Value).Y, 0, DrawHeight - bottomRightElements.DrawHeight);
292292
else
293293
bottomRightElements.Y = 0;
294294

osu.Game/Screens/Utility/CircleGameplay.cs

+2-2
Original file line numberDiff line numberDiff line change
@@ -201,8 +201,8 @@ protected override void UpdateAtLimitedRate(InputState inputState)
201201
{
202202
double preempt = (float)IBeatmapDifficultyInfo.DifficultyRange(SampleApproachRate.Value, 1800, 1200, 450);
203203

204-
approach.Scale = new Vector2(1 + 4 * (float)MathHelper.Clamp((HitTime - Clock.CurrentTime) / preempt, 0, 100));
205-
Alpha = (float)MathHelper.Clamp((Clock.CurrentTime - HitTime + 600) / 400, 0, 1);
204+
approach.Scale = new Vector2(1 + 4 * (float)Math.Clamp((HitTime - Clock.CurrentTime) / preempt, 0, 100));
205+
Alpha = (float)Math.Clamp((Clock.CurrentTime - HitTime + 600) / 400, 0, 1);
206206

207207
if (Clock.CurrentTime > HitTime + duration)
208208
Expire();

osu.Game/Screens/Utility/SampleComponents/LatencyMovableBox.cs

+5-4
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
// Copyright (c) ppy Pty Ltd <contact@ppy.sh>. Licensed under the MIT Licence.
22
// See the LICENCE file in the repository root for full licence text.
33

4+
using System;
45
using osu.Framework.Graphics;
56
using osu.Framework.Graphics.Shapes;
67
using osu.Framework.Input.Events;
@@ -55,22 +56,22 @@ protected override void UpdateAtLimitedRate(InputState inputState)
5556
{
5657
case Key.F:
5758
case Key.Up:
58-
box.Y = MathHelper.Clamp(box.Y - movementAmount, 0.1f, 0.9f);
59+
box.Y = Math.Clamp(box.Y - movementAmount, 0.1f, 0.9f);
5960
break;
6061

6162
case Key.J:
6263
case Key.Down:
63-
box.Y = MathHelper.Clamp(box.Y + movementAmount, 0.1f, 0.9f);
64+
box.Y = Math.Clamp(box.Y + movementAmount, 0.1f, 0.9f);
6465
break;
6566

6667
case Key.Z:
6768
case Key.Left:
68-
box.X = MathHelper.Clamp(box.X - movementAmount, 0.1f, 0.9f);
69+
box.X = Math.Clamp(box.X - movementAmount, 0.1f, 0.9f);
6970
break;
7071

7172
case Key.X:
7273
case Key.Right:
73-
box.X = MathHelper.Clamp(box.X + movementAmount, 0.1f, 0.9f);
74+
box.X = Math.Clamp(box.X + movementAmount, 0.1f, 0.9f);
7475
break;
7576
}
7677
}

osu.Game/Screens/Utility/ScrollingGameplay.cs

+1-1
Original file line numberDiff line numberDiff line change
@@ -165,7 +165,7 @@ protected override void UpdateAtLimitedRate(InputState inputState)
165165
{
166166
double preempt = (float)IBeatmapDifficultyInfo.DifficultyRange(SampleApproachRate.Value, 1800, 1200, 450);
167167

168-
Alpha = (float)MathHelper.Clamp((Clock.CurrentTime - HitTime + 600) / 400, 0, 1);
168+
Alpha = (float)Math.Clamp((Clock.CurrentTime - HitTime + 600) / 400, 0, 1);
169169
Y = judgement_position - (float)((HitTime - Clock.CurrentTime) / preempt);
170170

171171
if (Clock.CurrentTime > HitTime + duration)

0 commit comments

Comments
 (0)