Skip to content

Fixed version detection for newer game versions. #547

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 6 commits into from
Oct 28, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
Loading