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

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

Parent Directory Parent Directory | Revision Log Revision Log


Revision 362 - (show annotations) (download)
Sun Jun 10 01:35:18 2012 UTC (9 years ago) by william
Original Path: trunk/RomCheater/Docking/PIDSelector.cs
File size: 13434 byte(s)
+ make default view: list

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

  ViewVC Help
Powered by ViewVC 1.1.22