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

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

Parent Directory Parent Directory | Revision Log Revision Log


Revision 332 - (show annotations) (download)
Thu Jun 7 19:56:27 2012 UTC (9 years ago) by william
Original Path: trunk/RomCheater/Docking/PIDSelector.cs
File size: 13262 byte(s)
more wip for:
* launch and attach to process
* auto referesh process

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

  ViewVC Help
Powered by ViewVC 1.1.22