/[RomCheater]/trunk/RomCheater/Docking/FloatingPIDSelector.cs
ViewVC logotype

Annotation of /trunk/RomCheater/Docking/FloatingPIDSelector.cs

Parent Directory Parent Directory | Revision Log Revision Log


Revision 364 - (hide annotations) (download)
Sun Jun 10 02:25:23 2012 UTC (9 years ago) by william
Original Path: trunk/RomCheater/Docking/PIDSelector.cs
File size: 15520 byte(s)

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 363 using System.Reflection;
17 william 88
18 william 149 namespace RomCheater.Docking
19 william 88 {
20 william 196 public partial class PIDSelector : DockContent, IAcceptsPlugin<IConfigPlugin>, IAcceptsChangedProcess, IAcceptsProcessPID
21 william 88 {
22 william 329 private bool isRefreshing = false;
23     private bool isAutoRefreshing = false;
24 william 331 private bool isAttachingToProcess = false;
25 william 152 //private IConfigPlugin plugin = null;
26 william 88 //bool isInError = false;
27 william 196 public PIDSelector() { InitializeComponent(); this.ProcessPID = -1; OnSelectedProcessChanged = null; this.AcceptedPlugin = null; }
28 william 153 public PIDSelector(IConfigPlugin plugin) : this() { this.AcceptedPlugin = plugin; }
29 william 88 // unsued construtor (it's not implmented, may not be)
30     [Obsolete("constructor PIDSelector(int pid) is not implemented", false)]
31 william 196 public PIDSelector(int pid) : this() { this.ProcessPID = pid; }
32 william 88 [Obsolete("constructor PIDSelector(IConfigPlugin plugin, int pid) is not implemented", false)]
33 william 196 public PIDSelector(IConfigPlugin plugin, int pid) : this(plugin) { this.ProcessPID = pid; }
34 william 327
35 william 196 #region IAcceptsProcessPID
36     public int ProcessPID { get; set; }
37     #endregion
38 william 335 #region IAcceptsPlugin<IConfigPlugin> members
39     private IConfigPlugin _AcceptedPlugin;
40     public IConfigPlugin AcceptedPlugin {
41     get { return _AcceptedPlugin; }
42     set { _AcceptedPlugin = value; ProcessWatcherTimer.Enabled = (value != null); } }
43 william 196 #endregion
44     #region IAcceptsChangedProcess members
45 william 287 public event BaseEventHandler<ProcessChangedEventArgs> OnSelectedProcessChanged;
46 william 196 #endregion
47 william 151
48    
49 william 327
50 william 88 public new void Show() { this.Show(null); }
51 william 150 //public new void Show(IWin32Window owner) { this.PreInitShow(owner); }
52     public new void Show(DockPanel panel) { this.PreInitShow(panel); }
53     //public new DialogResult ShowDialog() { return this.ShowDialog(null); }
54     //public new DialogResult ShowDialog(IWin32Window owner) { return this.PreInitDialog(owner); }
55     //private void PreInitShow(IWin32Window owner)
56     //{
57     // if (!this.RefreshList()) { MessageBox.Show(string.Format("Could not find any Processes for plugin {0}. Please start an instance of one.",plugin.ToString())); }
58     // else { if (owner == null) { base.Show(); } else { base.Show(owner); } }
59     //}
60     private void PreInitShow(DockPanel panel)
61 william 88 {
62 william 150 //if (panel == null) return;
63 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())); }
64 william 150 else { if (panel == null) { base.Show(); } else { base.Show(panel); } }
65 william 88 }
66 william 150 //private DialogResult PreInitDialog(IWin32Window owner)
67     //{
68     // 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; }
69     // else { if (owner == null) { return base.ShowDialog(); } else { return base.ShowDialog(owner); } }
70     //}
71 william 363 //private void SetControlBackColor(Control control, KnownColor color) { SetControlBackColor(control, Color.FromKnownColor(color)); }
72     //private void SetControlBackColor(Control control, Color color) { control.BackColor = color; }
73     private void SetControlBackColor<T>(T component, KnownColor color) { SetControlBackColor(component, Color.FromKnownColor(color)); }
74     private void SetControlBackColor<T>(T component, Color color)
75     {
76     Type ComponentType = component.GetType();
77     PropertyInfo pBackColor = ComponentType.GetProperty("BackColor");
78     pBackColor.SetValue(component, color, null);
79     }
80     private void SetViewForProcessListView(View view)
81     {
82 william 364 Color baseColor = Color.FromKnownColor(KnownColor.GradientActiveCaption);
83     int GB_OFFSET = 10;
84     Color c = Color.FromArgb(baseColor.A, baseColor.R, baseColor.G + GB_OFFSET, baseColor.B + GB_OFFSET);
85 william 363 ResetViewButtonControls();
86     switch (view)
87     {
88     case View.Details: SetControlBackColor(btnDetails, c); break;
89     case View.LargeIcon: SetControlBackColor(btnLargeIcon, c); break;
90     case View.List: SetControlBackColor(btnList, c); break;
91     case View.SmallIcon: SetControlBackColor(btnSmallIcon, c); break;
92     case View.Tile: SetControlBackColor(btnTile, c); break;
93     }
94     lstProcessList.View = view;
95     }
96     private void ResetViewButtonControls()
97     {
98     KnownColor DefaultColor = KnownColor.Control;
99     SetControlBackColor(btnLargeIcon, DefaultColor);
100     SetControlBackColor(btnDetails, DefaultColor);
101     SetControlBackColor(btnSmallIcon, DefaultColor);
102     SetControlBackColor(btnList, DefaultColor);
103     SetControlBackColor(btnTile, DefaultColor);
104     }
105     private void btnLargeIcon_Click(object sender, EventArgs e) { SetViewForProcessListView(View.LargeIcon); }
106     private void btnDetails_Click(object sender, EventArgs e) { SetViewForProcessListView(View.Details); }
107     private void btnSmallIcon_Click(object sender, EventArgs e) { SetViewForProcessListView(View.SmallIcon); }
108     private void btnList_Click(object sender, EventArgs e) { SetViewForProcessListView(View.List); }
109     private void btnTile_Click(object sender, EventArgs e) { SetViewForProcessListView(View.Tile); }
110 william 88
111 william 156 private void PerformListViewItemSelect(ListViewItem li)
112 william 327 {
113 william 331 if (isAutoRefreshing && !isAttachingToProcess) return;
114 william 196 this.ProcessPID = Convert.ToInt32(li.SubItems[1].Text);
115 william 296 Process SelectedProcess = Process.GetProcessById(this.ProcessPID);
116 william 332
117     txtstatus_Pid.Text = SelectedProcess.Id.ToString();
118     txtstatus_Filename.Text = SelectedProcess.MainModule.FileName;
119    
120 william 296 logger.Debug.WriteLine("Using Process: ({0} : {1})", SelectedProcess.Id, SelectedProcess.ProcessName);
121 william 151 //this.Close();
122 william 287 if (OnSelectedProcessChanged != null)
123 william 327 OnSelectedProcessChanged(new ProcessChangedEventArgs(this, this.ProcessPID));
124 william 331 if (isAttachingToProcess)
125     isAttachingToProcess = false;
126 william 88 }
127 william 156 private void btnOK_Click(object sender, EventArgs e)
128     {
129 william 327
130 william 156 }
131 william 88
132 william 151 //private void btnCancel_Click(object sender, EventArgs e)
133     //{
134     // this.SelectedPid = -1;
135     // //this.Close();
136     //}
137 william 88
138     private void btnRefresh_Click(object sender, EventArgs e)
139 william 335 {
140     //this.RefreshList();
141 william 88 }
142    
143     private bool RefreshList()
144     {
145 william 338 lstProcessList.BeginUpdate();
146 william 335 //if (this.AcceptedPlugin == null) { return false; };
147 william 329 isRefreshing = true;
148 william 88 lstProcessList.Items.Clear();
149 william 335 //logger.VerboseDebug.WriteLine(System.Environment.StackTrace);
150 william 329 this.AcceptedPlugin.Reload(true);
151 william 88 // create a selection of PCSX2 processes
152 william 153 List<ProcContainer> proc_list = AcceptedPlugin.ValidProcessesForPlugin;
153 william 88
154 william 327 if (!(proc_list.Count > 0))
155     { //MessageBox.Show("Could not find any PCSX2 Processes. Please start an instance of one.");
156     return false;
157     }
158 william 88
159     List<ListViewItem> items = new List<ListViewItem>();
160     ImageList small_image_list = new ImageList();
161     ImageList large_image_list = new ImageList();
162 william 109
163 william 110 int small_width = 32;
164     int large_width = 48;
165 william 109
166     small_image_list.ImageSize = new Size(small_width, small_width);
167     large_image_list.ImageSize = new Size(large_width, large_width);
168 william 327 foreach (ProcContainer p in proc_list)
169 william 109 {
170     Bitmap small_image = null;
171     Bitmap large_image = null;
172     string image_key = string.Empty;
173     this.AddProcessItem(p, small_image_list.ImageSize, large_image_list.ImageSize, out image_key, out small_image, out large_image);
174     if (image_key != string.Empty)
175     {
176     if (small_image != null) { small_image_list.Images.Add(image_key, small_image); }
177     if (large_image != null) { large_image_list.Images.Add(image_key, large_image); }
178     ListViewItem item = new ListViewItem();
179 william 327 item.Name = p.GetHashCode().ToString();
180 william 109 // process name
181     item.Text = p.Name;
182     // pid
183     item.SubItems.Add(p.ProcessInfo.Id.ToString());
184     // FullPath
185     item.SubItems.Add(p.FileName);
186     // image key
187     item.ImageKey = p.FileName;
188     if (!items.Contains(item)) items.Add(item);
189     }
190     else
191     {
192 william 156 //logger.Error.WriteLine("image_key is an empty string!");
193     //if (small_image != null) { logger.Error.WriteLine(" [however small_image is not null]"); }
194     //if (large_image != null) { logger.Error.WriteLine(" [however large_image is not null]\n"); }
195 william 109 }
196     }
197 william 88 lstProcessList.SmallImageList = small_image_list;
198     lstProcessList.LargeImageList = large_image_list;
199     this.lstProcessList.Items.AddRange(items.ToArray());
200 william 156
201 william 331 if (lstProcessList.Items.Count > 1 && !isAttachingToProcess)
202 william 156 {
203     lstProcessList.Items[0].Selected = true;
204     }
205 william 329 isRefreshing = false;
206 william 338 lstProcessList.EndUpdate();
207 william 88 return true;
208     }
209    
210 william 109 private void AddProcessItem(ProcContainer p, Size small, Size large, out string image_key, out Bitmap small_image, out Bitmap large_image)
211 william 88 {
212 william 109 image_key = "";
213     small_image = null;
214     large_image = null;
215     ////small_image_list = new ImageList();
216     ////large_image_list = new ImageList();
217     //ListViewItem item = new ListViewItem();
218     //// process
219     //item.Text = p.Name;
220     //if (p.ProcessIcon != null)
221     //{
222     // //small_image_list.Images.Add(
223     // //large_image_list.Images.Add(p.FileName, p.ProcessIcon);
224     // item.ImageKey = p.FileName;
225     //}
226     //// pid
227     //item.SubItems.Add(p.ProcessInfo.Id.ToString());
228     //// FullPath
229     //item.SubItems.Add(p.FileName);
230     //if (!items.Contains(item)) items.Add(item);
231     if (p != null)
232 william 88 {
233 william 109 if (p.ProcessIcon != null)
234     {
235     image_key = p.FileName;
236     small_image = new Bitmap(p.ProcessIcon, small);
237     large_image = new Bitmap(p.ProcessIcon, large);
238     }
239 william 88 }
240 william 109
241 william 88 }
242 william 152
243     private void PIDSelector_Load(object sender, EventArgs e)
244     {
245 william 153 if (AcceptedPlugin == null || this.DesignMode) return;
246 william 332 txtstatus_Pid.Text = "";
247     txtstatus_Filename.Text = "";
248 william 363 ResetViewButtonControls();
249     SetControlBackColor(btnList, KnownColor.ActiveCaption);
250     SetViewForProcessListView(View.List);
251 william 335 this.RefreshList();
252 william 152 }
253 william 156
254    
255     private void lstProcessList_ItemSelectionChanged(object sender, ListViewItemSelectionChangedEventArgs e)
256     {
257     if (!e.IsSelected) return;
258     PerformListViewItemSelect(e.Item);
259     }
260    
261     private void lstProcessList_ItemActivate(object sender, EventArgs e)
262     {
263     ListViewItem Item = lstProcessList.SelectedItems[0];
264     PerformListViewItemSelect(Item);
265     }
266    
267 william 327 private void btnLaunchAndAttach_Click(object sender, EventArgs e)
268     {
269     DialogResult result = EXESelector.ShowDialog();
270     if (result != DialogResult.OK) return;
271     FileInfo fi = new FileInfo(EXESelector.FileName);
272     if (!fi.Exists) { logger.Warn.WriteLine("Cannot load and attach to non-existing exe file: {0}", fi.FullName); return; }
273 william 331 Process p = Process.Start(fi.FullName);
274     this.ProcessPID = p.Id;
275 william 335 isAttachingToProcess = true;
276 william 327 }
277 william 329
278     private void ProcessWatcherTimer_Tick(object sender, EventArgs e)
279     {
280     if (isRefreshing || isAutoRefreshing) return;
281     //if (lstProcessList.Items.Count == 0) return;
282     isAutoRefreshing = true;
283 william 332 int pid = this.ProcessPID;
284 william 335 this.RefreshList();
285 william 329 bool pid_still_exists = false;
286     int item_index = -1;
287     foreach (ListViewItem li2 in lstProcessList.Items)
288     {
289     int _pid = Convert.ToInt32(li2.SubItems[1].Text);
290     if (_pid == pid)
291     {
292     item_index = lstProcessList.Items.IndexOf(li2);
293     pid_still_exists = true;
294     break;
295     }
296     }
297     if (pid_still_exists)
298     {
299 william 335 //logger.Debug.WriteLine("Pid: {0} still exists", pid);
300 william 329 if (lstProcessList.SelectedItems.Count > 0)
301     {
302     if (lstProcessList.SelectedItems[0] != null)
303     if (lstProcessList.SelectedItems[0].Index != -1)
304     lstProcessList.Items[lstProcessList.SelectedItems[0].Index].Selected = false;
305     }
306     lstProcessList.Items[item_index].Selected = true;
307 william 332 isAutoRefreshing = false;
308 william 329 }
309     else
310     {
311     if (lstProcessList.SelectedItems.Count > 0)
312 william 332 {
313     if (lstProcessList.SelectedItems[0] != null)
314     {
315 william 353 int missing_pid = this.ProcessPID;
316     logger.Warn.WriteLine("Connection to selected process ({0}) has been lost...Selecting the first available process.", missing_pid);
317     MessageBox.Show(string.Format("Connection to selected process ({0}) has been lost...Selecting the first available process.", missing_pid), "Connection to selected process lost", MessageBoxButtons.OK, MessageBoxIcon.Warning);
318 william 332 }
319     isAutoRefreshing = false;
320     lstProcessList.Items[0].Selected = false;
321 william 329 lstProcessList.Items[0].Selected = true;
322 william 332 }
323 william 329 }
324     }
325 william 88 }
326     }
327    

  ViewVC Help
Powered by ViewVC 1.1.22