/// <summary> /// This method serializes a game save object into /// the StorageContainer for this game. /// By: Joseph Shaw /// </summary> /// <param name="device">The device we are saving to</param> /// <param name="gameData">The game data we are saving</param> /// <param name="updatePreview">Whether we are updating the preview, false only in delete game functionality</param> public static void DoSaveGame(StorageDevice device, CharacterSaveFile gameData, bool updatePreview) { // 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 if (updatePreview) { CharacterSaveFilePreview charPreview; MasterSaveFile masterSaveFile = GetMasterSaveFile(device); if (fileExists) { charPreview = masterSaveFile.charFiles.Find(charFile => charFile.CharacterSaveFile == gameData.fileName); masterSaveFile.charFiles.Remove(charPreview); } charPreview = new CharacterSaveFilePreview(); charPreview.CharacterSaveFile = gameData.fileName; charPreview.charSprite = gameData.charSprite; charPreview.characterType = gameData.characterType; charPreview.Level = gameData.level; if (masterSaveFile.charFiles == null) masterSaveFile.charFiles = new List<CharacterSaveFilePreview>(); masterSaveFile.charFiles.Add(charPreview); // Sort the list by the file name and resave it masterSaveFile.charFiles.OrderBy(s1 => s1.CharacterSaveFile); 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(); }
/// <summary> /// This method serializes a game save object into /// the StorageContainer for this game. /// By: Joseph Shaw /// </summary> /// <param name="device">The device we are saving to</param> /// <param name="gameData">The game data we are saving</param> /// <param name="updatePreview">Whether we are updating the preview, false only in delete game functionality</param> public static void DoSaveGame(StorageDevice device, CharacterSaveFile gameData, bool updatePreview) { // 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 if (updatePreview) { CharacterSaveFilePreview charPreview; MasterSaveFile masterSaveFile = GetMasterSaveFile(device); if (fileExists) { charPreview = masterSaveFile.charFiles.Find(charFile => charFile.CharacterSaveFile == gameData.fileName); masterSaveFile.charFiles.Remove(charPreview); } charPreview = new CharacterSaveFilePreview(); charPreview.CharacterSaveFile = gameData.fileName; charPreview.charSprite = gameData.charSprite; charPreview.characterType = gameData.characterType; charPreview.Level = gameData.level; if (masterSaveFile.charFiles == null) { masterSaveFile.charFiles = new List <CharacterSaveFilePreview>(); } masterSaveFile.charFiles.Add(charPreview); // Sort the list by the file name and resave it masterSaveFile.charFiles.OrderBy(s1 => s1.CharacterSaveFile); 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(); }