/// <summary>
 /// Creates an empty savegame. Does not switch it to the current savegame.
 /// Warning: Will overwrite any save game in the current slot.
 /// </summary>
 /// <param name="saveGameIndex">Valid values are 1, 2, 3.</param>
 public void CreateNewSaveGame(int saveGameIndex)
 {
 switch(saveGameIndex)
 {
     case 1: SaveGame1 = SaveData.GetEmptyData(saveGameIndex);
         SaveGame1.BeenCreated = true;
         break;
     case 2: SaveGame2 = SaveData.GetEmptyData(saveGameIndex);
         SaveGame2.BeenCreated = true;
         break;
     case 3: SaveGame3 = SaveData.GetEmptyData(saveGameIndex);
         SaveGame3.BeenCreated = true;
         break;
 }
 }
 /// <summary>
 /// Deletes a file, permanently. Auto-saves.
 /// </summary>
 /// <param name="i">Valid values are 1, 2, 3.</param>
 public void Delete(int i)
 {
 switch(i)
 {
     case 1: SaveGame1 = SaveData.GetEmptyData(i);
         break;
     case 2: SaveGame2 = SaveData.GetEmptyData(i);
         break;
     case 3: SaveGame3 = SaveData.GetEmptyData(i);
         break;
     default: throw new ArgumentOutOfRangeException("No such save file.");
 }
 OnSaveDeleted(i);
 Save(true);
 }
        /// <summary>
        /// Loads the options from the XML.
        /// </summary>
        public void Load()
        {
            Program.Cutter.WriteToLog(this, "Attempting to load game data.");
            #if XBOX360
            try
            {
                if(container == null)
                {
                    if(initializing)
                        throw new FileNotFoundException("Set Manager to null and continue along.");
                    int? devSelRes;
                    do
                    {
                        devSelRes = SimpleMessageBox.ShowMessageBox("Save", "No storage device is selected. Please select a device.",
                            new string[] { "Select a Device", "Continue Without Saving" }, 0, MessageBoxIcon.Alert);
                        switch(devSelRes)
                        {
                            case -1: // Fall through
                            case 1: returnOptionsToDefault();
                                return;
                            case 0: StorageDevice.BeginShowSelector(storageSelectEnd, null);
                                break;
                        }
                    } while(devSelRes == null);
                }

            try
            {
            FileNotFound:
                if(!container.FileExists(filename))
                {
                    if(!container.FileExists(backupName))
                    {
                        if(initializing)
                            throw new FileNotFoundException("Set Manager to null and continue along.");
                        int? fileResult;
                        do
                        {
                            fileResult = SimpleMessageBox.ShowMessageBox("No File Found", "No options file was found on the storage device. Would you like to create a new file or select a new device?",
                                new string[] { "Create File", "Select New Device", "Exit Without Loading" }, 0, MessageBoxIcon.Alert);
                            switch(fileResult)
                            {
                                case 0: New();
                                    return;
                                case 1: result = StorageDevice.BeginShowSelector(null, null);
                                    result.AsyncWaitHandle.WaitOne();
                                    device = StorageDevice.EndShowSelector(result);
                                    result.AsyncWaitHandle.Close();

                                    result = device.BeginOpenContainer("Accelerated Delivery", null, null);
                                    result.AsyncWaitHandle.WaitOne();
                                    container = device.EndOpenContainer(result);
                                    result.AsyncWaitHandle.Close();
                                    goto FileNotFound;
                                case -1: // Fall through
                                case 2: returnOptionsToDefault();
                                    return;
                            }
                        } while(fileResult == null);
                    }
                }

                currentStream = container.OpenFile(filename, FileMode.Open);
                HandleEncryption(ref currentStream);
                file = (SaveFile)saveDataSerializer.Deserialize(currentStream);
                currentStream.Close();
                container.Dispose();
            }
            catch
            {
                try
                {
                    if(!initializing)
                    {
                        int? lalala;
                        do
                        {
                            lalala = SimpleMessageBox.ShowMessageBox("Warning", "The save file was missing or corrupted, but a backup file was found. Loading backup file.", new string[] { "Okay" }, 0, MessageBoxIcon.Alert);
                        } while(lalala == null);
                    }
                    backupStream = container.OpenFile(backupName, FileMode.Open);
                    HandleEncryption(ref backupStream);
                    file = (SaveFile)saveDataSerializer.Deserialize(backupStream);
                    currentStream = container.CreateFile(filename);
                    backupStream.Seek(0, SeekOrigin.Begin);
                    backupStream.CopyTo(currentStream);
                    currentStream.Close();
                    backupStream.Close();
                    container.Dispose();
                }
                catch(Exception ex)
                {
                    if(backupStream != null)
                        backupStream.Close();
                    if(currentStream != null)
                        currentStream.Close();
                    if(container != null)
                        container.Dispose();
                    throw ex;
                }
            }
            #elif WINDOWS
            // Attempt to load primary save
            try
            {
                if(!File.Exists(filename))
                {
                    if(!File.Exists(backupName))
                        New();
                }
                currentStream = File.Open(filename, FileMode.Open);
                HandleEncryption(ref currentStream);
                try
                {
                    file = (SaveFile)saveDataSerializer.Deserialize(currentStream);
                }
                catch
                {
                    currentStream.Seek(0, SeekOrigin.Begin);
                    HandleEncryption(ref currentStream);
                    file = (SaveFile)saveDataSerializer.Deserialize(currentStream);
                }
                // at this point loading was successful
                // it will always be safe to copy to backup at this point
                currentStream.Seek(0, SeekOrigin.Begin);
                backupStream = File.Open(backupName, FileMode.Create);
                currentStream.CopyTo(backupStream);
                backupStream.Close();
                //HandleEncryption(ref currentStream); // re-encrypts the file
                currentStream.Close();
                SaveGame1 = file.save1;
                SaveGame2 = file.save2;
                SaveGame3 = file.save3;
                currentSave = file.currentSaveGame;
                SuccessfulLoad = true;
                Program.Cutter.WriteToLog(this, "Load successful.");
            }
            catch(Exception ex)
            {
                Program.Cutter.WriteToLog(this, "An error occurred while loading game data. Attempting to load backup save.");
                Program.Cutter.WriteExceptionToLog(ex, false);
                try
                {
                    FileStream dump = File.Open(Program.SavePath + "dump.sav", FileMode.Create);
                    currentStream.Seek(0, SeekOrigin.Begin);
                    currentStream.CopyTo(dump);
                    dump.Close();
                }
                catch(Exception exc)
                {
                    Program.Cutter.WriteToLog(this, "Could not dump save file.");
                    Program.Cutter.WriteExceptionToLog(exc, false);
                }
                finally
                {
                    // Attempt to load backup save and copy data to current save

                    try
                    {
                        if(File.Exists(backupName))
                        {
                            backupStream = File.Open(backupName, FileMode.Open);
                            HandleEncryption(ref backupStream);
                            file = (SaveFile)saveDataSerializer.Deserialize(backupStream);
                            currentStream = File.Open(filename, FileMode.Create);
                            backupStream.Seek(0, SeekOrigin.Begin);
                            backupStream.CopyTo(currentStream);
                            currentStream.Close();
                            backupStream.Close();
                            SaveGame1 = file.save1;
                            SaveGame2 = file.save2;
                            SaveGame3 = file.save3;
                            currentSave = file.currentSaveGame;
                            SuccessfulLoad = true;
                            Program.Cutter.WriteToLog(this, "Load successful.");
                        }
                        else
                        {
                            Program.Cutter.WriteToLog(this, "Backup save was not there. Load failed.");
                            DialogResult d = MessageBox.Show("The save file is corrupt. Is it okay to create a new save file? This will erase your progress.", "Error!", MessageBoxButtons.YesNo);
                            if(d == DialogResult.Yes)
                                New();
                            else
                                Program.Game.Exit();
                        }
                    }
                    catch
                    {
                        // Primary and backups are faulty, throw error
                        if(backupStream != null)
                            backupStream.Close();
                        if(currentStream != null)
                            currentStream.Close();

                        SuccessfulLoad = false;
                        Program.Cutter.WriteToLog(this, "Primary and backup saves are faulty or missing. Load failed. Error is:");
                        Program.Cutter.WriteExceptionToLog(ex, false);

                        DialogResult d = MessageBox.Show("The save file is corrupt. Is it okay to create a new save file? This will erase your progress.", "Error!", MessageBoxButtons.YesNo);
                        if(d == DialogResult.Yes)
                            New();
                        else
                            Program.Game.Exit();
                    }
                }
            }
            #endif
            }