/// <summary> /// This method serializes the master save file into /// the StorageContainer for this game. /// By: Joseph Shaw /// </summary> /// <param name="device"></param> public static void SaveMasterFile(StorageDevice device, MasterSaveFile masterSaveFile) { // Open a storage container. IAsyncResult result = device.BeginOpenContainer("DungeonCrawler", null, null); // Wait for the WaitHandle to become signaled. result.AsyncWaitHandle.WaitOne(); StorageContainer container = device.EndOpenContainer(result); // Close the wait handle. result.AsyncWaitHandle.Close(); // Check to see whether the save exists. if (container.FileExists(masterFileName)) { // Delete it so that we can create one fresh. container.DeleteFile(masterFileName); } // Create the file. Stream stream = container.CreateFile(masterFileName); // Create the BinaryFormatter BinaryFormatter binaryFormatter = new BinaryFormatter(); // Convert the file to binary and save it binaryFormatter.Serialize(stream, masterSaveFile); // Close the file. stream.Close(); // Dispose the container, to commit changes. container.Dispose(); }
/// <summary> /// This method loads the master save file object /// from the StorageContainer for this game. /// By: Joseph Shaw /// </summary> /// <param name="device">The device we are loading from</param> public static MasterSaveFile GetMasterSaveFile(StorageDevice device) { // Open a storage container. IAsyncResult result = device.BeginOpenContainer("DungeonCrawler", null, null); // Wait for the WaitHandle to become signaled. result.AsyncWaitHandle.WaitOne(); StorageContainer container = device.EndOpenContainer(result); // Close the wait handle. result.AsyncWaitHandle.Close(); // Check to see whether the save exists. if (!container.FileExists(masterFileName)) { // If not, dispose of the container and return a new MasterSaveFile. container.Dispose(); return(new MasterSaveFile()); } // Open the file. Stream stream = container.OpenFile(masterFileName, FileMode.Open); // Read the data from the file. BinaryFormatter binaryFormatter = new BinaryFormatter(); MasterSaveFile masterSaveFile = (MasterSaveFile)binaryFormatter.Deserialize(stream); // Close the file. stream.Close(); // Dispose the container. container.Dispose(); return(masterSaveFile); }
/// <summary> /// This method serializes the master save file into /// the StorageContainer for this game. /// By: Joseph Shaw /// </summary> /// <param name="device"></param> public static void SaveMasterFile(StorageDevice device, MasterSaveFile masterSaveFile) { // Open a storage container. IAsyncResult result = device.BeginOpenContainer("DungeonCrawler", null, null); // Wait for the WaitHandle to become signaled. result.AsyncWaitHandle.WaitOne(); StorageContainer container = device.EndOpenContainer(result); // Close the wait handle. result.AsyncWaitHandle.Close(); // Check to see whether the save exists. if (container.FileExists(masterFileName)) { // Delete it so that we can create one fresh. container.DeleteFile(masterFileName); } // Create the file. Stream stream = container.CreateFile(masterFileName); // Create the BinaryFormatter BinaryFormatter binaryFormatter = new BinaryFormatter(); // Convert the file to binary and save it binaryFormatter.Serialize(stream, masterSaveFile); // Close the file. stream.Close(); // Dispose the container, to commit changes. container.Dispose(); }
/// <summary> /// This method deletes a game save object from /// the StorageContainer for this game. /// By: Joseph Shaw /// </summary> /// <param name="device">The device we are deleting from</param> /// <param name="gameData">The game data we are deleting</param> public static void DoDeleteGame(StorageDevice device, CharacterSaveFile gameData) { // Open a storage container. IAsyncResult result = device.BeginOpenContainer("DungeonCrawler", null, null); // Wait for the WaitHandle to become signaled. result.AsyncWaitHandle.WaitOne(); StorageContainer container = device.EndOpenContainer(result); // Close the wait handle. result.AsyncWaitHandle.Close(); // Create the BinaryFormatter here in-case we have to create a new save BinaryFormatter binaryFormatter = new BinaryFormatter(); Stream stream; // Use this to tell us if this is a new save bool fileExists = false; // Check to see whether the save exists. if (container.FileExists(gameData.fileName)) { // Delete it so that we can create one fresh. container.DeleteFile(gameData.fileName); fileExists = true; } // Create/Update the charPreview to reflect the current player's level CharacterSaveFilePreview charPreview; MasterSaveFile masterSaveFile = GetMasterSaveFile(device); if (fileExists) { charPreview = masterSaveFile.charFiles.Find(charFile => charFile.CharacterSaveFile == gameData.fileName); masterSaveFile.charFiles.Remove(charPreview); } if (masterSaveFile.charFiles != null) { // Sort the list by the file name and rename the files to eliminate gaps masterSaveFile.charFiles.OrderBy(s1 => s1.CharacterSaveFile); masterSaveFile.charFiles = RenameFiles(device, masterSaveFile.charFiles); } else { masterSaveFile.charFiles = new List <CharacterSaveFilePreview>(); } SaveMasterFile(device, masterSaveFile); // Create the file. stream = container.CreateFile(gameData.fileName); // Convert the file to binary and save it binaryFormatter.Serialize(stream, gameData); // Close the file. stream.Close(); // Dispose the container, to commit changes. container.Dispose(); }