feat: run in task

This commit is contained in:
Anna 2024-02-18 01:07:40 -05:00
parent ffabe0ba0e
commit 4c585dc366
Signed by: anna
GPG Key ID: D0943384CD9F87D1
1 changed files with 33 additions and 32 deletions

View File

@ -28,10 +28,6 @@ internal class Command : IDisposable {
private void OnCommand(string command, string arguments) {
// TODO: offload as much of this as possible onto a new thread.
// probably want to keep the bitmap stuff on main thread so that
// thread start delay doesn't make a screenshot be delayed.
// TODO: eventually be able to do like /screenie --no-ui --format png
// etc.
if (arguments == "config") {
@ -40,42 +36,47 @@ internal class Command : IDisposable {
}
var meta = ScreenshotMetadata.Capture(this.Plugin);
using var bitmap = Photographer.Capture();
if (bitmap == null) {
var bitmapOuter = Photographer.Capture();
if (bitmapOuter == null) {
return;
}
var saveAs = this.Plugin.Config.SaveFormat;
byte[] imageData;
string ext;
if (saveAs.ToImageFormat() is { } format) {
var data = this.SaveNative(format, bitmap);
if (data == null) {
Task.Factory.StartNew(async () => {
using var bitmap = bitmapOuter;
var saveAs = this.Plugin.Config.SaveFormat;
byte[] imageData;
string ext;
if (saveAs.ToImageFormat() is { } format) {
var data = this.SaveNative(format, bitmap);
if (data == null) {
return;
}
(imageData, ext) = data.Value;
} else if (saveAs is Format.WebpLossless or Format.WebpLossy) {
using var webp = new WebPObject(bitmap);
imageData = saveAs == Format.WebpLossless
? webp.GetWebPLossless()
: webp.GetWebPLossy(this.Plugin.Config.SaveFormatData);
ext = "webp";
} else {
return;
}
(imageData, ext) = data.Value;
} else if (saveAs is Format.WebpLossless or Format.WebpLossy) {
using var webp = new WebPObject(bitmap);
imageData = saveAs == Format.WebpLossless
? webp.GetWebPLossless()
: webp.GetWebPLossy(this.Plugin.Config.SaveFormatData);
ext = "webp";
} else {
return;
}
string hash;
await using (var stream = new Blake3Stream(this.OpenFile(ext, meta))) {
await stream.WriteAsync(imageData);
hash = Convert.ToHexString(stream.ComputeHash().AsSpan());
}
string hash;
using (var stream = new Blake3Stream(this.OpenFile(ext, meta))) {
stream.Write(imageData);
hash = Convert.ToHexString(stream.ComputeHash().AsSpan());
}
meta.Blake3Hash = hash;
var json = JsonConvert.SerializeObject(meta, Formatting.Indented);
// TODO: save this info to database
Plugin.Log.Info(json);
meta.Blake3Hash = hash;
var json = JsonConvert.SerializeObject(meta, Formatting.Indented);
Plugin.Log.Info(json);
this.Plugin.ChatGui.Print("Screenshot saved.");
this.Plugin.ChatGui.Print("Screenshot saved.");
});
}
private (byte[], string)? SaveNative(ImageFormat format, Image bitmap) {