From ffdb229935afc7fff8ed2fc87d5d812b701e41eb Mon Sep 17 00:00:00 2001 From: Berkan Diler Date: Sun, 18 Feb 2024 19:32:13 +0100 Subject: [PATCH] Use ObjectDisposedException.ThrowIf throw heleper --- osu.Framework/Audio/AudioComponent.cs | 3 +-- osu.Framework/Audio/Sample/Sample.cs | 3 +-- osu.Framework/Audio/Sample/SampleChannel.cs | 3 +-- osu.Framework/Audio/Sample/SampleStore.cs | 2 +- osu.Framework/Audio/Track/TrackBass.cs | 3 +-- osu.Framework/Audio/Track/TrackStore.cs | 4 ++-- .../Graphics/Containers/CompositeDrawable.cs | 6 ++---- osu.Framework/Graphics/Containers/Container.cs | 4 +--- osu.Framework/Graphics/Drawable.cs | 9 +++------ .../Graphics/OpenGL/Buffers/GLVertexBuffer.cs | 3 +-- osu.Framework/Graphics/OpenGL/Shaders/GLShader.cs | 15 +++++---------- .../Graphics/OpenGL/Textures/GLTexture.cs | 3 +-- osu.Framework/Graphics/Rendering/Renderer.cs | 3 +-- .../Graphics/Rendering/RendererExtensions.cs | 6 ++---- osu.Framework/Graphics/Textures/Texture.cs | 3 +-- .../Graphics/Veldrid/Shaders/VeldridShader.cs | 6 ++---- osu.Framework/Platform/Windows/Native/Imm.cs | 3 +-- 17 files changed, 27 insertions(+), 52 deletions(-) diff --git a/osu.Framework/Audio/AudioComponent.cs b/osu.Framework/Audio/AudioComponent.cs index e757780688..77794ced34 100644 --- a/osu.Framework/Audio/AudioComponent.cs +++ b/osu.Framework/Audio/AudioComponent.cs @@ -74,8 +74,7 @@ public void Update() { ThreadSafety.EnsureNotUpdateThread(); - if (IsDisposed) - throw new ObjectDisposedException(ToString(), "Can not update disposed audio components."); + ObjectDisposedException.ThrowIf(IsDisposed, this); FrameStatistics.Add(StatisticsCounterType.TasksRun, PendingActions.Count); FrameStatistics.Increment(StatisticsCounterType.Components); diff --git a/osu.Framework/Audio/Sample/Sample.cs b/osu.Framework/Audio/Sample/Sample.cs index 1fe2e42578..54bd3f1174 100644 --- a/osu.Framework/Audio/Sample/Sample.cs +++ b/osu.Framework/Audio/Sample/Sample.cs @@ -33,8 +33,7 @@ public SampleChannel Play() public SampleChannel GetChannel() { - if (IsDisposed) - throw new ObjectDisposedException(ToString(), "Can not get a channel from a disposed sample."); + ObjectDisposedException.ThrowIf(IsDisposed, this); var channel = CreateChannel(); channel.OnPlay = onPlay; diff --git a/osu.Framework/Audio/Sample/SampleChannel.cs b/osu.Framework/Audio/Sample/SampleChannel.cs index d2b3da7fc9..4659004b37 100644 --- a/osu.Framework/Audio/Sample/SampleChannel.cs +++ b/osu.Framework/Audio/Sample/SampleChannel.cs @@ -22,8 +22,7 @@ protected SampleChannel(string name) public virtual void Play() { - if (IsDisposed) - throw new ObjectDisposedException(ToString(), "Can not play disposed sample channels."); + ObjectDisposedException.ThrowIf(IsDisposed, this); Played = true; OnPlay?.Invoke(this); diff --git a/osu.Framework/Audio/Sample/SampleStore.cs b/osu.Framework/Audio/Sample/SampleStore.cs index 5064b48d57..a47a9f15af 100644 --- a/osu.Framework/Audio/Sample/SampleStore.cs +++ b/osu.Framework/Audio/Sample/SampleStore.cs @@ -38,7 +38,7 @@ internal SampleStore([NotNull] IResourceStore store, [NotNull] AudioMixe public Sample Get(string name) { - if (IsDisposed) throw new ObjectDisposedException($"Cannot retrieve items for an already disposed {nameof(SampleStore)}"); + ObjectDisposedException.ThrowIf(IsDisposed, this); if (string.IsNullOrEmpty(name)) return null; diff --git a/osu.Framework/Audio/Track/TrackBass.cs b/osu.Framework/Audio/Track/TrackBass.cs index 05eef247b1..55088f81d8 100644 --- a/osu.Framework/Audio/Track/TrackBass.cs +++ b/osu.Framework/Audio/Track/TrackBass.cs @@ -256,8 +256,7 @@ private void setDirection(bool reverse) public override void Start() { - if (IsDisposed) - throw new ObjectDisposedException(ToString(), "Can not start disposed tracks."); + ObjectDisposedException.ThrowIf(IsDisposed, this); StartAsync().WaitSafely(); } diff --git a/osu.Framework/Audio/Track/TrackStore.cs b/osu.Framework/Audio/Track/TrackStore.cs index 83000a547d..495e3f0713 100644 --- a/osu.Framework/Audio/Track/TrackStore.cs +++ b/osu.Framework/Audio/Track/TrackStore.cs @@ -29,7 +29,7 @@ internal TrackStore([NotNull] IResourceStore store, [NotNull] AudioMixer public Track GetVirtual(double length = double.PositiveInfinity, string name = "virtual") { - if (IsDisposed) throw new ObjectDisposedException($"Cannot retrieve items for an already disposed {nameof(TrackStore)}"); + ObjectDisposedException.ThrowIf(IsDisposed, this); var track = new TrackVirtual(length, name); AddItem(track); @@ -38,7 +38,7 @@ public Track GetVirtual(double length = double.PositiveInfinity, string name = " public Track Get(string name) { - if (IsDisposed) throw new ObjectDisposedException($"Cannot retrieve items for an already disposed {nameof(TrackStore)}"); + ObjectDisposedException.ThrowIf(IsDisposed, this); if (string.IsNullOrEmpty(name)) return null; diff --git a/osu.Framework/Graphics/Containers/CompositeDrawable.cs b/osu.Framework/Graphics/Containers/CompositeDrawable.cs index 8f06f50dbe..2c775e1d0f 100644 --- a/osu.Framework/Graphics/Containers/CompositeDrawable.cs +++ b/osu.Framework/Graphics/Containers/CompositeDrawable.cs @@ -145,8 +145,7 @@ protected internal Task LoadComponentsAsync(IEnumerable co EnsureMutationAllowed($"load components via {nameof(LoadComponentsAsync)}"); - if (IsDisposed) - throw new ObjectDisposedException(ToString()); + ObjectDisposedException.ThrowIf(IsDisposed, this); disposalCancellationSource ??= new CancellationTokenSource(); @@ -222,8 +221,7 @@ protected void LoadComponents(IEnumerable components) wher if (LoadState < LoadState.Loading) throw new InvalidOperationException($"May not invoke {nameof(LoadComponent)} prior to this {nameof(CompositeDrawable)} being loaded."); - if (IsDisposed) - throw new ObjectDisposedException(ToString()); + ObjectDisposedException.ThrowIf(IsDisposed, this); loadComponents(components.ToList(), Dependencies, false); } diff --git a/osu.Framework/Graphics/Containers/Container.cs b/osu.Framework/Graphics/Containers/Container.cs index 015be59752..2c996f04b1 100644 --- a/osu.Framework/Graphics/Containers/Container.cs +++ b/osu.Framework/Graphics/Containers/Container.cs @@ -220,9 +220,7 @@ public virtual void Add(T drawable) throw new InvalidOperationException("Content may not be added to itself."); ArgumentNullException.ThrowIfNull(drawable); - - if (drawable.IsDisposed) - throw new ObjectDisposedException(nameof(drawable)); + ObjectDisposedException.ThrowIf(drawable.IsDisposed, drawable); if (Content == this) AddInternal(drawable); diff --git a/osu.Framework/Graphics/Drawable.cs b/osu.Framework/Graphics/Drawable.cs index c50fb4610c..fcf90930a8 100644 --- a/osu.Framework/Graphics/Drawable.cs +++ b/osu.Framework/Graphics/Drawable.cs @@ -243,8 +243,7 @@ internal void Load(IFrameBasedClock clock, IReadOnlyDependencyContainer dependen $"Tried to load long-running drawable type {GetType().ReadableName()} in a non-direct async context. See https://git.io/Je1YF for more details."); } - if (IsDisposed) - throw new ObjectDisposedException(ToString(), "Attempting to load an already disposed drawable."); + ObjectDisposedException.ThrowIf(IsDisposed, this); if (loadState == LoadState.NotLoaded) { @@ -466,8 +465,7 @@ protected internal Scheduler Scheduler /// False if the drawable should not be updated. public virtual bool UpdateSubTree() { - if (IsDisposed) - throw new ObjectDisposedException(ToString(), "Disposed Drawables may never be in the scene graph."); + ObjectDisposedException.ThrowIf(IsDisposed, this); if (ProcessCustomClock) customClock?.ProcessFrame(); @@ -1484,8 +1482,7 @@ public CompositeDrawable Parent get => parent; internal set { - if (IsDisposed) - throw new ObjectDisposedException(ToString(), "Disposed Drawables may never get a parent and return to the scene graph."); + ObjectDisposedException.ThrowIf(IsDisposed, this); if (value == null) ChildID = 0; diff --git a/osu.Framework/Graphics/OpenGL/Buffers/GLVertexBuffer.cs b/osu.Framework/Graphics/OpenGL/Buffers/GLVertexBuffer.cs index 524f925252..92036100b5 100644 --- a/osu.Framework/Graphics/OpenGL/Buffers/GLVertexBuffer.cs +++ b/osu.Framework/Graphics/OpenGL/Buffers/GLVertexBuffer.cs @@ -97,8 +97,7 @@ protected virtual void Dispose(bool disposing) public void Bind(bool forRendering) { - if (IsDisposed) - throw new ObjectDisposedException(ToString(), "Can not bind disposed vertex buffers."); + ObjectDisposedException.ThrowIf(IsDisposed, this); if (!isInitialised) { diff --git a/osu.Framework/Graphics/OpenGL/Shaders/GLShader.cs b/osu.Framework/Graphics/OpenGL/Shaders/GLShader.cs index b94033a466..624430a536 100644 --- a/osu.Framework/Graphics/OpenGL/Shaders/GLShader.cs +++ b/osu.Framework/Graphics/OpenGL/Shaders/GLShader.cs @@ -73,8 +73,7 @@ internal GLShader(GLRenderer renderer, string name, GLShaderPart[] parts, Shader private void compile() { - if (IsDisposed) - throw new ObjectDisposedException(ToString(), "Can not compile a disposed shader."); + ObjectDisposedException.ThrowIf(IsDisposed, this); if (IsLoaded) throw new InvalidOperationException("Attempting to compile an already-compiled shader."); @@ -92,8 +91,7 @@ private void compile() internal void EnsureShaderCompiled() { - if (IsDisposed) - throw new ObjectDisposedException(ToString(), "Can not compile a disposed shader."); + ObjectDisposedException.ThrowIf(IsDisposed, this); if (shaderCompileDelegate.State == RunState.Waiting) shaderCompileDelegate.RunTask(); @@ -101,8 +99,7 @@ internal void EnsureShaderCompiled() public void Bind() { - if (IsDisposed) - throw new ObjectDisposedException(ToString(), "Can not bind a disposed shader."); + ObjectDisposedException.ThrowIf(IsDisposed, this); if (IsBound) return; @@ -130,8 +127,7 @@ public void Unbind() public Uniform GetUniform(string name) where T : unmanaged, IEquatable { - if (IsDisposed) - throw new ObjectDisposedException(ToString(), "Can not retrieve uniforms from a disposed shader."); + ObjectDisposedException.ThrowIf(IsDisposed, this); EnsureShaderCompiled(); @@ -145,8 +141,7 @@ public virtual void BindUniformBlock(string blockName, IUniformBuffer buffer) if (buffer is not IGLUniformBuffer glBuffer) throw new ArgumentException($"Buffer must be an {nameof(IGLUniformBuffer)}."); - if (IsDisposed) - throw new ObjectDisposedException(ToString(), "Can not retrieve uniforms from a disposed shader."); + ObjectDisposedException.ThrowIf(IsDisposed, this); EnsureShaderCompiled(); diff --git a/osu.Framework/Graphics/OpenGL/Textures/GLTexture.cs b/osu.Framework/Graphics/OpenGL/Textures/GLTexture.cs index 2b22eccde7..a5ba2c5919 100644 --- a/osu.Framework/Graphics/OpenGL/Textures/GLTexture.cs +++ b/osu.Framework/Graphics/OpenGL/Textures/GLTexture.cs @@ -172,8 +172,7 @@ public virtual int TextureId { get { - if (!Available) - throw new ObjectDisposedException(ToString(), "Can not obtain ID of a disposed texture."); + ObjectDisposedException.ThrowIf(!Available, this); return textureId; } diff --git a/osu.Framework/Graphics/Rendering/Renderer.cs b/osu.Framework/Graphics/Rendering/Renderer.cs index 6e443a14e4..a5261aecec 100644 --- a/osu.Framework/Graphics/Rendering/Renderer.cs +++ b/osu.Framework/Graphics/Rendering/Renderer.cs @@ -830,8 +830,7 @@ private void freeUnusedVertexBuffers() public bool BindTexture(Texture texture, int unit, WrapMode? wrapModeS, WrapMode? wrapModeT) { - if (!texture.Available) - throw new ObjectDisposedException(nameof(texture), "Can not bind a disposed texture."); + ObjectDisposedException.ThrowIf(!texture.Available, texture); if (texture is TextureWhitePixel && lastBoundTextureIsAtlas[unit]) { diff --git a/osu.Framework/Graphics/Rendering/RendererExtensions.cs b/osu.Framework/Graphics/Rendering/RendererExtensions.cs index e56c7d0d14..848db8f702 100644 --- a/osu.Framework/Graphics/Rendering/RendererExtensions.cs +++ b/osu.Framework/Graphics/Rendering/RendererExtensions.cs @@ -30,8 +30,7 @@ public static class RendererExtensions public static void DrawTriangle(this IRenderer renderer, Texture texture, Triangle vertexTriangle, ColourInfo drawColour, RectangleF? textureRect = null, Action? vertexAction = null, Vector2? inflationPercentage = null, RectangleF? textureCoords = null) { - if (!texture.Available) - throw new ObjectDisposedException(texture.ToString(), "Can not draw a triangle with a disposed texture."); + ObjectDisposedException.ThrowIf(!texture.Available, texture); if (!renderer.BindTexture(texture)) return; @@ -121,8 +120,7 @@ public static void DrawTriangle(this IRenderer renderer, Texture texture, Triang public static void DrawQuad(this IRenderer renderer, Texture texture, Quad vertexQuad, ColourInfo drawColour, RectangleF? textureRect = null, Action? vertexAction = null, Vector2? inflationPercentage = null, Vector2? blendRangeOverride = null, RectangleF? textureCoords = null) { - if (!texture.Available) - throw new ObjectDisposedException(texture.ToString(), "Can not draw a quad with a disposed texture."); + ObjectDisposedException.ThrowIf(!texture.Available, texture); if (!renderer.BindTexture(texture)) return; diff --git a/osu.Framework/Graphics/Textures/Texture.cs b/osu.Framework/Graphics/Textures/Texture.cs index 0bf48a629b..5b1efd149e 100644 --- a/osu.Framework/Graphics/Textures/Texture.cs +++ b/osu.Framework/Graphics/Textures/Texture.cs @@ -192,8 +192,7 @@ internal int? MipLevel internal virtual void SetData(ITextureUpload upload, WrapMode wrapModeS, WrapMode wrapModeT, Opacity? opacity) { - if (!Available) - throw new ObjectDisposedException(ToString(), "Can not set data of a disposed texture."); + ObjectDisposedException.ThrowIf(!Available, this); if (upload.Bounds.Width > NativeTexture.MaxSize || upload.Bounds.Height > NativeTexture.MaxSize) throw new TextureTooLargeForGLException(); diff --git a/osu.Framework/Graphics/Veldrid/Shaders/VeldridShader.cs b/osu.Framework/Graphics/Veldrid/Shaders/VeldridShader.cs index 103caa6b87..27e2f0c806 100644 --- a/osu.Framework/Graphics/Veldrid/Shaders/VeldridShader.cs +++ b/osu.Framework/Graphics/Veldrid/Shaders/VeldridShader.cs @@ -59,8 +59,7 @@ public VeldridShader(VeldridRenderer renderer, string name, VeldridShaderPart[] internal void EnsureShaderInitialised() { - if (isDisposed) - throw new ObjectDisposedException(ToString(), "Can not compile a disposed shader."); + ObjectDisposedException.ThrowIf(isDisposed, this); if (shaderInitialiseDelegate.State == RunState.Waiting) shaderInitialiseDelegate.RunTask(); @@ -94,8 +93,7 @@ public void BindUniformBlock(string blockName, IUniformBuffer buffer) if (buffer is not IVeldridUniformBuffer veldridBuffer) throw new ArgumentException($"Buffer must be an {nameof(IVeldridUniformBuffer)}."); - if (isDisposed) - throw new ObjectDisposedException(ToString(), "Can not retrieve uniforms from a disposed shader."); + ObjectDisposedException.ThrowIf(isDisposed, this); EnsureShaderInitialised(); diff --git a/osu.Framework/Platform/Windows/Native/Imm.cs b/osu.Framework/Platform/Windows/Native/Imm.cs index 2134b7b795..912ade6b00 100644 --- a/osu.Framework/Platform/Windows/Native/Imm.cs +++ b/osu.Framework/Platform/Windows/Native/Imm.cs @@ -241,8 +241,7 @@ private bool tryGetCompositionTargetRange(out int targetStart, out int targetEnd /// Thrown if the was disposed/closed. private bool handleInvalidOrClosed() { - if (handle.IsClosed) - throw new ObjectDisposedException(handle.ToString(), $"Attempted to use a closed {nameof(InputContextHandle)}."); + ObjectDisposedException.ThrowIf(handle.IsClosed, handle); return handle.IsInvalid; }