Compare commits
22 Commits
feature/as
...
9bae3e647e
| Author | SHA1 | Date | |
|---|---|---|---|
| 9bae3e647e | |||
| 6cadd8edeb | |||
| 3e4084c42a | |||
| cce1cf7256 | |||
| 254b08bc81 | |||
| 912b320d8f | |||
| 8a3b40b4f8 | |||
| 619720feee | |||
| bfe8588d76 | |||
| b8af6e8c3a | |||
| 5e42d699c3 | |||
| 6f802ac12b | |||
| 162b71f309 | |||
| 30090f84ab | |||
| 93c58fa7fb | |||
| 78e3b4ef31 | |||
| db8ca971a8 | |||
| 638417d4f0 | |||
| 426786397c | |||
| 9bbccfc8f8 | |||
| eadd13931f | |||
| 59991f47d5 |
1
.gitattributes
vendored
Normal file
@@ -0,0 +1 @@
|
|||||||
|
*.dll filter=lfs diff=lfs merge=lfs -text
|
||||||
1
.gitignore
vendored
@@ -9,6 +9,7 @@
|
|||||||
*.user
|
*.user
|
||||||
*.userosscache
|
*.userosscache
|
||||||
*.sln.docstates
|
*.sln.docstates
|
||||||
|
AGENTS.md
|
||||||
|
|
||||||
# User-specific files (MonoDevelop/Xamarin Studio)
|
# User-specific files (MonoDevelop/Xamarin Studio)
|
||||||
*.userprefs
|
*.userprefs
|
||||||
|
|||||||
297
AGENTS.md
@@ -1,297 +0,0 @@
|
|||||||
# GhostEngine - Agent Development Guide
|
|
||||||
|
|
||||||
This guide provides essential information for AI coding agents working on the GhostEngine codebase.
|
|
||||||
|
|
||||||
## Project Overview
|
|
||||||
|
|
||||||
- **Type**: Game Engine
|
|
||||||
- **Language**: C#
|
|
||||||
- **Target Framework**: .NET 10.0
|
|
||||||
- **Special Features**: ECS architecture, D3D12 rendering, AOT compilation, WinUI 3 editor
|
|
||||||
- **Platform**: Windows (net10.0-windows10.0.22621.0 for editor projects)
|
|
||||||
|
|
||||||
## Build Commands
|
|
||||||
|
|
||||||
### Build Entire Solution
|
|
||||||
```bash
|
|
||||||
dotnet build GhostEngine.slnx
|
|
||||||
```
|
|
||||||
|
|
||||||
### Build Specific Project
|
|
||||||
```bash
|
|
||||||
dotnet build Ghost.Entities/Ghost.Entities.csproj
|
|
||||||
dotnet build Ghost.Editor/Ghost.Editor.csproj
|
|
||||||
```
|
|
||||||
|
|
||||||
### Build with Configuration
|
|
||||||
```bash
|
|
||||||
dotnet build GhostEngine.slnx -c Release
|
|
||||||
dotnet build GhostEngine.slnx -c Debug
|
|
||||||
```
|
|
||||||
|
|
||||||
### Clean Build
|
|
||||||
```bash
|
|
||||||
dotnet clean GhostEngine.slnx
|
|
||||||
dotnet build GhostEngine.slnx
|
|
||||||
```
|
|
||||||
|
|
||||||
## Test Commands
|
|
||||||
|
|
||||||
### Run All Tests (Custom Framework)
|
|
||||||
Tests use a custom test framework (not xUnit/NUnit/MSTest). Each test project is an executable.
|
|
||||||
|
|
||||||
```bash
|
|
||||||
# Run entity tests
|
|
||||||
dotnet run --project Ghost.Entities.Test/Ghost.Entities.Test.csproj
|
|
||||||
|
|
||||||
# Run shader tests
|
|
||||||
dotnet run --project Ghost.Shader.Test/Ghost.Shader.Test.csproj
|
|
||||||
```
|
|
||||||
|
|
||||||
### Run Single Test
|
|
||||||
Tests implement `ITest` interface. To run a specific test, modify the test project's `Program.cs`:
|
|
||||||
|
|
||||||
```csharp
|
|
||||||
// In Ghost.Entities.Test/Program.cs
|
|
||||||
TestRunner.Run<EntityQueryTest>(); // Run specific test
|
|
||||||
TestRunner.Run<EntityQueryTest>(10); // Run with 10 iterations
|
|
||||||
```
|
|
||||||
|
|
||||||
### Visual Tests (Graphics)
|
|
||||||
Graphics tests use WinUI 3 and require running as packaged apps:
|
|
||||||
|
|
||||||
```bash
|
|
||||||
dotnet run --project Ghost.Graphics.Test/Ghost.Graphics.Test.csproj
|
|
||||||
```
|
|
||||||
|
|
||||||
## Code Style Guidelines
|
|
||||||
|
|
||||||
### Formatting (from .editorconfig)
|
|
||||||
|
|
||||||
- **Braces**: Allman style - all opening braces on new lines
|
|
||||||
- **Line Length**: Max 400 characters (very permissive)
|
|
||||||
- **Single-line statements**: Preserved (allowed)
|
|
||||||
- **Single-line blocks**: Preserved (allowed)
|
|
||||||
|
|
||||||
```csharp
|
|
||||||
// Correct brace style
|
|
||||||
public void Method()
|
|
||||||
{
|
|
||||||
if (condition)
|
|
||||||
{
|
|
||||||
DoSomething();
|
|
||||||
}
|
|
||||||
}
|
|
||||||
```
|
|
||||||
|
|
||||||
### Imports
|
|
||||||
|
|
||||||
- **System directives**: NOT sorted first (dotnet_sort_system_directives_first = false)
|
|
||||||
- **No grouping**: Import directives not separated by blank lines
|
|
||||||
- **Order**: Organize by project convention, not alphabetically
|
|
||||||
|
|
||||||
```csharp
|
|
||||||
using Ghost.Core;
|
|
||||||
using Ghost.Entities;
|
|
||||||
using Misaki.HighPerformance.Collections;
|
|
||||||
using System.Diagnostics;
|
|
||||||
using TerraFX.Interop.DirectX;
|
|
||||||
```
|
|
||||||
|
|
||||||
### Types and Nullability
|
|
||||||
|
|
||||||
- **Nullable**: Enabled for all projects
|
|
||||||
- **Implicit usings**: Enabled
|
|
||||||
- **Unsafe code**: Allowed in most projects (AllowUnsafeBlocks = True)
|
|
||||||
- **Primary constructors**: NOT preferred (csharp_style_prefer_primary_constructors = false)
|
|
||||||
|
|
||||||
### Naming Conventions
|
|
||||||
|
|
||||||
- **Classes/Interfaces**: PascalCase (`EntityManager`, `ICommandBuffer`)
|
|
||||||
- **Methods**: PascalCase (`CreateEntity`, `GetComponent`)
|
|
||||||
- **Properties**: PascalCase (`IsSuccess`, `Value`)
|
|
||||||
- **Fields (private)**: Camel case with underscore prefix (`_entityLocations`, `_world`)
|
|
||||||
- **Fields (public/internal)**: Camel case, no prefix for struct fields (`archetypeID`, `chunkIndex`)
|
|
||||||
- **Type parameters**: Single letter or PascalCase (`T`, `TComponent`)
|
|
||||||
- **Constants**: PascalCase (no SCREAMING_SNAKE_CASE)
|
|
||||||
|
|
||||||
```csharp
|
|
||||||
public class EntityManager
|
|
||||||
{
|
|
||||||
private readonly World _world; // Private field
|
|
||||||
private UnsafeSlotMap<EntityLocation> _entityLocations;
|
|
||||||
|
|
||||||
public World World => _world; // Property
|
|
||||||
|
|
||||||
public Entity CreateEntity() { } // Method
|
|
||||||
}
|
|
||||||
|
|
||||||
internal struct EntityLocation // Struct
|
|
||||||
{
|
|
||||||
public int archetypeID; // Public struct field
|
|
||||||
public int chunkIndex;
|
|
||||||
}
|
|
||||||
```
|
|
||||||
|
|
||||||
### Error Handling
|
|
||||||
|
|
||||||
**Use Result Types** - Railway-oriented programming pattern:
|
|
||||||
|
|
||||||
```csharp
|
|
||||||
// Custom result types defined in Ghost.Core
|
|
||||||
public ErrorStatus DoOperation()
|
|
||||||
{
|
|
||||||
return ErrorStatus.None; // or ErrorStatus.NotFound, etc.
|
|
||||||
}
|
|
||||||
|
|
||||||
public Result<T> GetValue()
|
|
||||||
{
|
|
||||||
if (success)
|
|
||||||
return Result<T>.Success(value);
|
|
||||||
else
|
|
||||||
return Result<T>.Failure("Error message");
|
|
||||||
}
|
|
||||||
|
|
||||||
public Result<T, ErrorStatus> GetValueWithStatus()
|
|
||||||
{
|
|
||||||
if (success)
|
|
||||||
return value; // Implicit conversion
|
|
||||||
else
|
|
||||||
return ErrorStatus.NotFound; // Implicit conversion
|
|
||||||
}
|
|
||||||
|
|
||||||
// Extension methods for checking results
|
|
||||||
result.ThrowIfFailed();
|
|
||||||
var value = result.GetValueOrThrow();
|
|
||||||
var value = result.GetValueOrDefault(defaultValue);
|
|
||||||
if (result.TryGetValue(out var value)) { }
|
|
||||||
```
|
|
||||||
|
|
||||||
**Error Status Values**: None, NotFound, InvalidArgument, InvalidState, InternalError, PermissionDenied, NotSupported, OutOfMemory, Timeout, Cancelled, UnknownError
|
|
||||||
|
|
||||||
### Memory and Performance
|
|
||||||
|
|
||||||
- **Use unsafe code** when needed for performance-critical paths
|
|
||||||
- **Span<T> and stackalloc**: Prefer for temporary allocations
|
|
||||||
- **ref returns**: Use for zero-copy access to internal data
|
|
||||||
- **Allocator patterns**: Use `Allocator.Persistent` for long-lived allocations
|
|
||||||
- **AllocationManager**: Create stack scopes for temporary allocations
|
|
||||||
|
|
||||||
```csharp
|
|
||||||
// Stack allocation pattern
|
|
||||||
var entities = (Span<Entity>)stackalloc Entity[1];
|
|
||||||
|
|
||||||
// Using allocation scope
|
|
||||||
using var scope = AllocationManager.CreateStackScope();
|
|
||||||
var batchDestroy = new UnsafeList<EntityLocation>(entities.Length, scope.AllocationHandle);
|
|
||||||
|
|
||||||
// Ref returns for zero-copy access
|
|
||||||
public ref T GetSingleton<T>() where T : unmanaged, IComponent
|
|
||||||
{
|
|
||||||
var ptr = GetSingleton(ComponentTypeID<T>.Value);
|
|
||||||
return ref *(T*)ptr;
|
|
||||||
}
|
|
||||||
```
|
|
||||||
|
|
||||||
### Type Safety Patterns
|
|
||||||
|
|
||||||
**Strongly-typed identifiers**:
|
|
||||||
```csharp
|
|
||||||
Identifier<IComponent> componentID;
|
|
||||||
Identifier<Archetype> archetypeID;
|
|
||||||
Handle<T> resourceHandle;
|
|
||||||
```
|
|
||||||
|
|
||||||
**Generic constraints**:
|
|
||||||
```csharp
|
|
||||||
public void Method<T>() where T : unmanaged, IComponent
|
|
||||||
public void Method<T, E>() where E : struct, Enum
|
|
||||||
```
|
|
||||||
|
|
||||||
### Documentation
|
|
||||||
|
|
||||||
- **XML comments**: Required for public APIs
|
|
||||||
- **Summary tags**: Describe what, not how
|
|
||||||
- **Remarks**: Add for complex behavior, thread-safety warnings, structural changes
|
|
||||||
|
|
||||||
```csharp
|
|
||||||
/// <summary>
|
|
||||||
/// Create an entity with specified components.
|
|
||||||
/// </summary>
|
|
||||||
/// <param name="set">A set of component space IDs to add to the entities.</param>
|
|
||||||
/// <returns>The created entity.</returns>
|
|
||||||
/// <remarks>
|
|
||||||
/// This method causes structural changes and is not thread-safe.
|
|
||||||
/// Use <see cref="EntityCommandBuffer"/> to defer changes.
|
|
||||||
/// </remarks>
|
|
||||||
public Entity CreateEntity(ComponentSet set) { }
|
|
||||||
```
|
|
||||||
|
|
||||||
### Common Patterns
|
|
||||||
|
|
||||||
**ECS Component Registration**:
|
|
||||||
```csharp
|
|
||||||
// Type-safe component ID
|
|
||||||
ComponentTypeID<Transform>.Value
|
|
||||||
|
|
||||||
// Component sets for archetypes
|
|
||||||
var set = new ComponentSet(ComponentTypeID<Transform>.Value, ComponentTypeID<Velocity>.Value);
|
|
||||||
```
|
|
||||||
|
|
||||||
**Disposal Pattern**:
|
|
||||||
```csharp
|
|
||||||
private bool _disposed;
|
|
||||||
|
|
||||||
~MyClass()
|
|
||||||
{
|
|
||||||
Dispose();
|
|
||||||
}
|
|
||||||
|
|
||||||
public void Dispose()
|
|
||||||
{
|
|
||||||
if (_disposed) return;
|
|
||||||
|
|
||||||
// Cleanup code
|
|
||||||
_disposed = true;
|
|
||||||
GC.SuppressFinalize(this);
|
|
||||||
}
|
|
||||||
```
|
|
||||||
|
|
||||||
**Debug-only validation**:
|
|
||||||
```csharp
|
|
||||||
#if DEBUG || GHOST_EDITOR
|
|
||||||
if (!_isSuccess)
|
|
||||||
{
|
|
||||||
throw new InvalidOperationException($"Error: {_message}");
|
|
||||||
}
|
|
||||||
#endif
|
|
||||||
```
|
|
||||||
|
|
||||||
## Architecture Notes
|
|
||||||
|
|
||||||
### Entity Component System (ECS)
|
|
||||||
- Archetype-based storage (similar to Unity DOTS)
|
|
||||||
- Component data stored in chunks
|
|
||||||
- Queries use bitset signatures for fast matching
|
|
||||||
- Structural changes move entities between archetypes
|
|
||||||
|
|
||||||
### Graphics (D3D12)
|
|
||||||
- Hardware abstraction via `ICommandBuffer`
|
|
||||||
- Resource lifetime managed via handles
|
|
||||||
- Pipeline state objects (PSO) cached in library
|
|
||||||
- Native interop via TerraFX.Interop
|
|
||||||
|
|
||||||
### Custom Dependencies
|
|
||||||
- `Misaki.HighPerformance.*`: High-performance collections and utilities
|
|
||||||
- `TerraFX.Interop.*`: Native Windows/DirectX interop
|
|
||||||
- Custom source generators in `Ghost.Generator`
|
|
||||||
|
|
||||||
## Important Rules
|
|
||||||
|
|
||||||
1. **Never disable nullable warnings** - fix the root cause
|
|
||||||
2. **Use Result types** instead of throwing exceptions for expected failures
|
|
||||||
3. **Document thread-safety** in XML comments for public APIs
|
|
||||||
4. **AllowUnsafeBlocks** is enabled - use unsafe code when it improves performance
|
|
||||||
5. **Avoid collection expressions/initializers** (disabled in .editorconfig)
|
|
||||||
6. **Prefer explicit over implicit** - clarity over brevity
|
|
||||||
7. **Test changes** by running the appropriate test project executable
|
|
||||||
@@ -1,67 +0,0 @@
|
|||||||
using System.Runtime.CompilerServices;
|
|
||||||
|
|
||||||
namespace Ghost.Core;
|
|
||||||
|
|
||||||
public readonly struct TypeHandle
|
|
||||||
{
|
|
||||||
public readonly IntPtr Value
|
|
||||||
{
|
|
||||||
get;
|
|
||||||
}
|
|
||||||
|
|
||||||
private TypeHandle(IntPtr value)
|
|
||||||
{
|
|
||||||
Value = value;
|
|
||||||
}
|
|
||||||
|
|
||||||
/// <summary>
|
|
||||||
/// Gets the space handle for the specified space.
|
|
||||||
/// </summary>
|
|
||||||
/// <param name="type">The space to get the handle for.</param>
|
|
||||||
/// <returns>The space handle as a nint.</returns>
|
|
||||||
[MethodImpl(MethodImplOptions.AggressiveInlining)]
|
|
||||||
public static TypeHandle Get(Type type) => new TypeHandle(type.TypeHandle.Value);
|
|
||||||
|
|
||||||
/// <summary>
|
|
||||||
/// Gets the space handle for the specified space.
|
|
||||||
/// </summary>
|
|
||||||
/// <typeparam name="T">The space to get the handle for.</typeparam>
|
|
||||||
/// <returns>The space handle as a nint.</returns>
|
|
||||||
[MethodImpl(MethodImplOptions.AggressiveInlining)]
|
|
||||||
public static TypeHandle Get<T>() => Get(typeof(T));
|
|
||||||
|
|
||||||
/// <summary>
|
|
||||||
/// Converts a TypeHandle to a Type.
|
|
||||||
/// </summary>
|
|
||||||
/// <param name="handle">The TypeHandle to convert.</param>
|
|
||||||
/// <returns>The corresponding Type.</returns>
|
|
||||||
public Type? ToType()
|
|
||||||
{
|
|
||||||
return Type.GetTypeFromHandle(RuntimeTypeHandle.FromIntPtr(Value));
|
|
||||||
}
|
|
||||||
|
|
||||||
public override int GetHashCode()
|
|
||||||
{
|
|
||||||
return Value.GetHashCode();
|
|
||||||
}
|
|
||||||
|
|
||||||
public static implicit operator TypeHandle(IntPtr value)
|
|
||||||
{
|
|
||||||
return new TypeHandle(value);
|
|
||||||
}
|
|
||||||
|
|
||||||
public static implicit operator IntPtr(TypeHandle handle)
|
|
||||||
{
|
|
||||||
return handle.Value;
|
|
||||||
}
|
|
||||||
|
|
||||||
public static implicit operator TypeHandle(Type type)
|
|
||||||
{
|
|
||||||
return Get(type);
|
|
||||||
}
|
|
||||||
|
|
||||||
public static implicit operator Type?(TypeHandle handle)
|
|
||||||
{
|
|
||||||
return handle.ToType();
|
|
||||||
}
|
|
||||||
}
|
|
||||||
@@ -1,9 +0,0 @@
|
|||||||
using System;
|
|
||||||
using System.Collections.Generic;
|
|
||||||
using System.Text;
|
|
||||||
|
|
||||||
namespace Ghost.Core.Utilities;
|
|
||||||
|
|
||||||
internal class EnumUtility
|
|
||||||
{
|
|
||||||
}
|
|
||||||
@@ -1,115 +0,0 @@
|
|||||||
using Misaki.HighPerformance.LowLevel;
|
|
||||||
using System.ComponentModel;
|
|
||||||
using System.Diagnostics;
|
|
||||||
using System.Runtime.CompilerServices;
|
|
||||||
using System.Runtime.Versioning;
|
|
||||||
using TerraFX.Interop.Windows;
|
|
||||||
|
|
||||||
namespace Ghost.Core.Utilities;
|
|
||||||
|
|
||||||
[SupportedOSPlatform("windows10.0.19041.0")]
|
|
||||||
internal static unsafe partial class Win32Utility
|
|
||||||
{
|
|
||||||
[EditorBrowsable(EditorBrowsableState.Never)]
|
|
||||||
public readonly ref struct IID_PPV
|
|
||||||
{
|
|
||||||
public readonly Guid* iid;
|
|
||||||
public readonly void** ppv;
|
|
||||||
|
|
||||||
public IID_PPV(Guid* iid, void** ppv)
|
|
||||||
{
|
|
||||||
this.iid = iid;
|
|
||||||
this.ppv = ppv;
|
|
||||||
}
|
|
||||||
|
|
||||||
public void Deconstruct(out Guid* iid, out void** ppv)
|
|
||||||
{
|
|
||||||
iid = this.iid;
|
|
||||||
ppv = this.ppv;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
public static Guid* IID_NULL
|
|
||||||
{
|
|
||||||
[MethodImpl(MethodImplOptions.AggressiveInlining)]
|
|
||||||
get => (Guid*)Unsafe.AsPointer(ref Unsafe.AsRef(in IID.IID_NULL));
|
|
||||||
}
|
|
||||||
|
|
||||||
[MethodImpl(MethodImplOptions.AggressiveInlining)]
|
|
||||||
public static IID_PPV IID_PPV_ARGS<T>(ComPtr<T>* comPtr)
|
|
||||||
where T : unmanaged, IUnknown.Interface
|
|
||||||
{
|
|
||||||
return new IID_PPV(Windows.__uuidof<T>(), (void**)comPtr);
|
|
||||||
}
|
|
||||||
|
|
||||||
[MethodImpl(MethodImplOptions.AggressiveInlining)]
|
|
||||||
public static void Attach<T>(ref this UniquePtr<T> uPtr, T* other)
|
|
||||||
where T : unmanaged, IUnknown.Interface
|
|
||||||
{
|
|
||||||
var ptr = uPtr.Get();
|
|
||||||
if (ptr != null)
|
|
||||||
{
|
|
||||||
var refCount = ptr->Release();
|
|
||||||
Debug.Assert((refCount != 0) || (ptr != other));
|
|
||||||
}
|
|
||||||
|
|
||||||
uPtr = new UniquePtr<T>(other);
|
|
||||||
}
|
|
||||||
|
|
||||||
[MethodImpl(MethodImplOptions.AggressiveInlining)]
|
|
||||||
public static void Dispose<T>(ref this UniquePtr<T> uPtr)
|
|
||||||
where T : unmanaged, IUnknown.Interface
|
|
||||||
{
|
|
||||||
var ptr = uPtr.Detach();
|
|
||||||
if (ptr != null)
|
|
||||||
{
|
|
||||||
ptr->Release();
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
[MethodImpl(MethodImplOptions.AggressiveInlining)]
|
|
||||||
public static Result ToResult(this HRESULT hr, [CallerArgumentExpression(nameof(hr))] string? op = null)
|
|
||||||
{
|
|
||||||
if (hr.SUCCEEDED)
|
|
||||||
{
|
|
||||||
return Result.Success();
|
|
||||||
}
|
|
||||||
|
|
||||||
return Result.Failure($"{op} failed with code {hr}");
|
|
||||||
}
|
|
||||||
|
|
||||||
[MethodImpl(MethodImplOptions.AggressiveInlining)]
|
|
||||||
public static void** ReleaseAndGetVoidAddressOf<T>(ref this ComPtr<T> comPtr)
|
|
||||||
where T : unmanaged, IUnknown.Interface
|
|
||||||
{
|
|
||||||
return (void**)comPtr.ReleaseAndGetAddressOf();
|
|
||||||
}
|
|
||||||
|
|
||||||
[MethodImpl(MethodImplOptions.AggressiveInlining)]
|
|
||||||
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;
|
|
||||||
}
|
|
||||||
|
|
||||||
[MethodImpl(MethodImplOptions.AggressiveInlining)]
|
|
||||||
public static bool HasFlag<T>(this uint flags, T flag)
|
|
||||||
where T : Enum
|
|
||||||
{
|
|
||||||
return (flags & Unsafe.As<T, uint>(ref flag)) != 0;
|
|
||||||
}
|
|
||||||
|
|
||||||
extension(MemoryLeakException)
|
|
||||||
{
|
|
||||||
[MethodImpl(MethodImplOptions.AggressiveInlining)]
|
|
||||||
public static void ThrowIfRefCountNonZero(uint count)
|
|
||||||
{
|
|
||||||
if (count != 0)
|
|
||||||
{
|
|
||||||
throw new MemoryLeakException($"Reference count is not zero: {count}");
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
@@ -1,6 +0,0 @@
|
|||||||
namespace Ghost.Data.Repository;
|
|
||||||
|
|
||||||
internal class AssetsRepository
|
|
||||||
{
|
|
||||||
|
|
||||||
}
|
|
||||||
@@ -1,97 +0,0 @@
|
|||||||
using Ghost.Core;
|
|
||||||
|
|
||||||
namespace Ghost.Editor.Core.AppState;
|
|
||||||
|
|
||||||
internal partial class AppStateMachine : IDisposable, IAsyncDisposable
|
|
||||||
{
|
|
||||||
private Dictionary<StateKey, Lazy<IAppState>> _states = new();
|
|
||||||
private IAppState? _current;
|
|
||||||
|
|
||||||
private bool _disposed;
|
|
||||||
|
|
||||||
public void RegisterState(StateKey key, Func<IAppState> stateFactory)
|
|
||||||
{
|
|
||||||
_states[key] = new(stateFactory);
|
|
||||||
}
|
|
||||||
|
|
||||||
public async Task<Result> TransitionToAsync(StateKey stateKey, object? parameter = null)
|
|
||||||
{
|
|
||||||
var previous = _current;
|
|
||||||
if (!_states.TryGetValue(stateKey, out var next))
|
|
||||||
{
|
|
||||||
return Result.Failure($"State '{stateKey}' not found.");
|
|
||||||
}
|
|
||||||
|
|
||||||
Result result;
|
|
||||||
if (previous != null)
|
|
||||||
{
|
|
||||||
result = await previous.OnExitingAsync();
|
|
||||||
if (result.IsFailure)
|
|
||||||
{
|
|
||||||
return result;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
result = await next.Value.OnEnteringAsync(parameter);
|
|
||||||
if (result.IsFailure)
|
|
||||||
{
|
|
||||||
if (previous != null)
|
|
||||||
{
|
|
||||||
await previous.OnEnteredAsync(parameter);
|
|
||||||
}
|
|
||||||
|
|
||||||
return result;
|
|
||||||
}
|
|
||||||
|
|
||||||
if (previous != null)
|
|
||||||
{
|
|
||||||
result = await previous.OnExitedAsync();
|
|
||||||
if (result.IsFailure)
|
|
||||||
{
|
|
||||||
await next.Value.OnExitedAsync();
|
|
||||||
await previous.OnEnteredAsync(parameter);
|
|
||||||
return result;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
result = await next.Value.OnEnteredAsync(parameter);
|
|
||||||
if (result.IsFailure)
|
|
||||||
{
|
|
||||||
await next.Value.OnExitedAsync();
|
|
||||||
|
|
||||||
if (previous != null)
|
|
||||||
{
|
|
||||||
await previous.OnEnteredAsync(parameter);
|
|
||||||
}
|
|
||||||
|
|
||||||
return result;
|
|
||||||
}
|
|
||||||
|
|
||||||
_current = next.Value;
|
|
||||||
|
|
||||||
return Result.Success();
|
|
||||||
}
|
|
||||||
|
|
||||||
public void Dispose()
|
|
||||||
{
|
|
||||||
DisposeAsync().AsTask().Wait();
|
|
||||||
}
|
|
||||||
|
|
||||||
public async ValueTask DisposeAsync()
|
|
||||||
{
|
|
||||||
if (_disposed)
|
|
||||||
{
|
|
||||||
return;
|
|
||||||
}
|
|
||||||
|
|
||||||
_states.Clear();
|
|
||||||
if (_current != null)
|
|
||||||
{
|
|
||||||
await _current.OnExitingAsync();
|
|
||||||
await _current.OnExitedAsync();
|
|
||||||
}
|
|
||||||
|
|
||||||
_current = null;
|
|
||||||
_disposed = true;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
@@ -1,28 +0,0 @@
|
|||||||
using Ghost.Core;
|
|
||||||
|
|
||||||
namespace Ghost.Editor.Core.AppState;
|
|
||||||
|
|
||||||
internal interface IAppState
|
|
||||||
{
|
|
||||||
/// <summary>
|
|
||||||
/// Called when exiting the state.
|
|
||||||
/// </summary>
|
|
||||||
public ValueTask<Result> OnExitingAsync();
|
|
||||||
|
|
||||||
/// <summary>
|
|
||||||
/// Called when entering the state, right after OnEnteringAsync.
|
|
||||||
/// <paramref name="parameter">can be used to pass data into the state, such as a project to load.</summary>
|
|
||||||
/// </summary>
|
|
||||||
public ValueTask<Result> OnEnteringAsync(object? parameter);
|
|
||||||
|
|
||||||
/// <summary>
|
|
||||||
/// Called when exiting the state, specifically for pose transitions.
|
|
||||||
/// </summary>
|
|
||||||
public ValueTask<Result> OnExitedAsync();
|
|
||||||
|
|
||||||
/// <summary>
|
|
||||||
/// Called when entered the state, specifically after the state has been fully initialized and is ready for interaction.
|
|
||||||
/// </summary>
|
|
||||||
/// <param name="parameter">can be used to pass data into the state, such as a project to load.</param>
|
|
||||||
public ValueTask<Result> OnEnteredAsync(object? parameter);
|
|
||||||
}
|
|
||||||
@@ -1,8 +0,0 @@
|
|||||||
namespace Ghost.Editor.Core.AppState;
|
|
||||||
|
|
||||||
internal enum StateKey
|
|
||||||
{
|
|
||||||
None,
|
|
||||||
Landing,
|
|
||||||
EngineEditor,
|
|
||||||
}
|
|
||||||
@@ -1,134 +0,0 @@
|
|||||||
# Asset Database Plan
|
|
||||||
|
|
||||||
AssetDB is a core component of the Ghost Editor that manages the storage, retrieval, and organization of various assets used within the editor.
|
|
||||||
This document outlines the plan for implementing the AssetDB, including its structure, functionality, and integration with other components of the Ghost Editor.
|
|
||||||
|
|
||||||
## Data Structure
|
|
||||||
|
|
||||||
- Asset Metadata: Each asset will have associated metadata, including:
|
|
||||||
- Unique Identifier (GUID)
|
|
||||||
- Version (Version of the asset pipeline, not the asset. This is primarily for migration when we redesign the asset pipeline in the future)
|
|
||||||
- Tags
|
|
||||||
- Importer Settings
|
|
||||||
|
|
||||||
An simplified example of metadata file (filename.png.gmeta):
|
|
||||||
```json
|
|
||||||
{
|
|
||||||
"Guid": "123e4567-e89b-12d3-a456-426614174000",
|
|
||||||
"Version": 1,
|
|
||||||
"Tags": ["Environment", "Texture"],
|
|
||||||
"ImporterSettings": [
|
|
||||||
"TextureImporter": {
|
|
||||||
"Version": 1,
|
|
||||||
"MaxSize": 2048,
|
|
||||||
"MipLevels": 1
|
|
||||||
},
|
|
||||||
"OtherImporter": {
|
|
||||||
|
|
||||||
}
|
|
||||||
]
|
|
||||||
}
|
|
||||||
```
|
|
||||||
|
|
||||||
- Asset: The base class for all assets.
|
|
||||||
|
|
||||||
- Asset Database: A centralized database that stores and manages all assets. It will handle:
|
|
||||||
- Asset registration and deregistration
|
|
||||||
- Asset lookup by GUID
|
|
||||||
- Asset lookup by path
|
|
||||||
- Automatic handle file creation, remove, rename, move, etc.
|
|
||||||
- Asset dependency management
|
|
||||||
- Automatic asset re-importing when source files change.
|
|
||||||
- Asset tagging.
|
|
||||||
- Add type specific default importer settings for new asset.
|
|
||||||
- SQLite (`Microsoft.Data.Sqlite`) for persistent storage and efficient querying.
|
|
||||||
|
|
||||||
An simplified data model example in SQLite:
|
|
||||||
```sql
|
|
||||||
CREATE TABLE Assets (
|
|
||||||
Guid TEXT PRIMARY KEY,
|
|
||||||
Path TEXT NOT NULL,
|
|
||||||
Type INTEGER,
|
|
||||||
Version INTEGER,
|
|
||||||
Tags TEXT,
|
|
||||||
DependencyGuids TEXT
|
|
||||||
);
|
|
||||||
```
|
|
||||||
|
|
||||||
## Simplified Workflow
|
|
||||||
|
|
||||||
### New Asset Addition
|
|
||||||
|
|
||||||
1. A file is added to the project directory.
|
|
||||||
2. Generates the metadata for the asset with name filename.etx + ".gmeta" (You can get the extension from Ghost.Editor.Core.Utilities.FileExtensions.META_FILE_EXTENSION)
|
|
||||||
3. Add the asset to the database.
|
|
||||||
|
|
||||||
### File Removal
|
|
||||||
|
|
||||||
1. A file is removed from the project directory.
|
|
||||||
2. Deletes the corresponding asset metadata.
|
|
||||||
3. Remove the asset from the database.
|
|
||||||
4. Mark dependent assets as dirty for re-importing.
|
|
||||||
|
|
||||||
### File Renaming/Moving
|
|
||||||
|
|
||||||
1. A file is renamed or moved within the project directory.
|
|
||||||
2. Check if the new path has an existing metadata file (just in case user move the file with the metadata file together).
|
|
||||||
- If exists, validate the data and update the database accordingly.
|
|
||||||
- if not, regenerate the metadata file for the new path and update the database.
|
|
||||||
3. Delete the old metadata file if exists.
|
|
||||||
|
|
||||||
### File Modification
|
|
||||||
|
|
||||||
1. A file is modified in the project directory.
|
|
||||||
2. Check the file hash to see if it has changed.
|
|
||||||
- If changed, mark the asset as dirty for re-importing.
|
|
||||||
- If not, do nothing.
|
|
||||||
|
|
||||||
### Asset Importing (You don't need to write any assets importer right now, just write the framework and a simple test importer if it's needed for unit test)
|
|
||||||
|
|
||||||
1. An asset is marked as dirty.
|
|
||||||
2. The asset importer for that type processes the asset based on its importer settings.
|
|
||||||
3. Validate the references and dependencies, report errors if any (for example, missing dependencies).
|
|
||||||
|
|
||||||
## Features Checklist
|
|
||||||
|
|
||||||
### In Code (API)
|
|
||||||
|
|
||||||
- [ ] Find GUID by path
|
|
||||||
- [ ] Find path by GUID
|
|
||||||
- [ ] Load asset by GUID (May need special asset loader, not included in this plan, leave an API and TODO comment)
|
|
||||||
- [ ] API for adding/removing/moving/copying assets
|
|
||||||
- [ ] API for opening asset in editor or external program
|
|
||||||
- [ ] API to set asset dirty and save all assets if dirty
|
|
||||||
- [ ] Get and set asset tags.
|
|
||||||
- [ ] Refresh asset database (re-scan project directory for changes)
|
|
||||||
|
|
||||||
### In Editor (API Only, I will handle the UI part)
|
|
||||||
|
|
||||||
- [ ] Asset Browser window to view and manage assets (automatically refresh when assets change)
|
|
||||||
- [ ] Skip meta file in asset browser view
|
|
||||||
- [ ] Search assets by name, tag, type, etc.
|
|
||||||
|
|
||||||
### In Background
|
|
||||||
|
|
||||||
- [ ] File system watcher to monitor changes in the project directory and update the AssetDB accordingly.
|
|
||||||
- [ ] Automatic asset re-importing when source files change (detect changes via file system watcher and quick hash comparison).
|
|
||||||
- [ ] Asset dependency management.
|
|
||||||
- [ ] Validate and fix AssetDB on project load (check for missing/corrupted assets if user add/delete/rename/move files when the editor is not running, etc.)
|
|
||||||
- [ ] Asset importer system to handle different asset types and their import settings.
|
|
||||||
|
|
||||||
## Testing
|
|
||||||
|
|
||||||
Make sure everything builds correctly at first.
|
|
||||||
Write unit tests and integration tests to ensure the AssetDB functions correctly inside the `Ghost.UnitTests` project.
|
|
||||||
|
|
||||||
## Critical Considerations
|
|
||||||
|
|
||||||
- Performance: Ensure that the AssetDB operations are efficient, especially for large projects with many assets.
|
|
||||||
- Stability: The meta data files should be the only source of truth for asset information.
|
|
||||||
The AssetDB should be able to recover from inconsistencies by re-generating data from the meta files.
|
|
||||||
We still need to trust the meta data files even if they are corrupted or missing by regenerating them when necessary.
|
|
||||||
Database is used for caching and quick lookup only.
|
|
||||||
- Asynchronous patterns: Consider using asynchronous operations for file I/O and database operations to avoid blocking the main thread.
|
|
||||||
Packages like `Microsoft.Data.Sqlite` support async operations.
|
|
||||||
@@ -1,355 +0,0 @@
|
|||||||
using Ghost.Core;
|
|
||||||
|
|
||||||
namespace Ghost.Editor.Core.AssetHandle;
|
|
||||||
|
|
||||||
public static partial class AssetDatabase
|
|
||||||
{
|
|
||||||
/// <summary>
|
|
||||||
/// Create a new asset at the specified path.
|
|
||||||
/// Generates metadata and adds it to the database.
|
|
||||||
/// </summary>
|
|
||||||
/// <param name="assetPath">Path to create the asset at.</param>
|
|
||||||
/// <param name="content">Content to write to the asset file.</param>
|
|
||||||
/// <returns>Result indicating success or failure.</returns>
|
|
||||||
public static async ValueTask<Result> CreateAssetAsync(string assetPath, ReadOnlyMemory<byte> content, CancellationToken token = default)
|
|
||||||
{
|
|
||||||
if (AssetsDirectory == null)
|
|
||||||
{
|
|
||||||
return Result.Failure("AssetsDirectory not initialized");
|
|
||||||
}
|
|
||||||
|
|
||||||
if (!assetPath.StartsWith(AssetsDirectory.FullName, StringComparison.OrdinalIgnoreCase))
|
|
||||||
{
|
|
||||||
return Result.Failure("Asset path must be within the Assets directory");
|
|
||||||
}
|
|
||||||
|
|
||||||
if (File.Exists(assetPath))
|
|
||||||
{
|
|
||||||
return Result.Failure("Asset already exists");
|
|
||||||
}
|
|
||||||
|
|
||||||
try
|
|
||||||
{
|
|
||||||
var directory = Path.GetDirectoryName(assetPath);
|
|
||||||
if (!string.IsNullOrEmpty(directory) && !Directory.Exists(directory))
|
|
||||||
{
|
|
||||||
Directory.CreateDirectory(directory);
|
|
||||||
}
|
|
||||||
|
|
||||||
using var fs = File.Create(assetPath);
|
|
||||||
await fs.WriteAsync(content, token);
|
|
||||||
|
|
||||||
// GenerateMetaFileAsync will be called automatically by the file watcher
|
|
||||||
// But we'll call it directly to ensure it's created immediately
|
|
||||||
await GenerateMetaFileAsync(assetPath, token);
|
|
||||||
|
|
||||||
return Result.Success();
|
|
||||||
}
|
|
||||||
catch (Exception ex)
|
|
||||||
{
|
|
||||||
return Result.Failure($"Failed to create asset: {ex.Message}");
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
/// <summary>
|
|
||||||
/// Create an empty asset at the specified path.
|
|
||||||
/// Generates metadata and adds it to the database.
|
|
||||||
/// </summary>
|
|
||||||
/// <param name="assetPath">Path to create the asset at.</param>
|
|
||||||
/// <returns>Result indicating success or failure.</returns>
|
|
||||||
public static ValueTask<Result> CreateAssetAsync(string assetPath, CancellationToken token = default)
|
|
||||||
{
|
|
||||||
return CreateAssetAsync(assetPath, ReadOnlyMemory<byte>.Empty, token);
|
|
||||||
}
|
|
||||||
|
|
||||||
/// <summary>
|
|
||||||
/// Delete an asset and its metadata.
|
|
||||||
/// </summary>
|
|
||||||
/// <param name="guid">GUID of the asset to delete.</param>
|
|
||||||
/// <returns>Result indicating success or failure.</returns>
|
|
||||||
public static async ValueTask<Result> DeleteAssetAsync(Guid guid, CancellationToken token = default)
|
|
||||||
{
|
|
||||||
var pathResult = GuidToPath(guid);
|
|
||||||
if (pathResult.IsFailure)
|
|
||||||
{
|
|
||||||
return Result.Failure(pathResult.Message);
|
|
||||||
}
|
|
||||||
|
|
||||||
var fullPathResult = GetFullPath(pathResult.Value);
|
|
||||||
if (fullPathResult.IsFailure)
|
|
||||||
{
|
|
||||||
return Result.Failure(fullPathResult.Message);
|
|
||||||
}
|
|
||||||
|
|
||||||
try
|
|
||||||
{
|
|
||||||
var assetPath = fullPathResult.Value;
|
|
||||||
|
|
||||||
// Delete the asset file
|
|
||||||
if (File.Exists(assetPath))
|
|
||||||
{
|
|
||||||
File.Delete(assetPath);
|
|
||||||
}
|
|
||||||
|
|
||||||
// Delete the .gmeta file
|
|
||||||
var metaPath = assetPath + Utilities.FileExtensions.META_FILE_EXTENSION;
|
|
||||||
if (File.Exists(metaPath))
|
|
||||||
{
|
|
||||||
File.Delete(metaPath);
|
|
||||||
}
|
|
||||||
|
|
||||||
// Remove from database
|
|
||||||
await RemoveAssetFromDatabaseAsync(guid, token);
|
|
||||||
|
|
||||||
return Result.Success();
|
|
||||||
}
|
|
||||||
catch (Exception ex)
|
|
||||||
{
|
|
||||||
return Result.Failure($"Failed to delete asset: {ex.Message}");
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
/// <summary>
|
|
||||||
/// Delete an asset and its metadata by path.
|
|
||||||
/// </summary>
|
|
||||||
/// <param name="assetPath">Path to the asset to delete.</param>
|
|
||||||
/// <returns>Result indicating success or failure.</returns>
|
|
||||||
public static ValueTask<Result> DeleteAssetAsync(string assetPath, CancellationToken token = default)
|
|
||||||
{
|
|
||||||
var guidResult = PathToGuid(assetPath);
|
|
||||||
if (guidResult.IsFailure)
|
|
||||||
{
|
|
||||||
return new ValueTask<Result>(Task.FromResult(Result.Failure(guidResult.Message)));
|
|
||||||
}
|
|
||||||
|
|
||||||
return DeleteAssetAsync(guidResult.Value, token);
|
|
||||||
}
|
|
||||||
|
|
||||||
/// <summary>
|
|
||||||
/// Move an asset to a new location.
|
|
||||||
/// </summary>
|
|
||||||
/// <param name="guid">GUID of the asset to move.</param>
|
|
||||||
/// <param name="newPath">New path for the asset (relative or absolute).</param>
|
|
||||||
/// <returns>Result indicating success or failure.</returns>
|
|
||||||
public static async ValueTask<Result> MoveAssetAsync(Guid guid, string newPath, CancellationToken token = default)
|
|
||||||
{
|
|
||||||
var oldPathResult = GuidToPath(guid);
|
|
||||||
if (oldPathResult.IsFailure)
|
|
||||||
{
|
|
||||||
return Result.Failure(oldPathResult.Message);
|
|
||||||
}
|
|
||||||
|
|
||||||
var oldFullPathResult = GetFullPath(oldPathResult.Value);
|
|
||||||
if (oldFullPathResult.IsFailure)
|
|
||||||
{
|
|
||||||
return Result.Failure(oldFullPathResult.Message);
|
|
||||||
}
|
|
||||||
|
|
||||||
if (AssetsDirectory == null)
|
|
||||||
{
|
|
||||||
return Result.Failure("AssetsDirectory not initialized");
|
|
||||||
}
|
|
||||||
|
|
||||||
// Ensure new path is absolute and within assets directory
|
|
||||||
if (!Path.IsPathRooted(newPath))
|
|
||||||
{
|
|
||||||
newPath = Path.Combine(AssetsDirectory.FullName, newPath);
|
|
||||||
}
|
|
||||||
|
|
||||||
if (!newPath.StartsWith(AssetsDirectory.FullName, StringComparison.OrdinalIgnoreCase))
|
|
||||||
{
|
|
||||||
return Result.Failure("New path must be within the Assets directory");
|
|
||||||
}
|
|
||||||
|
|
||||||
if (File.Exists(newPath))
|
|
||||||
{
|
|
||||||
return Result.Failure("A file already exists at the new path");
|
|
||||||
}
|
|
||||||
|
|
||||||
try
|
|
||||||
{
|
|
||||||
var directory = Path.GetDirectoryName(newPath);
|
|
||||||
if (!string.IsNullOrEmpty(directory) && !Directory.Exists(directory))
|
|
||||||
{
|
|
||||||
Directory.CreateDirectory(directory);
|
|
||||||
}
|
|
||||||
|
|
||||||
// Read metadata and calculate hash before moving
|
|
||||||
var metaResult = await ReadMetaFileAsync(oldFullPathResult.Value, token);
|
|
||||||
if (metaResult.IsFailure)
|
|
||||||
{
|
|
||||||
return Result.Failure(metaResult.Message);
|
|
||||||
}
|
|
||||||
|
|
||||||
var fileHash = await CalculateFileHashAsync(oldFullPathResult.Value, token);
|
|
||||||
|
|
||||||
// Move the asset file
|
|
||||||
File.Move(oldFullPathResult.Value, newPath);
|
|
||||||
|
|
||||||
// Move the .gmeta file
|
|
||||||
var oldMetaPath = oldFullPathResult.Value + Utilities.FileExtensions.META_FILE_EXTENSION;
|
|
||||||
var newMetaPath = newPath + Utilities.FileExtensions.META_FILE_EXTENSION;
|
|
||||||
if (File.Exists(oldMetaPath))
|
|
||||||
{
|
|
||||||
File.Move(oldMetaPath, newMetaPath);
|
|
||||||
}
|
|
||||||
|
|
||||||
// Update database directly (bypassing file watcher)
|
|
||||||
await UpsertAssetAsync(newPath, metaResult.Value, fileHash, null, token);
|
|
||||||
|
|
||||||
return Result.Success();
|
|
||||||
}
|
|
||||||
catch (Exception ex)
|
|
||||||
{
|
|
||||||
return Result.Failure($"Failed to move asset: {ex.Message}");
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
/// <summary>
|
|
||||||
/// Move an asset to a new location by path.
|
|
||||||
/// </summary>
|
|
||||||
/// <param name="oldPath">Current path of the asset.</param>
|
|
||||||
/// <param name="newPath">New path for the asset (relative or absolute).</param>
|
|
||||||
/// <returns>Result indicating success or failure.</returns>
|
|
||||||
public static ValueTask<Result> MoveAssetAsync(string oldPath, string newPath, CancellationToken token = default)
|
|
||||||
{
|
|
||||||
var guidResult = PathToGuid(oldPath);
|
|
||||||
if (guidResult.IsFailure)
|
|
||||||
{
|
|
||||||
return ValueTask.FromResult(Result.Failure(guidResult.Message));
|
|
||||||
}
|
|
||||||
|
|
||||||
return MoveAssetAsync(guidResult.Value, newPath, token);
|
|
||||||
}
|
|
||||||
|
|
||||||
/// <summary>
|
|
||||||
/// Copy an asset to a new location with a new GUID.
|
|
||||||
/// </summary>
|
|
||||||
/// <param name="guid">GUID of the asset to copy.</param>
|
|
||||||
/// <param name="newPath">New path for the copied asset (relative or absolute).</param>
|
|
||||||
/// <returns>Result containing the new asset's GUID.</returns>
|
|
||||||
public static async ValueTask<Result<Guid>> CopyAssetAsync(Guid guid, string newPath, CancellationToken token = default)
|
|
||||||
{
|
|
||||||
var oldPathResult = GuidToPath(guid);
|
|
||||||
if (oldPathResult.IsFailure)
|
|
||||||
{
|
|
||||||
return Result<Guid>.Failure(oldPathResult.Message);
|
|
||||||
}
|
|
||||||
|
|
||||||
var oldFullPathResult = GetFullPath(oldPathResult.Value);
|
|
||||||
if (oldFullPathResult.IsFailure)
|
|
||||||
{
|
|
||||||
return Result<Guid>.Failure(oldFullPathResult.Message);
|
|
||||||
}
|
|
||||||
|
|
||||||
if (AssetsDirectory == null)
|
|
||||||
{
|
|
||||||
return Result<Guid>.Failure("AssetsDirectory not initialized");
|
|
||||||
}
|
|
||||||
|
|
||||||
// Ensure new path is absolute and within assets directory
|
|
||||||
if (!Path.IsPathRooted(newPath))
|
|
||||||
{
|
|
||||||
newPath = Path.Combine(AssetsDirectory.FullName, newPath);
|
|
||||||
}
|
|
||||||
|
|
||||||
if (!newPath.StartsWith(AssetsDirectory.FullName, StringComparison.OrdinalIgnoreCase))
|
|
||||||
{
|
|
||||||
return Result<Guid>.Failure("New path must be within the Assets directory");
|
|
||||||
}
|
|
||||||
|
|
||||||
if (File.Exists(newPath))
|
|
||||||
{
|
|
||||||
return Result<Guid>.Failure("A file already exists at the new path");
|
|
||||||
}
|
|
||||||
|
|
||||||
try
|
|
||||||
{
|
|
||||||
var directory = Path.GetDirectoryName(newPath);
|
|
||||||
if (!string.IsNullOrEmpty(directory) && !Directory.Exists(directory))
|
|
||||||
{
|
|
||||||
Directory.CreateDirectory(directory);
|
|
||||||
}
|
|
||||||
|
|
||||||
await using var oldFs = File.OpenRead(oldFullPathResult.Value);
|
|
||||||
await using var newFs = File.Create(newPath);
|
|
||||||
await oldFs.CopyToAsync(newFs, token);
|
|
||||||
|
|
||||||
// Generate new metadata with new GUID
|
|
||||||
await GenerateMetaFileAsync(newPath, token);
|
|
||||||
|
|
||||||
// Get the new GUID
|
|
||||||
var newGuidResult = PathToGuid(newPath);
|
|
||||||
if (newGuidResult.IsFailure)
|
|
||||||
{
|
|
||||||
return Result<Guid>.Failure(newGuidResult.Message);
|
|
||||||
}
|
|
||||||
|
|
||||||
return newGuidResult.Value;
|
|
||||||
}
|
|
||||||
catch (Exception ex)
|
|
||||||
{
|
|
||||||
return Result<Guid>.Failure($"Failed to copy asset: {ex.Message}");
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
/// <summary>
|
|
||||||
/// Copy an asset to a new location by path.
|
|
||||||
/// </summary>
|
|
||||||
/// <param name="sourcePath">Path of the asset to copy.</param>
|
|
||||||
/// <param name="destPath">New path for the copied asset (relative or absolute).</param>
|
|
||||||
/// <returns>Result containing the new asset's GUID.</returns>
|
|
||||||
public static ValueTask<Result<Guid>> CopyAssetAsync(string sourcePath, string destPath, CancellationToken token = default)
|
|
||||||
{
|
|
||||||
var guidResult = PathToGuid(sourcePath);
|
|
||||||
if (guidResult.IsFailure)
|
|
||||||
{
|
|
||||||
return new ValueTask<Result<Guid>>(Task.FromResult(Result<Guid>.Failure(guidResult.Message)));
|
|
||||||
}
|
|
||||||
|
|
||||||
return CopyAssetAsync(guidResult.Value, destPath, token);
|
|
||||||
}
|
|
||||||
|
|
||||||
/// <summary>
|
|
||||||
/// Mark an asset as dirty for re-importing (in-memory only).
|
|
||||||
/// </summary>
|
|
||||||
/// <param name="guid">GUID of the asset to mark dirty.</param>
|
|
||||||
/// <returns>Result indicating success or failure.</returns>
|
|
||||||
public static Result MarkDirtyAsync(Guid guid, CancellationToken token = default)
|
|
||||||
{
|
|
||||||
MarkDirty(guid);
|
|
||||||
return Result.Success();
|
|
||||||
}
|
|
||||||
|
|
||||||
/// <summary>
|
|
||||||
/// Import all dirty assets.
|
|
||||||
/// </summary>
|
|
||||||
/// <returns>Result indicating success or failure.</returns>
|
|
||||||
public static async Task<Result> ImportDirtyAssetsAsync(CancellationToken token = default)
|
|
||||||
{
|
|
||||||
var dirtyGuids = GetDirtyAssets();
|
|
||||||
|
|
||||||
foreach (var guid in dirtyGuids)
|
|
||||||
{
|
|
||||||
var pathResult = GuidToPath(guid);
|
|
||||||
if (pathResult.IsFailure)
|
|
||||||
{
|
|
||||||
continue;
|
|
||||||
}
|
|
||||||
|
|
||||||
var fullPathResult = GetFullPath(pathResult.Value);
|
|
||||||
if (fullPathResult.IsFailure)
|
|
||||||
{
|
|
||||||
continue;
|
|
||||||
}
|
|
||||||
|
|
||||||
var result = await ImportAssetAsync(fullPathResult.Value, token);
|
|
||||||
if (result.IsSuccess)
|
|
||||||
{
|
|
||||||
ClearDirty(guid);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
return Result.Success();
|
|
||||||
}
|
|
||||||
}
|
|
||||||
@@ -1,128 +0,0 @@
|
|||||||
using Ghost.Core;
|
|
||||||
using System.Reflection;
|
|
||||||
|
|
||||||
namespace Ghost.Editor.Core.AssetHandle;
|
|
||||||
|
|
||||||
public static partial class AssetDatabase
|
|
||||||
{
|
|
||||||
private static readonly Dictionary<Type, AssetImporter> s_importerInstances = new();
|
|
||||||
|
|
||||||
/// <summary>
|
|
||||||
/// Import an asset at the specified path.
|
|
||||||
/// </summary>
|
|
||||||
/// <param name="assetPath">Full path to the asset file.</param>
|
|
||||||
/// <returns>Result indicating success or failure.</returns>
|
|
||||||
private static async ValueTask<Result> ImportAssetAsync(string assetPath, CancellationToken token = default)
|
|
||||||
{
|
|
||||||
var extension = Path.GetExtension(assetPath);
|
|
||||||
|
|
||||||
if (!s_importerTypeLookup.TryGetValue(extension, out var importerType))
|
|
||||||
{
|
|
||||||
// No importer registered for this file type
|
|
||||||
return Result.Success();
|
|
||||||
}
|
|
||||||
|
|
||||||
// Get or create importer instance
|
|
||||||
if (!s_importerInstances.TryGetValue(importerType, out var importerInstance))
|
|
||||||
{
|
|
||||||
importerInstance = Activator.CreateInstance(importerType) as AssetImporter;
|
|
||||||
if (importerInstance is null)
|
|
||||||
{
|
|
||||||
return Result.Failure($"Failed to create importer instance for type {importerType.Name}");
|
|
||||||
}
|
|
||||||
|
|
||||||
s_importerInstances[importerType] = importerInstance;
|
|
||||||
}
|
|
||||||
|
|
||||||
// Read metadata
|
|
||||||
var metaResult = await ReadMetaFileAsync(assetPath, token);
|
|
||||||
if (metaResult.IsFailure)
|
|
||||||
{
|
|
||||||
return Result.Failure($"Failed to read asset metadata: {metaResult.Message}");
|
|
||||||
}
|
|
||||||
|
|
||||||
return await importerInstance.ImportAsync(assetPath, metaResult.Value, token);
|
|
||||||
}
|
|
||||||
|
|
||||||
/// <summary>
|
|
||||||
/// Get the importer type for a specific file extension.
|
|
||||||
/// </summary>
|
|
||||||
/// <param name="extension">File extension (e.g., ".png").</param>
|
|
||||||
/// <returns>The importer type if found, otherwise null.</returns>
|
|
||||||
public static Type? GetImporterType(string extension)
|
|
||||||
{
|
|
||||||
s_importerTypeLookup.TryGetValue(extension, out var importerType);
|
|
||||||
return importerType;
|
|
||||||
}
|
|
||||||
|
|
||||||
/// <summary>
|
|
||||||
/// Get all registered importer types and their supported extensions.
|
|
||||||
/// </summary>
|
|
||||||
/// <returns>Dictionary mapping extensions to importer types.</returns>
|
|
||||||
public static Dictionary<string, Type> GetAllImporters()
|
|
||||||
{
|
|
||||||
return new Dictionary<string, Type>(s_importerTypeLookup);
|
|
||||||
}
|
|
||||||
|
|
||||||
/// <summary>
|
|
||||||
/// Export in-memory asset data to disk.
|
|
||||||
/// The importer will serialize the data into a format it can later import.
|
|
||||||
/// </summary>
|
|
||||||
/// <typeparam name="T">Type of asset data to export.</typeparam>
|
|
||||||
/// <param name="assetPath">Full path where the asset should be saved.</param>
|
|
||||||
/// <param name="assetData">In-memory asset data to export.</param>
|
|
||||||
/// <returns>Result with the GUID of the exported asset.</returns>
|
|
||||||
public static async ValueTask<Result<Guid>> ExportAssetAsync<T>(string assetPath, T assetData, CancellationToken token = default) where T : class
|
|
||||||
{
|
|
||||||
var extension = Path.GetExtension(assetPath);
|
|
||||||
|
|
||||||
if (!s_importerTypeLookup.TryGetValue(extension, out var importerType))
|
|
||||||
{
|
|
||||||
return Result<Guid>.Failure($"No importer registered for extension {extension}");
|
|
||||||
}
|
|
||||||
|
|
||||||
// Get or create importer instance
|
|
||||||
if (!s_importerInstances.TryGetValue(importerType, out var importerInstance))
|
|
||||||
{
|
|
||||||
importerInstance = Activator.CreateInstance(importerType) as AssetImporter;
|
|
||||||
if (importerInstance is null)
|
|
||||||
{
|
|
||||||
return Result<Guid>.Failure($"Failed to create importer instance for type {importerType.Name}");
|
|
||||||
}
|
|
||||||
|
|
||||||
s_importerInstances[importerType] = importerInstance;
|
|
||||||
}
|
|
||||||
|
|
||||||
// Find and invoke the ExportAsync method
|
|
||||||
var exportMethod = importerType.GetMethod("ExportAsync", BindingFlags.Public | BindingFlags.Instance);
|
|
||||||
if (exportMethod == null)
|
|
||||||
{
|
|
||||||
return Result<Guid>.Failure($"ExportAsync method not found on importer {importerType.Name}. This importer does not support exporting.");
|
|
||||||
}
|
|
||||||
|
|
||||||
// Generate metadata for the new asset
|
|
||||||
var result = await GenerateMetaFileAsync(assetPath, token);
|
|
||||||
if (result.IsFailure)
|
|
||||||
{
|
|
||||||
return Result<Guid>.Failure($"Failed to generate metadata: {result.Message}");
|
|
||||||
}
|
|
||||||
|
|
||||||
var metaResult = await ReadMetaFileAsync(assetPath, token);
|
|
||||||
if (metaResult.IsFailure)
|
|
||||||
{
|
|
||||||
return Result<Guid>.Failure($"Failed to read metadata: {metaResult.Message}");
|
|
||||||
}
|
|
||||||
|
|
||||||
result = await importerInstance.ExportAsync(assetPath, assetData, metaResult.Value, token);
|
|
||||||
if (result.IsFailure)
|
|
||||||
{
|
|
||||||
return Result<Guid>.Failure(result.Message);
|
|
||||||
}
|
|
||||||
|
|
||||||
// Calculate file hash and update database
|
|
||||||
var fileHash = await CalculateFileHashAsync(assetPath, token);
|
|
||||||
await UpsertAssetAsync(assetPath, metaResult.Value, fileHash, null, token);
|
|
||||||
|
|
||||||
return metaResult.Value.Guid;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
@@ -1,212 +0,0 @@
|
|||||||
using Ghost.Core;
|
|
||||||
using Ghost.Data.Services;
|
|
||||||
using System.Collections.Concurrent;
|
|
||||||
using System.Text.Json;
|
|
||||||
|
|
||||||
namespace Ghost.Editor.Core.AssetHandle;
|
|
||||||
|
|
||||||
public static partial class AssetDatabase
|
|
||||||
{
|
|
||||||
// Asset cache - stores loaded assets by GUID
|
|
||||||
private static readonly ConcurrentDictionary<Guid, Asset> s_assetCache = new();
|
|
||||||
|
|
||||||
// LRU tracking - stores access time for each cached asset
|
|
||||||
private static readonly ConcurrentDictionary<Guid, DateTime> s_assetAccessTime = new();
|
|
||||||
|
|
||||||
// Maximum number of cached assets before eviction starts
|
|
||||||
private const int MAX_CACHED_ASSETS = 1000;
|
|
||||||
|
|
||||||
// Percentage of cache to evict when limit is reached (evict oldest 20%)
|
|
||||||
private const float _CACHE_EVICTION_PERCENTAGE = 0.2f;
|
|
||||||
|
|
||||||
private static Result<string> GetImportedAssetsDirectory()
|
|
||||||
{
|
|
||||||
if (AssetsDirectory == null)
|
|
||||||
{
|
|
||||||
return Result<string>.Failure("AssetsDirectory not initialized");
|
|
||||||
}
|
|
||||||
|
|
||||||
var cacheDir = Path.Combine(AssetsDirectory.Parent!.FullName, ProjectService.CACHE_FOLDER, "ImportedAssets");
|
|
||||||
if (!Directory.Exists(cacheDir))
|
|
||||||
{
|
|
||||||
Directory.CreateDirectory(cacheDir);
|
|
||||||
}
|
|
||||||
|
|
||||||
return cacheDir;
|
|
||||||
}
|
|
||||||
|
|
||||||
private static Result<string> GetImportedAssetPath(Guid guid)
|
|
||||||
{
|
|
||||||
var importedDirResult = GetImportedAssetsDirectory();
|
|
||||||
if (importedDirResult.IsFailure)
|
|
||||||
{
|
|
||||||
return Result<string>.Failure(importedDirResult.Message);
|
|
||||||
}
|
|
||||||
|
|
||||||
// Store imported assets as {GUID}.asset
|
|
||||||
var assetDataPath = Path.Combine(importedDirResult.Value, $"{guid}.asset");
|
|
||||||
return assetDataPath;
|
|
||||||
}
|
|
||||||
|
|
||||||
private static Result<T> LoadAssetInternal<T>(Guid guid) where T : Asset
|
|
||||||
{
|
|
||||||
// Check cache first
|
|
||||||
if (s_assetCache.TryGetValue(guid, out var cachedAsset))
|
|
||||||
{
|
|
||||||
// Update access time for LRU
|
|
||||||
s_assetAccessTime[guid] = DateTime.UtcNow;
|
|
||||||
|
|
||||||
if (cachedAsset is T typedAsset)
|
|
||||||
{
|
|
||||||
return typedAsset;
|
|
||||||
}
|
|
||||||
else
|
|
||||||
{
|
|
||||||
return Result<T>.Failure($"Cached asset is of type {cachedAsset.GetType().Name}, expected {typeof(T).Name}");
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
// Asset not in cache, load from disk
|
|
||||||
var assetPathResult = GetImportedAssetPath(guid);
|
|
||||||
if (assetPathResult.IsFailure)
|
|
||||||
{
|
|
||||||
return Result<T>.Failure(assetPathResult.Message);
|
|
||||||
}
|
|
||||||
|
|
||||||
var assetDataPath = assetPathResult.Value;
|
|
||||||
if (!File.Exists(assetDataPath))
|
|
||||||
{
|
|
||||||
return Result<T>.Failure($"Imported asset data not found at {assetDataPath}. Asset may not have been imported yet.");
|
|
||||||
}
|
|
||||||
|
|
||||||
try
|
|
||||||
{
|
|
||||||
// Read and deserialize asset data
|
|
||||||
var json = File.ReadAllText(assetDataPath);
|
|
||||||
var asset = JsonSerializer.Deserialize<T>(json);
|
|
||||||
if (asset == null)
|
|
||||||
{
|
|
||||||
return Result<T>.Failure("Failed to deserialize asset data");
|
|
||||||
}
|
|
||||||
|
|
||||||
// Add to cache
|
|
||||||
CacheAsset(guid, asset);
|
|
||||||
return asset;
|
|
||||||
}
|
|
||||||
catch (Exception ex)
|
|
||||||
{
|
|
||||||
return Result<T>.Failure($"Failed to load asset: {ex.Message}");
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
public static Result<T> LoadAssetAtPath<T>(string assetPath) where T : Asset
|
|
||||||
{
|
|
||||||
var guidResult = PathToGuid(assetPath);
|
|
||||||
if (guidResult.IsFailure)
|
|
||||||
{
|
|
||||||
return Result<T>.Failure(guidResult.Message);
|
|
||||||
}
|
|
||||||
|
|
||||||
return LoadAsset<T>(guidResult.Value);
|
|
||||||
}
|
|
||||||
|
|
||||||
private static void CacheAsset(Guid guid, Asset asset)
|
|
||||||
{
|
|
||||||
// Check if we need to evict old assets
|
|
||||||
if (s_assetCache.Count >= MAX_CACHED_ASSETS)
|
|
||||||
{
|
|
||||||
EvictOldestAssets();
|
|
||||||
}
|
|
||||||
|
|
||||||
s_assetCache[guid] = asset;
|
|
||||||
s_assetAccessTime[guid] = DateTime.UtcNow;
|
|
||||||
}
|
|
||||||
|
|
||||||
private static void EvictOldestAssets()
|
|
||||||
{
|
|
||||||
var evictionCount = (int)(MAX_CACHED_ASSETS * _CACHE_EVICTION_PERCENTAGE);
|
|
||||||
|
|
||||||
// Sort by access time and remove oldest entries
|
|
||||||
var oldestAssets = s_assetAccessTime
|
|
||||||
.OrderBy(kvp => kvp.Value)
|
|
||||||
.Take(evictionCount)
|
|
||||||
.Select(kvp => kvp.Key)
|
|
||||||
.ToList();
|
|
||||||
|
|
||||||
foreach (var guid in oldestAssets)
|
|
||||||
{
|
|
||||||
s_assetCache.TryRemove(guid, out _);
|
|
||||||
s_assetAccessTime.TryRemove(guid, out _);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
/// <summary>
|
|
||||||
/// Unload a specific asset from cache.
|
|
||||||
/// </summary>
|
|
||||||
/// <param name="guid">GUID of the asset to unload.</param>
|
|
||||||
public static void UnloadAsset(Guid guid)
|
|
||||||
{
|
|
||||||
s_assetCache.TryRemove(guid, out _);
|
|
||||||
s_assetAccessTime.TryRemove(guid, out _);
|
|
||||||
}
|
|
||||||
|
|
||||||
/// <summary>
|
|
||||||
/// Unload all assets from cache.
|
|
||||||
/// </summary>
|
|
||||||
public static void UnloadAllAssets()
|
|
||||||
{
|
|
||||||
s_assetCache.Clear();
|
|
||||||
s_assetAccessTime.Clear();
|
|
||||||
}
|
|
||||||
|
|
||||||
/// <summary>
|
|
||||||
/// Check if an asset is currently loaded in cache.
|
|
||||||
/// </summary>
|
|
||||||
/// <param name="guid">GUID of the asset.</param>
|
|
||||||
/// <returns>True if the asset is in cache.</returns>
|
|
||||||
public static bool IsAssetLoaded(Guid guid)
|
|
||||||
{
|
|
||||||
return s_assetCache.ContainsKey(guid);
|
|
||||||
}
|
|
||||||
|
|
||||||
/// <summary>
|
|
||||||
/// Get cache statistics.
|
|
||||||
/// </summary>
|
|
||||||
/// <returns>Tuple of (current cache size, max cache size).</returns>
|
|
||||||
public static (int currentSize, int maxSize) GetCacheStats()
|
|
||||||
{
|
|
||||||
return (s_assetCache.Count, MAX_CACHED_ASSETS);
|
|
||||||
}
|
|
||||||
|
|
||||||
/// <summary>
|
|
||||||
/// Save an imported asset to disk for later loading.
|
|
||||||
/// This should be called by importers after processing the source file.
|
|
||||||
/// </summary>
|
|
||||||
/// <typeparam name="T">Type of asset data.</typeparam>
|
|
||||||
/// <param name="guid">GUID of the asset.</param>
|
|
||||||
/// <param name="assetData">Processed asset data to save.</param>
|
|
||||||
/// <returns>Result indicating success or failure.</returns>
|
|
||||||
public static Result SaveImportedAsset<T>(Guid guid, T assetData)
|
|
||||||
where T : Asset
|
|
||||||
{
|
|
||||||
var assetPathResult = GetImportedAssetPath(guid);
|
|
||||||
if (assetPathResult.IsFailure)
|
|
||||||
{
|
|
||||||
return Result.Failure(assetPathResult.Message);
|
|
||||||
}
|
|
||||||
|
|
||||||
try
|
|
||||||
{
|
|
||||||
var json = JsonSerializer.Serialize(assetData, s_defaultJsonOptions);
|
|
||||||
File.WriteAllText(assetPathResult.Value, json);
|
|
||||||
|
|
||||||
// Invalidate cache for this asset so it gets reloaded next time
|
|
||||||
UnloadAsset(guid);
|
|
||||||
return Result.Success();
|
|
||||||
}
|
|
||||||
catch (Exception ex)
|
|
||||||
{
|
|
||||||
return Result.Failure($"Failed to save imported asset: {ex.Message}");
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
@@ -1,203 +0,0 @@
|
|||||||
using Ghost.Core;
|
|
||||||
using System.Text.Json;
|
|
||||||
|
|
||||||
namespace Ghost.Editor.Core.AssetHandle;
|
|
||||||
|
|
||||||
public static partial class AssetDatabase
|
|
||||||
{
|
|
||||||
/// <summary>
|
|
||||||
/// Get the relative path from the assets directory.
|
|
||||||
/// </summary>
|
|
||||||
private static Result<string> GetRelativePath(string fullPath)
|
|
||||||
{
|
|
||||||
if (AssetsDirectory == null)
|
|
||||||
{
|
|
||||||
return Result<string>.Failure("AssetsDirectory not initialized");
|
|
||||||
}
|
|
||||||
|
|
||||||
if (!fullPath.StartsWith(AssetsDirectory.FullName, StringComparison.OrdinalIgnoreCase))
|
|
||||||
{
|
|
||||||
return Result<string>.Failure("Path is not within assets directory");
|
|
||||||
}
|
|
||||||
|
|
||||||
return Path.GetRelativePath(AssetsDirectory.FullName, fullPath);
|
|
||||||
}
|
|
||||||
|
|
||||||
/// <summary>
|
|
||||||
/// Get the full path from a relative path.
|
|
||||||
/// </summary>
|
|
||||||
private static Result<string> GetFullPath(string relativePath)
|
|
||||||
{
|
|
||||||
if (AssetsDirectory == null)
|
|
||||||
{
|
|
||||||
return Result<string>.Failure("AssetsDirectory not initialized");
|
|
||||||
}
|
|
||||||
|
|
||||||
return Path.Combine(AssetsDirectory.FullName, relativePath);
|
|
||||||
}
|
|
||||||
|
|
||||||
/// <summary>
|
|
||||||
/// Find GUID by asset path.
|
|
||||||
/// </summary>
|
|
||||||
/// <param name="assetPath">Full or relative path to the asset.</param>
|
|
||||||
/// <returns>The GUID of the asset if found.</returns>
|
|
||||||
public static Result<Guid> PathToGuid(string assetPath)
|
|
||||||
{
|
|
||||||
var relativePath = assetPath;
|
|
||||||
|
|
||||||
// Convert to relative path if it's a full path
|
|
||||||
if (Path.IsPathRooted(assetPath))
|
|
||||||
{
|
|
||||||
var relResult = GetRelativePath(assetPath);
|
|
||||||
if (relResult.IsFailure)
|
|
||||||
{
|
|
||||||
return Result<Guid>.Failure(relResult.Message);
|
|
||||||
}
|
|
||||||
relativePath = relResult.Value;
|
|
||||||
}
|
|
||||||
|
|
||||||
// Normalize path separators
|
|
||||||
relativePath = relativePath.Replace('\\', '/');
|
|
||||||
|
|
||||||
lock (s_dbLock)
|
|
||||||
{
|
|
||||||
if (s_pathAssetLookup.TryGetValue(relativePath, out var guid))
|
|
||||||
{
|
|
||||||
return guid;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
return Result<Guid>.Failure("Asset not found in database");
|
|
||||||
}
|
|
||||||
|
|
||||||
/// <summary>
|
|
||||||
/// Find path by GUID.
|
|
||||||
/// </summary>
|
|
||||||
/// <param name="guid">GUID of the asset.</param>
|
|
||||||
/// <returns>The relative path to the asset if found.</returns>
|
|
||||||
public static Result<string> GuidToPath(Guid guid)
|
|
||||||
{
|
|
||||||
lock (s_dbLock)
|
|
||||||
{
|
|
||||||
if (s_assetPathLookup.TryGetValue(guid, out var path))
|
|
||||||
{
|
|
||||||
return path;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
return Result<string>.Failure("Asset GUID not found in database");
|
|
||||||
}
|
|
||||||
|
|
||||||
/// <summary>
|
|
||||||
/// Load asset by GUID with caching.
|
|
||||||
/// </summary>
|
|
||||||
/// <typeparam name="T">Type of asset to load.</typeparam>
|
|
||||||
/// <param name="guid">GUID of the asset.</param>
|
|
||||||
/// <returns>The loaded asset.</returns>
|
|
||||||
public static Result<T> LoadAsset<T>(Guid guid) where T : Asset
|
|
||||||
{
|
|
||||||
// Implemented in AssetDatabase.Loader.cs
|
|
||||||
return LoadAssetInternal<T>(guid);
|
|
||||||
}
|
|
||||||
|
|
||||||
/// <summary>
|
|
||||||
/// Get asset tags by GUID.
|
|
||||||
/// </summary>
|
|
||||||
/// <param name="guid">GUID of the asset.</param>
|
|
||||||
/// <returns>List of tags associated with the asset.</returns>
|
|
||||||
public static async ValueTask<Result<List<string>>> GetAssetTagsAsync(Guid guid, CancellationToken token = default)
|
|
||||||
{
|
|
||||||
var pathResult = GuidToPath(guid);
|
|
||||||
if (pathResult.IsFailure)
|
|
||||||
{
|
|
||||||
return Result<List<string>>.Failure(pathResult.Message);
|
|
||||||
}
|
|
||||||
|
|
||||||
var fullPathResult = GetFullPath(pathResult.Value);
|
|
||||||
if (fullPathResult.IsFailure)
|
|
||||||
{
|
|
||||||
return Result<List<string>>.Failure(fullPathResult.Message);
|
|
||||||
}
|
|
||||||
|
|
||||||
var metaResult = await ReadMetaFileAsync(fullPathResult.Value, token);
|
|
||||||
if (metaResult.IsFailure)
|
|
||||||
{
|
|
||||||
return Result<List<string>>.Failure(metaResult.Message);
|
|
||||||
}
|
|
||||||
|
|
||||||
return metaResult.Value.Tags;
|
|
||||||
}
|
|
||||||
|
|
||||||
/// <summary>
|
|
||||||
/// Set asset tags by GUID.
|
|
||||||
/// </summary>
|
|
||||||
/// <param name="guid">GUID of the asset.</param>
|
|
||||||
/// <param name="tags">New tags for the asset.</param>
|
|
||||||
/// <returns>Result indicating success or failure.</returns>
|
|
||||||
public static async ValueTask<Result> SetAssetTagsAsync(Guid guid, List<string> tags, CancellationToken token = default)
|
|
||||||
{
|
|
||||||
var pathResult = GuidToPath(guid);
|
|
||||||
if (pathResult.IsFailure)
|
|
||||||
{
|
|
||||||
return Result.Failure(pathResult.Message);
|
|
||||||
}
|
|
||||||
|
|
||||||
var fullPathResult = GetFullPath(pathResult.Value);
|
|
||||||
if (fullPathResult.IsFailure)
|
|
||||||
{
|
|
||||||
return Result.Failure(fullPathResult.Message);
|
|
||||||
}
|
|
||||||
|
|
||||||
var metaResult = await ReadMetaFileAsync(fullPathResult.Value, token);
|
|
||||||
if (metaResult.IsFailure)
|
|
||||||
{
|
|
||||||
return Result.Failure(metaResult.Message);
|
|
||||||
}
|
|
||||||
|
|
||||||
metaResult.Value.Tags = tags;
|
|
||||||
|
|
||||||
// Write updated metadata to .gmeta file
|
|
||||||
var writeResult = await WriteMetaFileAsync(fullPathResult.Value + Utilities.FileExtensions.META_FILE_EXTENSION, metaResult.Value, token);
|
|
||||||
if (writeResult.IsFailure)
|
|
||||||
{
|
|
||||||
return writeResult;
|
|
||||||
}
|
|
||||||
|
|
||||||
// Update database with new tags
|
|
||||||
var fileHash = await CalculateFileHashAsync(fullPathResult.Value, token);
|
|
||||||
return await UpsertAssetAsync(fullPathResult.Value, metaResult.Value, fileHash, null, token);
|
|
||||||
}
|
|
||||||
|
|
||||||
/// <summary>
|
|
||||||
/// Search assets by name pattern.
|
|
||||||
/// Supports SQL LIKE wildcards: * (any characters) and ? (single character).
|
|
||||||
/// </summary>
|
|
||||||
/// <param name="namePattern">Search pattern (e.g., "*.txt", "player?", "test*").</param>
|
|
||||||
/// <returns>List of matching asset GUIDs.</returns>
|
|
||||||
public static async Task<List<Guid>> FindAssetsByNameAsync(string namePattern, CancellationToken token = default)
|
|
||||||
{
|
|
||||||
return await GetAssetsByNameAsync(namePattern, token);
|
|
||||||
}
|
|
||||||
|
|
||||||
/// <summary>
|
|
||||||
/// Find assets by tag.
|
|
||||||
/// </summary>
|
|
||||||
/// <param name="tag">Tag to search for.</param>
|
|
||||||
/// <returns>List of asset GUIDs with the specified tag.</returns>
|
|
||||||
public static async Task<List<Guid>> FindAssetsByTagAsync(string tag, CancellationToken token = default)
|
|
||||||
{
|
|
||||||
return await GetAssetsByTagAsync(tag, token);
|
|
||||||
}
|
|
||||||
|
|
||||||
/// <summary>
|
|
||||||
/// Get all assets in the database.
|
|
||||||
/// </summary>
|
|
||||||
/// <returns>Dictionary mapping GUIDs to relative paths.</returns>
|
|
||||||
public static IReadOnlyDictionary<Guid, string> GetAllAssets()
|
|
||||||
{
|
|
||||||
lock (s_dbLock)
|
|
||||||
{
|
|
||||||
return s_assetPathLookup.AsReadOnly();
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
@@ -1,251 +0,0 @@
|
|||||||
using Ghost.Core;
|
|
||||||
using Ghost.Editor.Core.Utilities;
|
|
||||||
using System.Reflection;
|
|
||||||
using System.Runtime.CompilerServices;
|
|
||||||
using System.Security.Cryptography;
|
|
||||||
using System.Text.Json;
|
|
||||||
|
|
||||||
namespace Ghost.Editor.Core.AssetHandle;
|
|
||||||
|
|
||||||
public static partial class AssetDatabase
|
|
||||||
{
|
|
||||||
private static readonly Dictionary<string, Type> s_importerTypeLookup = new();
|
|
||||||
|
|
||||||
private static void InitializeMetaData()
|
|
||||||
{
|
|
||||||
if (s_watcher == null)
|
|
||||||
{
|
|
||||||
throw new InvalidOperationException("AssetDatabase is not initialized. Ensure that Initialize() is called before registering asset importers.");
|
|
||||||
}
|
|
||||||
|
|
||||||
var importerTypes = TypeCache.GetTypes().Where(t => t.GetCustomAttribute<AssetImporterAttribute>() != null);
|
|
||||||
foreach (var type in importerTypes)
|
|
||||||
{
|
|
||||||
var attribute = type.GetCustomAttribute<AssetImporterAttribute>()!;
|
|
||||||
foreach (var extension in attribute.SupportedExtensions)
|
|
||||||
{
|
|
||||||
s_importerTypeLookup[extension] = type;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
s_watcher.Created += OnFSEvent;
|
|
||||||
s_watcher.Deleted += OnFSEvent;
|
|
||||||
s_watcher.Changed += OnFSEvent;
|
|
||||||
s_watcher.Renamed += OnAssetRenamed;
|
|
||||||
}
|
|
||||||
|
|
||||||
private static Result<string> GetMetaFilePath(string assetPath)
|
|
||||||
{
|
|
||||||
if (Directory.Exists(assetPath))
|
|
||||||
{
|
|
||||||
return Result<string>.Failure("Cannot create metadata for directories");
|
|
||||||
}
|
|
||||||
|
|
||||||
if (Path.GetExtension(assetPath).Equals(FileExtensions.META_FILE_EXTENSION, StringComparison.OrdinalIgnoreCase))
|
|
||||||
{
|
|
||||||
return Result<string>.Failure("Cannot create metadata for metadata files");
|
|
||||||
}
|
|
||||||
|
|
||||||
return assetPath + FileExtensions.META_FILE_EXTENSION;
|
|
||||||
}
|
|
||||||
|
|
||||||
private static ImporterSettings? GetDefaultSettingsForAsset(string assetPath)
|
|
||||||
{
|
|
||||||
var extension = Path.GetExtension(assetPath);
|
|
||||||
|
|
||||||
if (s_importerTypeLookup.TryGetValue(extension, out var importerType))
|
|
||||||
{
|
|
||||||
var settingsType = importerType.BaseType?.GetGenericArguments()[0];
|
|
||||||
if (settingsType == null || !typeof(ImporterSettings).IsAssignableFrom(settingsType))
|
|
||||||
{
|
|
||||||
return null;
|
|
||||||
}
|
|
||||||
|
|
||||||
return (ImporterSettings?)Activator.CreateInstance(settingsType);
|
|
||||||
}
|
|
||||||
|
|
||||||
return null;
|
|
||||||
}
|
|
||||||
|
|
||||||
/// <summary>
|
|
||||||
/// Calculate SHA256 hash of a file for change detection.
|
|
||||||
/// </summary>
|
|
||||||
private static async Task<string> CalculateFileHashAsync(string filePath, CancellationToken token = default)
|
|
||||||
{
|
|
||||||
try
|
|
||||||
{
|
|
||||||
await using var stream = File.OpenRead(filePath);
|
|
||||||
var hash = await SHA256.HashDataAsync(stream, token);
|
|
||||||
return Convert.ToHexString(hash);
|
|
||||||
}
|
|
||||||
catch
|
|
||||||
{
|
|
||||||
return string.Empty;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
private static async Task<Result> WriteMetaFileAsync(string metaFilePath, AssetMeta metaData, CancellationToken token = default)
|
|
||||||
{
|
|
||||||
try
|
|
||||||
{
|
|
||||||
await using var fileStream = File.Create(metaFilePath);
|
|
||||||
await JsonSerializer.SerializeAsync(fileStream, metaData, s_defaultJsonOptions, token);
|
|
||||||
return Result.Success();
|
|
||||||
}
|
|
||||||
catch (Exception ex)
|
|
||||||
{
|
|
||||||
return Result.Failure(ex.Message);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
/// <summary>
|
|
||||||
/// Read metadata from a .gmeta file.
|
|
||||||
/// </summary>
|
|
||||||
private static async ValueTask<Result<AssetMeta>> ReadMetaFileAsync(string assetPath, CancellationToken token = default)
|
|
||||||
{
|
|
||||||
var metaFileResult = GetMetaFilePath(assetPath);
|
|
||||||
if (metaFileResult.IsFailure)
|
|
||||||
{
|
|
||||||
return Result<AssetMeta>.Failure(metaFileResult.Message);
|
|
||||||
}
|
|
||||||
|
|
||||||
if (!File.Exists(metaFileResult.Value))
|
|
||||||
{
|
|
||||||
return Result<AssetMeta>.Failure("Metadata file does not exist");
|
|
||||||
}
|
|
||||||
|
|
||||||
try
|
|
||||||
{
|
|
||||||
await using var fileStream = File.OpenRead(metaFileResult.Value);
|
|
||||||
var meta = await JsonSerializer.DeserializeAsync<AssetMeta>(fileStream, s_defaultJsonOptions, token);
|
|
||||||
if (meta == null)
|
|
||||||
{
|
|
||||||
return Result<AssetMeta>.Failure("Failed to deserialize metadata");
|
|
||||||
}
|
|
||||||
|
|
||||||
return meta;
|
|
||||||
}
|
|
||||||
catch (Exception ex)
|
|
||||||
{
|
|
||||||
return Result<AssetMeta>.Failure($"Failed to read metadata: {ex.Message}");
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
internal static async ValueTask<Result> GenerateMetaFileAsync(string assetPath, CancellationToken token = default)
|
|
||||||
{
|
|
||||||
Result r;
|
|
||||||
|
|
||||||
var metaFileResult = GetMetaFilePath(assetPath);
|
|
||||||
if (metaFileResult.IsFailure)
|
|
||||||
{
|
|
||||||
return Result.Failure(metaFileResult.Message);
|
|
||||||
}
|
|
||||||
|
|
||||||
if (File.Exists(metaFileResult.Value))
|
|
||||||
{
|
|
||||||
var existingMetaResult = await ReadMetaFileAsync(assetPath, token);
|
|
||||||
if (existingMetaResult.IsSuccess)
|
|
||||||
{
|
|
||||||
var existingMeta = existingMetaResult.Value;
|
|
||||||
if (s_assetPathLookup.TryGetValue(existingMeta.Guid, out var path))
|
|
||||||
{
|
|
||||||
var relResult = GetRelativePath(assetPath);
|
|
||||||
if (relResult.IsSuccess && assetPath != path)
|
|
||||||
{
|
|
||||||
// GUID conflict - regenerate
|
|
||||||
existingMeta.Guid = Guid.NewGuid();
|
|
||||||
r = await WriteMetaFileAsync(metaFileResult.Value, existingMeta, token);
|
|
||||||
if (r.IsFailure)
|
|
||||||
{
|
|
||||||
return r;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
// Calculate file hash and update database
|
|
||||||
var fileHash = await CalculateFileHashAsync(assetPath, token);
|
|
||||||
await UpsertAssetAsync(assetPath, existingMeta, fileHash, null, token);
|
|
||||||
return Result.Success();
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
// Calculate initial file hash
|
|
||||||
var fileHash2 = await CalculateFileHashAsync(assetPath, token);
|
|
||||||
|
|
||||||
var defaultSettings = GetDefaultSettingsForAsset(assetPath);
|
|
||||||
var metaData = new AssetMeta
|
|
||||||
{
|
|
||||||
Guid = Guid.NewGuid()
|
|
||||||
};
|
|
||||||
|
|
||||||
if (defaultSettings != null)
|
|
||||||
{
|
|
||||||
metaData.SetImporterSettings(defaultSettings.GetType().Name, defaultSettings);
|
|
||||||
}
|
|
||||||
|
|
||||||
r = await WriteMetaFileAsync(metaFileResult.Value, metaData, token);
|
|
||||||
if (r.IsFailure)
|
|
||||||
{
|
|
||||||
return r;
|
|
||||||
}
|
|
||||||
|
|
||||||
// Add to database
|
|
||||||
await UpsertAssetAsync(assetPath, metaData, fileHash2, null, token);
|
|
||||||
|
|
||||||
return r;
|
|
||||||
}
|
|
||||||
|
|
||||||
[MethodImpl(MethodImplOptions.AggressiveInlining)]
|
|
||||||
private static bool IsMetaFile(string path)
|
|
||||||
{
|
|
||||||
return Path.GetExtension(path).Equals(FileExtensions.META_FILE_EXTENSION, StringComparison.OrdinalIgnoreCase);
|
|
||||||
}
|
|
||||||
|
|
||||||
private static async void OnFSEvent(object sender, FileSystemEventArgs e)
|
|
||||||
{
|
|
||||||
if (IsMetaFile(e.FullPath))
|
|
||||||
{
|
|
||||||
return;
|
|
||||||
}
|
|
||||||
|
|
||||||
var type = e.ChangeType switch
|
|
||||||
{
|
|
||||||
WatcherChangeTypes.Created => AssetCommandType.FileCreated,
|
|
||||||
WatcherChangeTypes.Deleted => AssetCommandType.FileDeleted,
|
|
||||||
WatcherChangeTypes.Changed => AssetCommandType.FileModified,
|
|
||||||
_ => throw new InvalidOperationException("Unsupported file system event type")
|
|
||||||
};
|
|
||||||
|
|
||||||
await PostCommandAsync(new AssetCommand(type, e.FullPath, Timestamp: DateTime.UtcNow));
|
|
||||||
}
|
|
||||||
|
|
||||||
private static async void OnAssetRenamed(object sender, RenamedEventArgs e)
|
|
||||||
{
|
|
||||||
if (IsMetaFile(e.FullPath))
|
|
||||||
{
|
|
||||||
return;
|
|
||||||
}
|
|
||||||
|
|
||||||
await PostCommandAsync(new AssetCommand(AssetCommandType.FileRenamed, e.FullPath, e.OldFullPath, DateTime.UtcNow));
|
|
||||||
}
|
|
||||||
|
|
||||||
/// <summary>
|
|
||||||
/// Mark all assets that depend on the specified asset as dirty.
|
|
||||||
/// </summary>
|
|
||||||
private static async Task MarkDependentAssetsDirtyAsync(Guid assetGuid)
|
|
||||||
{
|
|
||||||
// TODO: We should have a reverse dependency lookup in the database to avoid scanning all assets.
|
|
||||||
|
|
||||||
// Query database for all assets and check their dependencies
|
|
||||||
var allAssets = GetAllAssets();
|
|
||||||
|
|
||||||
foreach (var kvp in allAssets)
|
|
||||||
{
|
|
||||||
var dependencies = await GetDependenciesAsync(kvp.Key, CancellationToken.None);
|
|
||||||
if (dependencies.Contains(assetGuid))
|
|
||||||
{
|
|
||||||
MarkDirty(kvp.Key);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
@@ -1,51 +0,0 @@
|
|||||||
using Ghost.Core;
|
|
||||||
using Ghost.Editor.Core.Utilities;
|
|
||||||
using System.Diagnostics;
|
|
||||||
using System.Reflection;
|
|
||||||
|
|
||||||
namespace Ghost.Editor.Core.AssetHandle;
|
|
||||||
|
|
||||||
public static partial class AssetDatabase
|
|
||||||
{
|
|
||||||
private static readonly Dictionary<string, Action<string>> s_assetOpenHandlers = new(StringComparer.OrdinalIgnoreCase);
|
|
||||||
|
|
||||||
private static void InitializeAssetHandle()
|
|
||||||
{
|
|
||||||
var methods = TypeCache.GetTypes()
|
|
||||||
.SelectMany(t => t.GetMethods(BindingFlags.Static | BindingFlags.Public | BindingFlags.NonPublic))
|
|
||||||
.Where(m => m.GetCustomAttribute<AssetOpenHandlerAttribute>() != null &&
|
|
||||||
m.GetParameters().Length == 1 &&
|
|
||||||
m.GetParameters()[0].ParameterType == typeof(string));
|
|
||||||
|
|
||||||
foreach (var method in methods)
|
|
||||||
{
|
|
||||||
var attr = method.GetCustomAttribute<AssetOpenHandlerAttribute>()!;
|
|
||||||
var del = (Action<string>)Delegate.CreateDelegate(typeof(Action<string>), method);
|
|
||||||
foreach (var ext in attr.Extensions)
|
|
||||||
{
|
|
||||||
if (s_assetOpenHandlers.ContainsKey(ext))
|
|
||||||
{
|
|
||||||
Logger.LogError($"Duplicate asset open handler for extension '{ext}' found in method '{method.Name}'. Existing handler will be overwritten.");
|
|
||||||
}
|
|
||||||
|
|
||||||
s_assetOpenHandlers[ext] = del;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
public static void OpenAsset(string path)
|
|
||||||
{
|
|
||||||
var extension = Path.GetExtension(path);
|
|
||||||
if (s_assetOpenHandlers.TryGetValue(extension, out var handler))
|
|
||||||
{
|
|
||||||
handler(path);
|
|
||||||
}
|
|
||||||
else
|
|
||||||
{
|
|
||||||
Process.Start(new ProcessStartInfo(path)
|
|
||||||
{
|
|
||||||
UseShellExecute = true
|
|
||||||
});
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
@@ -1,390 +0,0 @@
|
|||||||
using Ghost.Core;
|
|
||||||
using Ghost.Data.Services;
|
|
||||||
using Microsoft.Data.Sqlite;
|
|
||||||
using System.Text.Json;
|
|
||||||
|
|
||||||
namespace Ghost.Editor.Core.AssetHandle;
|
|
||||||
|
|
||||||
public static partial class AssetDatabase
|
|
||||||
{
|
|
||||||
private static SqliteConnection? s_dbConnection;
|
|
||||||
|
|
||||||
/// <summary>
|
|
||||||
/// Initialize the SQLite database for asset caching.
|
|
||||||
/// </summary>
|
|
||||||
private static async Task InitializeDatabaseAsync(CancellationToken token = default)
|
|
||||||
{
|
|
||||||
if (AssetsDirectory == null)
|
|
||||||
{
|
|
||||||
throw new InvalidOperationException("AssetsDirectory is not set. Initialize() must be called first.");
|
|
||||||
}
|
|
||||||
|
|
||||||
var dbPath = Path.Combine(AssetsDirectory.Parent!.FullName, ProjectService.CACHE_FOLDER, "AssetDatabase.db");
|
|
||||||
var cacheDir = Path.GetDirectoryName(dbPath);
|
|
||||||
if (!Directory.Exists(cacheDir))
|
|
||||||
{
|
|
||||||
Directory.CreateDirectory(cacheDir!);
|
|
||||||
}
|
|
||||||
|
|
||||||
var connectionString = new SqliteConnectionStringBuilder
|
|
||||||
{
|
|
||||||
DataSource = dbPath,
|
|
||||||
Mode = SqliteOpenMode.ReadWriteCreate,
|
|
||||||
Cache = SqliteCacheMode.Shared
|
|
||||||
}.ToString();
|
|
||||||
|
|
||||||
s_dbConnection = new SqliteConnection(connectionString);
|
|
||||||
await s_dbConnection.OpenAsync(token);
|
|
||||||
|
|
||||||
// Create tables
|
|
||||||
await using var cmd = s_dbConnection.CreateCommand();
|
|
||||||
cmd.CommandText = @"
|
|
||||||
CREATE TABLE IF NOT EXISTS Assets (
|
|
||||||
Guid TEXT PRIMARY KEY,
|
|
||||||
Path TEXT NOT NULL,
|
|
||||||
Version INTEGER NOT NULL,
|
|
||||||
Tags TEXT,
|
|
||||||
FileHash TEXT,
|
|
||||||
DependencyGuids TEXT,
|
|
||||||
LastModified INTEGER NOT NULL
|
|
||||||
);
|
|
||||||
CREATE INDEX IF NOT EXISTS idx_path ON Assets(Path);
|
|
||||||
";
|
|
||||||
|
|
||||||
await cmd.ExecuteNonQueryAsync(token);
|
|
||||||
}
|
|
||||||
|
|
||||||
/// <summary>
|
|
||||||
/// Add or update an asset in the database.
|
|
||||||
/// </summary>
|
|
||||||
/// <param name="assetPath">Full path to the asset file.</param>
|
|
||||||
/// <param name="meta">Asset metadata from .gmeta file.</param>
|
|
||||||
/// <param name="fileHash">SHA256 hash of the asset file content.</param>
|
|
||||||
/// <param name="dependencies">List of GUIDs this asset depends on (extracted during import).</param>
|
|
||||||
private static async ValueTask<Result> UpsertAssetAsync(string assetPath, AssetMeta meta, string fileHash, List<Guid>? dependencies = null, CancellationToken token = default)
|
|
||||||
{
|
|
||||||
if (s_dbConnection == null)
|
|
||||||
{
|
|
||||||
return Result.Failure("Database not initialized");
|
|
||||||
}
|
|
||||||
|
|
||||||
var relativePath = GetRelativePath(assetPath);
|
|
||||||
if (relativePath.IsFailure)
|
|
||||||
{
|
|
||||||
return Result.Failure(relativePath.Message);
|
|
||||||
}
|
|
||||||
|
|
||||||
try
|
|
||||||
{
|
|
||||||
lock (s_dbLock)
|
|
||||||
{
|
|
||||||
// If this GUID already exists with a different path, remove the old path mapping
|
|
||||||
if (s_assetPathLookup.TryGetValue(meta.Guid, out var oldPath) && oldPath != relativePath.Value)
|
|
||||||
{
|
|
||||||
s_pathAssetLookup.Remove(oldPath);
|
|
||||||
}
|
|
||||||
|
|
||||||
// Update lookups with new path (normalize path separators for consistency)
|
|
||||||
var normalizedPath = relativePath.Value.Replace('\\', '/');
|
|
||||||
s_assetPathLookup[meta.Guid] = normalizedPath;
|
|
||||||
s_pathAssetLookup[normalizedPath] = meta.Guid;
|
|
||||||
}
|
|
||||||
|
|
||||||
await using var cmd = s_dbConnection.CreateCommand();
|
|
||||||
cmd.CommandText = @"
|
|
||||||
INSERT OR REPLACE INTO Assets (Guid, Path, Version, Tags, FileHash, DependencyGuids, LastModified)
|
|
||||||
VALUES (@guid, @path, @version, @tags, @fileHash, @deps, @modified)
|
|
||||||
";
|
|
||||||
cmd.Parameters.AddWithValue("@guid", meta.Guid.ToString());
|
|
||||||
cmd.Parameters.AddWithValue("@path", relativePath.Value);
|
|
||||||
cmd.Parameters.AddWithValue("@version", meta.Version);
|
|
||||||
cmd.Parameters.AddWithValue("@tags", JsonSerializer.Serialize(meta.Tags));
|
|
||||||
cmd.Parameters.AddWithValue("@fileHash", fileHash);
|
|
||||||
cmd.Parameters.AddWithValue("@deps", JsonSerializer.Serialize(dependencies ?? new List<Guid>()));
|
|
||||||
cmd.Parameters.AddWithValue("@modified", DateTimeOffset.UtcNow.ToUnixTimeSeconds());
|
|
||||||
|
|
||||||
await cmd.ExecuteNonQueryAsync(token);
|
|
||||||
return Result.Success();
|
|
||||||
}
|
|
||||||
catch (Exception ex)
|
|
||||||
{
|
|
||||||
return Result.Failure($"Failed to upsert asset: {ex.Message}");
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
/// <summary>
|
|
||||||
/// Remove an asset from the database.
|
|
||||||
/// </summary>
|
|
||||||
private static async Task<Result> RemoveAssetFromDatabaseAsync(Guid guid, CancellationToken token = default)
|
|
||||||
{
|
|
||||||
if (s_dbConnection == null)
|
|
||||||
{
|
|
||||||
return Result.Failure("Database not initialized");
|
|
||||||
}
|
|
||||||
|
|
||||||
try
|
|
||||||
{
|
|
||||||
lock (s_dbLock)
|
|
||||||
{
|
|
||||||
if (s_assetPathLookup.TryGetValue(guid, out var path))
|
|
||||||
{
|
|
||||||
s_assetPathLookup.Remove(guid);
|
|
||||||
s_pathAssetLookup.Remove(path);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
await using var cmd = s_dbConnection.CreateCommand();
|
|
||||||
cmd.CommandText = "DELETE FROM Assets WHERE Guid = @guid";
|
|
||||||
cmd.Parameters.AddWithValue("@guid", guid.ToString());
|
|
||||||
|
|
||||||
await cmd.ExecuteNonQueryAsync(token);
|
|
||||||
return Result.Success();
|
|
||||||
}
|
|
||||||
catch (Exception ex)
|
|
||||||
{
|
|
||||||
return Result.Failure($"Failed to remove asset: {ex.Message}");
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
/// <summary>
|
|
||||||
/// Load all assets from the database into memory cache.
|
|
||||||
/// </summary>
|
|
||||||
private static async Task LoadAssetCacheFromDatabaseAsync(CancellationToken token = default)
|
|
||||||
{
|
|
||||||
if (s_dbConnection == null)
|
|
||||||
{
|
|
||||||
return;
|
|
||||||
}
|
|
||||||
|
|
||||||
try
|
|
||||||
{
|
|
||||||
await using var cmd = s_dbConnection.CreateCommand();
|
|
||||||
cmd.CommandText = "SELECT Guid, Path FROM Assets";
|
|
||||||
|
|
||||||
await using var reader = await cmd.ExecuteReaderAsync(token);
|
|
||||||
while (await reader.ReadAsync(token))
|
|
||||||
{
|
|
||||||
var guidStr = reader.GetString(0);
|
|
||||||
var path = reader.GetString(1);
|
|
||||||
|
|
||||||
if (Guid.TryParse(guidStr, out var guid))
|
|
||||||
{
|
|
||||||
lock (s_dbLock)
|
|
||||||
{
|
|
||||||
s_assetPathLookup[guid] = path;
|
|
||||||
s_pathAssetLookup[path] = guid;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
catch (Exception ex)
|
|
||||||
{
|
|
||||||
Logger.LogError($"Failed to load asset cache: {ex.Message}");
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
/// <summary>
|
|
||||||
/// Get assets by tag.
|
|
||||||
/// </summary>
|
|
||||||
private static async Task<List<Guid>> GetAssetsByTagAsync(string tag, CancellationToken token = default)
|
|
||||||
{
|
|
||||||
var result = new List<Guid>();
|
|
||||||
|
|
||||||
if (s_dbConnection == null)
|
|
||||||
{
|
|
||||||
return result;
|
|
||||||
}
|
|
||||||
|
|
||||||
try
|
|
||||||
{
|
|
||||||
await using var cmd = s_dbConnection.CreateCommand();
|
|
||||||
cmd.CommandText = "SELECT Guid, Tags FROM Assets";
|
|
||||||
|
|
||||||
await using var reader = await cmd.ExecuteReaderAsync(token);
|
|
||||||
while (await reader.ReadAsync(token))
|
|
||||||
{
|
|
||||||
var guidStr = reader.GetString(0);
|
|
||||||
var tagsJson = reader.GetString(1);
|
|
||||||
|
|
||||||
if (Guid.TryParse(guidStr, out var guid))
|
|
||||||
{
|
|
||||||
var tags = JsonSerializer.Deserialize<List<string>>(tagsJson);
|
|
||||||
if (tags != null && tags.Contains(tag, StringComparer.OrdinalIgnoreCase))
|
|
||||||
{
|
|
||||||
result.Add(guid);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
catch
|
|
||||||
{
|
|
||||||
// Silently fail
|
|
||||||
}
|
|
||||||
|
|
||||||
return result;
|
|
||||||
}
|
|
||||||
|
|
||||||
/// <summary>
|
|
||||||
/// Get the file hash for an asset from the database.
|
|
||||||
/// </summary>
|
|
||||||
private static async Task<string?> GetFileHashAsync(Guid guid, CancellationToken token = default)
|
|
||||||
{
|
|
||||||
if (s_dbConnection == null)
|
|
||||||
{
|
|
||||||
return null;
|
|
||||||
}
|
|
||||||
|
|
||||||
try
|
|
||||||
{
|
|
||||||
await using var cmd = s_dbConnection.CreateCommand();
|
|
||||||
cmd.CommandText = "SELECT FileHash FROM Assets WHERE Guid = @guid";
|
|
||||||
cmd.Parameters.AddWithValue("@guid", guid.ToString());
|
|
||||||
|
|
||||||
var result = await cmd.ExecuteScalarAsync(token);
|
|
||||||
return result?.ToString();
|
|
||||||
}
|
|
||||||
catch
|
|
||||||
{
|
|
||||||
return null;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
/// <summary>
|
|
||||||
/// Get the dependencies for an asset from the database.
|
|
||||||
/// </summary>
|
|
||||||
private static async Task<List<Guid>> GetDependenciesAsync(Guid guid, CancellationToken token = default)
|
|
||||||
{
|
|
||||||
if (s_dbConnection == null)
|
|
||||||
{
|
|
||||||
return new List<Guid>();
|
|
||||||
}
|
|
||||||
|
|
||||||
try
|
|
||||||
{
|
|
||||||
await using var cmd = s_dbConnection.CreateCommand();
|
|
||||||
cmd.CommandText = "SELECT DependencyGuids FROM Assets WHERE Guid = @guid";
|
|
||||||
cmd.Parameters.AddWithValue("@guid", guid.ToString());
|
|
||||||
|
|
||||||
var result = await cmd.ExecuteScalarAsync(token);
|
|
||||||
if (result != null)
|
|
||||||
{
|
|
||||||
var json = result.ToString();
|
|
||||||
return JsonSerializer.Deserialize<List<Guid>>(json ?? "[]") ?? new List<Guid>();
|
|
||||||
}
|
|
||||||
}
|
|
||||||
catch
|
|
||||||
{
|
|
||||||
// Silently fail
|
|
||||||
}
|
|
||||||
|
|
||||||
return new List<Guid>();
|
|
||||||
}
|
|
||||||
|
|
||||||
/// <summary>
|
|
||||||
/// Find assets by name pattern using database query with wildcards.
|
|
||||||
/// </summary>
|
|
||||||
/// <param name="namePattern">Pattern supporting * (any chars) and ? (single char).</param>
|
|
||||||
private static async Task<List<Guid>> GetAssetsByNameAsync(string namePattern, CancellationToken token = default)
|
|
||||||
{
|
|
||||||
var results = new List<Guid>();
|
|
||||||
|
|
||||||
if (s_dbConnection == null)
|
|
||||||
{
|
|
||||||
return results;
|
|
||||||
}
|
|
||||||
|
|
||||||
try
|
|
||||||
{
|
|
||||||
// Convert wildcard pattern to SQL LIKE pattern
|
|
||||||
var sqlPattern = namePattern.Replace('*', '%').Replace('?', '_');
|
|
||||||
|
|
||||||
await using var cmd = s_dbConnection.CreateCommand();
|
|
||||||
|
|
||||||
// Extract just the filename from the path for matching
|
|
||||||
// SQLite doesn't have a built-in path manipulation, so we search in the full path
|
|
||||||
// and filter by checking if the pattern matches the filename part
|
|
||||||
cmd.CommandText = @"
|
|
||||||
SELECT Guid, Path FROM Assets
|
|
||||||
WHERE Path LIKE '%' || @pattern || '%'
|
|
||||||
";
|
|
||||||
cmd.Parameters.AddWithValue("@pattern", sqlPattern);
|
|
||||||
|
|
||||||
await using var reader = await cmd.ExecuteReaderAsync(token);
|
|
||||||
while (await reader.ReadAsync(token))
|
|
||||||
{
|
|
||||||
var guidStr = reader.GetString(0);
|
|
||||||
var path = reader.GetString(1);
|
|
||||||
|
|
||||||
// Extract filename and check if it matches the pattern
|
|
||||||
var fileName = Path.GetFileName(path);
|
|
||||||
|
|
||||||
// Convert pattern to regex for proper matching
|
|
||||||
var regexPattern = "^" + System.Text.RegularExpressions.Regex.Escape(namePattern)
|
|
||||||
.Replace("\\*", ".*")
|
|
||||||
.Replace("\\?", ".") + "$";
|
|
||||||
|
|
||||||
if (System.Text.RegularExpressions.Regex.IsMatch(fileName, regexPattern, System.Text.RegularExpressions.RegexOptions.IgnoreCase))
|
|
||||||
{
|
|
||||||
if (Guid.TryParse(guidStr, out var guid))
|
|
||||||
{
|
|
||||||
results.Add(guid);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
catch
|
|
||||||
{
|
|
||||||
// Silently fail
|
|
||||||
}
|
|
||||||
|
|
||||||
return results;
|
|
||||||
}
|
|
||||||
|
|
||||||
/// <summary>
|
|
||||||
/// Remove orphaned entries from database (assets that no longer exist on disk).
|
|
||||||
/// </summary>
|
|
||||||
private static async Task RemoveOrphanedEntriesAsync(CancellationToken token = default)
|
|
||||||
{
|
|
||||||
if (s_dbConnection == null || AssetsDirectory == null)
|
|
||||||
{
|
|
||||||
return;
|
|
||||||
}
|
|
||||||
|
|
||||||
try
|
|
||||||
{
|
|
||||||
var orphanedGuids = new List<Guid>();
|
|
||||||
|
|
||||||
await using var cmd = s_dbConnection.CreateCommand();
|
|
||||||
cmd.CommandText = "SELECT Guid, Path FROM Assets";
|
|
||||||
|
|
||||||
await using var reader = await cmd.ExecuteReaderAsync(token);
|
|
||||||
while (await reader.ReadAsync(token))
|
|
||||||
{
|
|
||||||
var guidStr = reader.GetString(0);
|
|
||||||
var path = reader.GetString(1);
|
|
||||||
|
|
||||||
if (Guid.TryParse(guidStr, out var guid))
|
|
||||||
{
|
|
||||||
// Check if file exists
|
|
||||||
var fullPath = Path.Combine(AssetsDirectory.FullName, path);
|
|
||||||
if (!File.Exists(fullPath))
|
|
||||||
{
|
|
||||||
orphanedGuids.Add(guid);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
// Remove orphaned entries
|
|
||||||
foreach (var guid in orphanedGuids)
|
|
||||||
{
|
|
||||||
await RemoveAssetFromDatabaseAsync(guid, token);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
catch
|
|
||||||
{
|
|
||||||
// Silently fail - cleanup is best effort
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
@@ -1,531 +0,0 @@
|
|||||||
using Ghost.Core;
|
|
||||||
using Ghost.Data.Services;
|
|
||||||
using System.Collections.Concurrent;
|
|
||||||
using System.Text.Json;
|
|
||||||
using System.Text.Json.Serialization;
|
|
||||||
using System.Threading.Channels;
|
|
||||||
|
|
||||||
namespace Ghost.Editor.Core.AssetHandle;
|
|
||||||
|
|
||||||
/// <summary>
|
|
||||||
/// Command types for asset database operations.
|
|
||||||
/// </summary>
|
|
||||||
internal enum AssetCommandType
|
|
||||||
{
|
|
||||||
FileCreated,
|
|
||||||
FileModified,
|
|
||||||
FileDeleted,
|
|
||||||
FileRenamed,
|
|
||||||
ManualRefresh
|
|
||||||
}
|
|
||||||
|
|
||||||
/// <summary>
|
|
||||||
/// Represents a command to process an asset operation.
|
|
||||||
/// </summary>
|
|
||||||
internal readonly record struct AssetCommand(
|
|
||||||
AssetCommandType Type,
|
|
||||||
string Path,
|
|
||||||
string? OldPath = null,
|
|
||||||
DateTime Timestamp = default
|
|
||||||
);
|
|
||||||
|
|
||||||
/// <summary>
|
|
||||||
/// Centralized asset database that manages all assets in the project.
|
|
||||||
/// Handles asset registration, lookup, importing, and dependency management.
|
|
||||||
/// Uses SQLite for persistent storage and efficient querying.
|
|
||||||
/// </summary>
|
|
||||||
public static partial class AssetDatabase
|
|
||||||
{
|
|
||||||
private static FileSystemWatcher? s_watcher;
|
|
||||||
private static readonly Lock s_dbLock = new();
|
|
||||||
private static readonly Dictionary<Guid, string> s_assetPathLookup = new();
|
|
||||||
private static readonly Dictionary<string, Guid> s_pathAssetLookup = new();
|
|
||||||
|
|
||||||
// In-memory dirty asset tracking (for runtime modifications only)
|
|
||||||
// TODO: We do not handle the reimporting of dirty assets yet
|
|
||||||
private static readonly HashSet<Guid> s_dirtyAssets = new();
|
|
||||||
|
|
||||||
// Command buffer pattern - Channel for file system event commands
|
|
||||||
private static Channel<AssetCommand>? s_commandChannel;
|
|
||||||
private static Timer? s_commandProcessorTimer;
|
|
||||||
private static readonly Lock s_commandLock = new();
|
|
||||||
private static readonly ConcurrentQueue<AssetCommand> s_waitingCommands = new(); // Commands waiting for manual refresh
|
|
||||||
private static bool s_autoRefreshEnabled = true;
|
|
||||||
|
|
||||||
// Initialization guard
|
|
||||||
private static readonly Lock s_initializationLock = new();
|
|
||||||
private static bool s_initialized = false;
|
|
||||||
|
|
||||||
private static readonly TimeSpan s_debounceDelay = TimeSpan.FromMilliseconds(100);
|
|
||||||
private static ManualResetEventSlim s_resetEventSlim = new(false);
|
|
||||||
|
|
||||||
private static readonly JsonSerializerOptions s_defaultJsonOptions = new()
|
|
||||||
{
|
|
||||||
WriteIndented = true,
|
|
||||||
PropertyNamingPolicy = JsonNamingPolicy.CamelCase,
|
|
||||||
Converters =
|
|
||||||
{
|
|
||||||
new JsonStringEnumConverter(JsonNamingPolicy.CamelCase)
|
|
||||||
}
|
|
||||||
};
|
|
||||||
|
|
||||||
public static DirectoryInfo? AssetsDirectory
|
|
||||||
{
|
|
||||||
get;
|
|
||||||
private set;
|
|
||||||
}
|
|
||||||
|
|
||||||
/// <summary>
|
|
||||||
/// Initialize the asset database.
|
|
||||||
/// Must be called after project is loaded.
|
|
||||||
/// </summary>
|
|
||||||
|
|
||||||
internal static async Task Initialize(CancellationToken token = default)
|
|
||||||
{
|
|
||||||
lock (s_initializationLock)
|
|
||||||
{
|
|
||||||
if (s_initialized)
|
|
||||||
{
|
|
||||||
return;
|
|
||||||
}
|
|
||||||
|
|
||||||
s_initialized = true;
|
|
||||||
}
|
|
||||||
|
|
||||||
if (ProjectService.CurrentProject.Metadata == null)
|
|
||||||
{
|
|
||||||
throw new InvalidOperationException("Project metadata is not initialized. Ensure that the project is loaded before accessing the AssetDatabase.");
|
|
||||||
}
|
|
||||||
|
|
||||||
AssetsDirectory = new DirectoryInfo(Path.Combine(Path.GetDirectoryName(ProjectService.CurrentProject.Path)!, ProjectService.ASSETS_FOLDER));
|
|
||||||
|
|
||||||
s_commandChannel = Channel.CreateUnbounded<AssetCommand>(new UnboundedChannelOptions
|
|
||||||
{
|
|
||||||
SingleReader = false,
|
|
||||||
SingleWriter = false
|
|
||||||
});
|
|
||||||
|
|
||||||
// Initialize command processor timer (starts disabled, triggered by events)
|
|
||||||
s_commandProcessorTimer = new Timer(ProcessPendingCommands, null, Timeout.Infinite, Timeout.Infinite);
|
|
||||||
|
|
||||||
await InitializeDatabaseAsync(token);
|
|
||||||
await LoadAssetCacheFromDatabaseAsync(token);
|
|
||||||
|
|
||||||
s_watcher = new FileSystemWatcher
|
|
||||||
{
|
|
||||||
Path = AssetsDirectory.FullName,
|
|
||||||
IncludeSubdirectories = true,
|
|
||||||
EnableRaisingEvents = true,
|
|
||||||
NotifyFilter = NotifyFilters.FileName | NotifyFilters.DirectoryName | NotifyFilters.LastWrite
|
|
||||||
};
|
|
||||||
|
|
||||||
InitializeAssetHandle();
|
|
||||||
InitializeMetaData();
|
|
||||||
|
|
||||||
// TODO: Timestamp fake instead of full scan.
|
|
||||||
await ValidateAndFixDatabaseAsync(token);
|
|
||||||
}
|
|
||||||
|
|
||||||
/// <summary>
|
|
||||||
/// Validate the asset database and fix any inconsistencies.
|
|
||||||
/// Checks for missing/corrupted assets and regenerates metadata as needed.
|
|
||||||
/// </summary>
|
|
||||||
private static async Task<Result> ValidateAndFixDatabaseAsync(CancellationToken token = default)
|
|
||||||
{
|
|
||||||
if (AssetsDirectory == null)
|
|
||||||
{
|
|
||||||
return Result.Failure("AssetsDirectory not initialized");
|
|
||||||
}
|
|
||||||
|
|
||||||
try
|
|
||||||
{
|
|
||||||
// Scan all files in assets directory
|
|
||||||
var allFiles = Directory.EnumerateFiles(AssetsDirectory.FullName, "*.*", SearchOption.AllDirectories)
|
|
||||||
.Where(f => !f.EndsWith(Utilities.FileExtensions.META_FILE_EXTENSION, StringComparison.OrdinalIgnoreCase));
|
|
||||||
|
|
||||||
// Ensure all files have metadata
|
|
||||||
foreach (var file in allFiles)
|
|
||||||
{
|
|
||||||
var metaPath = file + Utilities.FileExtensions.META_FILE_EXTENSION;
|
|
||||||
if (!File.Exists(metaPath))
|
|
||||||
{
|
|
||||||
await GenerateMetaFileAsync(file, token);
|
|
||||||
}
|
|
||||||
else
|
|
||||||
{
|
|
||||||
// Validate and update database
|
|
||||||
var metaResult = await ReadMetaFileAsync(file, token);
|
|
||||||
if (metaResult.IsSuccess)
|
|
||||||
{
|
|
||||||
var fileHash = await CalculateFileHashAsync(file, token);
|
|
||||||
await UpsertAssetAsync(file, metaResult.Value, fileHash, null, token);
|
|
||||||
}
|
|
||||||
else
|
|
||||||
{
|
|
||||||
// Corrupted meta file - regenerate
|
|
||||||
await GenerateMetaFileAsync(file, token);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
// Remove orphaned entries from database (files that no longer exist)
|
|
||||||
await RemoveOrphanedEntriesAsync(token);
|
|
||||||
|
|
||||||
return Result.Success();
|
|
||||||
}
|
|
||||||
catch (Exception ex)
|
|
||||||
{
|
|
||||||
return Result.Failure($"Failed to validate database: {ex.Message}");
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
/// <summary>
|
|
||||||
/// Refresh the asset database manually.
|
|
||||||
/// Scans the project directory for changes and processes any queued file system events.
|
|
||||||
/// </summary>
|
|
||||||
public static async Task<Result> RefreshAsync(CancellationToken token = default)
|
|
||||||
{
|
|
||||||
// Flush waiting commands to channel
|
|
||||||
while (s_waitingCommands.TryDequeue(out var cmd))
|
|
||||||
{
|
|
||||||
s_commandChannel?.Writer.TryWrite(cmd);
|
|
||||||
}
|
|
||||||
|
|
||||||
s_resetEventSlim.Reset();
|
|
||||||
s_commandChannel?.Writer.TryWrite(new AssetCommand(AssetCommandType.ManualRefresh, string.Empty));
|
|
||||||
s_commandProcessorTimer?.Change(TimeSpan.Zero, Timeout.InfiniteTimeSpan);
|
|
||||||
|
|
||||||
await Task.Run(s_resetEventSlim.Wait, token);
|
|
||||||
return Result.Success();
|
|
||||||
}
|
|
||||||
|
|
||||||
/// <summary>
|
|
||||||
/// Mark an asset as dirty (modified in memory but not yet saved).
|
|
||||||
/// This state is NOT persisted and will be lost on application restart.
|
|
||||||
/// </summary>
|
|
||||||
public static void MarkDirty(Guid assetGuid)
|
|
||||||
{
|
|
||||||
lock (s_dbLock)
|
|
||||||
{
|
|
||||||
s_dirtyAssets.Add(assetGuid);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
/// <summary>
|
|
||||||
/// Check if an asset is marked as dirty.
|
|
||||||
/// </summary>
|
|
||||||
public static bool IsDirty(Guid assetGuid)
|
|
||||||
{
|
|
||||||
lock (s_dbLock)
|
|
||||||
{
|
|
||||||
return s_dirtyAssets.Contains(assetGuid);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
/// <summary>
|
|
||||||
/// Get all dirty assets.
|
|
||||||
/// </summary>
|
|
||||||
public static Guid[] GetDirtyAssets()
|
|
||||||
{
|
|
||||||
lock (s_dbLock)
|
|
||||||
{
|
|
||||||
return s_dirtyAssets.ToArray();
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
/// <summary>
|
|
||||||
/// Clear dirty flag for an asset (typically after saving).
|
|
||||||
/// </summary>
|
|
||||||
public static void ClearDirty(Guid assetGuid)
|
|
||||||
{
|
|
||||||
lock (s_dbLock)
|
|
||||||
{
|
|
||||||
s_dirtyAssets.Remove(assetGuid);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
/// <summary>
|
|
||||||
/// Clear all dirty flags.
|
|
||||||
/// </summary>
|
|
||||||
public static void ClearAllDirty()
|
|
||||||
{
|
|
||||||
lock (s_dbLock)
|
|
||||||
{
|
|
||||||
s_dirtyAssets.Clear();
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
/// <summary>
|
|
||||||
/// Enable or disable automatic asset database refresh.
|
|
||||||
/// When disabled, file system events are queued and processed only when RefreshAsync() is called.
|
|
||||||
/// </summary>
|
|
||||||
public static void SetAutoRefresh(bool enabled)
|
|
||||||
{
|
|
||||||
s_autoRefreshEnabled = enabled;
|
|
||||||
}
|
|
||||||
|
|
||||||
internal static void FlushPendingCommands()
|
|
||||||
{
|
|
||||||
// Stop timer temporarily
|
|
||||||
s_commandProcessorTimer?.Change(Timeout.Infinite, Timeout.Infinite);
|
|
||||||
|
|
||||||
// Give a tiny bit of time for any in-flight file watcher events to post to channel
|
|
||||||
Thread.Sleep(50);
|
|
||||||
|
|
||||||
// Process all commands now
|
|
||||||
ProcessPendingCommands(null);
|
|
||||||
}
|
|
||||||
|
|
||||||
private static async ValueTask PostCommandAsync(AssetCommand command, CancellationToken token = default)
|
|
||||||
{
|
|
||||||
if (s_commandChannel == null)
|
|
||||||
{
|
|
||||||
return;
|
|
||||||
}
|
|
||||||
|
|
||||||
if (s_autoRefreshEnabled)
|
|
||||||
{
|
|
||||||
await s_commandChannel.Writer.WriteAsync(command, token);
|
|
||||||
s_commandProcessorTimer?.Change(s_debounceDelay, Timeout.InfiniteTimeSpan);
|
|
||||||
}
|
|
||||||
else
|
|
||||||
{
|
|
||||||
s_waitingCommands.Enqueue(command);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
private static async void ProcessPendingCommands(object? state)
|
|
||||||
{
|
|
||||||
if (s_commandChannel == null)
|
|
||||||
{
|
|
||||||
return;
|
|
||||||
}
|
|
||||||
|
|
||||||
try
|
|
||||||
{
|
|
||||||
// // Collect all pending commands
|
|
||||||
// var commands = new List<AssetCommand>();
|
|
||||||
//
|
|
||||||
// while (s_commandChannel.Reader.TryRead(out var cmd))
|
|
||||||
// {
|
|
||||||
// commands.Add(cmd);
|
|
||||||
// }
|
|
||||||
|
|
||||||
// // Group commands by path (last command wins)
|
|
||||||
// var commandsByPath = new Dictionary<string, AssetCommand>();
|
|
||||||
// foreach (var cmd in commands)
|
|
||||||
// {
|
|
||||||
// commandsByPath[cmd.Path] = cmd;
|
|
||||||
// }
|
|
||||||
|
|
||||||
// NOTE: We handle the temp file filtering in each command handler now
|
|
||||||
// We should able to remove this allocation heavy code
|
|
||||||
|
|
||||||
// Filter out temp files (files that were created then deleted)
|
|
||||||
// lock (s_commandLock)
|
|
||||||
// {
|
|
||||||
// var pathsToProcess = commandsByPath.Keys.ToList();
|
|
||||||
// foreach (var path in pathsToProcess)
|
|
||||||
// {
|
|
||||||
// // If file was created/modified but doesn't exist anymore, skip
|
|
||||||
// if (!File.Exists(path) && commandsByPath[path].Type != AssetCommandType.FileDeleted)
|
|
||||||
// {
|
|
||||||
// commandsByPath.Remove(path);
|
|
||||||
// }
|
|
||||||
// }
|
|
||||||
//
|
|
||||||
// // Clear pending paths
|
|
||||||
// s_pendingCommandPaths.Clear();
|
|
||||||
// }
|
|
||||||
|
|
||||||
// Execute commands
|
|
||||||
// NOTE: We many don't need to collect all commands first, just process as we read.
|
|
||||||
// Channel in c# is thread-safe for multiple readers/writers.
|
|
||||||
//await foreach (var cmd in s_commandChannel.Reader.ReadAllAsync())
|
|
||||||
//{
|
|
||||||
// await ExecuteCommandAsync(cmd);
|
|
||||||
//}
|
|
||||||
|
|
||||||
while (s_commandChannel.Reader.TryRead(out var cmd))
|
|
||||||
{
|
|
||||||
await ExecuteCommandAsync(cmd);
|
|
||||||
}
|
|
||||||
|
|
||||||
await ImportDirtyAssetsAsync();
|
|
||||||
}
|
|
||||||
catch (Exception ex)
|
|
||||||
{
|
|
||||||
Logger.LogError($"Error processing commands: {ex.Message}");
|
|
||||||
}
|
|
||||||
finally
|
|
||||||
{
|
|
||||||
s_resetEventSlim.Set();
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
private static async ValueTask ExecuteCommandAsync(AssetCommand command)
|
|
||||||
{
|
|
||||||
switch (command.Type)
|
|
||||||
{
|
|
||||||
case AssetCommandType.FileCreated:
|
|
||||||
await HandleFileCreatedAsync(command.Path);
|
|
||||||
break;
|
|
||||||
|
|
||||||
case AssetCommandType.FileModified:
|
|
||||||
await HandleFileModifiedAsync(command.Path);
|
|
||||||
break;
|
|
||||||
|
|
||||||
case AssetCommandType.FileDeleted:
|
|
||||||
await HandleFileDeletedAsync(command.Path);
|
|
||||||
break;
|
|
||||||
|
|
||||||
case AssetCommandType.FileRenamed:
|
|
||||||
if (command.OldPath != null)
|
|
||||||
{
|
|
||||||
await HandleFileRenamedAsync(command.OldPath, command.Path);
|
|
||||||
}
|
|
||||||
break;
|
|
||||||
|
|
||||||
case AssetCommandType.ManualRefresh:
|
|
||||||
await ValidateAndFixDatabaseAsync(CancellationToken.None);
|
|
||||||
break;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
private static async ValueTask HandleFileCreatedAsync(string path)
|
|
||||||
{
|
|
||||||
if (!File.Exists(path))
|
|
||||||
{
|
|
||||||
return;
|
|
||||||
}
|
|
||||||
|
|
||||||
await GenerateMetaFileAsync(path, CancellationToken.None);
|
|
||||||
}
|
|
||||||
|
|
||||||
private static async ValueTask HandleFileModifiedAsync(string path)
|
|
||||||
{
|
|
||||||
if (!File.Exists(path))
|
|
||||||
{
|
|
||||||
return;
|
|
||||||
}
|
|
||||||
|
|
||||||
// Check if file hash changed
|
|
||||||
var metaResult = await ReadMetaFileAsync(path, CancellationToken.None);
|
|
||||||
if (metaResult.IsFailure)
|
|
||||||
{
|
|
||||||
// No .gmeta file - treat this as a new file creation
|
|
||||||
await HandleFileCreatedAsync(path);
|
|
||||||
return;
|
|
||||||
}
|
|
||||||
|
|
||||||
var newHash = await CalculateFileHashAsync(path, CancellationToken.None);
|
|
||||||
var oldHash = await GetFileHashAsync(metaResult.Value.Guid, CancellationToken.None);
|
|
||||||
|
|
||||||
if (oldHash != newHash)
|
|
||||||
{
|
|
||||||
// File changed - update database and mark as dirty
|
|
||||||
await UpsertAssetAsync(path, metaResult.Value, newHash, null, CancellationToken.None);
|
|
||||||
MarkDirty(metaResult.Value.Guid);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
private static async ValueTask HandleFileDeletedAsync(string path)
|
|
||||||
{
|
|
||||||
var metaFileResult = GetMetaFilePath(path);
|
|
||||||
if (metaFileResult.IsSuccess && File.Exists(metaFileResult.Value))
|
|
||||||
{
|
|
||||||
try
|
|
||||||
{
|
|
||||||
var metaResult = await ReadMetaFileAsync(path, CancellationToken.None);
|
|
||||||
if (metaResult.IsSuccess)
|
|
||||||
{
|
|
||||||
var meta = metaResult.Value;
|
|
||||||
|
|
||||||
// Remove from database
|
|
||||||
await RemoveAssetFromDatabaseAsync(meta.Guid, CancellationToken.None);
|
|
||||||
|
|
||||||
// Mark dependent assets as dirty
|
|
||||||
await MarkDependentAssetsDirtyAsync(meta.Guid);
|
|
||||||
}
|
|
||||||
|
|
||||||
File.Delete(metaFileResult.Value);
|
|
||||||
}
|
|
||||||
catch (Exception ex)
|
|
||||||
{
|
|
||||||
Logger.LogError($"Error deleting asset metadata: {ex.Message}");
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
private static async ValueTask HandleFileRenamedAsync(string oldPath, string newPath)
|
|
||||||
{
|
|
||||||
var oldMetaPath = oldPath + Utilities.FileExtensions.META_FILE_EXTENSION;
|
|
||||||
var newMetaPath = newPath + Utilities.FileExtensions.META_FILE_EXTENSION;
|
|
||||||
|
|
||||||
if (File.Exists(newMetaPath))
|
|
||||||
{
|
|
||||||
// Validate and update
|
|
||||||
await GenerateMetaFileAsync(newPath, CancellationToken.None);
|
|
||||||
}
|
|
||||||
else if (File.Exists(oldMetaPath))
|
|
||||||
{
|
|
||||||
// Move meta file
|
|
||||||
File.Move(oldMetaPath, newMetaPath);
|
|
||||||
|
|
||||||
// Update database with new path and recalculated hash
|
|
||||||
var metaResult = await ReadMetaFileAsync(newPath, CancellationToken.None);
|
|
||||||
if (metaResult.IsSuccess)
|
|
||||||
{
|
|
||||||
var fileHash = await CalculateFileHashAsync(newPath, CancellationToken.None);
|
|
||||||
await UpsertAssetAsync(newPath, metaResult.Value, fileHash, null, CancellationToken.None);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
else
|
|
||||||
{
|
|
||||||
// Generate new meta file
|
|
||||||
await GenerateMetaFileAsync(newPath, CancellationToken.None);
|
|
||||||
}
|
|
||||||
|
|
||||||
// Delete old meta if it still exists
|
|
||||||
if (File.Exists(oldMetaPath) && oldMetaPath != newMetaPath)
|
|
||||||
{
|
|
||||||
try
|
|
||||||
{
|
|
||||||
File.Delete(oldMetaPath);
|
|
||||||
}
|
|
||||||
catch
|
|
||||||
{
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
internal static void Shutdown()
|
|
||||||
{
|
|
||||||
lock (s_initializationLock)
|
|
||||||
{
|
|
||||||
if (!s_initialized)
|
|
||||||
{
|
|
||||||
return;
|
|
||||||
}
|
|
||||||
|
|
||||||
s_watcher?.Dispose();
|
|
||||||
s_watcher = null;
|
|
||||||
|
|
||||||
s_commandProcessorTimer?.Dispose();
|
|
||||||
s_commandProcessorTimer = null;
|
|
||||||
|
|
||||||
s_dbConnection?.Close();
|
|
||||||
s_dbConnection?.Dispose();
|
|
||||||
s_dbConnection = null;
|
|
||||||
|
|
||||||
s_assetPathLookup.Clear();
|
|
||||||
s_pathAssetLookup.Clear();
|
|
||||||
s_dirtyAssets.Clear();
|
|
||||||
s_waitingCommands.Clear();
|
|
||||||
s_importerInstances.Clear();
|
|
||||||
s_importerTypeLookup.Clear();
|
|
||||||
|
|
||||||
s_initialized = false;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
@@ -1,115 +0,0 @@
|
|||||||
# Asset Database Architecture
|
|
||||||
|
|
||||||
This document details the architectural design and data flow of the `AssetHandle` module in Ghost Editor.
|
|
||||||
|
|
||||||
## System Overview
|
|
||||||
|
|
||||||
The Asset Database acts as the bridge between the raw file system (Source Assets) and the runtime engine (Imported Assets). It maintains a consistent state using a dual-storage approach:
|
|
||||||
1. **File System**: The source of truth. Contains source files (e.g., `.png`, `.fbx`) and metadata files (`.gmeta`).
|
|
||||||
2. **SQLite Database**: An acceleration layer (cache) for fast lookups, dependency tracking, and searching.
|
|
||||||
|
|
||||||
## Data Flow
|
|
||||||
|
|
||||||
### 1. Asset Discovery & Registration
|
|
||||||
When the editor starts or a file changes:
|
|
||||||
1. **FileSystemWatcher** detects the change (Create/Delete/Modify/Rename).
|
|
||||||
2. **Event Handler** queues an `AssetCommand` (debounce mechanism prevents event storms).
|
|
||||||
3. **Command Processor** executes the command:
|
|
||||||
* **New File**: Generates a `.gmeta` file with a new GUID and default settings. Adds to SQLite.
|
|
||||||
* **Modified File**: Checks hash. If changed, marks asset as "Dirty" and updates SQLite.
|
|
||||||
* **Deleted File**: Removes from SQLite and marks dependents as "Dirty".
|
|
||||||
|
|
||||||
### 2. Import Pipeline
|
|
||||||
The import process converts source formats into engine-ready data.
|
|
||||||
|
|
||||||
**Flow:**
|
|
||||||
1. `AssetDatabase.ImportDirtyAssetsAsync()` or direct `ImportAssetAsync` is called.
|
|
||||||
2. System looks up the registered `AssetImporter` for the file extension.
|
|
||||||
3. `AssetImporter.ImportAsync` is invoked with the source path and metadata.
|
|
||||||
4. Importer reads source file and settings from metadata.
|
|
||||||
5. Importer processes data (e.g., compiles shaders, compresses textures).
|
|
||||||
6. Importer calls `AssetDatabase.SaveImportedAsset(guid, data)`.
|
|
||||||
7. Data is serialized to JSON (or binary) in the `Cache/ImportedAssets` directory as `{GUID}.asset`.
|
|
||||||
|
|
||||||
### 3. Loading Pipeline
|
|
||||||
When the engine requests an asset:
|
|
||||||
|
|
||||||
**Flow:**
|
|
||||||
1. `AssetDatabase.LoadAsset<T>(guid)` is called.
|
|
||||||
2. **Memory Cache Check**:
|
|
||||||
* Checks `s_assetCache` (ConcurrentDictionary).
|
|
||||||
* If found: Updates LRU timestamp and returns object.
|
|
||||||
* If not found: Proceeds to disk load.
|
|
||||||
3. **Disk Load**:
|
|
||||||
* Locates `{GUID}.asset` in `Cache/ImportedAssets`.
|
|
||||||
* Deserializes the data into the target runtime type (e.g., `TextureAsset`).
|
|
||||||
4. **Cache Update**:
|
|
||||||
* Adds new object to `s_assetCache`.
|
|
||||||
* If cache size > `MAX_CACHED_ASSETS` (1000), evicts oldest 20% based on access time.
|
|
||||||
|
|
||||||
## Key Components Diagram
|
|
||||||
|
|
||||||
```mermaid
|
|
||||||
graph TD
|
|
||||||
User[Editor / User] -->|File Ops| API[AssetDatabase API]
|
|
||||||
FS[File System] -->|Events| Watcher[FileSystemWatcher]
|
|
||||||
|
|
||||||
subgraph AssetDatabase
|
|
||||||
API --> DB[SQLite Database]
|
|
||||||
API --> Meta[Meta Handler]
|
|
||||||
API --> Loader[Asset Loader]
|
|
||||||
API --> Importer[Import System]
|
|
||||||
|
|
||||||
Watcher -->|Queue| Cmd[Command Processor]
|
|
||||||
Cmd --> Meta
|
|
||||||
Cmd --> DB
|
|
||||||
|
|
||||||
Importer -->|Read| FS
|
|
||||||
Importer -->|Write| Cache[Imported Assets Cache]
|
|
||||||
|
|
||||||
Loader -->|Read| Cache
|
|
||||||
Loader -->|Check| MemCache[Memory LRU Cache]
|
|
||||||
end
|
|
||||||
|
|
||||||
Meta -->|Read/Write| FS
|
|
||||||
DB -->|Index| FS
|
|
||||||
```
|
|
||||||
|
|
||||||
## Database Schema (SQLite)
|
|
||||||
|
|
||||||
The `AssetDatabase.db` contains a single `Assets` table:
|
|
||||||
|
|
||||||
| Column | Type | Description |
|
|
||||||
|--------|------|-------------|
|
|
||||||
| **Guid** | TEXT (PK) | The unique identifier of the asset. |
|
|
||||||
| **Path** | TEXT | Relative path from `Assets/` folder. Indexed for fast lookup. |
|
|
||||||
| **Version** | INTEGER | Importer version for migration support. |
|
|
||||||
| **Tags** | TEXT | JSON array of string tags. |
|
|
||||||
| **FileHash** | TEXT | SHA256 hash of the source file content. |
|
|
||||||
| **DependencyGuids** | TEXT | JSON array of GUIDs this asset depends on. |
|
|
||||||
| **LastModified** | INTEGER | Unix timestamp of last modification. |
|
|
||||||
|
|
||||||
## Detailed Subsystems
|
|
||||||
|
|
||||||
### Metadata System (`.gmeta`)
|
|
||||||
* **Format**: JSON.
|
|
||||||
* **Content**: GUID, Version, Tags, ImporterSettings (per importer type).
|
|
||||||
* **Strategy**: The `.gmeta` file is the *only* place the persistent GUID lives. If the database is corrupted, it can be rebuilt entirely by scanning the file system and reading `.gmeta` files.
|
|
||||||
|
|
||||||
### Threading & Safety
|
|
||||||
* **Locks**:
|
|
||||||
* `s_dbLock`: Protects in-memory dictionaries (`s_assetPathLookup`) and dirty tracking.
|
|
||||||
* `s_commandLock`: Protects the command queue for file events.
|
|
||||||
* **Async**: Heavy I/O operations (DB access, File I/O) are async.
|
|
||||||
* **Channels**: Uses `System.Threading.Channels` to decouple high-frequency file system events from database processing.
|
|
||||||
|
|
||||||
### Importer Registry
|
|
||||||
* Uses `TypeCache` and reflection to find classes with `[AssetImporter]`.
|
|
||||||
* Mappings are stored in `s_importerTypeLookup` (Extension -> Type).
|
|
||||||
* Importers are stateless (instantiated on demand or cached as singletons depending on implementation, currently cached in `s_importerInstances`).
|
|
||||||
|
|
||||||
## Future Improvements / Known Limitations
|
|
||||||
|
|
||||||
1. **Binary Formats**: Currently, imported assets are stored as JSON. For large assets (textures, models), a binary format is required for performance.
|
|
||||||
2. **Dependency Graph**: While dependencies are stored, a full graph traversal for complex invalidation (e.g., if A changes, re-import B which depends on A) is partial.
|
|
||||||
3. **Cross-Process Locking**: SQLite is file-based; concurrent access from multiple editor instances needs careful file locking mode configuration.
|
|
||||||
@@ -1,131 +0,0 @@
|
|||||||
# Asset Database Documentation
|
|
||||||
|
|
||||||
The Asset Database is a core component of the Ghost Editor responsible for managing the lifecycle, storage, import, and retrieval of project assets. It provides a unified API for interacting with assets, ensuring that metadata (GUIDs, tags, settings) stays synchronized with files on disk.
|
|
||||||
|
|
||||||
## Key Features
|
|
||||||
|
|
||||||
- **GUID-based Asset Identification**: Every asset is uniquely identified by a stable GUID, stored in a sidecar `.gmeta` file.
|
|
||||||
- **Automatic Importing**: Monitors the file system for changes and automatically imports assets using registered importers.
|
|
||||||
- **Dependency Tracking**: Tracks dependencies between assets to ensure validity and trigger re-imports when dependencies change.
|
|
||||||
- **Caching**: Implements an LRU (Least Recently Used) cache for loaded assets to optimize performance.
|
|
||||||
- **SQLite Backed**: Uses a local SQLite database for fast lookups (Path <-> GUID) and metadata queries.
|
|
||||||
- **Metadata Management**: Handles `.gmeta` files automatically, including generation, validation, and cleanup.
|
|
||||||
|
|
||||||
## usage
|
|
||||||
|
|
||||||
### Initialization
|
|
||||||
The Asset Database must be initialized after the project is loaded.
|
|
||||||
```csharp
|
|
||||||
await AssetDatabase.Initialize(cancellationToken);
|
|
||||||
```
|
|
||||||
|
|
||||||
### Loading Assets
|
|
||||||
Assets can be loaded by GUID or by Path.
|
|
||||||
|
|
||||||
```csharp
|
|
||||||
// Load by Path
|
|
||||||
var result = AssetDatabase.LoadAssetAtPath<TextureAsset>("Assets/Textures/my_texture.png");
|
|
||||||
if (result.IsSuccess)
|
|
||||||
{
|
|
||||||
var texture = result.Value;
|
|
||||||
}
|
|
||||||
|
|
||||||
// Load by GUID
|
|
||||||
var guid = ...;
|
|
||||||
var result = AssetDatabase.LoadAsset<TextureAsset>(guid);
|
|
||||||
```
|
|
||||||
|
|
||||||
### File Operations
|
|
||||||
Always use the `AssetDatabase` API for file operations to ensure metadata is preserved.
|
|
||||||
|
|
||||||
```csharp
|
|
||||||
// Create
|
|
||||||
await AssetDatabase.CreateAssetAsync("Assets/Data/config.json", dataBytes);
|
|
||||||
|
|
||||||
// Move
|
|
||||||
await AssetDatabase.MoveAssetAsync("Assets/Old/file.txt", "Assets/New/file.txt");
|
|
||||||
|
|
||||||
// Copy
|
|
||||||
await AssetDatabase.CopyAssetAsync("Assets/template.txt", "Assets/instance.txt");
|
|
||||||
|
|
||||||
// Delete
|
|
||||||
await AssetDatabase.DeleteAssetAsync("Assets/garbage.tmp");
|
|
||||||
```
|
|
||||||
|
|
||||||
### Searching
|
|
||||||
Find assets using wildcards or tags.
|
|
||||||
|
|
||||||
```csharp
|
|
||||||
// Find all PNGs
|
|
||||||
var guids = await AssetDatabase.FindAssetsByNameAsync("*.png");
|
|
||||||
|
|
||||||
// Find assets with a specific tag
|
|
||||||
var enemyAssets = await AssetDatabase.FindAssetsByTagAsync("Enemy");
|
|
||||||
```
|
|
||||||
|
|
||||||
### Tags
|
|
||||||
Manage asset tags for organization.
|
|
||||||
|
|
||||||
```csharp
|
|
||||||
// Get tags
|
|
||||||
var tagsResult = await AssetDatabase.GetAssetTagsAsync(guid);
|
|
||||||
|
|
||||||
// Set tags
|
|
||||||
await AssetDatabase.SetAssetTagsAsync(guid, new List<string> { "Level1", "Prop" });
|
|
||||||
```
|
|
||||||
|
|
||||||
### Opening Assets
|
|
||||||
Open an asset using its registered handler or the system default.
|
|
||||||
```csharp
|
|
||||||
AssetDatabase.OpenAsset("Assets/Docs/readme.txt");
|
|
||||||
```
|
|
||||||
|
|
||||||
## Extending the Asset Database
|
|
||||||
|
|
||||||
### Creating a New Importer
|
|
||||||
To support a new file type, create a class that inherits from `AssetImporter<T>` and decorate it with the `[AssetImporter]` attribute.
|
|
||||||
|
|
||||||
```csharp
|
|
||||||
[AssetImporter(".myfmt")]
|
|
||||||
internal class MyFormatImporter : AssetImporter<MyFormatSettings>
|
|
||||||
{
|
|
||||||
public override async Task<Result> ImportAsync(string assetPath, AssetMeta meta)
|
|
||||||
{
|
|
||||||
var settings = GetSettings(meta);
|
|
||||||
|
|
||||||
// 1. Read source file
|
|
||||||
// 2. Process data
|
|
||||||
// 3. Save imported data using AssetDatabase.SaveImportedAsset
|
|
||||||
|
|
||||||
var myAsset = new MyAsset(meta.Guid) { ... };
|
|
||||||
return AssetDatabase.SaveImportedAsset(meta.Guid, myAsset);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
internal class MyFormatSettings : ImporterSettings
|
|
||||||
{
|
|
||||||
public float Scale { get; set; } = 1.0f;
|
|
||||||
}
|
|
||||||
```
|
|
||||||
|
|
||||||
### Creating an Open Handler
|
|
||||||
To define custom behavior when an asset is opened (e.g., double-clicked in the editor), use the `[AssetOpenHandler]` attribute.
|
|
||||||
|
|
||||||
```csharp
|
|
||||||
internal static class MyHandlers
|
|
||||||
{
|
|
||||||
[AssetOpenHandler(".myfmt")]
|
|
||||||
private static void OpenMyFormat(string path)
|
|
||||||
{
|
|
||||||
// Open custom editor window
|
|
||||||
}
|
|
||||||
}
|
|
||||||
```
|
|
||||||
|
|
||||||
## Internal Architecture
|
|
||||||
|
|
||||||
- **AssetDatabase.cs**: Core initialization and event coordination.
|
|
||||||
- **AssetDatabase.SQLite.cs**: Database table management and queries.
|
|
||||||
- **AssetDatabase.Meta.cs**: `.gmeta` file handling and file system watcher events.
|
|
||||||
- **AssetDatabase.Importer.cs**: Importer discovery and execution.
|
|
||||||
- **AssetDatabase.Loader.cs**: Asset loading and caching logic.
|
|
||||||
@@ -1,81 +0,0 @@
|
|||||||
using Ghost.Core;
|
|
||||||
|
|
||||||
namespace Ghost.Editor.Core.AssetHandle;
|
|
||||||
|
|
||||||
public abstract class AssetImporter
|
|
||||||
{
|
|
||||||
/// <summary>
|
|
||||||
/// Import the asset at the specified path with the given settings.
|
|
||||||
/// </summary>
|
|
||||||
/// <param name="assetPath">Full path to the source asset file.</param>
|
|
||||||
/// <param name="meta">Metadata for the asset.</param>
|
|
||||||
/// <param name="token">Cancellation token.</param>
|
|
||||||
/// <returns>Result indicating success or failure.</returns>
|
|
||||||
public abstract ValueTask<Result> ImportAsync(string assetPath, AssetMeta meta, CancellationToken token = default);
|
|
||||||
|
|
||||||
/// <summary>
|
|
||||||
/// Export in-memory asset data to disk.
|
|
||||||
/// Override this method to support creating assets from code.
|
|
||||||
/// </summary>
|
|
||||||
/// <typeparam name="T">Type of asset data to export.</typeparam>
|
|
||||||
/// <param name="assetPath">Full path where the asset should be saved.</param>
|
|
||||||
/// <param name="assetData">In-memory asset data to serialize.</param>
|
|
||||||
/// <param name="meta">Metadata for the asset.</param>
|
|
||||||
/// <param name="token">Cancellation token.</param>
|
|
||||||
/// <returns>Result indicating success or failure.</returns>
|
|
||||||
public virtual ValueTask<Result> ExportAsync<T>(string assetPath, T assetData, AssetMeta meta, CancellationToken token = default)
|
|
||||||
where T : class
|
|
||||||
{
|
|
||||||
return ValueTask.FromResult(Result.Failure("This importer does not support exporting assets."));
|
|
||||||
}
|
|
||||||
|
|
||||||
/// <summary>
|
|
||||||
/// Validate dependencies referenced by this asset.
|
|
||||||
/// Dependencies are extracted from asset content during import and stored in the database.
|
|
||||||
/// </summary>
|
|
||||||
/// <param name="dependencies">List of dependency GUIDs extracted from the asset.</param>
|
|
||||||
/// <returns>Result indicating if all dependencies are valid.</returns>
|
|
||||||
protected virtual ValueTask<Result> ValidateDependenciesAsync(List<Guid> dependencies, CancellationToken token = default)
|
|
||||||
{
|
|
||||||
foreach (var dependencyGuid in dependencies)
|
|
||||||
{
|
|
||||||
var path = AssetDatabase.GuidToPath(dependencyGuid);
|
|
||||||
if (path.IsFailure)
|
|
||||||
{
|
|
||||||
return ValueTask.FromResult(Result.Failure($"Missing dependency: {dependencyGuid}"));
|
|
||||||
}
|
|
||||||
|
|
||||||
if (!File.Exists(path.Value))
|
|
||||||
{
|
|
||||||
return ValueTask.FromResult(Result.Failure($"Dependency file does not exist: {path.Value}"));
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
return ValueTask.FromResult(Result.Success());
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
public abstract class AssetImporter<TSettings> : AssetImporter
|
|
||||||
where TSettings : ImporterSettings, new()
|
|
||||||
{
|
|
||||||
/// <summary>
|
|
||||||
/// Get the settings for this importer from the metadata.
|
|
||||||
/// Creates default settings if none exist.
|
|
||||||
/// </summary>
|
|
||||||
/// <param name="meta">Asset metadata.</param>
|
|
||||||
/// <returns>The importer settings.</returns>
|
|
||||||
protected TSettings GetSettings(AssetMeta meta)
|
|
||||||
{
|
|
||||||
var typeName = GetType().Name;
|
|
||||||
var settings = meta.GetImporterSettings<TSettings>(typeName);
|
|
||||||
|
|
||||||
if (settings != null)
|
|
||||||
{
|
|
||||||
return settings;
|
|
||||||
}
|
|
||||||
|
|
||||||
var defaultSettings = new TSettings();
|
|
||||||
meta.SetImporterSettings(typeName, defaultSettings);
|
|
||||||
return defaultSettings;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
@@ -1,15 +0,0 @@
|
|||||||
namespace Ghost.Editor.Core.AssetHandle;
|
|
||||||
|
|
||||||
[AttributeUsage(AttributeTargets.Class, AllowMultiple = true)]
|
|
||||||
internal class AssetImporterAttribute : Attribute
|
|
||||||
{
|
|
||||||
public string[] SupportedExtensions
|
|
||||||
{
|
|
||||||
get;
|
|
||||||
}
|
|
||||||
|
|
||||||
public AssetImporterAttribute(params string[] supportedExtensions)
|
|
||||||
{
|
|
||||||
SupportedExtensions = supportedExtensions;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
@@ -1,85 +0,0 @@
|
|||||||
using System.Text.Json;
|
|
||||||
using System.Text.Json.Serialization;
|
|
||||||
|
|
||||||
namespace Ghost.Editor.Core.AssetHandle;
|
|
||||||
|
|
||||||
/// <summary>
|
|
||||||
/// Metadata for an asset, stored in .gmeta files.
|
|
||||||
/// Contains GUID, version, tags, and importer settings.
|
|
||||||
/// FileHash and Dependencies are stored in the database only, not in .gmeta files.
|
|
||||||
/// </summary>
|
|
||||||
public class AssetMeta
|
|
||||||
{
|
|
||||||
/// <summary>
|
|
||||||
/// Unique identifier for the asset.
|
|
||||||
/// </summary>
|
|
||||||
[JsonPropertyName("Guid")]
|
|
||||||
public Guid Guid
|
|
||||||
{
|
|
||||||
get;
|
|
||||||
set;
|
|
||||||
}
|
|
||||||
|
|
||||||
/// <summary>
|
|
||||||
/// Version of the asset pipeline (not the asset itself).
|
|
||||||
/// Used for migration when the asset pipeline is redesigned.
|
|
||||||
/// </summary>
|
|
||||||
[JsonPropertyName("Version")]
|
|
||||||
public int Version
|
|
||||||
{
|
|
||||||
get;
|
|
||||||
set;
|
|
||||||
} = 1;
|
|
||||||
|
|
||||||
/// <summary>
|
|
||||||
/// Tags for categorizing and searching assets.
|
|
||||||
/// </summary>
|
|
||||||
[JsonPropertyName("Tags")]
|
|
||||||
public List<string> Tags
|
|
||||||
{
|
|
||||||
get;
|
|
||||||
set;
|
|
||||||
} = new();
|
|
||||||
|
|
||||||
/// <summary>
|
|
||||||
/// Importer settings specific to this asset.
|
|
||||||
/// The key is the importer type name, and the value is a JSON element containing the settings.
|
|
||||||
/// Use GetImporterSettings<T>() and SetImporterSettings<T>() to work with strongly-typed settings.
|
|
||||||
/// </summary>
|
|
||||||
[JsonPropertyName("ImporterSettings")]
|
|
||||||
public Dictionary<string, JsonElement> ImporterSettings
|
|
||||||
{
|
|
||||||
get;
|
|
||||||
set;
|
|
||||||
} = new();
|
|
||||||
|
|
||||||
/// <summary>
|
|
||||||
/// Get importer settings of a specific type.
|
|
||||||
/// </summary>
|
|
||||||
public T? GetImporterSettings<T>(string importerName) where T : ImporterSettings
|
|
||||||
{
|
|
||||||
if (ImporterSettings.TryGetValue(importerName, out var element))
|
|
||||||
{
|
|
||||||
return element.Deserialize<T>();
|
|
||||||
}
|
|
||||||
return null;
|
|
||||||
}
|
|
||||||
|
|
||||||
/// <summary>
|
|
||||||
/// Set importer settings.
|
|
||||||
/// </summary>
|
|
||||||
public void SetImporterSettings<T>(string importerName, T settings) where T : ImporterSettings
|
|
||||||
{
|
|
||||||
var element = JsonSerializer.SerializeToElement(settings);
|
|
||||||
ImporterSettings[importerName] = element;
|
|
||||||
}
|
|
||||||
|
|
||||||
/// <summary>
|
|
||||||
/// Set importer settings (non-generic overload).
|
|
||||||
/// </summary>
|
|
||||||
internal void SetImporterSettings(string importerName, ImporterSettings settings)
|
|
||||||
{
|
|
||||||
var element = JsonSerializer.SerializeToElement(settings, settings.GetType());
|
|
||||||
ImporterSettings[importerName] = element;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
@@ -1,15 +0,0 @@
|
|||||||
namespace Ghost.Editor.Core.AssetHandle;
|
|
||||||
|
|
||||||
[AttributeUsage(AttributeTargets.Method)]
|
|
||||||
public class AssetOpenHandlerAttribute : Attribute
|
|
||||||
{
|
|
||||||
public string[] Extensions
|
|
||||||
{
|
|
||||||
get;
|
|
||||||
}
|
|
||||||
|
|
||||||
public AssetOpenHandlerAttribute(params string[] extensions)
|
|
||||||
{
|
|
||||||
Extensions = extensions.Select(e => e.StartsWith('.') ? e.ToLowerInvariant() : '.' + e.ToLowerInvariant()).ToArray();
|
|
||||||
}
|
|
||||||
}
|
|
||||||
@@ -1,5 +0,0 @@
|
|||||||
namespace Ghost.Editor.Core.AssetHandle;
|
|
||||||
|
|
||||||
public abstract class ImporterSettings
|
|
||||||
{
|
|
||||||
}
|
|
||||||
@@ -1,70 +0,0 @@
|
|||||||
using Ghost.Core;
|
|
||||||
|
|
||||||
namespace Ghost.Editor.Core.AssetHandle.Importers;
|
|
||||||
|
|
||||||
/// <summary>
|
|
||||||
/// Example importer settings for text assets.
|
|
||||||
/// </summary>
|
|
||||||
internal class TextImporterSettings : ImporterSettings
|
|
||||||
{
|
|
||||||
public string Encoding
|
|
||||||
{
|
|
||||||
get;
|
|
||||||
set;
|
|
||||||
} = "UTF-8";
|
|
||||||
|
|
||||||
public bool TrimWhitespace
|
|
||||||
{
|
|
||||||
get;
|
|
||||||
set;
|
|
||||||
} = false;
|
|
||||||
}
|
|
||||||
|
|
||||||
/// <summary>
|
|
||||||
/// Example importer for text files (.txt, .md).
|
|
||||||
/// This is a simple test importer to demonstrate the asset import system.
|
|
||||||
/// </summary>
|
|
||||||
[AssetImporter(".txt", ".md")]
|
|
||||||
internal class TextImporter : AssetImporter<TextImporterSettings>
|
|
||||||
{
|
|
||||||
public override async ValueTask<Result> ImportAsync(string assetPath, AssetMeta meta, CancellationToken token = default)
|
|
||||||
{
|
|
||||||
var settings = GetSettings(meta);
|
|
||||||
|
|
||||||
// Text files typically don't have dependencies
|
|
||||||
// If they did, you would extract them from the content here
|
|
||||||
var dependencies = new List<Guid>();
|
|
||||||
|
|
||||||
// Validate dependencies
|
|
||||||
var depResult = await ValidateDependenciesAsync(dependencies);
|
|
||||||
if (depResult.IsFailure)
|
|
||||||
{
|
|
||||||
return depResult;
|
|
||||||
}
|
|
||||||
|
|
||||||
try
|
|
||||||
{
|
|
||||||
// Read the file
|
|
||||||
var content = await File.ReadAllTextAsync(assetPath, token);
|
|
||||||
|
|
||||||
if (settings.TrimWhitespace)
|
|
||||||
{
|
|
||||||
content = content.Trim();
|
|
||||||
}
|
|
||||||
|
|
||||||
// TODO: Process the text content
|
|
||||||
// For example:
|
|
||||||
// - Convert to a specific format
|
|
||||||
// - Extract metadata
|
|
||||||
// - Generate assets
|
|
||||||
// - Save to output folder
|
|
||||||
|
|
||||||
// For now, just report success
|
|
||||||
return Result.Success();
|
|
||||||
}
|
|
||||||
catch (Exception ex)
|
|
||||||
{
|
|
||||||
return Result.Failure($"Failed to import text asset: {ex.Message}");
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
@@ -1,279 +0,0 @@
|
|||||||
using Ghost.Core;
|
|
||||||
using System.Text.Json;
|
|
||||||
|
|
||||||
namespace Ghost.Editor.Core.AssetHandle.Importers;
|
|
||||||
|
|
||||||
/// <summary>
|
|
||||||
/// Importer settings for texture assets.
|
|
||||||
/// </summary>
|
|
||||||
internal class TextureImporterSettings : ImporterSettings
|
|
||||||
{
|
|
||||||
/// <summary>
|
|
||||||
/// Whether to generate mipmaps for the texture.
|
|
||||||
/// </summary>
|
|
||||||
public bool GenerateMipmaps
|
|
||||||
{
|
|
||||||
get;
|
|
||||||
set;
|
|
||||||
} = true;
|
|
||||||
|
|
||||||
/// <summary>
|
|
||||||
/// Whether the texture uses sRGB color space.
|
|
||||||
/// </summary>
|
|
||||||
public bool SRGB
|
|
||||||
{
|
|
||||||
get;
|
|
||||||
set;
|
|
||||||
} = true;
|
|
||||||
|
|
||||||
/// <summary>
|
|
||||||
/// Maximum texture size. Images larger than this will be downscaled.
|
|
||||||
/// </summary>
|
|
||||||
public uint MaxSize
|
|
||||||
{
|
|
||||||
get;
|
|
||||||
set;
|
|
||||||
} = 2048;
|
|
||||||
|
|
||||||
/// <summary>
|
|
||||||
/// Texture compression format.
|
|
||||||
/// Options: "None", "BC1", "BC3", "BC7"
|
|
||||||
/// </summary>
|
|
||||||
public string CompressionFormat
|
|
||||||
{
|
|
||||||
get;
|
|
||||||
set;
|
|
||||||
} = "None";
|
|
||||||
|
|
||||||
/// <summary>
|
|
||||||
/// Texture filter mode.
|
|
||||||
/// Options: "Point", "Bilinear", "Trilinear"
|
|
||||||
/// </summary>
|
|
||||||
public string FilterMode
|
|
||||||
{
|
|
||||||
get;
|
|
||||||
set;
|
|
||||||
} = "Bilinear";
|
|
||||||
|
|
||||||
/// <summary>
|
|
||||||
/// Texture wrap mode.
|
|
||||||
/// Options: "Repeat", "Clamp", "Mirror"
|
|
||||||
/// </summary>
|
|
||||||
public string WrapMode
|
|
||||||
{
|
|
||||||
get;
|
|
||||||
set;
|
|
||||||
} = "Repeat";
|
|
||||||
}
|
|
||||||
|
|
||||||
/// <summary>
|
|
||||||
/// Importer for texture files (.png, .jpg, .jpeg, .dds, .tga, .bmp).
|
|
||||||
/// Processes image files and converts them into engine-ready texture assets.
|
|
||||||
/// </summary>
|
|
||||||
[AssetImporter(".png", ".jpg", ".jpeg", ".dds", ".tga", ".bmp")]
|
|
||||||
internal class TextureImporter : AssetImporter<TextureImporterSettings>
|
|
||||||
{
|
|
||||||
public override async ValueTask<Result> ImportAsync(string assetPath, AssetMeta meta, CancellationToken token = default)
|
|
||||||
{
|
|
||||||
var settings = GetSettings(meta);
|
|
||||||
|
|
||||||
// Textures typically don't reference other assets as dependencies
|
|
||||||
// If they did (e.g., normal maps referencing base textures), extract here
|
|
||||||
var dependencies = new List<Guid>();
|
|
||||||
|
|
||||||
// Validate dependencies
|
|
||||||
var depResult = await ValidateDependenciesAsync(dependencies, token);
|
|
||||||
if (depResult.IsFailure)
|
|
||||||
{
|
|
||||||
return depResult;
|
|
||||||
}
|
|
||||||
|
|
||||||
try
|
|
||||||
{
|
|
||||||
// Check if file exists
|
|
||||||
if (!File.Exists(assetPath))
|
|
||||||
{
|
|
||||||
return Result.Failure($"Source texture file not found: {assetPath}");
|
|
||||||
}
|
|
||||||
|
|
||||||
// Get image dimensions (simplified - in real implementation would use image library)
|
|
||||||
var (width, height) = GetImageDimensions(assetPath);
|
|
||||||
|
|
||||||
if (width == 0 || height == 0)
|
|
||||||
{
|
|
||||||
return Result.Failure("Failed to read image dimensions");
|
|
||||||
}
|
|
||||||
|
|
||||||
// Apply max size constraint
|
|
||||||
if (width > settings.MaxSize || height > settings.MaxSize)
|
|
||||||
{
|
|
||||||
var scale = Math.Min(settings.MaxSize / (float)width, settings.MaxSize / (float)height);
|
|
||||||
width = (uint)(width * scale);
|
|
||||||
height = (uint)(height * scale);
|
|
||||||
}
|
|
||||||
|
|
||||||
// Calculate mipmap count
|
|
||||||
uint mipLevels = 1;
|
|
||||||
if (settings.GenerateMipmaps)
|
|
||||||
{
|
|
||||||
mipLevels = CalculateMipLevels(width, height);
|
|
||||||
}
|
|
||||||
|
|
||||||
// Determine format
|
|
||||||
var format = settings.CompressionFormat == "None" ? "RGBA8" : settings.CompressionFormat;
|
|
||||||
|
|
||||||
// Create texture asset
|
|
||||||
var textureAsset = new TextureAsset(meta.Guid, Path.GetFileNameWithoutExtension(assetPath))
|
|
||||||
{
|
|
||||||
Width = width,
|
|
||||||
Height = height,
|
|
||||||
MipLevels = mipLevels,
|
|
||||||
Format = format,
|
|
||||||
IsSRGB = settings.SRGB,
|
|
||||||
SourcePath = assetPath
|
|
||||||
};
|
|
||||||
|
|
||||||
// Save the imported asset data
|
|
||||||
var saveResult = AssetDatabase.SaveImportedAsset(meta.Guid, textureAsset);
|
|
||||||
if (saveResult.IsFailure)
|
|
||||||
{
|
|
||||||
return Result.Failure($"Failed to save texture asset: {saveResult.Message}");
|
|
||||||
}
|
|
||||||
|
|
||||||
// In a real implementation, you would:
|
|
||||||
// 1. Load the image using a library like ImageSharp or StbImageSharp
|
|
||||||
// 2. Resize if needed
|
|
||||||
// 3. Generate mipmaps
|
|
||||||
// 4. Compress if needed
|
|
||||||
// 5. Save the processed texture data to the ImportedAssets folder
|
|
||||||
// 6. Update the hash in database
|
|
||||||
|
|
||||||
return Result.Success();
|
|
||||||
}
|
|
||||||
catch (Exception ex)
|
|
||||||
{
|
|
||||||
return Result.Failure($"Failed to import texture: {ex.Message}");
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
/// <summary>
|
|
||||||
/// Get image dimensions from file.
|
|
||||||
/// Simplified implementation - in production, use an image library.
|
|
||||||
/// </summary>
|
|
||||||
private static (uint width, uint height) GetImageDimensions(string imagePath)
|
|
||||||
{
|
|
||||||
// This is a placeholder implementation
|
|
||||||
// In a real implementation, you would use a library like:
|
|
||||||
// - ImageSharp
|
|
||||||
// - StbImageSharp
|
|
||||||
// - DirectXTex (for DDS files)
|
|
||||||
|
|
||||||
var extension = Path.GetExtension(imagePath).ToLowerInvariant();
|
|
||||||
|
|
||||||
if (extension == ".dds")
|
|
||||||
{
|
|
||||||
// For DDS files, read the header
|
|
||||||
// DDS header format: https://docs.microsoft.com/en-us/windows/win32/direct3ddds/dds-header
|
|
||||||
return ReadDDSHeader(imagePath);
|
|
||||||
}
|
|
||||||
else
|
|
||||||
{
|
|
||||||
// For PNG/JPG/etc, we would use an image library
|
|
||||||
// For now, return placeholder values
|
|
||||||
return (1024, 1024);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
/// <summary>
|
|
||||||
/// Read DDS file header to get dimensions.
|
|
||||||
/// </summary>
|
|
||||||
private static (uint width, uint height) ReadDDSHeader(string ddsPath)
|
|
||||||
{
|
|
||||||
try
|
|
||||||
{
|
|
||||||
using var stream = File.OpenRead(ddsPath);
|
|
||||||
using var reader = new BinaryReader(stream);
|
|
||||||
|
|
||||||
// Read magic number (should be "DDS ")
|
|
||||||
var magic = reader.ReadUInt32();
|
|
||||||
if (magic != 0x20534444) // "DDS " in little-endian
|
|
||||||
{
|
|
||||||
return (0, 0);
|
|
||||||
}
|
|
||||||
|
|
||||||
// Read header size (should be 124)
|
|
||||||
var headerSize = reader.ReadUInt32();
|
|
||||||
if (headerSize != 124)
|
|
||||||
{
|
|
||||||
return (0, 0);
|
|
||||||
}
|
|
||||||
|
|
||||||
// Skip flags
|
|
||||||
reader.ReadUInt32();
|
|
||||||
|
|
||||||
// Read height and width
|
|
||||||
var height = reader.ReadUInt32();
|
|
||||||
var width = reader.ReadUInt32();
|
|
||||||
|
|
||||||
return (width, height);
|
|
||||||
}
|
|
||||||
catch
|
|
||||||
{
|
|
||||||
return (0, 0);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
/// <summary>
|
|
||||||
/// Export a texture asset from memory to disk.
|
|
||||||
/// </summary>
|
|
||||||
public override async ValueTask<Result> ExportAsync<T>(string assetPath, T assetData, AssetMeta meta, CancellationToken token = default)
|
|
||||||
{
|
|
||||||
if (assetData is not TextureAsset textureAsset)
|
|
||||||
{
|
|
||||||
return Result.Failure($"Asset data is not a TextureAsset, got {typeof(T).Name}");
|
|
||||||
}
|
|
||||||
|
|
||||||
try
|
|
||||||
{
|
|
||||||
// In a real implementation, you would:
|
|
||||||
// 1. Convert the texture data to the appropriate format
|
|
||||||
// 2. Write the image file (PNG, DDS, etc.)
|
|
||||||
// 3. Save metadata
|
|
||||||
|
|
||||||
// For now, just save metadata as JSON
|
|
||||||
var json = JsonSerializer.Serialize(textureAsset, new JsonSerializerOptions
|
|
||||||
{
|
|
||||||
WriteIndented = true
|
|
||||||
});
|
|
||||||
|
|
||||||
await File.WriteAllTextAsync(assetPath, json, token);
|
|
||||||
|
|
||||||
return Result.Success();
|
|
||||||
}
|
|
||||||
catch (Exception ex)
|
|
||||||
{
|
|
||||||
return Result.Failure($"Failed to export texture: {ex.Message}");
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
/// <summary>
|
|
||||||
/// Calculate number of mipmap levels for a given texture size.
|
|
||||||
/// </summary>
|
|
||||||
private static uint CalculateMipLevels(uint width, uint height)
|
|
||||||
{
|
|
||||||
if (width == 0 || height == 0)
|
|
||||||
{
|
|
||||||
return 0;
|
|
||||||
}
|
|
||||||
|
|
||||||
uint count = 1;
|
|
||||||
while (width > 1 || height > 1)
|
|
||||||
{
|
|
||||||
width >>= 1;
|
|
||||||
height >>= 1;
|
|
||||||
count++;
|
|
||||||
}
|
|
||||||
|
|
||||||
return count;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
@@ -1,22 +0,0 @@
|
|||||||
namespace Ghost.Editor.Core.AssetHandle;
|
|
||||||
|
|
||||||
/// <summary>
|
|
||||||
/// The base class for all asset types in the Ghost Editor.
|
|
||||||
/// </summary>
|
|
||||||
public abstract class Asset
|
|
||||||
{
|
|
||||||
public abstract string Name
|
|
||||||
{
|
|
||||||
get; set;
|
|
||||||
}
|
|
||||||
|
|
||||||
public Guid ID
|
|
||||||
{
|
|
||||||
get;
|
|
||||||
}
|
|
||||||
|
|
||||||
protected Asset(Guid id)
|
|
||||||
{
|
|
||||||
ID = id;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
@@ -1,75 +0,0 @@
|
|||||||
namespace Ghost.Editor.Core.AssetHandle;
|
|
||||||
|
|
||||||
/// <summary>
|
|
||||||
/// Represents a texture asset.
|
|
||||||
/// </summary>
|
|
||||||
public class TextureAsset : Asset
|
|
||||||
{
|
|
||||||
public override string Name
|
|
||||||
{
|
|
||||||
get;
|
|
||||||
set;
|
|
||||||
}
|
|
||||||
|
|
||||||
/// <summary>
|
|
||||||
/// Width of the texture in pixels.
|
|
||||||
/// </summary>
|
|
||||||
public uint Width
|
|
||||||
{
|
|
||||||
get;
|
|
||||||
set;
|
|
||||||
}
|
|
||||||
|
|
||||||
/// <summary>
|
|
||||||
/// Height of the texture in pixels.
|
|
||||||
/// </summary>
|
|
||||||
public uint Height
|
|
||||||
{
|
|
||||||
get;
|
|
||||||
set;
|
|
||||||
}
|
|
||||||
|
|
||||||
/// <summary>
|
|
||||||
/// Number of mipmap levels.
|
|
||||||
/// </summary>
|
|
||||||
public uint MipLevels
|
|
||||||
{
|
|
||||||
get;
|
|
||||||
set;
|
|
||||||
}
|
|
||||||
|
|
||||||
/// <summary>
|
|
||||||
/// Texture format (e.g., "RGBA8", "BC1", "BC7").
|
|
||||||
/// </summary>
|
|
||||||
public string Format
|
|
||||||
{
|
|
||||||
get;
|
|
||||||
set;
|
|
||||||
}
|
|
||||||
|
|
||||||
/// <summary>
|
|
||||||
/// Whether the texture uses sRGB color space.
|
|
||||||
/// </summary>
|
|
||||||
public bool IsSRGB
|
|
||||||
{
|
|
||||||
get;
|
|
||||||
set;
|
|
||||||
}
|
|
||||||
|
|
||||||
/// <summary>
|
|
||||||
/// Relative path to the source image file.
|
|
||||||
/// </summary>
|
|
||||||
public string SourcePath
|
|
||||||
{
|
|
||||||
get;
|
|
||||||
set;
|
|
||||||
}
|
|
||||||
|
|
||||||
public TextureAsset(Guid id, string name) : base(id)
|
|
||||||
{
|
|
||||||
Name = name;
|
|
||||||
Format = "RGBA8";
|
|
||||||
IsSRGB = true;
|
|
||||||
SourcePath = string.Empty;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
@@ -1,7 +0,0 @@
|
|||||||
namespace Ghost.Editor.Core.Contracts;
|
|
||||||
|
|
||||||
public interface INavigationAware
|
|
||||||
{
|
|
||||||
public void OnNavigatedTo(object? parameter);
|
|
||||||
public void OnNavigatedFrom();
|
|
||||||
}
|
|
||||||
@@ -1,5 +0,0 @@
|
|||||||
<?xml version="1.0" encoding="utf-8" ?>
|
|
||||||
<ResourceDictionary
|
|
||||||
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
|
|
||||||
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
|
|
||||||
xmlns:local="using:Ghost.Editor.Controls.Internal" />
|
|
||||||
@@ -1,10 +0,0 @@
|
|||||||
namespace Ghost.Editor.Core.Inspector;
|
|
||||||
|
|
||||||
[AttributeUsage(AttributeTargets.Class)]
|
|
||||||
public class CustomEditorAttribute(Type targetType) : Attribute
|
|
||||||
{
|
|
||||||
internal Type TargetType
|
|
||||||
{
|
|
||||||
get;
|
|
||||||
} = targetType;
|
|
||||||
}
|
|
||||||
@@ -1,13 +0,0 @@
|
|||||||
using Microsoft.UI.Xaml;
|
|
||||||
using Microsoft.UI.Xaml.Controls;
|
|
||||||
|
|
||||||
namespace Ghost.Editor.Core.Inspector;
|
|
||||||
|
|
||||||
public interface IInspectable
|
|
||||||
{
|
|
||||||
public IconSource? CreateIcon();
|
|
||||||
|
|
||||||
public UIElement? CreateHeader();
|
|
||||||
|
|
||||||
public UIElement? CreateInspector();
|
|
||||||
}
|
|
||||||
@@ -1,12 +0,0 @@
|
|||||||
namespace Ghost.Editor.Core.Inspector;
|
|
||||||
|
|
||||||
internal interface IInspectorService
|
|
||||||
{
|
|
||||||
public IInspectable? SelectedInspectable
|
|
||||||
{
|
|
||||||
get;
|
|
||||||
set;
|
|
||||||
}
|
|
||||||
|
|
||||||
public event Action? OnSelectionChanged;
|
|
||||||
}
|
|
||||||
@@ -1,19 +0,0 @@
|
|||||||
namespace Ghost.Editor.Core.Inspector;
|
|
||||||
|
|
||||||
public class InspectorService : IInspectorService
|
|
||||||
{
|
|
||||||
public IInspectable? SelectedInspectable
|
|
||||||
{
|
|
||||||
get => field;
|
|
||||||
set
|
|
||||||
{
|
|
||||||
if (field != value)
|
|
||||||
{
|
|
||||||
field = value;
|
|
||||||
OnSelectionChanged?.Invoke();
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
public event Action? OnSelectionChanged;
|
|
||||||
}
|
|
||||||
@@ -1,9 +0,0 @@
|
|||||||
using CommunityToolkit.WinUI.Behaviors;
|
|
||||||
|
|
||||||
namespace Ghost.Editor.Core.Notifications;
|
|
||||||
|
|
||||||
public interface INotificationService
|
|
||||||
{
|
|
||||||
public void ShowNotification(string? message, MessageType type, int duration = 5, string? title = null);
|
|
||||||
public void ShowNotification(Notification notification);
|
|
||||||
}
|
|
||||||
@@ -1,9 +0,0 @@
|
|||||||
namespace Ghost.Editor.Core.Progress;
|
|
||||||
|
|
||||||
public interface IProgressService
|
|
||||||
{
|
|
||||||
public void ShowProgress(string message, double progress = 0.0);
|
|
||||||
public void ShowIndeterminateProgress(string message);
|
|
||||||
public void SetProgress(double progress);
|
|
||||||
public void HideProgress();
|
|
||||||
}
|
|
||||||
@@ -1,26 +0,0 @@
|
|||||||
using Microsoft.UI.Xaml;
|
|
||||||
|
|
||||||
namespace Ghost.Editor.Core.Utilities;
|
|
||||||
|
|
||||||
public static class EditorApplication
|
|
||||||
{
|
|
||||||
private static IServiceProvider? _serviceProvider;
|
|
||||||
|
|
||||||
public static Application Current => Application.Current;
|
|
||||||
|
|
||||||
internal static void Initialize(IServiceProvider serviceProvider)
|
|
||||||
{
|
|
||||||
_serviceProvider = serviceProvider;
|
|
||||||
}
|
|
||||||
|
|
||||||
public static T GetService<T>()
|
|
||||||
where T : class
|
|
||||||
{
|
|
||||||
if (_serviceProvider?.GetService(typeof(T)) is not T service)
|
|
||||||
{
|
|
||||||
throw new ArgumentException($"{typeof(T)} needs to be registered in ConfigureServices within App.xaml.cs.");
|
|
||||||
}
|
|
||||||
|
|
||||||
return service;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
@@ -1,36 +0,0 @@
|
|||||||
using Ghost.Core.Attributes;
|
|
||||||
using System.Reflection;
|
|
||||||
|
|
||||||
namespace Ghost.Editor.Core.Utilities;
|
|
||||||
|
|
||||||
public static class TypeCache
|
|
||||||
{
|
|
||||||
private static readonly TypeInfo[] s_types;
|
|
||||||
|
|
||||||
static TypeCache()
|
|
||||||
{
|
|
||||||
var loadableTypes = new List<Type>(512);
|
|
||||||
var assembliesToScan = AppDomain.CurrentDomain.GetAssemblies()
|
|
||||||
.Where(a => a.GetCustomAttribute<EngineAssemblyAttribute>() != null);
|
|
||||||
|
|
||||||
foreach (var assembly in assembliesToScan)
|
|
||||||
{
|
|
||||||
try
|
|
||||||
{
|
|
||||||
loadableTypes.AddRange(assembly.GetTypes());
|
|
||||||
}
|
|
||||||
catch (ReflectionTypeLoadException ex)
|
|
||||||
{
|
|
||||||
var types = ex.Types.Where(t => t != null);
|
|
||||||
loadableTypes.AddRange(types!);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
s_types = loadableTypes.Select(t => t.GetTypeInfo()).ToArray();
|
|
||||||
}
|
|
||||||
|
|
||||||
public static Type[] GetTypes()
|
|
||||||
{
|
|
||||||
return s_types;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
@@ -1,30 +0,0 @@
|
|||||||
using Ghost.Data.Resources;
|
|
||||||
using Ghost.Data.Services;
|
|
||||||
using Ghost.Editor.Core.Utilities;
|
|
||||||
using Microsoft.UI.Xaml;
|
|
||||||
|
|
||||||
namespace Ghost.Editor;
|
|
||||||
|
|
||||||
internal static class ActivationHandler
|
|
||||||
{
|
|
||||||
private static void FolderInitialization()
|
|
||||||
{
|
|
||||||
if (!Directory.Exists(DataPath.s_applicationDataFolder))
|
|
||||||
{
|
|
||||||
Directory.CreateDirectory(DataPath.s_applicationDataFolder);
|
|
||||||
}
|
|
||||||
|
|
||||||
if (!Directory.Exists(DataPath.s_projectTemplateFolder))
|
|
||||||
{
|
|
||||||
Directory.CreateDirectory(DataPath.s_projectTemplateFolder);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
public static void Handle(LaunchActivatedEventArgs args)
|
|
||||||
{
|
|
||||||
FolderInitialization();
|
|
||||||
ProjectService.EnsureDefaultTemplate();
|
|
||||||
|
|
||||||
EditorApplication.Initialize(((App)(Application.Current)).Host.Services);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
@@ -1,112 +0,0 @@
|
|||||||
using Ghost.Core;
|
|
||||||
using Ghost.Editor.Core.AppState;
|
|
||||||
using Ghost.Editor.Core.Inspector;
|
|
||||||
using Ghost.Editor.Core.Notifications;
|
|
||||||
using Ghost.Editor.Core.Progress;
|
|
||||||
using Ghost.Editor.Utilities;
|
|
||||||
using Microsoft.Extensions.DependencyInjection;
|
|
||||||
using Microsoft.Extensions.Hosting;
|
|
||||||
using Microsoft.UI.Xaml;
|
|
||||||
|
|
||||||
// To learn more about WinUI, the WinUI project structure,
|
|
||||||
// and more about our project templates, see: http://aka.ms/winui-project-info.
|
|
||||||
|
|
||||||
namespace Ghost.Editor;
|
|
||||||
|
|
||||||
/// <summary>
|
|
||||||
/// Provides application-specific behavior to supplement the default Application class.
|
|
||||||
/// </summary>
|
|
||||||
public partial class App : Application
|
|
||||||
{
|
|
||||||
private Window? _window;
|
|
||||||
|
|
||||||
internal static Window? Window
|
|
||||||
{
|
|
||||||
get => (Current as App)!._window;
|
|
||||||
set
|
|
||||||
{
|
|
||||||
if (Current is App app)
|
|
||||||
{
|
|
||||||
// HACK: As far as I can tell, there is no proper application shutdown event in WinUI 3.
|
|
||||||
app._window?.Closed -= app.OnClosed;
|
|
||||||
app._window = value;
|
|
||||||
app._window?.Closed += app.OnClosed;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
internal IHost Host
|
|
||||||
{
|
|
||||||
get;
|
|
||||||
}
|
|
||||||
|
|
||||||
/// <summary>
|
|
||||||
/// Initializes the singleton application object. This is the first line of authored code
|
|
||||||
/// executed, and as such is the logical equivalent of main() or WinMain().
|
|
||||||
/// </summary>
|
|
||||||
internal App()
|
|
||||||
{
|
|
||||||
InitializeComponent();
|
|
||||||
|
|
||||||
Host = Microsoft.Extensions.Hosting.Host.
|
|
||||||
CreateDefaultBuilder().
|
|
||||||
UseContentRoot(AppContext.BaseDirectory).
|
|
||||||
ConfigureServices((context, services) =>
|
|
||||||
{
|
|
||||||
HostHelper.AddLandingScope(context, services);
|
|
||||||
HostHelper.AddEngineScope(context, services);
|
|
||||||
|
|
||||||
services.AddSingleton<AppStateMachine>();
|
|
||||||
services.AddSingleton<INotificationService, NotificationService>();
|
|
||||||
services.AddSingleton<IProgressService, ProgressService>();
|
|
||||||
services.AddSingleton<IInspectorService, InspectorService>();
|
|
||||||
})
|
|
||||||
.Build();
|
|
||||||
|
|
||||||
UnhandledException += App_UnhandledException;
|
|
||||||
}
|
|
||||||
|
|
||||||
internal static IServiceScope CreateScope()
|
|
||||||
{
|
|
||||||
return (Current as App)!.Host.Services.CreateScope();
|
|
||||||
}
|
|
||||||
|
|
||||||
public static T GetService<T>() where T : class
|
|
||||||
{
|
|
||||||
if ((Current as App)!.Host.Services.GetService(typeof(T)) is not T service)
|
|
||||||
{
|
|
||||||
throw new ArgumentException($"{typeof(T)} needs to be registered in ConfigureServices within App.xaml.cs.");
|
|
||||||
}
|
|
||||||
|
|
||||||
return service;
|
|
||||||
}
|
|
||||||
|
|
||||||
/// <summary>
|
|
||||||
/// Invoked when the application is launched.
|
|
||||||
/// </summary>
|
|
||||||
/// <param name="args">Details about the launch request and process.</param>
|
|
||||||
protected override async void OnLaunched(LaunchActivatedEventArgs args)
|
|
||||||
{
|
|
||||||
base.OnLaunched(args);
|
|
||||||
|
|
||||||
await Host.StartAsync();
|
|
||||||
ActivationHandler.Handle(args);
|
|
||||||
|
|
||||||
var stateMachine = GetService<AppStateMachine>();
|
|
||||||
stateMachine.RegisterState(StateKey.Landing, () => new LandingState());
|
|
||||||
stateMachine.RegisterState(StateKey.EngineEditor, () => new EditorState());
|
|
||||||
|
|
||||||
await stateMachine.TransitionToAsync(StateKey.Landing);
|
|
||||||
}
|
|
||||||
|
|
||||||
private void OnClosed(object? sender, WindowEventArgs args)
|
|
||||||
{
|
|
||||||
Host.StopAsync().GetAwaiter().GetResult();
|
|
||||||
Host.Dispose();
|
|
||||||
}
|
|
||||||
|
|
||||||
private void App_UnhandledException(object sender, Microsoft.UI.Xaml.UnhandledExceptionEventArgs e)
|
|
||||||
{
|
|
||||||
Logger.LogError(e.Exception);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
Before Width: | Height: | Size: 1.1 KiB |
|
Before Width: | Height: | Size: 1.3 KiB |
|
Before Width: | Height: | Size: 1.6 KiB |
|
Before Width: | Height: | Size: 2.2 KiB |
|
Before Width: | Height: | Size: 4.4 KiB |
|
Before Width: | Height: | Size: 433 B |
|
Before Width: | Height: | Size: 583 B |
|
Before Width: | Height: | Size: 6.8 KiB |
|
Before Width: | Height: | Size: 852 B |
|
Before Width: | Height: | Size: 1.2 KiB |
@@ -1,38 +0,0 @@
|
|||||||
using CommunityToolkit.Mvvm.ComponentModel;
|
|
||||||
using Ghost.Editor.Core.Contracts;
|
|
||||||
using Microsoft.UI.Xaml.Controls;
|
|
||||||
using Microsoft.UI.Xaml.Navigation;
|
|
||||||
|
|
||||||
namespace Ghost.Editor.Controls;
|
|
||||||
|
|
||||||
public abstract partial class ViewModelPage<VM> : Page
|
|
||||||
where VM : ObservableObject
|
|
||||||
{
|
|
||||||
public VM ViewModel
|
|
||||||
{
|
|
||||||
get;
|
|
||||||
}
|
|
||||||
|
|
||||||
protected ViewModelPage(VM viewModel)
|
|
||||||
{
|
|
||||||
ViewModel = viewModel;
|
|
||||||
}
|
|
||||||
|
|
||||||
protected override void OnNavigatedTo(NavigationEventArgs e)
|
|
||||||
{
|
|
||||||
base.OnNavigatedTo(e);
|
|
||||||
if (ViewModel is INavigationAware navigationAware)
|
|
||||||
{
|
|
||||||
navigationAware.OnNavigatedTo(e.Parameter);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
protected override void OnNavigatedFrom(NavigationEventArgs e)
|
|
||||||
{
|
|
||||||
base.OnNavigatedFrom(e);
|
|
||||||
if (ViewModel is INavigationAware navigationAware)
|
|
||||||
{
|
|
||||||
navigationAware.OnNavigatedFrom();
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
@@ -1,60 +0,0 @@
|
|||||||
using Ghost.Core;
|
|
||||||
using Ghost.Data.Models;
|
|
||||||
using Ghost.Data.Services;
|
|
||||||
using Ghost.Editor.Core.AssetHandle;
|
|
||||||
using Ghost.Editor.View.Windows;
|
|
||||||
using Ghost.Engine;
|
|
||||||
|
|
||||||
namespace Ghost.Editor.Core.AppState;
|
|
||||||
|
|
||||||
internal class EditorState : IAppState
|
|
||||||
{
|
|
||||||
private EngineEditorWindow? _window;
|
|
||||||
private EngineCore? _engineCore;
|
|
||||||
|
|
||||||
public ValueTask<Result> OnExitingAsync()
|
|
||||||
{
|
|
||||||
if (App.Window == _window)
|
|
||||||
{
|
|
||||||
App.Window = null;
|
|
||||||
}
|
|
||||||
|
|
||||||
_engineCore?.Dispose();
|
|
||||||
|
|
||||||
return ValueTask.FromResult(Result.Success());
|
|
||||||
}
|
|
||||||
|
|
||||||
public ValueTask<Result> OnEnteringAsync(object? parameter)
|
|
||||||
{
|
|
||||||
if (parameter is not ProjectMetadataInfo metadataInfo)
|
|
||||||
{
|
|
||||||
return ValueTask.FromResult(Result.Failure("Invalid parameter for entering EditorState."));
|
|
||||||
}
|
|
||||||
|
|
||||||
ProjectService.CurrentProject = metadataInfo;
|
|
||||||
|
|
||||||
_engineCore = App.GetService<EngineCore>();
|
|
||||||
_engineCore.Init();
|
|
||||||
|
|
||||||
_window = App.GetService<EngineEditorWindow>();
|
|
||||||
_window.Activate();
|
|
||||||
|
|
||||||
App.Window = _window;
|
|
||||||
|
|
||||||
return ValueTask.FromResult(Result.Success());
|
|
||||||
}
|
|
||||||
|
|
||||||
public ValueTask<Result> OnExitedAsync()
|
|
||||||
{
|
|
||||||
_window?.Close();
|
|
||||||
_window = null;
|
|
||||||
|
|
||||||
return ValueTask.FromResult(Result.Success());
|
|
||||||
}
|
|
||||||
|
|
||||||
public async ValueTask<Result> OnEnteredAsync(object? parameter)
|
|
||||||
{
|
|
||||||
await AssetDatabase.Initialize();
|
|
||||||
return Result.Success();
|
|
||||||
}
|
|
||||||
}
|
|
||||||
@@ -1,42 +0,0 @@
|
|||||||
using Ghost.Core;
|
|
||||||
using Ghost.Editor.View.Windows;
|
|
||||||
|
|
||||||
namespace Ghost.Editor.Core.AppState;
|
|
||||||
|
|
||||||
internal class LandingState : IAppState
|
|
||||||
{
|
|
||||||
private LandingWindow? _window;
|
|
||||||
|
|
||||||
public ValueTask<Result> OnExitingAsync()
|
|
||||||
{
|
|
||||||
if (App.Window == _window)
|
|
||||||
{
|
|
||||||
App.Window = null;
|
|
||||||
}
|
|
||||||
|
|
||||||
return ValueTask.FromResult(Result.Success());
|
|
||||||
}
|
|
||||||
|
|
||||||
public ValueTask<Result> OnEnteringAsync(object? parameter)
|
|
||||||
{
|
|
||||||
_window = App.GetService<LandingWindow>();
|
|
||||||
_window.Activate();
|
|
||||||
|
|
||||||
App.Window = _window;
|
|
||||||
|
|
||||||
return ValueTask.FromResult(Result.Success());
|
|
||||||
}
|
|
||||||
|
|
||||||
public ValueTask<Result> OnExitedAsync()
|
|
||||||
{
|
|
||||||
_window?.Close();
|
|
||||||
_window = null;
|
|
||||||
|
|
||||||
return ValueTask.FromResult(Result.Success());
|
|
||||||
}
|
|
||||||
|
|
||||||
public ValueTask<Result> OnEnteredAsync(object? parameter)
|
|
||||||
{
|
|
||||||
return ValueTask.FromResult(Result.Success());
|
|
||||||
}
|
|
||||||
}
|
|
||||||
@@ -1,121 +0,0 @@
|
|||||||
<Project Sdk="Microsoft.NET.Sdk">
|
|
||||||
<PropertyGroup>
|
|
||||||
<OutputType>WinExe</OutputType>
|
|
||||||
<TargetFramework>net10.0-windows10.0.22621.0</TargetFramework>
|
|
||||||
<TargetPlatformMinVersion>10.0.17763.0</TargetPlatformMinVersion>
|
|
||||||
<Platforms>x86;x64;ARM64</Platforms>
|
|
||||||
<RuntimeIdentifiers>win-x86;win-x64;win-arm64</RuntimeIdentifiers>
|
|
||||||
<PublishProfile>win-$(Platform).pubxml</PublishProfile>
|
|
||||||
<UseWinUI>true</UseWinUI>
|
|
||||||
<EnableMsixTooling>true</EnableMsixTooling>
|
|
||||||
<!-- in .net 10, field keyword is not preview anymore, but we are still waiting roslyn team to update their code analyzer packages -->
|
|
||||||
<langversion>preview</langversion>
|
|
||||||
</PropertyGroup>
|
|
||||||
<ItemGroup>
|
|
||||||
<Content Include="Assets\SplashScreen.scale-200.png" />
|
|
||||||
<Content Include="Assets\LockScreenLogo.scale-200.png" />
|
|
||||||
<Content Include="Assets\Square150x150Logo.scale-200.png" />
|
|
||||||
<Content Include="Assets\StoreLogo.png" />
|
|
||||||
<Content Include="Assets\Wide310x150Logo.scale-200.png" />
|
|
||||||
</ItemGroup>
|
|
||||||
|
|
||||||
<ItemGroup>
|
|
||||||
<Manifest Include="$(ApplicationManifest)" />
|
|
||||||
</ItemGroup>
|
|
||||||
|
|
||||||
<!--
|
|
||||||
Defining the "Msix" ProjectCapability here allows the Single-project MSIX Packaging
|
|
||||||
Tools extension to be activated for this project even if the Windows App SDK Nuget
|
|
||||||
package has not yet been restored.
|
|
||||||
-->
|
|
||||||
<ItemGroup Condition="'$(DisableMsixProjectCapabilityAddedByProject)'!='true' and '$(EnableMsixTooling)'=='true'">
|
|
||||||
<ProjectCapability Include="Msix" />
|
|
||||||
</ItemGroup>
|
|
||||||
<ItemGroup>
|
|
||||||
<PackageReference Include="CommunityToolkit.WinUI.Controls.Primitives" Version="8.2.251219" />
|
|
||||||
<PackageReference Include="CommunityToolkit.WinUI.Controls.TabbedCommandBar" Version="8.2.251219" />
|
|
||||||
<PackageReference Include="Microsoft.Extensions.Hosting" Version="10.0.2" />
|
|
||||||
<PackageReference Include="Microsoft.Windows.CsWinRT" Version="2.2.0" />
|
|
||||||
<PackageReference Include="Microsoft.Windows.SDK.BuildTools" Version="10.0.26100.7463" />
|
|
||||||
<PackageReference Include="Microsoft.WindowsAppSDK" Version="1.8.260101001" />
|
|
||||||
<PackageReference Include="WinUIEx" Version="2.9.0" />
|
|
||||||
</ItemGroup>
|
|
||||||
<ItemGroup>
|
|
||||||
<ProjectReference Include="..\Ghost.Editor.Core\Ghost.Editor.Core.csproj" />
|
|
||||||
<ProjectReference Include="..\Ghost.Entities\Ghost.Entities.csproj" />
|
|
||||||
</ItemGroup>
|
|
||||||
<ItemGroup>
|
|
||||||
<Page Update="View\Pages\Landing\CreateProjectPage.xaml">
|
|
||||||
<Generator>MSBuild:Compile</Generator>
|
|
||||||
</Page>
|
|
||||||
</ItemGroup>
|
|
||||||
<ItemGroup>
|
|
||||||
<Page Update="View\Window\Landing.xaml">
|
|
||||||
<Generator>MSBuild:Compile</Generator>
|
|
||||||
</Page>
|
|
||||||
</ItemGroup>
|
|
||||||
<ItemGroup>
|
|
||||||
<Page Update="View\Pages\Landing\OpenProjectPage.xaml">
|
|
||||||
<Generator>MSBuild:Compile</Generator>
|
|
||||||
</Page>
|
|
||||||
</ItemGroup>
|
|
||||||
<ItemGroup>
|
|
||||||
<Page Update="View\Pages\EngineEditor\InspectorPage.xaml">
|
|
||||||
<Generator>MSBuild:Compile</Generator>
|
|
||||||
</Page>
|
|
||||||
</ItemGroup>
|
|
||||||
<ItemGroup>
|
|
||||||
<Page Update="View\Pages\EngineEditor\HierarchyPage.xaml">
|
|
||||||
<Generator>MSBuild:Compile</Generator>
|
|
||||||
</Page>
|
|
||||||
</ItemGroup>
|
|
||||||
<ItemGroup>
|
|
||||||
<Page Update="View\Pages\EngineEditor\ProjectPage.xaml">
|
|
||||||
<Generator>MSBuild:Compile</Generator>
|
|
||||||
</Page>
|
|
||||||
</ItemGroup>
|
|
||||||
<ItemGroup>
|
|
||||||
<Page Update="View\Pages\EngineEditor\ConsolePage.xaml">
|
|
||||||
<Generator>MSBuild:Compile</Generator>
|
|
||||||
</Page>
|
|
||||||
</ItemGroup>
|
|
||||||
<ItemGroup>
|
|
||||||
<Page Update="Themes\Override.xaml">
|
|
||||||
<Generator>MSBuild:Compile</Generator>
|
|
||||||
</Page>
|
|
||||||
</ItemGroup>
|
|
||||||
<ItemGroup>
|
|
||||||
<Page Update="View\Windows\EngineEditorWindow.xaml">
|
|
||||||
<Generator>MSBuild:Compile</Generator>
|
|
||||||
</Page>
|
|
||||||
</ItemGroup>
|
|
||||||
<ItemGroup>
|
|
||||||
<Page Update="View\Pages\EngineEditor\ScenePage.xaml">
|
|
||||||
<Generator>MSBuild:Compile</Generator>
|
|
||||||
</Page>
|
|
||||||
</ItemGroup>
|
|
||||||
<PropertyGroup Label="Globals" />
|
|
||||||
|
|
||||||
<!--
|
|
||||||
Defining the "HasPackageAndPublishMenuAddedByProject" property here allows the Solution
|
|
||||||
Explorer "Package and Publish" context menu entry to be enabled for this project even if
|
|
||||||
the Windows App SDK Nuget package has not yet been restored.
|
|
||||||
-->
|
|
||||||
<PropertyGroup Condition="'$(DisableHasPackageAndPublishMenuAddedByProject)'!='true' and '$(EnableMsixTooling)'=='true'">
|
|
||||||
<HasPackageAndPublishMenu>true</HasPackageAndPublishMenu>
|
|
||||||
</PropertyGroup>
|
|
||||||
|
|
||||||
<!-- Publish Properties -->
|
|
||||||
<PropertyGroup>
|
|
||||||
<PublishReadyToRun Condition="'$(Configuration)' == 'Debug'">False</PublishReadyToRun>
|
|
||||||
<PublishReadyToRun Condition="'$(Configuration)' != 'Debug'">True</PublishReadyToRun>
|
|
||||||
<PublishTrimmed Condition="'$(Configuration)' != 'Debug'">True</PublishTrimmed>
|
|
||||||
<Nullable>enable</Nullable>
|
|
||||||
<SupportedOSPlatformVersion>10.0.20348.0</SupportedOSPlatformVersion>
|
|
||||||
<ApplicationManifest>app.manifest</ApplicationManifest>
|
|
||||||
<PublishAot>False</PublishAot>
|
|
||||||
<PublishTrimmed>False</PublishTrimmed>
|
|
||||||
<RootNamespace>Ghost.Editor</RootNamespace>
|
|
||||||
<ImplicitUsings>enable</ImplicitUsings>
|
|
||||||
</PropertyGroup>
|
|
||||||
</Project>
|
|
||||||
@@ -1,20 +0,0 @@
|
|||||||
<?xml version="1.0" encoding="utf-8" ?>
|
|
||||||
<ResourceDictionary
|
|
||||||
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
|
|
||||||
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
|
|
||||||
xmlns:controls="using:Microsoft.UI.Xaml.Controls"
|
|
||||||
xmlns:internal="using:Ghost.Editor.Controls.Internal">
|
|
||||||
<ResourceDictionary.ThemeDictionaries>
|
|
||||||
<ResourceDictionary x:Key="Dark">
|
|
||||||
<StaticResource x:Key="TabViewItemHeaderBackgroundSelected" ResourceKey="ControlFillColorSecondaryBrush" />
|
|
||||||
</ResourceDictionary>
|
|
||||||
<ResourceDictionary x:Key="Light">
|
|
||||||
<StaticResource x:Key="TabViewItemHeaderBackgroundSelected" ResourceKey="ControlFillColorSecondaryBrush" />
|
|
||||||
</ResourceDictionary>
|
|
||||||
</ResourceDictionary.ThemeDictionaries>
|
|
||||||
|
|
||||||
<Style TargetType="internal:NavigationTabView">
|
|
||||||
<Setter Property="TabWidthMode" Value="Compact" />
|
|
||||||
</Style>
|
|
||||||
<Style TargetType="NumberBox" />
|
|
||||||
</ResourceDictionary>
|
|
||||||
@@ -1,50 +0,0 @@
|
|||||||
using Ghost.Data.Services;
|
|
||||||
using Ghost.Editor.View.Pages.EngineEditor;
|
|
||||||
using Ghost.Editor.View.Pages.Landing;
|
|
||||||
using Ghost.Editor.View.Windows;
|
|
||||||
using Ghost.Editor.ViewModels.Pages.EngineEditor;
|
|
||||||
using Ghost.Editor.ViewModels.Pages.Landing;
|
|
||||||
using Ghost.Editor.ViewModels.Windows;
|
|
||||||
using Ghost.Engine;
|
|
||||||
using Microsoft.Extensions.DependencyInjection;
|
|
||||||
using Microsoft.Extensions.Hosting;
|
|
||||||
|
|
||||||
namespace Ghost.Editor.Utilities;
|
|
||||||
|
|
||||||
internal static partial class HostHelper
|
|
||||||
{
|
|
||||||
public static void AddLandingScope(HostBuilderContext context, IServiceCollection services)
|
|
||||||
{
|
|
||||||
services.AddSingleton<LandingWindow>();
|
|
||||||
|
|
||||||
services.AddTransient<CreateProjectPage>();
|
|
||||||
services.AddTransient<CreateProjectViewModel>();
|
|
||||||
|
|
||||||
services.AddTransient<OpenProjectPage>();
|
|
||||||
services.AddTransient<OpenProjectViewModel>();
|
|
||||||
|
|
||||||
services.AddTransient<ProjectService>();
|
|
||||||
}
|
|
||||||
|
|
||||||
public static void AddEngineScope(HostBuilderContext context, IServiceCollection services)
|
|
||||||
{
|
|
||||||
services.AddSingleton<EngineCore>();
|
|
||||||
|
|
||||||
services.AddSingleton<EngineEditorWindow>();
|
|
||||||
services.AddSingleton<EngineEditorViewModel>();
|
|
||||||
|
|
||||||
services.AddTransient<ScenePage>();
|
|
||||||
|
|
||||||
services.AddTransient<HierarchyPage>();
|
|
||||||
services.AddTransient<HierarchyViewModel>();
|
|
||||||
|
|
||||||
services.AddTransient<ProjectPage>();
|
|
||||||
services.AddTransient<ProjectViewModel>();
|
|
||||||
|
|
||||||
services.AddTransient<ConsolePage>();
|
|
||||||
services.AddTransient<ConsoleViewModel>();
|
|
||||||
|
|
||||||
services.AddTransient<InspectorPage>();
|
|
||||||
services.AddTransient<InspectorViewModel>();
|
|
||||||
}
|
|
||||||
}
|
|
||||||
@@ -1,142 +0,0 @@
|
|||||||
<?xml version="1.0" encoding="utf-8" ?>
|
|
||||||
<Page
|
|
||||||
x:Class="Ghost.Editor.View.Pages.Landing.CreateProjectPage"
|
|
||||||
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
|
|
||||||
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
|
|
||||||
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
|
|
||||||
xmlns:data="using:Ghost.Data.Models"
|
|
||||||
xmlns:editor="using:Ghost.Editor.Core.Controls"
|
|
||||||
xmlns:local="using:Ghost.Editor.View.Pages.Landing"
|
|
||||||
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
|
|
||||||
NavigationCacheMode="Enabled"
|
|
||||||
mc:Ignorable="d">
|
|
||||||
|
|
||||||
<Grid>
|
|
||||||
<Grid.ColumnDefinitions>
|
|
||||||
<ColumnDefinition Width="Auto" />
|
|
||||||
<ColumnDefinition Width="*" />
|
|
||||||
</Grid.ColumnDefinitions>
|
|
||||||
|
|
||||||
<!-- Template Info -->
|
|
||||||
<Grid Grid.Column="0" Width="300">
|
|
||||||
<Grid.RowDefinitions>
|
|
||||||
<RowDefinition Height="Auto" />
|
|
||||||
<RowDefinition Height="*" />
|
|
||||||
</Grid.RowDefinitions>
|
|
||||||
|
|
||||||
<TextBlock
|
|
||||||
Grid.Row="0"
|
|
||||||
Margin="0,0,0,24"
|
|
||||||
Style="{StaticResource SubtitleTextBlockStyle}"
|
|
||||||
Text="Template" />
|
|
||||||
|
|
||||||
<ListView
|
|
||||||
Grid.Row="1"
|
|
||||||
ItemsSource="{x:Bind ViewModel.templates}"
|
|
||||||
SelectedItem="{x:Bind ViewModel.SelectedTemplate, Mode=TwoWay}">
|
|
||||||
<ListView.ItemTemplate>
|
|
||||||
<DataTemplate x:DataType="data:TemplateData">
|
|
||||||
<Grid>
|
|
||||||
<Grid.ColumnDefinitions>
|
|
||||||
<ColumnDefinition Width="Auto" />
|
|
||||||
<ColumnDefinition Width="*" />
|
|
||||||
</Grid.ColumnDefinitions>
|
|
||||||
|
|
||||||
<ImageIcon
|
|
||||||
Grid.Column="0"
|
|
||||||
Width="24"
|
|
||||||
Height="24">
|
|
||||||
<ImageIcon.Source>
|
|
||||||
<BitmapImage UriSource="{x:Bind GetIconURI()}" />
|
|
||||||
</ImageIcon.Source>
|
|
||||||
</ImageIcon>
|
|
||||||
<TextBlock
|
|
||||||
Grid.Column="1"
|
|
||||||
Margin="8,0"
|
|
||||||
VerticalAlignment="Center"
|
|
||||||
Text="{x:Bind Info.Name}" />
|
|
||||||
</Grid>
|
|
||||||
</DataTemplate>
|
|
||||||
</ListView.ItemTemplate>
|
|
||||||
</ListView>
|
|
||||||
</Grid>
|
|
||||||
|
|
||||||
<!-- Project Info -->
|
|
||||||
<Grid
|
|
||||||
Grid.Column="1"
|
|
||||||
Margin="16,0,0,0"
|
|
||||||
Padding="16"
|
|
||||||
Background="{ThemeResource CardBackgroundFillColorDefaultBrush}"
|
|
||||||
CornerRadius="{StaticResource OverlayCornerRadius}">
|
|
||||||
<Grid.RowDefinitions>
|
|
||||||
<RowDefinition Height="300" />
|
|
||||||
<RowDefinition Height="*" />
|
|
||||||
<RowDefinition Height="Auto" />
|
|
||||||
</Grid.RowDefinitions>
|
|
||||||
|
|
||||||
<Grid Grid.Row="0" CornerRadius="4">
|
|
||||||
<Image VerticalAlignment="Center" Stretch="UniformToFill">
|
|
||||||
<Image.Source>
|
|
||||||
<BitmapImage UriSource="{x:Bind ViewModel.SelectedTemplate.Value.GetPreviewURI(), Mode=OneWay}" />
|
|
||||||
</Image.Source>
|
|
||||||
</Image>
|
|
||||||
<Grid
|
|
||||||
MaxHeight="100"
|
|
||||||
VerticalAlignment="Bottom"
|
|
||||||
Background="{ThemeResource ControlOnImageFillColorDefaultBrush}">
|
|
||||||
<TextBlock
|
|
||||||
Margin="16"
|
|
||||||
VerticalAlignment="Bottom"
|
|
||||||
Foreground="{ThemeResource TextFillColorTertiaryBrush}"
|
|
||||||
Text="{x:Bind ViewModel.SelectedTemplate.Value.Info.Description, Mode=OneWay}" />
|
|
||||||
</Grid>
|
|
||||||
</Grid>
|
|
||||||
|
|
||||||
<StackPanel Grid.Row="1" Margin="8,0">
|
|
||||||
<TextBlock
|
|
||||||
Margin="0,16,0,8"
|
|
||||||
Style="{StaticResource TitleTextBlockStyle}"
|
|
||||||
Text="{x:Bind ViewModel.SelectedTemplate.Value.Info.Name, Mode=OneWay}" />
|
|
||||||
<TextBlock
|
|
||||||
Margin="0,8,0,16"
|
|
||||||
Foreground="{ThemeResource TextFillColorSecondaryBrush}"
|
|
||||||
Style="{StaticResource SubtitleTextBlockStyle}"
|
|
||||||
Text="Project Settings" />
|
|
||||||
|
|
||||||
<editor:PropertyField Label="Name">
|
|
||||||
<TextBox Text="{x:Bind ViewModel.ProjectName, Mode=TwoWay}" />
|
|
||||||
</editor:PropertyField>
|
|
||||||
<editor:PropertyField Label="Location">
|
|
||||||
<Grid>
|
|
||||||
<Grid.ColumnDefinitions>
|
|
||||||
<ColumnDefinition Width="*" />
|
|
||||||
<ColumnDefinition Width="Auto" />
|
|
||||||
</Grid.ColumnDefinitions>
|
|
||||||
|
|
||||||
<TextBox
|
|
||||||
Grid.Column="0"
|
|
||||||
IsReadOnly="True"
|
|
||||||
Text="{x:Bind ViewModel.ProjectLocation, Mode=TwoWay}" />
|
|
||||||
<Button
|
|
||||||
Grid.Column="1"
|
|
||||||
Margin="4,0,0,0"
|
|
||||||
VerticalAlignment="Stretch"
|
|
||||||
Command="{x:Bind ViewModel.SelectionProjectLocationCommand}">
|
|
||||||
<FontIcon FontSize="16" Glyph="" />
|
|
||||||
</Button>
|
|
||||||
</Grid>
|
|
||||||
</editor:PropertyField>
|
|
||||||
</StackPanel>
|
|
||||||
|
|
||||||
<Grid Grid.Row="2">
|
|
||||||
<Button
|
|
||||||
Width="150"
|
|
||||||
HorizontalAlignment="Right"
|
|
||||||
Command="{x:Bind ViewModel.CreateProjectCommand}"
|
|
||||||
Content="Create"
|
|
||||||
Style="{ThemeResource AccentButtonStyle}" />
|
|
||||||
</Grid>
|
|
||||||
</Grid>
|
|
||||||
|
|
||||||
</Grid>
|
|
||||||
</Page>
|
|
||||||
@@ -1,32 +0,0 @@
|
|||||||
using Ghost.Editor.ViewModels.Pages.Landing;
|
|
||||||
using Microsoft.UI.Xaml.Controls;
|
|
||||||
using Microsoft.UI.Xaml.Navigation;
|
|
||||||
|
|
||||||
namespace Ghost.Editor.View.Pages.Landing;
|
|
||||||
|
|
||||||
internal sealed partial class CreateProjectPage : Page
|
|
||||||
{
|
|
||||||
public CreateProjectViewModel ViewModel
|
|
||||||
{
|
|
||||||
get;
|
|
||||||
}
|
|
||||||
|
|
||||||
public CreateProjectPage()
|
|
||||||
{
|
|
||||||
ViewModel = App.GetService<CreateProjectViewModel>();
|
|
||||||
|
|
||||||
InitializeComponent();
|
|
||||||
}
|
|
||||||
|
|
||||||
protected override void OnNavigatedTo(NavigationEventArgs e)
|
|
||||||
{
|
|
||||||
base.OnNavigatedTo(e);
|
|
||||||
ViewModel.OnNavigatedTo(e.Parameter);
|
|
||||||
}
|
|
||||||
|
|
||||||
protected override void OnNavigatedFrom(NavigationEventArgs e)
|
|
||||||
{
|
|
||||||
base.OnNavigatedFrom(e);
|
|
||||||
ViewModel.OnNavigatedFrom();
|
|
||||||
}
|
|
||||||
}
|
|
||||||
@@ -1,165 +0,0 @@
|
|||||||
<?xml version="1.0" encoding="utf-8" ?>
|
|
||||||
<Page
|
|
||||||
x:Class="Ghost.Editor.View.Pages.Landing.OpenProjectPage"
|
|
||||||
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
|
|
||||||
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
|
|
||||||
xmlns:converters="using:Ghost.Editor.Utilities.Converters"
|
|
||||||
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
|
|
||||||
xmlns:data="using:Ghost.Data.Models"
|
|
||||||
xmlns:interactivity="using:Microsoft.Xaml.Interactivity"
|
|
||||||
xmlns:local="using:Ghost.Editor.View.Pages.Landing"
|
|
||||||
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
|
|
||||||
NavigationCacheMode="Enabled"
|
|
||||||
mc:Ignorable="d">
|
|
||||||
|
|
||||||
<Page.Resources>
|
|
||||||
<converters:GetDirectoryNameConverter x:Key="DirNameConverter" />
|
|
||||||
</Page.Resources>
|
|
||||||
|
|
||||||
<Grid x:Name="MainContainer">
|
|
||||||
<Grid.RowDefinitions>
|
|
||||||
<RowDefinition Height="Auto" />
|
|
||||||
<RowDefinition Height="Auto" />
|
|
||||||
<RowDefinition Height="*" />
|
|
||||||
</Grid.RowDefinitions>
|
|
||||||
|
|
||||||
<Grid Grid.Row="0" Margin="16,4">
|
|
||||||
<TextBlock
|
|
||||||
VerticalAlignment="Center"
|
|
||||||
Style="{StaticResource SubtitleTextBlockStyle}"
|
|
||||||
Text="Projects" />
|
|
||||||
<AutoSuggestBox
|
|
||||||
Width="300"
|
|
||||||
HorizontalAlignment="Right"
|
|
||||||
PlaceholderText="Search project by name"
|
|
||||||
QueryIcon="Find" />
|
|
||||||
</Grid>
|
|
||||||
|
|
||||||
<!-- Header for the ListView -->
|
|
||||||
<Grid Grid.Row="1" Margin="28,16,45,8">
|
|
||||||
<Grid.ColumnDefinitions>
|
|
||||||
<ColumnDefinition Width="*" />
|
|
||||||
<ColumnDefinition Width="200" />
|
|
||||||
<ColumnDefinition Width="165" />
|
|
||||||
</Grid.ColumnDefinitions>
|
|
||||||
|
|
||||||
<TextBlock
|
|
||||||
Grid.Column="0"
|
|
||||||
Style="{StaticResource CaptionTextBlockStyle}"
|
|
||||||
Text="NAME" />
|
|
||||||
<TextBlock
|
|
||||||
Grid.Column="1"
|
|
||||||
HorizontalAlignment="Right"
|
|
||||||
Style="{StaticResource CaptionTextBlockStyle}"
|
|
||||||
Text="LAST OPEN" />
|
|
||||||
<TextBlock
|
|
||||||
Grid.Column="2"
|
|
||||||
HorizontalAlignment="Right"
|
|
||||||
Style="{StaticResource CaptionTextBlockStyle}"
|
|
||||||
Text="ENGINE VERSION" />
|
|
||||||
</Grid>
|
|
||||||
|
|
||||||
<!-- Project ListView -->
|
|
||||||
<Grid
|
|
||||||
Grid.Row="2"
|
|
||||||
Padding="8"
|
|
||||||
AllowDrop="True"
|
|
||||||
DragEnter="ProjectContainer_DragEnter"
|
|
||||||
DragLeave="ProjectContainer_DragLeave"
|
|
||||||
DragOver="ProjectContainer_DragOver"
|
|
||||||
Drop="ProjectContainer_Drop">
|
|
||||||
<ListView
|
|
||||||
Padding="4,8"
|
|
||||||
Background="{ThemeResource CardBackgroundFillColorDefaultBrush}"
|
|
||||||
CornerRadius="{StaticResource OverlayCornerRadius}"
|
|
||||||
IsItemClickEnabled="True"
|
|
||||||
ItemClick="ListView_ItemClick"
|
|
||||||
ItemsSource="{x:Bind ViewModel.projects}"
|
|
||||||
SelectionMode="None">
|
|
||||||
<ListView.ItemTemplate>
|
|
||||||
<DataTemplate x:DataType="data:ProjectMetadataInfo">
|
|
||||||
<Grid Height="64" Padding="4,8">
|
|
||||||
<Grid.ColumnDefinitions>
|
|
||||||
<ColumnDefinition Width="*" />
|
|
||||||
<ColumnDefinition Width="200" />
|
|
||||||
<ColumnDefinition Width="100" />
|
|
||||||
<ColumnDefinition Width="65" />
|
|
||||||
</Grid.ColumnDefinitions>
|
|
||||||
|
|
||||||
<Grid Grid.Column="0" VerticalAlignment="Center">
|
|
||||||
<Grid.RowDefinitions>
|
|
||||||
<RowDefinition Height="Auto" />
|
|
||||||
<RowDefinition Height="Auto" />
|
|
||||||
</Grid.RowDefinitions>
|
|
||||||
|
|
||||||
<TextBlock
|
|
||||||
Grid.Row="0"
|
|
||||||
VerticalAlignment="Center"
|
|
||||||
FontSize="16"
|
|
||||||
Style="{StaticResource SubtitleTextBlockStyle}"
|
|
||||||
Text="{x:Bind Metadata.Name}" />
|
|
||||||
<TextBlock
|
|
||||||
Grid.Row="1"
|
|
||||||
Margin="0,4,0,0"
|
|
||||||
VerticalAlignment="Center"
|
|
||||||
Foreground="{ThemeResource TextFillColorSecondaryBrush}"
|
|
||||||
Style="{StaticResource CaptionTextBlockStyle}"
|
|
||||||
Text="{x:Bind Path, Converter={StaticResource DirNameConverter}}" />
|
|
||||||
</Grid>
|
|
||||||
|
|
||||||
<TextBlock
|
|
||||||
Grid.Column="1"
|
|
||||||
Margin="16,4"
|
|
||||||
HorizontalAlignment="Right"
|
|
||||||
VerticalAlignment="Center"
|
|
||||||
Text="{x:Bind Metadata.LastOpened}" />
|
|
||||||
<TextBlock
|
|
||||||
Grid.Column="2"
|
|
||||||
Margin="16,4"
|
|
||||||
HorizontalAlignment="Right"
|
|
||||||
VerticalAlignment="Center"
|
|
||||||
Text="{x:Bind Metadata.EngineVersion}" />
|
|
||||||
<Button
|
|
||||||
Grid.Column="3"
|
|
||||||
HorizontalAlignment="Right"
|
|
||||||
Background="Transparent"
|
|
||||||
BorderThickness="0">
|
|
||||||
<FontIcon Glyph="" />
|
|
||||||
</Button>
|
|
||||||
</Grid>
|
|
||||||
</DataTemplate>
|
|
||||||
</ListView.ItemTemplate>
|
|
||||||
</ListView>
|
|
||||||
|
|
||||||
<!-- Drag Visual -->
|
|
||||||
<Grid
|
|
||||||
x:Name="DragVisual"
|
|
||||||
HorizontalAlignment="Stretch"
|
|
||||||
VerticalAlignment="Stretch"
|
|
||||||
Background="{ThemeResource CardStrokeColorDefaultBrush}"
|
|
||||||
BorderBrush="{ThemeResource ControlStrongStrokeColorDefaultBrush}"
|
|
||||||
BorderThickness="2"
|
|
||||||
CornerRadius="{StaticResource OverlayCornerRadius}"
|
|
||||||
Visibility="{x:Bind ViewModel.DragVisibility, Mode=OneWay}">
|
|
||||||
<TextBlock
|
|
||||||
HorizontalAlignment="Center"
|
|
||||||
VerticalAlignment="Center"
|
|
||||||
Foreground="{ThemeResource TextFillColorSecondaryBrush}"
|
|
||||||
Style="{StaticResource TitleTextBlockStyle}"
|
|
||||||
Text="Drage Project Folder Here" />
|
|
||||||
</Grid>
|
|
||||||
</Grid>
|
|
||||||
|
|
||||||
<!-- Empty Place Holder -->
|
|
||||||
<Grid
|
|
||||||
x:Name="EmptyPlaceHolder"
|
|
||||||
Grid.Row="2"
|
|
||||||
Visibility="{x:Bind ViewModel.EmptyVisibility, Mode=OneWay}">
|
|
||||||
<TextBlock
|
|
||||||
HorizontalAlignment="Center"
|
|
||||||
VerticalAlignment="Center"
|
|
||||||
Style="{StaticResource TitleTextBlockStyle}"
|
|
||||||
Text="No projects found" />
|
|
||||||
</Grid>
|
|
||||||
</Grid>
|
|
||||||
</Page>
|
|
||||||
@@ -1,72 +0,0 @@
|
|||||||
using Ghost.Data.Models;
|
|
||||||
using Ghost.Editor.ViewModels.Pages.Landing;
|
|
||||||
using Microsoft.UI.Xaml;
|
|
||||||
using Microsoft.UI.Xaml.Controls;
|
|
||||||
using Microsoft.UI.Xaml.Navigation;
|
|
||||||
using Windows.ApplicationModel.DataTransfer;
|
|
||||||
|
|
||||||
namespace Ghost.Editor.View.Pages.Landing;
|
|
||||||
|
|
||||||
internal sealed partial class OpenProjectPage : Page
|
|
||||||
{
|
|
||||||
public OpenProjectViewModel ViewModel
|
|
||||||
{
|
|
||||||
get;
|
|
||||||
}
|
|
||||||
|
|
||||||
public OpenProjectPage()
|
|
||||||
{
|
|
||||||
ViewModel = App.GetService<OpenProjectViewModel>();
|
|
||||||
InitializeComponent();
|
|
||||||
}
|
|
||||||
|
|
||||||
protected override void OnNavigatedTo(NavigationEventArgs e)
|
|
||||||
{
|
|
||||||
base.OnNavigatedTo(e);
|
|
||||||
ViewModel.OnNavigatedTo(e.Parameter);
|
|
||||||
}
|
|
||||||
|
|
||||||
protected override void OnNavigatedFrom(NavigationEventArgs e)
|
|
||||||
{
|
|
||||||
base.OnNavigatedFrom(e);
|
|
||||||
ViewModel.OnNavigatedFrom();
|
|
||||||
}
|
|
||||||
|
|
||||||
private void ProjectContainer_DragEnter(object sender, DragEventArgs e)
|
|
||||||
{
|
|
||||||
ViewModel.DragVisibility = Visibility.Visible;
|
|
||||||
ViewModel.EmptyVisibility = Visibility.Collapsed;
|
|
||||||
}
|
|
||||||
|
|
||||||
private void ProjectContainer_DragLeave(object sender, DragEventArgs e)
|
|
||||||
{
|
|
||||||
ViewModel.DragVisibility = Visibility.Collapsed;
|
|
||||||
ViewModel.UpdateEmptyPlaceHolderVisibility();
|
|
||||||
}
|
|
||||||
|
|
||||||
private void ProjectContainer_DragOver(object sender, DragEventArgs e)
|
|
||||||
{
|
|
||||||
if (e.DataView.Contains(StandardDataFormats.StorageItems))
|
|
||||||
{
|
|
||||||
e.AcceptedOperation = DataPackageOperation.Link;
|
|
||||||
}
|
|
||||||
else
|
|
||||||
{
|
|
||||||
e.AcceptedOperation = DataPackageOperation.None;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
private async void ProjectContainer_Drop(object sender, DragEventArgs e)
|
|
||||||
{
|
|
||||||
await ViewModel.ContentDrop(e.DataView);
|
|
||||||
}
|
|
||||||
|
|
||||||
private async void ListView_ItemClick(object sender, ItemClickEventArgs e)
|
|
||||||
{
|
|
||||||
if (e.ClickedItem is ProjectMetadataInfo project)
|
|
||||||
{
|
|
||||||
await Task.Yield();
|
|
||||||
await ViewModel.OpenProjectAsync(project);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
@@ -1,76 +0,0 @@
|
|||||||
<?xml version="1.0" encoding="utf-8" ?>
|
|
||||||
<winex:WindowEx
|
|
||||||
x:Class="Ghost.Editor.View.Windows.LandingWindow"
|
|
||||||
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
|
|
||||||
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
|
|
||||||
xmlns:behaviors="using:CommunityToolkit.WinUI.Behaviors"
|
|
||||||
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
|
|
||||||
xmlns:interactivity="using:Microsoft.Xaml.Interactivity"
|
|
||||||
xmlns:local="using:Ghost.Editor.View.Windows"
|
|
||||||
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
|
|
||||||
xmlns:winex="using:WinUIEx"
|
|
||||||
Activated="WindowEx_Activated"
|
|
||||||
Closed="WindowEx_Closed"
|
|
||||||
IsResizable="False"
|
|
||||||
mc:Ignorable="d">
|
|
||||||
|
|
||||||
<Window.SystemBackdrop>
|
|
||||||
<MicaBackdrop />
|
|
||||||
</Window.SystemBackdrop>
|
|
||||||
|
|
||||||
<Grid>
|
|
||||||
<Grid.RowDefinitions>
|
|
||||||
<RowDefinition Height="32" />
|
|
||||||
<RowDefinition Height="*" />
|
|
||||||
</Grid.RowDefinitions>
|
|
||||||
|
|
||||||
<Grid Grid.Row="0">
|
|
||||||
<TextBlock
|
|
||||||
Margin="24,0,0,0"
|
|
||||||
VerticalAlignment="Bottom"
|
|
||||||
Style="{StaticResource BodyTextBlockStyle}"
|
|
||||||
Text="Ghost Engine" />
|
|
||||||
</Grid>
|
|
||||||
|
|
||||||
<Grid Grid.Row="1" Padding="24,0,24,18">
|
|
||||||
<Grid.RowDefinitions>
|
|
||||||
<RowDefinition Height="Auto" />
|
|
||||||
<RowDefinition Height="*" />
|
|
||||||
</Grid.RowDefinitions>
|
|
||||||
|
|
||||||
<SelectorBar
|
|
||||||
Grid.Row="0"
|
|
||||||
HorizontalAlignment="Right"
|
|
||||||
SelectionChanged="SelectorBar_SelectionChanged">
|
|
||||||
<SelectorBarItem IsSelected="True" Text="Open">
|
|
||||||
<SelectorBarItem.Icon>
|
|
||||||
<FontIcon Glyph="" />
|
|
||||||
</SelectorBarItem.Icon>
|
|
||||||
</SelectorBarItem>
|
|
||||||
<SelectorBarItem Text="Create">
|
|
||||||
<SelectorBarItem.Icon>
|
|
||||||
<FontIcon Glyph="" />
|
|
||||||
</SelectorBarItem.Icon>
|
|
||||||
</SelectorBarItem>
|
|
||||||
</SelectorBar>
|
|
||||||
|
|
||||||
<Frame
|
|
||||||
x:Name="ContentFrame"
|
|
||||||
Grid.Row="1"
|
|
||||||
Padding="8"
|
|
||||||
CacheMode="BitmapCache"
|
|
||||||
CacheSize="10" />
|
|
||||||
</Grid>
|
|
||||||
|
|
||||||
<Grid Grid.Row="1" Padding="16">
|
|
||||||
<InfoBar
|
|
||||||
x:Name="InfoBar"
|
|
||||||
HorizontalAlignment="Right"
|
|
||||||
VerticalAlignment="Bottom">
|
|
||||||
<interactivity:Interaction.Behaviors>
|
|
||||||
<behaviors:StackedNotificationsBehavior x:Name="NotificationQueue" />
|
|
||||||
</interactivity:Interaction.Behaviors>
|
|
||||||
</InfoBar>
|
|
||||||
</Grid>
|
|
||||||
</Grid>
|
|
||||||
</winex:WindowEx>
|
|
||||||
@@ -1,59 +0,0 @@
|
|||||||
using Ghost.Data.Resources;
|
|
||||||
using Ghost.Editor.Core.Notifications;
|
|
||||||
using Ghost.Editor.View.Pages.Landing;
|
|
||||||
using Ghost.Engine.Resources;
|
|
||||||
using Microsoft.UI.Xaml.Controls;
|
|
||||||
using Microsoft.UI.Xaml.Media.Animation;
|
|
||||||
using WinUIEx;
|
|
||||||
|
|
||||||
namespace Ghost.Editor.View.Windows;
|
|
||||||
|
|
||||||
internal sealed partial class LandingWindow : WindowEx
|
|
||||||
{
|
|
||||||
private readonly NotificationService _notificationService;
|
|
||||||
|
|
||||||
private int _previousSelectedIndex;
|
|
||||||
|
|
||||||
public LandingWindow()
|
|
||||||
{
|
|
||||||
_notificationService = (NotificationService)App.GetService<INotificationService>();
|
|
||||||
|
|
||||||
AppWindow.SetIcon(AssetsPath.s_appIconPath);
|
|
||||||
Title = EngineData.ENGINE_NAME;
|
|
||||||
|
|
||||||
InitializeComponent();
|
|
||||||
|
|
||||||
this.SetWindowSize(1000, 750);
|
|
||||||
this.CenterOnScreen();
|
|
||||||
|
|
||||||
ExtendsContentIntoTitleBar = true;
|
|
||||||
}
|
|
||||||
|
|
||||||
private void WindowEx_Activated(object sender, Microsoft.UI.Xaml.WindowActivatedEventArgs args)
|
|
||||||
{
|
|
||||||
_notificationService.SetReference(InfoBar, NotificationQueue);
|
|
||||||
}
|
|
||||||
|
|
||||||
private void WindowEx_Closed(object sender, Microsoft.UI.Xaml.WindowEventArgs args)
|
|
||||||
{
|
|
||||||
_notificationService.ClearReference();
|
|
||||||
}
|
|
||||||
|
|
||||||
private void SelectorBar_SelectionChanged(SelectorBar sender, SelectorBarSelectionChangedEventArgs e)
|
|
||||||
{
|
|
||||||
var selectedItem = sender.SelectedItem;
|
|
||||||
var currentSelectedIndex = sender.Items.IndexOf(selectedItem);
|
|
||||||
var pageType = currentSelectedIndex switch
|
|
||||||
{
|
|
||||||
1 => typeof(CreateProjectPage),
|
|
||||||
_ => typeof(OpenProjectPage),
|
|
||||||
};
|
|
||||||
|
|
||||||
var slideNavigationTransitionEffect = currentSelectedIndex - _previousSelectedIndex > 0 ?
|
|
||||||
SlideNavigationTransitionEffect.FromRight : SlideNavigationTransitionEffect.FromLeft;
|
|
||||||
|
|
||||||
ContentFrame.Navigate(pageType, null, new SlideNavigationTransitionInfo() { Effect = slideNavigationTransitionEffect });
|
|
||||||
|
|
||||||
_previousSelectedIndex = currentSelectedIndex;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
@@ -1,91 +0,0 @@
|
|||||||
using CommunityToolkit.Mvvm.ComponentModel;
|
|
||||||
using CommunityToolkit.Mvvm.Input;
|
|
||||||
using Ghost.Data.Models;
|
|
||||||
using Ghost.Data.Services;
|
|
||||||
using Ghost.Editor.Core.AppState;
|
|
||||||
using Ghost.Editor.Core.Contracts;
|
|
||||||
using Ghost.Editor.Core.Notifications;
|
|
||||||
using Ghost.Editor.Utilities;
|
|
||||||
using Ghost.Engine.Resources;
|
|
||||||
using System.Collections.ObjectModel;
|
|
||||||
|
|
||||||
namespace Ghost.Editor.ViewModels.Pages.Landing;
|
|
||||||
|
|
||||||
internal partial class CreateProjectViewModel(INotificationService notificationService, ProjectService projectService, AppStateMachine stateService) : ObservableObject, INavigationAware
|
|
||||||
{
|
|
||||||
public ObservableCollection<TemplateData> templates = new();
|
|
||||||
|
|
||||||
[ObservableProperty]
|
|
||||||
public partial TemplateData? SelectedTemplate
|
|
||||||
{
|
|
||||||
get;
|
|
||||||
set;
|
|
||||||
}
|
|
||||||
|
|
||||||
[ObservableProperty]
|
|
||||||
public partial string? ProjectName
|
|
||||||
{
|
|
||||||
get;
|
|
||||||
set;
|
|
||||||
}
|
|
||||||
|
|
||||||
[ObservableProperty]
|
|
||||||
public partial string? ProjectLocation
|
|
||||||
{
|
|
||||||
get;
|
|
||||||
set;
|
|
||||||
}
|
|
||||||
|
|
||||||
public async void OnNavigatedTo(object? parameter)
|
|
||||||
{
|
|
||||||
templates.Clear();
|
|
||||||
await foreach (var (path, info) in ProjectService.GetProjectTemplatesAsync())
|
|
||||||
{
|
|
||||||
templates.Add(new(path, info));
|
|
||||||
}
|
|
||||||
|
|
||||||
SelectedTemplate = templates.FirstOrDefault();
|
|
||||||
}
|
|
||||||
|
|
||||||
public void OnNavigatedFrom()
|
|
||||||
{
|
|
||||||
}
|
|
||||||
|
|
||||||
[RelayCommand]
|
|
||||||
private async Task SelectionProjectLocation()
|
|
||||||
{
|
|
||||||
var folder = await SystemUtilities.OpenFolderPickerAsync();
|
|
||||||
if (folder != null)
|
|
||||||
{
|
|
||||||
ProjectLocation = folder.Path;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
[RelayCommand]
|
|
||||||
private async Task CreateProject()
|
|
||||||
{
|
|
||||||
if (string.IsNullOrWhiteSpace(ProjectName)
|
|
||||||
|| !Directory.Exists(ProjectLocation)
|
|
||||||
|| !SelectedTemplate.HasValue)
|
|
||||||
{
|
|
||||||
notificationService.ShowNotification("Incorrect project info", MessageType.Error);
|
|
||||||
return;
|
|
||||||
}
|
|
||||||
|
|
||||||
var result = await projectService.CreateProjectAsync(ProjectName, ProjectLocation, EngineData.EngineVersion, SelectedTemplate.Value.directory);
|
|
||||||
if (result.IsFailure)
|
|
||||||
{
|
|
||||||
notificationService.ShowNotification(result.Message, MessageType.Error);
|
|
||||||
return;
|
|
||||||
}
|
|
||||||
|
|
||||||
try
|
|
||||||
{
|
|
||||||
await stateService.TransitionToAsync(StateKey.EngineEditor, result.Value);
|
|
||||||
}
|
|
||||||
catch (Exception e)
|
|
||||||
{
|
|
||||||
notificationService.ShowNotification($"Failed to load project: {e.Message}", MessageType.Error);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
@@ -1,106 +0,0 @@
|
|||||||
using CommunityToolkit.Mvvm.ComponentModel;
|
|
||||||
using Ghost.Data.Models;
|
|
||||||
using Ghost.Data.Services;
|
|
||||||
using Ghost.Editor.Core.AppState;
|
|
||||||
using Ghost.Editor.Core.Contracts;
|
|
||||||
using Ghost.Editor.Core.Notifications;
|
|
||||||
using Microsoft.UI.Xaml;
|
|
||||||
using System.Collections.ObjectModel;
|
|
||||||
using Windows.ApplicationModel.DataTransfer;
|
|
||||||
using Windows.Storage;
|
|
||||||
|
|
||||||
namespace Ghost.Editor.ViewModels.Pages.Landing;
|
|
||||||
|
|
||||||
internal partial class OpenProjectViewModel(ProjectService projectService, INotificationService _notificationService, AppStateMachine _stateService) : ObservableObject, INavigationAware
|
|
||||||
{
|
|
||||||
public readonly ObservableCollection<ProjectMetadataInfo> projects = new();
|
|
||||||
|
|
||||||
[ObservableProperty]
|
|
||||||
public partial Visibility EmptyVisibility
|
|
||||||
{
|
|
||||||
get;
|
|
||||||
set;
|
|
||||||
}
|
|
||||||
|
|
||||||
[ObservableProperty]
|
|
||||||
public partial Visibility DragVisibility
|
|
||||||
{
|
|
||||||
get;
|
|
||||||
set;
|
|
||||||
}
|
|
||||||
|
|
||||||
public void UpdateEmptyPlaceHolderVisibility()
|
|
||||||
{
|
|
||||||
EmptyVisibility = projects.Count == 0 ? Visibility.Visible : Visibility.Collapsed;
|
|
||||||
}
|
|
||||||
|
|
||||||
public async void OnNavigatedTo(object? parameter)
|
|
||||||
{
|
|
||||||
await foreach (var projectInfo in projectService.GetAllProjectAsync())
|
|
||||||
{
|
|
||||||
var metadata = await ProjectService.LoadMetadataAsync(projectInfo.MetadataPath);
|
|
||||||
if (metadata == null)
|
|
||||||
{
|
|
||||||
continue;
|
|
||||||
}
|
|
||||||
|
|
||||||
projects.Add(new(projectInfo.MetadataPath, metadata));
|
|
||||||
}
|
|
||||||
|
|
||||||
UpdateEmptyPlaceHolderVisibility();
|
|
||||||
DragVisibility = Visibility.Collapsed;
|
|
||||||
}
|
|
||||||
|
|
||||||
public void OnNavigatedFrom()
|
|
||||||
{
|
|
||||||
projects.Clear();
|
|
||||||
}
|
|
||||||
|
|
||||||
public async Task ContentDrop(DataPackageView dataView)
|
|
||||||
{
|
|
||||||
var errorMessage = string.Empty;
|
|
||||||
if (dataView.Contains(StandardDataFormats.StorageItems))
|
|
||||||
{
|
|
||||||
var items = await dataView.GetStorageItemsAsync();
|
|
||||||
var rootFolder = items.OfType<StorageFolder>().FirstOrDefault();
|
|
||||||
if (rootFolder != null)
|
|
||||||
{
|
|
||||||
var result = await projectService.AddProjectFromDirectoryAsync(rootFolder.Path);
|
|
||||||
if (result.IsSuccess)
|
|
||||||
{
|
|
||||||
projects.Add(result.Value);
|
|
||||||
goto CloseDropPanel;
|
|
||||||
}
|
|
||||||
else
|
|
||||||
{
|
|
||||||
errorMessage = result.Message;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
else
|
|
||||||
{
|
|
||||||
errorMessage = "Unsupported data format. Please drop a folder containing a project.";
|
|
||||||
}
|
|
||||||
|
|
||||||
_notificationService.ShowNotification(errorMessage, MessageType.Error);
|
|
||||||
|
|
||||||
CloseDropPanel:
|
|
||||||
DragVisibility = Visibility.Collapsed;
|
|
||||||
UpdateEmptyPlaceHolderVisibility();
|
|
||||||
}
|
|
||||||
|
|
||||||
public async Task OpenProjectAsync(ProjectMetadataInfo project)
|
|
||||||
{
|
|
||||||
try
|
|
||||||
{
|
|
||||||
project.Metadata.LastOpened = DateTime.Now;
|
|
||||||
await ProjectService.CreateMetadataFileAsync(project.Path, project.Metadata);
|
|
||||||
|
|
||||||
await _stateService.TransitionToAsync(StateKey.EngineEditor, project);
|
|
||||||
}
|
|
||||||
catch (Exception e)
|
|
||||||
{
|
|
||||||
_notificationService.ShowNotification($"Failed to load project: {e.Message}", MessageType.Error);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
@@ -1,13 +0,0 @@
|
|||||||
using CommunityToolkit.Mvvm.ComponentModel;
|
|
||||||
using Ghost.Data.Models;
|
|
||||||
using Ghost.Data.Services;
|
|
||||||
using Ghost.Engine.Resources;
|
|
||||||
|
|
||||||
namespace Ghost.Editor.ViewModels.Windows;
|
|
||||||
|
|
||||||
internal partial class EngineEditorViewModel : ObservableRecipient
|
|
||||||
{
|
|
||||||
public string engineVersionDescriptor = $"{EngineData.ENGINE_NAME} - {EngineData.EngineVersion}";
|
|
||||||
|
|
||||||
public ProjectMetadataInfo CurrentProject => ProjectService.CurrentProject;
|
|
||||||
}
|
|
||||||
@@ -1,16 +0,0 @@
|
|||||||
<Project Sdk="Microsoft.NET.Sdk">
|
|
||||||
|
|
||||||
<PropertyGroup>
|
|
||||||
<OutputType>Exe</OutputType>
|
|
||||||
<TargetFramework>net10.0</TargetFramework>
|
|
||||||
<ImplicitUsings>enable</ImplicitUsings>
|
|
||||||
<Nullable>enable</Nullable>
|
|
||||||
<AllowUnsafeBlocks>True</AllowUnsafeBlocks>
|
|
||||||
</PropertyGroup>
|
|
||||||
|
|
||||||
<ItemGroup>
|
|
||||||
<ProjectReference Include="..\Ghost.Entities\Ghost.Entities.csproj" />
|
|
||||||
<ProjectReference Include="..\Ghost.Test.Core\Ghost.Test.Core.csproj" />
|
|
||||||
</ItemGroup>
|
|
||||||
|
|
||||||
</Project>
|
|
||||||
@@ -1,7 +0,0 @@
|
|||||||
using Ghost.Entities.Test;
|
|
||||||
using Ghost.Test.Core;
|
|
||||||
using Misaki.HighPerformance.LowLevel.Buffer;
|
|
||||||
|
|
||||||
AllocationManager.EnableDebugLayer();
|
|
||||||
TestRunner.Run<SerializationTest>();
|
|
||||||
AllocationManager.Dispose();
|
|
||||||
@@ -1,176 +0,0 @@
|
|||||||
#if false
|
|
||||||
using Misaki.HighPerformance.LowLevel.Buffer;
|
|
||||||
using Misaki.HighPerformance.LowLevel.Collections;
|
|
||||||
using Misaki.HighPerformance.LowLevel.Utilities;
|
|
||||||
|
|
||||||
namespace Ghost.Entities;
|
|
||||||
|
|
||||||
public interface ISharedComponent
|
|
||||||
{
|
|
||||||
}
|
|
||||||
|
|
||||||
internal unsafe sealed class SharedComponentStore : IDisposable
|
|
||||||
{
|
|
||||||
private struct EntryInfo
|
|
||||||
{
|
|
||||||
public int RefCount;
|
|
||||||
public int HashCode;
|
|
||||||
public int Version;
|
|
||||||
public int NextFree; // free-list linkage (index)
|
|
||||||
}
|
|
||||||
|
|
||||||
private struct TypeStore : IDisposable
|
|
||||||
{
|
|
||||||
public int TypeSize;
|
|
||||||
public UnsafeList<byte> Data; // raw bytes, stride = TypeSize
|
|
||||||
public UnsafeList<EntryInfo> Infos; // parallel to Data entries (Entry 0 reserved)
|
|
||||||
public UnsafeHashMap<long, int> HashLookup; // (hashKey) -> entryIndex
|
|
||||||
public int FreeListHead; // head index, 0 means none (we'll use Infos[0].NextFree too if you prefer)
|
|
||||||
public int VersionCounter;
|
|
||||||
|
|
||||||
public void Dispose()
|
|
||||||
{
|
|
||||||
Data.Dispose();
|
|
||||||
Infos.Dispose();
|
|
||||||
HashLookup.Dispose();
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
private readonly UnsafeHashMap<int, TypeStore> _perType; // componentTypeId -> TypeStore
|
|
||||||
|
|
||||||
public SharedComponentStore(int initialCapacity = 16)
|
|
||||||
{
|
|
||||||
_perType = new UnsafeHashMap<int, TypeStore>(initialCapacity, Allocator.Persistent);
|
|
||||||
}
|
|
||||||
|
|
||||||
public void Dispose()
|
|
||||||
{
|
|
||||||
foreach (var kvp in _perType)
|
|
||||||
{
|
|
||||||
kvp.Value.Dispose();
|
|
||||||
}
|
|
||||||
|
|
||||||
_perType.Dispose();
|
|
||||||
}
|
|
||||||
|
|
||||||
public int InsertOrGet(int componentTypeId, int typeSize, void* data, int hashCode)
|
|
||||||
{
|
|
||||||
// Reserve index 0 for "default"
|
|
||||||
if (data == null)
|
|
||||||
{
|
|
||||||
return 0;
|
|
||||||
}
|
|
||||||
|
|
||||||
ref var store = ref GetOrCreateTypeStore(componentTypeId, typeSize);
|
|
||||||
|
|
||||||
// Combine (typeId, hash) into a single key; collisions handled by memcmp below.
|
|
||||||
var key = ((long)componentTypeId << 32) ^ (uint)hashCode;
|
|
||||||
|
|
||||||
if (store.HashLookup.TryGetValue(key, out var existingIndex))
|
|
||||||
{
|
|
||||||
var existingPtr = (byte*)store.Data.GetUnsafePtr() + (existingIndex * store.TypeSize);
|
|
||||||
if (new Span<byte>(existingPtr, store.TypeSize).SequenceEqual(new Span<byte>(data, store.TypeSize)))
|
|
||||||
{
|
|
||||||
((EntryInfo*)store.Infos.GetUnsafePtr())[existingIndex].RefCount++;
|
|
||||||
return existingIndex;
|
|
||||||
}
|
|
||||||
// If collision: fall through to insert (you may want a secondary structure).
|
|
||||||
}
|
|
||||||
|
|
||||||
int index = AllocateEntry(ref store);
|
|
||||||
|
|
||||||
var dst = (byte*)store.Data.GetUnsafePtr() + (index * store.TypeSize);
|
|
||||||
MemoryUtility.MemCpy(dst, data, (nuint)store.TypeSize);
|
|
||||||
|
|
||||||
store.Infos[index] = new EntryInfo
|
|
||||||
{
|
|
||||||
RefCount = 1,
|
|
||||||
HashCode = hashCode,
|
|
||||||
Version = ++store.VersionCounter,
|
|
||||||
NextFree = -1
|
|
||||||
};
|
|
||||||
|
|
||||||
store.HashLookup[key] = index;
|
|
||||||
return index;
|
|
||||||
}
|
|
||||||
|
|
||||||
public void AddRef(int componentTypeId, int index)
|
|
||||||
{
|
|
||||||
if (index == 0) return;
|
|
||||||
ref var store = ref _perType[componentTypeId];
|
|
||||||
store.Infos[index].RefCount++;
|
|
||||||
}
|
|
||||||
|
|
||||||
public void Release(int componentTypeId, int index)
|
|
||||||
{
|
|
||||||
if (index == 0) return;
|
|
||||||
ref var store = ref _perType.GetValueByKey(componentTypeId);
|
|
||||||
|
|
||||||
ref var info = ref store.Infos.Ptr[index];
|
|
||||||
info.RefCount--;
|
|
||||||
if (info.RefCount > 0) return;
|
|
||||||
|
|
||||||
// Remove from hash lookup (best-effort; collisions require more robust handling)
|
|
||||||
long key = ((long)componentTypeId << 32) ^ (uint)info.HashCode;
|
|
||||||
store.HashLookup.Remove(key);
|
|
||||||
|
|
||||||
// Push to free-list
|
|
||||||
info.NextFree = store.FreeListHead;
|
|
||||||
store.FreeListHead = index;
|
|
||||||
}
|
|
||||||
|
|
||||||
public void* GetDataPtr(int componentTypeId, int index)
|
|
||||||
{
|
|
||||||
if (index == 0) return null;
|
|
||||||
ref var store = ref _perType.GetValueByKey(componentTypeId);
|
|
||||||
return (byte*)store.Data.Ptr + (index * store.TypeSize);
|
|
||||||
}
|
|
||||||
|
|
||||||
private ref TypeStore GetOrCreateTypeStore(int componentTypeId, int typeSize)
|
|
||||||
{
|
|
||||||
if (_perType.TryGetValue(componentTypeId, out var existing))
|
|
||||||
{
|
|
||||||
// UnsafeHashMap returns by value in some implementations; you may need a different pattern here.
|
|
||||||
// Adjust to your container API (e.g., TryGetValueRef).
|
|
||||||
}
|
|
||||||
|
|
||||||
var store = new TypeStore
|
|
||||||
{
|
|
||||||
TypeSize = typeSize,
|
|
||||||
Data = new UnsafeList<byte>(typeSize * 16, Allocator.Persistent),
|
|
||||||
Infos = new UnsafeList<EntryInfo>(16, Allocator.Persistent),
|
|
||||||
HashLookup = new UnsafeHashMap<long, int>(16, Allocator.Persistent),
|
|
||||||
FreeListHead = 0,
|
|
||||||
VersionCounter = 0
|
|
||||||
};
|
|
||||||
|
|
||||||
// Create reserved default entry at index 0
|
|
||||||
store.Data.Resize(typeSize); // one element worth of bytes
|
|
||||||
store.Infos.Add(new EntryInfo { RefCount = int.MaxValue, HashCode = 0, Version = 0, NextFree = -1 });
|
|
||||||
|
|
||||||
_perType.Add(componentTypeId, store);
|
|
||||||
// NOTE: returning a ref requires a "get ref" API; adjust to your UnsafeHashMap capabilities.
|
|
||||||
return ref _perType.GetValueByKey(componentTypeId);
|
|
||||||
}
|
|
||||||
|
|
||||||
private static int AllocateEntry(ref TypeStore store)
|
|
||||||
{
|
|
||||||
if (store.FreeListHead != 0)
|
|
||||||
{
|
|
||||||
int idx = store.FreeListHead;
|
|
||||||
store.FreeListHead = store.Infos[idx].NextFree;
|
|
||||||
store.Infos[idx].NextFree = -1;
|
|
||||||
return idx;
|
|
||||||
}
|
|
||||||
|
|
||||||
int newIndex = store.Infos.Count;
|
|
||||||
store.Infos.Add(default);
|
|
||||||
|
|
||||||
int newByteCount = (newIndex + 1) * store.TypeSize;
|
|
||||||
store.Data.Resize(newByteCount);
|
|
||||||
|
|
||||||
return newIndex;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
#endif
|
|
||||||
@@ -1,5 +0,0 @@
|
|||||||
namespace Ghost.Graphics.Core;
|
|
||||||
|
|
||||||
public class Camera
|
|
||||||
{
|
|
||||||
}
|
|
||||||
@@ -1,145 +0,0 @@
|
|||||||
using Misaki.HighPerformance.LowLevel.Collections;
|
|
||||||
using Misaki.HighPerformance.Mathematics;
|
|
||||||
using System.Drawing;
|
|
||||||
using System.Runtime.InteropServices;
|
|
||||||
using TerraFX.Interop.DirectX;
|
|
||||||
|
|
||||||
namespace Ghost.Graphics.Core;
|
|
||||||
|
|
||||||
/// <summary>
|
|
||||||
/// Represents a color with 4 bytes components.
|
|
||||||
/// </summary>
|
|
||||||
[StructLayout(LayoutKind.Sequential, Size = 4)]
|
|
||||||
public struct Color32 : IEquatable<Color32>
|
|
||||||
{
|
|
||||||
public byte r;
|
|
||||||
public byte g;
|
|
||||||
public byte b;
|
|
||||||
public byte a;
|
|
||||||
|
|
||||||
public Color32(byte r, byte g, byte b, byte a)
|
|
||||||
{
|
|
||||||
this.r = r;
|
|
||||||
this.g = g;
|
|
||||||
this.b = b;
|
|
||||||
this.a = a;
|
|
||||||
}
|
|
||||||
|
|
||||||
public Color32(Color color)
|
|
||||||
: this(color.R, color.G, color.B, color.A)
|
|
||||||
{
|
|
||||||
}
|
|
||||||
|
|
||||||
public Color32(Color128 color128)
|
|
||||||
: this((byte)(color128.r * 255.0f), (byte)(color128.g * 255.0f), (byte)(color128.b * 255.0f), (byte)(color128.a * 255.0f))
|
|
||||||
{
|
|
||||||
}
|
|
||||||
|
|
||||||
public readonly bool Equals(Color32 other)
|
|
||||||
{
|
|
||||||
return r == other.r && g == other.g && b == other.b && a == other.a;
|
|
||||||
}
|
|
||||||
|
|
||||||
public override readonly bool Equals(object? obj)
|
|
||||||
{
|
|
||||||
return obj is Color32 color && Equals(color);
|
|
||||||
}
|
|
||||||
|
|
||||||
public override readonly int GetHashCode()
|
|
||||||
{
|
|
||||||
return HashCode.Combine(r, g, b, a);
|
|
||||||
}
|
|
||||||
|
|
||||||
public static bool operator ==(Color32 left, Color32 right)
|
|
||||||
{
|
|
||||||
return left.Equals(right);
|
|
||||||
}
|
|
||||||
|
|
||||||
public static bool operator !=(Color32 left, Color32 right)
|
|
||||||
{
|
|
||||||
return !(left == right);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
/// <summary>
|
|
||||||
/// Represents a color with 16 bytes components.
|
|
||||||
/// </summary>
|
|
||||||
[StructLayout(LayoutKind.Sequential, Size = 16)]
|
|
||||||
public struct Color128 : IEquatable<Color128>
|
|
||||||
{
|
|
||||||
public float r;
|
|
||||||
public float g;
|
|
||||||
public float b;
|
|
||||||
public float a;
|
|
||||||
|
|
||||||
public Color128(float r, float g, float b, float a)
|
|
||||||
{
|
|
||||||
this.r = r;
|
|
||||||
this.g = g;
|
|
||||||
this.b = b;
|
|
||||||
this.a = a;
|
|
||||||
}
|
|
||||||
|
|
||||||
public Color128(Color color)
|
|
||||||
: this(color.R / 255.0f, color.G / 255.0f, color.B / 255.0f, color.A / 255.0f)
|
|
||||||
{
|
|
||||||
}
|
|
||||||
|
|
||||||
public Color128(Color32 color32)
|
|
||||||
: this(color32.r / 255.0f, color32.g / 255.0f, color32.b / 255.0f, color32.a / 255.0f)
|
|
||||||
{
|
|
||||||
}
|
|
||||||
|
|
||||||
public Color128(float4 v)
|
|
||||||
: this(v.x, v.y, v.z, v.w)
|
|
||||||
{
|
|
||||||
}
|
|
||||||
|
|
||||||
public readonly bool Equals(Color128 other)
|
|
||||||
{
|
|
||||||
return r.Equals(other.r) && g.Equals(other.g) && b.Equals(other.b) && a.Equals(other.a);
|
|
||||||
}
|
|
||||||
|
|
||||||
public override readonly bool Equals(object? obj)
|
|
||||||
{
|
|
||||||
return obj is Color128 color && Equals(color);
|
|
||||||
}
|
|
||||||
|
|
||||||
public readonly override int GetHashCode()
|
|
||||||
{
|
|
||||||
return HashCode.Combine(r, g, b, a);
|
|
||||||
}
|
|
||||||
|
|
||||||
public static bool operator ==(Color128 left, Color128 right)
|
|
||||||
{
|
|
||||||
return left.Equals(right);
|
|
||||||
}
|
|
||||||
|
|
||||||
public static bool operator !=(Color128 left, Color128 right)
|
|
||||||
{
|
|
||||||
return !(left == right);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
|
|
||||||
[StructLayout(LayoutKind.Sequential)]
|
|
||||||
public struct Vertex
|
|
||||||
{
|
|
||||||
public static class Semantic
|
|
||||||
{
|
|
||||||
public const DXGI_FORMAT ALIGNED_FORMAT = DXGI_FORMAT.DXGI_FORMAT_R32G32B32A32_FLOAT;
|
|
||||||
public const int COUNT = 5;
|
|
||||||
|
|
||||||
public static readonly FixedText32 Position = new("POSITION");
|
|
||||||
public static readonly FixedText32 Normal = new("NORMAL");
|
|
||||||
public static readonly FixedText32 Tangent = new("TANGENT");
|
|
||||||
public static readonly FixedText32 Uv = new("TEXCOORD");
|
|
||||||
public static readonly FixedText32 Color = new("COLOR");
|
|
||||||
}
|
|
||||||
|
|
||||||
public float4 position;
|
|
||||||
public float4 normal;
|
|
||||||
public float4 tangent;
|
|
||||||
public float4 uv;
|
|
||||||
public Color128 color;
|
|
||||||
}
|
|
||||||
@@ -1,139 +0,0 @@
|
|||||||
using Ghost.Core;
|
|
||||||
using Ghost.Graphics.RHI;
|
|
||||||
using Ghost.Graphics.Core;
|
|
||||||
using Ghost.Graphics.RenderPasses;
|
|
||||||
using Ghost.Graphics.Contracts;
|
|
||||||
using Ghost.Graphics.RenderGraphModule;
|
|
||||||
|
|
||||||
namespace Ghost.Graphics.D3D12;
|
|
||||||
|
|
||||||
/// <summary>
|
|
||||||
/// D3D12 implementation of the renderer interface using RHI abstractions
|
|
||||||
/// </summary>
|
|
||||||
internal class D3D12Renderer : IRenderer
|
|
||||||
{
|
|
||||||
private readonly D3D12GraphicsEngine _graphicsEngine;
|
|
||||||
private readonly D3D12ResourceDatabase _resourceDatabase;
|
|
||||||
|
|
||||||
private readonly ICommandBuffer _commandBuffer;
|
|
||||||
private readonly RenderGraph _renderGraph;
|
|
||||||
|
|
||||||
private uint _frameIndex;
|
|
||||||
private bool _disposed;
|
|
||||||
|
|
||||||
// NOTE: Testing only.
|
|
||||||
private readonly MeshRenderPass _pass;
|
|
||||||
|
|
||||||
public IRenderOutput? RenderOutput
|
|
||||||
{
|
|
||||||
get; set;
|
|
||||||
}
|
|
||||||
|
|
||||||
// TODO: Add render graph support
|
|
||||||
|
|
||||||
public D3D12Renderer(D3D12GraphicsEngine graphicsEngine, D3D12ResourceDatabase resourceDatabase)
|
|
||||||
{
|
|
||||||
_graphicsEngine = graphicsEngine;
|
|
||||||
_resourceDatabase = resourceDatabase;
|
|
||||||
|
|
||||||
_commandBuffer = _graphicsEngine.CreateCommandBuffer(CommandBufferType.Graphics);
|
|
||||||
_renderGraph = new RenderGraph(_graphicsEngine);
|
|
||||||
|
|
||||||
// NOTE: Testing only.
|
|
||||||
_pass = new();
|
|
||||||
}
|
|
||||||
|
|
||||||
~D3D12Renderer()
|
|
||||||
{
|
|
||||||
Dispose();
|
|
||||||
}
|
|
||||||
|
|
||||||
public Result Render(ICommandAllocator commandAllocator)
|
|
||||||
{
|
|
||||||
if (RenderOutput is null)
|
|
||||||
{
|
|
||||||
return Result.Failure("Render target strategy is not set.");
|
|
||||||
}
|
|
||||||
|
|
||||||
var target = RenderOutput.GetRenderTarget();
|
|
||||||
if (target.IsInvalid)
|
|
||||||
{
|
|
||||||
return Result.Failure("Render target is invalid.");
|
|
||||||
}
|
|
||||||
|
|
||||||
_commandBuffer.Begin(commandAllocator);
|
|
||||||
RenderOutput.BeginRender(_commandBuffer);
|
|
||||||
|
|
||||||
// NOTE: Temporary solution: render directly to the swap chain back buffer if available.
|
|
||||||
// HACK: This is hard coded for testing purposes only.
|
|
||||||
|
|
||||||
var error = RenderScene(target, RenderOutput.Viewport, RenderOutput.Scissor);
|
|
||||||
if (error != Error.None)
|
|
||||||
{
|
|
||||||
_commandBuffer.End();
|
|
||||||
return Result.Failure(error);
|
|
||||||
}
|
|
||||||
|
|
||||||
RenderOutput.EndRender(_commandBuffer);
|
|
||||||
var r = _commandBuffer.End();
|
|
||||||
if (r.IsFailure)
|
|
||||||
{
|
|
||||||
return r;
|
|
||||||
}
|
|
||||||
|
|
||||||
_graphicsEngine.Device.GraphicsQueue.Submit(_commandBuffer);
|
|
||||||
RenderOutput.Present();
|
|
||||||
|
|
||||||
return Result.Success();
|
|
||||||
}
|
|
||||||
|
|
||||||
// TODO: A proper render graph integration.
|
|
||||||
private Error RenderScene(Handle<Texture> target, ViewportDesc viewport, RectDesc rect)
|
|
||||||
{
|
|
||||||
// NOTE: Testing only.
|
|
||||||
var ctx = new RenderingContext(_graphicsEngine, _commandBuffer);
|
|
||||||
if (_frameIndex == 0)
|
|
||||||
{
|
|
||||||
_pass.Initialize(ref ctx);
|
|
||||||
}
|
|
||||||
|
|
||||||
//_commandBuffer.BeginRenderPass(rtDesc, depthDesc, false);
|
|
||||||
_commandBuffer.SetViewport(viewport);
|
|
||||||
_commandBuffer.SetScissorRect(rect);
|
|
||||||
|
|
||||||
_renderGraph.Reset();
|
|
||||||
|
|
||||||
var backBuffer = _renderGraph.ImportTexture(target, "Back Buffer");
|
|
||||||
_pass.Build(_renderGraph, backBuffer);
|
|
||||||
|
|
||||||
// Create view state from viewport
|
|
||||||
var viewState = new ViewState((uint)viewport.Width, (uint)viewport.Height);
|
|
||||||
|
|
||||||
// Compile with view state
|
|
||||||
_renderGraph.Compile(in viewState);
|
|
||||||
_renderGraph.Execute(_commandBuffer);
|
|
||||||
|
|
||||||
//_commandBuffer.EndRenderPass();
|
|
||||||
_frameIndex++;
|
|
||||||
|
|
||||||
return Error.None;
|
|
||||||
}
|
|
||||||
|
|
||||||
public void Dispose()
|
|
||||||
{
|
|
||||||
if (_disposed)
|
|
||||||
{
|
|
||||||
return;
|
|
||||||
}
|
|
||||||
|
|
||||||
// NOTE: Testing only.
|
|
||||||
_pass.Cleanup(_resourceDatabase);
|
|
||||||
_renderGraph.Dispose();
|
|
||||||
|
|
||||||
_commandBuffer.Dispose();
|
|
||||||
|
|
||||||
_disposed = true;
|
|
||||||
|
|
||||||
GC.SuppressFinalize(this);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
@@ -1,24 +0,0 @@
|
|||||||
using System.Runtime.CompilerServices;
|
|
||||||
using TerraFX.Interop.DirectX;
|
|
||||||
using Ghost.Graphics.Core;
|
|
||||||
|
|
||||||
namespace Ghost.Graphics.D3D12.Utilities;
|
|
||||||
|
|
||||||
internal unsafe static class D3D12PipelineResource
|
|
||||||
{
|
|
||||||
private readonly static D3D12_INPUT_ELEMENT_DESC[] s_inputElementDescs = [
|
|
||||||
new D3D12_INPUT_ELEMENT_DESC{ SemanticName = (sbyte*)Vertex.Semantic.Position.GetUnsafePointer(), SemanticIndex = 0u, Format = Vertex.Semantic.ALIGNED_FORMAT, InputSlot = 0u, AlignedByteOffset = 0u, InputSlotClass = D3D12_INPUT_CLASSIFICATION.D3D12_INPUT_CLASSIFICATION_PER_VERTEX_DATA, InstanceDataStepRate = 0 },
|
|
||||||
new D3D12_INPUT_ELEMENT_DESC{ SemanticName = (sbyte*)Vertex.Semantic.Normal.GetUnsafePointer(), SemanticIndex = 0u, Format = Vertex.Semantic.ALIGNED_FORMAT, InputSlot = 0u, AlignedByteOffset = 16u, InputSlotClass = D3D12_INPUT_CLASSIFICATION.D3D12_INPUT_CLASSIFICATION_PER_VERTEX_DATA, InstanceDataStepRate = 0 },
|
|
||||||
new D3D12_INPUT_ELEMENT_DESC{ SemanticName = (sbyte*)Vertex.Semantic.Tangent.GetUnsafePointer(), SemanticIndex = 0u, Format = Vertex.Semantic.ALIGNED_FORMAT, InputSlot = 0u, AlignedByteOffset = 32u, InputSlotClass = D3D12_INPUT_CLASSIFICATION.D3D12_INPUT_CLASSIFICATION_PER_VERTEX_DATA, InstanceDataStepRate = 0 },
|
|
||||||
new D3D12_INPUT_ELEMENT_DESC{ SemanticName = (sbyte*)Vertex.Semantic.Uv.GetUnsafePointer(), SemanticIndex = 0u, Format = Vertex.Semantic.ALIGNED_FORMAT, InputSlot = 0u, AlignedByteOffset = 48u, InputSlotClass = D3D12_INPUT_CLASSIFICATION.D3D12_INPUT_CLASSIFICATION_PER_VERTEX_DATA, InstanceDataStepRate = 0 },
|
|
||||||
new D3D12_INPUT_ELEMENT_DESC{ SemanticName = (sbyte*)Vertex.Semantic.Color.GetUnsafePointer(), SemanticIndex = 0u, Format = Vertex.Semantic.ALIGNED_FORMAT, InputSlot = 0u, AlignedByteOffset = 64u, InputSlotClass = D3D12_INPUT_CLASSIFICATION.D3D12_INPUT_CLASSIFICATION_PER_VERTEX_DATA, InstanceDataStepRate = 0 },
|
|
||||||
];
|
|
||||||
|
|
||||||
public const DXGI_FORMAT SWAP_CHAIN_BACK_BUFFER_FORMAT = DXGI_FORMAT.DXGI_FORMAT_B8G8R8A8_UNORM;
|
|
||||||
|
|
||||||
public static D3D12_INPUT_LAYOUT_DESC InputLayoutDescription => new()
|
|
||||||
{
|
|
||||||
pInputElementDescs = (D3D12_INPUT_ELEMENT_DESC*)Unsafe.AsPointer(ref s_inputElementDescs[0]),
|
|
||||||
NumElements = (uint)s_inputElementDescs.Length
|
|
||||||
};
|
|
||||||
}
|
|
||||||
@@ -1,22 +0,0 @@
|
|||||||
using Ghost.Core;
|
|
||||||
using Ghost.Graphics.Contracts;
|
|
||||||
|
|
||||||
namespace Ghost.Graphics.RHI;
|
|
||||||
|
|
||||||
/// <summary>
|
|
||||||
/// High-level renderer interface that uses RHI abstractions
|
|
||||||
/// </summary>
|
|
||||||
public interface IRenderer : IDisposable
|
|
||||||
{
|
|
||||||
IRenderOutput? RenderOutput
|
|
||||||
{
|
|
||||||
get; set;
|
|
||||||
}
|
|
||||||
|
|
||||||
/// <summary>
|
|
||||||
/// Renders a frame
|
|
||||||
/// </summary>
|
|
||||||
/// <param name="commandAllocator">Command allocator to use for rendering</param>
|
|
||||||
/// <returns>Result of the rendering operation</returns>
|
|
||||||
Result Render(ICommandAllocator commandAllocator);
|
|
||||||
}
|
|
||||||
@@ -1,177 +0,0 @@
|
|||||||
using Ghost.Core;
|
|
||||||
using Ghost.Graphics.Core;
|
|
||||||
using Ghost.Graphics.RHI;
|
|
||||||
using Misaki.HighPerformance.LowLevel.Buffer;
|
|
||||||
using Misaki.HighPerformance.LowLevel.Collections;
|
|
||||||
using System.IO.Hashing;
|
|
||||||
|
|
||||||
namespace Ghost.Graphics.RenderGraphModule;
|
|
||||||
|
|
||||||
/// <summary>
|
|
||||||
/// Computes structural hashes of render graphs for compilation caching.
|
|
||||||
/// Hashes are based on graph topology and resource configurations, not runtime values.
|
|
||||||
/// </summary>
|
|
||||||
internal static class RenderGraphHasher
|
|
||||||
{
|
|
||||||
/// <summary>
|
|
||||||
/// Computes a hash of the entire render graph structure.
|
|
||||||
/// Used for cache invalidation - same hash means same compilation result.
|
|
||||||
/// </summary>
|
|
||||||
public static unsafe ulong ComputeGraphHash(List<RenderGraphPassBase> passes, RenderGraphResourceRegistry resources)
|
|
||||||
{
|
|
||||||
using var scope = AllocationManager.CreateStackScope();
|
|
||||||
var bufferPool = new UnsafeList<byte>(2048, scope.AllocationHandle);
|
|
||||||
var pData = (byte*)bufferPool.GetUnsafePtr();
|
|
||||||
var offset = 0;
|
|
||||||
|
|
||||||
// Hash pass count
|
|
||||||
*(int*)(pData + offset) = passes.Count;
|
|
||||||
offset += sizeof(int);
|
|
||||||
|
|
||||||
// Hash each pass structure (excluding names)
|
|
||||||
for (var i = 0; i < passes.Count; i++)
|
|
||||||
{
|
|
||||||
var pass = passes[i];
|
|
||||||
|
|
||||||
*(RenderPassType*)(pData + offset) = pass.type;
|
|
||||||
offset += sizeof(RenderPassType);
|
|
||||||
|
|
||||||
*(bool*)(pData + offset) = pass.allowCulling;
|
|
||||||
offset += sizeof(bool);
|
|
||||||
|
|
||||||
*(bool*)(pData + offset) = pass.asyncCompute;
|
|
||||||
offset += sizeof(bool);
|
|
||||||
|
|
||||||
// Hash depth attachment
|
|
||||||
offset = ComputeTextureHash(pData, offset, pass.depthAccess.id, resources);
|
|
||||||
|
|
||||||
pData[offset] = (byte)pass.depthAccess.accessFlags;
|
|
||||||
offset += sizeof(AccessFlags);
|
|
||||||
|
|
||||||
*(int*)(pData + offset) = pass.maxColorIndex;
|
|
||||||
offset += sizeof(int);
|
|
||||||
for (var j = 0; j <= pass.maxColorIndex; j++)
|
|
||||||
{
|
|
||||||
offset = ComputeTextureHash(pData, offset, pass.colorAccess[j].id, resources);
|
|
||||||
|
|
||||||
pData[offset] = (byte)pass.colorAccess[j].accessFlags;
|
|
||||||
offset += sizeof(AccessFlags);
|
|
||||||
}
|
|
||||||
|
|
||||||
for (var j = 0; j < (int)RenderGraphResourceType.Count; j++)
|
|
||||||
{
|
|
||||||
var readList = pass.resourceReads[j];
|
|
||||||
var writeList = pass.resourceWrites[j];
|
|
||||||
var createList = pass.resourceCreates[j];
|
|
||||||
|
|
||||||
*(int*)(pData + offset) = readList.Count;
|
|
||||||
offset += sizeof(int);
|
|
||||||
for (var k = 0; k < readList.Count; k++)
|
|
||||||
{
|
|
||||||
*(int*)(pData + offset) = readList[k].Value;
|
|
||||||
offset += sizeof(int);
|
|
||||||
}
|
|
||||||
|
|
||||||
*(int*)(pData + offset) = writeList.Count;
|
|
||||||
offset += sizeof(int);
|
|
||||||
for (var k = 0; k < writeList.Count; k++)
|
|
||||||
{
|
|
||||||
*(int*)(pData + offset) = writeList[k].Value;
|
|
||||||
offset += sizeof(int);
|
|
||||||
}
|
|
||||||
|
|
||||||
*(int*)(pData + offset) = createList.Count;
|
|
||||||
offset += sizeof(int);
|
|
||||||
for (var k = 0; k < createList.Count; k++)
|
|
||||||
{
|
|
||||||
*(int*)(pData + offset) = createList[k].Value;
|
|
||||||
offset += sizeof(int);
|
|
||||||
}
|
|
||||||
|
|
||||||
*(int*)(pData + offset) = pass.randomAccess.Count;
|
|
||||||
offset += sizeof(int);
|
|
||||||
for (var k = 0; k < pass.randomAccess.Count; k++)
|
|
||||||
{
|
|
||||||
*(int*)(pData + offset) = pass.randomAccess[k].Value;
|
|
||||||
offset += sizeof(int);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
*(int*)(pData + offset) = pass.GetRenderFuncHashCode();
|
|
||||||
offset += sizeof(int);
|
|
||||||
}
|
|
||||||
|
|
||||||
var span = new Span<byte>(pData, offset);
|
|
||||||
return XxHash64.HashToUInt64(span);
|
|
||||||
}
|
|
||||||
|
|
||||||
/// <summary>
|
|
||||||
/// Computes a hash of a texture resource's structural properties.
|
|
||||||
/// For imported textures, hashes the backing handle.
|
|
||||||
/// For transient textures, hashes the descriptor (respecting size mode).
|
|
||||||
/// </summary>
|
|
||||||
private static unsafe int ComputeTextureHash(byte* pData, int offset, Identifier<RGTexture> texture, RenderGraphResourceRegistry resources)
|
|
||||||
{
|
|
||||||
if (texture.IsInvalid)
|
|
||||||
{
|
|
||||||
return offset;
|
|
||||||
}
|
|
||||||
|
|
||||||
var resource = resources.GetResource(texture.AsResource());
|
|
||||||
|
|
||||||
// Hash imported flag
|
|
||||||
*(pData + offset) = resource.isImported ? (byte)1 : (byte)0;
|
|
||||||
offset += sizeof(byte);
|
|
||||||
|
|
||||||
// For imported textures, hash the backing resource handle
|
|
||||||
if (resource.isImported)
|
|
||||||
{
|
|
||||||
*(int*)(pData + offset) = resource.backingResource.GetHashCode();
|
|
||||||
offset += sizeof(int);
|
|
||||||
return offset;
|
|
||||||
}
|
|
||||||
|
|
||||||
var desc = resource.rgTextureDesc;
|
|
||||||
|
|
||||||
// Hash format (structural)
|
|
||||||
*(TextureFormat*)(pData + offset) = desc.format;
|
|
||||||
offset += sizeof(TextureFormat);
|
|
||||||
|
|
||||||
// Hash size mode (structural)
|
|
||||||
*(RGTextureSizeMode*)(pData + offset) = desc.sizeMode;
|
|
||||||
offset += sizeof(RGTextureSizeMode);
|
|
||||||
|
|
||||||
// Hash size specification based on mode
|
|
||||||
if (desc.sizeMode == RGTextureSizeMode.Absolute)
|
|
||||||
{
|
|
||||||
// Absolute mode: hash actual dimensions
|
|
||||||
*(uint*)(pData + offset) = desc.width;
|
|
||||||
offset += sizeof(uint);
|
|
||||||
*(uint*)(pData + offset) = desc.height;
|
|
||||||
offset += sizeof(uint);
|
|
||||||
}
|
|
||||||
else
|
|
||||||
{
|
|
||||||
// Relative mode: hash scale factors (NOT resolved dimensions)
|
|
||||||
*(float*)(pData + offset) = desc.scaleX;
|
|
||||||
offset += sizeof(float);
|
|
||||||
*(float*)(pData + offset) = desc.scaleY;
|
|
||||||
offset += sizeof(float);
|
|
||||||
}
|
|
||||||
|
|
||||||
// Hash other structural properties
|
|
||||||
*(TextureDimension*)(pData + offset) = desc.dimension;
|
|
||||||
offset += sizeof(TextureDimension);
|
|
||||||
*(uint*)(pData + offset) = desc.mipLevels;
|
|
||||||
offset += sizeof(uint);
|
|
||||||
*(TextureUsage*)(pData + offset) = desc.usage;
|
|
||||||
offset += sizeof(TextureUsage);
|
|
||||||
|
|
||||||
*(bool*)(pData + offset) = desc.clearAtFirstUse;
|
|
||||||
offset += sizeof(bool);
|
|
||||||
*(bool*)(pData + offset) = desc.discardAtLastUse;
|
|
||||||
offset += sizeof(bool);
|
|
||||||
|
|
||||||
return offset;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
@@ -1,5 +0,0 @@
|
|||||||
namespace Ghost.Graphics.RenderPasses;
|
|
||||||
|
|
||||||
internal class SimpleRenderPipeline
|
|
||||||
{
|
|
||||||
}
|
|
||||||
@@ -1,2 +0,0 @@
|
|||||||
// See https://aka.ms/new-console-template for more information
|
|
||||||
Console.WriteLine("Hello, World!");
|
|
||||||
@@ -1,10 +0,0 @@
|
|||||||
namespace Ghost.Test.Core;
|
|
||||||
|
|
||||||
public interface ITest
|
|
||||||
{
|
|
||||||
public void Setup();
|
|
||||||
|
|
||||||
public void Run();
|
|
||||||
|
|
||||||
public void Cleanup();
|
|
||||||
}
|
|
||||||
@@ -1,94 +0,0 @@
|
|||||||
using Ghost.Editor.Core.AssetHandle;
|
|
||||||
using Ghost.Editor.Core.AssetHandle.Importers;
|
|
||||||
using System.Text.Json;
|
|
||||||
|
|
||||||
namespace Ghost.UnitTest;
|
|
||||||
|
|
||||||
[TestClass]
|
|
||||||
public class AssetMetaTest
|
|
||||||
{
|
|
||||||
[TestMethod]
|
|
||||||
public void TestMetaSerialization()
|
|
||||||
{
|
|
||||||
var meta = new AssetMeta
|
|
||||||
{
|
|
||||||
Guid = Guid.NewGuid(),
|
|
||||||
Version = 1,
|
|
||||||
Tags = new List<string> { "Test", "Asset" }
|
|
||||||
};
|
|
||||||
|
|
||||||
var json = JsonSerializer.Serialize(meta, new JsonSerializerOptions { WriteIndented = true });
|
|
||||||
|
|
||||||
Assert.IsNotNull(json);
|
|
||||||
Assert.Contains("Guid", json);
|
|
||||||
Assert.Contains("Version", json);
|
|
||||||
Assert.Contains("Tags", json);
|
|
||||||
}
|
|
||||||
|
|
||||||
[TestMethod]
|
|
||||||
public void TestMetaDeserialization()
|
|
||||||
{
|
|
||||||
var guid = Guid.NewGuid();
|
|
||||||
|
|
||||||
var json = $@"{{
|
|
||||||
""Guid"": ""{guid}"",
|
|
||||||
""Version"": 1,
|
|
||||||
""Tags"": [""Test"", ""Asset""]
|
|
||||||
}}";
|
|
||||||
|
|
||||||
var meta = JsonSerializer.Deserialize<AssetMeta>(json);
|
|
||||||
|
|
||||||
Assert.IsNotNull(meta);
|
|
||||||
Assert.AreEqual(guid, meta.Guid);
|
|
||||||
Assert.AreEqual(1, meta.Version);
|
|
||||||
Assert.HasCount(2, meta.Tags);
|
|
||||||
Assert.Contains("Test", meta.Tags);
|
|
||||||
}
|
|
||||||
|
|
||||||
[TestMethod]
|
|
||||||
public void TestMetaWithSettings()
|
|
||||||
{
|
|
||||||
var meta = new AssetMeta
|
|
||||||
{
|
|
||||||
Guid = Guid.NewGuid(),
|
|
||||||
Version = 1
|
|
||||||
};
|
|
||||||
|
|
||||||
// Add importer settings using the new API
|
|
||||||
var settings = new TextImporterSettings
|
|
||||||
{
|
|
||||||
Encoding = "UTF-8",
|
|
||||||
TrimWhitespace = true
|
|
||||||
};
|
|
||||||
|
|
||||||
meta.SetImporterSettings("TextImporter", settings);
|
|
||||||
|
|
||||||
var json = JsonSerializer.Serialize(meta, new JsonSerializerOptions { WriteIndented = true });
|
|
||||||
var deserialized = JsonSerializer.Deserialize<AssetMeta>(json);
|
|
||||||
|
|
||||||
Assert.IsNotNull(deserialized);
|
|
||||||
Assert.Contains("TextImporter", deserialized.ImporterSettings.Keys);
|
|
||||||
|
|
||||||
// Test retrieving the settings
|
|
||||||
var retrievedSettings = deserialized.GetImporterSettings<TextImporterSettings>("TextImporter");
|
|
||||||
Assert.IsNotNull(retrievedSettings);
|
|
||||||
Assert.AreEqual("UTF-8", retrievedSettings.Encoding);
|
|
||||||
Assert.IsTrue(retrievedSettings.TrimWhitespace);
|
|
||||||
}
|
|
||||||
|
|
||||||
[TestMethod]
|
|
||||||
public void TestFileHashAndDependenciesNotSerialized()
|
|
||||||
{
|
|
||||||
var meta = new AssetMeta
|
|
||||||
{
|
|
||||||
Guid = Guid.NewGuid(),
|
|
||||||
Version = 1
|
|
||||||
};
|
|
||||||
|
|
||||||
var json = JsonSerializer.Serialize(meta, new JsonSerializerOptions { WriteIndented = true });
|
|
||||||
|
|
||||||
// FileHash and Dependencies should NOT be in the serialized JSON
|
|
||||||
Assert.DoesNotContain("FileHash", json);
|
|
||||||
Assert.DoesNotContain("Dependencies", json);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
@@ -1,44 +0,0 @@
|
|||||||
<Solution>
|
|
||||||
<Configurations>
|
|
||||||
<Platform Name="ARM64" />
|
|
||||||
<Platform Name="x64" />
|
|
||||||
<Platform Name="x86" />
|
|
||||||
</Configurations>
|
|
||||||
<Folder Name="/Editor/">
|
|
||||||
<Project Path="Ghost.Data/Ghost.Data.csproj" />
|
|
||||||
<Project Path="Ghost.Editor.Core/Ghost.Editor.Core.csproj" />
|
|
||||||
<Project Path="Ghost.Editor/Ghost.Editor.csproj">
|
|
||||||
<Platform Solution="*|ARM64" Project="ARM64" />
|
|
||||||
<Platform Solution="*|x64" Project="x64" />
|
|
||||||
<Platform Solution="*|x86" Project="x86" />
|
|
||||||
<Deploy />
|
|
||||||
</Project>
|
|
||||||
</Folder>
|
|
||||||
<Folder Name="/Library/">
|
|
||||||
<Project Path="Ghost.FMOD/Ghost.FMOD.csproj" />
|
|
||||||
<Project Path="Ghost.Zeux.MeshOptimizer/Ghost.Zeux.MeshOptimizer.csproj" />
|
|
||||||
</Folder>
|
|
||||||
<Folder Name="/Runtime/">
|
|
||||||
<Project Path="Ghost.Core/Ghost.Core.csproj" />
|
|
||||||
<Project Path="Ghost.Engine/Ghost.Engine.csproj" />
|
|
||||||
<Project Path="Ghost.Entities/Ghost.Entities.csproj" />
|
|
||||||
<Project Path="Ghost.Generator/Ghost.Generator.csproj" />
|
|
||||||
<Project Path="Ghost.Graphics/Ghost.Graphics.csproj" />
|
|
||||||
</Folder>
|
|
||||||
<Folder Name="/Test/">
|
|
||||||
<Project Path="Ghost.Entities.Test/Ghost.Entities.Test.csproj" />
|
|
||||||
<Project Path="Ghost.Graphics.Test/Ghost.Graphics.Test.csproj">
|
|
||||||
<Platform Solution="*|ARM64" Project="ARM64" />
|
|
||||||
<Platform Solution="*|x64" Project="x64" />
|
|
||||||
<Platform Solution="*|x86" Project="x86" />
|
|
||||||
<Deploy />
|
|
||||||
</Project>
|
|
||||||
<Project Path="Ghost.MicroTest/Ghost.MicroTest.csproj" Id="8c8ffa4b-e1e4-46a1-9221-7b508a109edd" />
|
|
||||||
<Project Path="Ghost.Shader.Test/Ghost.Shader.Test.csproj" />
|
|
||||||
<Project Path="Ghost.Test.Core/Ghost.Test.Core.csproj" />
|
|
||||||
<Project Path="Ghost.UnitTest/Ghost.UnitTest.csproj" Id="4da45668-456b-4dcc-acd8-6bfe154e6837">
|
|
||||||
<Platform Solution="Debug|x64" Project="x64" />
|
|
||||||
</Project>
|
|
||||||
</Folder>
|
|
||||||
<Project Path="Ghost.DSL/Ghost.DSL.csproj" />
|
|
||||||
</Solution>
|
|
||||||
1125
doc/meshlet-architecture.md
Normal file
@@ -1,5 +1,5 @@
|
|||||||
[*]
|
[*]
|
||||||
max_line_length = 400
|
max_line_length = 200
|
||||||
|
|
||||||
[*.cs]
|
[*.cs]
|
||||||
csharp_new_line_before_open_brace = all
|
csharp_new_line_before_open_brace = all
|
||||||
@@ -25,7 +25,7 @@
|
|||||||
</ItemGroup>
|
</ItemGroup>
|
||||||
|
|
||||||
<ItemGroup>
|
<ItemGroup>
|
||||||
<ProjectReference Include="../Ghost.Core/Ghost.Core.csproj" />
|
<ProjectReference Include="../../Runtime/Ghost.Core/Ghost.Core.csproj" />
|
||||||
</ItemGroup>
|
</ItemGroup>
|
||||||
|
|
||||||
</Project>
|
</Project>
|
||||||
@@ -44,28 +44,31 @@ internal static class DSLShaderCompiler
|
|||||||
};
|
};
|
||||||
}
|
}
|
||||||
|
|
||||||
private static uint CalculateCBufferSize(ReadOnlySpan<PropertyDescriptor> properties)
|
private static int LayoutCBufferProperties(Span<PropertyDescriptor> properties)
|
||||||
{
|
{
|
||||||
if (properties.IsEmpty)
|
if (properties.IsEmpty)
|
||||||
{
|
{
|
||||||
return 0;
|
return 0;
|
||||||
}
|
}
|
||||||
|
|
||||||
var currentOffset = 0u;
|
var currentOffset = 0;
|
||||||
|
|
||||||
foreach (var prop in properties)
|
foreach (ref var prop in properties)
|
||||||
{
|
{
|
||||||
var size = prop.type.GetSize();
|
var size = prop.type.GetSize();
|
||||||
|
|
||||||
if ((currentOffset % 16) + size > 16)
|
if ((currentOffset % 16) + size > 16)
|
||||||
{
|
{
|
||||||
currentOffset = (currentOffset + 15u) & ~15u;
|
currentOffset = (currentOffset + 15) & ~15;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
prop.offset = currentOffset;
|
||||||
|
prop.size = size;
|
||||||
|
|
||||||
currentOffset += size;
|
currentOffset += size;
|
||||||
}
|
}
|
||||||
|
|
||||||
return (currentOffset + 15u) & ~15u;
|
return (currentOffset + 15) & ~15;
|
||||||
}
|
}
|
||||||
|
|
||||||
// TODO: Implement shader inheritance resolution, including property and pass merging.
|
// TODO: Implement shader inheritance resolution, including property and pass merging.
|
||||||
@@ -98,12 +101,12 @@ internal static class DSLShaderCompiler
|
|||||||
|
|
||||||
descriptor.globalProperties = shaderGlobalProperties ?? Array.Empty<PropertyDescriptor>();
|
descriptor.globalProperties = shaderGlobalProperties ?? Array.Empty<PropertyDescriptor>();
|
||||||
descriptor.properties = shaderLocalProperties ?? Array.Empty<PropertyDescriptor>();
|
descriptor.properties = shaderLocalProperties ?? Array.Empty<PropertyDescriptor>();
|
||||||
descriptor.cbufferSize = CalculateCBufferSize(descriptor.properties);
|
descriptor.cbufferSize = LayoutCBufferProperties(descriptor.properties);
|
||||||
|
|
||||||
if (semantics.passes != null)
|
if (semantics.passes != null)
|
||||||
{
|
{
|
||||||
descriptor.passes = new PassDescriptor[semantics.passes.Count];
|
descriptor.passes = new PassDescriptor[semantics.passes.Count];
|
||||||
for (int i = 0; i < semantics.passes.Count; i++)
|
for (var i = 0; i < semantics.passes.Count; i++)
|
||||||
{
|
{
|
||||||
var pass = semantics.passes[i];
|
var pass = semantics.passes[i];
|
||||||
var localPipeline = MeragePipeline(pass.localPipeline, PipelineState.Default);
|
var localPipeline = MeragePipeline(pass.localPipeline, PipelineState.Default);
|
||||||
@@ -264,7 +267,7 @@ internal static class DSLShaderCompiler
|
|||||||
#ifndef {fileDefine}
|
#ifndef {fileDefine}
|
||||||
#define {fileDefine}
|
#define {fileDefine}
|
||||||
|
|
||||||
#include ""F:/csharp/GhostEngine/Ghost.Graphics/Shaders/Includes/Common.hlsl""");
|
#include ""F:/csharp/GhostEngine/src/Runtime//Ghost.Graphics/Shaders/Includes/Common.hlsl""");
|
||||||
|
|
||||||
sb.Append(@"
|
sb.Append(@"
|
||||||
struct PerMaterialData
|
struct PerMaterialData
|
||||||
@@ -303,7 +306,7 @@ struct PerMaterialData
|
|||||||
#ifndef GLOBALDATA_G_HLSL
|
#ifndef GLOBALDATA_G_HLSL
|
||||||
#define GLOBALDATA_G_HLSL
|
#define GLOBALDATA_G_HLSL
|
||||||
|
|
||||||
#include ""F:/csharp/GhostEngine/Ghost.Graphics/Shaders/Includes/Common.hlsl""
|
#include ""F:/csharp/GhostEngine/src/Runtime//Ghost.Graphics/Shaders/Includes/Common.hlsl""
|
||||||
|
|
||||||
struct GlobalData
|
struct GlobalData
|
||||||
{");
|
{");
|
||||||
@@ -205,7 +205,7 @@ public class ShaderVisitor : GhostShaderParserBaseVisitor<object>
|
|||||||
// Get the text between the braces
|
// Get the text between the braces
|
||||||
var start = context.LBRACE().Symbol.StopIndex + 1;
|
var start = context.LBRACE().Symbol.StopIndex + 1;
|
||||||
var stop = context.RBRACE().Symbol.StartIndex - 1;
|
var stop = context.RBRACE().Symbol.StartIndex - 1;
|
||||||
|
|
||||||
if (stop >= start)
|
if (stop >= start)
|
||||||
{
|
{
|
||||||
var input = context.Start.InputStream;
|
var input = context.Start.InputStream;
|
||||||