Refactor folder structure

This commit is contained in:
2026-02-18 00:50:46 +09:00
parent 426786397c
commit db8ca971a8
413 changed files with 2885 additions and 3634 deletions

View File

@@ -0,0 +1,11 @@
using Ghost.Engine.Models;
namespace Ghost.Engine;
internal static class ActivationHandler
{
public static void Handle(LaunchArgument args)
{
}
}

View File

@@ -0,0 +1,7 @@
using Ghost.Core.Attributes;
using System.Runtime.CompilerServices;
[assembly: InternalsVisibleTo("Ghost.Editor")]
[assembly: InternalsVisibleTo("Ghost.Editor.Core")]
[assembly: EngineAssembly]

View File

@@ -0,0 +1,24 @@
using Ghost.Engine.Editor;
using Ghost.Entities;
using System.Runtime.CompilerServices;
namespace Ghost.Engine.Components;
[HideEditor]
public struct Hierarchy : IComponent
{
public Entity parent;
public Entity firstChild;
public Entity nextSibling;
public static Hierarchy Root
{
[MethodImpl(MethodImplOptions.AggressiveInlining)]
get => new()
{
parent = Entity.Invalid,
firstChild = Entity.Invalid,
nextSibling = Entity.Invalid
};
}
}

View File

@@ -0,0 +1,9 @@
using Ghost.Entities;
using Misaki.HighPerformance.Mathematics;
namespace Ghost.Engine.Components;
public struct LocalToWorld : IComponent
{
public float4x4 matrix;
}

View File

@@ -0,0 +1,9 @@
using Ghost.Engine.Core;
using Ghost.Entities;
namespace Ghost.Engine.Components;
public struct SceneID : IComponent // TODO: ISharedComponent
{
public Scene scene;
}

View File

@@ -0,0 +1,154 @@
using Ghost.Entities;
using Misaki.HighPerformance.LowLevel.Buffer;
using Misaki.HighPerformance.LowLevel.Collections;
namespace Ghost.Engine.Core;
/// <summary>
/// Represents a runtime scene - a collection of entities with the same SceneID.
/// </summary>
public readonly struct Scene : IEquatable<Scene>
{
private readonly short _id;
/// <summary>
/// Gets the unique identifier of this scene.
/// </summary>
public short ID => _id;
/// <summary>
/// Gets whether this scene is valid.
/// </summary>
public bool IsValid => _id >= 0;
/// <summary>
/// Gets an invalid scene instance.
/// </summary>
public static Scene Invalid => new(-1);
internal Scene(short id)
{
_id = id;
}
public bool Equals(Scene other)
{
return _id == other._id;
}
public override bool Equals(object? obj)
{
return obj is Scene other && Equals(other);
}
public override int GetHashCode()
{
return _id.GetHashCode();
}
public static bool operator ==(Scene left, Scene right)
{
return left.Equals(right);
}
public static bool operator !=(Scene left, Scene right)
{
return !left.Equals(right);
}
public override string ToString()
{
return $"Scene(ID: {_id})";
}
}
/// <summary>
/// Manages scenes within a world.
/// </summary>
/// <remarks>
/// This is a minimal runtime representation. All metadata (like scene names)
/// should be stored in editor-only classes (SceneNode).
/// </remarks>
public static class SceneManager
{
private static short s_nextSceneID;
private static readonly Queue<short> s_recycledSceneIDs = new();
/// <summary>
/// Creates a new scene in the world.
/// </summary>
/// <returns>The created scene.</returns>
public static Scene CreateScene()
{
if (!s_recycledSceneIDs.TryDequeue(out var id))
{
id = s_nextSceneID++;
}
return new Scene(id);
}
/// <summary>
/// Destroys all entities belonging to the specified scene.
/// </summary>
/// <param name="scene">The scene to unload.</param>
/// <param name="world">The world containing the entities.</param>
public static void UnloadScene(Scene scene, World world)
{
var queryID = new QueryBuilder().WithAll<Components.SceneID>().Build(world);
ref var query = ref world.ComponentManager.GetEntityQueryReference(queryID);
using var scope = AllocationManager.CreateStackScope();
var entitiesToDestroy = new UnsafeList<Entity>(128, scope.AllocationHandle);
// Iterate through all matching entities
foreach (var chunk in query.GetChunkIterator())
{
var entities = chunk.GetEntities();
var sceneIDs = chunk.GetComponentData<Components.SceneID>();
for (var i = 0; i < chunk.Count; i++)
{
if (sceneIDs[i].scene.ID == scene.ID)
{
entitiesToDestroy.Add(entities[i]);
}
}
}
world.EntityManager.DestroyEntities(entitiesToDestroy.AsSpan());
s_recycledSceneIDs.Enqueue(scene.ID);
}
/// <summary>
/// Gets all entities belonging to the specified scene.
/// </summary>
/// <param name="scene">The scene to query.</param>
/// <param name="world">The world containing the entities.</param>
/// <param name="entities">Span to store the entities.</param>
/// <returns>The number of entities written to the span.</returns>
public static UnsafeList<Entity> GetSceneEntities(Scene scene, World world, AllocationHandle handle)
{
var queryID = new QueryBuilder().WithAll<Components.SceneID>().Build(world);
ref var query = ref world.ComponentManager.GetEntityQueryReference(queryID);
var entities = new UnsafeList<Entity>(128, handle);
// Iterate through all matching entities
foreach (var chunk in query.GetChunkIterator())
{
var chunkEntities = chunk.GetEntities();
var sceneIDs = chunk.GetComponentData<Components.SceneID>();
for (var i = 0; i < chunk.Count; i++)
{
if (sceneIDs[i].scene.ID == scene.ID)
{
entities.Add(chunkEntities[i]);
}
}
}
return entities;
}
}

View File

@@ -0,0 +1,6 @@
namespace Ghost.Engine.Editor;
[AttributeUsage(AttributeTargets.Class | AttributeTargets.Struct | AttributeTargets.Property | AttributeTargets.Field, Inherited = false, AllowMultiple = false)]
public class HideEditorAttribute : Attribute
{
}

View File

@@ -0,0 +1,38 @@
using Ghost.Entities;
using Misaki.HighPerformance.Jobs;
namespace Ghost.Engine;
public interface IEngineContext : IDisposable
{
JobScheduler JobScheduler { get; }
}
[AttributeUsage(AttributeTargets.Class, AllowMultiple = false, Inherited = false)]
internal class EngineEntryAttribute : Attribute
{
}
[EngineEntry]
internal sealed partial class EngineCore : IEngineContext
{
private readonly JobScheduler _jobScheduler;
public JobScheduler JobScheduler => _jobScheduler;
public EngineCore()
{
_jobScheduler = new JobScheduler(Environment.ProcessorCount - 2); // We -2 here, one for main thread, one for render thread
ComponentRegistry.GetOrRegisterComponentID<ManagedEntityRef>();
}
public void Init()
{
}
public void Dispose()
{
_jobScheduler.Dispose();
}
}

View File

@@ -0,0 +1,37 @@
<Project Sdk="Microsoft.NET.Sdk">
<PropertyGroup>
<TargetFramework>net10.0</TargetFramework>
<ImplicitUsings>enable</ImplicitUsings>
<Nullable>enable</Nullable>
<AllowUnsafeBlocks>True</AllowUnsafeBlocks>
</PropertyGroup>
<PropertyGroup Condition="'$(Configuration)|$(Platform)'=='Debug|AnyCPU'">
<IsAotCompatible>True</IsAotCompatible>
<IsTrimmable>True</IsTrimmable>
</PropertyGroup>
<PropertyGroup Condition="'$(Configuration)|$(Platform)'=='Release|AnyCPU'">
<IsAotCompatible>True</IsAotCompatible>
<IsTrimmable>True</IsTrimmable>
</PropertyGroup>
<ItemGroup>
<Compile Remove="Services\**" />
<EmbeddedResource Remove="Services\**" />
<None Remove="Services\**" />
</ItemGroup>
<ItemGroup>
<ProjectReference Include="..\Ghost.Core\Ghost.Core.csproj" />
<ProjectReference Include="..\Ghost.Entities\Ghost.Entities.csproj" />
<!--<ProjectReference Include="..\Ghost.Generator\Ghost.Generator.csproj" OutputItemType="Analyzer" ReferenceOutputAssembly="false" />-->
<ProjectReference Include="..\Ghost.Graphics\Ghost.Graphics.csproj" />
</ItemGroup>
<ItemGroup>
<PackageReference Include="MemoryPack" Version="1.21.4" />
</ItemGroup>
</Project>

View File

@@ -0,0 +1,32 @@
using System.Diagnostics.CodeAnalysis;
using System.Text.Json;
using System.Text.Json.Serialization;
namespace Ghost.Engine.IO;
public class CustomSerializerAttribute : JsonConverterAttribute
{
public CustomSerializerAttribute([DynamicallyAccessedMembers(DynamicallyAccessedMemberTypes.PublicParameterlessConstructor)] Type serializerType)
: base(serializerType)
{
}
}
public abstract class CustomSerializer<T> : JsonConverter<T>
{
public sealed override T? Read(ref Utf8JsonReader reader, Type typeToConvert, JsonSerializerOptions options)
{
return DeserializeJson(ref reader, options);
}
public sealed override void Write(Utf8JsonWriter writer, T value, JsonSerializerOptions options)
{
SerializeJson(writer, value, options);
}
public abstract void SerializeJson(Utf8JsonWriter writer, T value, JsonSerializerOptions options);
public abstract T? DeserializeJson(ref Utf8JsonReader reader, JsonSerializerOptions options);
public abstract void SerializeBinary(BinaryWriter writer, T value);
public abstract T? DeserializeBinary(BinaryReader reader);
}

View File

@@ -0,0 +1,5 @@
namespace Ghost.Engine.Models;
internal class LaunchArgument
{
}

View File

@@ -0,0 +1,8 @@
namespace Ghost.Engine.Resources;
internal class EngineData
{
public const string ENGINE_NAME = "Ghost Engine";
public readonly static Version EngineVersion = new(0, 1, 0);
}

View File

@@ -0,0 +1,13 @@
using System.Text.Json;
namespace Ghost.Engine.Resources;
public static class EngineResource
{
public static readonly JsonSerializerOptions defaultSerializerOptions = new()
{
WriteIndented = true,
IncludeFields = true,
IgnoreReadOnlyProperties = true,
};
}

View File

@@ -0,0 +1,54 @@
using Misaki.HighPerformance.Mathematics;
using System.Runtime.CompilerServices;
namespace Ghost.Engine.Utilities;
public static class MathUtility
{
extension(float4x4 matrix)
{
/// <summary>
/// Creates a transformation matrix from position, rotation, and scale vectors.
/// </summary>
/// <param name="position">Defines the translation component of the transformation matrix.</param>
/// <param name="rotation">Specifies the orientation of the object in 3D space.</param>
/// <param name="scale">Determines the size of the object along each axis.</param>
/// <returns>Returns a transformation matrix that combines the specified position, rotation, and scale.</returns>
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public static float4x4 TRS(float3 position, quaternion rotation, float3 scale)
{
var R = new float3x3(rotation);
return new float4x4(
R[0][0] * scale.x, R[0][1] * scale.y, R[0][2] * scale.z, position.x,
R[1][0] * scale.x, R[1][1] * scale.y, R[1][2] * scale.z, position.y,
R[2][0] * scale.x, R[2][1] * scale.y, R[2][2] * scale.z, position.z,
0f, 0f, 0f, 1f
);
}
/// <summary>
/// Gets the translation, rotation, and scale components from a transformation matrix.
/// </summary>
/// <param name="position">The position component extracted from the matrix.</param>
/// <param name="rotation">The rotation component extracted from the matrix as a quaternion.</param>
/// <param name="scale">The scale component extracted from the matrix.</param>
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public void GetTRS(out float3 position, out quaternion rotation, out float3 scale)
{
position = matrix.c3.xyz;
var scaleX = math.length(matrix.c0.xyz);
var scaleY = math.length(matrix.c1.xyz);
var scaleZ = math.length(matrix.c2.xyz);
scale = new float3(scaleX, scaleY, scaleZ);
var rotationMatrix = new float3x3(
matrix.c0.x / scale.x, matrix.c0.y / scale.x, matrix.c0.z / scale.x,
matrix.c1.x / scale.y, matrix.c1.y / scale.y, matrix.c1.z / scale.y,
matrix.c2.x / scale.z, matrix.c2.y / scale.z, matrix.c2.z / scale.z
);
rotation = new quaternion(rotationMatrix);
}
}
}

View File

@@ -0,0 +1,78 @@
using System.Text.Json;
namespace Ghost.Engine.Utilities;
public readonly ref struct Utf8JsonObjectScope : IDisposable
{
private readonly Utf8JsonWriter _writer;
public Utf8JsonObjectScope(Utf8JsonWriter writer)
{
_writer = writer;
_writer.WriteStartObject();
}
public void Dispose()
{
_writer.WriteEndObject();
}
}
public readonly ref struct Utf8JsonArrayScope : IDisposable
{
private readonly Utf8JsonWriter _writer;
public Utf8JsonArrayScope(Utf8JsonWriter writer)
{
_writer = writer;
_writer.WriteStartArray();
}
public void Dispose()
{
_writer.WriteEndArray();
}
}
public static class Utf8JsonWriterExtension
{
public static void WriteArray<T>(this Utf8JsonWriter writer, ReadOnlySpan<char> name, IEnumerable<T> source, Action<T> writeAction)
{
writer.WriteStartArray(name);
foreach (var item in source)
{
writeAction(item);
}
writer.WriteEndArray();
}
public static void WriteArray<T>(this Utf8JsonWriter writer, ReadOnlySpan<char> name, ReadOnlySpan<T> source, Action<T> writeAction)
{
writer.WriteStartArray(name);
foreach (var item in source)
{
writeAction(item);
}
writer.WriteEndArray();
}
public static void WriteObject(this Utf8JsonWriter writer, Action writeAction)
{
writer.WriteStartObject();
writeAction();
writer.WriteEndObject();
}
public static void WriteObject(this Utf8JsonWriter writer, ReadOnlySpan<char> name, Action writeAction)
{
writer.WriteStartObject(name);
writeAction();
writer.WriteEndObject();
}
public static Utf8JsonObjectScope StartObjectScope(this Utf8JsonWriter writer)
{
return new Utf8JsonObjectScope(writer);
}
public static Utf8JsonArrayScope StartArrayScope(this Utf8JsonWriter writer)
{
return new Utf8JsonArrayScope(writer);
}
}