JD2022-TU1/main/extern/ProfileEventViewer4/CaptureForm.cs

227 lines
8.2 KiB
C#

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using System.Threading;
using System.IO;
using XDevkit;
namespace ProfileEventViewer
{
public partial class CaptureForm : Form
{
public enum Platform
{
x360
}
public string FileName { get { return _FileName; } }
private Platform _Platform;
private string _FileName = "";
private bool _Cancelled = false;
private DialogResult _DialogResult = DialogResult.None;
public CaptureForm(Platform platform)
{
_Platform = platform;
InitializeComponent();
if (_Platform == Platform.x360)
{
backgroundWorkerX360.WorkerReportsProgress = true;
backgroundWorkerX360.RunWorkerAsync();
}
}
private void buttonCancel_Click(object sender, EventArgs e)
{
if (_Platform == Platform.x360)
{
if (backgroundWorkerX360.IsBusy)
{
textBoxOutput.AppendText("Cancel requested...\n");
_Cancelled = true;
}
else
{
DialogResult = _DialogResult;
}
}
}
private void backgroundWorkerX360_DoWork(object sender, DoWorkEventArgs e)
{
const string _X360ConnectionName = "Scimitar";
try
{
//
// Connect to the console
XboxManager xboxManager = new XboxManager();
XboxConsole console = null;
try
{
backgroundWorkerX360.ReportProgress(0, String.Format( "Connecting '{0}'",xboxManager.DefaultConsole));
console = xboxManager.OpenConsole(xboxManager.DefaultConsole);
backgroundWorkerX360.ReportProgress(15, String.Format( "Connected '{0}'",console.Name));
}
catch
{
backgroundWorkerX360.ReportProgress(0, String.Format("Cannot connect to '{0}'", xboxManager.DefaultConsole));
return;
}
if (_Cancelled) return;
//
// Open debug connection
uint connection = 0;
try
{
backgroundWorkerX360.ReportProgress(25, "Open debug connection named '" + _X360ConnectionName + "'");
connection = console.OpenConnection(_X360ConnectionName);
}
catch (Exception)
{
backgroundWorkerX360.ReportProgress(0, "Cannot open connection, make sure the running process have profile enable");
return;
}
if (_Cancelled) return;
//
// Send a connect command to make sure is't connected
try
{
string result;
console.SendTextCommand( connection, _X360ConnectionName + "!Connect", out result );
if ( !result.Contains("Connected") )
{
backgroundWorkerX360.ReportProgress(0, "Connection failed: " + result);
return;
}
}
catch (Exception)
{
backgroundWorkerX360.ReportProgress(0, "Connection failed");
return;
}
if (_Cancelled) return;
//
// Send the capture command
string pelFileName;
try
{
backgroundWorkerX360.ReportProgress(50, "Capture in progress...");
string result;
console.SendTextCommand(connection, _X360ConnectionName + "!CaptureProfile", out result);
if (result.Contains("CaptureProfileBegin") )
{
// Profiler may not be started, start it..
backgroundWorkerX360.ReportProgress(65, "Starting profiler, wait 2 sec..." );
Thread.Sleep(2000);
console.SendTextCommand(connection, _X360ConnectionName + "!CaptureProfile", out result);
}
// Check for success
if (result.Contains("CaptureProfileSuccess:"))
{
pelFileName = result.Substring(result.LastIndexOf(':') + 1);
}
else
{
backgroundWorkerX360.ReportProgress(0, "Capture failed: " + result);
return;
}
}
catch (Exception)
{
backgroundWorkerX360.ReportProgress(0, "Capture failed");
return;
}
backgroundWorkerX360.ReportProgress(50, "Capture done");
if (_Cancelled) return;
//
// Copy file to the pc
string programFullFileName = console.RunningProcessInfo.ProgramName;
string programFullPath = programFullFileName.Substring(0, programFullFileName.LastIndexOf('\\'));
string pelFullFileNameRemote = programFullPath + "\\" + pelFileName;
string pelFullFileNameLocal = System.IO.Path.GetTempPath() + pelFileName;
backgroundWorkerX360.ReportProgress(75, String.Format("Copy {0}:", pelFileName));
backgroundWorkerX360.ReportProgress(75, String.Format("\tfrom:\t'{0}'", pelFullFileNameRemote));
backgroundWorkerX360.ReportProgress(75, String.Format("\tto :\t'{0}'", pelFullFileNameLocal));
Thread.Sleep(1000);
try
{
console.ReceiveFile(pelFullFileNameLocal, pelFullFileNameRemote);
_FileName = pelFullFileNameLocal;
}
catch (Exception)
{
backgroundWorkerX360.ReportProgress(0, "Error: copy file failed");
return;
}
// Delete x360 pel file.
try
{
backgroundWorkerX360.ReportProgress(95, "Deleting: " + pelFullFileNameRemote);
console.DeleteFile(pelFullFileNameRemote);
}
catch (Exception)
{
backgroundWorkerX360.ReportProgress(95, "Error deleting file");
}
//done!
backgroundWorkerX360.ReportProgress(100, "Done");
}
catch (Exception ex)
{
backgroundWorkerX360.ReportProgress(0, "Error: " + ex.Message);
}
}
private void backgroundWorkerX360_RunWorkerCompleted(object sender, RunWorkerCompletedEventArgs e)
{
buttonCancel.Text = "Close";
if (_Cancelled )
{
textBoxOutput.AppendText("Cancelled\n");
_DialogResult = DialogResult.Cancel;
}
else
{
if (File.Exists(FileName) && progressBar.Value == 100)
{
if (checkBoxClose.Checked)
DialogResult = DialogResult.OK;
else
_DialogResult = DialogResult.OK;
}
else
{
textBoxOutput.AppendText("Cancelled\n");
_DialogResult = DialogResult.Cancel;
}
}
}
private void backgroundWorkerX360_ProgressChanged(object sender, ProgressChangedEventArgs e)
{
progressBar.Value = e.ProgressPercentage;
if ( e.UserState != null )
textBoxOutput.AppendText(e.UserState.ToString() + "\n");
}
}
}