@@ -244,43 +244,64 @@ public static string GetSteamDir()
244
244
return null ;
245
245
}
246
246
247
- public static string GetVersion ( )
247
+ public static async Task < string > GetVersion ( )
248
248
{
249
+ string result = string . Empty ;
250
+
251
+ var versions = await GetAllPossibleVersions ( ) ;
252
+
249
253
string filename = Path . Combine ( App . BeatSaberInstallDirectory , "Beat Saber_Data" , "globalgamemanagers" ) ;
250
254
using ( var stream = File . OpenRead ( filename ) )
251
- using ( var reader = new BinaryReader ( stream , Encoding . UTF8 ) )
255
+ using ( var reader = new StreamReader ( stream , Encoding . UTF8 ) )
252
256
{
253
- const string key = "public.app-category.games" ;
254
- int pos = 0 ;
257
+ var line = reader . ReadLine ( ) ;
255
258
256
- while ( stream . Position < stream . Length && pos < key . Length )
259
+ while ( line != null )
257
260
{
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 ( ) ;
260
271
}
261
272
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
+ }
264
278
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
+ }
271
288
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 ) ;
274
295
275
- var strlen = reader . ReadInt32 ( ) ;
276
- var strbytes = reader . ReadBytes ( strlen ) ;
296
+ return aliases ;
297
+ }
277
298
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 ( ) ;
279
303
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 ( ) ;
284
305
}
285
306
286
307
public static string GetOculusDir ( )
0 commit comments