From be0a1152e372933a572c7a4926a6389625f764d4 Mon Sep 17 00:00:00 2001 From: DeadlyKitten Date: Wed, 23 Oct 2024 17:58:45 -0700 Subject: [PATCH 1/6] Added utility function to fetch the versions list from BeatMods. --- ModAssistant/Classes/Utils.cs | 12 +++++++----- ModAssistant/MainWindow.xaml.cs | 4 +++- 2 files changed, 10 insertions(+), 6 deletions(-) diff --git a/ModAssistant/Classes/Utils.cs b/ModAssistant/Classes/Utils.cs index 567a2a92..686c17f9 100644 --- a/ModAssistant/Classes/Utils.cs +++ b/ModAssistant/Classes/Utils.cs @@ -275,12 +275,14 @@ public static string GetVersion() var strlen = reader.ReadInt32(); var strbytes = reader.ReadBytes(strlen); - var version = Encoding.UTF8.GetString(strbytes); + // TODO: should cache this + public static async Task> GetVersionsList() + { + var resp = await HttpClient.GetAsync(Utils.Constants.BeatModsVersions); + var body = await resp.Content.ReadAsStringAsync(); + List versions = JsonSerializer.Deserialize(body).ToList(); - //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; } public static string GetOculusDir() diff --git a/ModAssistant/MainWindow.xaml.cs b/ModAssistant/MainWindow.xaml.cs index 1a6c2ec9..a2a5a40b 100644 --- a/ModAssistant/MainWindow.xaml.cs +++ b/ModAssistant/MainWindow.xaml.cs @@ -114,7 +114,9 @@ private async void LoadVersionsAsync() { try { - var resp = await HttpClient.GetAsync(Utils.Constants.BeatModsVersions); + var versions = await Utils.GetVersionsList(); + + var resp = await HttpClient.GetAsync(Utils.Constants.BeatModsAlias); var body = await resp.Content.ReadAsStringAsync(); List versions = JsonSerializer.Deserialize(body).ToList(); From eb4bd423f71b872412251c305f15114b4fd6307b Mon Sep 17 00:00:00 2001 From: DeadlyKitten Date: Wed, 23 Oct 2024 17:59:01 -0700 Subject: [PATCH 2/6] Fixed version detection. --- ModAssistant/Classes/Utils.cs | 45 +++++++++++++++++---------------- ModAssistant/MainWindow.xaml.cs | 6 +---- 2 files changed, 24 insertions(+), 27 deletions(-) diff --git a/ModAssistant/Classes/Utils.cs b/ModAssistant/Classes/Utils.cs index 686c17f9..6e0bceee 100644 --- a/ModAssistant/Classes/Utils.cs +++ b/ModAssistant/Classes/Utils.cs @@ -244,36 +244,37 @@ public static string GetSteamDir() return null; } - public static string GetVersion() + public static async Task GetVersion() { + string result = string.Empty; + + var versions = await GetVersionsList(); + 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; - } - - if (stream.Position == stream.Length) // we went through the entire stream without finding the key - return null; - - while (stream.Position < stream.Length) - { - var current = (char)reader.ReadByte(); - if (char.IsDigit(current)) - break; + foreach (var version in versions) + { + if (line.Contains(version)) + { + result = version; + break; + } + } + if (!string.IsNullOrEmpty(result)) break; + line = reader.ReadLine(); } - var rewind = -sizeof(int) - sizeof(byte); - stream.Seek(rewind, SeekOrigin.Current); // rewind to the string length - - var strlen = reader.ReadInt32(); - var strbytes = reader.ReadBytes(strlen); + ////There is one version ending in "p1" on BeatMods + var filteredVersionMatch = Regex.Match(result, @"[\d]+.[\d]+.[\d]+(p1)?"); + return filteredVersionMatch.Success ? filteredVersionMatch.Value : result; + } + } // TODO: should cache this public static async Task> GetVersionsList() diff --git a/ModAssistant/MainWindow.xaml.cs b/ModAssistant/MainWindow.xaml.cs index a2a5a40b..346cc182 100644 --- a/ModAssistant/MainWindow.xaml.cs +++ b/ModAssistant/MainWindow.xaml.cs @@ -118,13 +118,9 @@ private async void LoadVersionsAsync() var resp = await HttpClient.GetAsync(Utils.Constants.BeatModsAlias); var body = await resp.Content.ReadAsStringAsync(); - List versions = JsonSerializer.Deserialize(body).ToList(); - - resp = await HttpClient.GetAsync(Utils.Constants.BeatModsAlias); - body = await resp.Content.ReadAsStringAsync(); Dictionary aliases = JsonSerializer.Deserialize>(body); - string version = Utils.GetVersion(); + string version = await Utils.GetVersion(); if (!versions.Contains(version) && CheckAliases(versions, aliases, version) == string.Empty) { versions.Insert(0, version); From cc81ae00c267cae2780718b8aa7224884b430933 Mon Sep 17 00:00:00 2001 From: DeadlyKitten Date: Sun, 27 Oct 2024 14:11:57 -0700 Subject: [PATCH 3/6] Add method to fetch alias dictionary from BeatMods. --- ModAssistant/Classes/Utils.cs | 9 +++++++++ ModAssistant/MainWindow.xaml.cs | 5 +---- 2 files changed, 10 insertions(+), 4 deletions(-) diff --git a/ModAssistant/Classes/Utils.cs b/ModAssistant/Classes/Utils.cs index 6e0bceee..e8afda7f 100644 --- a/ModAssistant/Classes/Utils.cs +++ b/ModAssistant/Classes/Utils.cs @@ -286,6 +286,15 @@ public static async Task> GetVersionsList() return versions; } + // TODO: should cache this + public static async Task> GetAliasDictionary() + { + var resp = await HttpClient.GetAsync(Constants.BeatModsAlias); + var body = await resp.Content.ReadAsStringAsync(); + var aliases = JsonSerializer.Deserialize>(body); + + return aliases; + } public static string GetOculusDir() { string OculusInstall = RegistryKey.OpenBaseKey(RegistryHive.LocalMachine, RegistryView.Registry64)?.OpenSubKey("SOFTWARE")?.OpenSubKey("Wow6432Node")?.OpenSubKey("Oculus VR, LLC")?.OpenSubKey("Oculus")?.OpenSubKey("Config")?.GetValue("InitialAppLibrary").ToString(); diff --git a/ModAssistant/MainWindow.xaml.cs b/ModAssistant/MainWindow.xaml.cs index 346cc182..0af7de24 100644 --- a/ModAssistant/MainWindow.xaml.cs +++ b/ModAssistant/MainWindow.xaml.cs @@ -115,10 +115,7 @@ private async void LoadVersionsAsync() try { var versions = await Utils.GetVersionsList(); - - var resp = await HttpClient.GetAsync(Utils.Constants.BeatModsAlias); - var body = await resp.Content.ReadAsStringAsync(); - Dictionary aliases = JsonSerializer.Deserialize>(body); + var aliases = await Utils.GetAliasDictionary(); string version = await Utils.GetVersion(); if (!versions.Contains(version) && CheckAliases(versions, aliases, version) == string.Empty) From e64be910ca8838ea646c3eaba27f4d81ae753b5a Mon Sep 17 00:00:00 2001 From: DeadlyKitten Date: Sun, 27 Oct 2024 14:12:30 -0700 Subject: [PATCH 4/6] Add utility method to fetch all possible version numbers. --- ModAssistant/Classes/Utils.cs | 9 +++++++++ 1 file changed, 9 insertions(+) diff --git a/ModAssistant/Classes/Utils.cs b/ModAssistant/Classes/Utils.cs index e8afda7f..165f1f97 100644 --- a/ModAssistant/Classes/Utils.cs +++ b/ModAssistant/Classes/Utils.cs @@ -295,6 +295,15 @@ public static async Task> GetAliasDictionary() return aliases; } + + public static async Task> GetAllPossibleVersions() + { + var versions = await GetVersionsList(); + var aliases = await GetAliasDictionary(); + + return versions.Concat(aliases.SelectMany(x => x.Value)).ToList(); + } + public static string GetOculusDir() { string OculusInstall = RegistryKey.OpenBaseKey(RegistryHive.LocalMachine, RegistryView.Registry64)?.OpenSubKey("SOFTWARE")?.OpenSubKey("Wow6432Node")?.OpenSubKey("Oculus VR, LLC")?.OpenSubKey("Oculus")?.OpenSubKey("Config")?.GetValue("InitialAppLibrary").ToString(); From 7b8caa36326f3240152641823974a32f5fc31eb3 Mon Sep 17 00:00:00 2001 From: DeadlyKitten Date: Sun, 27 Oct 2024 14:13:15 -0700 Subject: [PATCH 5/6] Small simplification. --- ModAssistant/Classes/Utils.cs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/ModAssistant/Classes/Utils.cs b/ModAssistant/Classes/Utils.cs index 165f1f97..df42caca 100644 --- a/ModAssistant/Classes/Utils.cs +++ b/ModAssistant/Classes/Utils.cs @@ -279,7 +279,7 @@ public static async Task GetVersion() // TODO: should cache this public static async Task> GetVersionsList() { - var resp = await HttpClient.GetAsync(Utils.Constants.BeatModsVersions); + var resp = await HttpClient.GetAsync(Constants.BeatModsVersions); var body = await resp.Content.ReadAsStringAsync(); List versions = JsonSerializer.Deserialize(body).ToList(); From 022eff9c25e36e2cd042fc54a08fce63980b5d13 Mon Sep 17 00:00:00 2001 From: DeadlyKitten <9684760+DeadlyKitten@users.noreply.github.com> Date: Sun, 27 Oct 2024 18:21:37 -0700 Subject: [PATCH 6/6] Whoops! Forgot to actually use the new method. --- ModAssistant/Classes/Utils.cs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/ModAssistant/Classes/Utils.cs b/ModAssistant/Classes/Utils.cs index df42caca..7d8bab2f 100644 --- a/ModAssistant/Classes/Utils.cs +++ b/ModAssistant/Classes/Utils.cs @@ -248,7 +248,7 @@ public static async Task GetVersion() { string result = string.Empty; - var versions = await GetVersionsList(); + var versions = await GetAllPossibleVersions(); string filename = Path.Combine(App.BeatSaberInstallDirectory, "Beat Saber_Data", "globalgamemanagers"); using (var stream = File.OpenRead(filename))