Пример #1
0
        private object ReadTypeClass(BinaryReaderX br, Type readType, ValueStorage storage)
        {
            var typeAttributes = new MemberAttributeInfo(readType);

            var bitFieldInfoAttribute = typeAttributes.BitFieldInfoAttribute;
            var alignmentAttribute    = typeAttributes.AlignmentAttribute;

            if (bitFieldInfoAttribute != null)
            {
                br.ResetBitBuffer();
            }

            br.BitOrder  = (bitFieldInfoAttribute?.BitOrder != BitOrder.Default ? bitFieldInfoAttribute?.BitOrder : br.BitOrder) ?? br.BitOrder;
            br.BlockSize = bitFieldInfoAttribute?.BlockSize ?? br.BlockSize;
            if (br.BlockSize != 8 && br.BlockSize != 4 && br.BlockSize != 2 && br.BlockSize != 1)
            {
                throw new InvalidBitFieldInfoException(br.BlockSize);
            }

            var item = Activator.CreateInstance(readType);

            var fields = readType.GetFields().OrderBy(fi => fi.MetadataToken);

            foreach (var field in fields)
            {
                // If field condition is false, read no value and leave field to default
                var conditionAttribute = field.GetCustomAttribute <ConditionAttribute>();
                if (!ResolveCondition(conditionAttribute, storage))
                {
                    continue;
                }

                var bitInfo = field.GetCustomAttribute <BitFieldAttribute>();

                object fieldValue;
                if (bitInfo != null)
                {
                    fieldValue = Convert.ChangeType(br.ReadBits(bitInfo.BitLength), field.FieldType);
                }
                else
                {
                    fieldValue = ReadTypeInternal(br, field.FieldType, storage, field);
                }

                storage.Add(field.Name, fieldValue);
                field.SetValue(item, fieldValue);
            }

            if (alignmentAttribute != null)
            {
                br.SeekAlignment(alignmentAttribute.Alignment);
            }

            return(item);
        }
Пример #2
0
        private void WriteTypeClass(BinaryWriterX bw, object writeValue, Type writeType, ValueStorage storage)
        {
            var typeAttributes = new MemberAttributeInfo(writeType);

            var bitFieldInfoAttribute = typeAttributes.BitFieldInfoAttribute;
            var alignmentAttribute    = typeAttributes.AlignmentAttribute;

            if (bitFieldInfoAttribute != null)
            {
                bw.Flush();
            }

            bw.BitOrder  = (bitFieldInfoAttribute?.BitOrder != BitOrder.Default ? bitFieldInfoAttribute?.BitOrder : bw.BitOrder) ?? bw.BitOrder;
            bw.BlockSize = bitFieldInfoAttribute?.BlockSize ?? bw.BlockSize;
            if (bw.BlockSize != 8 && bw.BlockSize != 4 && bw.BlockSize != 2 && bw.BlockSize != 1)
            {
                throw new InvalidBitFieldInfoException(bw.BlockSize);
            }

            var fields = writeType.GetFields().OrderBy(fi => fi.MetadataToken);

            foreach (var field in fields)
            {
                // If field condition is false, write no value and ignore field
                var conditionAttribute = field.GetCustomAttribute <ConditionAttribute>();
                if (!ResolveCondition(conditionAttribute, storage))
                {
                    continue;
                }

                var fieldValue = field.GetValue(writeValue);
                storage.Add(field.Name, fieldValue);

                var bitInfo = field.GetCustomAttribute <BitFieldAttribute>();
                if (bitInfo != null)
                {
                    bw.WriteBits(Convert.ToInt64(fieldValue), bitInfo.BitLength);
                }
                else
                {
                    WriteTypeInternal(bw, fieldValue, storage, field);
                }
            }

            bw.Flush();

            // Apply alignment
            if (alignmentAttribute != null)
            {
                bw.WriteAlignment(alignmentAttribute.Alignment);
            }
        }