-
Notifications
You must be signed in to change notification settings - Fork 0
/
Containers.cs
398 lines (343 loc) · 12.1 KB
/
Containers.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
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
using System;
using System.Collections.Generic;
using System.Text;
using System.Linq;
using System.Text.RegularExpressions;
namespace BISLib
{
/// <summary>
/// Describes the result of a launch operation
/// </summary>
public enum LaunchResults
{
/// <summary>
/// The game was launched successfully
/// </summary>
Success,
/// <summary>
/// The game failed to launch successfully
/// </summary>
Failure
}
/// <summary>
/// The type of launch operation to be performed
/// </summary>
public enum LaunchTypes
{
/// <summary>
/// The game will launch through Steam if it is installed
/// </summary>
Steam,
/// <summary>
/// The game will be run in Release mode
/// </summary>
Release,
/// <summary>
/// The game will be run in Beta mode (if a beta version is installed)
/// </summary>
Beta,
/// <summary>
/// The latest version of the game will be launched (Beta or Release)
/// </summary>
Latest
}
/// <summary>
/// Determines the type of engine used to select mods for
/// a certain <see cref="ModSelector"/>
/// </summary>
public enum SelectionEngines
{
/// <summary>
/// A function will be used to select mods
/// </summary>
Function,
/// <summary>
/// Only mods whose names exactly match the given values will be selected
/// </summary>
Exact,
/// <summary>
/// Uses a wildcard selection algorithm to match the mod names
/// </summary>
Wildcard,
/// <summary>
/// Uses a regular expression selection algorithm to match the mod names
/// </summary>
RegularExpressions
}
/// <summary>
/// Describes a set of options to be used for launching a game
/// </summary>
public class LaunchParameters
{
/// <summary>
/// Creates a new game launch parameters instance
/// </summary>
public LaunchParameters()
{
Game = Game.ArmA2;
LaunchType = LaunchTypes.Release;
ModSelectionFilters = new List<ModSelector>();
AdditionalArguments = new List<string>();
AdditionalModDirectories = new List<string>();
Server = GameServer.Singleplayer;
}
/// <summary>
/// Gets or Sets the game to launch
/// </summary>
public Game Game
{
get;
set;
}
/// <summary>
/// Gets or Sets the type of game to be launched
/// </summary>
public LaunchTypes LaunchType
{ get; set; }
/// <summary>
/// Gets or Sets the list of <see cref="ModSelector"/>s
/// which will be used to select mods.
/// </summary>
public List<ModSelector> ModSelectionFilters
{
get;
set;
}
/// <summary>
/// Gets or Sets the list of additional arguments which will
/// be provided to the game's executable
/// </summary>
public List<string> AdditionalArguments
{
get;
set;
}
/// <summary>
/// Gets or Sets the list of additional mod directories which
/// will be searched
/// </summary>
public List<string> AdditionalModDirectories
{ get; set; }
/// <summary>
/// Gets or Sets the <see cref="GameServer"/> which
/// will be joined when the game starts
/// </summary>
public GameServer Server
{ get; set; }
/// <summary>
/// Performs an operation on the mod parameter before
/// any <see cref="ModSelectionFilters"/> are used to
/// populate it.
/// </summary>
public Func<string, string> PreModFiltering
{ get; set; }
/// <summary>
/// Performs any operation on the mod parameter after
/// the <see cref="ModSelectionFilters"/> have been used
/// to populate it.
/// </summary>
public Func<string, string> PostModFiltering
{ get; set; }
/// <summary>
/// Filters the list of mods selected by the
/// <see cref="ModSelectionFilters"/> before they are
/// added to the mod paramters
/// </summary>
public Func<IEnumerable<string>, IEnumerable<string>> ModFilter
{ get; set; }
/// <summary>
/// Determines whether the game should be started if certain
/// <see cref="ModSelectionFilters"/> do not match any mods
/// </summary>
public Func<IEnumerable<ModSelector>, bool> MissingMods
{ get; set; }
/// <summary>
/// Allows you to override any selection made by the given selector
/// by returning a subset of the selected collection
/// </summary>
public Func<ModSelector, IEnumerable<string>, IEnumerable<string>> SelectorOverride
{
get;
set;
}
}
/// <summary>
/// Selects mods from a directory based on a set of selection criteria
/// </summary>
public class ModSelector
{
/// <summary>
/// Creates a <see cref="ModSelector"/> which uses a function to select mods
/// </summary>
/// <param name="matchEvaluator">
/// The function to use for selecting mods
/// </param>
public ModSelector(Func<string, bool> matchEvaluator)
{
if (MatchEvaluator == null)
throw new ArgumentNullException("matchEvaluator", "Must be a function taking a string argument and returning a boolean value");
Engine = SelectionEngines.Function;
MatchEvaluator = matchEvaluator;
}
/// <summary>
/// Creates a <see cref="ModSelector"/> which uses the specified selection engine to
/// select mods.
/// </summary>
/// <param name="engine">
/// The engine to use for selecting mods
/// </param>
/// <param name="selector">
/// The selector string to use for this <see cref="ModSelector"/>
/// </param>
/// <param name="exclusion">
/// Whether or not this selector will act as an exclusion filter
/// </param>
/// <exception cref="ArgumentException">
/// Thrown if <see cref="SelectionEngines.Function"/> is selected for <paramref name="engine"/>
/// </exception>
public ModSelector(SelectionEngines engine, string selector, bool exclusion)
{
if (engine == SelectionEngines.Function)
throw new ArgumentException("SelectionEngines.Function may not be specified here", "engine");
Engine = engine;
Selector = selector;
ExclusionFilter = exclusion;
MatchEvaluator = null;
}
/// <summary>
/// The selection engine to use for matching mod names
/// to the <see cref="Selector"/>.
/// </summary>
public SelectionEngines Engine
{
get;
private set;
}
/// <summary>
/// The selection filter to use for matching mod names
/// </summary>
public string Selector
{ get; private set; }
/// <summary>
/// Whether or not this <see cref="ModSelector"/> will be
/// treated as an exclusion filter
/// </summary>
public bool ExclusionFilter
{ get; set; }
/// <summary>
/// The function to use to determine whether or not to select a mod
/// </summary>
public Func<string, bool> MatchEvaluator
{
get;
private set;
}
private IEnumerable<string> MatchFunction(IEnumerable<string> values)
{
foreach (string value in values)
if (MatchEvaluator(value))
yield return value;
}
internal IEnumerable<string> Match(IEnumerable<string> values)
{
if (Engine == SelectionEngines.Function)
return MatchFunction(values);
if(Engine == SelectionEngines.RegularExpressions)
{
Regex regex = new Regex(Selector, RegexOptions.Compiled);
return values.Where(_ => regex.IsMatch(_));
}
else if (Engine == SelectionEngines.Exact)
{
return values.Where(_ => _.Equals(Selector, StringComparison.InvariantCultureIgnoreCase));
}
else if (Engine == SelectionEngines.Wildcard)
{
string[] parts = Selector.Split(new string[] {"*"}, StringSplitOptions.None);
string regexSelector = "";
for (int i = 0; i < parts.Length; i++)
regexSelector += Regex.Escape(parts[i]) + ".*";
regexSelector.Remove(regexSelector.Length - 2);
Regex regex = new Regex(regexSelector, RegexOptions.Compiled);
return values.Where(_ => regex.IsMatch(_));
}
return null;
}
}
public struct GameServer
{
public static GameServer Singleplayer
{
get
{
return new GameServer() { Address = ".", Port = 0, Password = "" };
}
}
public string Address
{ get; set; }
public int Port
{ get; set; }
public string Password
{ get; set; }
}
public class LaunchEventArgs : EventArgs
{
private LaunchEventArgs(LaunchParameters parameters, LaunchResults result, string message, List<string> selectedMods, List<string> excludedMods, List<ModSelector> failedSelectors)
{
Parameters = parameters;
Result = result;
Message = message;
SelectedMods = selectedMods;
ExcludedMods = excludedMods;
FailedSelectors = failedSelectors;
}
private LaunchEventArgs(LaunchParameters parameters, LaunchResults result, string message, DateTime launched, DateTime closed, List<string> selectedMods, List<string> excludedMods)
{
Parameters = parameters;
Result = result;
Message = message;
LaunchTime = launched;
CloseTime = closed;
SelectedMods = selectedMods;
ExcludedMods = excludedMods;
FailedSelectors = new List<ModSelector>();
}
public static LaunchEventArgs ForFailure(LaunchParameters parameters, string errorMessage)
{
return new LaunchEventArgs(parameters, LaunchResults.Failure, errorMessage, new List<string>(), new List<string>(), new List<ModSelector>());
}
public static LaunchEventArgs ForMissingMods(LaunchParameters parameters, string errorMessage, List<string> selectedMods, List<string> excludedMods, List<ModSelector> failedSelections)
{
return new LaunchEventArgs(parameters, LaunchResults.Failure, errorMessage, selectedMods, excludedMods, failedSelections);
}
public static LaunchEventArgs ForSuccess(LaunchParameters parameters, DateTime launched, DateTime closed, List<string> selectedMods, List<string> excludedMods)
{
return new LaunchEventArgs(parameters, LaunchResults.Success, "Game was successfully launched", launched, closed, selectedMods, excludedMods);
}
public LaunchResults Result
{
get;
private set;
}
public DateTime LaunchTime
{ get; private set; }
public DateTime CloseTime
{ get; private set; }
public List<string> SelectedMods
{ get; private set; }
public List<string> ExcludedMods
{ get; private set; }
public List<ModSelector> FailedSelectors
{ get; private set; }
public string Message
{
get;
private set;
}
public LaunchParameters Parameters
{
get;
private set;
}
}
}