Skip to content

Commit

Permalink
Vastly reduce the time taken to cancel chat renders and chat download…
Browse files Browse the repository at this point in the history
…s during image fetching, and chat updates in general
  • Loading branch information
ScrubN committed Apr 24, 2024
1 parent 3296002 commit 49709e0
Show file tree
Hide file tree
Showing 4 changed files with 37 additions and 8 deletions.
10 changes: 8 additions & 2 deletions TwitchDownloaderCore/ChatDownloader.cs
Original file line number Diff line number Diff line change
Expand Up @@ -409,8 +409,6 @@ public async Task DownloadAsync(CancellationToken cancellationToken)
List<CheerEmote> twitchBits = await TwitchHelper.GetBits(chatRoot.comments, downloadOptions.TempFolder, chatRoot.streamer.id.ToString(), cancellationToken: cancellationToken);
_progress.ReportProgress(50);

cancellationToken.ThrowIfCancellationRequested();

var totalImageCount = thirdPartyEmotes.Count + firstPartyEmotes.Count + twitchBadges.Count + twitchBits.Count;
var imagesProcessed = 0;

Expand All @@ -427,6 +425,8 @@ public async Task DownloadAsync(CancellationToken cancellationToken)
_progress.ReportProgress(++imagesProcessed * 100 / totalImageCount + 50);
}

cancellationToken.ThrowIfCancellationRequested();

foreach (TwitchEmote emote in firstPartyEmotes)
{
EmbedEmoteData newEmote = new EmbedEmoteData();
Expand All @@ -440,6 +440,8 @@ public async Task DownloadAsync(CancellationToken cancellationToken)
_progress.ReportProgress(++imagesProcessed * 100 / totalImageCount + 50);
}

cancellationToken.ThrowIfCancellationRequested();

foreach (ChatBadge badge in twitchBadges)
{
EmbedChatBadge newBadge = new EmbedChatBadge();
Expand All @@ -449,6 +451,8 @@ public async Task DownloadAsync(CancellationToken cancellationToken)
_progress.ReportProgress(++imagesProcessed * 100 / totalImageCount + 50);
}

cancellationToken.ThrowIfCancellationRequested();

foreach (CheerEmote bit in twitchBits)
{
EmbedCheerEmote newBit = new EmbedCheerEmote();
Expand All @@ -470,6 +474,8 @@ public async Task DownloadAsync(CancellationToken cancellationToken)
}
}

cancellationToken.ThrowIfCancellationRequested();

if (downloadOptions.DownloadFormat is ChatFormat.Json)
{
//Best effort, but if we fail oh well
Expand Down
10 changes: 10 additions & 0 deletions TwitchDownloaderCore/ChatRenderer.cs
Original file line number Diff line number Diff line change
Expand Up @@ -1622,6 +1622,8 @@ private async Task<List<ChatBadge>> GetScaledBadges(CancellationToken cancellati

foreach (var badge in badgeTask)
{
cancellationToken.ThrowIfCancellationRequested();

// Assume badges are always 2x scale, not 1x or 4x
var newScale = renderOptions.ReferenceScale * renderOptions.BadgeScale;
if (Math.Abs(newScale - 1.0) > 0.01)
Expand All @@ -1639,6 +1641,8 @@ private async Task<List<TwitchEmote>> GetScaledEmotes(CancellationToken cancella

foreach (var emote in emoteTask)
{
cancellationToken.ThrowIfCancellationRequested();

// Assume emojis are 4x scale
double newScale = emote.ImageScale * 0.5 * renderOptions.ReferenceScale * renderOptions.EmoteScale;
if (Math.Abs(newScale - 1.0) > 0.01)
Expand All @@ -1657,6 +1661,8 @@ private async Task<List<TwitchEmote>> GetScaledThirdEmotes(CancellationToken can

foreach (var emote in emoteThirdTask)
{
cancellationToken.ThrowIfCancellationRequested();

// Assume emojis are 4x scale
double newScale = emote.ImageScale * 0.5 * renderOptions.ReferenceScale * renderOptions.EmoteScale;
if (Math.Abs(newScale - 1.0) > 0.01)
Expand All @@ -1674,6 +1680,8 @@ private async Task<List<CheerEmote>> GetScaledBits(CancellationToken cancellatio

foreach (var cheer in cheerTask)
{
cancellationToken.ThrowIfCancellationRequested();

//Assume cheermotes are always 2x scale, not 1x or 4x
var newScale = renderOptions.ReferenceScale * renderOptions.EmoteScale;
if (Math.Abs(newScale - 1.0) > 0.01)
Expand All @@ -1696,6 +1704,8 @@ private async Task<Dictionary<string, SKBitmap>> GetScaledEmojis(CancellationTok
string[] emojiKeys = emojis.Keys.ToArray();
foreach (var emojiKey in emojiKeys)
{
cancellationToken.ThrowIfCancellationRequested();

SKBitmap bitmap = emojis[emojiKey];
SKImageInfo oldEmojiInfo = bitmap.Info;
SKImageInfo imageInfo = new SKImageInfo((int)(oldEmojiInfo.Width * emojiScale), (int)(oldEmojiInfo.Height * emojiScale));
Expand Down
6 changes: 6 additions & 0 deletions TwitchDownloaderCore/ChatUpdater.cs
Original file line number Diff line number Diff line change
Expand Up @@ -85,6 +85,8 @@ public async Task UpdateAsync(CancellationToken cancellationToken)

private async Task UpdateVideoInfo(int totalSteps, int currentStep, CancellationToken cancellationToken)
{
cancellationToken.ThrowIfCancellationRequested();

_progress.SetStatus($"Updating Video Info [{currentStep}/{totalSteps}]");
_progress.ReportProgress(currentStep * 100 / totalSteps);

Expand Down Expand Up @@ -175,6 +177,8 @@ private async Task UpdateVideoInfo(int totalSteps, int currentStep, Cancellation

private async Task UpdateChatTrim(int totalSteps, int currentStep, CancellationToken cancellationToken)
{
cancellationToken.ThrowIfCancellationRequested();

_progress.SetStatus($"Updating Chat Trim [{currentStep}/{totalSteps}]");
_progress.ReportProgress(currentStep * 100 / totalSteps);

Expand Down Expand Up @@ -220,6 +224,8 @@ private async Task UpdateChatTrim(int totalSteps, int currentStep, CancellationT

private async Task UpdateEmbeds(int currentStep, int totalSteps, CancellationToken cancellationToken)
{
cancellationToken.ThrowIfCancellationRequested();

_progress.SetStatus($"Updating Embeds [{currentStep}/{totalSteps}]");
_progress.ReportProgress(currentStep * 100 / totalSteps);

Expand Down
19 changes: 13 additions & 6 deletions TwitchDownloaderCore/TwitchHelper.cs
Original file line number Diff line number Diff line change
Expand Up @@ -367,6 +367,8 @@ public static async Task<List<TwitchEmote>> GetThirdPartyEmotes(List<Comment> co
{
foreach (EmbedEmoteData emoteData in embeddedData.thirdParty)
{
cancellationToken.ThrowIfCancellationRequested();

try
{
TwitchEmote newEmote = new TwitchEmote(emoteData.data, EmoteProvider.ThirdParty, emoteData.imageScale, emoteData.id, emoteData.name);
Expand Down Expand Up @@ -405,8 +407,6 @@ public static async Task<List<TwitchEmote>> GetThirdPartyEmotes(List<Comment> co
}
}

cancellationToken.ThrowIfCancellationRequested();

if (ffz)
{
try
Expand All @@ -419,8 +419,6 @@ public static async Task<List<TwitchEmote>> GetThirdPartyEmotes(List<Comment> co
}
}

cancellationToken.ThrowIfCancellationRequested();

if (stv)
{
try
Expand Down Expand Up @@ -489,6 +487,8 @@ public static async Task<List<TwitchEmote>> GetEmotes(List<Comment> comments, st
{
foreach (EmbedEmoteData emoteData in embeddedData.firstParty)
{
cancellationToken.ThrowIfCancellationRequested();

try
{
TwitchEmote newEmote = new TwitchEmote(emoteData.data, EmoteProvider.FirstParty, emoteData.imageScale, emoteData.id, emoteData.name);
Expand Down Expand Up @@ -615,6 +615,8 @@ public static async Task<List<ChatBadge>> GetChatBadges(List<Comment> comments,
{
foreach (EmbedChatBadge data in embeddedData.twitchBadges)
{
cancellationToken.ThrowIfCancellationRequested();

try
{
ChatBadge newBadge = new ChatBadge(data.name, data.versions);
Expand Down Expand Up @@ -703,8 +705,9 @@ public static async Task<Dictionary<string, SKBitmap>> GetEmojis(string cacheFol

foreach (var emoji in emojis)
{
var filePath = Path.Combine(emojiFolder,
emoji.Name.ToUpper().Replace(emojiVendor.UnicodeSequenceSeparator(), ' '));
cancellationToken.ThrowIfCancellationRequested();

var filePath = Path.Combine(emojiFolder, emoji.Name.ToUpper().Replace(emojiVendor.UnicodeSequenceSeparator(), ' '));
if (!File.Exists(filePath))
{
try
Expand All @@ -730,6 +733,8 @@ public static async Task<Dictionary<string, SKBitmap>> GetEmojis(string cacheFol
var failedToDecode = 0;
foreach (var emojiPath in emojiFiles)
{
cancellationToken.ThrowIfCancellationRequested();

await using var fs = File.OpenRead(emojiPath);
var emojiImage = SKBitmap.Decode(fs);

Expand Down Expand Up @@ -761,6 +766,8 @@ public static async Task<List<CheerEmote>> GetBits(List<Comment> comments, strin
{
foreach (EmbedCheerEmote data in embeddedData.twitchBits)
{
cancellationToken.ThrowIfCancellationRequested();

List<KeyValuePair<int, TwitchEmote>> tierList = new List<KeyValuePair<int, TwitchEmote>>();
CheerEmote newEmote = new CheerEmote() { prefix = data.prefix, tierList = tierList };
foreach (KeyValuePair<int, EmbedEmoteData> tier in data.tierList)
Expand Down

0 comments on commit 49709e0

Please sign in to comment.