forked from akiver/cs-demo-manager
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathExportCommand.cs
195 lines (180 loc) · 7.43 KB
/
ExportCommand.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
using System;
using System.Collections.Generic;
using System.IO;
using Core.Models.Source;
using System.Linq;
namespace CLI
{
internal abstract class ExportCommand : BaseCommand
{
protected Source _source;
protected readonly List<string> _demoPaths;
protected string _outputFolderPath;
protected bool _forceAnalyze = false;
protected long _focusSteamId = 0;
protected List<string> _availableSources = new List<string>();
public ExportCommand(string commandName, string description) : base(commandName, description)
{
_demoPaths = new List<string>();
foreach (Source source in Source.Sources)
{
if (source.Name != Pov.NAME)
{
_availableSources.Add(source.Name);
}
}
}
protected void ParseArgs(string[] args, string[] allowedOptions)
{
base.ParseArgs(args);
for (int index = 0; index < args.Length; index++)
{
string arg = args[index];
bool isOption = arg.StartsWith("--");
if (isOption)
{
switch (arg)
{
case "--source":
if (args.Length > index + 1)
{
index += 1;
string sourceName = args[index];
_source = Source.Factory(sourceName);
if (_source == null)
{
Console.WriteLine($@"Invalid source: {sourceName}");
Environment.Exit(1);
}
}
else
{
Console.WriteLine(@"Missing --source argument value");
Environment.Exit(1);
}
break;
case "--output":
if (args.Length > index + 1)
{
index += 1;
_outputFolderPath = Path.GetFullPath(args[index]).TrimEnd(Path.DirectorySeparatorChar);
bool folderExists = Directory.Exists(_outputFolderPath);
if (!folderExists)
{
Console.WriteLine(@"The output folder doesn't exists");
Environment.Exit(1);
}
if (!IsDirectoryWritable(_outputFolderPath))
{
Console.WriteLine(@"The output folder is not writable");
Environment.Exit(1);
}
}
else
{
Console.WriteLine(@"Missing --output argument value");
Environment.Exit(1);
}
break;
case "--force-analyze":
_forceAnalyze = true;
break;
case "--steamid":
if (args.Length > index + 1)
{
index += 1;
bool success = long.TryParse(args[index], out long steamId);
if (success)
{
_focusSteamId = steamId;
}
else
{
Console.WriteLine(@"Invalid SteamID");
Environment.Exit(1);
}
}
else
{
Console.WriteLine(@"Missing --steamid argument value");
Environment.Exit(1);
}
break;
default:
if (!allowedOptions.Contains(arg))
{
Console.WriteLine($@"Unknown option {arg}");
Environment.Exit(1);
}
break;
}
}
else
{
bool isDemoFile = arg.EndsWith(".dem");
if (isDemoFile)
{
bool fileExists = File.Exists(arg);
if (!fileExists)
{
Console.WriteLine($@"The file doesn't exists: {arg}");
Environment.Exit(1);
}
if (_demoPaths.Contains(arg))
{
continue;
}
_demoPaths.Add(arg);
}
else
{
try
{
if (arg.EndsWith("\""))
{
arg = arg.Substring(0, arg.Length - 1) + "\\";
}
string directoryPath = Path.GetFullPath(arg);
bool directoryExists = Directory.Exists(directoryPath);
if (directoryExists)
{
string[] files = Directory.GetFiles(directoryPath, "*.dem");
foreach (string file in files)
{
if (_demoPaths.Contains(file))
{
continue;
}
_demoPaths.Add(file);
}
}
else
{
Console.WriteLine($@"The directory doesn't exists: {arg}");
Environment.Exit(1);
}
}
catch (Exception ex)
{
Console.WriteLine($@"Invalid directory: {ex.Message}");
Environment.Exit(1);
}
}
}
}
}
protected string BuildOutputFolderPathFromDemoPath(string demoPath)
{
if (!string.IsNullOrEmpty(_outputFolderPath))
{
return _outputFolderPath;
}
string demoFolderPath = Path.GetDirectoryName(demoPath);
if (IsDirectoryWritable(demoFolderPath))
{
return demoFolderPath;
}
return Environment.GetFolderPath(Environment.SpecialFolder.Desktop);
}
}
}