Ejemplo n.º 1
0
        /// <summary>
        /// Called when graphics resources need to be loaded. Override this method to load any
        /// game-specific graphics resources.
        /// </summary>
        protected override void LoadContent()
        {
            try
            {
                // Configure ContentManager to load the SpriteFont.
                var titleStorage = new TitleStorage("Content");
                var vfsStorage   = new VfsStorage();
                vfsStorage.MountInfos.Add(new VfsMountInfo(titleStorage, null));
#if MONOGAME
                var assetsStorage = new ZipStorage(titleStorage, "Content.zip");
                vfsStorage.MountInfos.Add(new VfsMountInfo(assetsStorage, null));
                var drStorage = new ZipStorage(titleStorage, "DigitalRune.zip");
                vfsStorage.MountInfos.Add(new VfsMountInfo(drStorage, null));
#endif
                Content = new StorageContentManager(Services, vfsStorage);

                // Load SpriteFont.
                _spriteFont  = Content.Load <SpriteFont>("SpriteFont1");
                _spriteBatch = new SpriteBatch(GraphicsDevice);
            }
            catch
            {
                // Failed to load sprite font.
            }
        }
Ejemplo n.º 2
0
        public MonoGameContent LoadXnb(string rootDirectoryName, string fileName, bool cacheResult)
        {
            Debug.Assert(rootDirectoryName != null);
            Debug.Assert(rootDirectoryName.Length > 0);
            Debug.Assert(fileName != null);
            Debug.Assert(fileName.Length > 0);

            // Check whether content has already been loaded and is still cached.
            if (_cachedContent != null &&
                !_cachedContent.IsDisposed &&
                _cachedContent.RootDirectoryName == rootDirectoryName &&
                _cachedContent.FileName == fileName)
            {
                var result = _cachedContent;
                if (!cacheResult)
                {
                    _cachedContent = null;
                }

                return(result);
            }

            // Clear cache.
            _cachedContent?.Dispose();
            _cachedContent = null;

            // External references in MonoGame are relative to the content root directory, not the
            // model. Loading the model fails, if a external reference cannot be resolved.
            // --> Try different content root directories.
            var rootDirectory = new DirectoryInfo(rootDirectoryName);

            while (rootDirectory != null)
            {
                VfsStorage            storage = null;
                StorageContentManager content = null;
                try
                {
                    // Create a virtual file system which contains the DigitalRune effects and the content folder.
                    storage = new VfsStorage();
                    storage.MountInfos.Add(new VfsMountInfo(new ZipStorage(new TitleStorage("Content"), "DigitalRune.zip"), null));
                    storage.MountInfos.Add(new VfsMountInfo(new FileSystemStorage(rootDirectory.FullName), null));
                    content = new StorageContentManager(_services, storage);

                    string assetName = DRPath.GetRelativePath(rootDirectory.FullName, fileName);
                    var    asset     = content.Load <object>(assetName);

                    var result = new MonoGameContent(rootDirectoryName, fileName, storage, content, asset);

                    if (cacheResult)
                    {
                        _cachedContent = result;
                    }

                    return(result);
                }
                catch (Exception exception)
                {
                    storage?.Dispose();
                    content?.Dispose();

                    if (exception is ContentLoadException &&
                        exception.InnerException is ContentLoadException &&
                        rootDirectory.Parent != null)
                    {
                        // ExternalReference could probably not be resolved.
                        // --> Retry with parent folder as content root.
                    }
                    else
                    {
                        // Asset could not be loaded.
                        throw;
                    }
                }

                rootDirectory = rootDirectory.Parent;
            }

            // Execution should never reach this point.
            throw new EditorException("Unexpected error.");
        }