-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathProcessSelector.cs
200 lines (166 loc) · 6.44 KB
/
ProcessSelector.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
using System.Collections.Generic;
using System.Diagnostics;
using System.Text.RegularExpressions;
using System.Windows.Forms;
using System.Linq;
namespace Fishing
{
internal partial class ProcessSelector : Form
{
#region Classes
/// <summary>
/// Class container holding the PID and name of a POL process
/// </summary>
public class POLProcess
{
private int thisPOLID;
private string thisPOLName;
public bool IsAvailable
{
get
{
return Process.GetProcessesByName("pol").Any(x => x.Id == thisPOLID) &&
!ignoreThese.IsMatch(Process.GetProcessById(thisPOLID).MainWindowTitle);
}
}
public POLProcess(int intPOLID, string strPOLName)
{
this.thisPOLID = intPOLID;
this.thisPOLName = strPOLName + " - " + intPOLID.ToString();
}
public int POLID
{
get
{
return thisPOLID;
}
}
public string POLName
{
get
{
return thisPOLName;
}
}
} // @ internal class POLProcess
#endregion
#region Members
/// <summary>
/// 'Public' property, holding either the required or chosen POL process
/// </summary>
internal POLProcess ThisProcess { get; set; }
/// <summary>
/// Array holding all of the currently running "pol.exe" processes
/// </summary>
private Process[] p = Process.GetProcessesByName("pol");
/// <summary>
/// POL names that are not to be included in process selection
/// </summary>
/// <todo>
/// Add any 'names' that might be needed, e.g., from FR/GR/JP POL
/// </todo>
private static Regex ignoreThese = new Regex("(PlayOnline)|(Final Fantasy)");
/// <summary>
/// Whether or not an error occurred trying to find processes
/// </summary>
public bool error { get; private set; }
#endregion
#region Constructor
/// <summary>
/// Constructor
/// </summary>
internal ProcessSelector()
{
InitializeComponent();
ThisProcess = null;
error = false;
if(0 == p.Length) //no 'pol' process found, error out
{
MessageBox.Show("Please fully log into an FFXI character before trying to start.\r\n\r\nVana'diel times displayed will be estimates before start.", "Error");
error = true;
return;
}
else if(1 == p.Length) //automatically choose 'pol' process if only one running
{
if(ignoreThese.IsMatch(p[0].MainWindowTitle)) //if not fully logged in, error out
{
MessageBox.Show("Please fully log into an FFXI character before trying to start.\r\n\r\nVana'diel times displayed will be estimates before start.", "Error");
error = true;
return;
}
else //attach to the only running 'pol' process, if fully logged in
{
ThisProcess = new POLProcess(p[0].Id, p[0].MainWindowTitle);
}
}
else //more than one 'pol' process found, fill a ListBox with them to choose from
{
int index = GetProcesses();
if(-1 == index) //none of the 'pol' processes were actually logged in
{
MessageBox.Show("Please fully log into an FFXI character before trying to start.\r\n\r\nVana'diel times displayed will be estimates before start.", "Error");
error = true;
return;
}
if(-1 < index) //(index >= 0) means only one of the characters of all the 'pol' processes is logged in
{
ThisProcess = new POLProcess(p[index].Id, p[index].MainWindowTitle);
}
}
} // @ internal ProcessSelector()
#endregion
#region Methods
/// <summary>
/// Populate ListBox with currently logged in characters
/// </summary>
private int GetProcesses()
{
int processPlace = 0;
List<POLProcess> POLProcesses = new List<POLProcess>();
for(int i = 0; i < p.Length; i++)
{
//if "PlayOnline" or "Final Fantasy" are not in the WindowTitle
// add the process to the ListBox and store the p[index]
if(!ignoreThese.IsMatch(p[i].MainWindowTitle))
{
processPlace = i;
POLProcesses.Add(new POLProcess(p[i].Id, p[i].MainWindowTitle));
}
}
POLProcesses.TrimExcess(); //get rid of any null entries from List<> default capacity
lbProcessList.DataSource = POLProcesses;
lbProcessList.DisplayMember = "POLName";
lbProcessList.ValueMember = "POLID";
lbProcessList.ClearSelected();
if(0 == lbProcessList.Items.Count) //no characters fully logged in, return an indicator to that fact
{
return -1;
}
if(1 == lbProcessList.Items.Count) //only one fully logged in character found
{
return processPlace; //return the array index of that one logged in character
}
return -42; //process ListBox has two or more good entries
// negative return, so it doesn't look like a possible array index
} // @ private int GetProcesses()
#endregion
#region Events
private void btnProcessAttach_Click(object sender, System.EventArgs e)
{
if(-1 != lbProcessList.SelectedIndex)
{
ThisProcess = (POLProcess)lbProcessList.SelectedItem;
this.Close();
}
else
{
MessageBox.Show("Please select a process first!");
}
}
#endregion
private void btnExit_Click(object sender, System.EventArgs e)
{
System.Environment.Exit(1);
}
}
}