Virtu/Virtu/Xna/Services/XnaStorageService.cs
Sean Fausett bc4caaf2b1 Initial upgrade to VS 2010, .NET FW 4, SL 4, XNA 4, and Windows Phone 7 CTP.
--HG--
extra : convert_revision : svn%3Affd33b8c-2492-42e0-bdc5-587b920b7d6d/trunk%4046550
2010-05-19 23:42:10 +00:00

63 lines
1.8 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 path, Action<Stream> reader)
{
if (reader == null)
{
throw new ArgumentNullException("reader");
}
try
{
using (var storageContainer = _storageDevice.Value.OpenContainer(_game.Name))
{
using (var stream = new FileStream(Path.Combine(storageContainer.Path, path), FileMode.Open, FileAccess.Read, FileShare.Read))
{
reader(stream);
}
}
}
catch (FileNotFoundException)
{
}
}
public override void Save(string path, Action<Stream> writer)
{
if (writer == null)
{
throw new ArgumentNullException("writer");
}
using (var storageContainer = _storageDevice.Value.OpenContainer(_game.Name))
{
using (var stream = new FileStream(Path.Combine(storageContainer.Path, path), FileMode.Create, FileAccess.Write, FileShare.None))
{
writer(stream);
}
}
}
private GameBase _game;
private Lazy<StorageDevice> _storageDevice = new Lazy<StorageDevice>(() => StorageDevice.EndShowSelector(StorageDevice.BeginShowSelector(null, null)));
}
}