#region CPL License
/*
Nuclex Framework
Copyright (C) 2002-2011 Nuclex Development Labs
This library is free software; you can redistribute it and/or
modify it under the terms of the IBM Common Public License as
published by the IBM Corporation; either version 1.0 of the
License, or (at your option) any later version.
This library is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
IBM Common Public License for more details.
You should have received a copy of the IBM Common Public
License along with this library
*/
#endregion
using System;
using System.Collections.Generic;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Graphics;
using Microsoft.Xna.Framework.Content;
namespace Nuclex.Game.Content {
/// Manages global content that is shared within a game
///
/// This class allows your game to create a custom type of content manager
/// (for example, the LzmaContentManager) and share is globally throughout
/// your game through the ISharedContentService.
///
public class SharedContentManager : Component, ISharedContentService, IDisposable {
/// Initializes a new shared content manager
///
/// Game service container to use for accessing other game components
/// like the graphics device service. The shared content manager also
/// registers itself (under ) herein.
///
public SharedContentManager(GameServiceContainer gameServices) {
this.serviceContainer = gameServices;
// Register ourselfes as the shared content service for the game to allow
// other component to access the assets managed by us.
gameServices.AddService(typeof(ISharedContentService), this);
}
///
/// Allows the game component to perform any initialization it needs to before
/// starting to run. This is where it can query for any required services and
/// load content.
///
public override void Initialize() {
// Create a new content manager for the graphics device that just came up
this.contentManager = CreateContentManager();
}
/// Immediately releases all resources and unregisters the component
public virtual void Dispose() {
// Unregister the service if we registered it to the game service container
if (this.serviceContainer != null) {
object registeredService = this.serviceContainer.GetService(
typeof(ISharedContentService)
);
if (ReferenceEquals(registeredService, this)) {
this.serviceContainer.RemoveService(typeof(ISharedContentService));
}
this.serviceContainer = null;
}
// Release all content stored in the content manager
if (this.contentManager != null) {
this.contentManager.Dispose();
this.contentManager = null;
}
}
/// Loads or accesses shared game content
/// Type of the asset to be loaded or accessed
/// Path and name of the requested asset
/// The requested asset from the the shared game content store
public AssetType Load(string assetName) {
if (this.contentManager == null) {
throw new InvalidOperationException(
"Cannot load asset: shared content manager not initialized"
);
}
return this.contentManager.Load(assetName);
}
/// Creates a new content manager for the shared content provider
/// The newly created content manager
///
/// Override this method in a derived to call another constructor of the content
/// manager or to use a custom content manager implementation if you need to.
///
protected virtual ContentManager CreateContentManager() {
return new ContentManager(this.serviceContainer, "Content");
}
/// Unloads all resources from the shared content manager
public void Unload() {
// Unload all content in the content manager to release the graphics device
// resources again before the device is destroyed
this.contentManager.Unload();
}
///
/// Game service container the shared content manager is registered to
///
private GameServiceContainer serviceContainer;
/// Content manager containing the shared assets of the game
private ContentManager contentManager;
}
} // namespace Nuclex.Game.Content