private void SaveSplits(bool promptPBMessage) { var savePath = CurrentState.Run.FilePath; if (savePath == null) { SaveSplitsAs(promptPBMessage); return; } CurrentState.Run.FixSplits(); var stateCopy = CurrentState.Clone() as LiveSplitState; var modelCopy = new TimerModel(); modelCopy.CurrentState = stateCopy; var result = DialogResult.No; if (promptPBMessage && ((CurrentState.CurrentPhase == TimerPhase.Ended && CurrentState.Run.Last().PersonalBestSplitTime[CurrentState.CurrentTimingMethod] != null && CurrentState.Run.Last().SplitTime[CurrentState.CurrentTimingMethod] >= CurrentState.Run.Last().PersonalBestSplitTime[CurrentState.CurrentTimingMethod]) || CurrentState.CurrentPhase == TimerPhase.Running || CurrentState.CurrentPhase == TimerPhase.Paused)) { DontRedraw = true; result = MessageBox.Show(this, "This run did not beat your current splits. Would you like to save this run as a Personal Best?", "Save as Personal Best?", MessageBoxButtons.YesNoCancel, MessageBoxIcon.Question); DontRedraw = false; if (result == DialogResult.Yes) { Model.Reset(); modelCopy.SetRunAsPB(); modelCopy.UpdateAttemptHistory(); modelCopy.UpdateBestSegments(); modelCopy.UpdateSegmentHistory(); SetRun(stateCopy.Run); } else if (result == DialogResult.Cancel) return; } if (result == DialogResult.Yes) modelCopy.Reset(false); else modelCopy.Reset(); try { if (!File.Exists(savePath)) File.Create(savePath).Close(); using (var memoryStream = new MemoryStream()) { RunSaver.Save(stateCopy.Run, memoryStream); using (var stream = File.Open(savePath, FileMode.Create, FileAccess.Write)) { var buffer = memoryStream.GetBuffer(); stream.Write(buffer, 0, (int)memoryStream.Length); } CurrentState.Run.HasChanged = false; } AddSplitsFileToLRU(savePath, stateCopy.Run, CurrentState.CurrentTimingMethod); } catch (Exception ex) { MessageBox.Show(this, "Splits could not be saved!", "Save Failed", MessageBoxButtons.OK, MessageBoxIcon.Error); Log.Error(ex); } }
private void SaveSplits(bool promptPBMessage) { var savePath = CurrentState.Run.FilePath; if (savePath == null) { SaveSplitsAs(promptPBMessage); return; } var stateCopy = CurrentState.Clone() as LiveSplitState; var modelCopy = new TimerModel(); modelCopy.CurrentState = stateCopy; var result = System.Windows.Forms.DialogResult.No; if (promptPBMessage && ((CurrentState.CurrentPhase == TimerPhase.Ended && CurrentState.Run.Last().PersonalBestSplitTime != null && CurrentState.Run.Last().RTASplitTime >= CurrentState.Run.Last().PersonalBestSplitTime) || CurrentState.CurrentPhase == TimerPhase.Running || CurrentState.CurrentPhase == TimerPhase.Paused)) { DontRedraw = true; result = MessageBox.Show(this, "This run did not beat your current splits. Would you like to save this run as a Personal Best?", "Save as Personal Best?", MessageBoxButtons.YesNoCancel, MessageBoxIcon.Question); DontRedraw = false; if (result == System.Windows.Forms.DialogResult.Yes) { Model.Reset(); modelCopy.SetRunAsPB(); modelCopy.UpdateRunHistory(); modelCopy.UpdateBestSegments(); modelCopy.UpdateSegmentHistory(); SetRun(stateCopy.Run); } else if (result == System.Windows.Forms.DialogResult.Cancel) return; } if (result == System.Windows.Forms.DialogResult.Yes) modelCopy.ResetWithoutUpdating(); else modelCopy.Reset(); if (!System.IO.File.Exists(savePath)) System.IO.File.Create(savePath).Close(); using (var stream = System.IO.File.Open(savePath, System.IO.FileMode.OpenOrCreate | System.IO.FileMode.Truncate, System.IO.FileAccess.Write)) { RunSaver.Save(stateCopy.Run, stream); CurrentState.Run.HasChanged = false; } Settings.AddToRecentSplits(savePath); UpdateRecentSplits(); }