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

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

Parent Directory Parent Directory | Revision Log Revision Log


Revision 329 - (hide annotations) (download)
Thu Jun 7 19:18:31 2012 UTC (9 years ago) by william
Original Path: trunk/RomCheater/Docking/PIDSelector.cs
File size: 14068 byte(s)
+ add more logic for auto process refresh

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

  ViewVC Help
Powered by ViewVC 1.1.22