Skip to content

Commit

Permalink
Do not blindly assume non-system emojis always decode successfully (#…
Browse files Browse the repository at this point in the history
…1018)

* Do not assume emojis always decode successfully

* Fewer allocations and repeat dictionary lookups
  • Loading branch information
ScrubN authored Mar 29, 2024
1 parent b7344e1 commit 057a905
Show file tree
Hide file tree
Showing 2 changed files with 19 additions and 8 deletions.
18 changes: 11 additions & 7 deletions TwitchDownloaderCore/ChatRenderer.cs
Original file line number Diff line number Diff line change
Expand Up @@ -1691,22 +1691,26 @@ private async Task<List<CheerEmote>> GetScaledBits(CancellationToken cancellatio

private async Task<Dictionary<string, SKBitmap>> GetScaledEmojis(CancellationToken cancellationToken)
{
var emojiTask = await TwitchHelper.GetEmojis(renderOptions.TempFolder, renderOptions.EmojiVendor, cancellationToken);
var emojis = await TwitchHelper.GetEmojis(renderOptions.TempFolder, renderOptions.EmojiVendor, _progress, cancellationToken);

//Assume emojis are 4x (they're 72x72)
double emojiScale = 0.5 * renderOptions.ReferenceScale * renderOptions.EmojiScale;
List<string> emojiKeys = new List<string>(emojiTask.Keys);

// We can't just enumerate the dictionary because of the version checks
string[] emojiKeys = emojis.Keys.ToArray();
foreach (var emojiKey in emojiKeys)
{
SKImageInfo oldEmojiInfo = emojiTask[emojiKey].Info;
SKBitmap bitmap = emojis[emojiKey];
SKImageInfo oldEmojiInfo = bitmap.Info;
SKImageInfo imageInfo = new SKImageInfo((int)(oldEmojiInfo.Width * emojiScale), (int)(oldEmojiInfo.Height * emojiScale));
SKBitmap newBitmap = new SKBitmap(imageInfo);
emojiTask[emojiKey].ScalePixels(newBitmap, SKFilterQuality.High);
emojiTask[emojiKey].Dispose();
emojiTask[emojiKey] = newBitmap;
bitmap.ScalePixels(newBitmap, SKFilterQuality.High);
bitmap.Dispose();
newBitmap.SetImmutable();
emojis[emojiKey] = newBitmap;
}

return emojiTask;
return emojis;
}

private (int startTick, int totalTicks) GetVideoTicks()
Expand Down
9 changes: 8 additions & 1 deletion TwitchDownloaderCore/TwitchHelper.cs
Original file line number Diff line number Diff line change
Expand Up @@ -660,7 +660,7 @@ public static async Task<List<ChatBadge>> GetChatBadges(List<Comment> comments,
return returnList;
}

public static async Task<Dictionary<string, SKBitmap>> GetEmojis(string cacheFolder, EmojiVendor emojiVendor, CancellationToken cancellationToken = default)
public static async Task<Dictionary<string, SKBitmap>> GetEmojis(string cacheFolder, EmojiVendor emojiVendor, IProgress<ProgressReport> progress, CancellationToken cancellationToken = default)
{
var returnCache = new Dictionary<string, SKBitmap>();

Expand Down Expand Up @@ -722,6 +722,13 @@ public static async Task<Dictionary<string, SKBitmap>> GetEmojis(string cacheFol
{
await using var fs = File.OpenRead(emojiPath);
var emojiImage = SKBitmap.Decode(fs);

if (emojiImage is null)
{
progress.Report(new ProgressReport(ReportType.Log, $"Failed to decode emoji {Path.GetFileName(emojiPath)}, skipping."));
continue;
}

returnCache.Add(Path.GetFileNameWithoutExtension(emojiPath), emojiImage);
}

Expand Down

0 comments on commit 057a905

Please sign in to comment.