void saveLevel(IAsyncResult result) { SaveLevelData data = new SaveLevelData(); data.objectList = level.entityList; data.currentPosition = level.currentPosition; data.multiSelect = level.multiSelect; data.upTime = level.upTime; data.holdTime = level.holdTime; data.filename = level.levelName; device = StorageDevice.EndShowSelector(result); if (device != null && device.IsConnected) { IAsyncResult r = device.BeginOpenContainer("MyGamesStorage", null, null); result.AsyncWaitHandle.WaitOne(); StorageContainer container = device.EndOpenContainer(r); if (container.FileExists(level.levelName + ".sav")) container.DeleteFile(level.levelName + ".sav"); Stream stream = container.CreateFile(level.levelName + ".sav"); XmlSerializer serializer = new XmlSerializer(typeof(SaveLevelData)); serializer.Serialize(stream, data); stream.Close(); container.Dispose(); result.AsyncWaitHandle.Close(); } }
private void SaveToFile(IAsyncResult result) { device = StorageDevice.EndShowSelector(result); // Open a storage container. IAsyncResult r = device.BeginOpenContainer(containerName, null, null); result.AsyncWaitHandle.WaitOne(); StorageContainer container = device.EndOpenContainer(r); result.AsyncWaitHandle.Close(); // Delete old file and create new one. if (container.FileExists(fileName)) { container.DeleteFile(fileName); } Stream fileStream = container.CreateFile(fileName); // Write data to file. XmlSerializer serializer = new XmlSerializer(typeof(SaveGameData)); serializer.Serialize(fileStream, saveGameData); // Close file. fileStream.Close(); container.Dispose(); }
public void LoadSaveData(StorageDevice device) { result = device.BeginOpenContainer("Storage", null, null); result.AsyncWaitHandle.WaitOne(); StorageContainer container = device.EndOpenContainer(result); result.AsyncWaitHandle.Close(); string filename = "savegame.sav"; // Check to see whether the save exists. if (!container.FileExists(filename)) { // If not, dispose of the container and return. container.Dispose(); return; } //Open file. Stream stream = container.OpenFile(filename, FileMode.Open); XmlSerializer serializer = new XmlSerializer(typeof(SaveGameData)); loadedData = (SaveGameData)serializer.Deserialize(stream); //close file stream.Close(); container.Dispose(); }
/// <summary> /// This method gets the filenames from the universal storage file LbKTileData.sav /// </summary> /// <param name="device"></param> /// <param name="gamer"></param> /// <param name="fileNamesOnly"></param> public static void LoadGame(StorageDevice device, SignedInGamer gamer, bool fileNamesOnly) { // Open a storage container. // name of container is LbK Storage Device IAsyncResult result = device.BeginOpenContainer(gamer.Gamertag, null, null); // Wait for the WaitHandle to become signaled. result.AsyncWaitHandle.WaitOne(); StorageContainer container = device.EndOpenContainer(result); // Close the wait handle. result.AsyncWaitHandle.Close(); string filename = "LbKTileData.sav"; // Check to see whether the save exists. if (!container.FileExists(filename)) { // If not, dispose of the container and return. container.Dispose(); return; } // Open the file. Stream file = container.OpenFile(filename, FileMode.Open); // Read the data from the file. XmlSerializer serializer = new XmlSerializer(typeof(SaveGameData)); SaveGameData data = (SaveGameData)serializer.Deserialize(file); // Close the file. file.Close(); // Dispose the container. container.Dispose(); // Report the data to the console. if (fileNamesOnly) { fileNames = data.Names; } else { position = data.TilePosition; type = data.TileType; objectNumber = data.TileObjectNumber; count = data.TileCount; fileNames = data.Names; } GamePlayScreen.storageDevice = device; // load up game with respective device }
internal static StorageContainer OpenContainer(StorageDevice storageDevice, string saveGameName) { IAsyncResult result = storageDevice.BeginOpenContainer(saveGameName, null, null); // Wait for the WaitHandle to become signaled. result.AsyncWaitHandle.WaitOne(); StorageContainer container = storageDevice.EndOpenContainer(result); // Close the wait handle. result.AsyncWaitHandle.Close(); return container; }
/// <summary> /// This method loads a serialized data object /// from the StorageContainer for this game. /// </summary> /// <param name="device"></param> public static void LoadGame(StorageDevice device, SignedInGamer gamer) { // Open a storage container. // name of container is LbK Storage Device IAsyncResult result = device.BeginOpenContainer("LbK Storage Device", null, null); // Wait for the WaitHandle to become signaled. result.AsyncWaitHandle.WaitOne(); StorageContainer container = device.EndOpenContainer(result); // Close the wait handle. result.AsyncWaitHandle.Close(); string filename = "LbKSavedItems.sav"; // Check to see whether the save exists. if (!container.FileExists(filename)) { // If not, dispose of the container and return. container.Dispose(); nothingLoaded = true; return; } // Open the file. Stream file = container.OpenFile(filename, FileMode.Open); // Read the data from the file. XmlSerializer serializer = new XmlSerializer(typeof(SaveGameData)); SaveGameData data = (SaveGameData)serializer.Deserialize(file); // Close the file. file.Close(); // Dispose the container. container.Dispose(); // Report the data to the console. playerName = data.PlayerName; level = data.Level; score = data.PlayerScore; position = new Vector2(data.playerPosition.X, data.playerPosition.Y); checkPoint = data.CheckPoint; GamePlayScreen.storageDevice = device; // load up game with respective device }
public void loadGame(StorageDevice device) { Console.WriteLine("loading"); IAsyncResult result = device.BeginOpenContainer("Container", null, null); result.AsyncWaitHandle.WaitOne(); StorageContainer container = device.EndOpenContainer(result); result.AsyncWaitHandle.Close(); string filename = "savegame.sav"; if (!container.FileExists(filename)) { container.Dispose(); return; } Stream stream = container.OpenFile(filename, FileMode.Open); XmlSerializer serializer = new XmlSerializer(typeof(SaveGameData)); SaveGameData data = (SaveGameData)serializer.Deserialize(stream); stream.Close(); container.Dispose(); game.getZoneFactory().loadZones(game.getContentHandler(), data.currentPlayerZone); game.getZoneFactory().setCurrentZoneFromNumber(data.currentPlayerZone); game.getPlayer().setZoneLevel(data.currentPlayerLevel); game.getPlayer().setGlobalLocation(data.playerPosition); game.getKeyHandler().getMovementHandler().updateDrawLocations(game.getPlayer(), game.getZoneFactory().getCurrentZone()); game.getPlayer().getStats().setFireEnergy(data.fireEnergy); game.getPlayer().getStats().setCurrentFireEnergy(data.currentFireEnergy); game.getPlayer().getStats().setWaterEnergy(data.waterEnergy); game.getPlayer().getStats().setCurrentWaterEnergy(data.currentWaterEnergy); game.getPlayer().getStats().setNatureEnergy(data.natureEnergy); game.getPlayer().getStats().setCurrentNatureEnergy(data.currentNatureEnergy); Console.WriteLine("Zone number: " + data.currentPlayerZone); Console.WriteLine("Player level: " + data.currentPlayerLevel); Console.WriteLine("Position: " + data.playerPosition); loadRequested = false; Console.WriteLine("load completed"); game.getGameState().setGameState(); game.getKeyHandler().updateKeys(Keyboard.GetState()); }
void LoadFromDevice(IAsyncResult result) { device = StorageDevice.EndShowSelector(result); IAsyncResult r = device.BeginOpenContainer(ContainerName, null, null); result.AsyncWaitHandle.WaitOne(); StorageContainer container = device.EndOpenContainer(r); result.AsyncWaitHandle.Close(); if (container.FileExists(Filename)) { Stream stream = container.OpenFile(Filename, FileMode.Open); XmlSerializer serializer = new XmlSerializer(typeof(SaveGame)); SaveGame SaveData = (SaveGame)serializer.Deserialize(stream); stream.Close(); container.Dispose(); //Update the game based on the save game file } Replace(); }
/// <summary> /// Loads the save when StorageDevice is ready. /// </summary> /// <param name="result">gives the StorageDevice to use</param> void LoadFromDevice(IAsyncResult result) { _device = StorageDevice.EndShowSelector(result); var r = _device.BeginOpenContainer(containerName, null, null); result.AsyncWaitHandle.WaitOne(); var container = _device.EndOpenContainer(r); result.AsyncWaitHandle.Close(); if (!container.FileExists(filename)) return; var stream = container.OpenFile(filename, FileMode.Open); var bfm = new BinaryFormatter(); var saveData = (SaveData)bfm.Deserialize(stream); stream.Close(); container.Dispose(); //Update the game based on the save game file // Hero _inGame.Hero.gold = saveData.HeroData.gold; _inGame.Hero.Position = saveData.HeroData.Position; _inGame.Hero.CurrentMana = saveData.HeroData.CurrentMana; _inGame.Hero.CurrentHitPoints = saveData.HeroData.CurrentHitPoints; _inGame.Hero.MaxHitPoints = saveData.HeroData.MaxHitPoints; _inGame.Hero.MaxMana = saveData.HeroData.MaxMana; _inGame.Hero.exp = saveData.HeroData.exp; _inGame.Hero.level = saveData.HeroData.level; _inGame.Hero.tnl = saveData.HeroData.tnl; // mapGrid _inGame.mapGrid._tiles = saveData.mapgrid; _inGame.mapGrid.Initialize(); // liveGrid _inGame._game.Components.Remove(_inGame.liveGrid); _inGame.liveGrid = new LiveGrid(_inGame._game, ref _inGame.mapGrid); _inGame._game.Components.Add(_inGame.liveGrid); // monsters UnserializeMonsters(saveData.MonstersData); // missiles UnserializeMissiles(saveData.MissilesData); }
void save(IAsyncResult result) { device = StorageDevice.EndShowSelector(result); if (device != null && device.IsConnected) { SaveLevelNames data = new SaveLevelNames(); data.levelName = levelNames; data.timeScore = timeScores; data.hitsScore = hitsScores; IAsyncResult r = device.BeginOpenContainer("MyGamesStorage", null, null); StorageContainer container = device.EndOpenContainer(r); if (container.FileExists("ListofScores.sav")) container.DeleteFile("ListofScores.sav"); Stream stream = container.CreateFile("ListofScores.sav"); XmlSerializer serializer = new XmlSerializer(typeof(SaveLevelNames)); serializer.Serialize(stream, data); stream.Close(); container.Dispose(); } }
public void DoLoadGame(StorageDevice device) { // Open a storage container. IAsyncResult result = device.BeginOpenContainer("StorageDemo", null, null); // Wait for the WaitHandle to become signaled. result.AsyncWaitHandle.WaitOne(); StorageContainer container = device.EndOpenContainer(result); // Close the wait handle. result.AsyncWaitHandle.Close(); string filename = "testlevel.txt"; // Check to see whether the save exists. if (!container.FileExists(filename)) { // If not, dispose of the container and return. container.Dispose(); return; } // Open the file. Stream stream = container.OpenFile(filename, FileMode.Open); // Read the data from the file. XmlSerializer serializer = new XmlSerializer(typeof(testidata)); testidata = (int)serializer.Deserialize(stream); // Close the file. stream.Close(); // Dispose the container. container.Dispose(); // Report the data to the console. Debug.WriteLine("Leveldata: " + testidata); }
private void SaveToDevice(IAsyncResult result) { device = StorageDevice.EndShowSelector(result); if (device != null && device.IsConnected) { SaveGame SaveData = new SaveGame() { IsFirstTime = false, }; IAsyncResult r = device.BeginOpenContainer(ContainerName,null,null); result.AsyncWaitHandle.WaitOne(); StorageContainer container = device.EndOpenContainer(r); if(container.FileExists(Filename)) container.DeleteFile(Filename); Stream stream = container.CreateFile(Filename); XmlSerializer serializer = new XmlSerializer(typeof(SaveGame)); serializer.Serialize(stream,SaveData); stream.Close(); container.Dispose(); result.AsyncWaitHandle.Close(); } }
/// <summary> /// Ładowanie listy zapisanych stanów gry dla danego playera /// </summary> /// <param name="result">obiekt typu IAsyncResult dający dostęp do XNA Storage</param> /// <param name="gameLevel">poziom gry</param> /// <returns></returns> public static GameStateData LoadGameState(IAsyncResult result,string playerName) { filename = playerName; device = StorageDevice.EndShowSelector(result); // Open a storage container. IAsyncResult result2 = device.BeginOpenContainer(containerName, null, null); // Wait for the WaitHandle to become signaled. result.AsyncWaitHandle.WaitOne(); StorageContainer container = device.EndOpenContainer(result2); // Close the wait handle. result.AsyncWaitHandle.Close(); // Check to see whether the save exists. if (!container.FileExists(filename)) { container.Dispose(); return new GameStateData(0); } // Open the file. Stream stream = container.OpenFile(filename, FileMode.Open); // Read the data from the file. XmlSerializer serializer = new XmlSerializer(typeof(GameStateData)); GameStateData data = (GameStateData)serializer.Deserialize(stream); // Close the file. stream.Close(); // Dispose the container. container.Dispose(); //Return SettingsData return data; }
void loadLevelName(IAsyncResult result) { device = StorageDevice.EndShowSelector(result); IAsyncResult r = device.BeginOpenContainer("MyGamesStorage", null, null); StorageContainer container = device.EndOpenContainer(r); if (container.FileExists("ListofScores.sav")) { Stream stream = container.OpenFile("ListofScores.sav", FileMode.Open); XmlSerializer serializer = new XmlSerializer(typeof(SaveLevelNames)); stream.Seek(0, SeekOrigin.Begin); SaveLevelNames data = (SaveLevelNames)serializer.Deserialize(stream); levelNames = data.levelName; timeScores = data.timeScore; hitsScores = data.hitsScore; stream.Close(); container.Dispose(); } else { check = false; } }
void loadLevel(IAsyncResult result) { device = StorageDevice.EndShowSelector(result); IAsyncResult r = device.BeginOpenContainer("MyGamesStorage", null, null); result.AsyncWaitHandle.WaitOne(); StorageContainer container = device.EndOpenContainer(r); result.AsyncWaitHandle.Close(); if (container.FileExists(filename)) { Stream stream = container.OpenFile(filename, FileMode.Open); XmlSerializer serializer = new XmlSerializer(typeof(SaveLevelData)); SaveLevelData data = (SaveLevelData)serializer.Deserialize(stream); level.entityList = data.objectList; level.currentPosition = data.currentPosition; level.multiSelect = data.multiSelect; level.holdTime = data.holdTime; level.upTime = data.upTime; level.levelName = data.filename; stream.Close(); container.Dispose(); } }
/// <summary> /// Ładowanie listy najlepszych wyników dla danego poziomu /// </summary> /// <param name="result">obiekt typu IAsyncResult dający dostęp do XNA Storage</param> /// <param name="gameLevel">poziom gry</param> /// <returns></returns> public static HighScoreData LoadHighScores(IAsyncResult result, int gameLevel) { device = StorageDevice.EndShowSelector(result); // Open a storage container. IAsyncResult result2 = device.BeginOpenContainer(containerName, null, null); // Wait for the WaitHandle to become signaled. result.AsyncWaitHandle.WaitOne(); StorageContainer container = device.EndOpenContainer(result2); // Close the wait handle. result.AsyncWaitHandle.Close(); // Check to see whether the save exists. if (!container.FileExists(filename[gameLevel - 1])) { container.Dispose(); return new HighScoreData(0); } // Open the file. Stream stream = container.OpenFile(filename[gameLevel - 1], FileMode.Open); // Read the data from the file. XmlSerializer serializer = new XmlSerializer(typeof(HighScoreData)); HighScoreData data = (HighScoreData)serializer.Deserialize(stream); // Close the file. stream.Close(); // Dispose the container. container.Dispose(); //Return HighScoreData return data; }
private void LoadFromStorage(StorageDevice device) { IAsyncResult result = device.BeginOpenContainer("SaveData", null, null); result.AsyncWaitHandle.WaitOne(); StorageContainer container = device.EndOpenContainer(result); result.AsyncWaitHandle.Close(); string filename = "Save" + selectedStorage.ToString("D3") + ".sav"; if (!container.FileExists(filename)) return; Stream file = container.OpenFile(filename, FileMode.Open); XmlSerializer serializer = new XmlSerializer(typeof(SaveData)); save = (SaveData)serializer.Deserialize(file); file.Close(); container.Dispose(); }
private StorageContainer GetContainer(StorageDevice storageDevice) { //open storage container IAsyncResult result = storageDevice.BeginOpenContainer("Juntoki", null, null); // wait for the WaitHandle to be signaled result.AsyncWaitHandle.WaitOne(); StorageContainer returnContainer = storageDevice.EndOpenContainer(result); // kill the wait handle result.AsyncWaitHandle.Close(); return returnContainer; }
private void SaveToStorage(StorageDevice device) { IAsyncResult result = device.BeginOpenContainer("SaveData", null, null); result.AsyncWaitHandle.WaitOne(); StorageContainer container = device.EndOpenContainer(result); result.AsyncWaitHandle.Close(); string filename = "Save" + selectedStorage.ToString("D3") + ".sav"; Stream file; if (container.FileExists(filename)) container.DeleteFile(filename); file = container.CreateFile(filename); XmlSerializer serializer = new XmlSerializer(typeof(SaveData)); serializer.Serialize(file, save); file.Close(); 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 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 illustrates how to rename files. It presumes /// that demobinary.sav has been created. /// </summary> /// <param name="device"></param> private static void DoRename(StorageDevice device) { IAsyncResult result = device.BeginOpenContainer("StorageDemo", null, null); // Wait for the WaitHandle to become signaled. result.AsyncWaitHandle.WaitOne(); StorageContainer container = device.EndOpenContainer(result); // Close the wait handle. result.AsyncWaitHandle.Close(); // Add the container path to our file name. string oldfilename = "demobinary.sav"; string newfilename = "renamebinary.sav"; if (container.FileExists(oldfilename) && !container.FileExists(newfilename)) { Stream oldfile = container.OpenFile(oldfilename, FileMode.Open); Stream newfile = container.CreateFile(newfilename); //oldfile.CopyTo(newfile); oldfile.Close(); newfile.Close(); container.DeleteFile(oldfilename); } // Dispose the container, to commit the change. container.Dispose(); }
/// <summary> /// This method serializes a data object into /// the StorageContainer for this game. /// </summary> /// <param name="device"></param> private static void DoSaveGame(StorageDevice device) { // Create the data to save. SaveGameData data = new SaveGameData(); data.PlayerName = "Hiro"; data.AvatarPosition = new Vector2(360, 360); data.Level = 11; data.Score = 4200; // Open a storage container. IAsyncResult result = device.BeginOpenContainer("StorageDemo", null, null); // Wait for the WaitHandle to become signaled. result.AsyncWaitHandle.WaitOne(); StorageContainer container = device.EndOpenContainer(result); // Close the wait handle. result.AsyncWaitHandle.Close(); string filename = "savegame.sav"; // Check to see whether the save exists. if (container.FileExists(filename)) // Delete it so that we can create one fresh. container.DeleteFile(filename); // Create the file. Stream stream = container.CreateFile(filename); // Convert the object to XML data and put it in the stream. XmlSerializer serializer = new XmlSerializer(typeof(SaveGameData)); serializer.Serialize(stream, data); // Close the file. stream.Close(); // Dispose the container, to commit changes. container.Dispose(); }
/// <summary> /// This method illustrates how to open a file. It presumes /// that demobinary.sav has been created. /// </summary> /// <param name="device"></param> private static void DoOpen(StorageDevice device) { IAsyncResult result = device.BeginOpenContainer("StorageDemo", null, null); // Wait for the WaitHandle to become signaled. result.AsyncWaitHandle.WaitOne(); StorageContainer container = device.EndOpenContainer(result); // Close the wait handle. result.AsyncWaitHandle.Close(); // Add the container path to our file name. string filename = "demobinary.sav"; Stream file = container.OpenFile(filename, FileMode.Open); file.Close(); // Dispose the container. container.Dispose(); }
/// <summary> /// This method loads a serialized data object /// from the StorageContainer for this game. /// </summary> /// <param name="device"></param> private static void DoLoadGame(StorageDevice device) { // Open a storage container. IAsyncResult result = device.BeginOpenContainer("StorageDemo", null, null); // Wait for the WaitHandle to become signaled. result.AsyncWaitHandle.WaitOne(); StorageContainer container = device.EndOpenContainer(result); // Close the wait handle. result.AsyncWaitHandle.Close(); string filename = "savegame.sav"; // Check to see whether the save exists. if (!container.FileExists(filename)) { // If not, dispose of the container and return. container.Dispose(); return; } // Open the file. Stream stream = container.OpenFile(filename, FileMode.Open); // Read the data from the file. XmlSerializer serializer = new XmlSerializer(typeof(SaveGameData)); SaveGameData data = (SaveGameData)serializer.Deserialize(stream); // Close the file. stream.Close(); // Dispose the container. container.Dispose(); // Report the data to the console. Debug.WriteLine("Name: " + data.PlayerName); Debug.WriteLine("Level: " + data.Level.ToString()); Debug.WriteLine("Score: " + data.Score.ToString()); Debug.WriteLine("Position: " + data.AvatarPosition.ToString()); }
/// <summary> /// This method illustrates how to enumerate files in a /// StorageContainer. /// </summary> /// <param name="device"></param> private static void DoEnumerate(StorageDevice device) { IAsyncResult result = device.BeginOpenContainer("StorageDemo", null, null); // Wait for the WaitHandle to become signaled. result.AsyncWaitHandle.WaitOne(); StorageContainer container = device.EndOpenContainer(result); // Close the wait handle. result.AsyncWaitHandle.Close(); string[] FileList = container.GetFileNames(); foreach (string filename in FileList) { Console.WriteLine(filename); } // Dispose the container. container.Dispose(); }
/// <summary> /// This method deletes a file previously created by this demo. /// </summary> /// <param name="device"></param> private static void DoDelete(StorageDevice device) { IAsyncResult result = device.BeginOpenContainer("StorageDemo", null, null); // Wait for the WaitHandle to become signaled. result.AsyncWaitHandle.WaitOne(); StorageContainer container = device.EndOpenContainer(result); // Close the wait handle. result.AsyncWaitHandle.Close(); // Add the container path to our file name. string filename = "demobinary.sav"; if (container.FileExists(filename)) { container.DeleteFile(filename); } // Dispose the container, to commit the change. container.Dispose(); }
public void LoadFromDevice(IAsyncResult result) { device = StorageDevice.EndShowSelector(result); IAsyncResult r = device.BeginOpenContainer(containerName, null, null); result.AsyncWaitHandle.WaitOne(); StorageContainer container = device.EndOpenContainer(r); result.AsyncWaitHandle.Close(); if (container.FileExists(filename)) { Stream stream = container.OpenFile(filename, FileMode.Open); XmlSerializer serializer = new XmlSerializer(typeof(SaveGame)); SaveGame SaveData = (SaveGame)serializer.Deserialize(stream); stream.Close(); container.Dispose(); //Update the game based on the save game file skin = SaveData.skinSave; skin.mapTexture = Content.Load<Texture2D>(skin.background); soundON = SaveData.soundONSave; showFps = SaveData.showFPSSave; difficulty = SaveData.difficultySave; classicMode = SaveData.classicModeSave; } }