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

Contents of /trunk/RomCheater/Docking/PIDSelector.cs

Parent Directory Parent Directory | Revision Log Revision Log


Revision 365 - (show annotations) (download)
Sun Jun 10 02:53:09 2012 UTC (8 years, 3 months ago) by william
File size: 15933 byte(s)

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

  ViewVC Help
Powered by ViewVC 1.1.22