Exemplo n.º 1
0
        public IEnumerable <IResult> DoImportWorld()
        {
            string fileName = null;
            var    platform = Platform.Invalid;

            foreach (var result in this.SaveLoad.OpenFile(s => fileName = s, p => platform = p))
            {
                yield return(result);
            }

            if (fileName == null)
            {
                yield break;
            }

            FileFormats.SaveFile saveFile = null;

            yield return(new DelegateResult(() =>
            {
                using (var input = File.OpenRead(fileName))
                {
                    saveFile = FileFormats.SaveFile.Deserialize(input,
                                                                platform,
                                                                FileFormats.SaveFile.DeserializeSettings.None);
                }
            })
                         .Rescue <DllNotFoundException>().Execute(
                             x => new MyMessageBox("Failed to load save: " + x.Message, "Error")
                             .WithIcon(MessageBoxImage.Error).AsCoroutine())
                         .Rescue <FileFormats.SaveFormatException>().Execute(
                             x => new MyMessageBox("Failed to load save: " + x.Message, "Error")
                             .WithIcon(MessageBoxImage.Error).AsCoroutine())
                         .Rescue <FileFormats.SaveCorruptionException>().Execute(
                             x => new MyMessageBox("Failed to load save: " + x.Message, "Error")
                             .WithIcon(MessageBoxImage.Error).AsCoroutine())
                         .Rescue().Execute(
                             x =>
                             new MyMessageBox("An exception was thrown (press Ctrl+C to copy):\n\n" + x.ToString(),
                                              "Error")
                             .WithIcon(MessageBoxImage.Error).AsCoroutine()));

            if (saveFile != null)
            {
                // TODO: deep copy?
                this.Shell.SaveFile.SaveGame.RegionGameStages   = saveFile.SaveGame.RegionGameStages;
                this.Shell.SaveFile.SaveGame.WorldDiscoveryList = saveFile.SaveGame.WorldDiscoveryList;
                this.Shell.SaveFile.SaveGame.FullyExploredAreas = saveFile.SaveGame.FullyExploredAreas;
                yield return
                    (new MyMessageBox("Import successful.")
                     .WithButton(MessageBoxButton.OK)
                     .WithIcon(MessageBoxImage.Information));
            }
        }
        private void DoNewSaveFromPlayerClass(PlayerClassDefinition playerClass)
        {
            var saveFile = new FileFormats.SaveFile()
            {
                Platform = Platform.PC,
                SaveGame = new ProtoBufFormats.WillowTwoSave.WillowTwoPlayerSaveGame()
                {
                    SaveGameId    = 1,
                    SaveGuid      = (ProtoBufFormats.WillowTwoSave.Guid)Guid.NewGuid(),
                    PlayerClass   = playerClass.ResourcePath,
                    UIPreferences = new ProtoBufFormats.WillowTwoSave.UIPreferencesData()
                    {
                        CharacterName = Encoding.UTF8.GetBytes(playerClass.Name),
                    },
                    AppliedCustomizations = new List <string>()
                    {
                        "None",
                        "",
                        "",
                        "",
                        "None",
                    },
                },
            };

            FileFormats.SaveExpansion.ExtractExpansionSavedataFromUnloadableItemData(saveFile.SaveGame);

            this.General.ImportData(saveFile.SaveGame, saveFile.Platform);
            this.Character.ImportData(saveFile.SaveGame);
            this.Vehicle.ImportData(saveFile.SaveGame);
            this.CurrencyOnHand.ImportData(saveFile.SaveGame);
            this.Backpack.ImportData(saveFile.SaveGame, saveFile.Platform);
            this.Bank.ImportData(saveFile.SaveGame, saveFile.Platform);
            //this.FastTravel.ImportData(saveFile.SaveGame);
            this.SaveFile = saveFile;
            this.MaybeSwitchToGeneral();
        }
        public IEnumerable <IResult> ReadSave()
        {
            string fileName = null;
            var    platform = Platform.Invalid;

            foreach (var result in this.SaveLoad.OpenFile(s => fileName = s, p => platform = p))
            {
                yield return(result);
            }

            if (fileName == null)
            {
                yield break;
            }

            FileFormats.SaveFile saveFile = null;

            yield return(new DelegateResult(
                             () =>
            {
                using (var input = File.OpenRead(fileName))
                {
                    saveFile = FileFormats.SaveFile.Deserialize(
                        input,
                        platform,
                        FileFormats.SaveFile.DeserializeSettings.None);
                }

                try
                {
                    FileFormats.SaveExpansion.ExtractExpansionSavedataFromUnloadableItemData(
                        saveFile.SaveGame);

                    this.General.ImportData(saveFile.SaveGame, saveFile.Platform);
                    this.Character.ImportData(saveFile.SaveGame);
                    this.Vehicle.ImportData(saveFile.SaveGame);
                    this.CurrencyOnHand.ImportData(saveFile.SaveGame);
                    this.Backpack.ImportData(saveFile.SaveGame, saveFile.Platform);
                    this.Bank.ImportData(saveFile.SaveGame, saveFile.Platform);
                    //this.FastTravel.ImportData(saveFile.SaveGame);
                    this.SaveFile = saveFile;
                    this.MaybeSwitchToGeneral();
                }
                catch (Exception)
                {
                    this.SaveFile = null;
                    throw;
                }
            })
                         .Rescue <DllNotFoundException>().Execute(
                             x => new MyMessageBox("Failed to load save: " + x.Message, "Error")
                             .WithIcon(MessageBoxImage.Error).AsCoroutine())
                         .Rescue <FileFormats.SaveFormatException>().Execute(
                             x => new MyMessageBox("Failed to load save: " + x.Message, "Error")
                             .WithIcon(MessageBoxImage.Error).AsCoroutine())
                         .Rescue <FileFormats.SaveCorruptionException>().Execute(
                             x => new MyMessageBox("Failed to load save: " + x.Message, "Error")
                             .WithIcon(MessageBoxImage.Error).AsCoroutine())
                         .Rescue().Execute(
                             x =>
                             new MyMessageBox("An exception was thrown (press Ctrl+C to copy):\n\n" + x.ToString(),
                                              "Error")
                             .WithIcon(MessageBoxImage.Error).AsCoroutine()));


            if (saveFile != null &&
                saveFile.SaveGame.IsBadassModeSaveGame == true)
            {
                saveFile.SaveGame.IsBadassModeSaveGame = false;
                yield return
                    (new MyMessageBox("Your save file was set as 'Badass Mode', and this has now been cleared.\n\n" +
                                      "See http://bit.ly/graveyardsav for more details.",
                                      "Information")
                     .WithIcon(MessageBoxImage.Information));
            }


            if (this.SaveFile != null &&
                this.Backpack.BrokenWeapons.Count > 0)
            {
                var result = MessageBoxResult.No;
                do
                {
                    yield return
                        (new MyMessageBox(
                             "There were weapons in the backpack that failed to load. Do you want to remove them?\n\n" +
                             "If you choose not to remove them, they will remain in your save but will not be editable." +
                             (result != MessageBoxResult.Cancel
                                 ? "\n\nChoose Cancel to copy error information to the clipboard."
                                 : ""),
                             "Warning")
                         .WithButton(result != MessageBoxResult.Cancel
                                            ? MessageBoxButton.YesNoCancel
                                            : MessageBoxButton.YesNo)
                         .WithDefaultResult(MessageBoxResult.No)
                         .WithResultDo(r => result = r)
                         .WithIcon(MessageBoxImage.Warning));

                    if (result == MessageBoxResult.Yes)
                    {
                        this.Backpack.BrokenWeapons.Clear();
                    }
                    else if (result == MessageBoxResult.Cancel)
                    {
                        var sb = new StringBuilder();
                        this.Backpack.BrokenWeapons.ForEach(kv =>
                        {
                            sb.AppendLine(kv.Value.ToString());
                            sb.AppendLine();
                        });
                        if (MyClipboard.SetText(sb.ToString()) != MyClipboard.Result.Success)
                        {
                            MessageBox.Show("Clipboard failure.", "Error", MessageBoxButton.OK, MessageBoxImage.Error);
                        }
                    }
                }while (result == MessageBoxResult.Cancel);
            }

            if (this.SaveFile != null &&
                this.Backpack.BrokenItems.Count > 0)
            {
                var result = MessageBoxResult.No;
                do
                {
                    yield return
                        (new MyMessageBox(
                             "There were items in the backpack that failed to load. Do you want to remove them?\n\n" +
                             "If you choose not to remove them, they will remain in your save but will not be editable." +
                             (result != MessageBoxResult.Cancel
                                 ? "\n\nChoose Cancel to copy error information to the clipboard."
                                 : ""),
                             "Warning")
                         .WithButton(result != MessageBoxResult.Cancel
                                            ? MessageBoxButton.YesNoCancel
                                            : MessageBoxButton.YesNo)
                         .WithDefaultResult(MessageBoxResult.No)
                         .WithResultDo(r => result = r)
                         .WithIcon(MessageBoxImage.Warning));

                    if (result == MessageBoxResult.Yes)
                    {
                        this.Backpack.BrokenItems.Clear();
                    }
                    else if (result == MessageBoxResult.Cancel)
                    {
                        var sb = new StringBuilder();
                        this.Backpack.BrokenItems.ForEach(kv =>
                        {
                            sb.AppendLine(kv.Value.ToString());
                            sb.AppendLine();
                        });
                        if (MyClipboard.SetText(sb.ToString()) != MyClipboard.Result.Success)
                        {
                            MessageBox.Show("Clipboard failure.", "Error", MessageBoxButton.OK, MessageBoxImage.Error);
                        }
                    }
                }while (result == MessageBoxResult.Cancel);
            }

            if (this.SaveFile != null &&
                this.Bank.BrokenSlots.Count > 0)
            {
                var result = MessageBoxResult.No;
                do
                {
                    yield return
                        (new MyMessageBox(
                             "There were weapons or items in the bank that failed to load. Do you want to remove them?\n\n" +
                             "If you choose not to remove them, they will remain in your save but will not be editable." +
                             (result != MessageBoxResult.Cancel
                                 ? "\n\nChoose Cancel to copy error information to the clipboard."
                                 : ""),
                             "Warning")
                         .WithButton(result != MessageBoxResult.Cancel
                                            ? MessageBoxButton.YesNoCancel
                                            : MessageBoxButton.YesNo)
                         .WithDefaultResult(MessageBoxResult.No)
                         .WithResultDo(r => result = r)
                         .WithIcon(MessageBoxImage.Warning));

                    if (result == MessageBoxResult.Yes)
                    {
                        this.Bank.BrokenSlots.Clear();
                    }
                    else if (result == MessageBoxResult.Cancel)
                    {
                        var sb = new StringBuilder();
                        this.Bank.BrokenSlots.ForEach(kv =>
                        {
                            sb.AppendLine(kv.Value.ToString());
                            sb.AppendLine();
                        });
                        if (MyClipboard.SetText(sb.ToString()) != MyClipboard.Result.Success)
                        {
                            MessageBox.Show("Clipboard failure.", "Error", MessageBoxButton.OK, MessageBoxImage.Error);
                        }
                    }
                }while (result == MessageBoxResult.Cancel);
            }
        }