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