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

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

                if (skip)
                {
                    continue;
                }

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

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

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

                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);
        }
Example #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);
            }
Example #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, ReadableType> attrs = null;

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

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

                IEnumerable <ConditionalType> conditions = field.GetCustomAttributes <ConditionalType>();
                bool skip = false;
                foreach (ConditionalType 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);
        }
Example #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);
            }
Example #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);
        }
Example #7
0
 public override object Read(BinaryReader reader, FieldInfo field)
 {
     return(ReaderHelper.ReadType(field.FieldType, reader));
 }