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

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

Parent Directory Parent Directory | Revision Log Revision Log


Revision 686 - (show annotations) (download)
Mon Jun 17 09:31:52 2013 UTC (8 years ago) by william
File size: 17342 byte(s)
+ massive move of source files

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

  ViewVC Help
Powered by ViewVC 1.1.22