Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Do not blindly assume non-system emojis always decode successfully #1018

Merged
merged 2 commits into from
Mar 29, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
Loading