Core: Remove invalid characters before writing xml #178

This commit is contained in:
Genteure 2021-04-16 17:44:53 +08:00
parent 43379957f4
commit 2800969539

View File

@ -21,7 +21,7 @@ namespace BililiveRecorder.Core
}; };
private static readonly Regex invalidXMLChars = new Regex(@"(?<![\uD800-\uDBFF])[\uDC00-\uDFFF]|[\uD800-\uDBFF](?![\uDC00-\uDFFF])|[\x00-\x08\x0B\x0C\x0E-\x1F\x7F-\x9F\uFEFF\uFFFE\uFFFF]", RegexOptions.Compiled); private static readonly Regex invalidXMLChars = new Regex(@"(?<![\uD800-\uDBFF])[\uDC00-\uDFFF]|[\uD800-\uDBFF](?![\uDC00-\uDFFF])|[\x00-\x08\x0B\x0C\x0E-\x1F\x7F-\x9F\uFEFF\uFFFE\uFFFF]", RegexOptions.Compiled);
private static string RemoveInvalidXMLChars(string? text) => 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 XmlWriter? xmlWriter = null;
private DateTimeOffset offset = DateTimeOffset.UtcNow; private DateTimeOffset offset = DateTimeOffset.UtcNow;
@ -106,7 +106,7 @@ namespace BililiveRecorder.Core
this.xmlWriter.WriteStartElement("d"); this.xmlWriter.WriteStartElement("d");
this.xmlWriter.WriteAttributeString("p", $"{ts},{type},{size},{color},{st},0,{danmakuModel.UserID},0"); 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) if (recordDanmakuRaw)
this.xmlWriter.WriteAttributeString("raw", danmakuModel.RawObj?["info"]?.ToString(Newtonsoft.Json.Formatting.None)); this.xmlWriter.WriteAttributeString("raw", danmakuModel.RawObj?["info"]?.ToString(Newtonsoft.Json.Formatting.None));
this.xmlWriter.WriteValue(RemoveInvalidXMLChars(danmakuModel.CommentText)); this.xmlWriter.WriteValue(RemoveInvalidXMLChars(danmakuModel.CommentText));
@ -119,7 +119,7 @@ namespace BililiveRecorder.Core
this.xmlWriter.WriteStartElement("sc"); this.xmlWriter.WriteStartElement("sc");
var ts = Math.Max((DateTimeOffset.UtcNow - this.offset).TotalSeconds, 0d); var ts = Math.Max((DateTimeOffset.UtcNow - this.offset).TotalSeconds, 0d);
this.xmlWriter.WriteAttributeString("ts", ts.ToString()); 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("price", danmakuModel.Price.ToString());
this.xmlWriter.WriteAttributeString("time", danmakuModel.SCKeepTime.ToString()); this.xmlWriter.WriteAttributeString("time", danmakuModel.SCKeepTime.ToString());
if (recordDanmakuRaw) if (recordDanmakuRaw)
@ -134,8 +134,8 @@ namespace BililiveRecorder.Core
this.xmlWriter.WriteStartElement("gift"); this.xmlWriter.WriteStartElement("gift");
var ts = Math.Max((DateTimeOffset.UtcNow - this.offset).TotalSeconds, 0d); var ts = Math.Max((DateTimeOffset.UtcNow - this.offset).TotalSeconds, 0d);
this.xmlWriter.WriteAttributeString("ts", ts.ToString()); this.xmlWriter.WriteAttributeString("ts", ts.ToString());
this.xmlWriter.WriteAttributeString("user", danmakuModel.UserName); this.xmlWriter.WriteAttributeString("user", RemoveInvalidXMLChars(danmakuModel.UserName));
this.xmlWriter.WriteAttributeString("giftname", danmakuModel.GiftName); this.xmlWriter.WriteAttributeString("giftname", RemoveInvalidXMLChars(danmakuModel.GiftName));
this.xmlWriter.WriteAttributeString("giftcount", danmakuModel.GiftCount.ToString()); this.xmlWriter.WriteAttributeString("giftcount", danmakuModel.GiftCount.ToString());
if (recordDanmakuRaw) if (recordDanmakuRaw)
this.xmlWriter.WriteAttributeString("raw", danmakuModel.RawObj?["data"]?.ToString(Newtonsoft.Json.Formatting.None)); this.xmlWriter.WriteAttributeString("raw", danmakuModel.RawObj?["data"]?.ToString(Newtonsoft.Json.Formatting.None));
@ -148,7 +148,7 @@ namespace BililiveRecorder.Core
this.xmlWriter.WriteStartElement("guard"); this.xmlWriter.WriteStartElement("guard");
var ts = Math.Max((DateTimeOffset.UtcNow - this.offset).TotalSeconds, 0d); var ts = Math.Max((DateTimeOffset.UtcNow - this.offset).TotalSeconds, 0d);
this.xmlWriter.WriteAttributeString("ts", ts.ToString()); 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("level", danmakuModel.UserGuardLevel.ToString()); ;
this.xmlWriter.WriteAttributeString("count", danmakuModel.GiftCount.ToString()); this.xmlWriter.WriteAttributeString("count", danmakuModel.GiftCount.ToString());
if (recordDanmakuRaw) if (recordDanmakuRaw)