コード例 #1
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);
        }
コード例 #2
0
        private bool CollectionWrapper(PropertyModel prop, bool head)
        {
            if (!prop.IsCollection)
            {
                return(false);
            }

            if (head)
            {
        #line default
        #line hidden

        #line 93 "C:\Users\Thomas\Documents\Development\CGbR\CGbR\Generator\Clone\CloneGenerator.tt"
                this.Write(" \r\n                if (");


        #line default
        #line hidden

        #line 94 "C:\Users\Thomas\Documents\Development\CGbR\CGbR\Generator\Clone\CloneGenerator.tt"
                this.Write(this.ToStringHelper.ToStringWithCulture(prop.Name));


        #line default
        #line hidden

        #line 94 "C:\Users\Thomas\Documents\Development\CGbR\CGbR\Generator\Clone\CloneGenerator.tt"
                this.Write(" != null)\r\n                {\r\n                    var temp");


        #line default
        #line hidden

        #line 96 "C:\Users\Thomas\Documents\Development\CGbR\CGbR\Generator\Clone\CloneGenerator.tt"
                this.Write(this.ToStringHelper.ToStringWithCulture(prop.Name));


        #line default
        #line hidden

        #line 96 "C:\Users\Thomas\Documents\Development\CGbR\CGbR\Generator\Clone\CloneGenerator.tt"
                this.Write(" = ");


        #line default
        #line hidden

        #line 96 "C:\Users\Thomas\Documents\Development\CGbR\CGbR\Generator\Clone\CloneGenerator.tt"
                this.Write(this.ToStringHelper.ToStringWithCulture(GeneratorTools.CollectionConstructor(prop, $"{prop.Name}.{GeneratorTools.CollectionSize(prop)}")));


        #line default
        #line hidden

        #line 96 "C:\Users\Thomas\Documents\Development\CGbR\CGbR\Generator\Clone\CloneGenerator.tt"
                this.Write(";\r\n");


        #line default
        #line hidden

        #line 97 "C:\Users\Thomas\Documents\Development\CGbR\CGbR\Generator\Clone\CloneGenerator.tt"

                if (GeneratorTools.SupportsForLoop(prop))
                {
        #line default
        #line hidden

        #line 100 "C:\Users\Thomas\Documents\Development\CGbR\CGbR\Generator\Clone\CloneGenerator.tt"
                    this.Write("                    for (var i = 0; i < ");


        #line default
        #line hidden

        #line 101 "C:\Users\Thomas\Documents\Development\CGbR\CGbR\Generator\Clone\CloneGenerator.tt"
                    this.Write(this.ToStringHelper.ToStringWithCulture(prop.Name));


        #line default
        #line hidden

        #line 101 "C:\Users\Thomas\Documents\Development\CGbR\CGbR\Generator\Clone\CloneGenerator.tt"
                    this.Write(".");


        #line default
        #line hidden

        #line 101 "C:\Users\Thomas\Documents\Development\CGbR\CGbR\Generator\Clone\CloneGenerator.tt"
                    this.Write(this.ToStringHelper.ToStringWithCulture(GeneratorTools.CollectionSize(prop)));


        #line default
        #line hidden

        #line 101 "C:\Users\Thomas\Documents\Development\CGbR\CGbR\Generator\Clone\CloneGenerator.tt"
                    this.Write("; i++)\r\n                    {\r\n                        var value = ");


        #line default
        #line hidden

        #line 103 "C:\Users\Thomas\Documents\Development\CGbR\CGbR\Generator\Clone\CloneGenerator.tt"
                    this.Write(this.ToStringHelper.ToStringWithCulture(prop.Name));


        #line default
        #line hidden

        #line 103 "C:\Users\Thomas\Documents\Development\CGbR\CGbR\Generator\Clone\CloneGenerator.tt"
                    this.Write("[i];\r\n");


        #line default
        #line hidden

        #line 104 "C:\Users\Thomas\Documents\Development\CGbR\CGbR\Generator\Clone\CloneGenerator.tt"
                }
                else
                {
        #line default
        #line hidden

        #line 108 "C:\Users\Thomas\Documents\Development\CGbR\CGbR\Generator\Clone\CloneGenerator.tt"
                    this.Write("                    foreach (var value in ");


        #line default
        #line hidden

        #line 109 "C:\Users\Thomas\Documents\Development\CGbR\CGbR\Generator\Clone\CloneGenerator.tt"
                    this.Write(this.ToStringHelper.ToStringWithCulture(prop.Name));


        #line default
        #line hidden

        #line 109 "C:\Users\Thomas\Documents\Development\CGbR\CGbR\Generator\Clone\CloneGenerator.tt"
                    this.Write(")\r\n                    {\r\n");


        #line default
        #line hidden

        #line 111 "C:\Users\Thomas\Documents\Development\CGbR\CGbR\Generator\Clone\CloneGenerator.tt"
                }
            }
            else
            {
                if (prop.CollectionType == "Array")
                {
        #line default
        #line hidden

        #line 118 "C:\Users\Thomas\Documents\Development\CGbR\CGbR\Generator\Clone\CloneGenerator.tt"
                    this.Write("                        temp");


        #line default
        #line hidden

        #line 119 "C:\Users\Thomas\Documents\Development\CGbR\CGbR\Generator\Clone\CloneGenerator.tt"
                    this.Write(this.ToStringHelper.ToStringWithCulture(prop.Name));


        #line default
        #line hidden

        #line 119 "C:\Users\Thomas\Documents\Development\CGbR\CGbR\Generator\Clone\CloneGenerator.tt"
                    this.Write("[i] = value;\r\n");


        #line default
        #line hidden

        #line 120 "C:\Users\Thomas\Documents\Development\CGbR\CGbR\Generator\Clone\CloneGenerator.tt"
                }
                else
                {
        #line default
        #line hidden

        #line 124 "C:\Users\Thomas\Documents\Development\CGbR\CGbR\Generator\Clone\CloneGenerator.tt"
                    this.Write("                        temp");


        #line default
        #line hidden

        #line 125 "C:\Users\Thomas\Documents\Development\CGbR\CGbR\Generator\Clone\CloneGenerator.tt"
                    this.Write(this.ToStringHelper.ToStringWithCulture(prop.Name));


        #line default
        #line hidden

        #line 125 "C:\Users\Thomas\Documents\Development\CGbR\CGbR\Generator\Clone\CloneGenerator.tt"
                    this.Write(".Add(value);\r\n");


        #line default
        #line hidden

        #line 126 "C:\Users\Thomas\Documents\Development\CGbR\CGbR\Generator\Clone\CloneGenerator.tt"
                }


        #line default
        #line hidden

        #line 128 "C:\Users\Thomas\Documents\Development\CGbR\CGbR\Generator\Clone\CloneGenerator.tt"
                this.Write("                    }\r\n                    copy.");


        #line default
        #line hidden

        #line 130 "C:\Users\Thomas\Documents\Development\CGbR\CGbR\Generator\Clone\CloneGenerator.tt"
                this.Write(this.ToStringHelper.ToStringWithCulture(prop.Name));


        #line default
        #line hidden

        #line 130 "C:\Users\Thomas\Documents\Development\CGbR\CGbR\Generator\Clone\CloneGenerator.tt"
                this.Write(" = temp");


        #line default
        #line hidden

        #line 130 "C:\Users\Thomas\Documents\Development\CGbR\CGbR\Generator\Clone\CloneGenerator.tt"
                this.Write(this.ToStringHelper.ToStringWithCulture(prop.Name));


        #line default
        #line hidden

        #line 130 "C:\Users\Thomas\Documents\Development\CGbR\CGbR\Generator\Clone\CloneGenerator.tt"
                this.Write(";\r\n                }\r\n");


        #line default
        #line hidden

        #line 132 "C:\Users\Thomas\Documents\Development\CGbR\CGbR\Generator\Clone\CloneGenerator.tt"
            }

            return(true);
        }
コード例 #3
0
        //--------------------------------------
        // Code fragments wrapping a collection
        //--------------------------------------
        private void CollectionWrapper(PropertyModel property, bool start)
        {
            if (!property.IsCollection || property.ValueType == ModelValueType.Byte)
            {
                return;
            }

            if (start)
            {
        #line default
        #line hidden

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


        #line default
        #line hidden

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


        #line default
        #line hidden

        #line 245 "C:\Users\Thomas\Documents\Development\CGbR\CGbR\Generator\Serialization\BinarySerializerGenerator.tt"
                this.Write(" != null)\r\n            {\r\n");


        #line default
        #line hidden

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

                if (GeneratorTools.SupportsForLoop(property))
                {
                    var dimensions = new[] { "i", "j", "k" };
                    for (var dimension = 0; dimension < property.Dimensions; dimension++)
                    {
                        var targetDimension = property.Dimensions > 1 ? dimension : -1;



        #line default
        #line hidden

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


        #line default
        #line hidden

        #line 256 "C:\Users\Thomas\Documents\Development\CGbR\CGbR\Generator\Serialization\BinarySerializerGenerator.tt"
                        this.Write(this.ToStringHelper.ToStringWithCulture(dimensions[dimension]));


        #line default
        #line hidden

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


        #line default
        #line hidden

        #line 256 "C:\Users\Thomas\Documents\Development\CGbR\CGbR\Generator\Serialization\BinarySerializerGenerator.tt"
                        this.Write(this.ToStringHelper.ToStringWithCulture(dimensions[dimension]));


        #line default
        #line hidden

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


        #line default
        #line hidden

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


        #line default
        #line hidden

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


        #line default
        #line hidden

        #line 256 "C:\Users\Thomas\Documents\Development\CGbR\CGbR\Generator\Serialization\BinarySerializerGenerator.tt"
                        this.Write(this.ToStringHelper.ToStringWithCulture(GeneratorTools.CollectionSize(property, targetDimension)));


        #line default
        #line hidden

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


        #line default
        #line hidden

        #line 256 "C:\Users\Thomas\Documents\Development\CGbR\CGbR\Generator\Serialization\BinarySerializerGenerator.tt"
                        this.Write(this.ToStringHelper.ToStringWithCulture(dimensions[dimension]));


        #line default
        #line hidden

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


        #line default
        #line hidden

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


        #line default
        #line hidden

        #line 259 "C:\Users\Thomas\Documents\Development\CGbR\CGbR\Generator\Serialization\BinarySerializerGenerator.tt"
                    this.Write("                {\r\n                    var value = ");


        #line default
        #line hidden

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


        #line default
        #line hidden

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


        #line default
        #line hidden

        #line 261 "C:\Users\Thomas\Documents\Development\CGbR\CGbR\Generator\Serialization\BinarySerializerGenerator.tt"
                    this.Write(this.ToStringHelper.ToStringWithCulture(string.Join(",", Enumerable.Range(0, property.Dimensions).Select(i => dimensions[i]))));


        #line default
        #line hidden

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


        #line default
        #line hidden

        #line 262 "C:\Users\Thomas\Documents\Development\CGbR\CGbR\Generator\Serialization\BinarySerializerGenerator.tt"
                }
                else
                {
        #line default
        #line hidden

        #line 266 "C:\Users\Thomas\Documents\Development\CGbR\CGbR\Generator\Serialization\BinarySerializerGenerator.tt"
                    this.Write("                foreach(var value in ");


        #line default
        #line hidden

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


        #line default
        #line hidden

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


        #line default
        #line hidden

        #line 269 "C:\Users\Thomas\Documents\Development\CGbR\CGbR\Generator\Serialization\BinarySerializerGenerator.tt"
                }
            }
            else
            {
        #line default
        #line hidden

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


        #line default
        #line hidden

        #line 278 "C:\Users\Thomas\Documents\Development\CGbR\CGbR\Generator\Serialization\BinarySerializerGenerator.tt"
            }
        }
コード例 #4
0
        //-------------------------------------------------------
        // Generate 2 byte length prefix for collection length
        //-------------------------------------------------------
        private void GenerateLengthPrefix(PropertyModel property)
        {
            if (!property.IsCollection)
            {
                return;
            }



        #line default
        #line hidden

        #line 190 "C:\Users\Thomas\Documents\Development\CGbR\CGbR\Generator\Serialization\BinarySerializerGenerator.tt"
            this.Write("            // Two bytes length information for each dimension\r\n");


        #line default
        #line hidden

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

            for (var dimension = 0; dimension < property.Dimensions; dimension++)
            {
        #line default
        #line hidden

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


        #line default
        #line hidden

        #line 196 "C:\Users\Thomas\Documents\Development\CGbR\CGbR\Generator\Serialization\BinarySerializerGenerator.tt"
                this.Write(this.ToStringHelper.ToStringWithCulture(BlockCopy(true, $"(ushort)({property.Name} == null ? 0 : {property.Name}.{GeneratorTools.CollectionSize(property, property.Dimensions > 1 ? dimension : -1 )})", "2")));


        #line default
        #line hidden

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


        #line default
        #line hidden

        #line 197 "C:\Users\Thomas\Documents\Development\CGbR\CGbR\Generator\Serialization\BinarySerializerGenerator.tt"
            }
        }
コード例 #5
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());
        }