ViewVC Help
View File | Revision Log | Show Annotations | Download File | View Changeset | Root Listing
root/RomCheater/trunk/RomCheater/Docking/PIDSelector.cs
Revision: 331
Committed: Thu Jun 7 19:32:13 2012 UTC (10 years, 2 months ago) by william
File size: 12056 byte(s)
Log Message:
+ more wip for launch and attach to process

File Contents

# User Rev Content
1 william 88 using System;
2     using System.Collections.Generic;
3     using System.ComponentModel;
4     using System.Data;
5     using System.Drawing;
6     using System.Text;
7     using System.Windows.Forms;
8     using System.Diagnostics;
9     using RomCheater.PluginFramework.Interfaces;
10     using RomCheater.PluginFramework.Core;
11 william 109 using System.IO;
12     using RomCheater.Logging;
13 william 148 using WeifenLuo.WinFormsUI.Docking;
14 william 196 using RomCheater.PluginFramework.Events;
15 william 329 using System.Management;
16 william 88
17 william 149 namespace RomCheater.Docking
18 william 88 {
19 william 196 public partial class PIDSelector : DockContent, IAcceptsPlugin<IConfigPlugin>, IAcceptsChangedProcess, IAcceptsProcessPID
20 william 88 {
21 william 329 private bool isRefreshing = false;
22     private bool isAutoRefreshing = false;
23 william 331 private bool isAttachingToProcess = false;
24 william 152 //private IConfigPlugin plugin = null;
25 william 88 //bool isInError = false;
26 william 196 public PIDSelector() { InitializeComponent(); this.ProcessPID = -1; OnSelectedProcessChanged = null; this.AcceptedPlugin = null; }
27 william 153 public PIDSelector(IConfigPlugin plugin) : this() { this.AcceptedPlugin = plugin; }
28 william 88 // unsued construtor (it's not implmented, may not be)
29     [Obsolete("constructor PIDSelector(int pid) is not implemented", false)]
30 william 196 public PIDSelector(int pid) : this() { this.ProcessPID = pid; }
31 william 88 [Obsolete("constructor PIDSelector(IConfigPlugin plugin, int pid) is not implemented", false)]
32 william 196 public PIDSelector(IConfigPlugin plugin, int pid) : this(plugin) { this.ProcessPID = pid; }
33 william 327
34 william 196 #region IAcceptsProcessPID
35     public int ProcessPID { get; set; }
36     #endregion
37     #region IAcceptsPlugin<IConfigPlugin> members
38 william 153 public IConfigPlugin AcceptedPlugin { get; set; }
39 william 196 #endregion
40     #region IAcceptsChangedProcess members
41 william 287 public event BaseEventHandler<ProcessChangedEventArgs> OnSelectedProcessChanged;
42 william 196 #endregion
43 william 151
44    
45 william 327
46 william 88 public new void Show() { this.Show(null); }
47 william 150 //public new void Show(IWin32Window owner) { this.PreInitShow(owner); }
48     public new void Show(DockPanel panel) { this.PreInitShow(panel); }
49     //public new DialogResult ShowDialog() { return this.ShowDialog(null); }
50     //public new DialogResult ShowDialog(IWin32Window owner) { return this.PreInitDialog(owner); }
51     //private void PreInitShow(IWin32Window owner)
52     //{
53     // if (!this.RefreshList()) { MessageBox.Show(string.Format("Could not find any Processes for plugin {0}. Please start an instance of one.",plugin.ToString())); }
54     // else { if (owner == null) { base.Show(); } else { base.Show(owner); } }
55     //}
56     private void PreInitShow(DockPanel panel)
57 william 88 {
58 william 150 //if (panel == null) return;
59 william 153 if (!this.RefreshList()) { MessageBox.Show(string.Format("Could not find any Processes for plugin {0}. Please start an instance of one.", AcceptedPlugin.ToString())); }
60 william 150 else { if (panel == null) { base.Show(); } else { base.Show(panel); } }
61 william 88 }
62 william 150 //private DialogResult PreInitDialog(IWin32Window owner)
63     //{
64     // if (!this.RefreshList()) { MessageBox.Show(string.Format("Could not find any Processes for plugin {0}. Please start an instance of one.", plugin.ToString())); return DialogResult.Cancel; }
65     // else { if (owner == null) { return base.ShowDialog(); } else { return base.ShowDialog(owner); } }
66     //}
67 william 88 private void btnLargeIcon_Click(object sender, EventArgs e) { this.lstProcessList.View = View.LargeIcon; }
68     private void btnDetails_Click(object sender, EventArgs e) { this.lstProcessList.View = View.Details; }
69     private void btnSmallIcon_Click(object sender, EventArgs e) { this.lstProcessList.View = View.SmallIcon; }
70     private void btnList_Click(object sender, EventArgs e) { this.lstProcessList.View = View.List; }
71     private void btnTile_Click(object sender, EventArgs e) { this.lstProcessList.View = View.Tile; }
72    
73 william 156 private void PerformListViewItemSelect(ListViewItem li)
74 william 327 {
75 william 331 if (isAutoRefreshing && !isAttachingToProcess) return;
76 william 196 this.ProcessPID = Convert.ToInt32(li.SubItems[1].Text);
77 william 296 Process SelectedProcess = Process.GetProcessById(this.ProcessPID);
78     logger.Debug.WriteLine("Using Process: ({0} : {1})", SelectedProcess.Id, SelectedProcess.ProcessName);
79 william 151 //this.Close();
80 william 287 if (OnSelectedProcessChanged != null)
81 william 327 OnSelectedProcessChanged(new ProcessChangedEventArgs(this, this.ProcessPID));
82 william 331 if (isAttachingToProcess)
83     isAttachingToProcess = false;
84 william 88 }
85 william 156 private void btnOK_Click(object sender, EventArgs e)
86     {
87 william 327
88 william 156 }
89 william 88
90 william 151 //private void btnCancel_Click(object sender, EventArgs e)
91     //{
92     // this.SelectedPid = -1;
93     // //this.Close();
94     //}
95 william 88
96     private void btnRefresh_Click(object sender, EventArgs e)
97 william 329 {
98     this.RefreshList();
99 william 88 }
100    
101     private bool RefreshList()
102     {
103 william 329 isRefreshing = true;
104 william 88 lstProcessList.Items.Clear();
105 william 329 this.AcceptedPlugin.Reload(true);
106 william 88 // create a selection of PCSX2 processes
107 william 153 List<ProcContainer> proc_list = AcceptedPlugin.ValidProcessesForPlugin;
108 william 88
109 william 327 if (!(proc_list.Count > 0))
110     { //MessageBox.Show("Could not find any PCSX2 Processes. Please start an instance of one.");
111     return false;
112     }
113 william 88
114     List<ListViewItem> items = new List<ListViewItem>();
115     ImageList small_image_list = new ImageList();
116     ImageList large_image_list = new ImageList();
117 william 109
118 william 110 int small_width = 32;
119     int large_width = 48;
120 william 109
121     small_image_list.ImageSize = new Size(small_width, small_width);
122     large_image_list.ImageSize = new Size(large_width, large_width);
123 william 327 foreach (ProcContainer p in proc_list)
124 william 109 {
125     Bitmap small_image = null;
126     Bitmap large_image = null;
127     string image_key = string.Empty;
128     this.AddProcessItem(p, small_image_list.ImageSize, large_image_list.ImageSize, out image_key, out small_image, out large_image);
129     if (image_key != string.Empty)
130     {
131     if (small_image != null) { small_image_list.Images.Add(image_key, small_image); }
132     if (large_image != null) { large_image_list.Images.Add(image_key, large_image); }
133     ListViewItem item = new ListViewItem();
134 william 327 item.Name = p.GetHashCode().ToString();
135 william 109 // process name
136     item.Text = p.Name;
137     // pid
138     item.SubItems.Add(p.ProcessInfo.Id.ToString());
139     // FullPath
140     item.SubItems.Add(p.FileName);
141     // image key
142     item.ImageKey = p.FileName;
143     if (!items.Contains(item)) items.Add(item);
144     }
145     else
146     {
147 william 156 //logger.Error.WriteLine("image_key is an empty string!");
148     //if (small_image != null) { logger.Error.WriteLine(" [however small_image is not null]"); }
149     //if (large_image != null) { logger.Error.WriteLine(" [however large_image is not null]\n"); }
150 william 109 }
151     }
152 william 88 lstProcessList.SmallImageList = small_image_list;
153     lstProcessList.LargeImageList = large_image_list;
154     this.lstProcessList.Items.AddRange(items.ToArray());
155 william 156
156 william 331 if (lstProcessList.Items.Count > 1 && !isAttachingToProcess)
157 william 156 {
158     lstProcessList.Items[0].Selected = true;
159     }
160 william 329 isRefreshing = false;
161 william 88 return true;
162     }
163    
164 william 109 private void AddProcessItem(ProcContainer p, Size small, Size large, out string image_key, out Bitmap small_image, out Bitmap large_image)
165 william 88 {
166 william 109 image_key = "";
167     small_image = null;
168     large_image = null;
169     ////small_image_list = new ImageList();
170     ////large_image_list = new ImageList();
171     //ListViewItem item = new ListViewItem();
172     //// process
173     //item.Text = p.Name;
174     //if (p.ProcessIcon != null)
175     //{
176     // //small_image_list.Images.Add(
177     // //large_image_list.Images.Add(p.FileName, p.ProcessIcon);
178     // item.ImageKey = p.FileName;
179     //}
180     //// pid
181     //item.SubItems.Add(p.ProcessInfo.Id.ToString());
182     //// FullPath
183     //item.SubItems.Add(p.FileName);
184     //if (!items.Contains(item)) items.Add(item);
185     if (p != null)
186 william 88 {
187 william 109 if (p.ProcessIcon != null)
188     {
189     image_key = p.FileName;
190     small_image = new Bitmap(p.ProcessIcon, small);
191     large_image = new Bitmap(p.ProcessIcon, large);
192     }
193 william 88 }
194 william 109
195 william 88 }
196 william 152
197     private void PIDSelector_Load(object sender, EventArgs e)
198     {
199 william 153 if (AcceptedPlugin == null || this.DesignMode) return;
200 william 152 btnRefresh.PerformClick();
201     }
202 william 156
203    
204     private void lstProcessList_ItemSelectionChanged(object sender, ListViewItemSelectionChangedEventArgs e)
205     {
206     if (!e.IsSelected) return;
207     PerformListViewItemSelect(e.Item);
208     }
209    
210     private void lstProcessList_ItemActivate(object sender, EventArgs e)
211     {
212     ListViewItem Item = lstProcessList.SelectedItems[0];
213     PerformListViewItemSelect(Item);
214     }
215    
216 william 327 private void btnLaunchAndAttach_Click(object sender, EventArgs e)
217     {
218     DialogResult result = EXESelector.ShowDialog();
219     if (result != DialogResult.OK) return;
220     FileInfo fi = new FileInfo(EXESelector.FileName);
221     if (!fi.Exists) { logger.Warn.WriteLine("Cannot load and attach to non-existing exe file: {0}", fi.FullName); return; }
222 william 331 Process p = Process.Start(fi.FullName);
223     this.ProcessPID = p.Id;
224     isAttachingToProcess = true;
225 william 327 }
226 william 329
227     private void ProcessWatcherTimer_Tick(object sender, EventArgs e)
228     {
229     if (isRefreshing || isAutoRefreshing) return;
230     //if (lstProcessList.Items.Count == 0) return;
231     isAutoRefreshing = true;
232     int pid = this.ProcessPID;
233     btnRefresh.PerformClick();
234     bool pid_still_exists = false;
235     int item_index = -1;
236     foreach (ListViewItem li2 in lstProcessList.Items)
237     {
238     int _pid = Convert.ToInt32(li2.SubItems[1].Text);
239     if (_pid == pid)
240     {
241     item_index = lstProcessList.Items.IndexOf(li2);
242     pid_still_exists = true;
243     break;
244     }
245     }
246     if (pid_still_exists)
247     {
248 william 331 //logger.Debug.WriteLine("Pid: {0} still exists", pid);
249 william 329 if (lstProcessList.SelectedItems.Count > 0)
250     {
251     if (lstProcessList.SelectedItems[0] != null)
252     if (lstProcessList.SelectedItems[0].Index != -1)
253     lstProcessList.Items[lstProcessList.SelectedItems[0].Index].Selected = false;
254     }
255     lstProcessList.Items[item_index].Selected = true;
256     }
257     else
258     {
259     if (lstProcessList.SelectedItems.Count > 0)
260     lstProcessList.Items[0].Selected = true;
261     }
262     isAutoRefreshing = false;
263     }
264 william 88 }
265     }
266