Syntriax.Engine/Engine.Serialization/Serialization.cs

28 lines
1.3 KiB
C#
Raw Normal View History

2024-02-10 17:10:02 +03:00
using System;
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);
Type gameObjectType = (gameObjectDTO.ClassType is not null) ? TypeFactory.Get(gameObjectDTO.ClassType) : typeof(GameObject);
T t = new GameObjectFactory().Instantiate<T>(gameObjectType);
return t;
}
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);
}