/// <summary> /// This method loads a game save object /// from the StorageContainer for this game. /// By: Joseph Shaw /// </summary> /// <param name="device">The device we are loading from</param> /// <param name="fileName">The file we are loading</param> public static CharacterSaveFile DoLoadGame(StorageDevice device, string fileName) { // 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(fileName)) { // If not, dispose of the container and return. container.Dispose(); return(new CharacterSaveFile()); } // Open the file. Stream stream = container.OpenFile(fileName, FileMode.Open); // Read the data from the file. BinaryFormatter binaryFormatter = new BinaryFormatter(); CharacterSaveFile characterSaveData = (CharacterSaveFile)binaryFormatter.Deserialize(stream); // Close the file. stream.Close(); // Dispose the container. container.Dispose(); if (characterSaveData.quests == null) { characterSaveData.quests = new List <Quest>(); } DungeonCrawlerGame.game.Quests = characterSaveData.quests; return(characterSaveData); }
/// <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 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(); }