-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathPlugin.cs
179 lines (153 loc) · 6.62 KB
/
Plugin.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
// This file is part of Screenshot Location and Metadata.
//
// Screenshot Location and Metadata is free software: you can redistribute it
// and/or modify it under the terms of the GNU General Public License as
// published by the Free Software Foundation, either version 3 of the License,
// or (at your option) any later version.
//
// Screenshot Location and Metadata is distributed in the hope that it will be
// useful, but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General
// Public License for more details.
//
// You should have received a copy of the GNU General Public License along with
// Screenshot Location and Metadata. If not, see https://www.gnu.org/licenses/.
using System;
using System.Diagnostics;
using System.IO;
using System.Text.RegularExpressions;
using System.Threading;
using NetScriptFramework.SkyrimSE;
namespace ScreenshotLocationAndMetadata
{
public class Plugin : NetScriptFramework.Plugin
{
private static readonly Regex ScreenshotRegex = new Regex(@"^(enb|ScreenShot).*\.(png|jpg)$");
private readonly Configuration config = new Configuration();
public override string Key => nameof(ScreenshotLocationAndMetadata);
public override string Name => "Screenshot Location and Metadata";
public override string Author => "Max Kagamine";
public override int Version => AssemblyVersionToInt();
protected override bool Initialize(bool loadedAny)
{
Log.Name = Name;
Log.MinimumLogLevel = config.Verbose ? LogLevel.Verbose : LogLevel.Info;
var exePath = Process.GetCurrentProcess().MainModule.FileName;
var exeDirectory = Path.GetDirectoryName(exePath);
var watcher = new FileSystemWatcher(exeDirectory);
watcher.Created += OnCreated;
watcher.EnableRaisingEvents = true;
return true;
}
private void OnCreated(object sender, FileSystemEventArgs e)
{
// Check if created file is a screenshot
string filename = Path.GetFileName(e.FullPath);
if (!ScreenshotRegex.IsMatch(filename))
{
return;
}
try
{
WaitForFile(e.FullPath);
AddMetadata(MoveScreenshot(e.FullPath));
}
catch (Exception ex)
{
Log.Error(ex, $"Error handling screenshot {filename}");
}
}
private string MoveScreenshot(string originalPath)
{
if (string.IsNullOrEmpty(config.ScreenshotDirectory))
{
return originalPath;
}
string filename = Path.GetFileName(originalPath);
string newPath = Path.Combine(config.ScreenshotDirectory, filename);
Log.Verbose($"Moving {filename} to {config.ScreenshotDirectory}");
File.Move(originalPath, newPath);
return newPath;
}
private void AddMetadata(string imagePath)
{
if (!config.AddMetadata || MenuManager.Instance.IsMenuOpen("Main Menu"))
{
return;
}
if (Path.GetExtension(imagePath).Equals(".png", StringComparison.OrdinalIgnoreCase))
{
Log.Info($"Screenshot format must be set to JPG to add EXIF metadata (set {nameof(config.AddMetadata)} to False to turn off this warning)");
return;
}
var player = PlayerCharacter.Instance;
var metadata = new ScreenshotMetadata()
{
PlayerName = player.BaseActor.Name,
PlayerLevel = player.Level,
PlayerRace = player.Race.Name,
LocationName = MapNameHelper.GetMapName(player.ParentCell),
LocationConsoleCommand = GetConsoleCommandForCell(player.ParentCell),
FOV = PlayerCamera.Instance.ThirdPersonFOV, // Apparently FirstPersonFOV is only for the 1st person arms/weapons overlay
};
metadata.Save(imagePath);
}
/// <summary>
/// Get a coc (Center on Cell) or cow (Center on World) console command that can be used to return to the
/// screenshot location (or nearby). Returns null if not possible.
/// </summary>
/// <param name="cell">The cell in which the screenshot was taken. May be null.</param>
private string GetConsoleCommandForCell(TESObjectCELL cell)
{
if (!string.IsNullOrEmpty(cell?.EditorId) && cell.EditorId != "Wilderness")
{
return $"coc {cell.EditorId}";
}
if (!string.IsNullOrEmpty(cell?.WorldSpace?.EditorId))
{
return $"cow {cell.WorldSpace.EditorId} {cell.CoordinateX} {cell.CoordinateY}";
}
return null;
}
/// <summary>
/// Waits for the file to finish being written. (This will block the thread, but the FileSystemWatcher event
/// handler should be running on a separate thread anyway.)
/// </summary>
/// <param name="filePath">The file path.</param>
/// <exception cref="TimeoutException">Timed out while waiting for the file to unlock.</exception>
private void WaitForFile(string filePath)
{
int timeoutSeconds = 10;
while (true)
{
try
{
using (var stream = new FileStream(filePath, FileMode.Open, FileAccess.ReadWrite, FileShare.None))
{
if (stream.Length == 0)
{
throw new IOException("The screenshot file is empty. This could be a bug in ENB.");
}
return;
}
}
catch (IOException ex)
{
if (timeoutSeconds-- == 0)
{
throw new TimeoutException(
"Either the screenshot is taking an exceptionally long time to save, or something is " +
"preventing us from accessing the file. Perhaps the inner exception below will be of use.",
ex);
}
Thread.Sleep(TimeSpan.FromSeconds(1));
}
}
}
private static int AssemblyVersionToInt()
{
var version = typeof(Plugin).Assembly.GetName().Version;
return version.Major * 100 + version.Minor;
}
}
}