From 43841e210d2a1915d3c39bfe191ef62befbd3492 Mon Sep 17 00:00:00 2001 From: Berkan Diler Date: Tue, 5 Mar 2024 09:58:46 +0100 Subject: [PATCH 1/5] Use ObjectDisposedException.ThrowIf throw helper --- osu.Game/Database/RealmAccess.cs | 9 +++------ 1 file changed, 3 insertions(+), 6 deletions(-) diff --git a/osu.Game/Database/RealmAccess.cs b/osu.Game/Database/RealmAccess.cs index 4bd7f36cdd..167d170c81 100644 --- a/osu.Game/Database/RealmAccess.cs +++ b/osu.Game/Database/RealmAccess.cs @@ -489,8 +489,7 @@ namespace osu.Game.Database /// The work to run. public Task WriteAsync(Action action) { - if (isDisposed) - throw new ObjectDisposedException(nameof(RealmAccess)); + ObjectDisposedException.ThrowIf(isDisposed, this); // Required to ensure the write is tracked and accounted for before disposal. // Can potentially be avoided if we have a need to do so in the future. @@ -675,8 +674,7 @@ namespace osu.Game.Database private Realm getRealmInstance() { - if (isDisposed) - throw new ObjectDisposedException(nameof(RealmAccess)); + ObjectDisposedException.ThrowIf(isDisposed, this); bool tookSemaphoreLock = false; @@ -1189,8 +1187,7 @@ namespace osu.Game.Database if (!ThreadSafety.IsUpdateThread) throw new InvalidOperationException(@$"{nameof(BlockAllOperations)} must be called from the update thread."); - if (isDisposed) - throw new ObjectDisposedException(nameof(RealmAccess)); + ObjectDisposedException.ThrowIf(isDisposed, this); SynchronizationContext? syncContext = null; From 9bac60a98fc098cb4c6b1f118e61c5255bd5b235 Mon Sep 17 00:00:00 2001 From: Berkan Diler Date: Tue, 5 Mar 2024 10:19:47 +0100 Subject: [PATCH 2/5] Use ArgumentNullException.ThrowIfNull in more places --- osu.Game/Rulesets/UI/DrawableRuleset.cs | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) diff --git a/osu.Game/Rulesets/UI/DrawableRuleset.cs b/osu.Game/Rulesets/UI/DrawableRuleset.cs index 13e28279e6..bdc0ff85ba 100644 --- a/osu.Game/Rulesets/UI/DrawableRuleset.cs +++ b/osu.Game/Rulesets/UI/DrawableRuleset.cs @@ -135,8 +135,7 @@ namespace osu.Game.Rulesets.UI protected DrawableRuleset(Ruleset ruleset, IBeatmap beatmap, IReadOnlyList mods = null) : base(ruleset) { - if (beatmap == null) - throw new ArgumentNullException(nameof(beatmap), "Beatmap cannot be null."); + ArgumentNullException.ThrowIfNull(beatmap); if (!(beatmap is Beatmap tBeatmap)) throw new ArgumentException($"{GetType()} expected the beatmap to contain hitobjects of type {typeof(TObject)}.", nameof(beatmap)); From a89130348469e1b1ea8e025bb2c6cf4a85da51b3 Mon Sep 17 00:00:00 2001 From: Berkan Diler Date: Tue, 5 Mar 2024 10:20:30 +0100 Subject: [PATCH 3/5] Use ArgumentOutOfRangeException throw helper methods --- osu.Game.Rulesets.Osu/Beatmaps/OsuBeatmapProcessor.cs | 6 +++--- osu.Game.Rulesets.Osu/Edit/OsuHitObjectComposer.cs | 2 +- osu.Game/Beatmaps/Timing/TimeSignature.cs | 3 +-- osu.Game/Rulesets/Difficulty/Utils/ReverseQueue.cs | 3 +-- osu.Game/Rulesets/Objects/Types/PathType.cs | 3 +-- osu.Game/Screens/Ranking/Statistics/SimpleStatisticTable.cs | 3 +-- osu.Game/Utils/LimitedCapacityQueue.cs | 3 +-- osu.Game/Utils/StatelessRNG.cs | 2 +- 8 files changed, 10 insertions(+), 15 deletions(-) diff --git a/osu.Game.Rulesets.Osu/Beatmaps/OsuBeatmapProcessor.cs b/osu.Game.Rulesets.Osu/Beatmaps/OsuBeatmapProcessor.cs index 835c67ff19..9cc0a8c414 100644 --- a/osu.Game.Rulesets.Osu/Beatmaps/OsuBeatmapProcessor.cs +++ b/osu.Game.Rulesets.Osu/Beatmaps/OsuBeatmapProcessor.cs @@ -58,9 +58,9 @@ namespace osu.Game.Rulesets.Osu.Beatmaps private void applyStacking(Beatmap beatmap, int startIndex, int endIndex) { - if (startIndex > endIndex) throw new ArgumentOutOfRangeException(nameof(startIndex), $"{nameof(startIndex)} cannot be greater than {nameof(endIndex)}."); - if (startIndex < 0) throw new ArgumentOutOfRangeException(nameof(startIndex), $"{nameof(startIndex)} cannot be less than 0."); - if (endIndex < 0) throw new ArgumentOutOfRangeException(nameof(endIndex), $"{nameof(endIndex)} cannot be less than 0."); + ArgumentOutOfRangeException.ThrowIfGreaterThan(startIndex, endIndex); + ArgumentOutOfRangeException.ThrowIfNegative(startIndex); + ArgumentOutOfRangeException.ThrowIfNegative(endIndex); int extendedEndIndex = endIndex; diff --git a/osu.Game.Rulesets.Osu/Edit/OsuHitObjectComposer.cs b/osu.Game.Rulesets.Osu/Edit/OsuHitObjectComposer.cs index 448cfaf84c..3ead61f64a 100644 --- a/osu.Game.Rulesets.Osu/Edit/OsuHitObjectComposer.cs +++ b/osu.Game.Rulesets.Osu/Edit/OsuHitObjectComposer.cs @@ -334,7 +334,7 @@ namespace osu.Game.Rulesets.Osu.Edit /// The from a selected to a target . private OsuDistanceSnapGrid createGrid(Func sourceSelector, int targetOffset = 1) { - if (targetOffset < 1) throw new ArgumentOutOfRangeException(nameof(targetOffset)); + ArgumentOutOfRangeException.ThrowIfNegativeOrZero(targetOffset); int sourceIndex = -1; diff --git a/osu.Game/Beatmaps/Timing/TimeSignature.cs b/osu.Game/Beatmaps/Timing/TimeSignature.cs index 7499a725dc..377a878631 100644 --- a/osu.Game/Beatmaps/Timing/TimeSignature.cs +++ b/osu.Game/Beatmaps/Timing/TimeSignature.cs @@ -23,8 +23,7 @@ namespace osu.Game.Beatmaps.Timing public TimeSignature(int numerator) { - if (numerator < 1) - throw new ArgumentOutOfRangeException(nameof(numerator), numerator, "The numerator of a time signature must be positive."); + ArgumentOutOfRangeException.ThrowIfNegativeOrZero(numerator); Numerator = numerator; } diff --git a/osu.Game/Rulesets/Difficulty/Utils/ReverseQueue.cs b/osu.Game/Rulesets/Difficulty/Utils/ReverseQueue.cs index 3dc2d133ba..17c3c51cfb 100644 --- a/osu.Game/Rulesets/Difficulty/Utils/ReverseQueue.cs +++ b/osu.Game/Rulesets/Difficulty/Utils/ReverseQueue.cs @@ -27,8 +27,7 @@ namespace osu.Game.Rulesets.Difficulty.Utils public ReverseQueue(int initialCapacity) { - if (initialCapacity <= 0) - throw new ArgumentOutOfRangeException(nameof(initialCapacity)); + ArgumentOutOfRangeException.ThrowIfNegativeOrZero(initialCapacity); items = new T[initialCapacity]; capacity = initialCapacity; diff --git a/osu.Game/Rulesets/Objects/Types/PathType.cs b/osu.Game/Rulesets/Objects/Types/PathType.cs index 23f1ccf0bc..6983484dce 100644 --- a/osu.Game/Rulesets/Objects/Types/PathType.cs +++ b/osu.Game/Rulesets/Objects/Types/PathType.cs @@ -40,8 +40,7 @@ namespace osu.Game.Rulesets.Objects.Types public static PathType BSpline(int degree) { - if (degree <= 0) - throw new ArgumentOutOfRangeException(nameof(degree), "The degree of a B-Spline path must be greater than zero."); + ArgumentOutOfRangeException.ThrowIfNegativeOrZero(degree); return new PathType { Type = SplineType.BSpline, Degree = degree }; } diff --git a/osu.Game/Screens/Ranking/Statistics/SimpleStatisticTable.cs b/osu.Game/Screens/Ranking/Statistics/SimpleStatisticTable.cs index ed31bc8643..4abf0007a7 100644 --- a/osu.Game/Screens/Ranking/Statistics/SimpleStatisticTable.cs +++ b/osu.Game/Screens/Ranking/Statistics/SimpleStatisticTable.cs @@ -33,8 +33,7 @@ namespace osu.Game.Screens.Ranking.Statistics /// The s to display in this row. public SimpleStatisticTable(int columnCount, [ItemNotNull] IEnumerable items) { - if (columnCount < 1) - throw new ArgumentOutOfRangeException(nameof(columnCount)); + ArgumentOutOfRangeException.ThrowIfNegativeOrZero(columnCount); this.columnCount = columnCount; this.items = items.ToArray(); diff --git a/osu.Game/Utils/LimitedCapacityQueue.cs b/osu.Game/Utils/LimitedCapacityQueue.cs index d36aa8af2c..b96148d4a0 100644 --- a/osu.Game/Utils/LimitedCapacityQueue.cs +++ b/osu.Game/Utils/LimitedCapacityQueue.cs @@ -35,8 +35,7 @@ namespace osu.Game.Utils /// The number of items the queue can hold. public LimitedCapacityQueue(int capacity) { - if (capacity < 0) - throw new ArgumentOutOfRangeException(nameof(capacity)); + ArgumentOutOfRangeException.ThrowIfNegative(capacity); this.capacity = capacity; array = new T[capacity]; diff --git a/osu.Game/Utils/StatelessRNG.cs b/osu.Game/Utils/StatelessRNG.cs index 3db632fc42..8833dcbbdb 100644 --- a/osu.Game/Utils/StatelessRNG.cs +++ b/osu.Game/Utils/StatelessRNG.cs @@ -58,7 +58,7 @@ namespace osu.Game.Utils /// public static int NextInt(int maxValue, int seed, int series = 0) { - if (maxValue <= 0) throw new ArgumentOutOfRangeException(nameof(maxValue)); + ArgumentOutOfRangeException.ThrowIfNegativeOrZero(maxValue); return (int)(NextULong(seed, series) % (ulong)maxValue); } From 5965db4fd7344822d116aa7bd23ad2fa2ebfa22e Mon Sep 17 00:00:00 2001 From: Berkan Diler Date: Tue, 5 Mar 2024 10:20:57 +0100 Subject: [PATCH 4/5] Use ArgumentException.ThrowIfNullOrEmpty throw helper --- osu.Game/IO/WrappedStorage.cs | 3 +-- osu.Game/Online/API/OAuth.cs | 4 ++-- 2 files changed, 3 insertions(+), 4 deletions(-) diff --git a/osu.Game/IO/WrappedStorage.cs b/osu.Game/IO/WrappedStorage.cs index 95ff26db6a..5a8f4aef59 100644 --- a/osu.Game/IO/WrappedStorage.cs +++ b/osu.Game/IO/WrappedStorage.cs @@ -80,8 +80,7 @@ namespace osu.Game.IO public override Storage GetStorageForDirectory(string path) { - if (string.IsNullOrEmpty(path)) - throw new ArgumentException("Must be non-null and not empty string", nameof(path)); + ArgumentException.ThrowIfNullOrEmpty(path); if (!path.EndsWith(Path.DirectorySeparatorChar)) path += Path.DirectorySeparatorChar; diff --git a/osu.Game/Online/API/OAuth.cs b/osu.Game/Online/API/OAuth.cs index 4829310870..b06d9c6586 100644 --- a/osu.Game/Online/API/OAuth.cs +++ b/osu.Game/Online/API/OAuth.cs @@ -39,8 +39,8 @@ namespace osu.Game.Online.API internal void AuthenticateWithLogin(string username, string password) { - if (string.IsNullOrEmpty(username)) throw new ArgumentException("Missing username."); - if (string.IsNullOrEmpty(password)) throw new ArgumentException("Missing password."); + ArgumentException.ThrowIfNullOrEmpty(username); + ArgumentException.ThrowIfNullOrEmpty(password); var accessTokenRequest = new AccessTokenRequestPassword(username, password) { From d0f7ab3316584f92957cd738c57e6dfd85fb9fcc Mon Sep 17 00:00:00 2001 From: Berkan Diler Date: Tue, 5 Mar 2024 17:18:59 +0100 Subject: [PATCH 5/5] Revert some changes --- osu.Game/Online/API/OAuth.cs | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/osu.Game/Online/API/OAuth.cs b/osu.Game/Online/API/OAuth.cs index b06d9c6586..4829310870 100644 --- a/osu.Game/Online/API/OAuth.cs +++ b/osu.Game/Online/API/OAuth.cs @@ -39,8 +39,8 @@ namespace osu.Game.Online.API internal void AuthenticateWithLogin(string username, string password) { - ArgumentException.ThrowIfNullOrEmpty(username); - ArgumentException.ThrowIfNullOrEmpty(password); + if (string.IsNullOrEmpty(username)) throw new ArgumentException("Missing username."); + if (string.IsNullOrEmpty(password)) throw new ArgumentException("Missing password."); var accessTokenRequest = new AccessTokenRequestPassword(username, password) {