Syntriax.Engine/Engine.Serialization/Serialization.cs

62 lines
3.2 KiB
C#

using System;
using System.Collections.Generic;
using Engine.Serialization.DTOs;
using Syntriax.Engine.Core;
using Syntriax.Engine.Core.Abstract;
using Syntriax.Engine.Core.Factory;
namespace Engine.Serialization;
public static class Serialization
{
private static readonly ISerializer defaultSerializer = new YamlSerializer();
public static string SerializeGameObject(IGameObject gameObject) => Serialize(gameObject.ToDTO(), defaultSerializer);
public static T DeserializeGameObject<T>(string serializedString) where T : class, IGameObject
{
GameObjectDTO gameObjectDTO = Deserialize<GameObjectDTO>(serializedString, defaultSerializer);
return CreateGameObject<T>(gameObjectDTO);
}
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);
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);
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);
}