2024-02-10 17:10:02 +03:00
|
|
|
using System;
|
2024-02-10 20:02:38 +03:00
|
|
|
using System.Collections.Generic;
|
2024-02-10 17:10:02 +03:00
|
|
|
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);
|
2024-02-10 20:02:38 +03:00
|
|
|
return CreateGameObject<T>(gameObjectDTO);
|
|
|
|
}
|
|
|
|
|
|
|
|
private static T CreateGameObject<T>(GameObjectDTO gameObjectDTO) where T : class, IGameObject
|
|
|
|
{
|
2024-02-10 17:10:02 +03:00
|
|
|
Type gameObjectType = (gameObjectDTO.ClassType is not null) ? TypeFactory.Get(gameObjectDTO.ClassType) : typeof(GameObject);
|
2024-02-10 20:02:38 +03:00
|
|
|
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);
|
|
|
|
}
|
|
|
|
|
2024-02-10 17:10:02 +03:00
|
|
|
return t;
|
|
|
|
}
|
|
|
|
|
2024-02-10 20:02:38 +03:00
|
|
|
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;
|
|
|
|
}
|
|
|
|
|
2024-02-10 17:10:02 +03:00
|
|
|
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);
|
|
|
|
}
|