static GameSettings() { defaultInstance = new GameSettings(); Load(); } // GameSettings(loadSettings)
} // GameSettings(loadSettings) #endregion #region Load /// <summary> /// Load /// </summary> public static void Load() { needSave = false; FileStream file = FileHelper.LoadGameContentFile( SettingsFilename); if (file == null) { // Create new file after quitting needSave = true; return; } // if // If the file is empty, just create a new file with the default // settings. if (file.Length == 0) { // Close the file file.Close(); // But first check if there is maybe a file in the game directory // to load the default game settings from. file = FileHelper.LoadGameContentFile(SettingsFilename); if (file != null) { // Load everything into this class GameSettings loadedGameSettings = (GameSettings)new XmlSerializer(typeof(GameSettings)). Deserialize(file); if (loadedGameSettings != null) defaultInstance = loadedGameSettings; // Close the file file.Close(); } // if (file) // Save user settings file needSave = true; Save(); } // if (file.Length) else { // Else load everything into this class with help of the // XmlSerializer. GameSettings loadedGameSettings = (GameSettings)new XmlSerializer(typeof(GameSettings)). Deserialize(file); if (loadedGameSettings != null) defaultInstance = loadedGameSettings; // Close the file file.Close(); } // else } // Load()
/// <summary> /// Load /// </summary> public static void Load() { bool saveImmediately = false; needSave = false; FileHelper.StorageContainerMRE.WaitOne(); FileHelper.StorageContainerMRE.Reset(); try { //TODO: Use Nick Gravlyn's easy storage? StorageDevice storageDevice = FileHelper.XnaUserDevice; if ((storageDevice != null) && storageDevice.IsConnected) { IAsyncResult async = storageDevice.BeginOpenContainer("RacingGame", null, null); async.AsyncWaitHandle.WaitOne(); using (StorageContainer container = storageDevice.EndOpenContainer(async)) { async.AsyncWaitHandle.Close(); if (container.FileExists(SettingsFilename)) { using (Stream file = container.OpenFile(SettingsFilename, FileMode.Open, FileAccess.Read, FileShare.ReadWrite)) { if (file.Length > 0) { GameSettings loadedGameSettings = (GameSettings)new XmlSerializer( typeof(GameSettings)).Deserialize(file); if (loadedGameSettings != null) defaultInstance = loadedGameSettings; } else { // If the file is empty, just create a new file with the // default settings. needSave = true; saveImmediately = true; } } } else { // Create new file after exiting needSave = true; } } } } catch (Exception exc) { System.Diagnostics.Debug.WriteLine("Settings Load Failure: " + exc.ToString()); } FileHelper.StorageContainerMRE.Set(); if (saveImmediately) { Save(); } }