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

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

Parent Directory Parent Directory | Revision Log Revision Log


Revision 812 - (show annotations) (download)
Tue Apr 15 14:52:10 2014 UTC (5 years, 9 months ago) by william
File size: 17363 byte(s)
+ update logging to use Enterpise.Logging -- more work is still needed

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

  ViewVC Help
Powered by ViewVC 1.1.22