Refactor and enhance codebase for maintainability

Refactored and reorganized the codebase to improve readability, performance, and maintainability. Introduced new interfaces and structs for better resource management, updated project configuration files, and refactored shader and graphics pipeline management. Improved error handling, code formatting, and removed unused code and namespaces. Updated DLL references and method signatures for consistency and maintainability.
This commit is contained in:
2025-10-22 18:46:39 +09:00
parent 6d1b510ac1
commit d2d9f5feb7
80 changed files with 2836 additions and 2198 deletions

View File

@@ -0,0 +1,6 @@
namespace Ghost.Core.Contracts;
internal interface IReleasable
{
void InternalRelease();
}

View File

@@ -7,7 +7,18 @@
<AllowUnsafeBlocks>True</AllowUnsafeBlocks>
</PropertyGroup>
<PropertyGroup Condition="'$(Configuration)|$(Platform)'=='Debug|AnyCPU'">
<IsAotCompatible>True</IsAotCompatible>
<DefineConstants>$(DefineConstants);PLATEFORME_WIN64</DefineConstants>
</PropertyGroup>
<PropertyGroup Condition="'$(Configuration)|$(Platform)'=='Release|AnyCPU'">
<IsAotCompatible>True</IsAotCompatible>
<DefineConstants>$(DefineConstants);PLATEFORME_WIN64</DefineConstants>
</PropertyGroup>
<ItemGroup>
<PackageReference Include="System.IO.Hashing" Version="9.0.10" />
<PackageReference Include="TerraFX.Interop.Windows" Version="10.0.26100.2" />
</ItemGroup>
@@ -17,9 +28,5 @@
</Reference>
</ItemGroup>
<ItemGroup>
<Folder Include="Contracts\" />
</ItemGroup>
</Project>

View File

@@ -1,4 +1,5 @@
namespace Ghost.Core.Graphics;
public enum KeywordType
{
Static,
@@ -20,6 +21,8 @@ public struct ShaderEntryPoint
{
public string entry;
public string shader;
public readonly bool IsCreated => !string.IsNullOrEmpty(entry) && !string.IsNullOrEmpty(shader);
}
public struct KeywordsGroup
@@ -52,27 +55,11 @@ public interface IPassDescriptor
{
get;
}
}
public class FullPassDescriptor : IPassDescriptor
{
public string uniqueIdentifier = string.Empty;
public ShaderEntryPoint vertexShader;
public ShaderEntryPoint pixelShader;
public List<string>? defines;
public List<string>? includes;
public List<KeywordsGroup>? keywords;
public List<PropertyDescriptor>? properties;
public PipelineDescriptor localPipeline;
public string Identifier => uniqueIdentifier;
}
public class FallbackPassDescriptor : IPassDescriptor
{
public string fallbackPassIdentifier = string.Empty;
public string Identifier => fallbackPassIdentifier;
public string Name
{
get;
}
}
public struct PropertyDescriptor
@@ -82,6 +69,33 @@ public struct PropertyDescriptor
public object? defaultValue;
}
public class FullPassDescriptor : IPassDescriptor
{
public string uniqueIdentifier = string.Empty;
public string name = string.Empty;
public ShaderEntryPoint taskShader;
public ShaderEntryPoint meshShader;
public ShaderEntryPoint pixelShader;
public List<string>? defines;
public List<string>? includes;
public List<KeywordsGroup>? keywords;
public List<PropertyDescriptor>? properties;
public PipelineDescriptor localPipeline;
public string Identifier => uniqueIdentifier;
public string Name => name;
}
public class FallbackPassDescriptor : IPassDescriptor
{
public string fallbackPassIdentifier = string.Empty;
public string name = string.Empty;
public string Identifier => fallbackPassIdentifier;
public string Name => name;
}
public class ShaderDescriptor
{
public string name = string.Empty;

View File

@@ -7,7 +7,6 @@ public readonly struct Handle<T>
where T : IHandleType
{
public readonly int id;
public readonly int generation;
public Handle(int id, int generation)
@@ -22,7 +21,7 @@ public readonly struct Handle<T>
public readonly override int GetHashCode()
{
return id.GetHashCode();
return id + (generation << 16);
}
public readonly override bool Equals(object? obj)

View File

@@ -9,7 +9,7 @@ public enum LogLevel
Error
}
internal struct LogMessage
internal readonly struct LogMessage
{
public LogLevel Level
{
@@ -58,10 +58,9 @@ internal interface ILogger
}
public void Log(string message, LogLevel level);
public void Log(Exception exception);
public void Assert(bool condition, string message);
public void Clear();
}
// TODO: Add file logging.
@@ -98,6 +97,14 @@ internal class LoggerImplementation : ILogger
}
}
}
public void Clear()
{
lock (_lock)
{
_logs.Clear();
}
}
}
public static class Logger
@@ -149,4 +156,14 @@ public static class Logger
{
s_logger.Log(ex);
}
public static void Assert(bool condition, string message)
{
s_logger.Assert(condition, message);
}
public static void Clear()
{
s_logger.Clear();
}
}

View File

@@ -17,20 +17,14 @@ public readonly struct Result
return new Result(true);
}
public static Result Failure(string? message)
public static Result Fail(string? message)
{
return new Result(false, message);
}
public void ThrowIfFailed()
{
if (!success)
{
throw new InvalidOperationException($"Operation failed: {message}");
}
}
public override string ToString() => success ? "OK" : $"Error: {message}";
public static implicit operator Result(bool success) => success ? Success() : Fail(null);
}
public readonly struct Result<T>
@@ -52,18 +46,33 @@ public readonly struct Result<T>
return new Result<T>(true, data);
}
public static Result<T> Failure(string? message)
public static Result<T> Fail(string? message)
{
return new Result<T>(false, default!, message);
}
public void ThrowIfFailed()
public override string ToString() => success ? $"OK: {value}" : $"Error: {message}";
public static implicit operator Result<T>(T? data) => data is not null ? Success(data) : Fail(null);
}
public static class ResultExtensions
{
public static void ThrowIfFailed(this Result result)
{
if (!success)
if (!result.success)
{
throw new InvalidOperationException($"Operation failed: {message}");
throw new InvalidOperationException($"Operation failed: {result.message}");
}
}
public override string ToString() => success ? $"OK: {value}" : $"Error: {message}";
}
public static T GetValueOrThrow<T>(this Result<T> result)
{
if (!result.success)
{
throw new InvalidOperationException($"Operation failed: {result.message}");
}
return result.value;
}
}

View File

@@ -0,0 +1,12 @@
using Ghost.Core.Contracts;
namespace Ghost.Core.Utilities;
internal static class InternalResource
{
public static void Release<T>(ref T? resource)
where T : IReleasable
{
resource?.InternalRelease();
}
}

View File

@@ -0,0 +1,51 @@
using System.Diagnostics;
using System.Runtime.CompilerServices;
using System.Runtime.Versioning;
using TerraFX.Interop.Windows;
namespace Ghost.Core.Utilities;
#if PLATEFORME_WIN64
[SupportedOSPlatform("windows10.0.19041.0")]
internal unsafe static class Win32Utility
{
public static Guid* IID_NULL => (Guid*)Unsafe.AsPointer(ref Unsafe.AsRef(in IID.IID_NULL));
[Conditional("DEBUG")]
public static void Assert(this HRESULT hr)
{
Debug.Assert(hr.SUCCEEDED);
}
public static void ThrowIfFailed(this HRESULT hr)
{
Windows.ThrowIfFailed(hr);
}
public static void** GetVoidAddressOf<T>(this ComPtr<T> comPtr)
where T : unmanaged, IUnknown.Interface
{
return (void**)comPtr.GetAddressOf();
}
public static void** ReleaseAndGetVoidAddressOf<T>(this ComPtr<T> comPtr)
where T : unmanaged, IUnknown.Interface
{
return (void**)comPtr.ReleaseAndGetAddressOf();
}
public static ComPtr<T> Move<T>(ref this ComPtr<T> comPtr)
where T : unmanaged, IUnknown.Interface
{
var copy = default(ComPtr<T>);
comPtr.Swap(ref copy);
return copy;
}
public static bool HasFlag<T>(this uint flags, T flag)
where T : Enum
{
return (flags & Unsafe.As<T, uint>(ref flag)) != 0;
}
}
#endif