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