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.

86 lines
3.0 KiB

  1. #nullable enable
  2. using IPA.Utilities;
  3. using System;
  4. using System.Collections.Generic;
  5. using System.IO;
  6. using System.Linq;
  7. using System.Text;
  8. #if NET3
  9. using Net3_Proxy;
  10. using Path = Net3_Proxy.Path;
  11. using File = Net3_Proxy.File;
  12. using Directory = Net3_Proxy.Directory;
  13. #endif
  14. namespace IPA.Injector
  15. {
  16. internal static class GameVersionEarly
  17. {
  18. internal static string ResolveDataPath(string installDir) =>
  19. Directory.EnumerateDirectories(installDir, "*_Data").First();
  20. internal static string GlobalGameManagers(string installDir) =>
  21. Path.Combine(ResolveDataPath(installDir), "globalgamemanagers");
  22. internal static string GetGameVersion()
  23. {
  24. var mgr = GlobalGameManagers(UnityGame.InstallPath);
  25. using (var stream = File.OpenRead(mgr))
  26. using (var reader = new BinaryReader(stream, Encoding.UTF8))
  27. {
  28. const string key = "public.app-category.games";
  29. int pos = 0;
  30. while (stream.Position < stream.Length && pos < key.Length)
  31. {
  32. if (reader.ReadByte() == key[pos]) pos++;
  33. else pos = 0;
  34. }
  35. if (stream.Position == stream.Length) // we went through the entire stream without finding the key
  36. throw new KeyNotFoundException("Could not find key '" + key + "' in " + mgr);
  37. while (stream.Position < stream.Length)
  38. {
  39. var current = (char)reader.ReadByte();
  40. if (char.IsDigit(current))
  41. break;
  42. }
  43. var rewind = -sizeof(int) - sizeof(byte);
  44. _ = stream.Seek(rewind, SeekOrigin.Current); // rewind to the string length
  45. var strlen = reader.ReadInt32();
  46. var strbytes = reader.ReadBytes(strlen);
  47. return Encoding.UTF8.GetString(strbytes);
  48. }
  49. }
  50. internal static AlmostVersion SafeParseVersion() => new(GetGameVersion());
  51. private static void _Load()
  52. {
  53. UnityGame.SetEarlyGameVersion(SafeParseVersion());
  54. UnityGame.CheckGameVersionBoundary();
  55. }
  56. internal static void Load()
  57. {
  58. // This exists for the same reason the wierdness in Injector.Main does
  59. _ = Type.GetType("SemVer.Version, SemVer", false);
  60. _Load();
  61. }
  62. internal static readonly char[] IllegalCharacters = new char[]
  63. {
  64. '<', '>', ':', '/', '\\', '|', '?', '*', '"',
  65. '\u0000', '\u0001', '\u0002', '\u0003', '\u0004', '\u0005', '\u0006', '\u0007',
  66. '\u0008', '\u0009', '\u000a', '\u000b', '\u000c', '\u000d', '\u000e', '\u000d',
  67. '\u000f', '\u0010', '\u0011', '\u0012', '\u0013', '\u0014', '\u0015', '\u0016',
  68. '\u0017', '\u0018', '\u0019', '\u001a', '\u001b', '\u001c', '\u001d', '\u001f',
  69. };
  70. }
  71. }