Skip to content

Commit

Permalink
Properly parse exotic versions
Browse files Browse the repository at this point in the history
  • Loading branch information
affederaffe committed Dec 14, 2022
1 parent e3cbaaa commit 81673dc
Show file tree
Hide file tree
Showing 3 changed files with 12 additions and 9 deletions.
2 changes: 1 addition & 1 deletion BeatSaberModManager/BeatSaberModManager.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
<PropertyGroup>
<TargetFramework>net7.0</TargetFramework>
<LangVersion>preview</LangVersion>
<AssemblyVersion>0.0.3</AssemblyVersion>
<AssemblyVersion>0.0.4</AssemblyVersion>
<PublishSingleFile>true</PublishSingleFile>
<AppendTargetFrameworkToOutputPath>false</AppendTargetFrameworkToOutputPath>
<IncludeNativeLibrariesForSelfExtract>true</IncludeNativeLibrariesForSelfExtract>
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
using System.IO;
using System.Text;
using System.Text.RegularExpressions;
using System.Threading.Tasks;

using BeatSaberModManager.Services.Interfaces;
Expand All @@ -9,7 +10,7 @@
namespace BeatSaberModManager.Services.Implementations.BeatSaber
{
/// <inheritdoc />
public class BeatSaberGameVersionProvider : IGameVersionProvider
public partial class BeatSaberGameVersionProvider : IGameVersionProvider
{
/// <inheritdoc />
public async Task<string?> DetectGameVersionAsync(string installDir)
Expand All @@ -22,14 +23,15 @@ public class BeatSaberGameVersionProvider : IGameVersionProvider

SearchForKey(reader, key);
if (fileStream.Position == fileStream.Length) return null; // we went through the entire stream without finding the key
ReadWhileDigit(reader);
SearchForDigit(reader);

const int rewind = -sizeof(int) - sizeof(byte);
fileStream.Seek(rewind, SeekOrigin.Current); // rewind to the string length

int len = reader.ReadInt32();
byte[] bytes = reader.ReadBytes(len);
return Encoding.UTF8.GetString(bytes);
string str = reader.ReadString();
Regex regex = VersionRegex();
Match match = regex.Match(str);
return !match.Success ? null : match.Value;
}

private static void SearchForKey(BinaryReader reader, string key)
Expand All @@ -42,13 +44,16 @@ private static void SearchForKey(BinaryReader reader, string key)
}
}

private static void ReadWhileDigit(BinaryReader reader)
private static void SearchForDigit(BinaryReader reader)
{
while (reader.BaseStream.Position < reader.BaseStream.Length)
{
char current = (char)reader.ReadByte();
if (char.IsDigit(current)) break;
}
}

[GeneratedRegex("[\\d]+.[\\d]+.[\\d]+")]
private static partial Regex VersionRegex();
}
}
2 changes: 0 additions & 2 deletions BeatSaberModManager/Utils/IOUtils.cs
Original file line number Diff line number Diff line change
@@ -1,6 +1,4 @@
using System;
using System.Collections.Generic;
using System.Diagnostics.CodeAnalysis;
using System.IO;
using System.IO.Compression;
using System.Threading.Tasks;
Expand Down

0 comments on commit 81673dc

Please sign in to comment.