Beispiel #1
0
        protected internal override Video Read(
            ContentReader input,
            Video existingInstance
            )
        {
            string path = input.ReadString();

            path = Path.Combine(input.ContentManager.RootDirectory, path);
            path = TitleContainer.GetFilename(path);

            /* The path string includes the ".wmv" extension. Let's see if this
             * file exists in a format we actually support...
             */
            path = Normalize(Path.GetFileNameWithoutExtension(path));
            if (String.IsNullOrEmpty(path))
            {
                throw new ContentLoadException();
            }

            int   durationMS      = input.ReadObject <int>();
            int   width           = input.ReadObject <int>();
            int   height          = input.ReadObject <int>();
            float framesPerSecond = input.ReadObject <Single>();
            VideoSoundtrackType soundTrackType = (VideoSoundtrackType)input.ReadObject <int>();

            return(new Video(path, durationMS, width, height, framesPerSecond, soundTrackType));
        }
Beispiel #2
0
        protected virtual Stream OpenStream(string assetName)
        {
            Stream stream;

            try
            {
                string assetPath = TitleContainer.GetFilename(
                    Path.Combine(RootDirectoryFullPath, assetName) + ".xnb"
                    );
                stream = File.OpenRead(assetPath);
            }
            catch (FileNotFoundException fileNotFound)
            {
                throw new ContentLoadException("The content file was not found.", fileNotFound);
            }
            catch (DirectoryNotFoundException directoryNotFound)
            {
                throw new ContentLoadException("The directory was not found.", directoryNotFound);
            }
            catch (Exception exception)
            {
                throw new ContentLoadException("Opening stream error.", exception);
            }
            return(stream);
        }
Beispiel #3
0
        protected virtual void ReloadAsset <T>(string originalAssetName, T currentAsset)
        {
            string assetName = originalAssetName;

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

            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 to 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, null))
                        {
                            reader.InitializeTypeReaders();
                            reader.ReadObject <T>(currentAsset);
                            reader.ReadSharedResources();
                        }
                    }
                }
                finally
                {
                    if (stream != null)
                    {
                        stream.Dispose();
                    }
                }
            }
            catch (ContentLoadException)
            {
                // Try to reload as a non-xnb file.
                assetName = TitleContainer.GetFilename(
                    Path.Combine(RootDirectoryFullPath, assetName)
                    );
                assetName = Normalize <T>(assetName);
                ReloadRawAsset(currentAsset, assetName, originalAssetName);
            }
        }
Beispiel #4
0
        protected internal override Song Read(ContentReader input, Song existingInstance)
        {
            string path3    = input.ReadString();
            string filename = TitleContainer.GetFilename(Path.Combine(input.ContentManager.RootDirectory, input.ContentManager.CurrentAssetDirectory, path3));

            input.ReadObject <int>();
            return(new Song(filename));
        }
        protected internal override Song Read(ContentReader input, Song existingInstance)
        {
            string path = input.ReadString();

            // Songs don't have the full directory path in their .xnb. Build it.
            path = Path.Combine(input.ContentManager.RootDirectory, input.ContentManager.CurrentAssetDirectory, path);
            path = TitleContainer.GetFilename(path);

            /*int durationMS =*/ input.ReadObject <int>();

            return(new Song(path));
        }
Beispiel #6
0
        protected internal override Video Read(ContentReader input, Video existingInstance)
        {
            string path = input.ReadObject <string>();

            path = Path.Combine(input.ContentManager.RootDirectory, path);
            path = TitleContainer.GetFilename(path);

            /*int durationMS =*/ input.ReadObject <int>();
            /*int width =*/ input.ReadObject <int>();
            /*int height =*/ input.ReadObject <int>();
            /*float framesPerSecond =*/ input.ReadObject <Single>();
            /*int soundTrackType =*/ input.ReadObject <int>();   // 0 = Music, 1 = Dialog, 2 = Music and dialog
            return(new Video(path));
        }
Beispiel #7
0
        protected virtual void ReloadAsset <T>(string originalAssetName, T currentAsset)
        {
            string str = originalAssetName;

            if (string.IsNullOrEmpty(str))
            {
                throw new ArgumentNullException("assetName");
            }
            if (this.disposed)
            {
                throw new ObjectDisposedException("ContentManager");
            }
            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(str);
                try
                {
                    using (BinaryReader xnbReader = new BinaryReader(stream))
                    {
                        using (ContentReader contentReaderFromXnb = this.GetContentReaderFromXnb(str, ref stream, xnbReader, (Action <IDisposable>)null))
                        {
                            contentReaderFromXnb.InitializeTypeReaders();
                            contentReaderFromXnb.ReadObject <T>(currentAsset);
                            contentReaderFromXnb.ReadSharedResources();
                        }
                    }
                }
                finally
                {
                    if (stream != null)
                    {
                        stream.Dispose();
                    }
                }
            }
            catch (ContentLoadException ex)
            {
                string assetName = this.Normalize <T>(TitleContainer.GetFilename(Path.Combine(this.RootDirectory, str)));
                this.ReloadRawAsset <T>(currentAsset, assetName, originalAssetName);
            }
        }
Beispiel #8
0
        protected internal override Song Read(ContentReader input, Song existingInstance)
        {
            string path = input.ReadString();

            path = Path.Combine(input.ContentManager.RootDirectory, path);
            path = TitleContainer.GetFilename(path);

            /* The path string includes the ".wma" extension. Let's see if this
             * file exists in a format we actually support...
             */
            path = Normalize(Path.GetFileNameWithoutExtension(path));
            if (String.IsNullOrEmpty(path))
            {
                throw new ContentLoadException();
            }

            int durationMs = input.ReadObject <int>();

            return(new Song(path, durationMs));
        }
Beispiel #9
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);
        }
Beispiel #10
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);
            }
        }