From f69af05535250bd6395f1c2d17b9edb63b4310e3 Mon Sep 17 00:00:00 2001 From: bcssov Date: Wed, 3 Jul 2024 08:24:38 +0200 Subject: [PATCH] Ensure that we extract version number properly --- .../Extensions.Version.cs | 42 +++++++++++++++---- 1 file changed, 33 insertions(+), 9 deletions(-) diff --git a/src/IronyModManager.Shared/Extensions.Version.cs b/src/IronyModManager.Shared/Extensions.Version.cs index 5ada2845..c7c92f0a 100644 --- a/src/IronyModManager.Shared/Extensions.Version.cs +++ b/src/IronyModManager.Shared/Extensions.Version.cs @@ -4,17 +4,19 @@ // Created : 02-16-2020 // // Last Modified By : Mario -// Last Modified On : 07-21-2022 +// Last Modified On : 07-03-2024 // *********************************************************************** // // Mario // // // *********************************************************************** + using System; using System.Collections.Generic; using System.Linq; using System.Text; +using System.Text.RegularExpressions; namespace IronyModManager.Shared { @@ -25,6 +27,16 @@ public static partial class Extensions { #nullable enable + + #region Fields + + /// + /// The version extract + /// + private static readonly Regex versionExtract = new(@"(?:\*|\d+)(?:\.\d+|\.\*)+"); + + #endregion Fields + #region Methods /// @@ -32,8 +44,23 @@ public static partial class Extensions /// /// The value. /// System.Nullable<Shared.Version>. - public static Shared.Version? ToVersion(this string value) + public static Version? ToVersion(this string value) { + if (string.IsNullOrWhiteSpace(value)) + { + return null; + } + + // Detect PDX mess + if (value.Any(char.IsLetter)) + { + var result = versionExtract.Match(value); + if (result.Success) + { + value = result.Value; + } + } + var sb = new StringBuilder(); var count = 0; foreach (var item in value.Split(".")) @@ -43,6 +70,7 @@ public static partial class Extensions { parsed = "*"; } + if (int.TryParse(parsed, out var part)) { sb.Append($"{part}."); @@ -51,17 +79,13 @@ public static partial class Extensions { sb.Append($"{(count > 1 ? int.MaxValue : 0)}."); } + count++; } - if (Shared.Version.TryParse(sb.ToString().Trim().Trim('.'), out var parsedVersion)) - { - return parsedVersion; - } - return null; + + return Version.TryParse(sb.ToString().Trim().Trim('.'), out var parsedVersion) ? parsedVersion : null; } #endregion Methods - -#nullable disable } }