#region License
/* FNA - XNA4 Reimplementation for Desktop Platforms
* Copyright 2009-2016 Ethan Lee and the MonoGame Team
*
* Released under the Microsoft Public License.
* See LICENSE for details.
*/
#endregion
#region Using Statements
using System;
using System.IO;
using System.Threading;
using System.Runtime.Remoting.Messaging;
#endregion
namespace Microsoft.Xna.Framework.Storage
{
///
/// Exposes a storage device for storing user data.
///
///
/// MSDN documentation contains related conceptual article:
/// http://msdn.microsoft.com/en-us/library/bb200105.aspx
///
public sealed class StorageDevice
{
#region Public Properties
///
/// Returns the amount of free space.
///
public long FreeSpace
{
get
{
try
{
return new DriveInfo(storageRoot).AvailableFreeSpace;
}
catch(Exception e)
{
// Storage root was invalid or unavailable.
throw new StorageDeviceNotConnectedException(
"The storage device bound to the container is not connected.",
e
);
}
}
}
///
/// Returns true if this StorageDevice path is accessible, false otherwise.
///
public bool IsConnected
{
get
{
return FNAPlatform.IsStoragePathConnected(storageRoot);
}
}
///
/// Returns the total size of device.
///
public long TotalSpace
{
get
{
try
{
return new DriveInfo(storageRoot).TotalSize;
}
catch(Exception e)
{
// Storage root was invalid or unavailable.
throw new StorageDeviceNotConnectedException(
"The storage device bound to the container is not connected.",
e
);
}
}
}
#endregion
#region Private Variables
private PlayerIndex? devicePlayer;
#endregion
#region Private Static Variables
private static readonly string storageRoot = FNAPlatform.GetStorageRoot();
#endregion
#region Events
///
/// Fired when a device is removed or inserted.
///
public static event EventHandler DeviceChanged;
private void OnDeviceChanged()
{
if (DeviceChanged != null)
{
DeviceChanged(this, null);
}
}
#endregion
#region Private XNA Lies
private class NotAsyncLie : IAsyncResult
{
public object AsyncState
{
get;
private set;
}
public bool CompletedSynchronously
{
get
{
return true;
}
}
public bool IsCompleted
{
get
{
return true;
}
}
public WaitHandle AsyncWaitHandle
{
get;
private set;
}
public NotAsyncLie(object state)
{
AsyncState = state;
AsyncWaitHandle = new ManualResetEvent(true);
}
}
private class ShowSelectorLie : NotAsyncLie
{
public readonly PlayerIndex? PlayerIndex;
public ShowSelectorLie(object state, PlayerIndex? playerIndex) : base(state)
{
PlayerIndex = playerIndex;
}
}
private class OpenContainerLie : NotAsyncLie
{
public readonly string DisplayName;
public OpenContainerLie(object state, string displayName) : base(state)
{
DisplayName = displayName;
}
}
#endregion
#region Internal Constructors
internal StorageDevice(PlayerIndex? player)
{
devicePlayer = player;
}
#endregion
#region Public OpenContainer Methods
///
/// Begins the open for a StorageContainer.
///
/// The open StorageContainer.
/// Name of file.
/// Method to call on completion.
/// Request identifier object for callback (can be null).
public IAsyncResult BeginOpenContainer(
string displayName,
AsyncCallback callback,
object state
) {
IAsyncResult result = new OpenContainerLie(state, displayName);
if (callback != null)
{
callback(result);
}
return result;
}
///
/// Ends the open container process.
///
/// The open StorageContainer.
/// Result of BeginOpenContainer.
public StorageContainer EndOpenContainer(IAsyncResult result)
{
return new StorageContainer(
this,
(result as OpenContainerLie).DisplayName,
storageRoot,
devicePlayer
);
}
#endregion
#region Public ShowSelector Methods
///
/// Begin process to display the StorageDevice selector UI.
///
/// The show selector.
/// Method to invoke when device is selected by player.
/// Request identifier object for callback (can be null).
public static IAsyncResult BeginShowSelector(
AsyncCallback callback,
object state
) {
return BeginShowSelector(
0,
0,
callback,
state
);
}
///
/// Begin process to display the StorageDevice selector UI.
///
/// The show selector.
/// The PlayerIndex. Only PlayerIndex.One is valid on Windows.
/// Method to invoke when device is selected by player.
/// Request identifier object for callback (can be null).
public static IAsyncResult BeginShowSelector(
PlayerIndex player,
AsyncCallback callback,
object state
) {
return BeginShowSelector(
player,
0,
0,
callback,
state
);
}
///
/// Begin process to display the StorageDevice selector UI.
///
/// The show selector.
/// Size (in bytes) of data to write.
/// Number of directories to write.
/// Method to invoke when device is selected by player.
/// Request identifier object for callback (can be null).
public static IAsyncResult BeginShowSelector(
int sizeInBytes,
int directoryCount,
AsyncCallback callback,
object state
) {
IAsyncResult result = new ShowSelectorLie(state, null);
if (callback != null)
{
callback(result);
}
return result;
}
///
/// Begin process to display the StorageDevice selector UI.
///
/// The show selector.
/// The PlayerIndex. Only PlayerIndex.One is valid on Windows.
/// Size (in bytes) of data to write.
/// Number of directories to write.
/// Method to invoke when device is selected by player.
/// Request identifier object for callback (can be null).
public static IAsyncResult BeginShowSelector(
PlayerIndex player,
int sizeInBytes,
int directoryCount,
AsyncCallback callback,
object state
) {
IAsyncResult result = new ShowSelectorLie(state, player);
if (callback != null)
{
callback(result);
}
return result;
}
///
/// Ends the show selector user interface display.
///
/// The storage device.
/// The result of BeginShowSelector.
public static StorageDevice EndShowSelector(IAsyncResult result)
{
return new StorageDevice((result as ShowSelectorLie).PlayerIndex);
}
#endregion
#region Public StorageContainer Delete Method
public void DeleteContainer(string titleName)
{
throw new NotImplementedException();
}
#endregion
}
}