public static void WriteObject(IExportable instance, REDBinaryWriter red) { red.Write((byte)0); var fields = REDReflection.GetREDFields(instance.GetType()); foreach (var field in fields) { var value = field.GetValue(instance); if (value == null || value == default) { continue; } var(name, type) = REDReflection.GetREDNameTypePair(field); red.Write((CName)name); red.Write((CName)type); var start = red.BaseStream.Position; red.Write(0u); WriteProperty(value, red); var end = red.BaseStream.Position; red.BaseStream.Seek(start, SeekOrigin.Begin); red.Write(Convert.ToUInt32(end - start)); red.BaseStream.Seek(end, SeekOrigin.Begin); } red.Write((ushort)0); }
public static void WriteProperty(object value, REDBinaryWriter stream) { var type = value.GetType(); if (type.IsEnum) { var names = EnumExtensions.ConvertToNames((Enum)value); if (type.IsDefined(typeof(FlagsAttribute), false)) { foreach (var name in names) { stream.Write(name); } stream.Write((ushort)0); } else { if (names.Count() == 0) { stream.Write((ushort)0); } else { stream.Write(names.First()); } } return; } var tCode = Type.GetTypeCode(type); switch (tCode) { case TypeCode.Byte: stream.Write((byte)value); return; case TypeCode.UInt16: stream.Write((ushort)value); return; case TypeCode.UInt32: stream.Write((uint)value); return; case TypeCode.UInt64: stream.Write((ulong)value); return; case TypeCode.SByte: stream.Write((sbyte)value); return; case TypeCode.Int16: stream.Write((short)value); return; case TypeCode.Int32: stream.Write((int)value); return; case TypeCode.Int64: stream.Write((long)value); return; case TypeCode.Boolean: stream.Write((bool)value); return; case TypeCode.Single: stream.Write((float)value); return; case TypeCode.Double: stream.Write((double)value); return; case TypeCode.String: stream.Write((string)value); return; } if (value is IExportable obj) { } }