Exemplo n.º 1
0
        protected internal override void Write(ContentWriter output, IndexCollection value)
        {
            // Check if the buffer and can be saved as Int16.
            var shortIndices = true;

            foreach (var index in value)
            {
                if (index > ushort.MaxValue)
                {
                    shortIndices = false;
                    break;
                }
            }

            output.Write(shortIndices);

            var byteCount = shortIndices
                                ? value.Count * 2
                                : value.Count * 4;

            output.Write(byteCount);
            if (shortIndices)
            {
                foreach (var item in value)
                {
                    output.Write((ushort)item);
                }
            }
            else
            {
                foreach (var item in value)
                {
                    output.Write(item);
                }
            }
        }
Exemplo n.º 2
0
        protected internal override void Write(ContentWriter output, Texture2DContent value)
        {
            var mipmaps = value.Faces[0];   // Mipmap chain.
            var level0  = mipmaps[0];       // Most detailed mipmap level.

            SurfaceFormat format;

            if (!level0.TryGetFormat(out format))
            {
                throw new Exception("Couldn't get Format for TextureContent.");
            }

            output.Write((int)format);
            output.Write(level0.Width);
            output.Write(level0.Height);
            output.Write(mipmaps.Count);    // Number of mipmap levels.

            foreach (var level in mipmaps)
            {
                var pixelData = level.GetPixelData();
                output.Write(pixelData.Length);
                output.Write(pixelData);
            }
        }
Exemplo n.º 3
0
 /// <summary>
 /// Writes the value to the output.
 /// </summary>
 /// <param name="output">The output writer object.</param>
 /// <param name="value">The value to write to the output.</param>
 protected internal override void Write(ContentWriter output, TOutput value)
 {
     output.Write(value.Position);
     output.Write(value.Direction);
 }
Exemplo n.º 4
0
 /// <inheritdoc/>
 internal override void OnAddedToContentWriter(ContentWriter output)
 {
     base.OnAddedToContentWriter(output);
     _underlyingType       = Enum.GetUnderlyingType(typeof(T));
     _underlyingTypeWriter = output.GetTypeWriter(_underlyingType);
 }
 /// <summary>
 /// Writes the value to the output.
 /// </summary>
 /// <param name="output">The output writer object.</param>
 /// <param name="value">The value to write to the output.</param>
 protected internal override void Write(ContentWriter output, ExternalReference <T> value)
 {
     output.WriteExternalReference(value);
 }
 /// <summary>
 /// Allows type writers to add their element type writers to the content writer.
 /// </summary>
 /// <param name="writer">The content writer.</param>
 internal virtual void OnAddedToContentWriter(ContentWriter writer)
 {
 }
 /// <summary>
 /// Writes the value to the output.
 /// </summary>
 /// <param name="output">The output writer object.</param>
 /// <param name="value">The value to write to the output.</param>
 protected internal override void Write(ContentWriter output, TOutput value)
 {
     output.Write(value.Center);
     output.Write(value.Radius);
 }
Exemplo n.º 8
0
        /// <inheritdoc/>
        internal override void OnAddedToContentWriter(ContentWriter output)
        {
            base.OnAddedToContentWriter(output);

            _elementWriter = output.GetTypeWriter(typeof(T));
        }
Exemplo n.º 9
0
 protected internal abstract void Write(ContentWriter output, object value);
Exemplo n.º 10
0
 public override void Write(ContentWriter writer, string value)
 {
     writer.Write(value);
 }
Exemplo n.º 11
0
 public override void Write(ContentWriter writer, Single value)
 {
     writer.Write(value);
 }
Exemplo n.º 12
0
        private static void Write(object parent, ContentWriter output, MemberInfo member)
        {
            var property = member as PropertyInfo;
            var field    = member as FieldInfo;

            Debug.Assert(field != null || property != null);

            if (property != null)
            {
                // Properties must have at least a getter.
                if (property.CanRead == false)
                {
                    return;
                }

                // Skip over indexer properties.
                if (property.Name == "Item")
                {
                    var getMethod = ReflectionHelpers.GetPropertyGetMethod(property);
                    var setMethod = ReflectionHelpers.GetPropertySetMethod(property);

                    if ((getMethod != null && getMethod.GetParameters().Length > 0) ||
                        (setMethod != null && setMethod.GetParameters().Length > 0))
                    {
                        return;
                    }
                }
            }

            // Are we explicitly asked to ignore this item?
            if (ReflectionHelpers.GetCustomAttribute <ContentSerializerIgnoreAttribute>(member) != null)
            {
                return;
            }

            var contentSerializerAttribute = ReflectionHelpers.GetCustomAttribute <ContentSerializerAttribute>(member);

            if (contentSerializerAttribute == null)
            {
                if (property != null)
                {
                    // There is no ContentSerializerAttribute, so non-public
                    // properties cannot be serialized.
                    if (!ReflectionHelpers.PropertyIsPublic(property))
                    {
                        return;
                    }

                    // Check the type reader to see if it is safe to
                    // deserialize into the existing type.
                    if (!property.CanWrite && !output.CanDeserializeIntoExistingObject(property.PropertyType))
                    {
                        return;
                    }
                }
                else
                {
                    // There is no ContentSerializerAttribute, so non-public
                    // fields cannot be deserialized.
                    if (!field.IsPublic)
                    {
                        return;
                    }

                    // evolutional: Added check to skip initialise only fields
                    if (field.IsInitOnly)
                    {
                        return;
                    }
                }
            }

            Type   elementType;
            object memberObject;

            if (property != null)
            {
                elementType  = property.PropertyType;
                memberObject = property.GetValue(parent, null);
            }
            else
            {
                elementType  = field.FieldType;
                memberObject = field.GetValue(parent);
            }

            if (contentSerializerAttribute != null && contentSerializerAttribute.SharedResource)
            {
                output.WriteSharedResource(memberObject);
            }
            else
            {
                var writer = output.GetTypeWriter(elementType);
                if (writer == null || elementType == typeof(object) || elementType == typeof(Array))
                {
                    output.WriteObject(memberObject);
                }
                else
                {
                    output.WriteObject(memberObject, writer);
                }
            }
        }
Exemplo n.º 13
0
 public override void Write(ContentWriter writer, Int32 value)
 {
     writer.Write(value);
 }
Exemplo n.º 14
0
 public override void Write(ContentWriter writer, Char value)
 {
     writer.Write(value);
 }
Exemplo n.º 15
0
 protected abstract void WriteTextureHeader(ContentWriter output, SurfaceFormat format, int width, int height, int depth, int mipLevels);
Exemplo n.º 16
0
        protected internal override void Write(ContentWriter output, T value)
        {
            var typeWriter = output.GetTypeWriter(_underlyingType);

            output.WriteRawObject(Convert.ChangeType(value, _underlyingType), typeWriter);
        }
Exemplo n.º 17
0
 /// <summary>
 /// Writes the value to the output.
 /// </summary>
 /// <param name="output">The output writer object.</param>
 /// <param name="value">The value to write to the output.</param>
 protected internal override void Write(ContentWriter output, T value)
 {
 }
Exemplo n.º 18
0
 /// <summary>
 /// Writes the value to the output.
 /// </summary>
 /// <param name="output">The output writer object.</param>
 /// <param name="value">The value to write to the output.</param>
 protected internal override void Write(ContentWriter output, T value)
 {
     elementWriter.Write(output, Convert.ChangeType(value, underlyingType));
 }
Exemplo n.º 19
0
 public override void Write(ContentWriter writer, Vector3 value)
 {
     writer.Write(value);
 }
Exemplo n.º 20
0
 /// <summary>
 /// Writes the value to the output.
 /// </summary>
 /// <param name="output">The output writer object.</param>
 /// <param name="value">The value to write to the output.</param>
 protected internal override void Write(ContentWriter output, SongContent value)
 {
     output.Write(value.fileName);
     output.Write((int)value.duration.TotalMilliseconds);
 }
Exemplo n.º 21
0
 protected internal override void Write(ContentWriter output, VertexBufferContent value)
 {
     output.WriteRawObject(value.VertexDeclaration);
     output.Write((uint)(value.VertexData.Length / value.VertexDeclaration.VertexStride));
     output.Write(value.VertexData);
 }
Exemplo n.º 22
0
 /// <summary>
 /// Writes the value to the output.
 /// </summary>
 /// <param name="output">The output writer object.</param>
 /// <param name="value">The value to write to the output.</param>
 protected internal override void Write(ContentWriter output, TOutput value)
 {
     output.Write(value);
 }
Exemplo n.º 23
0
        private static void Write(object parent, ContentWriter output, MemberInfo member)
        {
            var property = member as PropertyInfo;
            var field    = member as FieldInfo;

            Debug.Assert(field != null || property != null);

            // Properties must have public get and set
            if (property != null && (property.CanWrite == false || property.CanRead == false))
            {
                return;
            }

            if (property != null && property.Name == "Item")
            {
                var getMethod = ReflectionHelpers.GetPropertyGetMethod(property);
                var setMethod = ReflectionHelpers.GetPropertySetMethod(property);

                if ((getMethod != null && getMethod.GetParameters().Length > 0) ||
                    (setMethod != null && setMethod.GetParameters().Length > 0))
                {
                    // This is presumably a property like this[indexer] and this
                    // should not get involved in the object deserialization.
                    return;
                }
            }

            var attr = ReflectionHelpers.GetCustomAttribute(member, typeof(ContentSerializerIgnoreAttribute));

            if (attr != null)
            {
                return;
            }

            var contentSerializerAttribute = ReflectionHelpers.GetCustomAttribute(member, typeof(ContentSerializerAttribute)) as ContentSerializerAttribute;

            bool isSharedResource = false;

            if (contentSerializerAttribute != null)
            {
                isSharedResource = contentSerializerAttribute.SharedResource;
            }
            else
            {
                if (property != null)
                {
                    if (!ReflectionHelpers.PropertyIsPublic(property))
                    {
                        return;
                    }
                }
                else
                {
                    if (!field.IsPublic)
                    {
                        return;
                    }

                    // evolutional: Added check to skip initialise only fields
                    if (field.IsInitOnly)
                    {
                        return;
                    }
                }
            }

            ContentTypeWriter writer;
            Type   elementType;
            object memberObject;

            if (property != null)
            {
                elementType  = property.PropertyType;
                writer       = output.GetTypeWriter(elementType);
                memberObject = property.GetValue(parent, null);
            }
            else
            {
                elementType  = field.FieldType;
                writer       = output.GetTypeWriter(elementType);
                memberObject = field.GetValue(parent);
            }

            if (!isSharedResource)
            {
                if (writer == null && elementType == typeof(object))
                {
                    // Write elements serialized as "object".
                    output.WriteObject(memberObject);
                }
                else
                {
                    // We can get here and still be NULL, exit gracefully.
                    if (writer == null)
                    {
                        return;
                    }

                    output.WriteObject(memberObject, writer);
                }
            }
            else
            {
                output.WriteSharedResource(memberObject);
            }
        }