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 |
//if (this.AcceptedPlugin == null) { return false; }; |
111 |
isRefreshing = true; |
112 |
lstProcessList.Items.Clear(); |
113 |
//logger.VerboseDebug.WriteLine(System.Environment.StackTrace); |
114 |
this.AcceptedPlugin.Reload(true); |
115 |
// create a selection of PCSX2 processes |
116 |
List<ProcContainer> proc_list = AcceptedPlugin.ValidProcessesForPlugin; |
117 |
|
118 |
if (!(proc_list.Count > 0)) |
119 |
{ //MessageBox.Show("Could not find any PCSX2 Processes. Please start an instance of one."); |
120 |
return false; |
121 |
} |
122 |
|
123 |
List<ListViewItem> items = new List<ListViewItem>(); |
124 |
ImageList small_image_list = new ImageList(); |
125 |
ImageList large_image_list = new ImageList(); |
126 |
|
127 |
int small_width = 32; |
128 |
int large_width = 48; |
129 |
|
130 |
small_image_list.ImageSize = new Size(small_width, small_width); |
131 |
large_image_list.ImageSize = new Size(large_width, large_width); |
132 |
foreach (ProcContainer p in proc_list) |
133 |
{ |
134 |
Bitmap small_image = null; |
135 |
Bitmap large_image = null; |
136 |
string image_key = string.Empty; |
137 |
this.AddProcessItem(p, small_image_list.ImageSize, large_image_list.ImageSize, out image_key, out small_image, out large_image); |
138 |
if (image_key != string.Empty) |
139 |
{ |
140 |
if (small_image != null) { small_image_list.Images.Add(image_key, small_image); } |
141 |
if (large_image != null) { large_image_list.Images.Add(image_key, large_image); } |
142 |
ListViewItem item = new ListViewItem(); |
143 |
item.Name = p.GetHashCode().ToString(); |
144 |
// process name |
145 |
item.Text = p.Name; |
146 |
// pid |
147 |
item.SubItems.Add(p.ProcessInfo.Id.ToString()); |
148 |
// FullPath |
149 |
item.SubItems.Add(p.FileName); |
150 |
// image key |
151 |
item.ImageKey = p.FileName; |
152 |
if (!items.Contains(item)) items.Add(item); |
153 |
} |
154 |
else |
155 |
{ |
156 |
//logger.Error.WriteLine("image_key is an empty string!"); |
157 |
//if (small_image != null) { logger.Error.WriteLine(" [however small_image is not null]"); } |
158 |
//if (large_image != null) { logger.Error.WriteLine(" [however large_image is not null]\n"); } |
159 |
} |
160 |
} |
161 |
lstProcessList.SmallImageList = small_image_list; |
162 |
lstProcessList.LargeImageList = large_image_list; |
163 |
this.lstProcessList.Items.AddRange(items.ToArray()); |
164 |
|
165 |
if (lstProcessList.Items.Count > 1 && !isAttachingToProcess) |
166 |
{ |
167 |
lstProcessList.Items[0].Selected = true; |
168 |
} |
169 |
isRefreshing = false; |
170 |
return true; |
171 |
} |
172 |
|
173 |
private void AddProcessItem(ProcContainer p, Size small, Size large, out string image_key, out Bitmap small_image, out Bitmap large_image) |
174 |
{ |
175 |
image_key = ""; |
176 |
small_image = null; |
177 |
large_image = null; |
178 |
////small_image_list = new ImageList(); |
179 |
////large_image_list = new ImageList(); |
180 |
//ListViewItem item = new ListViewItem(); |
181 |
//// process |
182 |
//item.Text = p.Name; |
183 |
//if (p.ProcessIcon != null) |
184 |
//{ |
185 |
// //small_image_list.Images.Add( |
186 |
// //large_image_list.Images.Add(p.FileName, p.ProcessIcon); |
187 |
// item.ImageKey = p.FileName; |
188 |
//} |
189 |
//// pid |
190 |
//item.SubItems.Add(p.ProcessInfo.Id.ToString()); |
191 |
//// FullPath |
192 |
//item.SubItems.Add(p.FileName); |
193 |
//if (!items.Contains(item)) items.Add(item); |
194 |
if (p != null) |
195 |
{ |
196 |
if (p.ProcessIcon != null) |
197 |
{ |
198 |
image_key = p.FileName; |
199 |
small_image = new Bitmap(p.ProcessIcon, small); |
200 |
large_image = new Bitmap(p.ProcessIcon, large); |
201 |
} |
202 |
} |
203 |
|
204 |
} |
205 |
|
206 |
private void PIDSelector_Load(object sender, EventArgs e) |
207 |
{ |
208 |
if (AcceptedPlugin == null || this.DesignMode) return; |
209 |
txtstatus_Pid.Text = ""; |
210 |
txtstatus_Filename.Text = ""; |
211 |
this.RefreshList(); |
212 |
} |
213 |
|
214 |
|
215 |
private void lstProcessList_ItemSelectionChanged(object sender, ListViewItemSelectionChangedEventArgs e) |
216 |
{ |
217 |
if (!e.IsSelected) return; |
218 |
PerformListViewItemSelect(e.Item); |
219 |
} |
220 |
|
221 |
private void lstProcessList_ItemActivate(object sender, EventArgs e) |
222 |
{ |
223 |
ListViewItem Item = lstProcessList.SelectedItems[0]; |
224 |
PerformListViewItemSelect(Item); |
225 |
} |
226 |
|
227 |
private void btnLaunchAndAttach_Click(object sender, EventArgs e) |
228 |
{ |
229 |
DialogResult result = EXESelector.ShowDialog(); |
230 |
if (result != DialogResult.OK) return; |
231 |
FileInfo fi = new FileInfo(EXESelector.FileName); |
232 |
if (!fi.Exists) { logger.Warn.WriteLine("Cannot load and attach to non-existing exe file: {0}", fi.FullName); return; } |
233 |
Process p = Process.Start(fi.FullName); |
234 |
this.ProcessPID = p.Id; |
235 |
isAttachingToProcess = true; |
236 |
} |
237 |
|
238 |
private void ProcessWatcherTimer_Tick(object sender, EventArgs e) |
239 |
{ |
240 |
if (isRefreshing || isAutoRefreshing) return; |
241 |
//if (lstProcessList.Items.Count == 0) return; |
242 |
isAutoRefreshing = true; |
243 |
int pid = this.ProcessPID; |
244 |
this.RefreshList(); |
245 |
bool pid_still_exists = false; |
246 |
int item_index = -1; |
247 |
foreach (ListViewItem li2 in lstProcessList.Items) |
248 |
{ |
249 |
int _pid = Convert.ToInt32(li2.SubItems[1].Text); |
250 |
if (_pid == pid) |
251 |
{ |
252 |
item_index = lstProcessList.Items.IndexOf(li2); |
253 |
pid_still_exists = true; |
254 |
break; |
255 |
} |
256 |
} |
257 |
if (pid_still_exists) |
258 |
{ |
259 |
//logger.Debug.WriteLine("Pid: {0} still exists", pid); |
260 |
if (lstProcessList.SelectedItems.Count > 0) |
261 |
{ |
262 |
if (lstProcessList.SelectedItems[0] != null) |
263 |
if (lstProcessList.SelectedItems[0].Index != -1) |
264 |
lstProcessList.Items[lstProcessList.SelectedItems[0].Index].Selected = false; |
265 |
} |
266 |
lstProcessList.Items[item_index].Selected = true; |
267 |
isAutoRefreshing = false; |
268 |
} |
269 |
else |
270 |
{ |
271 |
if (lstProcessList.SelectedItems.Count > 0) |
272 |
{ |
273 |
if (lstProcessList.SelectedItems[0] != null) |
274 |
{ |
275 |
string missing_name = lstProcessList.SelectedItems[0].Text; |
276 |
int missing_pid = Convert.ToInt32(lstProcessList.SelectedItems[0].SubItems[1].Text); |
277 |
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)); |
278 |
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); |
279 |
} |
280 |
isAutoRefreshing = false; |
281 |
lstProcessList.Items[0].Selected = false; |
282 |
lstProcessList.Items[0].Selected = true; |
283 |
} |
284 |
} |
285 |
} |
286 |
} |
287 |
} |
288 |
|