MrGravity.WorldSelect.CheckForSave C# (CSharp) Method

CheckForSave() public method

Checks if a save file for the game already exists - and loads it if so. Meant to be used solely on XBOX360. Should be used as soon as we know what PlayerIndex the "gamer" is using (IE right after the title screen). Note: if a save game does not exist the constructor for this class should have filled in the default values (0 stars, all but the first locked - on xbox our default xml file should always have the default values - we cannot save information to that file as it is a binary xnb file that can't be changed at run time) Do not call this until we know the PlayerIndex the player is using!
public CheckForSave ( ) : bool
return bool
        public bool CheckForSave()
        {
            IAsyncResult result;
            try
            {
                if (!DeviceSelected && !Guide.IsVisible)
                {
                    StorageDevice.BeginShowSelector(((ControllerControl)_mControls).ControllerIndex, SelectDevice, null);
                    DeviceSelected = true;
                }
            }
            catch (Exception e)
            {
                var errTemp = e.ToString();
                DeviceSelected = false;
                return false;
            }

            if (_device == null || !_device.IsConnected)
            {
            //mDeviceSelected = false;
                return false;
            }

            try
            {
                result = _device.BeginOpenContainer("Mr Gravity", null, null);
                result.AsyncWaitHandle.WaitOne();
                _container = _device.EndOpenContainer(result);
                result.AsyncWaitHandle.Close();
            }
            catch (Exception e)
            {
                var execTemp = e.ToString();
                _device = null;
                if (_container != null)
                {
                    _container.Dispose();
                }
                _container = null;
                DeviceSelected = false;
                return false;
            }

            try
            {
                if (_container.FileExists("LevelList.xml"))
                {
                    var stream = _container.OpenFile("LevelList.xml", FileMode.Open);
                    var serializer = new XmlSerializer(typeof(SaveGameData));
                    var data = (SaveGameData)serializer.Deserialize(stream);
                    stream.Close();
                    var i = 0;
                    foreach (var xLevels in data.SaveData.Elements())
                    {
                        foreach (var xLevelData in xLevels.Elements())
                        {
                            foreach (var xLevel in xLevelData.Elements())
                            {
                                if (xLevel.Name == XmlKeys.Unlocked && xLevel.Value == XmlKeys.True)
                                    _mLevels[i].Unlock();

                                if (xLevel.Name == XmlKeys.Timerstar)
                                    _mLevels[i].SetStar(LevelInfo.StarTypes.Time, Convert.ToInt32(xLevel.Value));

                                if (xLevel.Name == XmlKeys.Collectionstar)
                                    _mLevels[i].SetStar(LevelInfo.StarTypes.Collection, Convert.ToInt32(xLevel.Value));

                                if (xLevel.Name == XmlKeys.Deathstar)
                                    _mLevels[i].SetStar(LevelInfo.StarTypes.Death, Convert.ToInt32(xLevel.Value));

                            }
                            i++;
                        }
                    }

                }
            }
            catch (Exception e)
            {
                var exceTemp = e.ToString();
                if (_container != null)
                {
                    _container.Dispose();
                }
                _container = null;
                _device = null;
                DeviceSelected = false;
                return false;
            }

            if (_device.IsConnected)
            {
                _container.Dispose();
            }
            else
            {
                _device = null;
                _container = null;
                DeviceSelected = false;
            }

            UpdateStarCount();
            return true;
        }