Beispiel #1
0
        public ContentTypeReader[] LoadAssetReaders(ContentReader reader)
        {
            // Dummy variables required for it to work on iDevices ** DO NOT DELETE **
            // This forces the classes not to be optimized out when deploying to iDevices
            ListReader <Char>         hCharListReader      = new ListReader <Char>();
            ListReader <Rectangle>    hRectangleListReader = new ListReader <Rectangle>();
            ListReader <Vector3>      hVector3ListReader   = new ListReader <Vector3>();
            ListReader <StringReader> hStringListReader    = new ListReader <StringReader>();
            SpriteFontReader          hSpriteFontReader    = new SpriteFontReader();
            Texture2DReader           hTexture2DReader     = new Texture2DReader();
            CharReader      hCharReader      = new CharReader();
            RectangleReader hRectangleReader = new RectangleReader();
            StringReader    hStringReader    = new StringReader();
            Vector3Reader   hVector3Reader   = new Vector3Reader();
            CurveReader     hCurveReader     = new CurveReader();

            int numberOfReaders;

            ContentTypeReader[] contentReaders;


            // The first content byte i read tells me the number of content readers in this XNB file
            numberOfReaders = reader.ReadByte();
            contentReaders  = new ContentTypeReader[numberOfReaders];

            // For each reader in the file, we read out the length of the string which contains the type of the reader,
            // then we read out the string. Finally we instantiate an instance of that reader using reflection
            for (int i = 0; i < numberOfReaders; i++)
            {
                // This string tells us what reader we need to decode the following data
                // string readerTypeString = reader.ReadString();
                string originalReaderTypeString = reader.ReadString();

                // Need to resolve namespace differences
                string readerTypeString = originalReaderTypeString;

                readerTypeString = PrepareType(readerTypeString);

                Type l_readerType = Type.GetType(readerTypeString);

                if (l_readerType != null)
                {
                    contentReaders[i] = (ContentTypeReader)Activator.CreateInstance(l_readerType, true);
                }
                else
                {
                    throw new ContentLoadException("Could not find matching content reader of type " + originalReaderTypeString + " (" + readerTypeString + ")");
                }

                // I think the next 4 bytes refer to the "Version" of the type reader,
                // although it always seems to be zero
                int typeReaderVersion = reader.ReadInt32();
            }

            return(contentReaders);
        }
Beispiel #2
0
        internal ContentTypeReader[] LoadAssetReaders(ContentReader reader)
        {
#pragma warning disable 0219, 0649
            // Trick to prevent the linker removing the code, but not actually execute the code
            if (falseflag)
            {
                // Dummy variables required for it to work on iDevices ** DO NOT DELETE **
                // This forces the classes not to be optimized out when deploying to iDevices
                var hByteReader              = new ByteReader();
                var hSByteReader             = new SByteReader();
                var hDateTimeReader          = new DateTimeReader();
                var hDecimalReader           = new DecimalReader();
                var hBoundingSphereReader    = new BoundingSphereReader();
                var hBoundingFrustumReader   = new BoundingFrustumReader();
                var hRayReader               = new RayReader();
                var hCharListReader          = new ListReader <Char>();
                var hRectangleListReader     = new ListReader <Rectangle>();
                var hRectangleArrayReader    = new ArrayReader <Rectangle>();
                var hVector3ListReader       = new ListReader <Vector3>();
                var hStringListReader        = new ListReader <StringReader>();
                var hIntListReader           = new ListReader <Int32>();
                var hSpriteFontReader        = new SpriteFontReader();
                var hTexture2DReader         = new Texture2DReader();
                var hCharReader              = new CharReader();
                var hRectangleReader         = new RectangleReader();
                var hStringReader            = new StringReader();
                var hVector2Reader           = new Vector2Reader();
                var hVector3Reader           = new Vector3Reader();
                var hVector4Reader           = new Vector4Reader();
                var hCurveReader             = new CurveReader();
                var hIndexBufferReader       = new IndexBufferReader();
                var hBoundingBoxReader       = new BoundingBoxReader();
                var hMatrixReader            = new MatrixReader();
                var hBasicEffectReader       = new BasicEffectReader();
                var hVertexBufferReader      = new VertexBufferReader();
                var hAlphaTestEffectReader   = new AlphaTestEffectReader();
                var hEnumSpriteEffectsReader = new EnumReader <Graphics.SpriteEffects>();
                var hArrayFloatReader        = new ArrayReader <float>();
                var hArrayVector2Reader      = new ArrayReader <Vector2>();
                var hListVector2Reader       = new ListReader <Vector2>();
                var hArrayMatrixReader       = new ArrayReader <Matrix>();
                var hEnumBlendReader         = new EnumReader <Graphics.Blend>();
                var hNullableRectReader      = new NullableReader <Rectangle>();
                var hEffectMaterialReader    = new EffectMaterialReader();
                var hExternalReferenceReader = new ExternalReferenceReader();
                var hSoundEffectReader       = new SoundEffectReader();
                var hSongReader              = new SongReader();
                var hModelReader             = new ModelReader();
                var hInt32Reader             = new Int32Reader();
                var hEffectReader            = new EffectReader();
                var hSingleReader            = new SingleReader();

                // At the moment the Video class doesn't exist
                // on all platforms... Allow it to compile anyway.
#if ANDROID || (IOS && !TVOS) || MONOMAC || (WINDOWS && !OPENGL) || WINDOWS_UAP
                var hVideoReader = new VideoReader();
#endif
            }
#pragma warning restore 0219, 0649

            // The first content byte i read tells me the number of content readers in this XNB file
            var numberOfReaders = reader.Read7BitEncodedInt();
            var contentReaders  = new ContentTypeReader[numberOfReaders];
            var needsInitialize = new BitArray(numberOfReaders);
            _contentReaders = new Dictionary <Type, ContentTypeReader>(numberOfReaders);

            // Lock until we're done allocating and initializing any new
            // content type readers...  this ensures we can load content
            // from multiple threads and still cache the readers.
            lock (_locker)
            {
                // For each reader in the file, we read out the length of the string which contains the type of the reader,
                // then we read out the string. Finally we instantiate an instance of that reader using reflection
                for (var i = 0; i < numberOfReaders; i++)
                {
                    // This string tells us what reader we need to decode the following data
                    // string readerTypeString = reader.ReadString();
                    string originalReaderTypeString = reader.ReadString();

                    Func <ContentTypeReader> readerFunc;
                    if (typeCreators.TryGetValue(originalReaderTypeString, out readerFunc))
                    {
                        contentReaders[i]  = readerFunc();
                        needsInitialize[i] = true;
                    }
                    else
                    {
                        //System.Diagnostics.Debug.WriteLine(originalReaderTypeString);

                        // Need to resolve namespace differences
                        string readerTypeString = originalReaderTypeString;

                        readerTypeString = PrepareType(readerTypeString);

                        var l_readerType = Type.GetType(readerTypeString);
                        if (l_readerType != null)
                        {
                            ContentTypeReader typeReader;
                            if (!_contentReadersCache.TryGetValue(l_readerType, out typeReader))
                            {
                                try
                                {
                                    typeReader = l_readerType.GetDefaultConstructor().Invoke(null) as ContentTypeReader;
                                }
                                catch (TargetInvocationException ex)
                                {
                                    // If you are getting here, the Mono runtime is most likely not able to JIT the type.
                                    // In particular, MonoTouch needs help instantiating types that are only defined in strings in Xnb files.
                                    throw new InvalidOperationException(
                                              "Failed to get default constructor for ContentTypeReader. To work around, add a creation function to ContentTypeReaderManager.AddTypeCreator() " +
                                              "with the following failed type string: " + originalReaderTypeString, ex);
                                }

                                needsInitialize[i] = true;

                                _contentReadersCache.Add(l_readerType, typeReader);
                            }

                            contentReaders[i] = typeReader;
                        }
                        else
                        {
                            throw new ContentLoadException(
                                      "Could not find ContentTypeReader Type. Please ensure the name of the Assembly that contains the Type matches the assembly in the full type name: " +
                                      originalReaderTypeString + " (" + readerTypeString + ")");
                        }
                    }

                    var targetType = contentReaders[i].TargetType;
                    if (targetType != null)
                    {
                        if (!_contentReaders.ContainsKey(targetType))
                        {
                            _contentReaders.Add(targetType, contentReaders[i]);
                        }
                    }

                    // I think the next 4 bytes refer to the "Version" of the type reader,
                    // although it always seems to be zero
                    reader.ReadInt32();
                }

                // Initialize any new readers.
                for (var i = 0; i < contentReaders.Length; i++)
                {
                    if (needsInitialize.Get(i))
                    {
                        contentReaders[i].Initialize(this);
                    }
                }
            } // lock (_locker)

            return(contentReaders);
        }
Beispiel #3
0
        internal ContentTypeReader[] LoadAssetReaders(ContentReader reader)
        {
#pragma warning disable 0219, 0649

            /* Trick to prevent the linker removing the code, but not actually execute the code
             * FIXME: Do we really need this in FNA?
             */
            if (falseflag)
            {
                /* Dummy variables required for it to work on iDevices ** DO NOT DELETE **
                 * This forces the classes not to be optimized out when deploying to iDevices
                 */
                ByteReader                hByteReader            = new ByteReader();
                SByteReader               hSByteReader           = new SByteReader();
                DateTimeReader            hDateTimeReader        = new DateTimeReader();
                DecimalReader             hDecimalReader         = new DecimalReader();
                BoundingSphereReader      hBoundingSphereReader  = new BoundingSphereReader();
                BoundingFrustumReader     hBoundingFrustumReader = new BoundingFrustumReader();
                RayReader                 hRayReader             = new RayReader();
                ListReader <char>         hCharListReader        = new ListReader <Char>();
                ListReader <Rectangle>    hRectangleListReader   = new ListReader <Rectangle>();
                ArrayReader <Rectangle>   hRectangleArrayReader  = new ArrayReader <Rectangle>();
                ListReader <Vector3>      hVector3ListReader     = new ListReader <Vector3>();
                ListReader <StringReader> hStringListReader      = new ListReader <StringReader>();
                ListReader <int>          hIntListReader         = new ListReader <Int32>();
                SpriteFontReader          hSpriteFontReader      = new SpriteFontReader();
                Texture2DReader           hTexture2DReader       = new Texture2DReader();
                CharReader                hCharReader            = new CharReader();
                RectangleReader           hRectangleReader       = new RectangleReader();
                StringReader              hStringReader          = new StringReader();
                Vector2Reader             hVector2Reader         = new Vector2Reader();
                Vector3Reader             hVector3Reader         = new Vector3Reader();
                Vector4Reader             hVector4Reader         = new Vector4Reader();
                CurveReader               hCurveReader           = new CurveReader();
                IndexBufferReader         hIndexBufferReader     = new IndexBufferReader();
                BoundingBoxReader         hBoundingBoxReader     = new BoundingBoxReader();
                MatrixReader              hMatrixReader          = new MatrixReader();
                BasicEffectReader         hBasicEffectReader     = new BasicEffectReader();
                VertexBufferReader        hVertexBufferReader    = new VertexBufferReader();
                AlphaTestEffectReader     hAlphaTestEffectReader = new AlphaTestEffectReader();
                EnumReader <Microsoft.Xna.Framework.Graphics.SpriteEffects> hEnumSpriteEffectsReader = new EnumReader <Graphics.SpriteEffects>();
                ArrayReader <float>   hArrayFloatReader   = new ArrayReader <float>();
                ArrayReader <Vector2> hArrayVector2Reader = new ArrayReader <Vector2>();
                ListReader <Vector2>  hListVector2Reader  = new ListReader <Vector2>();
                ArrayReader <Matrix>  hArrayMatrixReader  = new ArrayReader <Matrix>();
                EnumReader <Microsoft.Xna.Framework.Graphics.Blend> hEnumBlendReader = new EnumReader <Graphics.Blend>();
                NullableReader <Rectangle> hNullableRectReader      = new NullableReader <Rectangle>();
                EffectMaterialReader       hEffectMaterialReader    = new EffectMaterialReader();
                ExternalReferenceReader    hExternalReferenceReader = new ExternalReferenceReader();
                SoundEffectReader          hSoundEffectReader       = new SoundEffectReader();
                SongReader  hSongReader  = new SongReader();
                ModelReader hModelReader = new ModelReader();
                Int32Reader hInt32Reader = new Int32Reader();
            }
#pragma warning restore 0219, 0649

            /* The first content byte i read tells me the number of
             * content readers in this XNB file.
             */
            int numberOfReaders                 = reader.Read7BitEncodedInt();
            ContentTypeReader[] newReaders      = new ContentTypeReader[numberOfReaders];
            BitArray            needsInitialize = new BitArray(numberOfReaders);
            contentReaders = new Dictionary <Type, ContentTypeReader>(numberOfReaders);

            /* Lock until we're done allocating and initializing any new
             * content type readers... this ensures we can load content
             * from multiple threads and still cache the readers.
             */
            lock (locker)
            {
                /* For each reader in the file, we read out the
                 * length of the string which contains the type
                 * of the reader, then we read out the string.
                 * Finally we instantiate an instance of that
                 * reader using reflection.
                 */
                for (int i = 0; i < numberOfReaders; i += 1)
                {
                    /* This string tells us what reader we
                     * need to decode the following data.
                     */
                    string originalReaderTypeString = reader.ReadString();

                    Func <ContentTypeReader> readerFunc;
                    if (typeCreators.TryGetValue(originalReaderTypeString, out readerFunc))
                    {
                        newReaders[i]      = readerFunc();
                        needsInitialize[i] = true;
                    }
                    else
                    {
                        // Need to resolve namespace differences
                        string readerTypeString = originalReaderTypeString;
                        readerTypeString = PrepareType(readerTypeString);

                        Type l_readerType = Type.GetType(readerTypeString);
                        if (l_readerType != null)
                        {
                            ContentTypeReader typeReader;
                            if (!contentReadersCache.TryGetValue(l_readerType, out typeReader))
                            {
                                try
                                {
                                    typeReader = l_readerType.GetDefaultConstructor().Invoke(null) as ContentTypeReader;
                                }
                                catch (TargetInvocationException ex)
                                {
                                    /* If you are getting here, the Mono runtime
                                     * is most likely not able to JIT the type.
                                     * In particular, MonoTouch needs help
                                     * instantiating types that are only defined
                                     * in strings in Xnb files.
                                     */
                                    throw new InvalidOperationException(
                                              "Failed to get default constructor for ContentTypeReader. " +
                                              "To work around, add a creation function to ContentTypeReaderManager.AddTypeCreator() " +
                                              "with the following failed type string: " + originalReaderTypeString,
                                              ex
                                              );
                                }

                                needsInitialize[i] = true;

                                contentReadersCache.Add(l_readerType, typeReader);
                            }

                            newReaders[i] = typeReader;
                        }
                        else
                        {
                            throw new ContentLoadException(
                                      "Could not find ContentTypeReader Type. " +
                                      "Please ensure the name of the Assembly that " +
                                      "contains the Type matches the assembly in the full type name: " +
                                      originalReaderTypeString + " (" + readerTypeString + ")"
                                      );
                        }
                    }

                    contentReaders.Add(newReaders[i].TargetType, newReaders[i]);

                    /* I think the next 4 bytes refer to the "Version" of the type reader,
                     * although it always seems to be zero.
                     */
                    reader.ReadInt32();
                }

                // Initialize any new readers.
                for (int i = 0; i < newReaders.Length; i += 1)
                {
                    if (needsInitialize.Get(i))
                    {
                        newReaders[i].Initialize(this);
                    }
                }
            }             // lock (locker)

            return(newReaders);
        }
 internal ContentTypeReader[] LoadAssetReaders()
 {
   if (ContentTypeReaderManager.falseflag)
   {
     ByteReader byteReader = new ByteReader();
     SByteReader sbyteReader = new SByteReader();
     DateTimeReader dateTimeReader = new DateTimeReader();
     DecimalReader decimalReader = new DecimalReader();
     BoundingSphereReader boundingSphereReader = new BoundingSphereReader();
     BoundingFrustumReader boundingFrustumReader = new BoundingFrustumReader();
     RayReader rayReader = new RayReader();
     ListReader<char> listReader1 = new ListReader<char>();
     ListReader<Rectangle> listReader2 = new ListReader<Rectangle>();
     ArrayReader<Rectangle> arrayReader1 = new ArrayReader<Rectangle>();
     ListReader<Vector3> listReader3 = new ListReader<Vector3>();
     ListReader<StringReader> listReader4 = new ListReader<StringReader>();
     ListReader<int> listReader5 = new ListReader<int>();
     SpriteFontReader spriteFontReader = new SpriteFontReader();
     Texture2DReader texture2Dreader = new Texture2DReader();
     CharReader charReader = new CharReader();
     RectangleReader rectangleReader = new RectangleReader();
     StringReader stringReader = new StringReader();
     Vector2Reader vector2Reader = new Vector2Reader();
     Vector3Reader vector3Reader = new Vector3Reader();
     Vector4Reader vector4Reader = new Vector4Reader();
     CurveReader curveReader = new CurveReader();
     IndexBufferReader indexBufferReader = new IndexBufferReader();
     BoundingBoxReader boundingBoxReader = new BoundingBoxReader();
     MatrixReader matrixReader = new MatrixReader();
     BasicEffectReader basicEffectReader = new BasicEffectReader();
     VertexBufferReader vertexBufferReader = new VertexBufferReader();
     AlphaTestEffectReader testEffectReader = new AlphaTestEffectReader();
     EnumReader<SpriteEffects> enumReader1 = new EnumReader<SpriteEffects>();
     ArrayReader<float> arrayReader2 = new ArrayReader<float>();
     ArrayReader<Vector2> arrayReader3 = new ArrayReader<Vector2>();
     ListReader<Vector2> listReader6 = new ListReader<Vector2>();
     ArrayReader<Matrix> arrayReader4 = new ArrayReader<Matrix>();
     EnumReader<Blend> enumReader2 = new EnumReader<Blend>();
     NullableReader<Rectangle> nullableReader = new NullableReader<Rectangle>();
     EffectMaterialReader effectMaterialReader = new EffectMaterialReader();
     ExternalReferenceReader externalReferenceReader = new ExternalReferenceReader();
   }
   int length = this._reader.Read7BitEncodedInt();
   this.contentReaders = new ContentTypeReader[length];
   for (int index = 0; index < length; ++index)
   {
     string str = this._reader.ReadString();
     Func<ContentTypeReader> func;
     if (ContentTypeReaderManager.typeCreators.TryGetValue(str, out func))
     {
       this.contentReaders[index] = func();
     }
     else
     {
       string typeName = ContentTypeReaderManager.PrepareType(str);
       Type type = Type.GetType(typeName);
       if (type != (Type) null)
       {
         try
         {
           this.contentReaders[index] = ContentExtensions.GetDefaultConstructor(type).Invoke((object[]) null) as ContentTypeReader;
         }
         catch (TargetInvocationException ex)
         {
           throw new InvalidOperationException("Failed to get default constructor for ContentTypeReader. To work around, add a creation function to ContentTypeReaderManager.AddTypeCreator() with the following failed type string: " + str);
         }
       }
       else
         throw new ContentLoadException("Could not find matching content reader of type " + str + " (" + typeName + ")");
     }
     this._reader.ReadInt32();
   }
   return this.contentReaders;
 }
        public ContentTypeReader[] LoadAssetReaders(ContentReader reader)
        {
            // Dummy variables required for it to work on iDevices ** DO NOT DELETE **
            // This forces the classes not to be optimized out when deploying to iDevices
            ListReader <Char>         hCharListReader      = new ListReader <Char>();
            ListReader <Rectangle>    hRectangleListReader = new ListReader <Rectangle>();
            ListReader <Vector3>      hVector3ListReader   = new ListReader <Vector3>();
            ListReader <StringReader> hStringListReader    = new ListReader <StringReader>();
            SpriteFontReader          hSpriteFontReader    = new SpriteFontReader();
            Texture2DReader           hTexture2DReader     = new Texture2DReader();
            CharReader      hCharReader      = new CharReader();
            RectangleReader hRectangleReader = new RectangleReader();
            StringReader    hStringReader    = new StringReader();
            Vector3Reader   hVector3Reader   = new Vector3Reader();
            int             numberOfReaders;

            ContentTypeReader[] contentReaders;

            // The first 4 bytes should be the "XNBw" header. i use that to detect an invalid file
            byte[] headerBuffer = new byte[4];
            reader.Read(headerBuffer, 0, 4);

            string headerString = Encoding.UTF8.GetString(headerBuffer, 0, 4);

            if (string.Compare(headerString, "XNBw", StringComparison.InvariantCultureIgnoreCase) != 0 &&
                string.Compare(headerString, "XNBx", StringComparison.InvariantCultureIgnoreCase) != 0 &&
                string.Compare(headerString, "XNBm", StringComparison.InvariantCultureIgnoreCase) != 0)
            {
                throw new ContentLoadException("Asset does not appear to be a valid XNB file. Did you process your content for Windows?");
            }

            // I think these two bytes are some kind of version number. Either for the XNB file or the type readers
            byte version = reader.ReadByte();

            if (version != 5)
            {
                throw new ContentLoadException("Invalid XNB file version.");
            }

            byte compressed = reader.ReadByte();
            // The next int32 is the length of the XNB file
            int xnbLength = reader.ReadInt32();

            if (compressed != 0)
            {
                throw new NotImplementedException("MonoGame cannot read compressed XNB files. Please use the XNB files from the Debug build of your XNA game instead. If someone wants to contribute decompression logic, that would be fantastic.");
            }

            // The next byte i read tells me the number of content readers in this XNB file
            numberOfReaders = reader.ReadByte();
            contentReaders  = new ContentTypeReader[numberOfReaders];

            // For each reader in the file, we read out the length of the string which contains the type of the reader,
            // then we read out the string. Finally we instantiate an instance of that reader using reflection
            for (int i = 0; i < numberOfReaders; i++)
            {
                // This string tells us what reader we need to decode the following data
                // string readerTypeString = reader.ReadString();
                string originalReaderTypeString = reader.ReadString();

                // Need to resolve namespace differences
                string readerTypeString = originalReaderTypeString;

                readerTypeString = PrepareType(readerTypeString);

                Type l_readerType = Type.GetType(readerTypeString);

                if (l_readerType != null)
                {
                    contentReaders[i] = (ContentTypeReader)Activator.CreateInstance(l_readerType, true);
                }
                else
                {
                    throw new ContentLoadException("Could not find matching content reader of type " + originalReaderTypeString + " (" + readerTypeString + ")");
                }

                // I think the next 4 bytes refer to the "Version" of the type reader,
                // although it always seems to be zero
                int typeReaderVersion = reader.ReadInt32();
            }

            return(contentReaders);
        }
        internal ContentTypeReader[] LoadAssetReaders()
        {
#pragma warning disable 0219, 0649
            // Trick to prevent the linker removing the code, but not actually execute the code
            if (falseflag)
            {
                // Dummy variables required for it to work on iDevices ** DO NOT DELETE **
                // This forces the classes not to be optimized out when deploying to iDevices
                var hByteReader              = new ByteReader();
                var hSByteReader             = new SByteReader();
                var hDateTimeReader          = new DateTimeReader();
                var hDecimalReader           = new DecimalReader();
                var hBoundingSphereReader    = new BoundingSphereReader();
                var hBoundingFrustumReader   = new BoundingFrustumReader();
                var hRayReader               = new RayReader();
                var hCharListReader          = new ListReader <Char>();
                var hRectangleListReader     = new ListReader <Rectangle>();
                var hRectangleArrayReader    = new ArrayReader <Rectangle>();
                var hVector3ListReader       = new ListReader <Vector3>();
                var hStringListReader        = new ListReader <StringReader>();
                var hIntListReader           = new ListReader <Int32>();
                var hSpriteFontReader        = new SpriteFontReader();
                var hTexture2DReader         = new Texture2DReader();
                var hCharReader              = new CharReader();
                var hRectangleReader         = new RectangleReader();
                var hStringReader            = new StringReader();
                var hVector2Reader           = new Vector2Reader();
                var hVector3Reader           = new Vector3Reader();
                var hVector4Reader           = new Vector4Reader();
                var hCurveReader             = new CurveReader();
                var hIndexBufferReader       = new IndexBufferReader();
                var hBoundingBoxReader       = new BoundingBoxReader();
                var hMatrixReader            = new MatrixReader();
                var hBasicEffectReader       = new BasicEffectReader();
                var hVertexBufferReader      = new VertexBufferReader();
                var hAlphaTestEffectReader   = new AlphaTestEffectReader();
                var hEnumSpriteEffectsReader = new EnumReader <Graphics.SpriteEffects>();
                var hArrayFloatReader        = new ArrayReader <float>();
                var hArrayVector2Reader      = new ArrayReader <Vector2>();
                var hListVector2Reader       = new ListReader <Vector2>();
                var hArrayMatrixReader       = new ArrayReader <Matrix>();
                var hEnumBlendReader         = new EnumReader <Graphics.Blend>();
                var hNullableRectReader      = new NullableReader <Rectangle>();
                var hEffectMaterialReader    = new EffectMaterialReader();
                var hExternalReferenceReader = new ExternalReferenceReader();
                var hSoundEffectReader       = new SoundEffectReader();
                var hSongReader              = new SongReader();
            }
#pragma warning restore 0219, 0649

            int numberOfReaders;

            // The first content byte i read tells me the number of content readers in this XNB file
            numberOfReaders = _reader.Read7BitEncodedInt();
            contentReaders  = new ContentTypeReader[numberOfReaders];

            // For each reader in the file, we read out the length of the string which contains the type of the reader,
            // then we read out the string. Finally we instantiate an instance of that reader using reflection
            for (int i = 0; i < numberOfReaders; i++)
            {
                // This string tells us what reader we need to decode the following data
                // string readerTypeString = reader.ReadString();
                string originalReaderTypeString = _reader.ReadString();

                Func <ContentTypeReader> readerFunc;
                if (typeCreators.TryGetValue(originalReaderTypeString, out readerFunc))
                {
                    contentReaders[i] = readerFunc();
                }
                else
                {
                    //System.Diagnostics.Debug.WriteLine(originalReaderTypeString);

                    // Need to resolve namespace differences
                    string readerTypeString = originalReaderTypeString;

                    readerTypeString = PrepareType(readerTypeString);

                    var l_readerType = Type.GetType(readerTypeString);
                    if (l_readerType != null)
                    {
                        try
                        {
                            contentReaders[i] = l_readerType.GetDefaultConstructor().Invoke(null) as ContentTypeReader;
                        }
                        catch (TargetInvocationException ex)
                        {
                            // If you are getting here, the Mono runtime is most likely not able to JIT the type.
                            // In particular, MonoTouch needs help instantiating types that are only defined in strings in Xnb files.
                            throw new InvalidOperationException(
                                      "Failed to get default constructor for ContentTypeReader. To work around, add a creation function to ContentTypeReaderManager.AddTypeCreator() " +
                                      "with the following failed type string: " + originalReaderTypeString);
                        }
                    }
                    else
                    {
                        throw new ContentLoadException(
                                  "Could not find ContentTypeReader Type. Please ensure the name of the Assembly that contains the Type matches the assembly in the full type name: " +
                                  originalReaderTypeString + " (" + readerTypeString + ")");
                    }
                }

                // I think the next 4 bytes refer to the "Version" of the type reader,
                // although it always seems to be zero
                int typeReaderVersion = _reader.ReadInt32();
            }

            return(contentReaders);
        }
        internal ContentTypeReader[] LoadAssetReaders()
        {
#pragma warning disable 0219, 0649
            // Trick to prevent the linker removing the code, but not actually execute the code
            if (falseflag)
            {
                // Dummy variables required for it to work on iDevices ** DO NOT DELETE **
                // This forces the classes not to be optimized out when deploying to iDevices
                var hByteReader            = new ByteReader();
                var hSByteReader           = new SByteReader();
                var hDateTimeReader        = new DateTimeReader();
                var hDecimalReader         = new DecimalReader();
                var hBoundingSphereReader  = new BoundingSphereReader();
                var hBoundingFrustumReader = new BoundingFrustumReader();
                var hRayReader             = new RayReader();
                var hCharListReader        = new ListReader <Char>();
                var hRectangleListReader   = new ListReader <Rectangle>();
                var hVector3ListReader     = new ListReader <Vector3>();
                var hStringListReader      = new ListReader <StringReader>();
                var hSpriteFontReader      = new SpriteFontReader();
                var hTexture2DReader       = new Texture2DReader();
                var hCharReader            = new CharReader();
                var hRectangleReader       = new RectangleReader();
                var hStringReader          = new StringReader();
                var hVector2Reader         = new Vector2Reader();
                var hVector3Reader         = new Vector3Reader();
                var hVector4Reader         = new Vector4Reader();
                var hCurveReader           = new CurveReader();
                var hIndexBufferReader     = new IndexBufferReader();
                var hBoundingBoxReader     = new BoundingBoxReader();
                var hMatrixReader          = new MatrixReader();
                var hBasicEffectReader     = new BasicEffectReader();
                var hVertexBufferReader    = new VertexBufferReader();
                var hAlphaTestEffectReader = new AlphaTestEffectReader();
            }
#pragma warning restore 0219, 0649

            int numberOfReaders;

            // The first content byte i read tells me the number of content readers in this XNB file
            numberOfReaders = _reader.Read7BitEncodedInt();
            contentReaders  = new ContentTypeReader[numberOfReaders];

            // For each reader in the file, we read out the length of the string which contains the type of the reader,
            // then we read out the string. Finally we instantiate an instance of that reader using reflection
            for (int i = 0; i < numberOfReaders; i++)
            {
                // This string tells us what reader we need to decode the following data
                // string readerTypeString = reader.ReadString();
                string originalReaderTypeString = _reader.ReadString();

                // Need to resolve namespace differences
                string readerTypeString = originalReaderTypeString;

                readerTypeString = PrepareType(readerTypeString);

                Type l_readerType = Type.GetType(readerTypeString);

                if (l_readerType != null)
                {
                    contentReaders[i] = (ContentTypeReader)Activator.CreateInstance(l_readerType, true);
                }
                else
                {
                    throw new ContentLoadException("Could not find matching content reader of type " + originalReaderTypeString + " (" + readerTypeString + ")");
                }

                // I think the next 4 bytes refer to the "Version" of the type reader,
                // although it always seems to be zero
                int typeReaderVersion = _reader.ReadInt32();
            }

            return(contentReaders);
        }
Beispiel #8
0
        public ContentTypeReader[] LoadAssetReaders(ContentReader reader)
        {
            // Dummy variables required for it to work on iDevices ** DO NOT DELETE **
            // This forces the classes not to be optimized out when deploying to iDevices
            ListReader <Char>         hCharListReader      = new ListReader <Char>();
            ListReader <Rectangle>    hRectangleListReader = new ListReader <Rectangle>();
            ListReader <Vector3>      hVector3ListReader   = new ListReader <Vector3>();
            ListReader <StringReader> hStringListReader    = new ListReader <StringReader>();
            SpriteFontReader          hSpriteFontReader    = new SpriteFontReader();
            Texture2DReader           hTexture2DReader     = new Texture2DReader();
            CharReader      hCharReader      = new CharReader();
            RectangleReader hRectangleReader = new RectangleReader();
            StringReader    hStringReader    = new StringReader();
            Vector3Reader   hVector3Reader   = new Vector3Reader();

            int numberOfReaders;

            ContentTypeReader[] contentReaders;

            // The first 4 bytes should be the "XNBw" header. i use that to detect an invalid file
            byte[] headerBuffer = new byte[4];
            reader.Read(headerBuffer, 0, 4);
            string headerString = Encoding.UTF8.GetString(headerBuffer, 0, 4);

            if (string.Compare(headerString, "XNBw", StringComparison.InvariantCultureIgnoreCase) != 0)
            {
                throw new ContentLoadException("Asset does not appear to be a valid XNB file.  Did you process your content for Windows?");
            }

            // I think these two bytes are some kind of version number. Either for the XNB file or the type readers
            byte version    = reader.ReadByte();
            byte compressed = reader.ReadByte();
            // The next int32 is the length of the XNB file
            int xnbLength = reader.ReadInt32();

            if (compressed != 0)
            {
                throw new NotImplementedException("MonoGame cannot read compressed XNB files. Please use the XNB files from the Debug build of your XNA game instead. If someone wants to contribute decompression logic, that would be fantastic.");
            }

            // The next byte i read tells me the number of content readers in this XNB file
            numberOfReaders = reader.ReadByte();
            contentReaders  = new ContentTypeReader[numberOfReaders];

            // For each reader in the file, we read out the length of the string which contains the type of the reader,
            // then we read out the string. Finally we instantiate an instance of that reader using reflection
            for (int i = 0; i < numberOfReaders; i++)
            {
                // This string tells us what reader we need to decode the following data
                // string readerTypeString = reader.ReadString();
                string originalReaderTypeString = reader.ReadString();

                // Need to resolve namespace differences
                string readerTypeString = originalReaderTypeString;

                /*if(readerTypeString.IndexOf(", Microsoft.Xna.Framework") != -1)
                 * {
                 *      string[] tokens = readerTypeString.Split(new char[] { ',' });
                 *      readerTypeString = "";
                 *      for(int j = 0; j < tokens.Length; j++)
                 *      {
                 *              if(j != 0)
                 *                      readerTypeString += ",";
                 *
                 *              if(j == 1)
                 *                      readerTypeString += " Microsoft.Xna.Framework";
                 *              else
                 *                      readerTypeString += tokens[j];
                 *      }
                 *      readerTypeString = readerTypeString.Replace(", Microsoft.Xna.Framework", "@");
                 * }*/

                if (readerTypeString.Contains("PublicKey"))
                {
                    //if (readerTypeString.Contains("[[")) {
                    readerTypeString = readerTypeString.Split(new char[] { '[', '[' })[0] + "[" +
                                       readerTypeString.Split(new char[] { '[', '[' })[2].Split(',')[0] + "]";
                    //}
                    //else {
                    //	// If the readerTypeString did not contain "[[" to split the
                    //	// types then we assume it is XNA 4.0 which splits the types
                    //	// by ', '
                    //	readerTypeString = readerTypeString.Split(new char[] { ',', ' '})[0];
                    //
                    //}
                }

                readerTypeString = readerTypeString.Replace("Microsoft.Xna.Framework", "Microsoft.Xna.Framework");
                Type l_readerType = Type.GetType(readerTypeString);

                if (l_readerType != null)
                {
                    contentReaders[i] = (ContentTypeReader)Activator.CreateInstance(l_readerType, true);
                }
                else
                {
                    throw new ContentLoadException("Could not find matching content reader of type " + originalReaderTypeString);
                }



                // I think the next 4 bytes refer to the "Version" of the type reader,
                // although it always seems to be zero
                int typeReaderVersion = reader.ReadInt32();
            }

            return(contentReaders);
        }
        private ContentTypeReader GetReaderByTypeName(string originalReaderTypeString, out bool needsInitialize)
        {
            string            readerTypeString = PrepareType(originalReaderTypeString);
            ContentTypeReader typeReader;

            if (contentReaders.TryGetValue(readerTypeString, out typeReader))
            {
                needsInitialize = true;
                return(typeReader);
            }
            else
            {
                int length = originalReaderTypeString.IndexOf(',');
                if (length == -1)
                {
                    length = originalReaderTypeString.Length;
                }
                string readerTypeName = originalReaderTypeString.Substring(0, length);
                // we need to hardcode typereaders because reflection doesn't work on consoles
                switch (readerTypeName)
                {
                case "System.Char":
                case "Microsoft.Xna.Framework.Content.CharReader":
                    typeReader = new CharReader();
                    break;

                case "Microsoft.Xna.Framework.Vector3":
                case "Microsoft.Xna.Framework.Content.Vector3Reader":
                    typeReader = new Vector3Reader();
                    break;

                case "Microsoft.Xna.Framework.Rectangle":
                case "Microsoft.Xna.Framework.Content.RectangleReader":
                    typeReader = new RectangleReader();
                    break;

                case "Microsoft.Xna.Framework.Content.Texture2DReader":
                    typeReader = new Texture2DReader();
                    break;

                case "Microsoft.Xna.Framework.Content.SpriteFontReader":
                    typeReader = new SpriteFontReader();
                    break;

                case "Microsoft.Xna.Framework.Content.ListReader`1[[Microsoft.Xna.Framework.Rectangle":
                    typeReader = new ListReader <Rectangle>();
                    break;

                case "Microsoft.Xna.Framework.Content.ListReader`1[[System.Char":
                    typeReader = new ListReader <Char>();
                    break;

                case "Microsoft.Xna.Framework.Content.ListReader`1[[Microsoft.Xna.Framework.Vector3":
                    typeReader = new ListReader <Vector3>();
                    break;

                case "TextureAtlasContent.TextureAtlasReader":
                    typeReader = new TextureAtlasReader();
                    break;

                case "TextureAtlasContent.TextureRegionReader":
                    typeReader = new TextureRegionReader();
                    break;

                default:
                    throw new ContentLoadException(
                              "Could not find ContentTypeReader Type. " +
                              "Please ensure the name of the Assembly that " +
                              "contains the Type matches the assembly in the full type name: " +
                              originalReaderTypeString + " (" + readerTypeString + ")");
                }

                typeReader.Initialize(this);
                contentReaders.Add(readerTypeString, typeReader);
                needsInitialize = false;
                return(typeReader);
            }
        }