From 3cad34c681aba8899c5a70b2f5b9ea61b236f353 Mon Sep 17 00:00:00 2001
From: lay295 <fizzohd@gmail.com>
Date: Thu, 24 Oct 2019 18:39:20 -0400
Subject: [PATCH] Support ReChat json files

---
 TwitchDownloaderWPF/PageChatRender.xaml.cs | 56 ++++++++++++++++------
 1 file changed, 41 insertions(+), 15 deletions(-)

diff --git a/TwitchDownloaderWPF/PageChatRender.xaml.cs b/TwitchDownloaderWPF/PageChatRender.xaml.cs
index 22619a99..eaa71aeb 100644
--- a/TwitchDownloaderWPF/PageChatRender.xaml.cs
+++ b/TwitchDownloaderWPF/PageChatRender.xaml.cs
@@ -90,7 +90,19 @@ private void btnRender_Click(object sender, RoutedEventArgs e)
         private void BackgroundRenderManager_DoWork(object sender, DoWorkEventArgs e)
         {
             RenderOptions renderOptions = (RenderOptions)e.Argument;
-            ChatRoot chatJson = JsonConvert.DeserializeObject<ChatRoot>(File.ReadAllText(renderOptions.json_path));
+            ChatRoot chatJson;
+            try
+            {
+                chatJson = JsonConvert.DeserializeObject<ChatRoot>(File.ReadAllText(renderOptions.json_path));
+            }
+            catch (JsonSerializationException)
+            {
+                chatJson = new ChatRoot();
+                chatJson.comments = JsonConvert.DeserializeObject<List<Comment>>(File.ReadAllText(renderOptions.json_path));
+                chatJson.streamer = new Streamer();
+                chatJson.streamer.id = Int32.Parse(chatJson.comments.First().channel_id);
+                chatJson.streamer.name = "";
+            }
             BlockingCollection<TwitchComment> finalComments = new  BlockingCollection<TwitchComment>();
             List<ThirdPartyEmote> thirdPartyEmotes = new List<ThirdPartyEmote>();
             List<ChatBadge> chatBadges = new List<ChatBadge>();
@@ -123,7 +135,7 @@ private void BackgroundRenderManager_DoWork(object sender, DoWorkEventArgs e)
             {
                 if (comment.source != "chat")
                     continue;
-                if (comment.message.user_notice_params.msg_id != null && comment.message.user_notice_params.msg_id != "")
+                if (comment.message.user_notice_params != null && (comment.message.user_notice_params.msg_id != null && comment.message.user_notice_params.msg_id != ""))
                     continue;
 
                 string userName = comment.commenter.display_name.ToString();
@@ -171,14 +183,21 @@ private void BackgroundRenderManager_DoWork(object sender, DoWorkEventArgs e)
             RenderVideo(renderOptions, new List<TwitchComment>(finalComments.ToArray()), chatJson.comments, sender);
 
             (sender as BackgroundWorker).ReportProgress(0, new Progress("Cleaning up..."));
-            string[] files = Directory.GetFiles(downloadFolder);
-            for (int i = 0; i < files.Length; i++)
+            try
+            {
+                Directory.Delete(downloadFolder, true);
+            }
+            catch
             {
-                try
+                string[] files = Directory.GetFiles(downloadFolder);
+                for (int i = 0; i < files.Length; i++)
                 {
-                    File.Delete(files[i]);
+                    try
+                    {
+                        File.Delete(files[i]);
+                    }
+                    catch { }
                 }
-                catch { }
             }
         }
 
@@ -649,17 +668,24 @@ private void BackgroundRenderManager_RunWorkerCompleted(object sender, RunWorker
 
         private void BackgroundRenderManager_ProgressChanged(object sender, ProgressChangedEventArgs e)
         {
-            Progress update = (Progress)e.UserState;
-            statusProgressBar.Value = e.ProgressPercentage >= 100 ? 100 : e.ProgressPercentage;
+            try
+            {
+                Progress update = (Progress)e.UserState;
+                statusProgressBar.Value = e.ProgressPercentage >= 100 ? 100 : e.ProgressPercentage;
 
-            if (e.ProgressPercentage > 0 && !update.justMessage)
+                if (e.ProgressPercentage > 0 && !update.justMessage)
+                {
+                    int timeLeftInt = (int)Math.Floor(100.0 / update.percent_double * update.time_passed) - update.time_passed;
+                    TimeSpan timeLeft = new TimeSpan(0, 0, timeLeftInt);
+                    statusMessage.Text = String.Format("{0} ({1} left)", update.message, timeLeft.ToString(@"h\hm\ms\s"));
+                }
+                else
+                    statusMessage.Text = update.message;
+            }
+            catch (Exception ex)
             {
-                int timeLeftInt = (int)Math.Floor(100.0 / update.percent_double * update.time_passed) - update.time_passed;
-                TimeSpan timeLeft = new TimeSpan(0, 0, timeLeftInt);
-                statusMessage.Text = String.Format("{0} ({1} left)", update.message, timeLeft.ToString(@"h\hm\ms\s"));
+                AppendLog("Error: " + ex.Message);
             }
-            else
-                statusMessage.Text = update.message;
         }
 
         private void GetChatBadges(List<ChatBadge> chatBadges, Streamer streamerInfo, RenderOptions renderOptions)