Skip to content

Commit

Permalink
Merge pull request #547 from DeadlyKitten/fix/version-detection
Browse files Browse the repository at this point in the history
Fixed version detection for newer game versions.
  • Loading branch information
DeadlyKitten authored Oct 28, 2024
2 parents be6fa51 + 022eff9 commit 35a5afb
Show file tree
Hide file tree
Showing 2 changed files with 48 additions and 32 deletions.
69 changes: 45 additions & 24 deletions ModAssistant/Classes/Utils.cs
Original file line number Diff line number Diff line change
Expand Up @@ -244,43 +244,64 @@ public static string GetSteamDir()
return null;
}

public static string GetVersion()
public static async Task<string> GetVersion()
{
string result = string.Empty;

var versions = await GetAllPossibleVersions();

string filename = Path.Combine(App.BeatSaberInstallDirectory, "Beat Saber_Data", "globalgamemanagers");
using (var stream = File.OpenRead(filename))
using (var reader = new BinaryReader(stream, Encoding.UTF8))
using (var reader = new StreamReader(stream, Encoding.UTF8))
{
const string key = "public.app-category.games";
int pos = 0;
var line = reader.ReadLine();

while (stream.Position < stream.Length && pos < key.Length)
while (line != null)
{
if (reader.ReadByte() == key[pos]) pos++;
else pos = 0;
foreach (var version in versions)
{
if (line.Contains(version))
{
result = version;
break;
}
}
if (!string.IsNullOrEmpty(result)) break;
line = reader.ReadLine();
}

if (stream.Position == stream.Length) // we went through the entire stream without finding the key
return null;
////There is one version ending in "p1" on BeatMods
var filteredVersionMatch = Regex.Match(result, @"[\d]+.[\d]+.[\d]+(p1)?");
return filteredVersionMatch.Success ? filteredVersionMatch.Value : result;
}
}

while (stream.Position < stream.Length)
{
var current = (char)reader.ReadByte();
if (char.IsDigit(current))
break;
}
// TODO: should cache this
public static async Task<List<string>> GetVersionsList()
{
var resp = await HttpClient.GetAsync(Constants.BeatModsVersions);
var body = await resp.Content.ReadAsStringAsync();
List<string> versions = JsonSerializer.Deserialize<string[]>(body).ToList();

return versions;
}

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

var strlen = reader.ReadInt32();
var strbytes = reader.ReadBytes(strlen);
return aliases;
}

var version = Encoding.UTF8.GetString(strbytes);
public static async Task<List<string>> GetAllPossibleVersions()
{
var versions = await GetVersionsList();
var aliases = await GetAliasDictionary();

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

public static string GetOculusDir()
Expand Down
11 changes: 3 additions & 8 deletions ModAssistant/MainWindow.xaml.cs
Original file line number Diff line number Diff line change
Expand Up @@ -114,15 +114,10 @@ private async void LoadVersionsAsync()
{
try
{
var resp = await HttpClient.GetAsync(Utils.Constants.BeatModsVersions);
var body = await resp.Content.ReadAsStringAsync();
List<string> versions = JsonSerializer.Deserialize<string[]>(body).ToList();
var versions = await Utils.GetVersionsList();
var aliases = await Utils.GetAliasDictionary();

resp = await HttpClient.GetAsync(Utils.Constants.BeatModsAlias);
body = await resp.Content.ReadAsStringAsync();
Dictionary<string, string[]> aliases = JsonSerializer.Deserialize<Dictionary<string, string[]>>(body);

string version = Utils.GetVersion();
string version = await Utils.GetVersion();
if (!versions.Contains(version) && CheckAliases(versions, aliases, version) == string.Empty)
{
versions.Insert(0, version);
Expand Down

0 comments on commit 35a5afb

Please sign in to comment.