Пример #1
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 = FileHelpers.NormalizeFilePathSeparators(
                    Path.Combine(RootDirectoryFullPath, assetName)
                    );
                assetName = Normalize <T>(assetName);
                ReloadRawAsset(currentAsset, assetName, originalAssetName);
            }
        }
Пример #2
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);
            }
        }