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

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

Parent Directory Parent Directory | Revision Log Revision Log


Revision 332 - (hide 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 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 331 private bool isAttachingToProcess = false;
24 william 152 //private IConfigPlugin plugin = null;
25 william 88 //bool isInError = false;
26 william 196 public PIDSelector() { InitializeComponent(); this.ProcessPID = -1; OnSelectedProcessChanged = null; this.AcceptedPlugin = null; }
27 william 153 public PIDSelector(IConfigPlugin plugin) : this() { this.AcceptedPlugin = plugin; }
28 william 88 // unsued construtor (it's not implmented, may not be)
29     [Obsolete("constructor PIDSelector(int pid) is not implemented", false)]
30 william 196 public PIDSelector(int pid) : this() { this.ProcessPID = pid; }
31 william 88 [Obsolete("constructor PIDSelector(IConfigPlugin plugin, int pid) is not implemented", false)]
32 william 196 public PIDSelector(IConfigPlugin plugin, int pid) : this(plugin) { this.ProcessPID = pid; }
33 william 327
34 william 196 #region IAcceptsProcessPID
35     public int ProcessPID { get; set; }
36     #endregion
37     #region IAcceptsPlugin<IConfigPlugin> members
38 william 153 public IConfigPlugin AcceptedPlugin { get; set; }
39 william 196 #endregion
40     #region IAcceptsChangedProcess members
41 william 287 public event BaseEventHandler<ProcessChangedEventArgs> OnSelectedProcessChanged;
42 william 196 #endregion
43 william 151
44    
45 william 327
46 william 88 public new void Show() { this.Show(null); }
47 william 150 //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 william 88 {
58 william 150 //if (panel == null) return;
59 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())); }
60 william 150 else { if (panel == null) { base.Show(); } else { base.Show(panel); } }
61 william 88 }
62 william 150 //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 william 88 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 william 156 private void PerformListViewItemSelect(ListViewItem li)
74 william 327 {
75 william 331 if (isAutoRefreshing && !isAttachingToProcess) return;
76 william 196 this.ProcessPID = Convert.ToInt32(li.SubItems[1].Text);
77 william 296 Process SelectedProcess = Process.GetProcessById(this.ProcessPID);
78 william 332
79     txtstatus_Pid.Text = SelectedProcess.Id.ToString();
80     txtstatus_Filename.Text = SelectedProcess.MainModule.FileName;
81    
82 william 296 logger.Debug.WriteLine("Using Process: ({0} : {1})", SelectedProcess.Id, SelectedProcess.ProcessName);
83 william 151 //this.Close();
84 william 287 if (OnSelectedProcessChanged != null)
85 william 327 OnSelectedProcessChanged(new ProcessChangedEventArgs(this, this.ProcessPID));
86 william 331 if (isAttachingToProcess)
87     isAttachingToProcess = false;
88 william 88 }
89 william 156 private void btnOK_Click(object sender, EventArgs e)
90     {
91 william 327
92 william 156 }
93 william 88
94 william 151 //private void btnCancel_Click(object sender, EventArgs e)
95     //{
96     // this.SelectedPid = -1;
97     // //this.Close();
98     //}
99 william 88
100     private void btnRefresh_Click(object sender, EventArgs e)
101 william 329 {
102     this.RefreshList();
103 william 88 }
104    
105     private bool RefreshList()
106     {
107 william 329 isRefreshing = true;
108 william 88 lstProcessList.Items.Clear();
109 william 329 this.AcceptedPlugin.Reload(true);
110 william 88 // create a selection of PCSX2 processes
111 william 153 List<ProcContainer> proc_list = AcceptedPlugin.ValidProcessesForPlugin;
112 william 88
113 william 327 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 william 88
118     List<ListViewItem> items = new List<ListViewItem>();
119     ImageList small_image_list = new ImageList();
120     ImageList large_image_list = new ImageList();
121 william 109
122 william 110 int small_width = 32;
123     int large_width = 48;
124 william 109
125     small_image_list.ImageSize = new Size(small_width, small_width);
126     large_image_list.ImageSize = new Size(large_width, large_width);
127 william 327 foreach (ProcContainer p in proc_list)
128 william 109 {
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 william 327 item.Name = p.GetHashCode().ToString();
139 william 109 // 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 william 156 //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 william 109 }
155     }
156 william 88 lstProcessList.SmallImageList = small_image_list;
157     lstProcessList.LargeImageList = large_image_list;
158     this.lstProcessList.Items.AddRange(items.ToArray());
159 william 156
160 william 331 if (lstProcessList.Items.Count > 1 && !isAttachingToProcess)
161 william 156 {
162     lstProcessList.Items[0].Selected = true;
163     }
164 william 329 isRefreshing = false;
165 william 88 return true;
166     }
167    
168 william 109 private void AddProcessItem(ProcContainer p, Size small, Size large, out string image_key, out Bitmap small_image, out Bitmap large_image)
169 william 88 {
170 william 109 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 william 88 {
191 william 109 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 william 88 }
198 william 109
199 william 88 }
200 william 152
201     private void PIDSelector_Load(object sender, EventArgs e)
202     {
203 william 153 if (AcceptedPlugin == null || this.DesignMode) return;
204 william 332 txtstatus_Pid.Text = "";
205     txtstatus_Filename.Text = "";
206 william 152 btnRefresh.PerformClick();
207     }
208 william 156
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 william 327 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 william 331 Process p = Process.Start(fi.FullName);
229     this.ProcessPID = p.Id;
230     isAttachingToProcess = true;
231 william 327 }
232 william 329
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 william 332 int pid = this.ProcessPID;
239 william 329 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 william 331 //logger.Debug.WriteLine("Pid: {0} still exists", pid);
255 william 329 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 william 332 isAutoRefreshing = false;
263 william 329 }
264     else
265     {
266     if (lstProcessList.SelectedItems.Count > 0)
267 william 332 {
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 william 329 lstProcessList.Items[0].Selected = true;
278 william 332 }
279 william 329 }
280     }
281 william 88 }
282     }
283    

  ViewVC Help
Powered by ViewVC 1.1.22