From f62006a63575ad53b4f0b753d381d33e1af65c51 Mon Sep 17 00:00:00 2001 From: Aneesh Dogra Date: Mon, 16 Dec 2024 19:14:27 +0100 Subject: [PATCH 1/2] ChatReader: add try catch to prevent crash if parsing fails --- Core/Chat/ChatReader.cs | 21 +++++++++++++-------- 1 file changed, 13 insertions(+), 8 deletions(-) diff --git a/Core/Chat/ChatReader.cs b/Core/Chat/ChatReader.cs index c98c455c..f28eb567 100644 --- a/Core/Chat/ChatReader.cs +++ b/Core/Chat/ChatReader.cs @@ -65,13 +65,18 @@ public void Update(IAddonDataProvider reader) string text = sb.ToString().ToLowerInvariant(); sb.Clear(); - - int firstSpaceIdx = text.AsSpan().IndexOf(' '); - string author = text.AsSpan(0, firstSpaceIdx).ToString(); - string msg = text.AsSpan(firstSpaceIdx + 1).ToString(); - - ChatMessageEntry entry = new(DateTime.Now, type, author, msg); - Messages.Add(entry); - logger.LogInformation(entry.ToString()); + try + { + int firstSpaceIdx = text.AsSpan().IndexOf(' '); + string author = text.AsSpan(0, firstSpaceIdx).ToString(); + string msg = text.AsSpan(firstSpaceIdx + 1).ToString(); + + ChatMessageEntry entry = new(DateTime.Now, type, author, msg); + Messages.Add(entry); + logger.LogInformation(entry.ToString()); + } catch (Exception e) + { + logger.LogError("ChatEntryError: " + e.ToString()); + } } } From a9495434c5ac44f02679a8272ce86d0b0e2b2271 Mon Sep 17 00:00:00 2001 From: Xian55 <367101+Xian55@users.noreply.github.com> Date: Thu, 19 Dec 2024 17:33:46 +0100 Subject: [PATCH 2/2] Fix: Core: ChatReader: Dont attempt to search for the Author name when 'Space' character was not found in the payload. --- Core/Chat/ChatReader.cs | 23 ++++++++++++----------- 1 file changed, 12 insertions(+), 11 deletions(-) diff --git a/Core/Chat/ChatReader.cs b/Core/Chat/ChatReader.cs index f28eb567..8a28d07c 100644 --- a/Core/Chat/ChatReader.cs +++ b/Core/Chat/ChatReader.cs @@ -65,18 +65,19 @@ public void Update(IAddonDataProvider reader) string text = sb.ToString().ToLowerInvariant(); sb.Clear(); - try - { - int firstSpaceIdx = text.AsSpan().IndexOf(' '); - string author = text.AsSpan(0, firstSpaceIdx).ToString(); - string msg = text.AsSpan(firstSpaceIdx + 1).ToString(); - - ChatMessageEntry entry = new(DateTime.Now, type, author, msg); - Messages.Add(entry); - logger.LogInformation(entry.ToString()); - } catch (Exception e) + + int firstSpaceIdx = text.AsSpan().IndexOf(' '); + if (firstSpaceIdx == -1) { - logger.LogError("ChatEntryError: " + e.ToString()); + logger.LogError($"Malformed payload: {text}"); + return; } + + string author = text.AsSpan(0, firstSpaceIdx).ToString(); + string msg = text.AsSpan(firstSpaceIdx + 1).ToString(); + + ChatMessageEntry entry = new(DateTime.Now, type, author, msg); + Messages.Add(entry); + logger.LogInformation(entry.ToString()); } }