You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

54 lines
1.9 KiB

  1. using IPA.Utilities;
  2. using System.Collections.Generic;
  3. using System.IO;
  4. using System.Linq;
  5. using System.Text;
  6. #if NET3
  7. using Net3_Proxy;
  8. using Path = Net3_Proxy.Path;
  9. using File = Net3_Proxy.File;
  10. using Directory = Net3_Proxy.Directory;
  11. #endif
  12. namespace IPA.Injector
  13. {
  14. internal static class GameVersionEarly
  15. {
  16. private static string ResolveDataPath(string installDir) =>
  17. Path.Combine(Directory.EnumerateDirectories(installDir, "*_Data").First(), "globalgamemanagers");
  18. internal static string GetGameVersion()
  19. {
  20. var mgr = ResolveDataPath(BeatSaber.InstallPath);
  21. using (var stream = File.OpenRead(mgr))
  22. using (var reader = new BinaryReader(stream, Encoding.UTF8))
  23. {
  24. const string key = "public.app-category.games";
  25. int pos = 0;
  26. while (stream.Position < stream.Length && pos < key.Length)
  27. {
  28. if (reader.ReadByte() == key[pos]) pos++;
  29. else pos = 0;
  30. }
  31. if (stream.Position == stream.Length) // we went through the entire stream without finding the key
  32. throw new KeyNotFoundException("Could not find key '" + key + "' in " + mgr);
  33. // otherwise pos == key.Length, which means we found it
  34. int offset = 136 - key.Length - sizeof(int);
  35. stream.Seek(offset, SeekOrigin.Current); // advance past junk to beginning of string
  36. int strlen = reader.ReadInt32(); // assumes LE
  37. var strbytes = reader.ReadBytes(strlen);
  38. return Encoding.UTF8.GetString(strbytes);
  39. }
  40. }
  41. internal static SemVer.Version SafeParseVersion() => new SemVer.Version(GetGameVersion(), true);
  42. internal static void Load() => BeatSaber.SetEarlyGameVersion(SafeParseVersion());
  43. }
  44. }