From 3be87bb2b04c7630af952eff67ac26d2e2d50248 Mon Sep 17 00:00:00 2001
From: "feal87@gmail.com"
 <feal87@gmail.com@96395faa-99c1-11dd-bbfe-3dabce05a288>
Date: Wed, 22 Dec 2010 06:59:53 +0000
Subject: [PATCH] 1) Fixed crash when no ini was created, but an override is
 attempted. 2) Started writting the debug mode. (a little gif browser grouped
 by VSync is created)

git-svn-id: http://pcsx2.googlecode.com/svn/trunk@4119 96395faa-99c1-11dd-bbfe-3dabce05a288
---
 tools/GSDumpGUI/Core/Program.cs               | 81 +++++++++++++++++-
 tools/GSDumpGUI/Forms/frmMain.Designer.cs     | 83 +++++++++++++++----
 tools/GSDumpGUI/Forms/frmMain.cs              | 45 +++++++++-
 tools/GSDumpGUI/Library/GSDXWrapper.cs        | 34 +++++++-
 .../Library/TCPLibrary/Message/TCPMessage.cs  |  6 +-
 5 files changed, 228 insertions(+), 21 deletions(-)

diff --git a/tools/GSDumpGUI/Core/Program.cs b/tools/GSDumpGUI/Core/Program.cs
index 7f15808c24..b8ec038240 100644
--- a/tools/GSDumpGUI/Core/Program.cs
+++ b/tools/GSDumpGUI/Core/Program.cs
@@ -21,6 +21,7 @@ namespace GSDumpGUI
         static public TCPLibrary.MessageBased.Core.BaseMessageClient Client;
         static private Boolean ChangeIcon;
         static private GSDump dump;
+        static private GSDXWrapper wrap;
 
         [STAThread]
         static void Main(String[] args)
@@ -66,7 +67,7 @@ namespace GSDumpGUI
                 String Operation = args[2];
                 Int32 Renderer = Convert.ToInt32(args[3]);
 
-                GSDXWrapper wrap = new GSDXWrapper();
+                wrap = new GSDXWrapper();
                 wrap.Load(DLLPath);
                 Directory.SetCurrentDirectory(Path.GetDirectoryName(AppDomain.CurrentDomain.BaseDirectory + "GSDumpGSDXConfigs\\" + Path.GetFileName(DLLPath) + "\\"));
                 if (Operation == "GSReplay")
@@ -149,6 +150,53 @@ namespace GSDumpGUI
                     {
                         MessageBox.Show("Savestate too old to be read. :(", "Warning");
                         frmMain.Focus();
+                    }), new object[] { null });
+                    break;
+                case MessageType.GetDebugMode:
+                    frmMain.Invoke(new Action<object>(delegate(object e)
+                    {
+                        frmMain.chkDebugMode.Checked = (Boolean)Mess.Parameters[0];
+
+                        frmMain.lblGif.Enabled = frmMain.chkDebugMode.Checked;
+                        frmMain.btnRunToSelection.Enabled = frmMain.chkDebugMode.Checked;
+                        frmMain.treTreeView.Enabled = frmMain.chkDebugMode.Checked;
+                        frmMain.btnStep.Enabled = frmMain.chkDebugMode.Checked;
+                        if (frmMain.chkDebugMode.Checked == false)
+                            frmMain.treTreeView.Nodes.Clear();
+
+                    }), new object[] { null });
+                    break;
+                case MessageType.DebugState:
+                    frmMain.Invoke(new Action<object>(delegate(object e)
+                    {
+                        frmMain.treTreeView.Nodes.Clear();
+                        List<TreeNode> parents = new List<TreeNode>();
+                        List<TreeNode> nodes = new List<TreeNode>();
+                        foreach (var itm in Mess.Parameters)
+                        {
+                            String[] parts = itm.ToString().Split(new char[] { '|' });
+                            switch (parts[1])
+                            {
+                                case "Transfer":
+                                    nodes.Add(new TreeNode(parts[0] + " - " + parts[1] + " - " + parts[2]));
+                                    break;
+                                case "ReadFIFO2":
+                                    nodes.Add(new TreeNode(parts[0] + " - " + parts[1]));
+                                    break;
+                                case "VSync":
+                                    TreeNode tn = new TreeNode();
+                                    tn.Text = parts[0] + " - " + parts[1];
+                                    tn.Nodes.AddRange(nodes.ToArray());
+                                    parents.Add(tn);
+
+                                    nodes.Clear();
+                                    break;
+                                case "Registers":
+                                    nodes.Add(new TreeNode(parts[0] + " - " + parts[1]));
+                                    break;
+                            }
+                        }
+                        frmMain.treTreeView.Nodes.AddRange(parents.ToArray());
                     }), new object[] { null });                    
                     break;
                 default:
@@ -199,6 +247,37 @@ namespace GSDumpGUI
                     }
                     Client.Send(msg);
                     break;
+                case MessageType.SetDebugMode:
+                    wrap.DebugMode = (Boolean)Mess.Parameters[0];
+
+                    msg = new TCPMessage();
+                    msg.MessageType = MessageType.GetDebugMode;
+                    msg.Parameters.Add(wrap.DebugMode);
+                    Client.Send(msg);
+
+                    if (wrap.DebugMode)
+                    {
+                        msg = new TCPMessage();
+                        msg.MessageType = MessageType.DebugState;
+                        msg.Parameters.AddRange(wrap.GetGifPackets(dump));
+                        Client.Send(msg);
+                    }
+                    break;
+                case MessageType.GetDebugMode:
+                    msg = new TCPMessage();
+                    msg.MessageType = MessageType.GetDebugMode;
+                    msg.Parameters.Add(wrap.DebugMode);
+                    Client.Send(msg);
+
+                    if (wrap.DebugMode)
+                    {
+                        msg = new TCPMessage();
+                        msg.MessageType = MessageType.DebugState;
+                        msg.Parameters.AddRange(wrap.GetGifPackets(dump));
+                        Client.Send(msg);
+                    }
+                    break;
+
                 default:
                     break;
             }
diff --git a/tools/GSDumpGUI/Forms/frmMain.Designer.cs b/tools/GSDumpGUI/Forms/frmMain.Designer.cs
index b7f37384fd..9b0af7949b 100644
--- a/tools/GSDumpGUI/Forms/frmMain.Designer.cs
+++ b/tools/GSDumpGUI/Forms/frmMain.Designer.cs
@@ -58,7 +58,6 @@
             this.lblChild = new System.Windows.Forms.Label();
             this.lblDumpSize = new System.Windows.Forms.Label();
             this.txtDumpSize = new System.Windows.Forms.Label();
-            this.lblWIP = new System.Windows.Forms.Label();
             this.txtGIFPackets = new System.Windows.Forms.Label();
             this.lblGIFPackets = new System.Windows.Forms.Label();
             this.txtPath1 = new System.Windows.Forms.Label();
@@ -73,6 +72,11 @@
             this.label7 = new System.Windows.Forms.Label();
             this.txtRegisters = new System.Windows.Forms.Label();
             this.label6 = new System.Windows.Forms.Label();
+            this.chkDebugMode = new System.Windows.Forms.CheckBox();
+            this.lblGif = new System.Windows.Forms.Label();
+            this.btnStep = new System.Windows.Forms.Button();
+            this.btnRunToSelection = new System.Windows.Forms.Button();
+            this.treTreeView = new System.Windows.Forms.TreeView();
             ((System.ComponentModel.ISupportInitialize)(this.pctBox)).BeginInit();
             this.SuspendLayout();
             // 
@@ -368,17 +372,6 @@
             this.txtDumpSize.Size = new System.Drawing.Size(0, 13);
             this.txtDumpSize.TabIndex = 30;
             // 
-            // lblWIP
-            // 
-            this.lblWIP.AutoSize = true;
-            this.lblWIP.Font = new System.Drawing.Font("Times New Roman", 48F, System.Drawing.FontStyle.Bold, System.Drawing.GraphicsUnit.Point, ((byte)(0)));
-            this.lblWIP.ForeColor = System.Drawing.Color.Red;
-            this.lblWIP.Location = new System.Drawing.Point(468, 587);
-            this.lblWIP.Name = "lblWIP";
-            this.lblWIP.Size = new System.Drawing.Size(508, 73);
-            this.lblWIP.TabIndex = 31;
-            this.lblWIP.Text = "Work in Progress";
-            // 
             // txtGIFPackets
             // 
             this.txtGIFPackets.AutoSize = true;
@@ -505,11 +498,70 @@
             this.label6.TabIndex = 44;
             this.label6.Text = "Registers Packets";
             // 
+            // chkDebugMode
+            // 
+            this.chkDebugMode.AutoSize = true;
+            this.chkDebugMode.Enabled = false;
+            this.chkDebugMode.Location = new System.Drawing.Point(481, 487);
+            this.chkDebugMode.Name = "chkDebugMode";
+            this.chkDebugMode.Size = new System.Drawing.Size(88, 17);
+            this.chkDebugMode.TabIndex = 46;
+            this.chkDebugMode.Text = "Debug Mode";
+            this.chkDebugMode.UseVisualStyleBackColor = true;
+            this.chkDebugMode.CheckedChanged += new System.EventHandler(this.chkDebugMode_CheckedChanged);
+            // 
+            // lblGif
+            // 
+            this.lblGif.AutoSize = true;
+            this.lblGif.Enabled = false;
+            this.lblGif.Location = new System.Drawing.Point(427, 520);
+            this.lblGif.Name = "lblGif";
+            this.lblGif.Size = new System.Drawing.Size(66, 13);
+            this.lblGif.TabIndex = 48;
+            this.lblGif.Text = "GIF Packets";
+            // 
+            // btnStep
+            // 
+            this.btnStep.Enabled = false;
+            this.btnStep.Location = new System.Drawing.Point(629, 533);
+            this.btnStep.Name = "btnStep";
+            this.btnStep.Size = new System.Drawing.Size(108, 40);
+            this.btnStep.TabIndex = 49;
+            this.btnStep.TabStop = false;
+            this.btnStep.Text = "Step";
+            this.btnStep.UseVisualStyleBackColor = true;
+            this.btnStep.Click += new System.EventHandler(this.btnStep_Click);
+            // 
+            // btnRunToSelection
+            // 
+            this.btnRunToSelection.Enabled = false;
+            this.btnRunToSelection.Location = new System.Drawing.Point(629, 579);
+            this.btnRunToSelection.Name = "btnRunToSelection";
+            this.btnRunToSelection.Size = new System.Drawing.Size(108, 40);
+            this.btnRunToSelection.TabIndex = 50;
+            this.btnRunToSelection.TabStop = false;
+            this.btnRunToSelection.Text = "Run To Selection";
+            this.btnRunToSelection.UseVisualStyleBackColor = true;
+            this.btnRunToSelection.Click += new System.EventHandler(this.btnRunToSelection_Click);
+            // 
+            // treTreeView
+            // 
+            this.treTreeView.Enabled = false;
+            this.treTreeView.Location = new System.Drawing.Point(420, 539);
+            this.treTreeView.Name = "treTreeView";
+            this.treTreeView.Size = new System.Drawing.Size(200, 240);
+            this.treTreeView.TabIndex = 51;
+            // 
             // GSDumpGUI
             // 
             this.AutoScaleDimensions = new System.Drawing.SizeF(6F, 13F);
             this.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font;
             this.ClientSize = new System.Drawing.Size(988, 790);
+            this.Controls.Add(this.treTreeView);
+            this.Controls.Add(this.btnRunToSelection);
+            this.Controls.Add(this.btnStep);
+            this.Controls.Add(this.lblGif);
+            this.Controls.Add(this.chkDebugMode);
             this.Controls.Add(this.txtRegisters);
             this.Controls.Add(this.label6);
             this.Controls.Add(this.txtReadFifo);
@@ -524,7 +576,6 @@
             this.Controls.Add(this.label2);
             this.Controls.Add(this.txtGIFPackets);
             this.Controls.Add(this.lblGIFPackets);
-            this.Controls.Add(this.lblWIP);
             this.Controls.Add(this.txtDumpSize);
             this.Controls.Add(this.lblDumpSize);
             this.Controls.Add(this.lstProcesses);
@@ -599,7 +650,6 @@
         public System.Windows.Forms.ListBox lstProcesses;
         private System.Windows.Forms.Label lblDumpSize;
         public System.Windows.Forms.Label txtDumpSize;
-        private System.Windows.Forms.Label lblWIP;
         public System.Windows.Forms.Label txtGIFPackets;
         private System.Windows.Forms.Label lblGIFPackets;
         public System.Windows.Forms.Label txtPath1;
@@ -614,6 +664,11 @@
         private System.Windows.Forms.Label label7;
         public System.Windows.Forms.Label txtRegisters;
         private System.Windows.Forms.Label label6;
+        public System.Windows.Forms.CheckBox chkDebugMode;
+        public System.Windows.Forms.TreeView treTreeView;
+        public System.Windows.Forms.Label lblGif;
+        public System.Windows.Forms.Button btnStep;
+        public System.Windows.Forms.Button btnRunToSelection;
     }
 }
 
diff --git a/tools/GSDumpGUI/Forms/frmMain.cs b/tools/GSDumpGUI/Forms/frmMain.cs
index dcdaa20c2c..6a26822aca 100644
--- a/tools/GSDumpGUI/Forms/frmMain.cs
+++ b/tools/GSDumpGUI/Forms/frmMain.cs
@@ -195,10 +195,17 @@ namespace GSDumpGUI
                 {
                     String ini = File.ReadAllText(AppDomain.CurrentDomain.BaseDirectory + "GSDumpGSDXConfigs\\" + GSDXName + "\\inis\\gsdx.ini");
                     int pos = ini.IndexOf("Renderer=", 0);
-                    String newini = ini.Substring(0, pos + 9);
-                    newini += SelectedRenderer;
-                    newini += ini.Substring(pos + 10, ini.Length - pos - 10);
-                    File.WriteAllText(AppDomain.CurrentDomain.BaseDirectory + "GSDumpGSDXConfigs\\" + GSDXName + "\\inis\\gsdx.ini", newini);
+                    if (pos != -1)
+                    {
+                        String newini = ini.Substring(0, pos + 9);
+                        newini += SelectedRenderer;
+                        newini += ini.Substring(pos + 10, ini.Length - pos - 10);
+                        File.WriteAllText(AppDomain.CurrentDomain.BaseDirectory + "GSDumpGSDXConfigs\\" + GSDXName + "\\inis\\gsdx.ini", newini);
+                    }
+                    else
+                    {
+                        File.WriteAllText(AppDomain.CurrentDomain.BaseDirectory + "GSDumpGSDXConfigs\\" + GSDXName + "\\inis\\gsdx.ini", ini + Environment.NewLine + "Renderer=" + SelectedRenderer);
+                    }
                 }
             }
             if (lstDumps.SelectedItem != null)
@@ -369,13 +376,43 @@ namespace GSDumpGUI
         {
             if (lstProcesses.SelectedIndex != -1)
             {
+                chkDebugMode.Enabled = true;
+
                 TCPMessage msg = new TCPMessage();
+                msg.MessageType = MessageType.GetDebugMode;
+                msg.Parameters.Add(chkDebugMode.Checked);
+                Program.Clients.Find(a => a.IPAddress == lstProcesses.SelectedItem.ToString()).Send(msg);
+
+                msg = new TCPMessage();
                 msg.MessageType = MessageType.SizeDump;
                 Program.Clients.Find(a => a.IPAddress == lstProcesses.SelectedItem.ToString()).Send(msg);
+
                 msg = new TCPMessage();
                 msg.MessageType = MessageType.Statistics;
                 Program.Clients.Find(a => a.IPAddress == lstProcesses.SelectedItem.ToString()).Send(msg);
             }
+            else
+            {
+                chkDebugMode.Enabled = false;
+            }
+        }
+
+        private void chkDebugMode_CheckedChanged(object sender, EventArgs e)
+        {
+            TCPMessage msg = new TCPMessage();
+            msg.MessageType = MessageType.SetDebugMode;
+            msg.Parameters.Add(chkDebugMode.Checked);
+            Program.Clients.Find(a => a.IPAddress == lstProcesses.SelectedItem.ToString()).Send(msg);
+        }
+
+        private void btnStep_Click(object sender, EventArgs e)
+        {
+            MessageBox.Show("Not Implemented");
+        }
+
+        private void btnRunToSelection_Click(object sender, EventArgs e)
+        {
+            MessageBox.Show("Not Implemented");
         }
     }
 }
diff --git a/tools/GSDumpGUI/Library/GSDXWrapper.cs b/tools/GSDumpGUI/Library/GSDXWrapper.cs
index 8976c9cc71..2fd7301b09 100644
--- a/tools/GSDumpGUI/Library/GSDXWrapper.cs
+++ b/tools/GSDumpGUI/Library/GSDXWrapper.cs
@@ -3,6 +3,8 @@ using System.Collections.Generic;
 using System.Text;
 using System.Runtime.InteropServices;
 using System.IO;
+using TCPLibrary.MessageBased.Core;
+using System.Threading;
 
 namespace GSDumpGUI
 {
@@ -49,6 +51,11 @@ namespace GSDumpGUI
 
         private Boolean Running;
 
+        public List<TCPMessage> QueueMessage;
+        public Boolean DebugMode;
+        public GSData CurrentGIFPacket;
+        public AutoResetEvent Event;
+
         static public Boolean IsValidGSDX(String DLL)
         {
             NativeMethods.SetErrorMode(0x8007);
@@ -168,7 +175,10 @@ namespace GSDumpGUI
 
         public unsafe void Run(GSDump dump, int rendererOverride)
         {
+            QueueMessage = new List<TCPMessage>();
             Running = true;
+            Event = new AutoResetEvent(true);
+
             GSinit();
             fixed (byte* pointer = dump.Registers)
             {
@@ -199,11 +209,13 @@ namespace GSDumpGUI
                                 Running = false;
                                 break;
                             }
+
                             foreach (var itm in dump.Data)
                             {
+                                CurrentGIFPacket = itm;
+
                                 Step(itm, pointer);
                             }
-
                         }
 
                         GSclose();
@@ -275,5 +287,25 @@ namespace GSDumpGUI
         {
             Running = false;
         }
+
+        internal List<Object> GetGifPackets(GSDump dump)
+        {
+            List<Object> Data = new List<Object>();
+            for (int i = 0; i < dump.Data.Count; i++)
+            {
+                String act = i.ToString() + "|";
+                act += dump.Data[i].id.ToString() + "|";
+                if (dump.Data[i].GetType().IsSubclassOf(typeof(GSData)))
+                {
+                    act += ((GSTransfer)dump.Data[i]).Path.ToString();
+                }
+                else
+                {
+
+                }
+                Data.Add(act);
+            }
+            return Data;
+        }
     }
 }
diff --git a/tools/GSDumpGUI/Library/TCPLibrary/Message/TCPMessage.cs b/tools/GSDumpGUI/Library/TCPLibrary/Message/TCPMessage.cs
index dc0512e732..dc6222e09a 100644
--- a/tools/GSDumpGUI/Library/TCPLibrary/Message/TCPMessage.cs
+++ b/tools/GSDumpGUI/Library/TCPLibrary/Message/TCPMessage.cs
@@ -111,6 +111,10 @@ namespace TCPLibrary.MessageBased.Core
         MaxUsers,
         SizeDump,
         Statistics,
-        StateOld
+        StateOld,
+
+        GetDebugMode,
+        SetDebugMode,
+        DebugState
     }
 }