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.

70 lines
2.3 KiB

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