Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse files

Merge pull request #1074 from WaaghMan/branch_storage2

Windows 8 Storage fixes
  • Loading branch information...
commit 6691dd7793501af392e6fcc87d32bd254e45a7df 2 parents 1ad52c3 + 0d764e8
@tomspilman tomspilman authored
View
63 MonoGame.Framework/Storage/StorageContainer.cs
@@ -118,9 +118,13 @@ internal StorageContainer(StorageDevice device, string name, PlayerIndex? player
_playerIndex = playerIndex;
// From the examples the root is based on MyDocuments folder
+#if WINDOWS_STOREAPP
+ var saved = "";
+#else
var root = StorageDevice.StorageRoot;
var saved = Path.Combine(root,"SavedGames");
- _storagePath = Path.Combine(saved,name);
+#endif
+ _storagePath = Path.Combine(saved, name);
var playerSave = string.Empty;
if (playerIndex.HasValue) {
@@ -133,7 +137,8 @@ internal StorageContainer(StorageDevice device, string name, PlayerIndex? player
// Create the "device" if need be
#if WINDOWS_STOREAPP
var folder = ApplicationData.Current.LocalFolder;
- folder.CreateFolderAsync(_storagePath, CreationCollisionOption.OpenIfExists).GetResults();
+ var task = folder.CreateFolderAsync(_storagePath, CreationCollisionOption.OpenIfExists);
+ task.AsTask().Wait();
#else
if (!Directory.Exists(_storagePath))
Directory.CreateDirectory(_storagePath);
@@ -182,7 +187,8 @@ public void CreateDirectory (string directory)
// Now let's try to create it
#if WINDOWS_STOREAPP
var folder = ApplicationData.Current.LocalFolder;
- folder.CreateFolderAsync(dirPath, CreationCollisionOption.OpenIfExists).GetResults();
+ var task = folder.CreateFolderAsync(dirPath, CreationCollisionOption.OpenIfExists);
+ task.AsTask().Wait();
#else
Directory.CreateDirectory(dirPath);
#endif
@@ -226,8 +232,8 @@ public void DeleteDirectory (string directory)
// Now let's try to delete itd
#if WINDOWS_STOREAPP
var folder = ApplicationData.Current.LocalFolder;
- var deleteFolder = folder.GetFolderAsync(dirPath).GetResults();
- deleteFolder.DeleteAsync().GetResults();
+ var deleteFolder = folder.GetFolderAsync(dirPath).AsTask().GetAwaiter().GetResult();
+ deleteFolder.DeleteAsync().AsTask().Wait();
#else
Directory.Delete(dirPath);
#endif
@@ -247,8 +253,8 @@ public void DeleteFile (string file)
#if WINDOWS_STOREAPP
var folder = ApplicationData.Current.LocalFolder;
- var deleteFile = folder.GetFileAsync(filePath).GetResults();
- deleteFile.DeleteAsync().GetResults();
+ var deleteFile = folder.GetFileAsync(filePath).AsTask().GetAwaiter().GetResult();
+ deleteFile.DeleteAsync().AsTask().Wait();
#else
// Now let's try to delete it
File.Delete(filePath);
@@ -271,9 +277,17 @@ public bool DirectoryExists (string directory)
#if WINDOWS_STOREAPP
var folder = ApplicationData.Current.LocalFolder;
- var result = folder.GetFolderAsync(dirPath).GetResults();
+
+ try
+ {
+ var result = folder.GetFolderAsync(dirPath).GetResults();
return result != null;
-#else
+ }
+ catch
+ {
+ return false;
+ }
+#else
return Directory.Exists(dirPath);
#endif
}
@@ -303,8 +317,16 @@ public bool FileExists (string file)
#if WINDOWS_STOREAPP
var folder = ApplicationData.Current.LocalFolder;
- var existsFile = folder.GetFileAsync(filePath).GetResults();
- return existsFile != null;
+ // GetFile returns an exception if the file doesn't exist, so we catch it here and return the boolean.
+ try
+ {
+ var existsFile = folder.GetFileAsync(filePath).GetAwaiter().GetResult();
+ return existsFile != null;
+ }
+ catch
+ {
+ return false;
+ }
#else
// return A new file with read/write access.
return File.Exists(filePath);
@@ -319,7 +341,7 @@ public string[] GetDirectoryNames ()
{
#if WINDOWS_STOREAPP
var folder = ApplicationData.Current.LocalFolder;
- var results = folder.GetFoldersAsync().GetResults();
+ var results = folder.GetFoldersAsync().AsTask().GetAwaiter().GetResult();
return results.Select<StorageFolder, string>(e => e.Name).ToArray();
#else
return Directory.GetDirectories(_storagePath);
@@ -344,7 +366,7 @@ public string[] GetFileNames ()
{
#if WINDOWS_STOREAPP
var folder = ApplicationData.Current.LocalFolder;
- var results = folder.GetFilesAsync().GetResults();
+ var results = folder.GetFilesAsync().AsTask().GetAwaiter().GetResult();
return results.Select<StorageFile, string>(e => e.Name).ToArray();
#else
return Directory.GetFiles(_storagePath);
@@ -365,7 +387,7 @@ public string[] GetFileNames (string searchPattern)
var folder = ApplicationData.Current.LocalFolder;
var options = new QueryOptions( CommonFileQuery.DefaultQuery, new [] { searchPattern } );
var query = folder.CreateFileQueryWithOptions(options);
- var files = query.GetFilesAsync().GetResults();
+ var files = query.GetFilesAsync().AsTask().GetAwaiter().GetResult();
return files.Select<StorageFile, string>(e => e.Name).ToArray();
#else
return Directory.GetFiles(_storagePath, searchPattern);
@@ -420,10 +442,14 @@ public Stream OpenFile (string file, FileMode fileMode, FileAccess fileAccess, F
}
else if (fileMode == FileMode.OpenOrCreate)
{
- if ( fileAccess == FileAccess.Read )
+ if (fileAccess == FileAccess.Read)
return folder.OpenStreamForReadAsync(filePath).GetAwaiter().GetResult();
else
- return folder.OpenStreamForWriteAsync(filePath, CreationCollisionOption.OpenIfExists).GetAwaiter().GetResult();
+ {
+ // Not using OpenStreamForReadAsync because the stream position is placed at the end of the file, instead of the beginning
+ var f = folder.CreateFileAsync(filePath, CreationCollisionOption.OpenIfExists).AsTask().GetAwaiter().GetResult();
+ return f.OpenAsync(FileAccessMode.ReadWrite).AsTask().GetAwaiter().GetResult().AsStream();
+ }
}
else if (fileMode == FileMode.Truncate)
{
@@ -432,7 +458,10 @@ public Stream OpenFile (string file, FileMode fileMode, FileAccess fileAccess, F
else
{
//if (fileMode == FileMode.Append)
- return folder.OpenStreamForWriteAsync(filePath, CreationCollisionOption.OpenIfExists).GetAwaiter().GetResult();
+ // Not using OpenStreamForReadAsync because the stream position is placed at the end of the file, instead of the beginning
+ folder.CreateFileAsync(filePath, CreationCollisionOption.OpenIfExists).AsTask().GetAwaiter().GetResult().OpenAsync(FileAccessMode.ReadWrite).AsTask().GetAwaiter().GetResult().AsStream();
+ var f = folder.CreateFileAsync(filePath, CreationCollisionOption.OpenIfExists).AsTask().GetAwaiter().GetResult();
+ return f.OpenAsync(FileAccessMode.ReadWrite).AsTask().GetAwaiter().GetResult().AsStream();
}
#else
return File.Open(filePath, fileMode, fileAccess, fileShare);
View
45 MonoGame.Framework/Storage/StorageDevice.cs
@@ -204,6 +204,12 @@ string GetDevicePath
/// </summary>
public static event EventHandler<EventArgs> DeviceChanged;
+#if WINRT
+ // Dirty trick to avoid the need to get the delegate from the IAsyncResult (can't be done in WinRT)
+ static Delegate showDelegate;
+ static Delegate containerDelegate;
+#endif
+
// Summary:
// Begins the process for opening a StorageContainer containing any files for
// the specified title.
@@ -228,7 +234,10 @@ private IAsyncResult OpenContainer (string displayName, AsyncCallback callback,
{
try {
OpenContainerAsynchronous AsynchronousOpen = new OpenContainerAsynchronous (Open);
- return AsynchronousOpen.BeginInvoke (displayName, callback, state);
+#if WINRT
+ containerDelegate = AsynchronousOpen;
+#endif
+ return AsynchronousOpen.BeginInvoke (displayName, callback, state);
} finally {
}
}
@@ -301,8 +310,13 @@ public static IAsyncResult BeginShowSelector (PlayerIndex player, AsyncCallback
public static IAsyncResult BeginShowSelector (int sizeInBytes, int directoryCount, AsyncCallback callback, object state)
{
var del = new ShowSelectorAsynchronousShowNoPlayer (Show);
+
+#if WINRT
+ showDelegate = del;
+#endif
return del.BeginInvoke(sizeInBytes, directoryCount, callback, state);
}
+
//
// Summary:
@@ -332,8 +346,11 @@ public static IAsyncResult BeginShowSelector (int sizeInBytes, int directoryCoun
public static IAsyncResult BeginShowSelector (PlayerIndex player, int sizeInBytes, int directoryCount, AsyncCallback callback, object state)
{
var del = new ShowSelectorAsynchronousShow (Show);
- return del.BeginInvoke(player, sizeInBytes, directoryCount, callback, state);
- }
+#if WINRT
+ showDelegate = del;
+#endif
+ return del.BeginInvoke(player, sizeInBytes, directoryCount, callback, state);
+ }
// Private method to handle the creation of the StorageDevice
private static StorageDevice Show (PlayerIndex player, int sizeInBytes, int directoryCount)
@@ -370,7 +387,7 @@ public StorageContainer EndOpenContainer (IAsyncResult result)
try {
#if WINRT
// AsyncResult does not exist in WinRT
- var asyncResult = result.AsyncState as OpenContainerAsynchronous;
+ var asyncResult = containerDelegate as OpenContainerAsynchronous;
if (asyncResult != null)
{
// Wait for the WaitHandle to become signaled.
@@ -379,6 +396,7 @@ public StorageContainer EndOpenContainer (IAsyncResult result)
// Call EndInvoke to retrieve the results.
returnValue = asyncResult.EndInvoke(result);
}
+ containerDelegate = null;
#else
// Retrieve the delegate.
AsyncResult asyncResult = result as AsyncResult;
@@ -414,24 +432,26 @@ public StorageContainer EndOpenContainer (IAsyncResult result)
// The IAsyncResult returned from BeginShowSelector.
public static StorageDevice EndShowSelector (IAsyncResult result)
{
-#if WINRT
- throw new NotImplementedException();
-#else
-
-
- // Retrieve the delegate.
- AsyncResult asyncResult = (AsyncResult)result;
if (!result.IsCompleted) {
// Wait for the WaitHandle to become signaled.
try {
result.AsyncWaitHandle.WaitOne ();
} finally {
+#if !WINRT
result.AsyncWaitHandle.Close ();
+#endif
}
}
+#if WINRT
+ var del = showDelegate;
+ showDelegate = null;
+#else
+ // Retrieve the delegate.
+ AsyncResult asyncResult = (AsyncResult)result;
- var del = asyncResult.AsyncDelegate;
+ var del = asyncResult.AsyncDelegate;
+#endif
if (del is ShowSelectorAsynchronousShow)
return (del as ShowSelectorAsynchronousShow).EndInvoke (result);
@@ -439,7 +459,6 @@ public static StorageDevice EndShowSelector (IAsyncResult result)
return (del as ShowSelectorAsynchronousShowNoPlayer).EndInvoke (result);
else
throw new ArgumentException ("result");
-#endif
}
internal static string StorageRoot
Please sign in to comment.
Something went wrong with that request. Please try again.