forked from hantabaru1014/ResoniteScreenshotExtensions
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathPhotoMetadataPatch.cs
More file actions
262 lines (241 loc) · 11.4 KB
/
PhotoMetadataPatch.cs
File metadata and controls
262 lines (241 loc) · 11.4 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
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
using Elements.Assets;
using Elements.Core;
using FreeImageAPI;
using FrooxEngine;
using HarmonyLib;
using MimeDetective;
using ResoniteModLoader;
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using static ResoniteScreenshotExtensions.DiscordWebhookClient;
namespace ResoniteScreenshotExtensions;
public partial class ResoniteScreenshotExtensions : ResoniteMod
{
[HarmonyPatch(typeof(PhotoMetadata))]
class PhotoMetadata_Patch
{
const string MENU_ITEM_TAG = "RSE_POST_TO_DISCORD";
static readonly Uri DISCORD_ICON_URI = new Uri("resdb:///f6c1a66250d366d213789faefab59a9ec6ac6334e6c4be0af254680c148810dc.png");
static void PostToDiscord(Metadata metadata, string filePath)
{
if (_config == null || (_config.GetValue(DiscordWebhookUrlKey) ?? "").Length == 0) return;
var fields = new List<EmbedField>();
if (_config.GetValue(DiscordWebhookEmbedLocationNameKey))
{
fields.Add(new EmbedField("LocationName", SanitizeText(metadata.LocationName)));
}
if (_config.GetValue(DiscordWebhookEmbedLocationHostKey))
{
fields.Add(new EmbedField("LocationHost", SanitizeText(metadata.LocationHost.Name ?? metadata.LocationHost.Id)));
}
fields.Add(new EmbedField("TakenBy", SanitizeText(metadata.TakenBy.Name ?? metadata.TakenBy.Id)));
var unixTimestamp = (int)metadata.TimeTaken.ToUniversalTime().Subtract(new DateTime(1970, 1, 1, 0, 0, 0, DateTimeKind.Utc)).TotalSeconds;
fields.Add(new EmbedField("TimeTaken", $"<t:{unixTimestamp}>"));
if (_config.GetValue(DiscordWebhookEmbedUsersKey))
{
fields.Add(new EmbedField("Users", metadata.UserInfos.Select(u => SanitizeText(u.User.Name ?? u.User.Id)).Aggregate((acc, curr) => $"{acc}, {curr}")));
}
var client = new DiscordWebhookClient(_config?.GetValue(DiscordWebhookUrlKey) ?? "");
try
{
_ = client.SendImageWithMetadata(filePath, fields).ContinueWith(t =>
{
if (t.IsFaulted)
{
Error("Failed to send image to Discord: " + t.Exception);
}
else
{
if (t.Result)
{
Msg("Image sent to Discord successfully.");
}
else
{
Error("Failed to send image to Discord");
}
}
});
}
catch (Exception ex)
{
Error(ex);
}
}
static void SaveImage(PhotoMetadata photoMetadata, string srcPath, string dstPath, ImageFormat format)
{
using (var bmp = new FreeImageBitmap(srcPath))
{
if (_config?.GetValue(SavePhotoMetadataToFileKey) ?? false)
{
XmpMetadata.UpsertPhotoMetadata(bmp, photoMetadata);
}
// EnsureNonHDR
var imgType = bmp.ImageType;
if (imgType == FREE_IMAGE_TYPE.FIT_RGBF || imgType == FREE_IMAGE_TYPE.FIT_RGBAF)
{
bmp.TmoDrago03(0, 0);
}
// TextureEncoder.ConvertToJPG と同じ処理
if (format == ImageFormat.JPEG)
{
// Ensure24BPP
if (bmp.IsTransparent || bmp.ColorDepth > 24)
{
bmp.ConvertColorDepth(FREE_IMAGE_COLOR_DEPTH.FICD_24_BPP);
}
}
switch (format)
{
case ImageFormat.JPEG:
bmp.Save(dstPath, FREE_IMAGE_FORMAT.FIF_JPEG, FREE_IMAGE_SAVE_FLAGS.JPEG_QUALITYSUPERB);
break;
case ImageFormat.WEBP:
FREE_IMAGE_SAVE_FLAGS quality = (_config?.GetValue(LossyWebpKey) ?? false) ? (FREE_IMAGE_SAVE_FLAGS)_config.GetValue(LossyWebpQualityKey) : FREE_IMAGE_SAVE_FLAGS.WEBP_LOSSLESS;
bmp.Save(dstPath, FreeImage.GetFIFFromFormat("webp"), quality);
break;
case ImageFormat.PNG:
bmp.Save(dstPath, FREE_IMAGE_FORMAT.FIF_PNG, FREE_IMAGE_SAVE_FLAGS.PNG_Z_BEST_COMPRESSION);
break;
}
}
if (_config != null && _config.GetValue(DiscordWebhookAutoUploadKey) && (_config.GetValue(DiscordWebhookUrlKey) ?? "").Length > 0)
{
PostToDiscord(new Metadata(photoMetadata), srcPath);
}
}
[HarmonyPostfix]
[HarmonyPatch(nameof(PhotoMetadata.NotifyOfScreenshot))]
static void NotifyOfScreenshot_Postfix(PhotoMetadata __instance)
{
if (!(_config?.GetValue(EnabledKey) ?? false)) return;
// PhotoMetadata を WindowsPlatformConnector.NotifyOfScreenshot に確実に渡すのが面倒なのでここで代替する
__instance.StartGlobalTask(async () =>
{
try
{
var tex = __instance.Slot.GetComponent<StaticTexture2D>();
var url = tex?.URL.Value;
if (url is null) return;
await new ToBackground();
// キャッシュが効いてるはずなので重複して実行しても大してコストはかからない認識
var tmpPath = await __instance.Engine.AssetManager.GatherAssetFile(url, 100f);
if (tmpPath is null) return;
var timeTaken = __instance.TimeTaken.Value.ToLocalTime();
string pictures = Environment.GetFolderPath(Environment.SpecialFolder.MyPictures);
pictures = Path.Combine(pictures, __instance.Engine.Cloud.Platform.Name);
if (_config?.GetValue(DigFolderWhenSavingKey) ?? false)
{
pictures = Path.Combine(pictures, timeTaken.ToString("yyyy-MM"));
}
Directory.CreateDirectory(pictures);
string filename = timeTaken.ToString("yyyy-MM-dd HH.mm.ss");
var format = _config?.GetValue(ImageFormatKey) ?? ImageFormat.JPEG;
string extension = _keepOriginalScreenshotFormat ? Path.GetExtension(tmpPath) : format switch
{
ImageFormat.JPEG => ".jpg",
ImageFormat.WEBP => ".webp",
ImageFormat.PNG => ".png",
_ => ".jpg"
};
if (string.IsNullOrWhiteSpace(extension))
{
FileType fileType = new FileInfo(tmpPath).GetFileType();
if (fileType != null)
extension = "." + fileType.Extension;
}
extension = extension.ToLower();
await WindowsPlatformConnector.ScreenshotSemaphore.WaitAsync().ConfigureAwait(false);
try
{
int num = 1;
string str1;
do
{
string str2 = filename;
if (num > 1)
str2 += string.Format(" ({0})", num);
str1 = Path.Combine(pictures, str2 + extension);
num++;
}
while (File.Exists(str1));
if (_keepOriginalScreenshotFormat)
{
if (extension != ".jpg" && extension != ".webp" && extension != ".png")
{
File.Copy(tmpPath, str1);
File.SetAttributes(str1, FileAttributes.Normal);
Msg($"{str1} is an unsupported format, so metadata was not saved.");
}
else
{
var extFormat = extension switch
{
".jpg" => ImageFormat.JPEG,
".webp" => ImageFormat.WEBP,
".png" => ImageFormat.PNG,
_ => ImageFormat.JPEG
};
SaveImage(__instance, tmpPath, str1, extFormat);
}
}
else
{
SaveImage(__instance, tmpPath, str1, format);
}
}
catch (Exception ex)
{
Error("Exception saving screenshot to Windows:\n" + ex);
_config?.Set(EnabledKey, false);
NotificationMessage.SpawnTextMessage("[ScreenshotExtensions] Failed saving screenshot!", colorX.Red);
}
finally
{
WindowsPlatformConnector.ScreenshotSemaphore.Release();
}
}
catch (Exception ex)
{
Error("Exception saving screenshot to Windows:\n" + ex);
_config.Set(EnabledKey, false);
NotificationMessage.SpawnTextMessage("[ScreenshotExtensions] Failed saving screenshot!", colorX.Red);
}
});
}
[HarmonyPostfix]
[HarmonyPatch(nameof(PhotoMetadata.GenerateMenuItems))]
static void GenerateMenuItems_Postfix(PhotoMetadata __instance, ContextMenu menu)
{
if (!(_config?.GetValue(DiscordWebhookGenerateMenuKey) ?? false) || (_config.GetValue(DiscordWebhookUrlKey) ?? "").Length == 0) return;
if (!__instance.Enabled) return;
var item = menu.Slot.GetComponentInChildren<ContextMenuItem>((i) => i.Slot.Tag == MENU_ITEM_TAG);
if (item == null)
{
item = menu.AddItem("Post to Discord", DISCORD_ICON_URI, null);
item.Slot.Tag = MENU_ITEM_TAG;
}
item.Button.LocalPressed += (button, eventData) =>
{
__instance.LocalUser.CloseContextMenu(null);
Msg("Posting to Discord...");
__instance.StartGlobalTask(async () =>
{
var tex = __instance.Slot.GetComponent<StaticTexture2D>();
var url = tex?.URL.Value;
if (url is null) return;
await new ToBackground();
var tmpPath = await __instance.Engine.AssetManager.GatherAssetFile(url, 100f);
if (tmpPath is null) return;
PostToDiscord(new Metadata(__instance), tmpPath);
});
};
}
private static string SanitizeText(string text)
{
return new StringRenderTree(text).GetRawString().Replace("\\", "");
}
}
}