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.

85 lines
3.0 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(UnityGame.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. while (stream.Position < stream.Length)
  37. {
  38. var current = (char)reader.ReadByte();
  39. if (char.IsDigit(current))
  40. break;
  41. }
  42. var rewind = -sizeof(int) - sizeof(byte);
  43. stream.Seek(rewind, SeekOrigin.Current); // rewind to the string length
  44. var strlen = reader.ReadInt32();
  45. var strbytes = reader.ReadBytes(strlen);
  46. return Encoding.UTF8.GetString(strbytes);
  47. }
  48. }
  49. internal static AlmostVersion SafeParseVersion() => new AlmostVersion(GetGameVersion());
  50. private static void _Load()
  51. {
  52. UnityGame.SetEarlyGameVersion(SafeParseVersion());
  53. UnityGame.CheckGameVersionBoundary();
  54. }
  55. internal static void Load()
  56. {
  57. // This exists for the same reason the wierdness in Injector.Main does
  58. _ = Type.GetType("SemVer.Version, SemVer", false);
  59. _Load();
  60. }
  61. internal static readonly char[] IllegalCharacters = new char[]
  62. {
  63. '<', '>', ':', '/', '\\', '|', '?', '*', '"',
  64. '\u0000', '\u0001', '\u0002', '\u0003', '\u0004', '\u0005', '\u0006', '\u0007',
  65. '\u0008', '\u0009', '\u000a', '\u000b', '\u000c', '\u000d', '\u000e', '\u000d',
  66. '\u000f', '\u0010', '\u0011', '\u0012', '\u0013', '\u0014', '\u0015', '\u0016',
  67. '\u0017', '\u0018', '\u0019', '\u001a', '\u001b', '\u001c', '\u001d', '\u001f',
  68. };
  69. }
  70. }