Refactor project structure and improve performance

Changed the `ProjectRepository` class to be static for easier usage.
Changed `ProjectService` constants to public properties for accessibility.
Changed `App.xaml` to consolidate theme resources into `Override.xaml`.
Changed `App.xaml.cs` to implement an `AppStateMachine` for better state management.
Changed `ConsolePage` and `HierarchyPage` to utilize the new ViewModel structure.
Changed `ProjectPage` to use the `ExplorerItem` model for asset display.
Changed `Entity` and `EntityManager` to enhance component management with a new `IComponentData` interface.
Changed the `Logger` class to introduce structured logging functionality.
Changed the system architecture to support dependency management for better organization.
Changed the `QueryEnumerable` class to allow for more flexible entity queries.
Changed the `TypeHandle` class to improve efficiency in retrieving type handles.
Changed the `World` class to support robust world management and multiple worlds.
Updated the `Test` class to demonstrate the new entity and component management system.
This commit is contained in:
2025-06-05 21:45:50 +09:00
parent 61bbb1bc68
commit bab3be2508
69 changed files with 2184 additions and 1582 deletions

View File

@@ -0,0 +1,27 @@
namespace Ghost.Entities.Systems;
/// <summary>
/// Attribute to declare that a system depends on one or more other systems.
/// </summary>
[AttributeUsage(AttributeTargets.Struct | AttributeTargets.Class, AllowMultiple = false)]
public class DependsOnAttribute : Attribute
{
public Type[] Prerequisites
{
get;
}
public DependsOnAttribute(params Type[] prerequisites)
{
Prerequisites = prerequisites;
}
}
public interface ISystem
{
public void OnCreate(in SystemState state);
public void OnUpdate(in SystemState state);
public void OnDestroy(in SystemState state);
}

View File

@@ -0,0 +1,92 @@
using System.Reflection;
namespace Ghost.Entities.Systems;
internal class SystemDependencyBuilder(List<Type> allSystemTypes)
{
private Dictionary<Type, List<Type>> _dependencies = new();
public void BuildDependencyGraph()
{
foreach (var systemType in allSystemTypes)
{
if (!typeof(ISystem).IsAssignableFrom(systemType) || systemType.IsAbstract || systemType.IsInterface)
{
throw new ArgumentException($"{systemType.Name} is not a concrete ISystem type.");
}
var directDependencies = new List<Type>();
var dependsOnAttributes = systemType.GetCustomAttributes<DependsOnAttribute>(false);
foreach (var attr in dependsOnAttributes)
{
directDependencies.AddRange(attr.Prerequisites);
}
_dependencies[systemType] = directDependencies;
}
}
private void Visit(Type systemType, HashSet<Type> visited, HashSet<Type> permanentMark, List<Type> executionOrder)
{
if (permanentMark.Contains(systemType))
{
return;
}
if (visited.Contains(systemType))
{
throw new InvalidOperationException($"Circular dependency detected involving system: {systemType.Name}");
}
visited.Add(systemType); // Mark as currently visiting
if (_dependencies.TryGetValue(systemType, out var directDependencies))
{
foreach (var dependencyType in directDependencies)
{
// Ensure the dependency is a registered system type
if (!allSystemTypes.Contains(dependencyType))
{
throw new InvalidOperationException($"System {systemType.Name} depends on unregistered system {dependencyType.Name}.");
}
Visit(dependencyType, visited, permanentMark, executionOrder);
}
}
visited.Remove(systemType); // Done visiting this node in the current path
permanentMark.Add(systemType); // Mark as permanently processed
executionOrder.Add(systemType); // Add to the sorted list (this will be reversed later for correct order)
}
/// <summary>
/// Builds the topological order of systems.
/// </summary>
/// <returns>A list of system types in the order they should be executed.</returns>
/// <exception cref="InvalidOperationException">Thrown if a circular dependency is detected.</exception>"
public List<Type> BuildExecutionOrder()
{
var executionOrder = new List<Type>(allSystemTypes.Count);
var visited = new HashSet<Type>(); // Tracks visited nodes in the current DFS path (for cycle detection)
var permanentMark = new HashSet<Type>(); // Tracks nodes whose dependencies have been fully resolved
// Initialize dependencies for all registered systems, even those without explicit attributes
foreach (var sysType in allSystemTypes)
{
if (!_dependencies.ContainsKey(sysType))
{
_dependencies[sysType] = new();
}
}
foreach (var systemType in allSystemTypes)
{
if (!permanentMark.Contains(systemType))
{
Visit(systemType, visited, permanentMark, executionOrder);
}
}
return executionOrder;
}
}

View File

@@ -0,0 +1,10 @@
namespace Ghost.Entities.Systems;
public struct SystemState
{
public World World
{
get;
init;
}
}

View File

@@ -0,0 +1,95 @@
using System.Runtime.CompilerServices;
namespace Ghost.Entities.Systems;
[SkipLocalsInit]
public struct SystemStorage
{
private readonly List<Type> _systems = new();
private readonly List<ISystem> _executionList = new();
private readonly World _world;
public event Action<Type>? SystemAdded;
public event Action<Type>? SystemRemoved;
internal SystemStorage(World world)
{
_world = world;
}
public readonly void AddSystem(Type systemType)
{
_systems.Add(systemType);
SystemAdded?.Invoke(systemType);
}
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public readonly void AddSystem<T>()
where T : ISystem, new()
{
AddSystem(typeof(T));
}
public readonly void RemoveSystem(Type systemType)
{
_systems.Remove(systemType);
SystemRemoved?.Invoke(systemType);
}
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public readonly void RemoveSystem<T>()
where T : ISystem, new()
{
RemoveSystem(typeof(T));
}
internal void CreateSystems()
{
var builder = new SystemDependencyBuilder(_systems);
builder.BuildDependencyGraph();
var executionOrder = builder.BuildExecutionOrder();
var state = new SystemState()
{
World = _world,
};
foreach (var systemType in executionOrder)
{
var system = (ISystem?)Activator.CreateInstance(systemType) ?? throw new InvalidOperationException($"Failed to create instance of system type {systemType.Name}.");
_executionList.Add(system);
system.OnCreate(in state);
}
}
internal void UpdateSystems()
{
var state = new SystemState()
{
World = _world,
};
foreach (var system in _executionList)
{
system.OnUpdate(in state);
}
}
internal void Dispose()
{
var state = new SystemState()
{
World = _world,
};
foreach (var system in _executionList)
{
system.OnDestroy(in state);
}
_systems.Clear();
_executionList.Clear();
}
}