Start of serializaation

- Allow enumerating over unregistered components
- Refactored Editor a bit
This commit is contained in:
Simon Lübeß
2022-05-14 23:01:08 +02:00
parent 1d3d30f88d
commit 3956080520
15 changed files with 845 additions and 248 deletions
+6
View File
@@ -167,6 +167,12 @@ namespace GlitchyEngine
}
}
/// Closes the applcation.
public void Close()
{
_running = false;
}
public void PushLayer(Layer ownLayer)
{
Profiler.ProfileFunction!();
@@ -0,0 +1,37 @@
using System;
namespace Bon.Integrated
{
extension Serialize
{
public static void Value<T>(BonWriter writer, StringView identifier, in T value, BonEnvironment env = gBonEnv)
{
writer.Identifier(identifier);
Serialize.Value(writer, ValueView(typeof(T), &value), env);
}
public static void Value<T>(BonWriter writer, in T value, BonEnvironment env = gBonEnv)
{
Serialize.Value(writer, ValueView(typeof(T), &value), env);
}
}
extension Deserialize
{
public static Result<void> Value<T>(BonReader reader, StringView identifier, out T value, BonEnvironment env = gBonEnv)
{
value = ?;
if (Try!(reader.Identifier()) != identifier)
return .Err;
return Deserialize.Value(reader, ValueView(typeof(T), &value), env);
}
public static Result<void> Value<T>(BonReader reader, out T value, BonEnvironment env = gBonEnv)
{
value = ?;
return Deserialize.Value(reader, ValueView(typeof(T), &value), env);
}
}
}
+2
View File
@@ -1,7 +1,9 @@
using Bon;
using System;
namespace GlitchyEngine.Math
{
[BonTarget]
public struct Quaternion
{
public const Quaternion Zero = .();
+2
View File
@@ -1,7 +1,9 @@
using Bon;
using System;
namespace GlitchyEngine.Math
{
[BonTarget]
[SwizzleVector(2, "Vector")]
public struct Vector2
{
+2
View File
@@ -1,7 +1,9 @@
using Bon;
using System;
namespace GlitchyEngine.Math
{
[BonTarget]
[SwizzleVector(3, "Vector")]
public struct Vector3
{
+2
View File
@@ -1,7 +1,9 @@
using Bon;
using System;
namespace GlitchyEngine.Math
{
[BonTarget]
[SwizzleVector(4, "Vector")]
public struct Vector4
{
@@ -4,6 +4,7 @@
#pragma warning disable 4204
using System;
using Bon;
namespace GlitchyEngine.Math
{
@@ -35,4 +36,16 @@ namespace GlitchyEngine.Math
}
}
namespace DirectX
{
[BonTarget]
extension Color;
[BonTarget]
extension ColorRGB;
[BonTarget]
extension ColorRGBA;
}
#endif
+2 -8
View File
@@ -1,6 +1,6 @@
using System;
using GlitchyEngine.Math;
using GlitchyEngine.Renderer;
using System;
namespace GlitchyEngine.World
{
@@ -251,9 +251,8 @@ namespace GlitchyEngine.World
struct CameraComponent : IDisposableComponent
{
public SceneCamera Camera;
public SceneCamera Camera = .();
public bool Primary = true; // Todo: probably move into scene
public bool FixedAspectRatio = false;
private RenderTarget2D _renderTarget = null;
public RenderTarget2D RenderTarget
@@ -265,11 +264,6 @@ namespace GlitchyEngine.World
}
}
public this()
{
Camera = .();
}
public void Dispose()
{
_renderTarget?.ReleaseRef();
+2 -2
View File
@@ -56,7 +56,7 @@ namespace GlitchyEngine.World
for (var (entity, transform, camera) in _ecsWorld.Enumerate<TransformComponent, CameraComponent>())
{
if (camera.Primary)
if (camera.Primary && camera.RenderTarget != null)
{
primaryCamera = &camera.Camera;
primaryCameraTransform = transform.WorldTransform;
@@ -134,7 +134,7 @@ namespace GlitchyEngine.World
for (var (entity, cameraComponent) in _ecsWorld.Enumerate<CameraComponent>())
{
if (!cameraComponent.FixedAspectRatio)
if (!cameraComponent.Camera.FixedAspectRatio)
{
cameraComponent.Camera.SetViewportSize(width, height);
}
+445
View File
@@ -0,0 +1,445 @@
using Bon;
using Bon.Integrated;
using System;
using System.Reflection;
using System.IO;
using GlitchyEngine.Math;
namespace GlitchyEngine.World
{
using internal GlitchyEngine.World;
class SceneSerializer
{
private Scene _scene;
public this(Scene scene)
{
_scene = scene;
}
public void Serialize(StringView filePath)
{
Debug.Profiler.ProfileResourceFunction!();
String buffer = scope String();
let writer = scope BonWriter(buffer, true);
Serialize.Start(writer);
gBonEnv.serializeFlags |= .IncludeDefault | .Verbose;
using (writer.ObjectBlock())
{
// TODO: Scene name goes here!
Serialize.Value(writer, "Name", "Scene name here pls!!!");
writer.Identifier("Entities");
using (writer.ArrayBlock())
{
for (EcsEntity e in _scene._ecsWorld.Enumerate())
{
Entity entity = .(e, _scene);
// TODO: also serialize object with EditorComponent (e.g. to save the location of the editor camera)
if (entity.HasComponent<EditorComponent>())
continue;
SerializeEntity(writer, entity);
}
}
writer.EntryEnd();
}
Serialize.End(writer);
String targetDirectory = Path.GetDirectoryPath(filePath, .. scope String());
Directory.CreateDirectory(targetDirectory);
File.WriteAllText(filePath, buffer);
}
static void SerializeEntity(BonWriter writer, Entity entity)
{
writer.EntryStart();
using (writer.ObjectBlock())
{
// TODO: Entity GUID goes here!
Serialize.Value(writer, "Id", entity.Handle.Index);
SerializeComponent<EditorComponent>(writer, entity, "EditorComponent", scope (component) => {});
SerializeComponent<DebugNameComponent>(writer, entity, "NameComponent", scope (component) =>
{
Serialize.Value(writer, "Name", component.DebugName);
});
SerializeComponent<SpriterRendererComponent>(writer, entity, "SpriterRendererComponent", scope (component) =>
{
// TODO: Texture
Serialize.Value(writer, "Color", component.Color);
});
SerializeComponent<TransformComponent>(writer, entity, "TransformComponent", scope (component) =>
{
// TODO: Use GUIDs
if (component.Parent != .InvalidEntity)
Serialize.Value(writer, "ParentId", component.Parent.Index);
Serialize.Value(writer, "Position", component.Position);
Serialize.Value(writer, "Rotation", component.Rotation);
Serialize.Value(writer, "Scale", component.Scale);
Serialize.Value(writer, "EditorEulerRotation", component.EditorRotationEuler);
});
SerializeComponent<CameraComponent>(writer, entity, "CameraComponent", scope (component) =>
{
SceneCamera camera = component.Camera;
Serialize.Value(writer, "Primary", component.Primary);
// TODO: Render target
Serialize.Value(writer, "ProjectionType", camera.ProjectionType);
Serialize.Value(writer, "PerspectiveFovY", camera.PerspectiveFovY);
Serialize.Value(writer, "PerspectiveNearPlane", camera.PerspectiveNearPlane);
Serialize.Value(writer, "PerspectiveFarPlane", camera.PerspectiveFarPlane);
Serialize.Value(writer, "OrthographicHeight", camera.OrthographicHeight);
Serialize.Value(writer, "OrthographicNearPlane", camera.OrthographicNearPlane);
Serialize.Value(writer, "OrthographicFarPlane", camera.OrthographicFarPlane);
Serialize.Value(writer, "AspectRatio", camera.AspectRatio);
Serialize.Value(writer, "FixedAspectRatio", camera.FixedAspectRatio);
});
// TODO: native script component
SerializeComponent<LightComponent>(writer, entity, "LightComponent", scope (component) =>
{
SceneLight light = component.SceneLight;
Serialize.Value(writer, "LightType", light.LightType);
Serialize.Value(writer, "Illuminance", light.Illuminance);
Serialize.Value(writer, "Color", light.Color);
});
}
writer.EntryEnd();
}
static void SerializeComponent<T>(BonWriter writer, Entity entity, String identifier, delegate void(T* component) serialize) where T : struct, new
{
if (!entity.HasComponent<T>())
return;
var component = entity.GetComponent<T>();
writer.Identifier(identifier);
using (writer.ObjectBlock())
{
serialize(component);
}
writer.EntryEnd();
}
public void SerializeRuntime(StringView filePath)
{
Runtime.NotImplemented();
}
public Result<void> Deserialize(StringView filePath)
{
Debug.Profiler.ProfileResourceFunction!();
String buffer = scope String();
File.ReadAllText(filePath, buffer);
let reader = scope BonReader();
Try!(reader.Setup(buffer));
Try!(Deserialize.Start(reader));
Try!(reader.ObjectBlock());
// TODO: Scene name goes here!
String testName;
Deserialize.Value(reader, "Name", out testName);
delete testName;
Try!(reader.EntryEnd());
if (Try!(reader.Identifier()) != "Entities")
return .Err;
Try!(reader.ArrayBlock());
bool first = true;
while (reader.ArrayHasMore())
{
if (!first)
{
Try!(reader.EntryEnd());
}
Try!(DeserializeEntity(reader));
first = false;
}
Try!(reader.ArrayBlockEnd());
Try!(reader.ObjectBlockEnd());
Try!(Deserialize.End(reader));
return .Ok;
}
private Result<void> DeserializeEntity(BonReader reader)
{
Try!(reader.ObjectBlock());
Entity entity = _scene.CreateEntity();
// TODO: GUID?
Deserialize.Value<uint32>(reader, "Id", let id);
while(reader.ObjectHasMore())
{
Try!(reader.EntryEnd());
StringView identifier = Try!(reader.Identifier());
switch(identifier)
{
case "EditorComponent":
Try!(DeserializeComponent<EditorComponent>(reader, entity, scope (component) => { return .Ok; }));
case "NameComponent":
Try!(DeserializeComponent<DebugNameComponent>(reader, entity, scope (component) =>
{
String name;
Deserialize.Value(reader, "Name", out name);
component.SetName(name);
delete name;
return .Ok;
}));
case "NameComponent":
Try!(DeserializeComponent<DebugNameComponent>(reader, entity, scope (component) =>
{
String name;
Deserialize.Value(reader, "Name", out name);
component.SetName(name);
delete name;
return .Ok;
}));
case "SpriterRendererComponent":
Try!(DeserializeComponent<SpriterRendererComponent>(reader, entity, scope (component) =>
{
// TODO: Texture
Try!(Deserialize.Value(reader, "Color", out component.Color));
return .Ok;
}));
case "TransformComponent":
Try!(DeserializeComponent<TransformComponent>(reader, entity, scope (component) =>
{
let nextId = Try!(reader.Identifier());
if (nextId == "ParentId")
{
// TODO: Use GUIDs
uint32 pId;
Deserialize.Value(reader, out pId);
reader.EntryEnd();
Deserialize.Value(reader, "Position", out component.[Friend]_position);
reader.EntryEnd();
}
else if (nextId == "Position")
{
Deserialize.Value(reader, out component.[Friend]_position);
reader.EntryEnd();
}
else
{
return .Err;
}
Deserialize.Value(reader, "Rotation", out component.[Friend]_rotation);
reader.EntryEnd();
Deserialize.Value(reader, "Scale", out component.[Friend]_scale);
reader.EntryEnd();
Deserialize.Value(reader, "EditorEulerRotation", out component.[Friend]_editorRotationEuler);
component.IsDirty = true;
return .Ok;
}));
case "CameraComponent":
Try!(DeserializeComponent<CameraComponent>(reader, entity, scope (component) =>
{
SceneCamera camera = component.Camera;
Deserialize.Value(reader, "Primary", out component.Primary);
reader.EntryEnd();
// TODO: Render target
Deserialize.Value(reader, "ProjectionType", out camera.[Friend]_projectionType);
reader.EntryEnd();
Deserialize.Value(reader, "PerspectiveFovY", out camera.[Friend]_perspectiveFovY);
reader.EntryEnd();
Deserialize.Value(reader, "PerspectiveNearPlane", out camera.[Friend]_perspectiveNearPlane);
reader.EntryEnd();
Deserialize.Value(reader, "PerspectiveFarPlane", out camera.[Friend]_perspectiveFarPlane);
reader.EntryEnd();
Deserialize.Value(reader, "OrthographicHeight", out camera.[Friend]_orthographicHeight);
reader.EntryEnd();
Deserialize.Value(reader, "OrthographicNearPlane", out camera.[Friend]_orthographicNearPlane);
reader.EntryEnd();
Deserialize.Value(reader, "OrthographicFarPlane", out camera.[Friend]_orthographicFarPlane);
reader.EntryEnd();
Deserialize.Value(reader, "AspectRatio", out camera.[Friend]_aspectRatio);
reader.EntryEnd();
Deserialize.Value(reader, "FixedAspectRatio", out camera.[Friend]_fixedAspectRatio);
camera.[Friend]CalculateProjection();
return .Ok;
}));
// TODO: native script component
case "LightComponent":
Try!(DeserializeComponent<LightComponent>(reader, entity, scope (component) =>
{
SceneLight light = component.SceneLight;
Deserialize.Value(reader, "LightType", out light.[Friend]_type);
reader.EntryEnd();
Deserialize.Value(reader, "Illuminance", out light.[Friend]_illuminance);
reader.EntryEnd();
Deserialize.Value(reader, "Color", out light.[Friend]_color);
return .Ok;
}));
default:
return .Err;
}
}
Try!(reader.ObjectBlockEnd());
return .Ok;
/*writer.EntryStart();
using (writer.ObjectBlock())
{
// TODO: Entity GUID goes here!
Serialize.Value(writer, "Id", entity.Handle.Index);
SerializeComponent<EditorComponent>(writer, entity, "EditorComponent", scope (component) => {});
SerializeComponent<DebugNameComponent>(writer, entity, "NameComponent", scope (component) =>
{
Serialize.Value(writer, "Name", component.DebugName);
});
SerializeComponent<SpriterRendererComponent>(writer, entity, "SpriterRendererComponent", scope (component) =>
{
// TODO: Texture
Serialize.Value(writer, "Color", component.Color);
});
SerializeComponent<TransformComponent>(writer, entity, "TransformComponent", scope (component) =>
{
// TODO: Use GUIDs
if (component.Parent != .InvalidEntity)
Serialize.Value(writer, "ParentId", component.Parent.Index);
Serialize.Value(writer, "Position", component.Position);
Serialize.Value(writer, "Rotation", component.Rotation);
Serialize.Value(writer, "Scale", component.Scale);
Serialize.Value(writer, "EditorEulerRotation", component.EditorRotationEuler);
});
SerializeComponent<CameraComponent>(writer, entity, "CameraComponent", scope (component) =>
{
SceneCamera camera = component.Camera;
Serialize.Value(writer, "Primary", component.Primary);
// TODO: Render target
Serialize.Value(writer, "ProjectionType", camera.ProjectionType);
Serialize.Value(writer, "PerspectiveFovY", camera.PerspectiveFovY);
Serialize.Value(writer, "PerspectiveNearPlane", camera.PerspectiveNearPlane);
Serialize.Value(writer, "PerspectiveFarPlane", camera.PerspectiveFarPlane);
Serialize.Value(writer, "OrthographicHeight", camera.OrthographicHeight);
Serialize.Value(writer, "OrthographicNearPlane", camera.OrthographicNearPlane);
Serialize.Value(writer, "OrthographicFarPlane", camera.OrthographicFarPlane);
Serialize.Value(writer, "AspectRatio", camera.AspectRatio);
Serialize.Value(writer, "FixedAspectRatio", camera.FixedAspectRatio);
});
// TODO: native script component
SerializeComponent<LightComponent>(writer, entity, "LightComponent", scope (component) =>
{
SceneLight light = component.SceneLight;
Serialize.Value(writer, "LightType", light.LightType);
Serialize.Value(writer, "Illuminance", light.Illuminance);
Serialize.Value(writer, "Color", light.Color);
});
}
writer.EntryEnd();*/
}
static Result<void> DeserializeComponent<T>(BonReader reader, Entity entity, delegate Result<void>(T* component) deserialize) where T : struct, new
{
Try!(reader.ObjectBlock());
T* component;
if (entity.HasComponent<T>())
component = entity.GetComponent<T>();
else
component = entity.AddComponent<T>();
Try!(deserialize(component));
Try!(reader.ObjectBlockEnd());
return .Ok;
}
public bool DeserializeRuntime(StringView filePath)
{
Runtime.NotImplemented();
}
}
}
+51 -11
View File
@@ -13,6 +13,8 @@ namespace GlitchyEngine.World
internal EcsWorld.BitmaskEntry* _currentEntry;
internal EcsWorld.BitmaskEntry* _endEntry;
public bool IsEmpty => _bitMask == null;
public this(EcsWorld world, Type[] componentTypes)
{
_world = world;
@@ -32,7 +34,11 @@ namespace GlitchyEngine.World
}
else
{
Log.EngineLogger.AssertDebug(false, "Queried component is not registered for this world. This is invalid because the query would never return any results.");
#if GE_WORLD_ENUMERATOR_UNREGISTERED_COMPONENT_IS_WARNING
Log.EngineLogger.Warning($"Queried component of type \"{type}\" is not registered for this world. The query will never return any results.");
#endif
DeleteAndNullify!(_bitMask);
_endEntry = _currentEntry;
}
}
}
@@ -67,7 +73,14 @@ namespace GlitchyEngine.World
public this(EcsWorld world) : base(world, scope Type[](typeof(TComponent)))
{
_componentPool = &world.GetComponentPool<TComponent>();
if (base.IsEmpty)
{
_componentPool = null;
}
else
{
_componentPool = &world.GetComponentPool<TComponent>();
}
}
public new Result<(EcsEntity Entity, TComponent* Component)> GetNext() mut
@@ -92,8 +105,16 @@ namespace GlitchyEngine.World
public this(EcsWorld world) : base(world, scope Type[](typeof(TComponent0), typeof(TComponent1)))
{
_componentPool0 = &world.GetComponentPool<TComponent0>();
_componentPool1 = &world.GetComponentPool<TComponent1>();
if (base.IsEmpty)
{
_componentPool0 = null;
_componentPool1 = null;
}
else
{
_componentPool0 = &world.GetComponentPool<TComponent0>();
_componentPool1 = &world.GetComponentPool<TComponent1>();
}
}
public new Result<(EcsEntity Entity, TComponent0* Component0, TComponent1* Component1)> GetNext() mut
@@ -120,9 +141,18 @@ namespace GlitchyEngine.World
public this(EcsWorld world) : base(world, scope Type[](typeof(TComponent0), typeof(TComponent1), typeof(TComponent2)))
{
_componentPool0 = &world.GetComponentPool<TComponent0>();
_componentPool1 = &world.GetComponentPool<TComponent1>();
_componentPool2 = &world.GetComponentPool<TComponent2>();
if (base.IsEmpty)
{
_componentPool0 = null;
_componentPool1 = null;
_componentPool2 = null;
}
else
{
_componentPool0 = &world.GetComponentPool<TComponent0>();
_componentPool1 = &world.GetComponentPool<TComponent1>();
_componentPool2 = &world.GetComponentPool<TComponent2>();
}
}
public new Result<(EcsEntity Entity, TComponent0* Component0, TComponent1* Component1, TComponent2* Component2)> GetNext() mut
@@ -151,10 +181,20 @@ namespace GlitchyEngine.World
public this(EcsWorld world) : base(world, scope Type[](typeof(TComponent0), typeof(TComponent1), typeof(TComponent2), typeof(TComponent3)))
{
_componentPool0 = &world.GetComponentPool<TComponent0>();
_componentPool1 = &world.GetComponentPool<TComponent1>();
_componentPool2 = &world.GetComponentPool<TComponent2>();
_componentPool3 = &world.GetComponentPool<TComponent3>();
if (base.IsEmpty)
{
_componentPool0 = null;
_componentPool1 = null;
_componentPool2 = null;
_componentPool3 = null;
}
else
{
_componentPool0 = &world.GetComponentPool<TComponent0>();
_componentPool1 = &world.GetComponentPool<TComponent1>();
_componentPool2 = &world.GetComponentPool<TComponent2>();
_componentPool3 = &world.GetComponentPool<TComponent3>();
}
}
public new Result<(EcsEntity Entity, TComponent0* Component0, TComponent1* Component1, TComponent2* Component2, TComponent3* Component3)> GetNext() mut