Esempio n. 1
0
        public static object Deserialize(Stream stream, FormatCompression compression)
        {
            BinaryReader reader = new BinaryReader(stream);

            switch (compression)
            {
            case FormatCompression.Binary:
                //int defaultTimeout = stream.ReadTimeout;
                //stream.ReadTimeout = 10; //Like really, how long does the computer need to read a 4 byte signed integer? Change if needed
                byte[] packet = reader.ReadBytes(reader.ReadInt32());
                object obj    = BinaryConstructor.Construct(packet);
                return(obj);

            case FormatCompression.String:
                string objGraphData = reader.ReadString();
                return(Constructor.Construct(objGraphData));

            default:
                throw new SerializationException("Please choose a format compression");
            }
        }
Esempio n. 2
0
        public static void Serialize(Stream stream, object obj, FormatCompression compression)
        {
            BinaryWriter writer = new BinaryWriter(stream);

            switch (compression)
            {
            case FormatCompression.Binary:
                byte[] packet = BinaryDeconstructor.Deconstruct(obj);
                writer.Write(packet.Length);
                writer.Write(packet);
                break;

            case FormatCompression.String:
                writer.Write(Deconstructor.Deconstruct(obj));
                break;

            default:
                throw new SerializationException("Please choose a format compression");
            }
            writer.Flush();
        }
Esempio n. 3
0
        /// <summary>Construct a <see cref="Format"/>.</summary>
        /// <param name="elementType">The type of an element.</param>
        /// <param name="systemType">The type of the combined value, or <c>null</c> if there is none. If <paramref name="columns"/> is 1 and <paramref name="rows"/> is one (a scalar), and <paramref name="systemType"/> is <c>null</c>, then it will be assigned the value of <paramref name="elementType"/>. The default is <c>null</c>.</param>
        /// <param name="columns">The number of columns. This is 1 for scalars (with a <paramref name="rows"/> of 1) and vectors (with a <paramref name="rows"/> greater than 1), more than 1 for matrices. The default is <c>1</c>.</param>
        /// <param name="rows">The number of rows. This is 1 (with a <paramref name="column"/> of 1) for scalars, and greater for matrices. The default is <c>1</c>.</param>
        /// <param name="isSrgb">Get whether this is an sRgb color; <paramref name="columns"/> must be 1, and <paramref name="rows"/> must be 3 or 4. The default is <c>false</c>.</param>
        /// <param name="isReversed">Whether the components are reversed.</param>
        /// <param name="compression">What type of compression is in use. In this case <paramref name="elementType"/> and <paramref name="systemType"/> should be <c>null</c>.</param>
        internal Format(Type elementType, Type systemType = null, int columns = 1, int rows = 1, bool isSrgb = false, bool isReversed = false, FormatCompression compression = FormatCompression.None)
        {
            if (elementType == null && compression == FormatCompression.None)
            {
                throw new ArgumentNullException("elementType");
            }
            if (systemType == null && columns == 1 && rows == 1)
            {
                systemType = elementType;
            }

            ElementType = elementType;
            SystemType  = systemType;
            Columns     = columns;
            Rows        = rows;

            Compression      = compression;
            IsInteger        = !IsCompressed && IsIntegerSet.Contains(ElementType);
            IsUnsigned       = IsCompressed || IsUnsignedSet.Contains(ElementType);
            IsNormalized     = IsCompressed || IsNormalizedSet.Contains(ElementType);
            IsReversed       = isReversed;
            BitsPerComponent = IsCompressed ? 0 : Marshal.SizeOf(elementType) * 8;

            // Assign GL properties.
            if (!IsCompressed)
            {
                ActiveAttribType        = (GL.ActiveAttribType)GetActiveAttribType(elementType, columns, rows);
                PixelFormat             = (GL.PixelFormat?)GetPixelFormat(columns, rows, IsInteger, isReversed);
                PixelInternalFormat     = (GL.PixelInternalFormat?)GetPixelInternalFormat(columns, rows, elementType);
                PixelType               = (GL.PixelType?)GetPixelType(elementType);
                VertexAttribPointerType = (GL.VertexAttribPointerType?)GetVertexAttribPointerType(elementType);
            }
            else
            {
                switch (Compression)
                {
                case FormatCompression.DXT1:
                    PixelInternalFormat = GL.PixelInternalFormat.CompressedRgbS3tcDxt1Ext;
                    break;

                case FormatCompression.DXT3:
                    PixelInternalFormat = GL.PixelInternalFormat.CompressedRgbaS3tcDxt3Ext;
                    break;

                case FormatCompression.DXT5:
                    PixelInternalFormat = GL.PixelInternalFormat.CompressedRgbaS3tcDxt5Ext;
                    break;

                default:
                    throw new NotImplementedException();
                }
            }

            if (ActiveAttribType != GL.ActiveAttribType.None)
            {
                ByActiveAttributeType[ActiveAttribType] = this;
            }

            Channels = GetFormatChannels();

            if (systemType != null)
            {
                Format conflict;
                if (systemTypes.TryGetValue(systemType, out conflict))
                {
                    throw new ArgumentException(string.Format("The format elementType={0} systemType={1} size={2}x{3} conflicts with the format elementType={4} size={5}x{6} which has the same systemType.", elementType, systemType, Columns, Rows, conflict.ElementType, conflict.Columns, conflict.Rows), "systemType");
                }
                systemTypes.Add(systemType, this);
            }
        }