1 |
#region Logging Defines |
2 |
// include this any class or method that required logging, and comment-out what is not needed |
3 |
#define LOGGING_ENABLED |
4 |
#region Enabled logging levels |
5 |
#define LOGGING_ENABLE_INFO |
6 |
#define LOGGING_ENABLE_WARN |
7 |
#define LOGGING_ENABLE_DEBUG |
8 |
#define LOGGING_ENABLE_VERBOSEDEBUG |
9 |
#define LOGGING_ENABLE_ERROR |
10 |
#define LOGGING_ENABLE_VERBOSEERROR |
11 |
#define LOGGING_ENABLE_PROFILER |
12 |
#endregion |
13 |
#endregion |
14 |
using System; |
15 |
using System.Collections.Generic; |
16 |
using System.ComponentModel; |
17 |
using System.Data; |
18 |
using System.Drawing; |
19 |
using System.Text; |
20 |
using System.Windows.Forms; |
21 |
using System.Diagnostics; |
22 |
using RomCheater.PluginFramework.Interfaces; |
23 |
using RomCheater.PluginFramework.Core; |
24 |
using System.IO; |
25 |
using RomCheater.Logging; |
26 |
using WeifenLuo.WinFormsUI.Docking; |
27 |
using RomCheater.PluginFramework.Events; |
28 |
using System.Management; |
29 |
using System.Reflection; |
30 |
|
31 |
namespace RomCheater.Docking |
32 |
{ |
33 |
public partial class FloatingPIDSelector : DockContent, IAcceptsPlugin<IConfigPlugin>, IAcceptsChangedProcess, IAcceptsProcessPID |
34 |
{ |
35 |
private const int ProcessRefershInterval = 5000; // refresh interval in milliseconds |
36 |
private bool isUserInteracting = false; |
37 |
private bool isRefreshing = false; |
38 |
private bool isAutoRefreshing = false; |
39 |
private bool isAttachingToProcess = false; |
40 |
//private IConfigPlugin plugin = null; |
41 |
//bool isInError = false; |
42 |
public FloatingPIDSelector() { InitializeComponent(); this.ProcessPID = -1; OnSelectedProcessChanged = null; this.AcceptedPlugin = null; } |
43 |
public FloatingPIDSelector(IConfigPlugin plugin) : this() { this.AcceptedPlugin = plugin; } |
44 |
// unsued construtor (it's not implmented, may not be) |
45 |
[Obsolete("constructor PIDSelector(int pid) is not implemented", false)] |
46 |
public FloatingPIDSelector(int pid) : this() { this.ProcessPID = pid; } |
47 |
[Obsolete("constructor PIDSelector(IConfigPlugin plugin, int pid) is not implemented", false)] |
48 |
public FloatingPIDSelector(IConfigPlugin plugin, int pid) : this(plugin) { this.ProcessPID = pid; } |
49 |
|
50 |
#region IAcceptsProcessPID |
51 |
public int ProcessPID { get; set; } |
52 |
#endregion |
53 |
#region IAcceptsPlugin<IConfigPlugin> members |
54 |
private IConfigPlugin _AcceptedPlugin; |
55 |
public IConfigPlugin AcceptedPlugin { |
56 |
get { return _AcceptedPlugin; } |
57 |
set { _AcceptedPlugin = value; ProcessWatcherTimer.Enabled = (value != null); } } |
58 |
#endregion |
59 |
#region IAcceptsChangedProcess members |
60 |
public event BaseEventHandler<ProcessChangedEventArgs> OnSelectedProcessChanged; |
61 |
#endregion |
62 |
|
63 |
|
64 |
|
65 |
public new void Show() { this.Show(null); } |
66 |
//public new void Show(IWin32Window owner) { this.PreInitShow(owner); } |
67 |
public new void Show(DockPanel panel) { this.PreInitShow(panel); } |
68 |
//public new DialogResult ShowDialog() { return this.ShowDialog(null); } |
69 |
//public new DialogResult ShowDialog(IWin32Window owner) { return this.PreInitDialog(owner); } |
70 |
//private void PreInitShow(IWin32Window owner) |
71 |
//{ |
72 |
// if (!this.RefreshList()) { MessageBox.Show(string.Format("Could not find any Processes for plugin {0}. Please start an instance of one.",plugin.ToString())); } |
73 |
// else { if (owner == null) { base.Show(); } else { base.Show(owner); } } |
74 |
//} |
75 |
private void PreInitShow(DockPanel panel) |
76 |
{ |
77 |
//if (panel == null) return; |
78 |
if (!this.RefreshList()) { MessageBox.Show(string.Format("Could not find any Processes for plugin {0}. Please start an instance of one.", AcceptedPlugin.ToString())); } |
79 |
else { if (panel == null) { base.Show(); } else { base.Show(panel); } } |
80 |
} |
81 |
//private DialogResult PreInitDialog(IWin32Window owner) |
82 |
//{ |
83 |
// 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; } |
84 |
// else { if (owner == null) { return base.ShowDialog(); } else { return base.ShowDialog(owner); } } |
85 |
//} |
86 |
//private void SetControlBackColor(Control control, KnownColor color) { SetControlBackColor(control, Color.FromKnownColor(color)); } |
87 |
//private void SetControlBackColor(Control control, Color color) { control.BackColor = color; } |
88 |
private void SetControlBackColor<T>(T component, KnownColor color) { SetControlBackColor(component, Color.FromKnownColor(color)); } |
89 |
private void SetControlBackColor<T>(T component, Color color) |
90 |
{ |
91 |
Type ComponentType = component.GetType(); |
92 |
PropertyInfo pBackColor = ComponentType.GetProperty("BackColor"); |
93 |
pBackColor.SetValue(component, color, null); |
94 |
} |
95 |
private void SetViewForProcessListView(View view) |
96 |
{ |
97 |
Color baseColor = Color.FromKnownColor(KnownColor.GradientActiveCaption); |
98 |
int GB_OFFSET = 10; |
99 |
Color c = Color.FromArgb(baseColor.A, baseColor.R, baseColor.G + GB_OFFSET, baseColor.B + GB_OFFSET); |
100 |
ResetViewButtonControls(); |
101 |
switch (view) |
102 |
{ |
103 |
case View.Details: SetControlBackColor(btnDetails, c); break; |
104 |
case View.LargeIcon: SetControlBackColor(btnLargeIcon, c); break; |
105 |
case View.List: SetControlBackColor(btnList, c); break; |
106 |
case View.SmallIcon: SetControlBackColor(btnSmallIcon, c); break; |
107 |
case View.Tile: SetControlBackColor(btnTile, c); break; |
108 |
} |
109 |
lstProcessList.View = view; |
110 |
} |
111 |
private void ResetViewButtonControls() |
112 |
{ |
113 |
KnownColor DefaultColor = KnownColor.Control; |
114 |
SetControlBackColor(btnLargeIcon, DefaultColor); |
115 |
SetControlBackColor(btnDetails, DefaultColor); |
116 |
SetControlBackColor(btnSmallIcon, DefaultColor); |
117 |
SetControlBackColor(btnList, DefaultColor); |
118 |
SetControlBackColor(btnTile, DefaultColor); |
119 |
} |
120 |
private void btnLargeIcon_Click(object sender, EventArgs e) { SetViewForProcessListView(View.LargeIcon); } |
121 |
private void btnDetails_Click(object sender, EventArgs e) { SetViewForProcessListView(View.Details); } |
122 |
private void btnSmallIcon_Click(object sender, EventArgs e) { SetViewForProcessListView(View.SmallIcon); } |
123 |
private void btnList_Click(object sender, EventArgs e) { SetViewForProcessListView(View.List); } |
124 |
private void btnTile_Click(object sender, EventArgs e) { SetViewForProcessListView(View.Tile); } |
125 |
|
126 |
private void PerformListViewItemSelect(ListViewItem li) |
127 |
{ |
128 |
if (isAutoRefreshing && !isAttachingToProcess) return; |
129 |
this.ProcessPID = Convert.ToInt32(li.SubItems[1].Text); |
130 |
Process SelectedProcess = Process.GetProcessById(this.ProcessPID); |
131 |
|
132 |
txtstatus_Pid.Text = SelectedProcess.Id.ToString(); |
133 |
txtstatus_Filename.Text = SelectedProcess.MainModule.FileName; |
134 |
|
135 |
logger.Debug.WriteLine("Using Process: ({0} : {1})", SelectedProcess.Id, SelectedProcess.ProcessName); |
136 |
//this.Close(); |
137 |
if (OnSelectedProcessChanged != null) |
138 |
OnSelectedProcessChanged(new ProcessChangedEventArgs(this, this.ProcessPID)); |
139 |
if (isAttachingToProcess) |
140 |
isAttachingToProcess = false; |
141 |
} |
142 |
private void btnOK_Click(object sender, EventArgs e) |
143 |
{ |
144 |
|
145 |
} |
146 |
|
147 |
//private void btnCancel_Click(object sender, EventArgs e) |
148 |
//{ |
149 |
// this.SelectedPid = -1; |
150 |
// //this.Close(); |
151 |
//} |
152 |
|
153 |
private void btnRefresh_Click(object sender, EventArgs e) |
154 |
{ |
155 |
//this.RefreshList(); |
156 |
} |
157 |
|
158 |
private bool RefreshList() |
159 |
{ |
160 |
lstProcessList.BeginUpdate(); |
161 |
//if (this.AcceptedPlugin == null) { return false; }; |
162 |
isRefreshing = true; |
163 |
lstProcessList.Items.Clear(); |
164 |
//logger.VerboseDebug.WriteLine(System.Environment.StackTrace); |
165 |
this.AcceptedPlugin.Reload(true); |
166 |
// create a selection of PCSX2 processes |
167 |
List<ProcContainer> proc_list = AcceptedPlugin.ValidProcessesForPlugin; |
168 |
|
169 |
if (!(proc_list.Count > 0)) |
170 |
{ //MessageBox.Show("Could not find any PCSX2 Processes. Please start an instance of one."); |
171 |
return false; |
172 |
} |
173 |
|
174 |
List<ListViewItem> items = new List<ListViewItem>(); |
175 |
ImageList small_image_list = new ImageList(); |
176 |
ImageList large_image_list = new ImageList(); |
177 |
|
178 |
int small_width = 24; |
179 |
int large_width = 32; |
180 |
|
181 |
small_image_list.ImageSize = new Size(small_width, small_width); |
182 |
large_image_list.ImageSize = new Size(large_width, large_width); |
183 |
foreach (ProcContainer p in proc_list) |
184 |
{ |
185 |
Bitmap small_image = null; |
186 |
Bitmap large_image = null; |
187 |
string image_key = string.Empty; |
188 |
this.AddProcessItem(p, small_image_list.ImageSize, large_image_list.ImageSize, out image_key, out small_image, out large_image); |
189 |
if (image_key != string.Empty) |
190 |
{ |
191 |
if (small_image != null) { small_image_list.Images.Add(image_key, small_image); } |
192 |
if (large_image != null) { large_image_list.Images.Add(image_key, large_image); } |
193 |
ListViewItem item = new ListViewItem(); |
194 |
item.Name = p.GetHashCode().ToString(); |
195 |
// process name |
196 |
item.Text = p.Name; |
197 |
// pid |
198 |
item.SubItems.Add(p.ProcessInfo.Id.ToString()); |
199 |
// FullPath |
200 |
item.SubItems.Add(p.FileName); |
201 |
// image key |
202 |
item.ImageKey = p.FileName; |
203 |
if (!items.Contains(item)) items.Add(item); |
204 |
} |
205 |
else |
206 |
{ |
207 |
//logger.Error.WriteLine("image_key is an empty string!"); |
208 |
//if (small_image != null) { logger.Error.WriteLine(" [however small_image is not null]"); } |
209 |
//if (large_image != null) { logger.Error.WriteLine(" [however large_image is not null]\n"); } |
210 |
ListViewItem item = new ListViewItem(); |
211 |
item.Name = p.GetHashCode().ToString(); |
212 |
// process name |
213 |
item.Text = p.Name; |
214 |
// pid |
215 |
item.SubItems.Add(p.ProcessInfo.Id.ToString()); |
216 |
// FullPath |
217 |
item.SubItems.Add(p.FileName); |
218 |
// image key |
219 |
item.ImageKey = p.FileName; |
220 |
if (!items.Contains(item)) items.Add(item); |
221 |
} |
222 |
} |
223 |
lstProcessList.SmallImageList = small_image_list; |
224 |
lstProcessList.LargeImageList = large_image_list; |
225 |
this.lstProcessList.Items.AddRange(items.ToArray()); |
226 |
|
227 |
if (lstProcessList.Items.Count > 1 && !isAttachingToProcess) |
228 |
{ |
229 |
lstProcessList.Items[0].Selected = true; |
230 |
} |
231 |
isRefreshing = false; |
232 |
lstProcessList.EndUpdate(); |
233 |
return true; |
234 |
} |
235 |
|
236 |
private void AddProcessItem(ProcContainer p, Size small, Size large, out string image_key, out Bitmap small_image, out Bitmap large_image) |
237 |
{ |
238 |
image_key = ""; |
239 |
small_image = null; |
240 |
large_image = null; |
241 |
////small_image_list = new ImageList(); |
242 |
////large_image_list = new ImageList(); |
243 |
//ListViewItem item = new ListViewItem(); |
244 |
//// process |
245 |
//item.Text = p.Name; |
246 |
//if (p.ProcessIcon != null) |
247 |
//{ |
248 |
// //small_image_list.Images.Add( |
249 |
// //large_image_list.Images.Add(p.FileName, p.ProcessIcon); |
250 |
// item.ImageKey = p.FileName; |
251 |
//} |
252 |
//// pid |
253 |
//item.SubItems.Add(p.ProcessInfo.Id.ToString()); |
254 |
//// FullPath |
255 |
//item.SubItems.Add(p.FileName); |
256 |
//if (!items.Contains(item)) items.Add(item); |
257 |
if (p != null) |
258 |
{ |
259 |
if (p.ProcessIcon != null) |
260 |
{ |
261 |
image_key = p.FileName; |
262 |
small_image = new Bitmap(p.ProcessIcon, small); |
263 |
large_image = new Bitmap(p.ProcessIcon, large); |
264 |
} |
265 |
else |
266 |
{ |
267 |
Bitmap bmp = RomCheater.Properties.Resources.DefaulApplicationIcon.ToBitmap(); |
268 |
image_key = p.FileName; |
269 |
small_image = new Bitmap(bmp, small); |
270 |
large_image = new Bitmap(bmp, large); |
271 |
} |
272 |
} |
273 |
|
274 |
} |
275 |
|
276 |
private void PIDSelector_Load(object sender, EventArgs e) |
277 |
{ |
278 |
if (AcceptedPlugin == null || this.DesignMode) return; |
279 |
ProcessWatcherTimer.Interval = ProcessRefershInterval; |
280 |
txtstatus_Pid.Text = ""; |
281 |
txtstatus_Filename.Text = ""; |
282 |
SetViewForProcessListView(View.LargeIcon); |
283 |
this.RefreshList(); |
284 |
} |
285 |
|
286 |
|
287 |
private void lstProcessList_ItemSelectionChanged(object sender, ListViewItemSelectionChangedEventArgs e) |
288 |
{ |
289 |
if (!e.IsSelected) return; |
290 |
PerformListViewItemSelect(e.Item); |
291 |
} |
292 |
|
293 |
private void lstProcessList_ItemActivate(object sender, EventArgs e) |
294 |
{ |
295 |
ListViewItem Item = lstProcessList.SelectedItems[0]; |
296 |
PerformListViewItemSelect(Item); |
297 |
} |
298 |
|
299 |
private void btnLaunchAndAttach_Click(object sender, EventArgs e) |
300 |
{ |
301 |
DialogResult result = EXESelector.ShowDialog(); |
302 |
if (result != DialogResult.OK) return; |
303 |
FileInfo fi = new FileInfo(EXESelector.FileName); |
304 |
if (!fi.Exists) { logger.Warn.WriteLine("Cannot load and attach to non-existing exe file: {0}", fi.FullName); return; } |
305 |
Process p = Process.Start(fi.FullName); |
306 |
this.ProcessPID = p.Id; |
307 |
isAttachingToProcess = true; |
308 |
} |
309 |
|
310 |
private void ProcessWatcherTimer_Tick(object sender, EventArgs e) |
311 |
{ |
312 |
if (this.AcceptedPlugin != null) |
313 |
if (this.AcceptedPlugin.SearchInProgess) |
314 |
return; |
315 |
if (isRefreshing || isAutoRefreshing || isUserInteracting) return; |
316 |
//if (lstProcessList.Items.Count == 0) return; |
317 |
isAutoRefreshing = true; |
318 |
int pid = this.ProcessPID; |
319 |
this.RefreshList(); |
320 |
bool pid_still_exists = false; |
321 |
int item_index = -1; |
322 |
foreach (ListViewItem li2 in lstProcessList.Items) |
323 |
{ |
324 |
int _pid = Convert.ToInt32(li2.SubItems[1].Text); |
325 |
if (_pid == pid) |
326 |
{ |
327 |
item_index = lstProcessList.Items.IndexOf(li2); |
328 |
pid_still_exists = true; |
329 |
break; |
330 |
} |
331 |
} |
332 |
if (pid_still_exists) |
333 |
{ |
334 |
//logger.Debug.WriteLine("Pid: {0} still exists", pid); |
335 |
if (lstProcessList.SelectedItems.Count > 0) |
336 |
{ |
337 |
if (lstProcessList.SelectedItems[0] != null) |
338 |
if (lstProcessList.SelectedItems[0].Index != -1) |
339 |
lstProcessList.Items[lstProcessList.SelectedItems[0].Index].Selected = false; |
340 |
} |
341 |
lstProcessList.Items[item_index].Selected = true; |
342 |
isAutoRefreshing = false; |
343 |
} |
344 |
else |
345 |
{ |
346 |
if (lstProcessList.SelectedItems.Count > 0) |
347 |
{ |
348 |
if (lstProcessList.SelectedItems[0] != null) |
349 |
{ |
350 |
int missing_pid = this.ProcessPID; |
351 |
logger.Warn.WriteLine("Connection to selected process ({0}) has been lost...Selecting the first available process.", missing_pid); |
352 |
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); |
353 |
} |
354 |
isAutoRefreshing = false; |
355 |
lstProcessList.Items[0].Selected = false; |
356 |
lstProcessList.Items[0].Selected = true; |
357 |
} |
358 |
} |
359 |
} |
360 |
|
361 |
|
362 |
private void lstProcessList_MouseMove(object sender, MouseEventArgs e) |
363 |
{ |
364 |
isUserInteracting = true; |
365 |
} |
366 |
|
367 |
private void lstProcessList_MouseLeave(object sender, EventArgs e) |
368 |
{ |
369 |
isUserInteracting = false; |
370 |
} |
371 |
} |
372 |
} |
373 |
|