Virtu/Virtu/Xna/Services/XnaStorageService.cs
Sean Fausett 0182641281 Merged machine settings into machine components.
Added save state support to all machine components.
Switched from xml serialization to binary serialization.
Refactored audio service for performance.
Bumped machine version to 0.9.0 for next release.
Miscellaneous cosmetic or minor changes.
2010-11-29 09:08:11 +13:00

68 lines
2.0 KiB
C#

using System;
using System.IO;
using Jellyfish.Library;
using Microsoft.Xna.Framework.Storage;
namespace Jellyfish.Virtu.Services
{
public sealed class XnaStorageService : StorageService
{
public XnaStorageService(Machine machine, GameBase game) :
base(machine)
{
if (game == null)
{
throw new ArgumentNullException("game");
}
_game = game;
}
public override void Load(string fileName, Action<Stream> reader)
{
if (reader == null)
{
throw new ArgumentNullException("reader");
}
try
{
using (var storageContainer = OpenContainer())
{
using (var stream = storageContainer.OpenFile(fileName, FileMode.Open, FileAccess.Read, FileShare.Read))
{
reader(stream);
}
}
}
catch (FileNotFoundException)
{
}
}
public override void Save(string fileName, Action<Stream> writer)
{
if (writer == null)
{
throw new ArgumentNullException("writer");
}
using (var storageContainer = OpenContainer())
{
using (var stream = storageContainer.OpenFile(fileName, FileMode.Create, FileAccess.Write, FileShare.None))
{
writer(stream);
}
}
}
private StorageContainer OpenContainer()
{
return _storageDevice.Value.EndOpenContainer(_storageDevice.Value.BeginOpenContainer(_game.Name, null, null));
}
private GameBase _game;
private Lazy<StorageDevice> _storageDevice = new Lazy<StorageDevice>(() => StorageDevice.EndShowSelector(StorageDevice.BeginShowSelector(null, null)));
}
}