Ejemplo n.º 1
0
        /// <see cref="IClassSerializationTools"/>
        int IClassSerializationTools.FixedSize(ClassModel model, PropertyModel property)
        {
            var child = GetChild(model, property);

            if (child != null)
            {
                if (child is ClassModel && !BinarySize.IsVariable((ClassModel)child))
                {
                    return(BinarySize.OfClass((ClassModel)child, this));
                }
                if (child is EnumModel)
                {
                    return(BinarySize.OfType(((EnumModel)child).BaseType));
                }
            }

            // Fall back to supported types
            switch (property.ElementType)
            {
            case nameof(DateTime):
                return(8);

            default:
                return(0);
            }
        }
Ejemplo n.º 2
0
        /// <see cref="IClassSerializationTools"/>
        string IClassSerializationTools.ReferenceSize(ClassModel model, PropertyModel property)
        {
            var child = GetChild(model, property);

            // Check if the class was parsed or comes from another assembly
            if (child == null)
            {
                // For now we just switch supported type names
                switch (property.ElementType)
                {
                case nameof(DateTime):
                    return(property.IsCollection ? $"{GeneratorTools.CollectionSize(property)} * 8" : null);

                default:
                    return(null);
                }
            }

            string entrySize = null;

            if (child is ClassModel)
            {
                var classChild = (ClassModel)child;
                if (property.IsCollection)
                {
                    entrySize = BinarySize.IsVariable(classChild) ? "Sum(entry => entry.Size)" : $"{GeneratorTools.CollectionSize(property)} * {BinarySize.OfClass(classChild, this)}";
                }
                else if (BinarySize.IsVariable(property))
                {
                    entrySize = "Size";
                }
                else
                {
                    return(null);
                }
            }
            else if (child is EnumModel)
            {
                var enumChild = (EnumModel)child;
                if (property.IsCollection)
                {
                    entrySize = $"{GeneratorTools.CollectionSize(property)} * {BinarySize.OfType(enumChild.BaseType)}";
                }
                else
                {
                    return(null);
                }
            }


            return(entrySize);
        }
Ejemplo n.º 3
0
        /// <summary>
        /// Create the template output
        /// </summary>
        public virtual string TransformText()
        {
            this.Write("        /// <summary>\r\n        /// Binary size of the object\r\n        /// </summa" +
                       "ry>\r\n        public int Size\r\n        {\r\n            get \r\n            { \r\n     " +
                       "           var size = ");

            #line 13 "C:\Users\Thomas\Documents\Development\CGbR\CGbR\Generator\Serialization\BinarySerializerGenerator.tt"
            this.Write(this.ToStringHelper.ToStringWithCulture(BinarySize.OfClass(Model, Tools)));

            #line default
            #line hidden
            this.Write(";\r\n                // Add size for collections and strings\r\n");

            #line 15 "C:\Users\Thomas\Documents\Development\CGbR\CGbR\Generator\Serialization\BinarySerializerGenerator.tt"

            foreach (var property in Model.Properties.Where(BinarySize.IsVariable)
                     .Where(p => p.HasAttribute(nameof(DataMemberAttribute))))
            {
                string entrySize;
                switch (property.ValueType)
                {
                case ModelValueType.Class:
                    // Find class from other messages
                    entrySize = Tools.ReferenceSize(Model, property);
                    if (entrySize == null)
                    {
                        continue;
                    }
                    break;

                case ModelValueType.String:
                    // Flexible strings are only included as their length because the length field is part of the fixed size
                    entrySize = property.IsCollection ? "Sum(s => s.Length + 2)" : "Length";
                    break;

                case ModelValueType.Byte:
                    entrySize = "Length";
                    break;

                default:
                    entrySize = $"{GeneratorTools.CollectionSize(property)} * {BinarySize.OfProperty(property)}";
                    break;
                }


            #line default
            #line hidden
                this.Write("                size += ");

            #line 40 "C:\Users\Thomas\Documents\Development\CGbR\CGbR\Generator\Serialization\BinarySerializerGenerator.tt"
                this.Write(this.ToStringHelper.ToStringWithCulture(property.Name));

            #line default
            #line hidden
                this.Write(" == null ? 0 : ");

            #line 40 "C:\Users\Thomas\Documents\Development\CGbR\CGbR\Generator\Serialization\BinarySerializerGenerator.tt"
                this.Write(this.ToStringHelper.ToStringWithCulture($"{property.Name}.{entrySize}"));

            #line default
            #line hidden
                this.Write(";\r\n");

            #line 41 "C:\Users\Thomas\Documents\Development\CGbR\CGbR\Generator\Serialization\BinarySerializerGenerator.tt"
            }


            #line default
            #line hidden
            this.Write(@"  
                return size;              
            }
        }

        /// <summary>
        /// Convert object to bytes
        /// </summary>
        public byte[] ToBytes()
        {
            var index = 0;
            var bytes = new byte[Size];

            return ToBytes(bytes, ref index);
        }

        /// <summary>
        /// Convert object to bytes within object tree
        /// </summary>
        void IByteSerializable.ToBytes(byte[] bytes, ref int index)
        {
            ToBytes(bytes, ref index);
        }

        /// <summary>
        /// Convert object to bytes within object tree
        /// </summary>
        public byte[] ToBytes(byte[] bytes, ref int index)
        {
            if (index + Size > bytes.Length)
                throw new ArgumentOutOfRangeException(""index"", ""Object does not fit in array"");

");

            #line 75 "C:\Users\Thomas\Documents\Development\CGbR\CGbR\Generator\Serialization\BinarySerializerGenerator.tt"

            foreach (var property in Model.Properties.WhereAttribute(nameof(DataMemberAttribute)))
            {
            #line default
            #line hidden
                this.Write("            // Convert ");

            #line 79 "C:\Users\Thomas\Documents\Development\CGbR\CGbR\Generator\Serialization\BinarySerializerGenerator.tt"
                this.Write(this.ToStringHelper.ToStringWithCulture(property.Name));

            #line default
            #line hidden
                this.Write("\r\n");

            #line 80 "C:\Users\Thomas\Documents\Development\CGbR\CGbR\Generator\Serialization\BinarySerializerGenerator.tt"

                ToBytes(property);
            }


            #line default
            #line hidden
            this.Write("            return bytes;\r\n        }\r\n\r\n        /// <summary>\r\n        /// Create" +
                       " object from byte array\r\n        /// </summary>\r\n        public ");

            #line 90 "C:\Users\Thomas\Documents\Development\CGbR\CGbR\Generator\Serialization\BinarySerializerGenerator.tt"
            this.Write(this.ToStringHelper.ToStringWithCulture(Model.Name));

            #line default
            #line hidden
            this.Write(@" FromBytes(byte[] bytes)
        {
            var index = 0;            
            return FromBytes(bytes, ref index); 
        }

        /// <summary>
        /// Create object from segment in byte array
        /// </summary>
        void IByteSerializable.FromBytes(byte[] bytes, ref int index)
        {
            FromBytes(bytes, ref index);
        }

        /// <summary>
        /// Create object from segment in byte array
        /// </summary>
        public ");

            #line 107 "C:\Users\Thomas\Documents\Development\CGbR\CGbR\Generator\Serialization\BinarySerializerGenerator.tt"
            this.Write(this.ToStringHelper.ToStringWithCulture(Model.Name));

            #line default
            #line hidden
            this.Write(" FromBytes(byte[] bytes, ref int index)\r\n        {\r\n");

            #line 109 "C:\Users\Thomas\Documents\Development\CGbR\CGbR\Generator\Serialization\BinarySerializerGenerator.tt"

            foreach (var property in Model.Properties.WhereAttribute(nameof(DataMemberAttribute)))
            {
            #line default
            #line hidden
                this.Write("            // Read ");

            #line 113 "C:\Users\Thomas\Documents\Development\CGbR\CGbR\Generator\Serialization\BinarySerializerGenerator.tt"
                this.Write(this.ToStringHelper.ToStringWithCulture(property.Name));

            #line default
            #line hidden
                this.Write("\r\n");

            #line 114 "C:\Users\Thomas\Documents\Development\CGbR\CGbR\Generator\Serialization\BinarySerializerGenerator.tt"

                FromBytes(property);
            }


            #line default
            #line hidden
            this.Write("\r\n            return this;\r\n        }\r\n");
            return(this.GenerationEnvironment.ToString());
        }
Ejemplo n.º 4
0
        //---------------------------------------------
        // Generate a byte conversion for this field
        //--------------------------------------------
        private void ToBytes(PropertyModel property)
        {
            // Indentation throughout the method
            var indent = property.IsCollection && property.ValueType != ModelValueType.Byte ? "\t" : string.Empty;

            // Optional length prefix
            GenerateLengthPrefix(property);

            CollectionWrapper(property, true);

            // Now we add the real value
            var    target = property.IsCollection ? "value" : property.Name;
            var    nullable = false;
            string conversion, increment = null;

            switch (property.ValueType)
            {
            case ModelValueType.String:
                conversion = $"GeneratorByteConverter.Include({target}, bytes, ref index)";
                break;

            case ModelValueType.Class:
                conversion = Tools.ClassToBytes(Model, property);
                break;

            case ModelValueType.Boolean:
                conversion = $"bytes[index++] = {target} ? (byte)1 : (byte)0";
                break;

            case ModelValueType.Byte:
                conversion = property.IsCollection ? BlockCopy(false, property.Name, $"{property.Name}.Length") : $"bytes[index++] = {target}";
                increment  = property.IsCollection ? $"{property.Name}.Length" : null;
                nullable   = property.IsCollection;
                break;

            default:
                conversion = BlockCopy(true, target, BinarySize.OfProperty(property).ToString("D"));
                break;
            }

            if (nullable || property.IsCollection)
            {
                indent += "\t";
            }

            NullableWrapper(property, nullable, true);



        #line default
        #line hidden

        #line 167 "C:\Users\Thomas\Documents\Development\CGbR\CGbR\Generator\Serialization\BinarySerializerGenerator.tt"
            this.Write("            ");


        #line default
        #line hidden

        #line 168 "C:\Users\Thomas\Documents\Development\CGbR\CGbR\Generator\Serialization\BinarySerializerGenerator.tt"
            this.Write(this.ToStringHelper.ToStringWithCulture(indent));


        #line default
        #line hidden

        #line 168 "C:\Users\Thomas\Documents\Development\CGbR\CGbR\Generator\Serialization\BinarySerializerGenerator.tt"
            this.Write(this.ToStringHelper.ToStringWithCulture(conversion));


        #line default
        #line hidden

        #line 168 "C:\Users\Thomas\Documents\Development\CGbR\CGbR\Generator\Serialization\BinarySerializerGenerator.tt"
            this.Write(";\r\n");


        #line default
        #line hidden

        #line 169 "C:\Users\Thomas\Documents\Development\CGbR\CGbR\Generator\Serialization\BinarySerializerGenerator.tt"

            if (!string.IsNullOrEmpty(increment))
            {
        #line default
        #line hidden

        #line 172 "C:\Users\Thomas\Documents\Development\CGbR\CGbR\Generator\Serialization\BinarySerializerGenerator.tt"
                this.Write("            ");


        #line default
        #line hidden

        #line 173 "C:\Users\Thomas\Documents\Development\CGbR\CGbR\Generator\Serialization\BinarySerializerGenerator.tt"
                this.Write(this.ToStringHelper.ToStringWithCulture(indent));


        #line default
        #line hidden

        #line 173 "C:\Users\Thomas\Documents\Development\CGbR\CGbR\Generator\Serialization\BinarySerializerGenerator.tt"
                this.Write("index += ");


        #line default
        #line hidden

        #line 173 "C:\Users\Thomas\Documents\Development\CGbR\CGbR\Generator\Serialization\BinarySerializerGenerator.tt"
                this.Write(this.ToStringHelper.ToStringWithCulture(increment));


        #line default
        #line hidden

        #line 173 "C:\Users\Thomas\Documents\Development\CGbR\CGbR\Generator\Serialization\BinarySerializerGenerator.tt"
                this.Write(";\r\n");


        #line default
        #line hidden

        #line 174 "C:\Users\Thomas\Documents\Development\CGbR\CGbR\Generator\Serialization\BinarySerializerGenerator.tt"
            }

            NullableWrapper(property, nullable, false);

            CollectionWrapper(property, false);
        }