private async void SaveSessionHistoryToolStripMenuItem_Click()

in Tools/MultiplayerSessionHistoryViewer/Form1.cs [963:1052]


        private async void SaveSessionHistoryToolStripMenuItem_Click(object sender, EventArgs e)
        {
            if (this.listView1.SelectedItems.Count != 1)
            {
                return;
            }
            
            int selectedIndex = this.listView1.SelectedIndices[0];

            HistoricalDocument document = new HistoricalDocument()
            {
                SessionName = this.listView1.Items[selectedIndex].SubItems[0].Text,
                Branch = this.listView1.Items[selectedIndex].SubItems[1].Text,
                NumSnapshots = int.Parse(this.listView1.Items[selectedIndex].SubItems[2].Text),
                LastModified = this.listView1.Items[selectedIndex].SubItems[3].Text,
                IsExpired = bool.Parse(this.listView1.Items[selectedIndex].SubItems[4].Text),
                ActivityId = this.listView1.Items[selectedIndex].SubItems[5].Text,
            };

            string eToken = await ToolAuthentication.GetDevTokenSilentlyAsync(this.tbScid.Text, this.tbSandbox.Text);

            this.lblExplanation.Text = "Downloading change history for the selected session...";

            Tuple<SessionHistoryDocumentResponse, string> queryResponse = await this.QueryForDocSessionHistoryAsync(eToken, document.SessionName, document.Branch);

            if (queryResponse.Item2 != null)
            {
                this.downloadPanel.Hide();

                MessageBox.Show(string.Format("{0}\n\nThe server may have been busy.\nPlease try again.", queryResponse.Item2), "Error!");
                return;
            }

            string errMsg = null;

            if (queryResponse.Item1 != null)
            {
                foreach (var item in queryResponse.Item1.Results)
                {
                    Tuple<string, string> getSnapshotResponse = await this.QueryForDocSessionHistoryChangeAsync(document.SessionName, document.Branch, item.ChangeNumber);
                    string snapshotBody = getSnapshotResponse.Item1;
                    errMsg = getSnapshotResponse.Item2;

                    if (errMsg != null)
                    {
                        break;
                    }

                    DocumentStateSnapshot snapshot = new DocumentStateSnapshot()
                    {
                        Change = item.ChangeNumber,
                        ModifiedByXuids = item.ChangedBy,
                        Timestamp = item.Timestamp,
                        TitleId = item.TitleId,
                        ServiceId = item.ServiceId,
                        CorrelationId = item.CorrelationId,
                        ChangeDetails = item.Details,
                        Body = snapshotBody != null ? snapshotBody : string.Empty
                    };

                    document.DocumentSnapshots.Add(snapshot);
                }
            }

            this.downloadPanel.Hide();

            if (errMsg != null)
            {
                MessageBox.Show(string.Format("{0}\n\nPlease try again.", errMsg), "Error");
            }
            else
            {
                // serialize the HistoricalDocument to json
                string testString = JsonConvert.SerializeObject(document);

                var saveDialog = new SaveFileDialog()
                {
                    Filter = SessionHistoryFileFilter,
                    FileName = string.Format("{0}~{1}", document.SessionName, document.Branch)
                };

                if (saveDialog.ShowDialog() == DialogResult.OK)
                {
                    using (StreamWriter sw = new StreamWriter(saveDialog.FileName))
                    {
                        sw.Write(testString);
                    }
                }
            }
        }