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

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

Parent Directory Parent Directory | Revision Log Revision Log


Revision 329 - (show annotations) (download)
Thu Jun 7 19:18:31 2012 UTC (8 years, 3 months ago) by william
File size: 14068 byte(s)
+ add more logic for auto process refresh

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
17 namespace RomCheater.Docking
18 {
19 public partial class PIDSelector : DockContent, IAcceptsPlugin<IConfigPlugin>, IAcceptsChangedProcess, IAcceptsProcessPID
20 {
21 private bool isRefreshing = false;
22 private bool isAutoRefreshing = false;
23 //private IConfigPlugin plugin = null;
24 //bool isInError = false;
25 public PIDSelector() { InitializeComponent(); this.ProcessPID = -1; OnSelectedProcessChanged = null; this.AcceptedPlugin = null; }
26 public PIDSelector(IConfigPlugin plugin) : this() { this.AcceptedPlugin = plugin; }
27 // unsued construtor (it's not implmented, may not be)
28 [Obsolete("constructor PIDSelector(int pid) is not implemented", false)]
29 public PIDSelector(int pid) : this() { this.ProcessPID = pid; }
30 [Obsolete("constructor PIDSelector(IConfigPlugin plugin, int pid) is not implemented", false)]
31 public PIDSelector(IConfigPlugin plugin, int pid) : this(plugin) { this.ProcessPID = pid; }
32
33 #region IAcceptsProcessPID
34 public int ProcessPID { get; set; }
35 #endregion
36 #region IAcceptsPlugin<IConfigPlugin> members
37 public IConfigPlugin AcceptedPlugin { get; set; }
38 #endregion
39 #region IAcceptsChangedProcess members
40 public event BaseEventHandler<ProcessChangedEventArgs> OnSelectedProcessChanged;
41 #endregion
42
43
44
45 public new void Show() { this.Show(null); }
46 //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 {
57 //if (panel == null) return;
58 if (!this.RefreshList()) { MessageBox.Show(string.Format("Could not find any Processes for plugin {0}. Please start an instance of one.", AcceptedPlugin.ToString())); }
59 else { if (panel == null) { base.Show(); } else { base.Show(panel); } }
60 }
61 //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 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 private void PerformListViewItemSelect(ListViewItem li)
73 {
74 if (isAutoRefreshing) return;
75 this.ProcessPID = Convert.ToInt32(li.SubItems[1].Text);
76 Process SelectedProcess = Process.GetProcessById(this.ProcessPID);
77 logger.Debug.WriteLine("Using Process: ({0} : {1})", SelectedProcess.Id, SelectedProcess.ProcessName);
78 //this.Close();
79 if (OnSelectedProcessChanged != null)
80 OnSelectedProcessChanged(new ProcessChangedEventArgs(this, this.ProcessPID));
81 }
82 private void btnOK_Click(object sender, EventArgs e)
83 {
84
85 }
86
87 //private void btnCancel_Click(object sender, EventArgs e)
88 //{
89 // this.SelectedPid = -1;
90 // //this.Close();
91 //}
92
93 private void btnRefresh_Click(object sender, EventArgs e)
94 {
95 this.RefreshList();
96 }
97
98 private bool RefreshList()
99 {
100 isRefreshing = true;
101 lstProcessList.Items.Clear();
102 this.AcceptedPlugin.Reload(true);
103 // create a selection of PCSX2 processes
104 List<ProcContainer> proc_list = AcceptedPlugin.ValidProcessesForPlugin;
105
106 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
111 List<ListViewItem> items = new List<ListViewItem>();
112 ImageList small_image_list = new ImageList();
113 ImageList large_image_list = new ImageList();
114
115 int small_width = 32;
116 int large_width = 48;
117
118 small_image_list.ImageSize = new Size(small_width, small_width);
119 large_image_list.ImageSize = new Size(large_width, large_width);
120 foreach (ProcContainer p in proc_list)
121 {
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 item.Name = p.GetHashCode().ToString();
132 // 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 //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 }
148 }
149 lstProcessList.SmallImageList = small_image_list;
150 lstProcessList.LargeImageList = large_image_list;
151 this.lstProcessList.Items.AddRange(items.ToArray());
152
153 if (lstProcessList.Items.Count > 1)
154 {
155 lstProcessList.Items[0].Selected = true;
156 }
157 isRefreshing = false;
158 return true;
159 }
160
161 private void AddProcessItem(ProcContainer p, Size small, Size large, out string image_key, out Bitmap small_image, out Bitmap large_image)
162 {
163 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 {
184 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 }
191
192 }
193
194 private void PIDSelector_Load(object sender, EventArgs e)
195 {
196 if (AcceptedPlugin == null || this.DesignMode) return;
197 btnRefresh.PerformClick();
198 }
199
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 private void btnLaunchAndAttach_Click(object sender, EventArgs e)
214 {
215 DialogResult result = EXESelector.ShowDialog();
216 if (result != DialogResult.OK) return;
217
218 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 if (lstProcessList.Items.Count == 2)
261 {
262 lstProcessList.Items[0].Selected = false;
263 lstProcessList.Items[1].Selected = true;
264 }
265 else
266 {
267 if (lstProcessList.SelectedItems.Count > 0)
268 lstProcessList.Items[lstProcessList.SelectedItems[0].Index].Selected = false;
269 lstProcessList.Items[lstProcessList.Items.IndexOf(item)].Selected = true;
270 }
271 }
272
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 }
311 }
312

  ViewVC Help
Powered by ViewVC 1.1.22