From 85280c746d83861997952d67ed0f78a8740e2ceb Mon Sep 17 00:00:00 2001 From: Misaki Date: Sun, 30 Nov 2025 19:06:31 +0900 Subject: [PATCH] Refactor error handling and improve type safety Refactored error handling across the codebase by replacing exceptions with `Result`-based error handling for better robustness and consistency. - Updated `ResultExtensions` to use `EqualityComparer` for comparisons. - Enhanced `RenderingContext` with `GetValueOrThrow` for resource validation and added type constraints for texture methods. - Introduced `CommandError` and `RecordError` in `D3D12CommandBuffer` for improved error tracking. - Refactored `D3D12ResourceDatabase` to use `Result` objects for resource queries. - Updated `ICommandBuffer` and `IResourceDatabase` interfaces to return `Result` objects. - Improved type safety by replacing `int` with `uint` where appropriate. - Simplified texture handling in `MeshRenderPass` with new `CreateTexture` logic. - Cleaned up project files by removing unused and redundant entries. These changes enhance code maintainability, improve error reporting, and ensure type safety throughout the project. --- Ghost.Core/Result.cs | 2 +- .../Ghost.Graphics.Test.csproj | 21 -- Ghost.Graphics/Core/RenderingContext.cs | 39 +++- Ghost.Graphics/D3D12/D3D12CommandBuffer.cs | 187 +++++++++++++----- Ghost.Graphics/D3D12/D3D12GraphicsEngine.cs | 3 +- Ghost.Graphics/D3D12/D3D12Renderer.cs | 2 +- .../D3D12/D3D12ResourceAllocator.cs | 1 + Ghost.Graphics/D3D12/D3D12ResourceDatabase.cs | 42 ++-- Ghost.Graphics/D3D12/D3D12SwapChain.cs | 7 +- Ghost.Graphics/Ghost.Graphics.csproj | 4 - Ghost.Graphics/RHI/Common.cs | 24 ++- Ghost.Graphics/RHI/ICommandBuffer.cs | 2 +- Ghost.Graphics/RHI/IResourceDatabase.cs | 4 +- Ghost.Graphics/RHI/RHIUtility.cs | 27 ++- Ghost.Graphics/RenderPasses/MeshRenderPass.cs | 5 +- 15 files changed, 244 insertions(+), 126 deletions(-) diff --git a/Ghost.Core/Result.cs b/Ghost.Core/Result.cs index 9f7428d..7db37c0 100644 --- a/Ghost.Core/Result.cs +++ b/Ghost.Core/Result.cs @@ -178,7 +178,7 @@ public static class ResultExtensions public static T GetValueOrThrow(this Result result, S expect) where S : Enum { - if (result.Status?.Equals(expect) ?? false) + if (!EqualityComparer.Default.Equals(result.Status, expect)) { throw new InvalidOperationException($"Operation failed: expected status {expect}, but got {result.Status}"); } diff --git a/Ghost.Graphics.Test/Ghost.Graphics.Test.csproj b/Ghost.Graphics.Test/Ghost.Graphics.Test.csproj index b85c415..7c904f9 100644 --- a/Ghost.Graphics.Test/Ghost.Graphics.Test.csproj +++ b/Ghost.Graphics.Test/Ghost.Graphics.Test.csproj @@ -57,18 +57,6 @@ - - - - MSBuild:Compile - - - - - - MSBuild:Compile - -