Skip to content

Commit 5298ab1

Browse files
committed
Handle dynamic screenshots exceptions (Closes #1204) (#1206)
1 parent 64df882 commit 5298ab1

File tree

1 file changed

+28
-11
lines changed

1 file changed

+28
-11
lines changed

Daybreak/Services/Screenshots/ScreenshotService.cs

Lines changed: 28 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,18 @@
11
using System.Collections.Concurrent;
22
using System.Drawing;
3+
using System.Extensions.Core;
34
using System.IO;
45
using Daybreak.Shared.Models;
56
using Daybreak.Shared.Models.ColorPalette;
67
using Daybreak.Shared.Services.Screenshots;
8+
using Microsoft.Extensions.Logging;
79
using static Daybreak.Shared.Models.Themes.Theme;
810

911
namespace Daybreak.Services.Screenshots;
1012

11-
public sealed class ScreenshotService : IScreenshotService
13+
public sealed class ScreenshotService(
14+
ILogger<ScreenshotService> logger)
15+
: IScreenshotService
1216
{
1317
private const string GuildWarsFolder = "Guild Wars";
1418
private const string ScreensFolder = "Screens";
@@ -17,6 +21,8 @@ public sealed class ScreenshotService : IScreenshotService
1721
private static readonly ColorThiefDotNet.ColorThief ColorThief = new();
1822
private static readonly ConcurrentDictionary<string, ScreenshotEntry> EntryCache = [];
1923

24+
private readonly ILogger<ScreenshotService> logger = logger;
25+
2026
public Task<ScreenshotEntry?> GetRandomScreenshot(CancellationToken cancellationToken)
2127
{
2228
return Task.Factory.StartNew(this.GetRandomScreenshotInternal, cancellationToken, TaskCreationOptions.LongRunning, TaskScheduler.Current).Unwrap();
@@ -41,27 +47,38 @@ public sealed class ScreenshotService : IScreenshotService
4147
return default;
4248
}
4349

44-
return GetEntryFromPath(selectedScreenshot);
50+
return this.GetEntryFromPath(selectedScreenshot);
4551
}
4652

47-
private static ScreenshotEntry? GetEntryFromPath(string path)
53+
private ScreenshotEntry? GetEntryFromPath(string path)
4854
{
55+
var scopedLogger = this.logger.CreateScopedLogger();
4956
if (EntryCache.TryGetValue(path, out var cachedEntry))
5057
{
58+
scopedLogger.LogDebug("Screenshot entry found in cache for path {path}", path);
5159
return cachedEntry;
5260
}
5361

54-
using var bitmap = GetBitmap(path);
55-
if (bitmap is null)
62+
try
5663
{
64+
using var bitmap = GetBitmap(path);
65+
if (bitmap is null)
66+
{
67+
return default;
68+
}
69+
70+
var dominantColor = ColorThief.GetColor(bitmap, quality: 2);
71+
var closestAccent = GetClosestAccentColor(Color.FromArgb(dominantColor.Color.A, dominantColor.Color.R, dominantColor.Color.G, dominantColor.Color.B));
72+
var entry = new ScreenshotEntry(path, closestAccent, dominantColor.IsDark ? LightDarkMode.Dark : LightDarkMode.Light);
73+
EntryCache[path] = entry;
74+
scopedLogger.LogDebug("Screenshot entry created and cached for path {path}", path);
75+
return entry;
76+
}
77+
catch(Exception e)
78+
{
79+
scopedLogger.LogError(e, "Failed to get screenshot entry from path {path}", path);
5780
return default;
5881
}
59-
60-
var dominantColor = ColorThief.GetColor(bitmap, quality: 2);
61-
var closestAccent = GetClosestAccentColor(Color.FromArgb(dominantColor.Color.A, dominantColor.Color.R, dominantColor.Color.G, dominantColor.Color.B));
62-
var entry = new ScreenshotEntry(path, closestAccent, dominantColor.IsDark ? LightDarkMode.Dark : LightDarkMode.Light);
63-
EntryCache[path] = entry;
64-
return entry;
6582
}
6683

6784
private static AccentColor GetClosestAccentColor(Color color)

0 commit comments

Comments
 (0)