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

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

Parent Directory Parent Directory | Revision Log Revision Log


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

1 william 414 #region Logging Defines
2     // include this any class or method that required logging, and comment-out what is not needed
3 william 415
4 william 414 #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 william 88 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 william 686
23 william 88 using RomCheater.PluginFramework.Core;
24 william 109 using System.IO;
25     using RomCheater.Logging;
26 william 148 using WeifenLuo.WinFormsUI.Docking;
27 william 196 using RomCheater.PluginFramework.Events;
28 william 329 using System.Management;
29 william 363 using System.Reflection;
30 william 686 using RomCheater.Core;
31 william 812 using Enterprise.Logging;
32 william 88
33 william 149 namespace RomCheater.Docking
34 william 88 {
35 william 366 public partial class FloatingPIDSelector : DockContent, IAcceptsPlugin<IConfigPlugin>, IAcceptsChangedProcess, IAcceptsProcessPID
36 william 88 {
37 william 365 private const int ProcessRefershInterval = 5000; // refresh interval in milliseconds
38     private bool isUserInteracting = false;
39 william 329 private bool isRefreshing = false;
40     private bool isAutoRefreshing = false;
41 william 331 private bool isAttachingToProcess = false;
42 william 152 //private IConfigPlugin plugin = null;
43 william 88 //bool isInError = false;
44 william 366 public FloatingPIDSelector() { InitializeComponent(); this.ProcessPID = -1; OnSelectedProcessChanged = null; this.AcceptedPlugin = null; }
45     public FloatingPIDSelector(IConfigPlugin plugin) : this() { this.AcceptedPlugin = plugin; }
46 william 88 // unsued construtor (it's not implmented, may not be)
47     [Obsolete("constructor PIDSelector(int pid) is not implemented", false)]
48 william 366 public FloatingPIDSelector(int pid) : this() { this.ProcessPID = pid; }
49 william 88 [Obsolete("constructor PIDSelector(IConfigPlugin plugin, int pid) is not implemented", false)]
50 william 366 public FloatingPIDSelector(IConfigPlugin plugin, int pid) : this(plugin) { this.ProcessPID = pid; }
51 william 327
52 william 196 #region IAcceptsProcessPID
53     public int ProcessPID { get; set; }
54     #endregion
55 william 335 #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 william 196 #endregion
61     #region IAcceptsChangedProcess members
62 william 287 public event BaseEventHandler<ProcessChangedEventArgs> OnSelectedProcessChanged;
63 william 196 #endregion
64 william 151
65    
66 william 327
67 william 88 public new void Show() { this.Show(null); }
68 william 150 //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 william 88 {
79 william 150 //if (panel == null) return;
80 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())); }
81 william 150 else { if (panel == null) { base.Show(); } else { base.Show(panel); } }
82 william 88 }
83 william 150 //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 william 363 //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 william 364 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 william 363 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 william 88
128 william 156 private void PerformListViewItemSelect(ListViewItem li)
129 william 327 {
130 william 331 if (isAutoRefreshing && !isAttachingToProcess) return;
131 william 196 this.ProcessPID = Convert.ToInt32(li.SubItems[1].Text);
132 william 296 Process SelectedProcess = Process.GetProcessById(this.ProcessPID);
133 william 332
134     txtstatus_Pid.Text = SelectedProcess.Id.ToString();
135     txtstatus_Filename.Text = SelectedProcess.MainModule.FileName;
136    
137 william 812 gLog.Debug.WriteLine("Using Process: ({0} : {1})", SelectedProcess.Id, SelectedProcess.ProcessName);
138 william 151 //this.Close();
139 william 287 if (OnSelectedProcessChanged != null)
140 william 327 OnSelectedProcessChanged(new ProcessChangedEventArgs(this, this.ProcessPID));
141 william 331 if (isAttachingToProcess)
142     isAttachingToProcess = false;
143 william 88 }
144 william 156 private void btnOK_Click(object sender, EventArgs e)
145     {
146 william 327
147 william 156 }
148 william 88
149 william 151 //private void btnCancel_Click(object sender, EventArgs e)
150     //{
151     // this.SelectedPid = -1;
152     // //this.Close();
153     //}
154 william 88
155     private void btnRefresh_Click(object sender, EventArgs e)
156 william 335 {
157     //this.RefreshList();
158 william 88 }
159    
160     private bool RefreshList()
161     {
162 william 338 lstProcessList.BeginUpdate();
163 william 335 //if (this.AcceptedPlugin == null) { return false; };
164 william 329 isRefreshing = true;
165 william 88 lstProcessList.Items.Clear();
166 william 335 //logger.VerboseDebug.WriteLine(System.Environment.StackTrace);
167 william 329 this.AcceptedPlugin.Reload(true);
168 william 88 // create a selection of PCSX2 processes
169 william 153 List<ProcContainer> proc_list = AcceptedPlugin.ValidProcessesForPlugin;
170 william 88
171 william 327 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 william 88
176     List<ListViewItem> items = new List<ListViewItem>();
177     ImageList small_image_list = new ImageList();
178     ImageList large_image_list = new ImageList();
179 william 109
180 william 365 int small_width = 24;
181     int large_width = 32;
182 william 109
183     small_image_list.ImageSize = new Size(small_width, small_width);
184     large_image_list.ImageSize = new Size(large_width, large_width);
185 william 327 foreach (ProcContainer p in proc_list)
186 william 109 {
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 william 327 item.Name = p.GetHashCode().ToString();
197 william 109 // 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 william 156 //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 william 380 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 william 109 }
224     }
225 william 88 lstProcessList.SmallImageList = small_image_list;
226     lstProcessList.LargeImageList = large_image_list;
227     this.lstProcessList.Items.AddRange(items.ToArray());
228 william 156
229 william 331 if (lstProcessList.Items.Count > 1 && !isAttachingToProcess)
230 william 156 {
231     lstProcessList.Items[0].Selected = true;
232     }
233 william 329 isRefreshing = false;
234 william 338 lstProcessList.EndUpdate();
235 william 88 return true;
236     }
237    
238 william 109 private void AddProcessItem(ProcContainer p, Size small, Size large, out string image_key, out Bitmap small_image, out Bitmap large_image)
239 william 88 {
240 william 109 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 william 88 {
261 william 109 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 william 381 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 william 88 }
275 william 109
276 william 88 }
277 william 152
278     private void PIDSelector_Load(object sender, EventArgs e)
279     {
280 william 153 if (AcceptedPlugin == null || this.DesignMode) return;
281 william 365 ProcessWatcherTimer.Interval = ProcessRefershInterval;
282 william 332 txtstatus_Pid.Text = "";
283     txtstatus_Filename.Text = "";
284 william 365 SetViewForProcessListView(View.LargeIcon);
285 william 335 this.RefreshList();
286 william 152 }
287 william 156
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 william 327 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 william 812 if (!fi.Exists) { gLog.Warn.WriteLine("Cannot load and attach to non-existing exe file: {0}", fi.FullName); return; }
307 william 331 Process p = Process.Start(fi.FullName);
308     this.ProcessPID = p.Id;
309 william 335 isAttachingToProcess = true;
310 william 327 }
311 william 329
312     private void ProcessWatcherTimer_Tick(object sender, EventArgs e)
313     {
314 william 399 if (this.AcceptedPlugin != null)
315     if (this.AcceptedPlugin.SearchInProgess)
316     return;
317 william 365 if (isRefreshing || isAutoRefreshing || isUserInteracting) return;
318 william 329 //if (lstProcessList.Items.Count == 0) return;
319     isAutoRefreshing = true;
320 william 332 int pid = this.ProcessPID;
321 william 335 this.RefreshList();
322 william 329 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 william 335 //logger.Debug.WriteLine("Pid: {0} still exists", pid);
337 william 329 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 william 332 isAutoRefreshing = false;
345 william 329 }
346     else
347     {
348     if (lstProcessList.SelectedItems.Count > 0)
349 william 332 {
350     if (lstProcessList.SelectedItems[0] != null)
351     {
352 william 353 int missing_pid = this.ProcessPID;
353 william 812 gLog.Warn.WriteLine("Connection to selected process ({0}) has been lost...Selecting the first available process.", missing_pid);
354 william 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);
355 william 332 }
356     isAutoRefreshing = false;
357     lstProcessList.Items[0].Selected = false;
358 william 329 lstProcessList.Items[0].Selected = true;
359 william 332 }
360 william 329 }
361     }
362 william 365
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 william 88 }
374     }
375    

  ViewVC Help
Powered by ViewVC 1.1.22