-
Notifications
You must be signed in to change notification settings - Fork 11
Expand file tree
/
Copy pathProgramOptions.cs
More file actions
235 lines (192 loc) · 7.98 KB
/
ProgramOptions.cs
File metadata and controls
235 lines (192 loc) · 7.98 KB
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
//using Newtonsoft.Json;
using System;
using System.IO;
using System.Collections.Generic;
namespace FFBitrateViewer
{
public class FileItemPO
{
//[JsonProperty("enabled")]
public bool IsEnabled { get; set; } = true;
//[JsonProperty("filespec")]
public string? FS { get; set; }
public FileItemPO() { }
public FileItemPO(string fs, bool enabled = true)
{
FS = fs;
IsEnabled = enabled;
}
public FileItemPO(FileItem file)
{
FS = file.FS;
IsEnabled = file.IsEnabled;
}
}
public class ProgramOptions
{
private static readonly char Separator = '|'; // For serializing lists (Files)
public bool? AdjustStartTimeOnPlot { get; set; }
public bool? LogCommands { get; set; }
public string? PlotViewType { get; set; } = "frame";
public string? TempDir { get; set; }
//[JsonProperty("files")]
public List<FileItemPO> Files { get; set; }
public ProgramOptions()
{
Files = new List<FileItemPO>();
}
public void Add(ProgramOptions options)
{
if (options.Files.Count > 0)
{
Files.Clear();
Files.AddRange(options.Files);
}
if (options.AdjustStartTimeOnPlot != null) AdjustStartTimeOnPlot = options.AdjustStartTimeOnPlot;
if (options.LogCommands != null) LogCommands = options.LogCommands;
if (options.PlotViewType != null) PlotViewType = options.PlotViewType;
}
public static ProgramOptions LoadFromSettings()
{
var result = new ProgramOptions();
var source = Properties.Settings.Default;
result.AdjustStartTimeOnPlot = string.Equals(source[nameof(AdjustStartTimeOnPlot)]?.ToString(), "True", StringComparison.OrdinalIgnoreCase);
result.LogCommands = string.Equals(source[nameof(LogCommands)]?.ToString(), "True", StringComparison.OrdinalIgnoreCase);
var plotViewType = NormalizePlotViewType(source[nameof(PlotViewType)].ToString());
if (!string.IsNullOrEmpty(plotViewType)) result.PlotViewType = plotViewType;
result.Files.Clear();
result.Files.AddRange(FilesDeserialize(source[nameof(Files)]?.ToString()));
return result;
}
public void SaveToSettings()
{
var target = Properties.Settings.Default;
target[nameof(AdjustStartTimeOnPlot)] = AdjustStartTimeOnPlot == true;
target[nameof(LogCommands)] = LogCommands == true;
target[nameof(PlotViewType)] = PlotViewType;
target[nameof(Files)] = FilesSerialize(Files);
target.Save();
}
private static string FilesSerialize(List<FileItemPO> files)
{
var items = new List<string>();
foreach (var file in files) items.Add((file.IsEnabled ? "1" : "0") + file.FS);
return string.Join(Separator.ToString(), items);
}
private static List<FileItemPO> FilesDeserialize(string? serialized)
{
var result = new List<FileItemPO>();
if (serialized != null)
{
string[] lines = serialized.Split(Separator);
foreach (var line in lines) if (!string.IsNullOrEmpty(line)) result.Add(new FileItemPO(line[1..], line[0] == '1'));
}
return result;
}
protected static string? NormalizePlotViewType(string? value)
{
value = string.IsNullOrEmpty(value) ? null : value.ToLower();
switch (value)
{
case "frame":
case "second":
return value;
case "gop":
return "GOP";
}
return null;
}
}
// FFBitrateViewer.exe
// [-adjust-start-time-on-plot]
// [-exit]
// [-log-commands]
// [-log-level=(DEBUG|ERROR|INFO|WARNING)]
// [-plot-view-type=(frame|second|gop)]
// [-run]
// [-temp-dir=<dirspec>]
// /path/to/file1.mp4 [/path/to/file2.mp4] [...]
public class ArgsOptions : ProgramOptions
{
public bool IsFilled { get { return Files.Count > 0; } }
public LogLevel LogLevel { get; set; } = LogLevel.INFO;
public bool Exit { get; set; }
public bool Run { get; set; }
public ArgsOptions(string[] args) : base()
{
int count = args.Length;
int i = 1; /*skipping executable path in 0*/
for (; i < count; ++i)
{
string s = args[i];
if (s.StartsWith("--"))
{
s = s[2..];
}
else if (s[0] == '-' || s[0] == '/')
{
s = s[1..];
} else {
break; // Not an option -- exiting options processing loop. The next parameters will be treated as file name
}
var parts = s.Split('=');
string? svalue = (parts.Length > 1) ? parts[1].Trim() : null;
bool bvalue = string.IsNullOrEmpty(svalue) || string.Equals(svalue, "1") || string.Equals(svalue, "true", StringComparison.OrdinalIgnoreCase);
switch (parts[0].ToUpper())
{
case "ADJUST-START-TIME-ON-PLOT":
AdjustStartTimeOnPlot = bvalue;
continue;
case "EXIT":
Exit = bvalue;
continue;
case "LOG-COMMANDS":
LogCommands = bvalue;
continue;
case "LOG-LEVEL":
if (!string.IsNullOrEmpty(svalue))
{
switch (svalue.ToUpper())
{
case "DEBUG":
LogLevel = LogLevel.DEBUG;
continue;
case "ERROR":
LogLevel = LogLevel.ERROR;
continue;
case "INFO":
LogLevel = LogLevel.INFO;
continue;
case "WARNING":
LogLevel = LogLevel.WARNING;
break;
}
}
// todo@ warning?
break;
case "PLOT-VIEW-TYPE":
var plotViewType = NormalizePlotViewType(svalue);
if (!string.IsNullOrEmpty(plotViewType))
{
PlotViewType = plotViewType;
continue;
}
break; // warning?
case "RUN":
Run = bvalue;
continue;
case "TEMP-DIR":
if (!string.IsNullOrEmpty(svalue))
{
svalue = svalue.TrimEnd(Path.DirectorySeparatorChar, Path.AltDirectorySeparatorChar) + Path.DirectorySeparatorChar;
if (!Directory.Exists(svalue)) Directory.CreateDirectory(svalue);
TempDir = svalue;
continue;
}
break; // warning?
}
}
while (i < count) Files.Add(new FileItemPO(args[i++]));
}
}
}