Click or drag to resize
DigitalRuneStorage and Virtual File Systems

This topic describes the storage API for cross-platform file access and virtual file systems.

This topic contains the following sections:

Storages

Different platforms have different APIs for accessing files. For example:

  • Desktop apps access the file system directly (namespace System.IO).
  • Silverlight apps use isolated storage.
  • Windows Store apps use storage folders.
  • XNA games use the title storage and user storage on Xbox 360.
  • Etc.

DigitalRune introduces a common storage API. The interface IStorage provides access to files from different sources. The namespace DigitalRune.Storages includes several storage providers:

The following storage types are built on top of other storages:

  • ZipStorage provides access to files stored in ZIP archives. The ZIP archive can be read from any of the existing storages.
  • VfsStorage maps existing storages into a virtual file system. Different storage devices and locations can be accessed as a single directory hierarchy.

Additional storage providers will be added in future releases. Third-party developers can also provide custom implementations.

PAK file format

Video games have a tradition of packing game content into single package files (.PAK files). Package formats are used to simplify the file structure, add file compression and encryption. The actual file format of a .PAK file may vary from one game to another.

The ZipStorage provides access to package files that use the ZIP file format. That means, game assets can be packed into a ZIP package and the game logic can access the files in the package. The ZipStorage automatically handles decompression and encryption (optional).

Developers can provide custom IStorage implementations to provide support for other package file formats.

DigitalRune includes a command-line tool Pack for packing files into a ZIP archive. The tool was designed to be used from the command-line or called as a Build Event in a Visual Studio project.

Note Note

It is not required to use the Pack.exe provided by DigitalRune. Any standard ZIP tool can be used to create/update packages.

Virtual file systems

The class VfsStorage can be used to create a virtual file system. A virtual file system is abstraction on top of concrete storages. Existing storage locations can be mapped into a virtual directory hierarchy.

Virtual files systems are commonly used in games to abstract access to files. The actual storage locations and file IO may vary from one platform to another. For example, game content can be provided on a removable media (ROM cartridge, DVD, Blu-ray Disc, etc.), can be installed on a local harddisk, or can be cached in memory for faster access. The storage classes can be used to access these locations. A VfsStorage can be set up to map these locations into a virtual file system. The game logic reads assets from the virtual file system. Using this approach the game logic can more easily be ported to different platforms.

Example: MonoGame Samples project

The following structure is used in the Samples project for MonoGame: The game assets are stored in the Content subdirectory of the application directory. The sample assets (*.xnb files) are packed into a single ZIP archive Assets.zip. The DigitalRune Graphics assets are stored in DigitalRune.zip.

<gameLocation>/
    Content/
      Assets.zip
      DigitalRune.zip
      other assets...

The following code is used to set up the virtual file system:

Setting up a virtual file system
// Create a TitleStorage that reads files from "<gameLocation>/Content".
var titleStorage = new TitleStorage("Content");

// The VfsStorage creates a virtual file system.
var vfsStorage = new VfsStorage();

// Map the titleStorage to the root "/" of the virtual file system.
vfsStorage.MountInfos.Add(new VfsMountInfo(titleStorage, null));

// The game assets (*.xnb files) are packed as a ZIP archive "Content/Assets.zip".
// A ZipStorage can be used to access files inside the ZIP archive.
// Mount the zipped files to the root "/" of the virtual file system.
var assetsStorage = new ZipStorage(titleStorage, "Assets.zip");
vfsStorage.MountInfos.Add(new VfsMountInfo(assetsStorage, null));

// DigitalRune Graphics assets (built-in effects) are stored in "Content/DigitalRune.zip".
// Mount the DigitalRune assets to the root of the virtual file system.
var drStorage = new ZipStorage(titleStorage, "DigitalRune.zip");
vfsStorage.MountInfos.Add(new VfsMountInfo(drStorage, null));

Once the virtual file system is set up, files can be easily be open for reading.

Reading a file from a virtual file system
try
{
  // To read the file "Models/Model.xnb" which is stored in "Assets.zip":
  using (var stream = vfsStorage.OpenFile("Models/Model.xnb")
  {
    // Here: Read file content.
  }
}
catch (Exception ex)
{
  // Here: Handle file IO exceptions.
}

In XNA an StorageContentManager can be used instead of the regular XNA ContentManager.

StorageContentManager for XNA
// The StorageContentManager can be used in XNA to read game assets from the
// virtual file system. (Replaces the XNA ContentManager.)
Content = new StorageContentManager(Services, vfsStorage);

Note that multiple storage locations can be mapped to the same mount point. In the example above three storages (Content/*.*, Content/Assets.zip, and Content/DigitalRune.zip) are mapped to the root "/" of the virtual file system. The order of the entries in the MountInfos list determines the search order.

See Also