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 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);
        }