//IEnumerable<object> Read(params Type[] eventTypes); //void Invoke(params KeyValuePair<Type, Action<object>>[] handlers); private List <TypeReaderFactory> CreateTypeReaders(Dictionary <Type, Action <object> > handlers) { var knownProxies = new Dictionary <Type, List <Type> >(); var typesMap = new Type[ushort.MaxValue]; var typeReaders = new TypeReaderFactory[ushort.MaxValue]; foreach (var type in handlers) { var originalTypes = knownProxies.ContainsKey(type.Key) ? knownProxies[type.Key] : new List <Type> { type.Key }; foreach (var originalType in originalTypes) { if (_typeIdsMap.ContainsKey(originalType.FullName)) { var typeId = _typeIdsMap[originalType.FullName]; var path = _folder + "\\" + originalType.FullName + "\\"; typesMap[typeId] = type.Key; typeReaders[typeId] = new TypeReaderFactory(path, type.Key, _customReaders) { Handler = handlers[type.Key] }; } } } return(typeReaders.Where(t => t != null).ToList()); }
public void Setup() { var readerFactory = new TypeReaderFactory(); var writerFactory = new TypeWriterFactory(); var loader = new ResourceLoader(); loader.AddImportersAndProcessors(typeof(IImporter).Assembly); manager = new ResourceContext(null, readerFactory, writerFactory, loader, Environment.CurrentDirectory + "\\Assets", Environment.CurrentDirectory); }
public override Instance ReadChildInstance(string name, PluginGraph graph, Type childType) { ITypeReader reader = TypeReaderFactory.GetReader(childType); if (reader == null) { return(base.ReadChildInstance(name, graph, childType)); } XmlElement element = getChildNode(name); return(element == null ? null : reader.Read(element, childType)); }
public void CanSerializeAndDeserializeFSM() { fsm.AddState("Test"); var state = fsm.AddState("Foo"); state.AddTransition("BAR", "Test"); fsm.AddVariable("some_int", 2); fsm.AddVariable("some_string", "Hell0"); var stream = new MemoryStream(); var wfactory = new TypeWriterFactory(); var rfactory = new TypeReaderFactory(); AssetWriter.WriteAsset(stream, fsm, wfactory); stream.Position = 0; var result = AssetReader.ReadAsset<FSMComponent>(stream, rfactory); }
public void CanSerializeAndDeserializeFSM() { fsm.AddState("Test"); var state = fsm.AddState("Foo"); state.AddTransition("BAR", "Test"); fsm.AddVariable("some_int", 2); fsm.AddVariable("some_string", "Hell0"); var stream = new MemoryStream(); var wfactory = new TypeWriterFactory(); var rfactory = new TypeReaderFactory(); AssetWriter.WriteAsset(stream, fsm, wfactory); stream.Position = 0; var result = AssetReader.ReadAsset <FSMComponent>(stream, rfactory); }
public void Setup() { this.provider = new TypeReaderFactory(); }
private ITypeReaderFactory CreateTypeReaderFactory() { var factory = new TypeReaderFactory(); return factory; }
private ITypeReaderFactory CreateTypeReaderFactory() { var factory = new TypeReaderFactory(); return(factory); }
private Action <object> CreateReader(PropertyInfo column, Dictionary <Type, Func <BinaryReader, object> > customReaders) { // Column didn't exist when write occurred. if (_columnStream == null) { return o => { } } ; if (customReaders.ContainsKey(column.PropertyType)) { var reader = customReaders[column.PropertyType]; return(o => column.SetValue(o, reader(_columnReader))); } var paramObject = Expression.Parameter(typeof(object), "o"); var typedObject = Expression.Convert(paramObject, column.DeclaringType); if (column.PropertyType == typeof(bool)) { return(o => column.SetValue(o, _columnReader.ReadBoolean())); } if (column.PropertyType == typeof(bool?)) { return(o => column.SetValue(o, _columnReader.ReadBoolean() ? _columnReader.ReadBoolean() : (bool?)null)); } if (column.PropertyType == typeof(byte)) { return(o => column.SetValue(o, _columnReader.ReadByte())); } if (column.PropertyType == typeof(byte?)) { return(o => column.SetValue(o, _columnReader.ReadBoolean() ? _columnReader.ReadByte() : (byte?)null)); } if (column.PropertyType == typeof(sbyte)) { return(o => column.SetValue(o, _columnReader.ReadSByte())); } if (column.PropertyType == typeof(sbyte?)) { return(o => column.SetValue(o, _columnReader.ReadBoolean() ? _columnReader.ReadSByte() : (sbyte?)null)); } if (column.PropertyType == typeof(short)) { return(o => column.SetValue(o, _columnReader.ReadInt16())); } if (column.PropertyType == typeof(short?)) { return(o => column.SetValue(o, _columnReader.ReadBoolean() ? _columnReader.ReadInt16() : (short?)null)); } if (column.PropertyType == typeof(ushort)) { return(o => column.SetValue(o, _columnReader.ReadUInt16())); } if (column.PropertyType == typeof(ushort?)) { return(o => column.SetValue(o, _columnReader.ReadBoolean() ? _columnReader.ReadUInt16() : (ushort?)null)); } if (column.PropertyType == typeof(int)) { return(o => column.SetValue(o, _columnReader.ReadInt32())); } if (column.PropertyType == typeof(int?)) { return(o => column.SetValue(o, _columnReader.ReadBoolean() ? _columnReader.ReadInt32() : (int?)null)); } if (column.PropertyType == typeof(uint)) { return(o => column.SetValue(o, _columnReader.ReadUInt32())); } if (column.PropertyType == typeof(uint?)) { return(o => column.SetValue(o, _columnReader.ReadBoolean() ? _columnReader.ReadUInt32() : (uint?)null)); } if (column.PropertyType == typeof(long)) { return(o => column.SetValue(o, _columnReader.ReadInt64())); } if (column.PropertyType == typeof(long?)) { return(o => column.SetValue(o, _columnReader.ReadBoolean() ? _columnReader.ReadInt64() : (long?)null)); } if (column.PropertyType == typeof(ulong)) { return(o => column.SetValue(o, _columnReader.ReadUInt64())); } if (column.PropertyType == typeof(ulong?)) { return(o => column.SetValue(o, _columnReader.ReadBoolean() ? _columnReader.ReadUInt64() : (ulong?)null)); } if (column.PropertyType == typeof(Guid)) { return(o => column.SetValue(o, new Guid(_columnReader.ReadBytes(16)))); } if (column.PropertyType == typeof(Guid?)) { return(o => column.SetValue(o, _columnReader.ReadBoolean() ? new Guid(_columnReader.ReadBytes(16)) : (Guid?)null)); } if (column.PropertyType == typeof(decimal)) { var reader = new DecimalReader(_columnStream); var paramValue = Expression.Parameter(typeof(decimal), "d"); var call = Expression.Call(typedObject, column.GetSetMethod(), paramValue); var setter = Expression.Lambda <Action <object, decimal> >(call, paramObject, paramValue).Compile(); return(o => setter(o, reader.ReadDecimal(0))); } if (column.PropertyType == typeof(decimal?)) { return(o => column.SetValue(o, _columnReader.ReadBoolean() ? _columnReader.ReadDecimal() : (decimal?)null)); } if (column.PropertyType == typeof(double)) { return(o => column.SetValue(o, _columnReader.ReadDouble())); } if (column.PropertyType == typeof(double?)) { return(o => column.SetValue(o, _columnReader.ReadBoolean() ? _columnReader.ReadDouble() : (double?)null)); } if (column.PropertyType == typeof(DateTime)) { return(o => column.SetValue(o, new DateTime(_columnReader.ReadInt64()))); } if (column.PropertyType == typeof(DateTime?)) { return(o => column.SetValue(o, _columnReader.ReadBoolean() ? new DateTime(_columnReader.ReadInt64()) : (DateTime?)null)); } if (column.PropertyType == typeof(DateTimeOffset)) { return(o => column.SetValue(o, new DateTimeOffset(_columnReader.ReadInt64(), new TimeSpan(_columnReader.ReadInt64())))); } if (column.PropertyType == typeof(DateTimeOffset?)) { return(o => column.SetValue(o, _columnReader.ReadBoolean() ? new DateTimeOffset(_columnReader.ReadInt64(), new TimeSpan(_columnReader.ReadInt64())) : (DateTimeOffset?)null)); } if (column.PropertyType == typeof(string)) { return(o => column.SetValue(o, _columnReader.ReadString())); } if (column.PropertyType.IsEnum) { var underlyingType = Enum.GetUnderlyingType(column.PropertyType); if (underlyingType == typeof(byte)) { return(o => column.SetValue(o, _columnReader.ReadByte())); } if (underlyingType == typeof(short)) { return(o => column.SetValue(o, _columnReader.ReadInt16())); } if (underlyingType == typeof(int)) { return(o => column.SetValue(o, _columnReader.ReadInt32())); } throw new NotSupportedException("Unsupported underlying enum type: " + underlyingType.FullName); } if (Nullable.GetUnderlyingType(column.PropertyType)?.IsEnum == true) { var underlyingType = Enum.GetUnderlyingType(Nullable.GetUnderlyingType(column.PropertyType)); if (underlyingType == typeof(byte)) { return(o => column.SetValue(o, _columnReader.ReadBoolean() ? (object)_columnReader.ReadByte() : null)); } if (underlyingType == typeof(short)) { return(o => column.SetValue(o, _columnReader.ReadBoolean() ? (object)_columnReader.ReadInt16() : null)); } if (underlyingType == typeof(int)) { return(o => column.SetValue(o, _columnReader.ReadBoolean() ? (object)_columnReader.ReadInt32() : null)); } throw new NotSupportedException("Unsupported underlying enum type: " + underlyingType.FullName); } if (column.PropertyType.IsArray) { var reader = new TypeReaderFactory(_path + column.Name + ".", column.PropertyType.GetElementType(), customReaders); return(o => { var isNonNull = _columnReader.ReadBoolean(); if (!isNonNull) { column.SetValue(o, null); } else { var length = _columnReader.ReadUInt16(); var array = (Array)Activator.CreateInstance(column.PropertyType, length); for (int i = 0; i < length; i++) { array.SetValue(reader.ReadNew(), i); } column.SetValue(o, array); } }); } if (!column.PropertyType.IsValueType) { var reader = new TypeReaderFactory(_path + column.Name + ".", column.PropertyType, customReaders); return(o => { var isNonNull = _columnReader.ReadBoolean(); column.SetValue(o, isNonNull ? reader.ReadNew() : null); }); } throw new Exception("Unsupported column type " + column.PropertyType.Name); }