From ffaebbea2005567ffcd6f9b491bf728711f4f4fd Mon Sep 17 00:00:00 2001 From: Meivyn <793322+Meivyn@users.noreply.github.com> Date: Thu, 15 Aug 2024 15:38:16 -0400 Subject: [PATCH] Fix game version parsing (#30) --- .../BeatSaber/BeatSaberGameVersionProvider.cs | 46 +++++++++++++++---- 1 file changed, 37 insertions(+), 9 deletions(-) diff --git a/BeatSaberModManager/Services/Implementations/BeatSaber/BeatSaberGameVersionProvider.cs b/BeatSaberModManager/Services/Implementations/BeatSaber/BeatSaberGameVersionProvider.cs index f34b5c2..ad06838 100644 --- a/BeatSaberModManager/Services/Implementations/BeatSaber/BeatSaberGameVersionProvider.cs +++ b/BeatSaberModManager/Services/Implementations/BeatSaber/BeatSaberGameVersionProvider.cs @@ -1,4 +1,4 @@ -using System.IO; +using System.IO; using System.Text; using System.Text.RegularExpressions; using System.Threading.Tasks; @@ -27,12 +27,10 @@ public partial class BeatSaberGameVersionProvider : IGameVersionProvider SearchForKey(reader, key); if (fileStream.Position == fileStream.Length) return null; // we went through the entire stream without finding the key - SearchForDigit(reader); + (long startIndex, long endIndex) = SearchForVersion(reader); - const int rewind = -sizeof(int) - sizeof(byte); - fileStream.Seek(rewind, SeekOrigin.Current); // rewind to the string length - - int length = reader.ReadInt32(); + int length = (int)(endIndex - startIndex); + _ = fileStream.Seek(-length, SeekOrigin.Current); // rewind to the string length byte[] bytes = reader.ReadBytes(length); string str = Encoding.UTF8.GetString(bytes); Regex regex = VersionRegex(); @@ -50,14 +48,44 @@ private static void SearchForKey(BinaryReader reader, string key) } } - private static void SearchForDigit(BinaryReader reader) + private static (long, long) SearchForVersion(BinaryReader reader) { - while (reader.BaseStream.Position < reader.BaseStream.Length) + long startIndex = 0; + long endIndex = 0; + Stream stream = reader.BaseStream; + long streamLength = stream.Length; + while (stream.Position < streamLength && endIndex == 0) { char current = (char)reader.ReadByte(); if (char.IsDigit(current)) - break; + { + startIndex = stream.Position - 1; + int dotCount = 0; + + while (stream.Position < streamLength) + { + current = (char)reader.ReadByte(); + if (char.IsDigit(current)) + { + if (dotCount == 2 && stream.Position < streamLength && !char.IsDigit((char)reader.PeekChar())) + { + endIndex = stream.Position; + break; + } + } + else if (current == '.') + { + dotCount++; + } + else + { + break; + } + } + } } + + return (startIndex, endIndex); } [GeneratedRegex(@"[\d]+.[\d]+.[\d]+")]