-
Notifications
You must be signed in to change notification settings - Fork 375
/
ArgParser.cs
217 lines (207 loc) · 6.52 KB
/
ArgParser.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
#nullable enable
using System;
using System.Collections.Generic;
using System.Linq;
using System.IO;
namespace BizHawk.Client.Common
{
/// <summary>
/// Parses command line flags from a string array into various instance fields.
/// </summary>
/// <remarks>
/// If a flag is given multiple times, the last is taken.<br/>
/// If a flag that isn't recognised is given, it is parsed as a filename. As noted above, the last filename is taken.
/// </remarks>
public static class ArgParser
{
/// <exception cref="ArgParserException"><c>--socket_ip</c> passed without specifying <c>--socket_port</c> or vice-versa</exception>
public static void ParseArguments(out ParsedCLIFlags parsed, string[] args)
{
string? cmdLoadSlot = null;
string? cmdLoadState = null;
string? cmdConfigPath = null;
string? cmdConfigFile = null;
string? cmdMovie = null;
string? cmdDumpType = null;
HashSet<int>? currAviWriterFrameList = null;
int? autoDumpLength = null;
bool? printVersion = null;
string? cmdDumpName = null;
bool? autoCloseOnDump = null;
bool? chromeless = null;
bool? startFullscreen = null;
string? luaScript = null;
bool? luaConsole = null;
int? socketPort = null;
string? socketIP = null;
string? mmfFilename = null;
string? urlGet = null;
string? urlPost = null;
bool? audiosync = null;
string? openExtToolDll = null;
string? cmdRom = null;
for (var i = 0; i < args.Length; i++)
{
var arg = args[i];
if (arg == ">")
{
// For some reason sometimes visual studio will pass this to us on the commandline. it makes no sense.
var stdout = args[++i];
Console.SetOut(new StreamWriter(stdout));
continue;
}
var argDowncased = arg.ToLower();
if (argDowncased.StartsWith("--load-slot="))
{
cmdLoadSlot = argDowncased.Substring(argDowncased.IndexOf('=') + 1);
}
else if (argDowncased.StartsWith("--load-state="))
{
cmdLoadState = arg.Substring(arg.IndexOf('=') + 1);
}
else if (argDowncased.StartsWith("--config="))
{
cmdConfigFile = arg.Substring(arg.IndexOf('=') + 1);
}
else if (argDowncased.StartsWith("--movie="))
{
cmdMovie = arg.Substring(arg.IndexOf('=') + 1);
}
else if (argDowncased.StartsWith("--dump-type="))
{
cmdDumpType = argDowncased.Substring(argDowncased.IndexOf('=') + 1);
}
else if (argDowncased.StartsWith("--dump-frames="))
{
string list = argDowncased.Substring(argDowncased.IndexOf('=') + 1);
string[] items = list.Split(',');
currAviWriterFrameList = new HashSet<int>();
foreach (string item in items)
{
currAviWriterFrameList.Add(int.Parse(item));
}
// automatically set dump length to maximum frame
autoDumpLength = currAviWriterFrameList.OrderBy(x => x).Last();
}
else if (argDowncased.StartsWith("--version"))
{
printVersion = true;
}
else if (argDowncased.StartsWith("--dump-name="))
{
cmdDumpName = arg.Substring(arg.IndexOf('=') + 1);
}
else if (argDowncased.StartsWith("--dump-length="))
{
int.TryParse(argDowncased.Substring(argDowncased.IndexOf('=') + 1), out var len);
autoDumpLength = len;
}
else if (argDowncased.StartsWith("--dump-close"))
{
autoCloseOnDump = true;
}
else if (argDowncased.StartsWith("--chromeless"))
{
// chrome is never shown, even in windowed mode
chromeless = true;
}
else if (argDowncased.StartsWith("--fullscreen"))
{
startFullscreen = true;
}
else if (argDowncased.StartsWith("--lua="))
{
luaScript = arg.Substring(arg.IndexOf('=') + 1);
luaConsole = true;
}
else if (argDowncased.StartsWith("--luaconsole"))
{
luaConsole = true;
}
else if (argDowncased.StartsWith("--socket_port="))
{
int.TryParse(argDowncased.Substring(argDowncased.IndexOf('=') + 1), out var port);
if (port > 0) socketPort = port;
}
else if (argDowncased.StartsWith("--socket_ip="))
{
socketIP = argDowncased.Substring(argDowncased.IndexOf('=') + 1);
}
else if (argDowncased.StartsWith("--mmf="))
{
mmfFilename = arg.Substring(arg.IndexOf('=') + 1);
}
else if (argDowncased.StartsWith("--url_get="))
{
urlGet = arg.Substring(arg.IndexOf('=') + 1);
}
else if (argDowncased.StartsWith("--url_post="))
{
urlPost = arg.Substring(arg.IndexOf('=') + 1);
}
else if (argDowncased.StartsWith("--audiosync="))
{
audiosync = argDowncased.Substring(argDowncased.IndexOf('=') + 1) == "true";
}
else if (argDowncased.StartsWith("--open-ext-tool-dll="))
{
// the first ext. tool from ExternalToolManager.ToolStripMenu which satisfies both of these will be opened:
// - available (no load errors, correct system/rom, etc.)
// - dll path matches given string; or dll filename matches given string with or without `.dll`
openExtToolDll = arg.Substring(20);
}
else
{
cmdRom = arg;
}
}
var httpAddresses = urlGet == null && urlPost == null
? ((string?, string?)?) null // don't bother
: (urlGet, urlPost);
(string, int)? socketAddress;
if (socketIP == null && socketPort == null)
{
socketAddress = null; // don't bother
}
else if (socketIP == null || socketPort == null)
{
throw new ArgParserException("Socket server needs both --socket_ip and --socket_port. Socket server was not started");
}
else
{
socketAddress = (socketIP, socketPort.Value);
}
parsed = new ParsedCLIFlags(
cmdLoadSlot: cmdLoadSlot,
cmdLoadState: cmdLoadState,
cmdConfigPath: cmdConfigPath,
cmdConfigFile: cmdConfigFile,
cmdMovie: cmdMovie,
cmdDumpType: cmdDumpType,
currAviWriterFrameList: currAviWriterFrameList,
autoDumpLength: autoDumpLength ?? 0,
printVersion: printVersion ?? false,
cmdDumpName: cmdDumpName,
autoCloseOnDump: autoCloseOnDump ?? false,
chromeless: chromeless ?? false,
startFullscreen: startFullscreen ?? false,
luaScript: luaScript,
luaConsole: luaConsole ?? false,
socketAddress: socketAddress,
mmfFilename: mmfFilename,
httpAddresses: httpAddresses,
audiosync: audiosync,
openExtToolDll: openExtToolDll,
cmdRom: cmdRom
);
}
public static string? GetCmdConfigFile(string[] args)
{
return args.FirstOrDefault(arg => arg.StartsWith("--config=", StringComparison.InvariantCultureIgnoreCase))?.Substring(9);
}
public sealed class ArgParserException : Exception
{
public ArgParserException(string message) : base(message) {}
}
}
}