public override SerializableStructure CreateInstance(string typeFullName, Dump data) { var deserializedType = Type.GetType(typeFullName) ?? Type.GetType(typeFullName + ", " + typeof(SafeSerializationMgr).Assembly.FullName); if (deserializedType == null) { throw new KOSSerializationException("Unrecognized type: " + typeFullName); } SerializableStructure instance = Activator.CreateInstance(deserializedType) as SerializableStructure; if (instance is IHasSharedObjects) { IHasSharedObjects withSharedObjects = instance as IHasSharedObjects; withSharedObjects.Shared = sharedObjects; } if (instance != null) { instance.LoadDump(data); } return(instance); }
protected object InvokeDelegate(SerializableStructure stack, string suffixName, params object[] parameters) { var lengthObj = stack.GetSuffix(suffixName) as DelegateSuffixResult; Assert.IsNotNull(lengthObj); Assert.IsNotNull(lengthObj.Del); return(lengthObj.Del.DynamicInvoke(parameters)); }
protected object InvokeDelegate(SerializableStructure stack, string suffixName, params object[] parameters) { var lengthObj = stack.GetSuffix(suffixName) as DelegateSuffixResult; Assert.IsNotNull(lengthObj); cpu.PushArgumentStack(null); // fake delegate info cpu.PushArgumentStack(new KOSArgMarkerType()); foreach (object param in parameters) { cpu.PushArgumentStack(param); } lengthObj.Invoke(cpu); return(lengthObj.Value); }
private static object MapProperties(SerializableStructure from, Type tTo) { var to = Activator.CreateInstance(tTo) ?? throw new NotSupportedException(); DoMapping(from.Type); return(to); int DoMapping(SerializableType type) { var idx = 0; if (null != type.Base) { idx += DoMapping(type.Base); } foreach (var field in type.Fields) { var prop = to.GetType().GetRuntimeProperty(field.Name); var fieldVal = from.Fields[idx]; if (null != prop) { if (field.Type.Type == PrimitiveType.Complex) { prop.SetValue(to, ConvertComplexType(fieldVal, prop.PropertyType, field.IsArray)); } else { prop.SetValue(to, ConvertType(fieldVal, field)); } idx++; } else { #if DEBUG throw new InvalidDataException($"Field not supported: {field.Name}"); #endif } } return(idx); } }
public override void Execute(SafeSharedObjects shared) { object pathObject = PopValueAssert(shared, true); SerializableStructure serialized = PopValueAssert(shared, true) as SerializableStructure; AssertArgBottomAndConsume(shared); if (serialized == null) { throw new KOSException("This type is not serializable"); } string serializedString = new SafeSerializationMgr(shared).Serialize(serialized, JsonFormatter.WriterInstance); FileContent fileContent = new FileContent(serializedString); GlobalPath path = shared.VolumeMgr.GlobalPathFromObject(pathObject); Volume volume = shared.VolumeMgr.GetVolumeFromPath(path); ReturnValue = volume.SaveFile(path, fileContent); }
public override void Execute(SharedObjects shared) { string fileName = PopValueAssert(shared, true).ToString(); SerializableStructure serialized = PopValueAssert(shared, true) as SerializableStructure; AssertArgBottomAndConsume(shared); if (serialized == null) { throw new KOSException("This type is not serializable"); } string serializedString = new SerializationMgr(shared).Serialize(serialized, JsonFormatter.WriterInstance); FileContent fileContent = new FileContent(serializedString); if (shared.VolumeMgr != null) { shared.VolumeMgr.CurrentVolume.Save(fileName, fileContent); } }
public static void DumpSerializedTypes(SerializableStructure structure, StreamWriter sw, int indent) { string spaces = new string(' ', indent * 2); if (indent > 10) { sw.WriteLine($"{spaces}Max depth exceded"); } sw.WriteLine($"{spaces}{structure.Type} : {structure.Base?.ToString() ?? "object"}"); if (structure.Base != null && structure.Base.Type.Namespace != "UnityEngine") { DumpSerializedTypes(structure.Base, sw, indent + 1); } foreach (var field in structure.Fields) { spaces = new string(' ', (indent + 1) * 2); if (field.Type == PrimitiveType.Complex) { ISerializableStructure ComplexType = Util.GetMember <ISerializableStructure>(field, "ComplexType"); if (ComplexType is SerializablePointer pptr) { sw.WriteLine($"{spaces}PPTR<{ComplexType}> {field.Name};"); } else if (ComplexType is SerializableStructure fieldStructure) { sw.WriteLine($"{spaces}{fieldStructure} {field.Name};"); DumpSerializedTypes(fieldStructure, sw, indent + 1); } else { sw.WriteLine($"{spaces}{ComplexType} {field.Name};"); } } else { sw.WriteLine($"{spaces}{field.Type} {field.Name};"); } } }
private string Serialize(SerializableStructure o) { return(new SafeSerializationMgr().Serialize(o, TerminalFormatter.Instance, false)); }
private string Serialize(SerializableStructure o) { return(new SafeSerializationMgr().Serialize(o, JsonFormatter.WriterInstance)); }
private static T MapProperties <T>(SerializableStructure from) { return((T)MapProperties(from, typeof(T))); }