Refactor project structure and enhance functionality

Added `InternalsVisibleTo` attribute for "Ghost.Editor" in `AssemblyInfo.cs`.
Added a binary file `Empty.zip` to the project.
Added a new `ProjectMetadata` class in `ProjectMetadata.cs`.
Added new states and interfaces for managing application states in `EditorState.cs`, `LandingState.cs`, and `IAppState.cs`.
Added a notification service in `INotificationService.cs` and `StackedNotificationService.cs`.
Added new XAML files for UI components, including `InspectorView.xaml` and `InternalControls.xaml`.

Changed the `ProjectInfo` class in `ProjectInfo.cs` to include a `MetadataPath` property instead of `Path` and `EngineVersion`.
Changed the `TemplateInfo` class in `TemplateInfo.cs` to use a struct instead of a class for `TemplateData`.
Changed the `ProjectService` class to use the new `ProjectRepository` for managing project data.

Removed several using directives and the entire `ProjectRepository` class from `ProjectRepository.cs`, replacing it with a new implementation.
Removed old methods and properties in `EntityManager` and `World` classes to improve entity management and component handling.

Updated the `Ghost.Data.csproj` file to include the new `Empty.zip` file as a content item.
Updated the `ProjectRepository` class to manage project data using SQLite.
Updated various XAML files to include new styles and controls, improving the overall UI design.
Updated the `CreateProjectViewModel` to include a notification service and handle project creation logic.
Updated the test project to include references to the new `Ghost.Graphics` project and modified test cases to align with the new structure.
This commit is contained in:
2025-05-31 01:45:34 +09:00
parent 67b6040b5e
commit 61bbb1bc68
66 changed files with 1923 additions and 733 deletions

View File

@@ -5,4 +5,5 @@ global using WorldID = System.UInt16;
using System.Runtime.CompilerServices;
[assembly: InternalsVisibleTo("Ghost.Engine")]
[assembly: InternalsVisibleTo("Ghost.Editor")]
[assembly: InternalsVisibleTo("Ghost.Test")]

View File

@@ -23,11 +23,12 @@ internal interface IComponentPool : IDisposable
get;
}
public void Remove(Entity entity);
public bool Remove(Entity entity);
public bool Has(Entity entity);
}
internal interface IComponentPool<T> : IComponentPool
where T : IComponentData
{
public void Add(Entity entity, T Component);
}
@@ -115,8 +116,10 @@ internal class ComponentPool<T> : IComponentPool<T>
_nextId++;
}
public void Remove(Entity entity)
public bool Remove(Entity entity)
{
// We do not remove anything here, the generation of the entity will be used to determine if the component is valid.
return true;
}
public ref T GetRef(Entity entity)
@@ -213,13 +216,62 @@ internal class ScriptComponentPool : IComponentPool<ScriptComponent>
component.Owner = entity;
}
public void Remove(Entity entity)
public bool Remove<T>(Entity entity)
where T : ScriptComponent
{
if (!Has(entity)
|| !_scriptComponents!.TryGetValue(entity, out var scriptList)
|| scriptList == null)
{
return;
return false;
}
var scriptToRemove = scriptList.FirstOrDefault(script => script is T);
if (scriptToRemove == null)
{
return false;
}
scriptToRemove.OnDestroy();
scriptList.Remove(scriptToRemove);
if (scriptList.Count == 0)
{
_scriptComponents.Remove(entity);
}
return true;
}
public bool RemoveAt(Entity entity, int index)
{
if (!Has(entity)
|| !_scriptComponents!.TryGetValue(entity, out var scriptList)
|| scriptList == null)
{
return false;
}
if (index < 0 || index > scriptList.Count)
{
return false;
}
scriptList.RemoveAt(index);
if (scriptList.Count == 0)
{
_scriptComponents.Remove(entity);
}
return true;
}
public bool Remove(Entity entity)
{
if (!Has(entity)
|| !_scriptComponents!.TryGetValue(entity, out var scriptList)
|| scriptList == null)
{
return false;
}
foreach (var script in scriptList)
@@ -228,6 +280,7 @@ internal class ScriptComponentPool : IComponentPool<ScriptComponent>
}
_scriptComponents.Remove(entity);
return true;
}
public bool Has(Entity entity)
@@ -287,7 +340,15 @@ internal class ComponentStorage : IDisposable
private readonly Dictionary<nint, BitSet> _componentEntityMasks = new();
private readonly ScriptComponentPool _scriptComponentPool = new();
private readonly World _world;
internal ComponentStorage(World world)
{
_world = world;
}
internal Dictionary<nint, IComponentPool> ComponentPools => _componentPools;
internal Dictionary<nint, BitSet> ComponentEntityMasks => _componentEntityMasks;
internal ScriptComponentPool ScriptComponentPool => _scriptComponentPool;
[MethodImpl(MethodImplOptions.AggressiveInlining)]

View File

@@ -112,6 +112,11 @@ public class EntityManager : IDisposable
public int EntityCount => _entities.Count;
public ReadOnlySpan<Entity> Entities => CollectionsMarshal.AsSpan(_entities);
public event Action<Entity, Type>? OnComponentAdded;
public event Action<Entity, Type>? OnComponentRemoved;
public event Action<Entity>? OnEntityCreated;
public event Action<EntityID>? OnEntityRemoved;
internal EntityManager(World world, int initialCapacity)
{
_entities = new(initialCapacity);
@@ -125,17 +130,20 @@ public class EntityManager : IDisposable
/// <returns>The created <see cref="Entity"/>.</returns>
public Entity CreateEntity()
{
Entity entity;
if (_freeEntitySlots.TryDequeue(out var id))
{
return _entities[id];
entity = _entities[id];
}
else
{
id = _entities.Count;
var entity = new Entity(id, 0, _world.ID);
entity = new Entity(id, 0, _world.ID);
_entities.Add(entity);
return entity;
}
OnEntityCreated?.Invoke(entity);
return entity;
}
/// <summary>
@@ -149,13 +157,14 @@ public class EntityManager : IDisposable
return;
}
_world._componentStorage.Remove(entity);
_world.ComponentStorage.Remove(entity);
var slot = _entities[entity.ID];
slot.IncrementGeneration();
_entities[entity.ID] = slot;
_freeEntitySlots.Enqueue(entity.ID);
OnEntityRemoved?.Invoke(entity.ID);
entity = Entity.Invalid;
}
@@ -187,8 +196,9 @@ public class EntityManager : IDisposable
public void AddComponent<T>(Entity entity, T component)
where T : struct, IComponentData
{
_world._componentStorage.GetOrCreateComponentPool<T>().Add(entity, component);
_world._componentStorage.GetOrCreateMask(TypeHandle<T>.Value).SetBit(entity.ID);
_world.ComponentStorage.GetOrCreateComponentPool<T>().Add(entity, component);
_world.ComponentStorage.GetOrCreateMask(TypeHandle<T>.Value).SetBit(entity.ID);
OnComponentAdded?.Invoke(entity, typeof(T));
}
/// <summary>
@@ -197,14 +207,23 @@ public class EntityManager : IDisposable
/// <typeparam name="T">The type of the component to remove.</typeparam>
/// <param name="entity">The entity for which the component is to be remove.</param>
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public void RemoveComponent<T>(Entity entity)
public bool RemoveComponent<T>(Entity entity)
where T : struct, IComponentData
{
if (_world._componentStorage.TryGetPool<T>(out var pool) && pool.Has(entity))
if (!_world.ComponentStorage.TryGetPool<T>(out var pool) || !pool.Has(entity))
{
pool.Remove(entity);
_world._componentStorage.GetOrCreateMask(TypeHandle<T>.Value).ClearBit(entity.ID);
return false;
}
if (!pool.Remove(entity))
{
return false;
}
_world.ComponentStorage.GetOrCreateMask(TypeHandle<T>.Value).ClearBit(entity.ID);
OnComponentRemoved?.Invoke(entity, typeof(T));
return true;
}
/// <summary>
@@ -217,7 +236,7 @@ public class EntityManager : IDisposable
public void SetComponent<T>(Entity entity, T component)
where T : struct, IComponentData
{
_world._componentStorage.GetOrCreateComponentPool<T>().Set(entity, component);
_world.ComponentStorage.GetOrCreateComponentPool<T>().Set(entity, component);
}
/// <summary>
@@ -229,7 +248,7 @@ public class EntityManager : IDisposable
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public bool HasComponent(Entity entity, nint typeHandle)
{
return _world._componentStorage.TryGetMask(typeHandle, out var bitSet) && bitSet.IsSet(entity.ID);
return _world.ComponentStorage.TryGetMask(typeHandle, out var bitSet) && bitSet.IsSet(entity.ID);
}
/// <summary>
@@ -242,7 +261,7 @@ public class EntityManager : IDisposable
public Ref<T> GetComponent<T>(Entity entity)
where T : struct, IComponentData
{
if (_world._componentStorage.TryGetPool<T>(out var pool) && pool.Has(entity))
if (_world.ComponentStorage.TryGetPool<T>(out var pool) && pool.Has(entity))
{
return new Ref<T>(ref pool.GetRef(entity));
}
@@ -261,7 +280,8 @@ public class EntityManager : IDisposable
public void AddScript<T>(Entity entity)
where T : ScriptComponent, new()
{
_world._componentStorage.ScriptComponentPool.Add(entity, new T());
_world.ComponentStorage.ScriptComponentPool.Add(entity, new T());
OnComponentAdded?.Invoke(entity, typeof(ScriptComponent));
}
/// <summary>
@@ -280,7 +300,31 @@ public class EntityManager : IDisposable
}
var instance = (ScriptComponent?)Activator.CreateInstance(type) ?? throw new InvalidOperationException($"Failed to create instance of {type}.");
_world._componentStorage.ScriptComponentPool.Add(entity, instance);
_world.ComponentStorage.ScriptComponentPool.Add(entity, instance);
OnComponentAdded?.Invoke(entity, typeof(ScriptComponent));
}
public bool RemoveScript<T>(Entity entity)
where T : ScriptComponent
{
if (!_world.ComponentStorage.ScriptComponentPool.Remove<T>(entity))
{
return false;
}
OnComponentRemoved?.Invoke(entity, typeof(ScriptComponent));
return true;
}
public bool RemoveScriptAt(Entity entity, int index)
{
if (!_world.ComponentStorage.ScriptComponentPool.RemoveAt(entity, index))
{
return false;
}
OnComponentRemoved?.Invoke(entity, typeof(ScriptComponent));
return true;
}
/// <summary>
@@ -292,7 +336,7 @@ public class EntityManager : IDisposable
public T? GetScript<T>(Entity entity)
where T : ScriptComponent
{
return (T?)_world._componentStorage.ScriptComponentPool.Get(entity)?
return (T?)_world.ComponentStorage.ScriptComponentPool.Get(entity)?
.FirstOrDefault(script => script is T tScript);
}
@@ -305,7 +349,7 @@ public class EntityManager : IDisposable
public IEnumerable<T> GetScripts<T>(Entity entity)
where T : ScriptComponent
{
return (IEnumerable<T>?)_world._componentStorage.ScriptComponentPool.Get(entity)?.Where(script => script is T tScript) ?? Enumerable.Empty<T>();
return (IEnumerable<T>?)_world.ComponentStorage.ScriptComponentPool.Get(entity)?.Where(script => script is T tScript) ?? Enumerable.Empty<T>();
}
public void Dispose()

View File

@@ -33,11 +33,11 @@ public static class EntityHelpers
/// <typeparam name="T">The type of the component to remove.</typeparam>
/// <param name="entity">The entity for which the component is to be remove.</param>
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public static void RemoveComponent<T>(this Entity entity)
public static bool RemoveComponent<T>(this Entity entity)
where T : struct, IComponentData
{
var world = entity.GetWorld();
world.EntityManager.RemoveComponent<T>(entity);
return world.EntityManager.RemoveComponent<T>(entity);
}
/// <summary>

View File

@@ -37,7 +37,7 @@ internal struct QueryFilter()
// Compute All mask (intersection)
foreach (var typeHandle in _all)
{
var mask = world._componentStorage.GetOrCreateMask(typeHandle);
var mask = world.ComponentStorage.GetOrCreateMask(typeHandle);
if (!hasAll)
{
@@ -52,7 +52,7 @@ internal struct QueryFilter()
// Compute Any mask (union)
foreach (var typeHandle in _any)
{
var mask = world._componentStorage.GetOrCreateMask(typeHandle);
var mask = world.ComponentStorage.GetOrCreateMask(typeHandle);
if (!hasAny)
{
@@ -66,7 +66,7 @@ internal struct QueryFilter()
// Compute Absent mask (union for exclusion)
foreach (var typeHandle in _absent)
{
var mask = world._componentStorage.GetOrCreateMask(typeHandle);
var mask = world.ComponentStorage.GetOrCreateMask(typeHandle);
if (!hasAbsent)
{

View File

@@ -2,14 +2,32 @@
public abstract class ScriptComponent : IComponentData
{
private bool _enable = true;
/// <summary>
/// Gets or sets a value indicating whether this script component is enabled.
/// </summary>
public bool Enable
{
get;
set;
} = true;
get => _enable;
set
{
if (_enable == value)
{
return;
}
_enable = value;
if (_enable)
{
OnEnable();
}
else
{
OnDisable();
}
}
}
/// <summary>
/// Gets the entity that owns this script component.

View File

@@ -1,19 +1,30 @@
namespace Ghost.Entities;
using System.Runtime.CompilerServices;
namespace Ghost.Entities;
public abstract class SystemBase
{
/// <summary>
/// Gets the execution order of the current operation or component.
/// </summary>
public virtual int ExecutionOrder => 0;
/// <summary>
/// Gets or sets a value indicating whether the feature is enabled.
/// </summary>
public virtual bool Enable
{
get;
set;
} = true;
/// <summary>
/// The world that this system belongs to.
/// </summary>
public World World
{
get;
init;
internal set;
} = null!;
public virtual void OnCreate()
@@ -29,38 +40,73 @@ public abstract class SystemBase
}
}
internal class SystemStorage : IDisposable
public class SystemStorage : IDisposable
{
private readonly List<SystemBase> _systems = new();
private readonly List<SystemBase> _executionList = new();
private readonly World _world;
public event Action<SystemBase>? SystemAdded;
public event Action<SystemBase>? SystemRemoved;
internal SystemStorage(World world)
{
_world = world;
}
public void AddSystem<T>(T system)
where T : SystemBase
{
_systems.Add(system);
system.World = _world;
if (system.Enable)
{
system.OnCreate();
}
SystemAdded?.Invoke(system);
}
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public void AddSystem<T>()
where T : SystemBase, new()
{
AddSystem(new T());
}
public void RemoveSystem<T>(T system)
where T : SystemBase
{
system.World = null!;
_systems.Remove(system);
if (system.Enable)
{
system.OnDestroy();
}
SystemRemoved?.Invoke(system);
}
public void RebuildExecutionList()
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public void RemoveSystem<T>()
where T : SystemBase, new()
{
var system = _systems.FirstOrDefault(s => s is T);
if (system != null)
{
RemoveSystem(system);
}
}
internal void RebuildExecutionList()
{
_executionList.Clear();
_executionList.AddRange(_systems.OrderBy(s => s.ExecutionOrder));
}
public void UpdateSystems()
internal void UpdateSystems()
{
foreach (var system in _systems)
{

View File

@@ -1,19 +0,0 @@
namespace Ghost.Entities.Utilities;
internal class Box<T>
where T : struct
{
public T Value
{
get;
set;
}
public Box(T value)
{
Value = value;
}
public static implicit operator T(Box<T> box) => box.Value;
public static implicit operator Box<T>(T value) => new(value);
}

View File

@@ -1,64 +0,0 @@
using System.Numerics;
namespace Ghost.Entities.Utilities;
internal readonly struct ComponentMask
{
private readonly ulong[] _words;
public ComponentMask(int entityCapacity)
{
_words = new ulong[(entityCapacity + 63) / 64];
}
public void Set(int entityIndex)
=> _words[entityIndex >> 6] |= 1UL << (entityIndex & 63);
public void Clear(int entityIndex)
=> _words[entityIndex >> 6] &= ~(1UL << (entityIndex & 63));
public bool Get(int entityIndex)
=> ((_words[entityIndex >> 6] >> (entityIndex & 63)) & 1) != 0;
// Bitwise AND
public ComponentMask And(in ComponentMask other)
{
var result = new ComponentMask(_words.Length * 64);
for (var i = 0; i < _words.Length; i++)
result._words[i] = _words[i] & other._words[i];
return result;
}
// Bitwise OR
public ComponentMask Or(in ComponentMask other)
{
var result = new ComponentMask(_words.Length * 64);
for (var i = 0; i < _words.Length; i++)
result._words[i] = _words[i] | other._words[i];
return result;
}
// Bitwise NOT
public ComponentMask Not()
{
var result = new ComponentMask(_words.Length * 64);
for (var i = 0; i < _words.Length; i++)
result._words[i] = ~_words[i];
return result;
}
// Iterate set bits (fast scan)
public IEnumerable<int> GetEntityIndices()
{
for (var word = 0; word < _words.Length; word++)
{
var bits = _words[word];
while (bits != 0)
{
var lowBit = BitOperations.TrailingZeroCount(bits);
yield return (word << 6) + lowBit;
bits &= bits - 1; // clear lowest set bit
}
}
}
}

View File

@@ -12,6 +12,8 @@ public partial class World
private static int s_maxWorldCount = (int)MathF.Pow(2, Entity.WORLD_INDEX_BITS);
public static int WorldCount => s_worlds.Count - s_freeWorldSlots.Count;
public static World Create(int entityCapacity = 16)
{
lock (s_worlds)
@@ -46,31 +48,21 @@ public partial class World : IDisposable
{
private readonly WorldID _id;
private readonly EntityManager _entityManager;
private readonly ComponentStorage _componentStorage;
private readonly SystemStorage _systemStorage;
internal readonly ComponentStorage _componentStorage;
internal readonly SystemStorage _systemStorage;
internal ComponentStorage ComponentStorage => _componentStorage;
public WorldID ID => _id;
public EntityManager EntityManager => _entityManager;
public SystemStorage SystemStorage => _systemStorage;
private World(WorldID id, int entityCapacity)
{
_id = id;
_entityManager = new EntityManager(this, entityCapacity);
_componentStorage = new ComponentStorage();
_systemStorage = new SystemStorage();
}
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public void AddSystem<T>()
where T : SystemBase, new()
{
var instance = new T
{
World = this
};
_systemStorage.AddSystem(instance);
_componentStorage = new ComponentStorage(this);
_systemStorage = new SystemStorage(this);
}
[MethodImpl(MethodImplOptions.AggressiveInlining)]