From 280096953946715832d35e6c678363940ea52065 Mon Sep 17 00:00:00 2001 From: Genteure Date: Fri, 16 Apr 2021 17:44:53 +0800 Subject: [PATCH] Core: Remove invalid characters before writing xml #178 --- BililiveRecorder.Core/BasicDanmakuWriter.cs | 12 ++++++------ 1 file changed, 6 insertions(+), 6 deletions(-) diff --git a/BililiveRecorder.Core/BasicDanmakuWriter.cs b/BililiveRecorder.Core/BasicDanmakuWriter.cs index dca2c0d..1571dfb 100644 --- a/BililiveRecorder.Core/BasicDanmakuWriter.cs +++ b/BililiveRecorder.Core/BasicDanmakuWriter.cs @@ -21,7 +21,7 @@ namespace BililiveRecorder.Core }; private static readonly Regex invalidXMLChars = new Regex(@"(? string.IsNullOrEmpty(text) ? string.Empty : invalidXMLChars.Replace(text, string.Empty); + private static string RemoveInvalidXMLChars(string? text) => string.IsNullOrWhiteSpace(text) ? string.Empty : invalidXMLChars.Replace(text, string.Empty); private XmlWriter? xmlWriter = null; private DateTimeOffset offset = DateTimeOffset.UtcNow; @@ -106,7 +106,7 @@ namespace BililiveRecorder.Core this.xmlWriter.WriteStartElement("d"); this.xmlWriter.WriteAttributeString("p", $"{ts},{type},{size},{color},{st},0,{danmakuModel.UserID},0"); - this.xmlWriter.WriteAttributeString("user", danmakuModel.UserName); + this.xmlWriter.WriteAttributeString("user", RemoveInvalidXMLChars(danmakuModel.UserName)); if (recordDanmakuRaw) this.xmlWriter.WriteAttributeString("raw", danmakuModel.RawObj?["info"]?.ToString(Newtonsoft.Json.Formatting.None)); this.xmlWriter.WriteValue(RemoveInvalidXMLChars(danmakuModel.CommentText)); @@ -119,7 +119,7 @@ namespace BililiveRecorder.Core this.xmlWriter.WriteStartElement("sc"); var ts = Math.Max((DateTimeOffset.UtcNow - this.offset).TotalSeconds, 0d); this.xmlWriter.WriteAttributeString("ts", ts.ToString()); - this.xmlWriter.WriteAttributeString("user", danmakuModel.UserName); + this.xmlWriter.WriteAttributeString("user", RemoveInvalidXMLChars(danmakuModel.UserName)); this.xmlWriter.WriteAttributeString("price", danmakuModel.Price.ToString()); this.xmlWriter.WriteAttributeString("time", danmakuModel.SCKeepTime.ToString()); if (recordDanmakuRaw) @@ -134,8 +134,8 @@ namespace BililiveRecorder.Core this.xmlWriter.WriteStartElement("gift"); var ts = Math.Max((DateTimeOffset.UtcNow - this.offset).TotalSeconds, 0d); this.xmlWriter.WriteAttributeString("ts", ts.ToString()); - this.xmlWriter.WriteAttributeString("user", danmakuModel.UserName); - this.xmlWriter.WriteAttributeString("giftname", danmakuModel.GiftName); + this.xmlWriter.WriteAttributeString("user", RemoveInvalidXMLChars(danmakuModel.UserName)); + this.xmlWriter.WriteAttributeString("giftname", RemoveInvalidXMLChars(danmakuModel.GiftName)); this.xmlWriter.WriteAttributeString("giftcount", danmakuModel.GiftCount.ToString()); if (recordDanmakuRaw) this.xmlWriter.WriteAttributeString("raw", danmakuModel.RawObj?["data"]?.ToString(Newtonsoft.Json.Formatting.None)); @@ -148,7 +148,7 @@ namespace BililiveRecorder.Core this.xmlWriter.WriteStartElement("guard"); var ts = Math.Max((DateTimeOffset.UtcNow - this.offset).TotalSeconds, 0d); this.xmlWriter.WriteAttributeString("ts", ts.ToString()); - this.xmlWriter.WriteAttributeString("user", danmakuModel.UserName); + this.xmlWriter.WriteAttributeString("user", RemoveInvalidXMLChars(danmakuModel.UserName)); this.xmlWriter.WriteAttributeString("level", danmakuModel.UserGuardLevel.ToString()); ; this.xmlWriter.WriteAttributeString("count", danmakuModel.GiftCount.ToString()); if (recordDanmakuRaw)