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