Skip to content

Commit eb4bd42

Browse files
committed
Fixed version detection.
1 parent be0a115 commit eb4bd42

File tree

2 files changed

+24
-27
lines changed

2 files changed

+24
-27
lines changed

ModAssistant/Classes/Utils.cs

Lines changed: 23 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -244,36 +244,37 @@ public static string GetSteamDir()
244244
return null;
245245
}
246246

247-
public static string GetVersion()
247+
public static async Task<string> GetVersion()
248248
{
249+
string result = string.Empty;
250+
251+
var versions = await GetVersionsList();
252+
249253
string filename = Path.Combine(App.BeatSaberInstallDirectory, "Beat Saber_Data", "globalgamemanagers");
250254
using (var stream = File.OpenRead(filename))
251-
using (var reader = new BinaryReader(stream, Encoding.UTF8))
255+
using (var reader = new StreamReader(stream, Encoding.UTF8))
252256
{
253-
const string key = "public.app-category.games";
254-
int pos = 0;
257+
var line = reader.ReadLine();
255258

256-
while (stream.Position < stream.Length && pos < key.Length)
259+
while (line != null)
257260
{
258-
if (reader.ReadByte() == key[pos]) pos++;
259-
else pos = 0;
260-
}
261-
262-
if (stream.Position == stream.Length) // we went through the entire stream without finding the key
263-
return null;
264-
265-
while (stream.Position < stream.Length)
266-
{
267-
var current = (char)reader.ReadByte();
268-
if (char.IsDigit(current))
269-
break;
261+
foreach (var version in versions)
262+
{
263+
if (line.Contains(version))
264+
{
265+
result = version;
266+
break;
267+
}
268+
}
269+
if (!string.IsNullOrEmpty(result)) break;
270+
line = reader.ReadLine();
270271
}
271272

272-
var rewind = -sizeof(int) - sizeof(byte);
273-
stream.Seek(rewind, SeekOrigin.Current); // rewind to the string length
274-
275-
var strlen = reader.ReadInt32();
276-
var strbytes = reader.ReadBytes(strlen);
273+
////There is one version ending in "p1" on BeatMods
274+
var filteredVersionMatch = Regex.Match(result, @"[\d]+.[\d]+.[\d]+(p1)?");
275+
return filteredVersionMatch.Success ? filteredVersionMatch.Value : result;
276+
}
277+
}
277278

278279
// TODO: should cache this
279280
public static async Task<List<string>> GetVersionsList()

ModAssistant/MainWindow.xaml.cs

Lines changed: 1 addition & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -118,13 +118,9 @@ private async void LoadVersionsAsync()
118118

119119
var resp = await HttpClient.GetAsync(Utils.Constants.BeatModsAlias);
120120
var body = await resp.Content.ReadAsStringAsync();
121-
List<string> versions = JsonSerializer.Deserialize<string[]>(body).ToList();
122-
123-
resp = await HttpClient.GetAsync(Utils.Constants.BeatModsAlias);
124-
body = await resp.Content.ReadAsStringAsync();
125121
Dictionary<string, string[]> aliases = JsonSerializer.Deserialize<Dictionary<string, string[]>>(body);
126122

127-
string version = Utils.GetVersion();
123+
string version = await Utils.GetVersion();
128124
if (!versions.Contains(version) && CheckAliases(versions, aliases, version) == string.Empty)
129125
{
130126
versions.Insert(0, version);

0 commit comments

Comments
 (0)