예제 #1
0
        public void Dispose()
        {
            if (_isDisposed)
            {
                return;
            }

            _isDisposed = true;

            CoreApplication.Suspending -= OnCoreApplicationSuspending;

            // Wait until game loop is finished.
            _gameLoopTask.Wait();

            _titleStorage.Dispose();
            _assetsStorage.Dispose();
            _digitalRuneStorage.Dispose();
            _vfsStorage.Dispose();
            _contentManager.Dispose();
            _serviceContainer.Dispose();
            _graphicsManager.Dispose();
            _dxgiOutput.Dispose();
        }
예제 #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.");
        }