Syntriax.Engine/Engine.Serialization/Serialization.cs

86 lines
4.5 KiB
C#
Raw Permalink Normal View History

2024-02-10 17:10:02 +03:00
using System;
2024-02-10 20:02:38 +03:00
using System.Collections.Generic;
2024-02-12 10:48:56 +03:00
using System.IO;
using System.Linq;
using System.Reflection;
2024-02-10 17:10:02 +03:00
using Syntriax.Engine.Core;
using Syntriax.Engine.Core.Abstract;
using Syntriax.Engine.Core.Factory;
2024-02-12 10:48:56 +03:00
using YamlDotNet.Serialization;
2024-02-10 17:10:02 +03:00
namespace Engine.Serialization;
public static class Serialization
{
2024-02-12 10:48:56 +03:00
private static readonly ISerializer defaultSerializer = new SerializerBuilder().;
2024-02-10 17:10:02 +03:00
2024-02-12 10:48:56 +03:00
public static string Serialize(object @object)
2024-02-10 17:10:02 +03:00
{
2024-02-12 10:48:56 +03:00
EntityDto dto = new();
BindingFlags bindingFlags = BindingFlags.Public |
BindingFlags.NonPublic |
BindingFlags.Instance;
2024-02-10 20:02:38 +03:00
2024-02-12 10:48:56 +03:00
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 "";
2024-02-10 17:10:02 +03:00
}
2024-02-12 10:48:56 +03:00
// 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);
// }
2024-02-10 20:02:38 +03:00
2024-02-12 10:48:56 +03:00
// 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);
2024-02-10 20:02:38 +03:00
2024-02-12 10:48:56 +03:00
// 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);
2024-02-10 20:02:38 +03:00
2024-02-12 10:48:56 +03:00
// 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);
2024-02-10 20:02:38 +03:00
2024-02-12 10:48:56 +03:00
// public static T Deserialize<T>(string serializedString) => Deserialize<T>(serializedString, defaultSerializer);
// public static T Deserialize<T>(string serializedString, ISerializer serializer) => serializer.Deserialize<T>(serializedString);
2024-02-10 17:10:02 +03:00
2024-02-12 10:48:56 +03:00
// public static T Deserialize<T>(StreamReader reader) => Deserialize<T>(reader, defaultSerializer);
// public static T Deserialize<T>(StreamReader reader, ISerializer serializer) => serializer.Deserialize<T>(reader);
2024-02-10 17:10:02 +03:00
}