wip: Serialization Test
This commit is contained in:
parent
0148afea52
commit
1df5eee59d
|
@ -1,53 +0,0 @@
|
|||
using System;
|
||||
using System.Collections.Generic;
|
||||
using Engine.Serialization.DTOs;
|
||||
|
||||
using YamlDotNet.Core;
|
||||
using YamlDotNet.Core.Events;
|
||||
using YamlDotNet.Serialization;
|
||||
|
||||
namespace Engine.Serialization;
|
||||
|
||||
internal class BehaviourControllerYamlConverter : IYamlTypeConverter
|
||||
{
|
||||
public bool Accepts(Type type) => type == typeof(BehaviourControllerDTO);
|
||||
|
||||
public object ReadYaml(IParser parser, Type type)
|
||||
{
|
||||
if (parser.Current is not MappingStart)
|
||||
throw new InvalidOperationException("Expected MappingStart");
|
||||
|
||||
parser.MoveNext();
|
||||
var behaviourController = new BehaviourControllerDTO();
|
||||
while (parser.Current != null && parser.Current is not MappingEnd)
|
||||
{
|
||||
var propertyName = ((Scalar)parser.Current).Value;
|
||||
parser.MoveNext();
|
||||
switch (propertyName)
|
||||
{
|
||||
case nameof(BehaviourControllerDTO.ClassType):
|
||||
behaviourController.ClassType = ((Scalar)parser.Current).Value;
|
||||
break;
|
||||
case nameof(BehaviourControllerDTO.Behaviours):
|
||||
behaviourController.Behaviours = (List<BehaviourDTO>)new BehaviourDTOListConverter().ReadYaml(parser, typeof(List<BehaviourDTO>));
|
||||
break;
|
||||
}
|
||||
parser.MoveNext();
|
||||
}
|
||||
return behaviourController;
|
||||
}
|
||||
|
||||
public void WriteYaml(IEmitter emitter, object? value, Type type)
|
||||
{
|
||||
var behaviourController = (BehaviourControllerDTO)(value ?? throw new Exception());
|
||||
|
||||
BehaviourDTOListConverter behaviourDTOListConverter = new();
|
||||
|
||||
emitter.Emit(new MappingStart());
|
||||
emitter.Emit(new Scalar(nameof(BehaviourControllerDTO.ClassType)));
|
||||
emitter.Emit(new Scalar(behaviourController.ClassType));
|
||||
emitter.Emit(new Scalar(nameof(BehaviourControllerDTO.Behaviours)));
|
||||
behaviourDTOListConverter.WriteYaml(emitter, behaviourController.Behaviours, typeof(List<BehaviourDTO>));
|
||||
emitter.Emit(new MappingEnd());
|
||||
}
|
||||
}
|
|
@ -1,41 +0,0 @@
|
|||
using System;
|
||||
using System.Collections.Generic;
|
||||
using Engine.Serialization.DTOs;
|
||||
|
||||
using YamlDotNet.Core;
|
||||
using YamlDotNet.Core.Events;
|
||||
using YamlDotNet.Serialization;
|
||||
|
||||
namespace Engine.Serialization;
|
||||
|
||||
public class BehaviourDTOListConverter : IYamlTypeConverter
|
||||
{
|
||||
public bool Accepts(Type type)
|
||||
{
|
||||
return type == typeof(List<BehaviourDTO>);
|
||||
}
|
||||
|
||||
public object ReadYaml(IParser parser, Type type)
|
||||
{
|
||||
var behaviours = new List<BehaviourDTO>();
|
||||
if (parser.Current is SequenceStart)
|
||||
{
|
||||
parser.MoveNext();
|
||||
while (parser.Current != null && !(parser.Current is SequenceEnd))
|
||||
{
|
||||
behaviours.Add((BehaviourDTO)new BehaviourYamlConverter().ReadYaml(parser, typeof(BehaviourDTO)));
|
||||
parser.MoveNext();
|
||||
}
|
||||
}
|
||||
return behaviours;
|
||||
}
|
||||
|
||||
public void WriteYaml(IEmitter emitter, object? value, Type type)
|
||||
{
|
||||
var behaviours = (List<BehaviourDTO>)(value ?? throw new Exception());
|
||||
emitter.Emit(new SequenceStart(null, null, false, SequenceStyle.Block));
|
||||
foreach (var behaviour in behaviours)
|
||||
new BehaviourYamlConverter().WriteYaml(emitter, behaviour, typeof(BehaviourDTO));
|
||||
emitter.Emit(new SequenceEnd());
|
||||
}
|
||||
}
|
|
@ -1,62 +0,0 @@
|
|||
using System;
|
||||
|
||||
using Engine.Serialization.DTOs;
|
||||
|
||||
using YamlDotNet.Core;
|
||||
using YamlDotNet.Core.Events;
|
||||
using YamlDotNet.Serialization;
|
||||
|
||||
namespace Engine.Serialization;
|
||||
|
||||
internal class BehaviourYamlConverter : IYamlTypeConverter
|
||||
{
|
||||
public bool Accepts(Type type) => type == typeof(BehaviourDTO);
|
||||
|
||||
public object ReadYaml(IParser parser, Type type)
|
||||
{
|
||||
if (parser.Current is not MappingStart)
|
||||
throw new InvalidOperationException("Expected MappingStart");
|
||||
|
||||
parser.MoveNext();
|
||||
var behaviour = new BehaviourDTO();
|
||||
while (parser.Current != null && parser.Current is not MappingEnd)
|
||||
{
|
||||
var propertyName = ((Scalar)parser.Current).Value;
|
||||
parser.MoveNext();
|
||||
switch (propertyName)
|
||||
{
|
||||
case nameof(BehaviourDTO.ClassType):
|
||||
behaviour.ClassType = ((Scalar)parser.Current).Value;
|
||||
break;
|
||||
case nameof(BehaviourDTO.Priority):
|
||||
behaviour.Priority = int.Parse(((Scalar)parser.Current).Value);
|
||||
break;
|
||||
case nameof(BehaviourDTO.StateEnable):
|
||||
behaviour.StateEnable = (StateEnableDTO)(new StateEnableYamlConverter().ReadYaml(parser, typeof(StateEnableDTO)) ?? new Exception());
|
||||
break;
|
||||
}
|
||||
parser.MoveNext();
|
||||
}
|
||||
return behaviour;
|
||||
}
|
||||
|
||||
public void WriteYaml(IEmitter emitter, object? value, Type type)
|
||||
{
|
||||
var behaviour = (BehaviourDTO)(value ?? throw new Exception());
|
||||
|
||||
StateEnableYamlConverter stateEnableYamlConverter = new();
|
||||
|
||||
emitter.Emit(new MappingStart());
|
||||
|
||||
emitter.Emit(new Scalar(nameof(BehaviourDTO.ClassType)));
|
||||
emitter.Emit(new Scalar(behaviour.ClassType.ToString()));
|
||||
|
||||
emitter.Emit(new Scalar(nameof(BehaviourDTO.Priority)));
|
||||
emitter.Emit(new Scalar(behaviour.Priority.ToString()));
|
||||
|
||||
emitter.Emit(new Scalar(nameof(BehaviourDTO.StateEnable)));
|
||||
stateEnableYamlConverter.WriteYaml(emitter, behaviour.StateEnable, typeof(StateEnableDTO));
|
||||
|
||||
emitter.Emit(new MappingEnd());
|
||||
}
|
||||
}
|
|
@ -1,8 +1,8 @@
|
|||
using System.Collections.Generic;
|
||||
|
||||
namespace Engine.Serialization.DTOs;
|
||||
namespace Engine.Serialization;
|
||||
|
||||
internal record struct BehaviourControllerDTO(
|
||||
string ClassType,
|
||||
List<BehaviourDTO> Behaviours
|
||||
);
|
||||
public class BehaviourControllerDto : ClassInterchangeableDto
|
||||
{
|
||||
public Dictionary<string, object?> Behaviours { get; set; } = [];
|
||||
}
|
||||
|
|
|
@ -1,7 +0,0 @@
|
|||
namespace Engine.Serialization.DTOs;
|
||||
|
||||
internal record struct BehaviourDTO(
|
||||
string ClassType,
|
||||
int Priority,
|
||||
StateEnableDTO StateEnable
|
||||
);
|
|
@ -0,0 +1,6 @@
|
|||
namespace Engine.Serialization;
|
||||
|
||||
public class ClassInterchangeableDto : EntityDto
|
||||
{
|
||||
public string ClassType { get; set; } = null!;
|
||||
}
|
|
@ -0,0 +1,8 @@
|
|||
using System.Collections.Generic;
|
||||
|
||||
namespace Engine.Serialization;
|
||||
|
||||
public class EntityDto
|
||||
{
|
||||
public Dictionary<string, object?> Fields { get; set; } = [];
|
||||
}
|
|
@ -1,8 +0,0 @@
|
|||
using System.Collections.Generic;
|
||||
|
||||
namespace Engine.Serialization.DTOs;
|
||||
|
||||
internal record struct GameManagerDTO(
|
||||
string ClassType,
|
||||
List<GameObjectDTO> GameObjects
|
||||
);
|
|
@ -1,10 +1,10 @@
|
|||
namespace Engine.Serialization.DTOs;
|
||||
namespace Engine.Serialization;
|
||||
|
||||
internal record struct GameObjectDTO(
|
||||
string ClassType,
|
||||
string Id,
|
||||
string Name,
|
||||
TransformDTO Transform,
|
||||
BehaviourControllerDTO BehaviourController,
|
||||
StateEnableDTO StateEnable
|
||||
);
|
||||
public class GameObjectDto : ClassInterchangeableDto
|
||||
{
|
||||
string Id { get; set; } = null!;
|
||||
string Name { get; set; } = null!;
|
||||
TransformDto Transform { get; set; } = null!;
|
||||
BehaviourControllerDto BehaviourController { get; set; } = null!;
|
||||
StateEnableDto StateEnable { get; set; } = null!;
|
||||
}
|
||||
|
|
|
@ -1,6 +1,7 @@
|
|||
namespace Engine.Serialization.DTOs;
|
||||
namespace Engine.Serialization;
|
||||
|
||||
internal record struct StateEnableDTO(
|
||||
string ClassType,
|
||||
bool Enabled
|
||||
);
|
||||
public class StateEnableDto : ClassInterchangeableDto
|
||||
{
|
||||
public bool Enabled { get; set; } = false;
|
||||
|
||||
}
|
||||
|
|
|
@ -1,11 +1,11 @@
|
|||
using Syntriax.Engine.Core;
|
||||
|
||||
namespace Engine.Serialization.DTOs;
|
||||
namespace Engine.Serialization;
|
||||
|
||||
internal record struct TransformDTO(
|
||||
string ClassType,
|
||||
string? ParentId,
|
||||
Vector2D Position,
|
||||
Vector2D Scale,
|
||||
float Rotation
|
||||
);
|
||||
public class TransformDto : ClassInterchangeableDto
|
||||
{
|
||||
string? ParentId { get; set; } = null;
|
||||
Vector2D Position { get; set; } = Vector2D.Zero;
|
||||
Vector2D Scale { get; set; } = Vector2D.Zero;
|
||||
float Rotation { get; set; } = 0f;
|
||||
}
|
||||
|
|
|
@ -1,53 +0,0 @@
|
|||
using System;
|
||||
using System.Collections.Generic;
|
||||
using Engine.Serialization.DTOs;
|
||||
|
||||
using YamlDotNet.Core;
|
||||
using YamlDotNet.Core.Events;
|
||||
using YamlDotNet.Serialization;
|
||||
|
||||
namespace Engine.Serialization;
|
||||
|
||||
internal class GameManagerYamlConverter : IYamlTypeConverter
|
||||
{
|
||||
public bool Accepts(Type type) => type == typeof(GameManagerDTO);
|
||||
|
||||
public object ReadYaml(IParser parser, Type type)
|
||||
{
|
||||
if (parser.Current is not MappingStart)
|
||||
throw new InvalidOperationException("Expected MappingStart");
|
||||
|
||||
parser.MoveNext();
|
||||
var gameManager = new GameManagerDTO();
|
||||
while (parser.Current != null && parser.Current is not MappingEnd)
|
||||
{
|
||||
var propertyName = ((Scalar)parser.Current).Value;
|
||||
parser.MoveNext();
|
||||
switch (propertyName)
|
||||
{
|
||||
case nameof(GameManagerDTO.ClassType):
|
||||
gameManager.ClassType = ((Scalar)parser.Current).Value;
|
||||
break;
|
||||
case nameof(GameManagerDTO.GameObjects):
|
||||
gameManager.GameObjects = (List<GameObjectDTO>)new GameObjectDTOListConverter().ReadYaml(parser, typeof(List<GameObjectDTO>));
|
||||
break;
|
||||
}
|
||||
parser.MoveNext();
|
||||
}
|
||||
return gameManager;
|
||||
}
|
||||
|
||||
public void WriteYaml(IEmitter emitter, object? value, Type type)
|
||||
{
|
||||
var gameManager = (GameManagerDTO)(value ?? throw new Exception());
|
||||
|
||||
GameObjectDTOListConverter gameObjectDTOListConverter = new();
|
||||
|
||||
emitter.Emit(new MappingStart());
|
||||
emitter.Emit(new Scalar(nameof(GameManagerDTO.ClassType)));
|
||||
emitter.Emit(new Scalar(gameManager.ClassType));
|
||||
emitter.Emit(new Scalar(nameof(GameManagerDTO.GameObjects)));
|
||||
gameObjectDTOListConverter.WriteYaml(emitter, gameManager.GameObjects, typeof(List<GameObjectDTO>));
|
||||
emitter.Emit(new MappingEnd());
|
||||
}
|
||||
}
|
|
@ -1,41 +0,0 @@
|
|||
using System;
|
||||
using System.Collections.Generic;
|
||||
using Engine.Serialization.DTOs;
|
||||
|
||||
using YamlDotNet.Core;
|
||||
using YamlDotNet.Core.Events;
|
||||
using YamlDotNet.Serialization;
|
||||
|
||||
namespace Engine.Serialization;
|
||||
|
||||
public class GameObjectDTOListConverter : IYamlTypeConverter
|
||||
{
|
||||
public bool Accepts(Type type)
|
||||
{
|
||||
return type == typeof(List<GameObjectDTO>);
|
||||
}
|
||||
|
||||
public object ReadYaml(IParser parser, Type type)
|
||||
{
|
||||
var gameObjects = new List<GameObjectDTO>();
|
||||
if (parser.Current is SequenceStart)
|
||||
{
|
||||
parser.MoveNext();
|
||||
while (parser.Current != null && !(parser.Current is SequenceEnd))
|
||||
{
|
||||
gameObjects.Add((GameObjectDTO)new GameObjectYamlConverter().ReadYaml(parser, typeof(GameObjectDTO)));
|
||||
parser.MoveNext();
|
||||
}
|
||||
}
|
||||
return gameObjects;
|
||||
}
|
||||
|
||||
public void WriteYaml(IEmitter emitter, object? value, Type type)
|
||||
{
|
||||
var gameObjects = (List<GameObjectDTO>)(value ?? throw new Exception());
|
||||
emitter.Emit(new SequenceStart(null, null, false, SequenceStyle.Block));
|
||||
foreach (var gameObject in gameObjects)
|
||||
new GameObjectYamlConverter().WriteYaml(emitter, gameObject, typeof(BehaviourDTO));
|
||||
emitter.Emit(new SequenceEnd());
|
||||
}
|
||||
}
|
|
@ -1,74 +0,0 @@
|
|||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Linq;
|
||||
|
||||
using Engine.Serialization.DTOs;
|
||||
|
||||
using YamlDotNet.Core;
|
||||
using YamlDotNet.Core.Events;
|
||||
using YamlDotNet.Serialization;
|
||||
|
||||
namespace Engine.Serialization;
|
||||
|
||||
internal class GameObjectYamlConverter : IYamlTypeConverter
|
||||
{
|
||||
public bool Accepts(Type type) => type == typeof(GameObjectDTO);
|
||||
|
||||
public object ReadYaml(IParser parser, Type type)
|
||||
{
|
||||
if (parser.Current is not MappingStart)
|
||||
throw new InvalidOperationException("Expected MappingStart");
|
||||
|
||||
parser.MoveNext();
|
||||
var gameObject = new GameObjectDTO();
|
||||
while (parser.Current != null && parser.Current is not MappingEnd)
|
||||
{
|
||||
var propertyName = ((Scalar)parser.Current).Value;
|
||||
parser.MoveNext();
|
||||
switch (propertyName)
|
||||
{
|
||||
case nameof(GameObjectDTO.Id):
|
||||
gameObject.Id = ((Scalar)parser.Current).Value;
|
||||
break;
|
||||
case nameof(GameObjectDTO.Name):
|
||||
gameObject.Name = ((Scalar)parser.Current).Value;
|
||||
break;
|
||||
case nameof(GameObjectDTO.Transform):
|
||||
gameObject.Transform = (TransformDTO)(new TransformYamlConverter().ReadYaml(parser, typeof(TransformDTO)) ?? new Exception());
|
||||
break;
|
||||
case nameof(GameObjectDTO.StateEnable):
|
||||
gameObject.StateEnable = (StateEnableDTO)(new StateEnableYamlConverter().ReadYaml(parser, typeof(StateEnableDTO)) ?? new Exception());
|
||||
break;
|
||||
case nameof(GameObjectDTO.BehaviourController):
|
||||
gameObject.BehaviourController = (BehaviourControllerDTO)(new BehaviourControllerYamlConverter().ReadYaml(parser, typeof(BehaviourControllerDTO)) ?? new Exception());
|
||||
break;
|
||||
}
|
||||
parser.MoveNext();
|
||||
}
|
||||
return gameObject;
|
||||
}
|
||||
|
||||
public void WriteYaml(IEmitter emitter, object? value, Type type)
|
||||
{
|
||||
var gameObject = (GameObjectDTO)(value ?? throw new Exception());
|
||||
|
||||
TransformYamlConverter transformYamlConverter = new();
|
||||
StateEnableYamlConverter stateEnableYamlConverter = new();
|
||||
BehaviourControllerYamlConverter behaviourControllerYamlConverter = new();
|
||||
|
||||
emitter.Emit(new MappingStart());
|
||||
emitter.Emit(new Scalar(nameof(GameObjectDTO.Id)));
|
||||
emitter.Emit(new Scalar(gameObject.Id.ToString()));
|
||||
emitter.Emit(new Scalar(nameof(GameObjectDTO.ClassType)));
|
||||
emitter.Emit(new Scalar(gameObject.ClassType));
|
||||
emitter.Emit(new Scalar(nameof(GameObjectDTO.Name)));
|
||||
emitter.Emit(new Scalar(gameObject.Name.ToString()));
|
||||
emitter.Emit(new Scalar(nameof(GameObjectDTO.Transform)));
|
||||
transformYamlConverter.WriteYaml(emitter, gameObject.Transform, typeof(TransformDTO));
|
||||
emitter.Emit(new Scalar(nameof(GameObjectDTO.StateEnable)));
|
||||
stateEnableYamlConverter.WriteYaml(emitter, gameObject.StateEnable, typeof(StateEnableDTO));
|
||||
emitter.Emit(new Scalar(nameof(GameObjectDTO.BehaviourController)));
|
||||
behaviourControllerYamlConverter.WriteYaml(emitter, gameObject.BehaviourController, typeof(BehaviourControllerDTO));
|
||||
emitter.Emit(new MappingEnd());
|
||||
}
|
||||
}
|
|
@ -1,7 +0,0 @@
|
|||
namespace Engine.Serialization;
|
||||
|
||||
public interface ISerializer
|
||||
{
|
||||
public string Serialize<T>(T @object);
|
||||
public T Deserialize<T>(string serializedString);
|
||||
}
|
|
@ -1,36 +0,0 @@
|
|||
using System.Collections.Generic;
|
||||
using Engine.Serialization.DTOs;
|
||||
using Syntriax.Engine.Core.Abstract;
|
||||
|
||||
namespace Engine.Serialization;
|
||||
|
||||
internal static class InternalExtensions
|
||||
{
|
||||
public static TransformDTO ToDTO(this ITransform transform)
|
||||
=> new(transform.GetType().FullName ?? throw new System.Exception(), transform.Parent?.GameObject.Id, transform.Position, transform.Scale, transform.Rotation);
|
||||
|
||||
public static GameObjectDTO ToDTO(this IGameObject gameObject)
|
||||
=> new(gameObject.GetType().FullName ?? throw new System.Exception(), gameObject.Id, gameObject.Name, gameObject.Transform.ToDTO(), gameObject.BehaviourController.ToDTO(), gameObject.StateEnable.ToDTO());
|
||||
|
||||
public static GameManagerDTO ToDTO(this IGameManager gameManager)
|
||||
{
|
||||
List<GameObjectDTO> dtos = [];
|
||||
foreach (var gameObject in gameManager)
|
||||
dtos.Add(gameObject.ToDTO());
|
||||
|
||||
return new(gameManager.GetType().FullName ?? throw new System.Exception(), dtos);
|
||||
}
|
||||
|
||||
public static StateEnableDTO ToDTO(this IStateEnable stateEnable)
|
||||
=> new(stateEnable.GetType().FullName ?? throw new System.Exception(), stateEnable.Enabled);
|
||||
|
||||
public static BehaviourControllerDTO ToDTO(this IBehaviourController behaviourController)
|
||||
{
|
||||
List<BehaviourDTO> dtos = [];
|
||||
foreach (var behaviour in behaviourController)
|
||||
dtos.Add(new(behaviour.GetType().FullName ?? throw new System.Exception(), behaviour.Priority, behaviour.StateEnable.ToDTO()));
|
||||
return new(behaviourController.GetType().FullName ?? throw new System.Exception(), dtos);
|
||||
}
|
||||
public static BehaviourDTO ToDTO(this IBehaviour behaviour)
|
||||
=> new(behaviour.GetType().FullName ?? throw new System.Exception(), behaviour.Priority, behaviour.StateEnable.ToDTO());
|
||||
}
|
|
@ -1,61 +1,85 @@
|
|||
using System;
|
||||
using System.Collections.Generic;
|
||||
using Engine.Serialization.DTOs;
|
||||
using System.IO;
|
||||
using System.Linq;
|
||||
using System.Reflection;
|
||||
using Syntriax.Engine.Core;
|
||||
using Syntriax.Engine.Core.Abstract;
|
||||
using Syntriax.Engine.Core.Factory;
|
||||
using YamlDotNet.Serialization;
|
||||
|
||||
namespace Engine.Serialization;
|
||||
|
||||
public static class Serialization
|
||||
{
|
||||
private static readonly ISerializer defaultSerializer = new YamlSerializer();
|
||||
private static readonly ISerializer defaultSerializer = new SerializerBuilder().;
|
||||
|
||||
public static string SerializeGameObject(IGameObject gameObject) => Serialize(gameObject.ToDTO(), defaultSerializer);
|
||||
public static T DeserializeGameObject<T>(string serializedString) where T : class, IGameObject
|
||||
public static string Serialize(object @object)
|
||||
{
|
||||
GameObjectDTO gameObjectDTO = Deserialize<GameObjectDTO>(serializedString, defaultSerializer);
|
||||
return CreateGameObject<T>(gameObjectDTO);
|
||||
EntityDto dto = new();
|
||||
BindingFlags bindingFlags = BindingFlags.Public |
|
||||
BindingFlags.NonPublic |
|
||||
BindingFlags.Instance;
|
||||
|
||||
Type type = @object.GetType();
|
||||
FieldInfo[] fieldInfos = type.GetFields(bindingFlags);
|
||||
List<(string Name, object?)> list = fieldInfos.Where(f => f.FieldType.IsValueType || f.FieldType == typeof(string)).Select(f => (f.Name, f.GetValue(@object))).ToList();
|
||||
|
||||
return "";
|
||||
}
|
||||
|
||||
private static T CreateGameObject<T>(GameObjectDTO gameObjectDTO) where T : class, IGameObject
|
||||
{
|
||||
Type gameObjectType = (gameObjectDTO.ClassType is not null) ? TypeFactory.Get(gameObjectDTO.ClassType) : typeof(GameObject);
|
||||
Type stateEnableType = (gameObjectDTO.StateEnable.ClassType is not null) ? TypeFactory.Get(gameObjectDTO.StateEnable.ClassType) : typeof(Transform);
|
||||
Type transformType = (gameObjectDTO.Transform.ClassType is not null) ? TypeFactory.Get(gameObjectDTO.Transform.ClassType) : typeof(Transform);
|
||||
Type behaviourControllerType = (gameObjectDTO.BehaviourController.ClassType is not null) ? TypeFactory.Get(gameObjectDTO.BehaviourController.ClassType) : typeof(Transform);
|
||||
// public static string SerializeGameObject(IGameObject gameObject) => Serialize(gameObject, defaultSerializer);
|
||||
// public static T DeserializeGameObject<T>(StreamReader reader) where T : class, IGameObject
|
||||
// {
|
||||
// Dictionary<string, object?> gameObjectDTO = Deserialize<Dictionary<string, object?>>(reader, defaultSerializer);
|
||||
// return CreateGameObject<T>(gameObjectDTO);
|
||||
// }
|
||||
|
||||
ITransform transform = TypeFactory.Get<ITransform>(transformType);
|
||||
IStateEnable stateEnable = TypeFactory.Get<IStateEnable>(stateEnableType);
|
||||
IBehaviourController behaviourController = TypeFactory.Get<IBehaviourController>(behaviourControllerType);
|
||||
T t = new GameObjectFactory().Instantiate<T>(transform, behaviourController, stateEnable, gameObjectType);
|
||||
// private static T CreateGameObject<T>(Dictionary<string, object?> gameObject) where T : class, IGameObject
|
||||
// {
|
||||
// Type gameObjectType = gameObject.TryGetValue("ClassType", out var goType) ? TypeFactory.Get(goType?.ToString() ?? throw new Exception()) : typeof(GameObject);
|
||||
// Type stateEnableType = gameObject.TryGetValue("StateEnable.ClassType", out var seType) ? TypeFactory.Get(seType?.ToString() ?? throw new Exception()) : typeof(Transform);
|
||||
// Type transformType = gameObject.TryGetValue("Transform.ClassType", out var tType) ? TypeFactory.Get(tType?.ToString() ?? throw new Exception()) : typeof(Transform);
|
||||
// Type behaviourControllerType = gameObject.TryGetValue("BehaviourController.ClassType", out var bcType) ? TypeFactory.Get(bcType?.ToString() ?? throw new Exception()) : typeof(Transform);
|
||||
|
||||
foreach (var behaviourDto in gameObjectDTO.BehaviourController.Behaviours)
|
||||
{
|
||||
IBehaviour behaviour = TypeFactory.Get<IBehaviour>(behaviourDto.ClassType);
|
||||
behaviourController.AddBehaviour(behaviour);
|
||||
}
|
||||
|
||||
return t;
|
||||
}
|
||||
|
||||
public static string SerializeGameManager(IGameManager gameManager) => Serialize(gameManager.ToDTO(), defaultSerializer);
|
||||
public static T DeserializeGameManager<T>(string serializedString) where T : class, IGameManager
|
||||
{
|
||||
GameManagerDTO gameManagerDto = Deserialize<GameManagerDTO>(serializedString, defaultSerializer);
|
||||
|
||||
Type gameManagerType = (gameManagerDto.ClassType is not null) ? TypeFactory.Get(gameManagerDto.ClassType) : typeof(GameManager);
|
||||
T gameManager = TypeFactory.Get<T>(gameManagerType);
|
||||
|
||||
foreach (var gameObjectDto in gameManagerDto.GameObjects)
|
||||
gameManager.RegisterGameObject(CreateGameObject<IGameObject>(gameObjectDto));
|
||||
|
||||
return gameManager;
|
||||
}
|
||||
|
||||
public static string Serialize<T>(T @object) => Serialize(@object, defaultSerializer);
|
||||
public static string Serialize<T>(T @object, ISerializer serializer) => serializer.Serialize(@object);
|
||||
|
||||
public static T Deserialize<T>(string serializedString) => Deserialize<T>(serializedString, defaultSerializer);
|
||||
public static T Deserialize<T>(string serializedString, ISerializer serializer) => serializer.Deserialize<T>(serializedString);
|
||||
// ITransform transform = TypeFactory.Get<ITransform>(transformType);
|
||||
// IStateEnable stateEnable = TypeFactory.Get<IStateEnable>(stateEnableType);
|
||||
// IBehaviourController behaviourController = TypeFactory.Get<IBehaviourController>(behaviourControllerType);
|
||||
// T t = new GameObjectFactory().Instantiate<T>(transform, behaviourController, stateEnable, gameObjectType);
|
||||
|
||||
// Dictionary<string, object?>? behaviours = gameObject["BehaviourController"] as Dictionary<string, object?> ?? throw new Exception();
|
||||
|
||||
// foreach ((var key, var value) in behaviours)
|
||||
// {
|
||||
// Dictionary<string, object?> values = value as Dictionary<string, object?> ?? throw new Exception();
|
||||
|
||||
// IBehaviour behaviour = TypeFactory.Get<IBehaviour>(values["ClassType"]);
|
||||
// behaviourController.AddBehaviour(behaviour);
|
||||
// }
|
||||
|
||||
// return t;
|
||||
// }
|
||||
|
||||
// public static string SerializeGameManager(IGameManager gameManager) => Serialize(gameManager, defaultSerializer);
|
||||
// public static T DeserializeGameManager<T>(StreamReader reader) where T : class, IGameManager
|
||||
// {
|
||||
// GameManagerDTO gameManagerDto = Deserialize<GameManagerDTO>(reader, defaultSerializer);
|
||||
|
||||
// Type gameManagerType = (gameManagerDto.ClassType is not null) ? TypeFactory.Get(gameManagerDto.ClassType) : typeof(GameManager);
|
||||
// T gameManager = TypeFactory.Get<T>(gameManagerType);
|
||||
|
||||
// foreach (var gameObjectDto in gameManagerDto.GameObjects)
|
||||
// gameManager.RegisterGameObject(CreateGameObject<IGameObject>(gameObjectDto));
|
||||
|
||||
// return gameManager;
|
||||
// }
|
||||
|
||||
// public static string Serialize<T>(T @object) => Serialize(@object, defaultSerializer);
|
||||
// public static string Serialize<T>(T @object, ISerializer serializer) => serializer.Serialize(@object);
|
||||
|
||||
// public static T Deserialize<T>(string serializedString) => Deserialize<T>(serializedString, defaultSerializer);
|
||||
// public static T Deserialize<T>(string serializedString, ISerializer serializer) => serializer.Deserialize<T>(serializedString);
|
||||
|
||||
// public static T Deserialize<T>(StreamReader reader) => Deserialize<T>(reader, defaultSerializer);
|
||||
// public static T Deserialize<T>(StreamReader reader, ISerializer serializer) => serializer.Deserialize<T>(reader);
|
||||
}
|
||||
|
|
|
@ -1,52 +0,0 @@
|
|||
using System;
|
||||
using System.Linq;
|
||||
|
||||
using Engine.Serialization.DTOs;
|
||||
|
||||
using YamlDotNet.Core;
|
||||
using YamlDotNet.Core.Events;
|
||||
using YamlDotNet.Serialization;
|
||||
|
||||
namespace Engine.Serialization;
|
||||
|
||||
internal class StateEnableYamlConverter : IYamlTypeConverter
|
||||
{
|
||||
public bool Accepts(Type type) => type == typeof(StateEnableDTO);
|
||||
|
||||
public object ReadYaml(IParser parser, Type type)
|
||||
{
|
||||
if (parser.Current is not MappingStart)
|
||||
throw new InvalidOperationException("Expected MappingStart");
|
||||
|
||||
parser.MoveNext();
|
||||
var stateEnable = new StateEnableDTO();
|
||||
while (parser.Current != null && parser.Current is not MappingEnd)
|
||||
{
|
||||
var propertyName = ((Scalar)parser.Current).Value;
|
||||
parser.MoveNext();
|
||||
switch (propertyName)
|
||||
{
|
||||
case nameof(StateEnableDTO.ClassType):
|
||||
stateEnable.ClassType = ((Scalar)parser.Current).Value;
|
||||
break;
|
||||
case nameof(StateEnableDTO.Enabled):
|
||||
stateEnable.Enabled = bool.Parse(((Scalar)parser.Current).Value);
|
||||
break;
|
||||
}
|
||||
parser.MoveNext();
|
||||
}
|
||||
return stateEnable;
|
||||
}
|
||||
|
||||
public void WriteYaml(IEmitter emitter, object? value, Type type)
|
||||
{
|
||||
var stateEnable = (StateEnableDTO)(value ?? throw new Exception());
|
||||
|
||||
emitter.Emit(new MappingStart());
|
||||
emitter.Emit(new Scalar(nameof(StateEnableDTO.ClassType)));
|
||||
emitter.Emit(new Scalar(stateEnable.ClassType));
|
||||
emitter.Emit(new Scalar(nameof(StateEnableDTO.Enabled)));
|
||||
emitter.Emit(new Scalar(stateEnable.Enabled.ToString()));
|
||||
emitter.Emit(new MappingEnd());
|
||||
}
|
||||
}
|
|
@ -1,71 +0,0 @@
|
|||
using System;
|
||||
using System.Linq;
|
||||
|
||||
using Engine.Serialization.DTOs;
|
||||
|
||||
using Syntriax.Engine.Core;
|
||||
|
||||
using YamlDotNet.Core;
|
||||
using YamlDotNet.Core.Events;
|
||||
using YamlDotNet.Serialization;
|
||||
|
||||
namespace Engine.Serialization;
|
||||
|
||||
internal class TransformYamlConverter : IYamlTypeConverter
|
||||
{
|
||||
public bool Accepts(Type type) => type == typeof(TransformDTO);
|
||||
|
||||
public object ReadYaml(IParser parser, Type type)
|
||||
{
|
||||
if (parser.Current is not MappingStart)
|
||||
throw new InvalidOperationException("Expected MappingStart");
|
||||
|
||||
parser.MoveNext();
|
||||
var transform = new TransformDTO();
|
||||
while (parser.Current != null && parser.Current is not MappingEnd)
|
||||
{
|
||||
var propertyName = ((Scalar)parser.Current).Value;
|
||||
parser.MoveNext();
|
||||
switch (propertyName)
|
||||
{
|
||||
case nameof(TransformDTO.ParentId):
|
||||
transform.ParentId = new Vector2DYamlConverter().ReadYaml(parser, typeof(Vector2D))?.ToString();
|
||||
break;
|
||||
case nameof(TransformDTO.Position):
|
||||
transform.Position = (Vector2D)(new Vector2DYamlConverter().ReadYaml(parser, typeof(Vector2D)) ?? new Exception());
|
||||
break;
|
||||
case nameof(TransformDTO.Scale):
|
||||
transform.Scale = (Vector2D)(new Vector2DYamlConverter().ReadYaml(parser, typeof(Vector2D)) ?? new Exception());
|
||||
break;
|
||||
case nameof(TransformDTO.Rotation):
|
||||
transform.Rotation = float.Parse(((Scalar)parser.Current).Value);
|
||||
break;
|
||||
}
|
||||
parser.MoveNext();
|
||||
}
|
||||
return transform;
|
||||
}
|
||||
|
||||
public void WriteYaml(IEmitter emitter, object? value, Type type)
|
||||
{
|
||||
var transform = (TransformDTO)(value ?? throw new Exception());
|
||||
|
||||
Vector2DYamlConverter vector2DYamlConverter = new();
|
||||
|
||||
emitter.Emit(new MappingStart());
|
||||
if (transform.ParentId is not null)
|
||||
{
|
||||
emitter.Emit(new Scalar(nameof(TransformDTO.ParentId)));
|
||||
emitter.Emit(new Scalar(transform.ParentId));
|
||||
}
|
||||
emitter.Emit(new Scalar(nameof(TransformDTO.ClassType)));
|
||||
emitter.Emit(new Scalar(transform.ClassType));
|
||||
emitter.Emit(new Scalar(nameof(TransformDTO.Position)));
|
||||
vector2DYamlConverter.WriteYaml(emitter, transform.Position, typeof(Vector2D));
|
||||
emitter.Emit(new Scalar(nameof(TransformDTO.Scale)));
|
||||
vector2DYamlConverter.WriteYaml(emitter, transform.Scale, typeof(Vector2D));
|
||||
emitter.Emit(new Scalar(nameof(TransformDTO.Rotation)));
|
||||
emitter.Emit(new Scalar(transform.Rotation.ToString()));
|
||||
emitter.Emit(new MappingEnd());
|
||||
}
|
||||
}
|
|
@ -1,35 +0,0 @@
|
|||
using YamlDotNet.Serialization;
|
||||
using YamlDotNet.Serialization.NamingConventions;
|
||||
|
||||
namespace Engine.Serialization;
|
||||
|
||||
public class YamlSerializer : ISerializer
|
||||
{
|
||||
private readonly YamlDotNet.Serialization.ISerializer serializer = new SerializerBuilder()
|
||||
.WithNamingConvention(CamelCaseNamingConvention.Instance)
|
||||
.WithTypeConverter(new BehaviourYamlConverter())
|
||||
.WithTypeConverter(new BehaviourControllerYamlConverter())
|
||||
.WithTypeConverter(new BehaviourDTOListConverter())
|
||||
.WithTypeConverter(new GameManagerYamlConverter())
|
||||
.WithTypeConverter(new GameObjectYamlConverter())
|
||||
.WithTypeConverter(new GameObjectDTOListConverter())
|
||||
.WithTypeConverter(new StateEnableYamlConverter())
|
||||
.WithTypeConverter(new TransformYamlConverter())
|
||||
.WithTypeConverter(new Vector2DYamlConverter())
|
||||
.Build();
|
||||
private readonly YamlDotNet.Serialization.IDeserializer deserializer = new DeserializerBuilder()
|
||||
.WithNamingConvention(UnderscoredNamingConvention.Instance)
|
||||
.WithTypeConverter(new BehaviourYamlConverter())
|
||||
.WithTypeConverter(new BehaviourControllerYamlConverter())
|
||||
.WithTypeConverter(new BehaviourDTOListConverter())
|
||||
.WithTypeConverter(new GameManagerYamlConverter())
|
||||
.WithTypeConverter(new GameObjectYamlConverter())
|
||||
.WithTypeConverter(new GameObjectDTOListConverter())
|
||||
.WithTypeConverter(new StateEnableYamlConverter())
|
||||
.WithTypeConverter(new TransformYamlConverter())
|
||||
.WithTypeConverter(new Vector2DYamlConverter())
|
||||
.Build();
|
||||
|
||||
public string Serialize<T>(T @object) => serializer.Serialize(@object);
|
||||
public T Deserialize<T>(string serializedString) => deserializer.Deserialize<T>(serializedString);
|
||||
}
|
Loading…
Reference in New Issue