using System; using System.Collections.Generic; 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 SerializerBuilder().; public static string Serialize(object @object) { 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 ""; } // public static string SerializeGameObject(IGameObject gameObject) => Serialize(gameObject, defaultSerializer); // public static T DeserializeGameObject(StreamReader reader) where T : class, IGameObject // { // Dictionary gameObjectDTO = Deserialize>(reader, defaultSerializer); // return CreateGameObject(gameObjectDTO); // } // private static T CreateGameObject(Dictionary 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); // ITransform transform = TypeFactory.Get(transformType); // IStateEnable stateEnable = TypeFactory.Get(stateEnableType); // IBehaviourController behaviourController = TypeFactory.Get(behaviourControllerType); // T t = new GameObjectFactory().Instantiate(transform, behaviourController, stateEnable, gameObjectType); // Dictionary? behaviours = gameObject["BehaviourController"] as Dictionary ?? throw new Exception(); // foreach ((var key, var value) in behaviours) // { // Dictionary values = value as Dictionary ?? throw new Exception(); // IBehaviour behaviour = TypeFactory.Get(values["ClassType"]); // behaviourController.AddBehaviour(behaviour); // } // return t; // } // public static string SerializeGameManager(IGameManager gameManager) => Serialize(gameManager, defaultSerializer); // public static T DeserializeGameManager(StreamReader reader) where T : class, IGameManager // { // GameManagerDTO gameManagerDto = Deserialize(reader, defaultSerializer); // Type gameManagerType = (gameManagerDto.ClassType is not null) ? TypeFactory.Get(gameManagerDto.ClassType) : typeof(GameManager); // T gameManager = TypeFactory.Get(gameManagerType); // foreach (var gameObjectDto in gameManagerDto.GameObjects) // gameManager.RegisterGameObject(CreateGameObject(gameObjectDto)); // return gameManager; // } // public static string Serialize(T @object) => Serialize(@object, defaultSerializer); // public static string Serialize(T @object, ISerializer serializer) => serializer.Serialize(@object); // public static T Deserialize(string serializedString) => Deserialize(serializedString, defaultSerializer); // public static T Deserialize(string serializedString, ISerializer serializer) => serializer.Deserialize(serializedString); // public static T Deserialize(StreamReader reader) => Deserialize(reader, defaultSerializer); // public static T Deserialize(StreamReader reader, ISerializer serializer) => serializer.Deserialize(reader); }