Beispiel #1
0
        public virtual void Read(BinaryReader reader)
        {
            FieldInfo[] fields = ReaderHelper.GetFields(GetType());
            foreach (FieldInfo field in fields)
            {
                IReadableType type = field.GetCustomAttributes <IReadableType>().FirstOrDefault();

                IEnumerable <IConditionalType> conditions = field.GetCustomAttributes <IConditionalType>();
                bool skip = false;
                foreach (IConditionalType condition in conditions)
                {
                    if (!condition.ShouldDo(fields, this))
                    {
                        skip = true;
                    }
                }

                if (skip)
                {
                    continue;
                }

                if (type == null)
                {
                    type = new Types.Default();
                }
                field.SetValue(this, type.Read(reader, field));
                if (__readabledata_attrs == null)
                {
                    __readabledata_attrs = new Dictionary <string, IReadableType>();
                }
                __readabledata_attrs[field.Name] = type;
            }
        }
Beispiel #2
0
        public long GetMutableSize(Type t, object obj)
        {
            FieldInfo[] fields = ReaderHelper.GetFields(t);
            long        size   = 0;

            foreach (FieldInfo field in fields)
            {
                IReadableType type = field.GetCustomAttributes <IReadableType>().FirstOrDefault();
                if (type == null)
                {
                    type = new Types.Default();
                }

                IEnumerable <IConditionalType> conditions = field.GetCustomAttributes <IConditionalType>();

                if (field.FieldType.IsArray || field.GetValue(obj) is string)
                {
                    size += type.GetSize(field, field.GetValue(obj));
                }
                else if (field.FieldType.IsClass)
                {
                    size += type.GetSize(field, field.GetValue(obj));
                }
                else if (conditions.Any())
                {
                    size += type.GetSize(field, field.GetValue(obj));
                }
            }

            return(size);
        }
Beispiel #3
0
            public override object Read(BinaryReader reader, FieldInfo field)
            {
                Array array = Array.CreateInstance(Type, Count);

                for (int i = 0; i < Count; i++)
                {
                    array.SetValue(ReaderHelper.ReadType(Type, reader), i);
                }
                return(array);
            }
Beispiel #4
0
        public static long GetClassSize(Type t, object obj, string breakStartField = null, string breakEndField = null, bool breakIncludeBaseStartSize = false)
        {
            FieldInfo[] fields = ReaderHelper.GetFields(t);
            long        size   = 0;
            Dictionary <string, IReadableType> attrs = null;

            if (t.IsSubclassOf(typeof(ReadableData)))
            {
                attrs = ((ReadableData)obj).__readabledata_attrs;   // get the same Attribute instances as before
            }

            foreach (FieldInfo field in fields)
            {
                IReadableType type = field.GetCustomAttributes <IReadableType>()
                                     .FirstOrDefault(x => x.GetType() != typeof(Types.Conditional));
                if (type == null)
                {
                    type = new Types.Default();
                }
                if (breakStartField != null && field.Name == breakStartField)
                {
                    return(size + (breakIncludeBaseStartSize ? type.GetNoDataStartSize(field, field.GetValue(obj)) : 0));
                }

                IEnumerable <IConditionalType> conditions = field.GetCustomAttributes <IConditionalType>();
                bool skip = false;
                foreach (IConditionalType condition in conditions)
                {
                    if (!condition.ShouldDo(fields, obj))
                    {
                        skip = true;
                    }
                }

                if (skip == false)
                {
                    if (attrs != null && attrs.ContainsKey(field.Name))
                    {
                        type = attrs[field.Name];                                                  // restore
                    }
                    size += type.GetSize(field, field.GetValue(obj));
                }
                if (breakEndField != null && field.Name == breakEndField)
                {
                    return(size);
                }
            }

            if (breakEndField != null || breakStartField != null)
            {
                return(-1);
            }

            return(size);
        }
Beispiel #5
0
            public override object Read(BinaryReader reader, FieldInfo field)
            {
                long count = Convert.ToInt64(ReaderHelper.ReadType(CountType, reader));

                Array array = Array.CreateInstance(Type, count);

                for (int i = 0; i < count; i++)
                {
                    array.SetValue(ReaderHelper.ReadType(Type, reader), i);
                }
                return(array);
            }
Beispiel #6
0
        public static long GetObjectSize(Type t, object obj)
        {
            long size = 0;

            bool isPrimitive = t.IsPrimitive || t == typeof(decimal);
            bool isStruct    = t.IsValueType && !t.IsPrimitive;

            if (t.IsArray)
            {
                size += GetArraySize(t, obj);
            }
            else if (obj is string)
            {
                size += ((string)obj).Length;
            }
            else if (t.IsClass)
            {
                size += GetClassSize(t, obj);
            }
            else if (isPrimitive)
            {
                size += ReaderHelper.GetPrimitiveSize(t);
            }
            else if (t.IsEnum)
            {
                size += GetObjectSize(t.GetEnumUnderlyingType(), obj);
            }
            else if (isStruct)
            {
                size += GetClassSize(t, obj);
            }
            else
            {
                throw new Exception("error");
            }

            return(size);
        }
Beispiel #7
0
 public override object Read(BinaryReader reader, FieldInfo field)
 {
     return(ReaderHelper.ReadType(field.FieldType, reader));
 }