예제 #1
0
        protected T ReadAsset <T>(string assetName, Action <IDisposable> recordDisposableObject)
        {
            if (this.disposed)
            {
                throw new ObjectDisposedException(base.ToString());
            }

            if (string.IsNullOrEmpty(assetName))
            {
                throw new ArgumentNullException("assetName");
            }

            using (Stream assetStream = this.OpenStream(assetName))
            {
                using (ContentReader reader = new ContentReader(this, assetStream, assetName, recordDisposableObject))
                {
                    return(reader.ReadAsset <T>());
                }
            }
        }
예제 #2
0
        protected T ReadAsset <T>(string assetName, Action <IDisposable> recordDisposableObject)
        {
            if (string.IsNullOrEmpty(assetName))
            {
                throw new ArgumentNullException("assetName");
            }
            if (disposed)
            {
                throw new ObjectDisposedException("ContentManager");
            }

            object result            = null;
            Stream stream            = null;
            string modifiedAssetName = String.Empty;             // Will be used if we have to guess a filename

            try
            {
                stream = OpenStream(assetName);
            }
            catch (Exception e)
            {
                // Okay, so we couldn't open it. Maybe it needs a different extension?
                // FIXME: This only works for files on the disk, what about custom streams? -flibit
                modifiedAssetName = MonoGame.Utilities.FileHelpers.NormalizeFilePathSeparators(
                    Path.Combine(RootDirectoryFullPath, assetName)
                    );
                if (typeof(T) == typeof(Texture2D) || typeof(T) == typeof(Texture))
                {
                    modifiedAssetName = Texture2DReader.Normalize(modifiedAssetName);
                }
                else if ((typeof(T) == typeof(SoundEffect)))
                {
                    modifiedAssetName = SoundEffectReader.Normalize(modifiedAssetName);
                }
                else if ((typeof(T) == typeof(Effect)))
                {
                    modifiedAssetName = EffectReader.Normalize(modifiedAssetName);
                }
                else if ((typeof(T) == typeof(Song)))
                {
                    modifiedAssetName = SongReader.Normalize(modifiedAssetName);
                }
                else if ((typeof(T) == typeof(Video)))
                {
                    modifiedAssetName = VideoReader.Normalize(modifiedAssetName);
                }
                else
                {
                    // No raw format available, disregard!
                    modifiedAssetName = null;
                }

                // Did we get anything...?
                if (String.IsNullOrEmpty(modifiedAssetName))
                {
                    // Nope, nothing we're aware of!
                    throw new ContentLoadException(
                              "Could not load asset " + assetName + "! Error: " + e.Message,
                              e
                              );
                }

                stream = TitleContainer.OpenStream(modifiedAssetName);
            }

            // Check for XNB header
            stream.Read(xnbHeader, 0, xnbHeader.Length);
            if (xnbHeader[0] == 'X' &&
                xnbHeader[1] == 'N' &&
                xnbHeader[2] == 'B' &&
                targetPlatformIdentifiers.Contains((char)xnbHeader[3]))
            {
                using (BinaryReader xnbReader = new BinaryReader(stream))
                    using (ContentReader reader = GetContentReaderFromXnb(assetName, ref stream, xnbReader, (char)xnbHeader[3], recordDisposableObject))
                    {
                        result = reader.ReadAsset <T>();
                        GraphicsResource resource = result as GraphicsResource;
                        if (resource != null)
                        {
                            resource.Name = assetName;
                        }
                    }
            }
            else
            {
                // It's not an XNB file. Try to load as a raw asset instead.

                // FIXME: Assuming seekable streams! -flibit
                stream.Seek(0, SeekOrigin.Begin);

                if (typeof(T) == typeof(Texture2D) || typeof(T) == typeof(Texture))
                {
                    Texture2D texture;
                    if (xnbHeader[0] == 'D' &&
                        xnbHeader[1] == 'D' &&
                        xnbHeader[2] == 'S' &&
                        xnbHeader[3] == ' ')
                    {
                        texture = Texture2D.DDSFromStreamEXT(
                            GetGraphicsDevice(),
                            stream
                            );
                    }
                    else
                    {
                        texture = Texture2D.FromStream(
                            GetGraphicsDevice(),
                            stream
                            );
                    }
                    texture.Name = assetName;
                    result       = texture;
                }
                else if ((typeof(T) == typeof(SoundEffect)))
                {
                    result = SoundEffect.FromStream(stream);
                }
                else if ((typeof(T) == typeof(Effect)))
                {
                    byte[] data = new byte[stream.Length];
                    stream.Read(data, 0, (int)stream.Length);
                    result = new Effect(GetGraphicsDevice(), data);
                }
                else if ((typeof(T) == typeof(Song)))
                {
                    // FIXME: Not using the stream! -flibit
                    result = new Song(modifiedAssetName);
                }
                else if ((typeof(T) == typeof(Video)))
                {
                    // FIXME: Not using the stream! -flibit
                    result = new Video(modifiedAssetName, GetGraphicsDevice());
                    FNALoggerEXT.LogWarn(
                        "Video " +
                        modifiedAssetName +
                        " does not have an XNB file! Hacking Duration property!"
                        );
                }
                else
                {
                    stream.Close();
                    throw new ContentLoadException("Could not load " + assetName + " asset!");
                }

                /* Because Raw Assets skip the ContentReader step, they need to have their
                 * disposables recorded here. Doing it outside of this catch will
                 * result in disposables being logged twice.
                 */
                IDisposable disposableResult = result as IDisposable;
                if (disposableResult != null)
                {
                    if (recordDisposableObject != null)
                    {
                        recordDisposableObject(disposableResult);
                    }
                    else
                    {
                        disposableAssets.Add(disposableResult);
                    }
                }

                /* Because we're not using a BinaryReader for raw assets, we
                 * need to close the stream ourselves.
                 * -flibit
                 */
                stream.Close();
            }

            return((T)result);
        }
예제 #3
0
        protected T ReadAsset <T>(string assetName, Action <IDisposable> recordDisposableObject)
        {
            if (string.IsNullOrEmpty(assetName))
            {
                throw new ArgumentNullException("assetName");
            }
            if (disposed)
            {
                throw new ObjectDisposedException("ContentManager");
            }

            string originalAssetName = assetName;
            object result            = null;

            if (this.graphicsDeviceService == null)
            {
                this.graphicsDeviceService = serviceProvider.GetService(typeof(IGraphicsDeviceService)) as IGraphicsDeviceService;
                if (this.graphicsDeviceService == null)
                {
                    throw new InvalidOperationException("No Graphics Device Service");
                }
            }

            Stream stream = null;

            try
            {
                //try load it traditionally
                stream = OpenStream(assetName);

                // Try to load as XNB file
                try
                {
                    using (BinaryReader xnbReader = new BinaryReader(stream))
                    {
                        using (ContentReader reader = GetContentReaderFromXnb(assetName, ref stream, xnbReader, recordDisposableObject))
                        {
                            result = reader.ReadAsset <T>();
                            if (result is GraphicsResource)
                            {
                                ((GraphicsResource)result).Name = originalAssetName;
                            }
                        }
                    }
                }
                finally
                {
                    if (stream != null)
                    {
                        stream.Dispose();
                    }
                }
            }
            catch (ContentLoadException ex)
            {
                //MonoGame try to load as a non-content file

                assetName = TitleContainer.GetFilename(Path.Combine(RootDirectory, assetName));

                assetName = Normalize <T>(assetName);

                if (string.IsNullOrEmpty(assetName))
                {
                    throw new ContentLoadException("Could not load " + originalAssetName + " asset as a non-content file!", ex);
                }

                result = ReadRawAsset <T>(assetName, originalAssetName);

                // Because Raw Assets skip the ContentReader step, they need to have their
                // disopsables recorded here. Doing it outside of this catch will
                // result in disposables being logged twice.
                if (result is IDisposable)
                {
                    if (recordDisposableObject != null)
                    {
                        recordDisposableObject(result as IDisposable);
                    }
                    else
                    {
                        disposableAssets.Add(result as IDisposable);
                    }
                }
            }

            if (result == null)
            {
                throw new ContentLoadException("Could not load " + originalAssetName + " asset!");
            }

            return((T)result);
        }
예제 #4
0
        protected T ReadAsset <T>(string assetName, Action <IDisposable> recordDisposableObject)
        {
            if (string.IsNullOrEmpty(assetName))
            {
                throw new ArgumentNullException("assetName");
            }
            if (this.disposed)
            {
                throw new ObjectDisposedException("ContentManager");
            }
            string originalAssetName = assetName;
            object obj = (object)null;

            if (this.graphicsDeviceService == null)
            {
                this.graphicsDeviceService = this.serviceProvider.GetService(typeof(IGraphicsDeviceService)) as IGraphicsDeviceService;
                if (this.graphicsDeviceService == null)
                {
                    throw new InvalidOperationException("No Graphics Device Service");
                }
            }
            try
            {
                Stream stream = this.OpenStream(assetName);
                try
                {
                    using (BinaryReader xnbReader = new BinaryReader(stream))
                    {
                        using (ContentReader contentReaderFromXnb = this.GetContentReaderFromXnb(assetName, ref stream, xnbReader, recordDisposableObject))
                        {
                            obj = contentReaderFromXnb.ReadAsset <T>();
                            if (obj is GraphicsResource)
                            {
                                ((GraphicsResource)obj).Name = originalAssetName;
                            }
                        }
                    }
                }
                finally
                {
                    if (stream != null)
                    {
                        stream.Dispose();
                    }
                }
            }
            catch (ContentLoadException ex)
            {
                assetName = TitleContainer.GetFilename(Path.Combine(this.RootDirectory, assetName));
                assetName = this.Normalize <T>(assetName);
                if (string.IsNullOrEmpty(assetName))
                {
                    throw new ContentLoadException("Could not load " + originalAssetName + " asset as a non-content file!", (Exception)ex);
                }
                obj = this.ReadRawAsset <T>(assetName, originalAssetName);
                if (obj is IDisposable)
                {
                    if (recordDisposableObject != null)
                    {
                        recordDisposableObject(obj as IDisposable);
                    }
                    else
                    {
                        this.disposableAssets.Add(obj as IDisposable);
                    }
                }
            }
            if (obj == null)
            {
                throw new ContentLoadException("Could not load " + originalAssetName + " asset!");
            }
            else
            {
                return((T)obj);
            }
        }
예제 #5
0
        public T ReadAsset <T>(object asset, string assetName, Action <IDisposable> recordDisposableObject)
        {
            if (string.IsNullOrEmpty(assetName))
            {
                throw new ArgumentNullException("assetName");
            }
            if (disposed)
            {
                throw new ObjectDisposedException("ContentManager");
            }

            string originalAssetName = assetName;
            object result            = null;

            /* QQQ
             * if (this.graphicsDeviceService == null)
             * {
             *      this.graphicsDeviceService = serviceProvider.GetService(typeof(IGraphicsDeviceService)) as IGraphicsDeviceService;
             *      if (this.graphicsDeviceService == null)
             *      {
             *              throw new InvalidOperationException("No Graphics Device Service");
             *      }
             * }
             */
            Stream    stream = null;
            TextAsset binData;

            if (asset == null)
            {
                binData = (TextAsset)NativeLoad(assetName, typeof(TextAsset));
            }
            else
            {
                binData = (TextAsset)asset;
            }

            stream = new MemoryStream(binData.bytes);

            // Try to load as XNB file
            try
            {
                using (BinaryReader xnbReader = new BinaryReader(stream))
                {
                    using (ContentReader reader = GetContentReaderFromXnb(assetName, ref stream, xnbReader, recordDisposableObject))
                    {
                        result = reader.ReadAsset <T>();
                        if (result is GraphicsResource)
                        {
                            ((GraphicsResource)result).Name = originalAssetName;
                        }
                    }
                }
            }
            finally
            {
                if (stream != null)
                {
                    stream.Dispose();
                }
            }

            if (result == null)
            {
                throw new ContentLoadException("Could not load " + originalAssetName + " asset!");
            }

            return((T)result);
        }
예제 #6
0
        protected T ReadAsset <T>(string assetName, Action <IDisposable> recordDisposableObject)
        {
            if (string.IsNullOrEmpty(assetName))
            {
                throw new ArgumentNullException("assetName");
            }
            if (disposed)
            {
                throw new ObjectDisposedException("ContentManager");
            }

            object result = null;

            // FIXME: Should this block be here? -flibit
            if (graphicsDeviceService == null)
            {
                graphicsDeviceService = ServiceProvider.GetService(typeof(IGraphicsDeviceService)) as IGraphicsDeviceService;
                if (graphicsDeviceService == null)
                {
                    throw new InvalidOperationException("No Graphics Device Service");
                }
            }

            Stream stream            = null;
            string modifiedAssetName = String.Empty;             // Will be used if we have to guess a filename

            try
            {
                stream = OpenStream(assetName);
            }
            catch (Exception e)
            {
                // Okay, so we couldn’t open it. Maybe it needs a different extension?
                // FIXME: Normalizing checks for a file on the disk, what about custom streams? -flibit
                modifiedAssetName = FileHelpers.NormalizeFilePathSeparators(
                    Path.Combine(RootDirectoryFullPath, assetName)
                    );
                if (typeof(T) == typeof(Texture2D) || typeof(T) == typeof(Texture))
                {
                    modifiedAssetName = Texture2DReader.Normalize(modifiedAssetName);
                }
                else if ((typeof(T) == typeof(SoundEffect)))
                {
                    modifiedAssetName = SoundEffectReader.Normalize(modifiedAssetName);
                }
                else if ((typeof(T) == typeof(Effect)))
                {
                    modifiedAssetName = EffectReader.Normalize(modifiedAssetName);
                }
                else if ((typeof(T) == typeof(Song)))
                {
                    modifiedAssetName = SongReader.Normalize(modifiedAssetName);
                }
                else if ((typeof(T) == typeof(Video)))
                {
                    modifiedAssetName = VideoReader.Normalize(modifiedAssetName);
                }

                // Did we get anything…?
                if (String.IsNullOrEmpty(modifiedAssetName))
                {
                    // Nope, nothing we’re aware of!
                    throw new ContentLoadException(
                              "Could not load asset " + assetName + "! Error: " + e.Message,
                              e
                              );
                }

                stream = File.OpenRead(modifiedAssetName);
            }

            using (BinaryReader xnbReader = new BinaryReader(stream))
            {
                try
                {
                    // Try to load as XNB file
                    using (ContentReader reader = GetContentReaderFromXnb(assetName, ref stream, xnbReader, recordDisposableObject))
                    {
                        result = reader.ReadAsset <T>();
                        GraphicsResource resource = result as GraphicsResource;
                        if (resource != null)
                        {
                            resource.Name = assetName;
                        }
                    }
                }
                catch (Exception e)
                {
                    // FIXME: Assuming seekable streams! -flibit
                    stream.Seek(0, SeekOrigin.Begin);

                    // Try to load as a raw asset
                    if (typeof(T) == typeof(Texture2D) || typeof(T) == typeof(Texture))
                    {
                        Texture2D texture = Texture2D.FromStream(
                            graphicsDeviceService.GraphicsDevice,
                            stream
                            );
                        texture.Name = assetName;
                        result       = texture;
                    }
                    else if ((typeof(T) == typeof(SoundEffect)))
                    {
                        result = SoundEffect.FromStream(stream);
                    }
                    else if ((typeof(T) == typeof(Effect)))
                    {
                        byte[] data = new byte[stream.Length];
                        stream.Read(data, 0, (int)stream.Length);
                        result = new Effect(graphicsDeviceService.GraphicsDevice, data);
                    }
                    else if ((typeof(T) == typeof(Song)))
                    {
                        // FIXME: Not using the stream! -flibit
                        result = new Song(modifiedAssetName);
                    }
                    else if ((typeof(T) == typeof(Video)))
                    {
                        // FIXME: Not using the stream! -flibit
                        result = new Video(modifiedAssetName);
                    }
                    else
                    {
                        // We dunno WTF this is, give them the XNB Exception.
                        throw e;
                    }

                    /* Because Raw Assets skip the ContentReader step, they need to have their
                     * disposables recorded here. Doing it outside of this catch will
                     * result in disposables being logged twice.
                     */
                    IDisposable disposableResult = result as IDisposable;
                    if (disposableResult != null)
                    {
                        if (recordDisposableObject != null)
                        {
                            recordDisposableObject(disposableResult);
                        }
                        else
                        {
                            disposableAssets.Add(disposableResult);
                        }
                    }
                }
            }

            if (result == null)
            {
                throw new ContentLoadException("Could not load " + assetName + " asset!");
            }

            return((T)result);
        }