Skip to content

Commit 35a5afb

Browse files
authored
Merge pull request #547 from DeadlyKitten/fix/version-detection
Fixed version detection for newer game versions.
2 parents be6fa51 + 022eff9 commit 35a5afb

File tree

2 files changed

+48
-32
lines changed

2 files changed

+48
-32
lines changed

ModAssistant/Classes/Utils.cs

Lines changed: 45 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -244,43 +244,64 @@ 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 GetAllPossibleVersions();
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;
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();
260271
}
261272

262-
if (stream.Position == stream.Length) // we went through the entire stream without finding the key
263-
return null;
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+
}
264278

265-
while (stream.Position < stream.Length)
266-
{
267-
var current = (char)reader.ReadByte();
268-
if (char.IsDigit(current))
269-
break;
270-
}
279+
// TODO: should cache this
280+
public static async Task<List<string>> GetVersionsList()
281+
{
282+
var resp = await HttpClient.GetAsync(Constants.BeatModsVersions);
283+
var body = await resp.Content.ReadAsStringAsync();
284+
List<string> versions = JsonSerializer.Deserialize<string[]>(body).ToList();
285+
286+
return versions;
287+
}
271288

272-
var rewind = -sizeof(int) - sizeof(byte);
273-
stream.Seek(rewind, SeekOrigin.Current); // rewind to the string length
289+
// TODO: should cache this
290+
public static async Task<Dictionary<string, string[]>> GetAliasDictionary()
291+
{
292+
var resp = await HttpClient.GetAsync(Constants.BeatModsAlias);
293+
var body = await resp.Content.ReadAsStringAsync();
294+
var aliases = JsonSerializer.Deserialize<Dictionary<string, string[]>>(body);
274295

275-
var strlen = reader.ReadInt32();
276-
var strbytes = reader.ReadBytes(strlen);
296+
return aliases;
297+
}
277298

278-
var version = Encoding.UTF8.GetString(strbytes);
299+
public static async Task<List<string>> GetAllPossibleVersions()
300+
{
301+
var versions = await GetVersionsList();
302+
var aliases = await GetAliasDictionary();
279303

280-
//There is one version ending in "p1" on BeatMods
281-
var filteredVersionMatch = Regex.Match(version, @"[\d]+.[\d]+.[\d]+(p1)?");
282-
return filteredVersionMatch.Success ? filteredVersionMatch.Value : version;
283-
}
304+
return versions.Concat(aliases.SelectMany(x => x.Value)).ToList();
284305
}
285306

286307
public static string GetOculusDir()

ModAssistant/MainWindow.xaml.cs

Lines changed: 3 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -114,15 +114,10 @@ private async void LoadVersionsAsync()
114114
{
115115
try
116116
{
117-
var resp = await HttpClient.GetAsync(Utils.Constants.BeatModsVersions);
118-
var body = await resp.Content.ReadAsStringAsync();
119-
List<string> versions = JsonSerializer.Deserialize<string[]>(body).ToList();
117+
var versions = await Utils.GetVersionsList();
118+
var aliases = await Utils.GetAliasDictionary();
120119

121-
resp = await HttpClient.GetAsync(Utils.Constants.BeatModsAlias);
122-
body = await resp.Content.ReadAsStringAsync();
123-
Dictionary<string, string[]> aliases = JsonSerializer.Deserialize<Dictionary<string, string[]>>(body);
124-
125-
string version = Utils.GetVersion();
120+
string version = await Utils.GetVersion();
126121
if (!versions.Contains(version) && CheckAliases(versions, aliases, version) == string.Empty)
127122
{
128123
versions.Insert(0, version);

0 commit comments

Comments
 (0)