-
-
Notifications
You must be signed in to change notification settings - Fork 0
/
EliteScreenshots.cs
278 lines (240 loc) · 10.1 KB
/
EliteScreenshots.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
#nullable enable
using System;
using System.Drawing;
using System.Drawing.Imaging;
using System.IO;
using System.Linq;
using System.Text;
using System.Text.RegularExpressions;
using System.Threading;
namespace EliteScreenshots
{
public class EliteScreenshots
{
private static dynamic? VA;
private static readonly string screenshotsDirectory = Path.Combine(Environment.GetFolderPath(Environment.SpecialFolder.MyPictures), @"Frontier Developments\Elite Dangerous");
private static readonly string defaultFormat = "%datetime%-%cmdr%-%system%-%body%";
private static readonly string defaultOutputDirectory = Environment.GetFolderPath(Environment.SpecialFolder.DesktopDirectory);
private static readonly Regex standardRegex = new Regex(@"^Screenshot_\d{4}\.bmp$");
private static readonly Regex highResRegex = new Regex(@"^HighResScreenShot_\d{4}-\d{2}-\d{2}_\d{2}-\d{2}-\d{2}.bmp$");
private static readonly Regex tokenRegex = new Regex(@"%(?<token>[\w: \-\.]*)%");
private static FileSystemWatcher FileWatcher
{
get
{
if (fileWatcher == null)
{
fileWatcher = new FileSystemWatcher(screenshotsDirectory);
fileWatcher.Created += (source, EventArgs) => { FileChangedHandler(EventArgs); };
}
return fileWatcher!;
}
}
private static FileSystemWatcher? fileWatcher;
public static string VERSION = "0.1";
public static string VA_DisplayName() => $"EliteScreenshots Plugin {VERSION}";
public static string VA_DisplayInfo() => VA_DisplayName();
public static Guid VA_Id() => new Guid("{252490FD-2E6F-4703-900B-02ED98D717C2}");
public static void VA_Init1(dynamic vaProxy)
{
VA = vaProxy;
VA.TextVariableChanged += new Action<string, string, string, Guid?>(TextVariableChanged);
VA.SetText("EliteScreenshots.version", VERSION);
try
{
// inform about old shots
DirectoryInfo dirInfo = new DirectoryInfo(screenshotsDirectory);
int standardCount = dirInfo.GetFiles().Where(file => standardRegex.IsMatch(file.Name)).Count();
int highResCount = dirInfo.GetFiles().Where(file => highResRegex.IsMatch(file.Name)).Count();
if (standardCount > 0 && highResCount > 0)
{
LogInfo($"There are {standardCount} old screenshots and {highResCount} old high res screenshots.");
}
else if (standardCount > 0)
{
LogInfo($"There are {standardCount} old screenshots.");
}
else if (highResCount > 0)
{
LogInfo($"There are {highResCount} old high res screenshots.");
}
if (standardCount > 0 || highResCount > 0)
{
LogInfo($"Run the “convertold” plugin context to convert them.");
}
}
catch (Exception e)
{
LogError(e.Message);
}
finally
{
FileWatcher.EnableRaisingEvents = true;
}
}
public static void VA_Invoke1(dynamic vaProxy)
{
VA = vaProxy;
try
{
string context = VA.Context.ToLower();
if (context == "convertold")
{
ConvertOldShots();
}
else
{
LogError($"Invalid plugin context: '{context}'.");
}
}
catch (Exception e)
{
LogError(e.Message);
}
}
public static void VA_StopCommand() { }
public static void VA_Exit1(dynamic vaProxy) { }
private static void LogError(string message)
{
VA!.WriteToLog($"ERROR | EliteScreenshots: {message}", "red");
}
private static void LogInfo(string message)
{
VA!.WriteToLog($"INFO | EliteScreenshots: {message}", "blue");
}
private static void LogWarn(string message)
{
VA!.WriteToLog($"WARN | EliteScreenshots: {message}", "yellow");
}
private static string getTargetFileName(bool highres = false, string? fromFile = null)
{
StringBuilder sb = new StringBuilder(VA!.GetText("EliteScreenshots.format#") ?? defaultFormat);
MatchCollection matches = tokenRegex.Matches(sb.ToString());
string token;
string value;
if (String.IsNullOrEmpty(fromFile))
{
foreach (Match match in matches)
{
token = match.Groups["token"].Value;
value = token switch
{
"body" => VA!.GetText("Status body name") ?? "unknown",
"cmdr" => VA!.GetText("Name") ?? "unknown",
"date" => DateTime.Now.ToString("yyyy-MM-dd"),
"datetime" => DateTime.Now.ToString("yyyy-MM-dd HH-mm-ss"),
"shipname" => VA!.GetText("Ship name") ?? "unknown",
"system" => VA!.GetText("System name") ?? "unknown",
"time" => DateTime.Now.ToString("HH-mm-ss"),
"vehicle" => VA!.GetText("Status vehicle") ?? "unknown",
_ => $"%{token}%",
};
sb.Replace($"%{token}%", value);
}
}
else
{
foreach (Match match in matches)
{
token = match.Groups["token"].Value;
DateTime fileDateTime = File.GetCreationTime(fromFile);
value = token switch
{
"body" => "unknown",
"cmdr" => "unknown",
"date" => fileDateTime.ToString("yyyy-MM-dd"),
"datetime" => fileDateTime.ToString("yyyy-MM-dd HH-mm-ss"),
"shipname" => "unknown",
"system" => "unknown",
"time" => fileDateTime.ToString("HH-mm-ss"),
"vehicle" => "unknown",
_ => $"%{token}%",
};
sb.Replace($"%{token}%", value);
}
}
foreach (char c in System.IO.Path.GetInvalidFileNameChars())
{
sb.Replace(c, '_');
}
string outputDirectory = VA!.GetText("EliteScreenshots.outputDirectory#") ?? defaultOutputDirectory;
string targetFilename = Path.Combine(outputDirectory, $"{sb}{(highres ? "-highres" : "")}.png");
if (File.Exists(targetFilename))
{
int i = 1;
string newFileName;
do
{
newFileName = Path.Combine(outputDirectory, $"{sb}{(highres ? "-highres" : "")}_{i:D4}.png");
i++;
} while (File.Exists(newFileName));
targetFilename = newFileName;
}
return targetFilename;
}
private static string ConvertAndMove(string source, string? target = null, bool highres = false)
{
target ??= getTargetFileName(highres);
using (Bitmap bm = new Bitmap(source)) {
bm.Save(target, ImageFormat.Png);
}
LogInfo($"Saved{(highres ? " high resolution" : "")} screenshot to '{target}'.");
File.Delete(source);
return target;
}
private static void ConvertOldShots ()
{
DirectoryInfo dirInfo = new DirectoryInfo(screenshotsDirectory);
foreach (FileInfo fileInfo in dirInfo.GetFiles().Where(file => standardRegex.IsMatch(file.Name)).ToList())
{
ConvertAndMove(fileInfo.FullName, target: getTargetFileName(fromFile: fileInfo.FullName));
}
foreach (FileInfo fileInfo in dirInfo.GetFiles().Where(file => highResRegex.IsMatch(file.Name)).ToList())
{
ConvertAndMove(fileInfo.FullName, target: getTargetFileName(fromFile: fileInfo.FullName, highres: true), highres: true);
}
}
public static void TextVariableChanged(string name, string from, string to, Guid? internalID)
{
if (name == "EliteScreenshots.format#")
{
// FIXXME: check if actually valid, error otherwise
// (or just give example output along with it?)
LogInfo($"Output format changed to '{to}'.");
}
if (name == "EliteScreenshots.outputDirectory#")
{
// FIXXME check if it exists
LogInfo($"Output directory changed to '{to}'.");
}
}
private static void FileChangedHandler(FileSystemEventArgs eventArgs)
{
string name = eventArgs.Name;
try
{
if (standardRegex.IsMatch(name))
{
ConvertAndMove(Path.Combine(screenshotsDirectory, name));
}
else if (highResRegex.IsMatch(name))
{
// This is ugly AF …
// But I have to wait for Elite to finish writing, and no,
// I have not been able to find a viable alternative to this
// that would be less ugly.
Thread.Sleep(5000);
ConvertAndMove(Path.Combine(screenshotsDirectory, name), highres: true);
}
else
{
throw new Exception($"Found new file '{name}', but it does not appear to be a screenshot.");
}
}
catch (Exception e)
{
LogError(e.Message);
}
}
}
}