using System.Reflection; using Shiki.Common.Factory; using Shiki.Common.Identity; using UniScan.Core.State.Node; using UniScan.Core.State.Node.Nodes; namespace UniScan.Core.State; public interface IStateSerializable { void SerializeStatesInto(ref Dictionary states);//TODO codegen for this too Dictionary SerializeStates() { Dictionary states = []; SerializeStatesInto(ref states); return states; } } public static class StateSerializer { public static IStateNode SerializeState(object state, Func constructor) { return constructor(state); } public static IStateNode SerializeState(T state, Func constructor) { return constructor(state); } public static T SerializeState(TValue state) where T : IFactoryConstructable, IStateNode { return T.CreateInstance(state); } public static Dictionary SerializeContainer(object container) { if (container is IStateSerializable serializable) { return serializable.SerializeStates(); } Dictionary nodes = new(); PropertyInfo[] properties = container.GetType().GetProperties(); foreach (PropertyInfo prop in properties) { StatePropertyAttribute? attr = prop.GetCustomAttribute(); if (attr == null) continue; ConstructorInfo? ctor = attr.Type.GetConstructor([prop.PropertyType]); if (ctor == null) { throw new InvalidOperationException($"Cannot serialize property '{prop.Name}' due to linked StateNode not containing accepting constructor"); } IStateNode node = ctor.Invoke([prop.GetValue(container)]) as IStateNode ?? throw new InvalidOperationException(); nodes[attr.Identifier] = node; } return nodes; } }