From 6cc8232a46ea252caa5c3604d56186bdb02e0ab7 Mon Sep 17 00:00:00 2001 From: ScrubN <72096833+ScrubN@users.noreply.github.com> Date: Sat, 6 Jan 2024 18:04:24 -0500 Subject: [PATCH] Add non-throwing result for non-crucial values --- TwitchDownloaderCore/Tools/M3U8.cs | 35 ++++++++++++++++++++++-------- 1 file changed, 26 insertions(+), 9 deletions(-) diff --git a/TwitchDownloaderCore/Tools/M3U8.cs b/TwitchDownloaderCore/Tools/M3U8.cs index f166dd88..900b5470 100644 --- a/TwitchDownloaderCore/Tools/M3U8.cs +++ b/TwitchDownloaderCore/Tools/M3U8.cs @@ -1,5 +1,7 @@ using System; using System.Collections.Generic; +using System.Diagnostics.CodeAnalysis; +using System.Globalization; using System.IO; using System.Text; using System.Text.RegularExpressions; @@ -149,7 +151,7 @@ private static bool ParseM3U8Key(ReadOnlySpan text, Metadata.Builder metad } else if (text.StartsWith(PROGRAM_DATE_TIME_KEY)) { - extProgramDateTime = ParsingHelpers.ParseDateTimeOffset(text, PROGRAM_DATE_TIME_KEY); + extProgramDateTime = ParsingHelpers.ParseDateTimeOffset(text, PROGRAM_DATE_TIME_KEY, false); } else if (text.StartsWith(Stream.ExtByteRange.BYTE_RANGE_KEY)) { @@ -588,11 +590,11 @@ public static ExtStreamInfo Parse(ReadOnlySpan text) if (text.StartsWith(KEY_PROGRAM_ID)) { - streamInfo.ProgramId = ParsingHelpers.ParseIntValue(text, KEY_PROGRAM_ID); + streamInfo.ProgramId = ParsingHelpers.ParseIntValue(text, KEY_PROGRAM_ID, false); } else if (text.StartsWith(KEY_BANDWIDTH)) { - streamInfo.Bandwidth = ParsingHelpers.ParseIntValue(text, KEY_BANDWIDTH); + streamInfo.Bandwidth = ParsingHelpers.ParseIntValue(text, KEY_BANDWIDTH, false); } else if (text.StartsWith(KEY_CODECS)) { @@ -608,7 +610,7 @@ public static ExtStreamInfo Parse(ReadOnlySpan text) } else if (text.StartsWith(KEY_FRAMERATE)) { - streamInfo.Framerate = ParsingHelpers.ParseDecimalValue(text, KEY_FRAMERATE); + streamInfo.Framerate = ParsingHelpers.ParseDecimalValue(text, KEY_FRAMERATE, false); } var nextIndex = text.UnEscapedIndexOf(','); @@ -711,7 +713,7 @@ public static string ParseStringValue(ReadOnlySpan text, ReadOnlySpan text, ReadOnlySpan keyName) + public static int ParseIntValue(ReadOnlySpan text, ReadOnlySpan keyName, bool strict = true) { var temp = text[keyName.Length..]; temp = temp[..NextKeyStart(temp)]; @@ -719,10 +721,13 @@ public static int ParseIntValue(ReadOnlySpan text, ReadOnlySpan keyN if (int.TryParse(temp, NumberStyles.Integer, CultureInfo.InvariantCulture, out var intValue)) return intValue; + if (!strict) + return default; + throw new FormatException($"Unable to parse integer from: {text}"); } - public static uint ParseUIntValue(ReadOnlySpan text, ReadOnlySpan keyName) + public static uint ParseUIntValue(ReadOnlySpan text, ReadOnlySpan keyName, bool strict = true) { var temp = text[keyName.Length..]; temp = temp[..NextKeyStart(temp)]; @@ -730,10 +735,13 @@ public static uint ParseUIntValue(ReadOnlySpan text, ReadOnlySpan ke if (uint.TryParse(temp, NumberStyles.Integer, CultureInfo.InvariantCulture, out var uIntValue)) return uIntValue; + if (!strict) + return default; + throw new FormatException($"Unable to parse integer from: {text}"); } - public static decimal ParseDecimalValue(ReadOnlySpan text, ReadOnlySpan keyName) + public static decimal ParseDecimalValue(ReadOnlySpan text, ReadOnlySpan keyName, bool strict = true) { var temp = text[keyName.Length..]; temp = temp[..NextKeyStart(temp)]; @@ -741,10 +749,13 @@ public static decimal ParseDecimalValue(ReadOnlySpan text, ReadOnlySpan text, ReadOnlySpan keyName) + public static bool ParseBooleanValue(ReadOnlySpan text, ReadOnlySpan keyName, bool strict = true) { var temp = text[keyName.Length..]; @@ -759,6 +770,9 @@ public static bool ParseBooleanValue(ReadOnlySpan text, ReadOnlySpan if (bool.TryParse(temp, out var booleanValue)) return booleanValue; + if (!strict) + return default; + throw new FormatException($"Unable to parse boolean from: {text}"); } @@ -770,7 +784,7 @@ public static Stream.ExtStreamInfo.StreamResolution ParseResolution(ReadOnlySpan return Stream.ExtStreamInfo.StreamResolution.Parse(temp); } - public static DateTimeOffset ParseDateTimeOffset(ReadOnlySpan text, ReadOnlySpan keyName) + public static DateTimeOffset ParseDateTimeOffset(ReadOnlySpan text, ReadOnlySpan keyName, bool strict = true) { var temp = text[keyName.Length..]; temp = temp[..NextKeyStart(temp)]; @@ -778,6 +792,9 @@ public static DateTimeOffset ParseDateTimeOffset(ReadOnlySpan text, ReadOn if (DateTimeOffset.TryParse(temp, out var dateTimeOffset)) return dateTimeOffset; + if (!strict) + return default; + throw new FormatException($"Unable to parse DateTimeOffset from: {text}"); }