Refactoring Rendering backend
This commit is contained in:
@@ -1,17 +0,0 @@
|
||||
namespace Ghost.Core;
|
||||
|
||||
public unsafe readonly struct ConstPtr<T>
|
||||
where T : unmanaged
|
||||
{
|
||||
private readonly T* _ptr;
|
||||
|
||||
public ConstPtr(T* ptr)
|
||||
{
|
||||
_ptr = ptr;
|
||||
}
|
||||
|
||||
public readonly T* Ptr => _ptr;
|
||||
|
||||
public static implicit operator T*(ConstPtr<T> constPtr) => constPtr._ptr;
|
||||
public static implicit operator ConstPtr<T>(T* pointer) => new(pointer);
|
||||
}
|
||||
@@ -13,4 +13,8 @@
|
||||
</Reference>
|
||||
</ItemGroup>
|
||||
|
||||
<ItemGroup>
|
||||
<Folder Include="Contracts\" />
|
||||
</ItemGroup>
|
||||
|
||||
</Project>
|
||||
|
||||
@@ -1,6 +1,10 @@
|
||||
namespace Ghost.Core;
|
||||
|
||||
public interface IHandleType;
|
||||
public interface IIdentifierType;
|
||||
|
||||
public readonly struct Handle<T>
|
||||
where T : IHandleType
|
||||
{
|
||||
public readonly int id;
|
||||
|
||||
@@ -14,6 +18,8 @@ public readonly struct Handle<T>
|
||||
|
||||
public static Handle<T> Invalid => new(-1, -1);
|
||||
|
||||
public bool IsValid => this != Invalid;
|
||||
|
||||
public readonly override int GetHashCode()
|
||||
{
|
||||
return id.GetHashCode();
|
||||
@@ -46,6 +52,7 @@ public readonly struct Handle<T>
|
||||
}
|
||||
|
||||
public readonly struct Identifier<T>
|
||||
where T : IIdentifierType
|
||||
{
|
||||
public readonly int value;
|
||||
|
||||
@@ -56,6 +63,8 @@ public readonly struct Identifier<T>
|
||||
|
||||
public static Identifier<T> Invalid => new(-1);
|
||||
|
||||
public bool IsValid => this != Invalid;
|
||||
|
||||
public readonly override int GetHashCode()
|
||||
{
|
||||
return value.GetHashCode();
|
||||
|
||||
152
Ghost.Core/Logging.cs
Normal file
152
Ghost.Core/Logging.cs
Normal file
@@ -0,0 +1,152 @@
|
||||
using System.Collections.ObjectModel;
|
||||
|
||||
namespace Ghost.Core;
|
||||
|
||||
public enum LogLevel
|
||||
{
|
||||
Info,
|
||||
Warning,
|
||||
Error
|
||||
}
|
||||
|
||||
internal struct LogMessage
|
||||
{
|
||||
public LogLevel Level
|
||||
{
|
||||
get;
|
||||
}
|
||||
|
||||
public string Message
|
||||
{
|
||||
get;
|
||||
}
|
||||
|
||||
public string? StackTrace
|
||||
{
|
||||
get;
|
||||
}
|
||||
|
||||
public DateTime Timestamp
|
||||
{
|
||||
get;
|
||||
}
|
||||
|
||||
public LogMessage(LogLevel level, string message, string? stackTrace = null)
|
||||
{
|
||||
Level = level;
|
||||
Message = message;
|
||||
StackTrace = stackTrace;
|
||||
Timestamp = DateTime.Now;
|
||||
}
|
||||
|
||||
public override string ToString()
|
||||
{
|
||||
if (StackTrace != null)
|
||||
{
|
||||
return $"{Timestamp:HH:mm:ss} [{Level}] {Message}\n{StackTrace}";
|
||||
}
|
||||
|
||||
return $"{Timestamp:HH:mm:ss} [{Level}] {Message}";
|
||||
}
|
||||
}
|
||||
|
||||
internal interface ILogger
|
||||
{
|
||||
public ReadOnlyObservableCollection<LogMessage> Logs
|
||||
{
|
||||
get;
|
||||
}
|
||||
|
||||
public void Log(string message, LogLevel level);
|
||||
|
||||
public void Log(Exception exception);
|
||||
|
||||
public void Assert(bool condition, string message);
|
||||
}
|
||||
|
||||
// TODO: Add file logging.
|
||||
internal class LoggerImplementation : ILogger
|
||||
{
|
||||
private readonly ObservableCollection<LogMessage> _logs = new();
|
||||
private readonly Lock _lock = new();
|
||||
|
||||
public ReadOnlyObservableCollection<LogMessage> Logs => new(_logs);
|
||||
|
||||
public void Log(string message, LogLevel level)
|
||||
{
|
||||
lock (_lock)
|
||||
{
|
||||
_logs.Add(new LogMessage(level, message));
|
||||
}
|
||||
}
|
||||
|
||||
public void Log(Exception exception)
|
||||
{
|
||||
lock (_lock)
|
||||
{
|
||||
_logs.Add(new LogMessage(LogLevel.Error, exception.Message, exception.StackTrace));
|
||||
}
|
||||
}
|
||||
|
||||
public void Assert(bool condition, string message)
|
||||
{
|
||||
lock (_lock)
|
||||
{
|
||||
if (!condition)
|
||||
{
|
||||
Log(message, LogLevel.Error);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
public static class Logger
|
||||
{
|
||||
private static readonly ILogger s_logger = new LoggerImplementation();
|
||||
internal static ReadOnlyObservableCollection<LogMessage> Logs => s_logger.Logs;
|
||||
|
||||
public static void Log(LogLevel level, object? message)
|
||||
{
|
||||
s_logger.Log(message?.ToString() ?? "null", level);
|
||||
}
|
||||
|
||||
public static void Log(LogLevel level, string message)
|
||||
{
|
||||
s_logger.Log(message, level);
|
||||
}
|
||||
|
||||
public static void LogInfo(object? message)
|
||||
{
|
||||
s_logger.Log(message?.ToString() ?? "null", LogLevel.Info);
|
||||
}
|
||||
|
||||
public static void LogInfo(string message)
|
||||
{
|
||||
s_logger.Log(message, LogLevel.Info);
|
||||
}
|
||||
|
||||
public static void LogWarning(object? message)
|
||||
{
|
||||
s_logger.Log(message?.ToString() ?? "null", LogLevel.Warning);
|
||||
}
|
||||
|
||||
public static void LogWarning(string message)
|
||||
{
|
||||
s_logger.Log(message, LogLevel.Warning);
|
||||
}
|
||||
|
||||
public static void LogError(object? message)
|
||||
{
|
||||
s_logger.Log(message?.ToString() ?? "null", LogLevel.Error);
|
||||
}
|
||||
|
||||
public static void LogError(string message)
|
||||
{
|
||||
s_logger.Log(message, LogLevel.Error);
|
||||
}
|
||||
|
||||
public static void LogError(Exception ex)
|
||||
{
|
||||
s_logger.Log(ex);
|
||||
}
|
||||
}
|
||||
@@ -22,7 +22,7 @@ public readonly struct Result
|
||||
return new Result(false, message);
|
||||
}
|
||||
|
||||
public void EnsureSuccess()
|
||||
public void ThrowIfFailed()
|
||||
{
|
||||
if (!success)
|
||||
{
|
||||
@@ -57,7 +57,7 @@ public readonly struct Result<T>
|
||||
return new Result<T>(false, default!, message);
|
||||
}
|
||||
|
||||
public void EnsureSuccess()
|
||||
public void ThrowIfFailed()
|
||||
{
|
||||
if (!success)
|
||||
{
|
||||
|
||||
Reference in New Issue
Block a user