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.

128 lines
4.9 KiB

  1. using IPA.Config;
  2. using System;
  3. using System.IO;
  4. using System.Reflection;
  5. using UnityEngine;
  6. #if NET3
  7. using Path = Net3_Proxy.Path;
  8. #endif
  9. namespace IPA.Utilities
  10. {
  11. /// <summary>
  12. /// Provides some basic utility methods and properties of Beat Saber
  13. /// </summary>
  14. public static class BeatSaber
  15. {
  16. private static AlmostVersion _gameVersion;
  17. /// <summary>
  18. /// Provides the current game version.
  19. /// </summary>
  20. /// <value>the SemVer version of the game</value>
  21. public static AlmostVersion GameVersion => _gameVersion ?? (_gameVersion = new AlmostVersion(ApplicationVersionProxy));
  22. internal static void SetEarlyGameVersion(AlmostVersion ver)
  23. {
  24. _gameVersion = ver;
  25. Logging.Logger.log.Debug($"GameVersion set early to {ver}");
  26. }
  27. private static string ApplicationVersionProxy => Application.version;
  28. internal static void EnsureRuntimeGameVersion()
  29. {
  30. try
  31. {
  32. var rtVer = new AlmostVersion(ApplicationVersionProxy);
  33. if (!rtVer.Equals(_gameVersion)) // this actually uses stricter equality than == for AlmostVersion
  34. {
  35. Logging.Logger.log.Warn($"Early version {_gameVersion} parsed from game files doesn't match runtime version {rtVer}!");
  36. _gameVersion = rtVer;
  37. }
  38. }
  39. catch (MissingMethodException e)
  40. {
  41. Logging.Logger.log.Error("Application.version was not found! Cannot check early parsed version");
  42. if (SelfConfig.Debug_.ShowHandledErrorStackTraces_)
  43. Logging.Logger.log.Error(e);
  44. }
  45. }
  46. internal static bool IsGameVersionBoundary { get; private set; }
  47. internal static AlmostVersion OldVersion { get; private set; }
  48. internal static void CheckGameVersionBoundary()
  49. {
  50. var gameVer = GameVersion;
  51. var lastVerS = SelfConfig.LastGameVersion_;
  52. OldVersion = lastVerS != null ? new AlmostVersion(lastVerS, gameVer) : null;
  53. IsGameVersionBoundary = OldVersion != null && gameVer != OldVersion;
  54. SelfConfig.SelfConfigRef.Value.LastGameVersion = gameVer.ToString();
  55. SelfConfig.LoaderConfig.Store(SelfConfig.SelfConfigRef.Value);
  56. }
  57. /// <summary>
  58. /// The different types of releases of the game.
  59. /// </summary>
  60. public enum Release
  61. {
  62. /// <summary>
  63. /// Indicates a Steam release.
  64. /// </summary>
  65. Steam,
  66. /// <summary>
  67. /// Indicates an Oculus release.
  68. /// </summary>
  69. Oculus
  70. }
  71. private static Release? _releaseCache;
  72. /// <summary>
  73. /// Gets the <see cref="Release"/> type of this installation of Beat Saber
  74. /// </summary>
  75. /// <value>the type of release this is</value>
  76. public static Release ReleaseType => (_releaseCache ?? (_releaseCache = FindSteamVRAsset() ? Release.Steam : Release.Oculus)).Value;
  77. private static string _installRoot;
  78. /// <summary>
  79. /// Gets the path to the Beat Saber install directory.
  80. /// </summary>
  81. /// <value>the path of the game install directory</value>
  82. public static string InstallPath
  83. {
  84. get
  85. {
  86. if (_installRoot == null)
  87. _installRoot = Path.GetFullPath(
  88. Path.Combine(Path.GetDirectoryName(Assembly.GetExecutingAssembly().Location), "..", ".."));
  89. return _installRoot;
  90. }
  91. }
  92. /// <summary>
  93. /// The path to the `Libs` folder. Use only if necessary.
  94. /// </summary>
  95. /// <value>the path to the library directory</value>
  96. public static string LibraryPath => Path.Combine(InstallPath, "Libs");
  97. /// <summary>
  98. /// The path to the `Libs\Native` folder. Use only if necessary.
  99. /// </summary>
  100. /// <value>the path to the native library directory</value>
  101. public static string NativeLibraryPath => Path.Combine(LibraryPath, "Native");
  102. /// <summary>
  103. /// The directory to load plugins from.
  104. /// </summary>
  105. /// <value>the path to the plugin directory</value>
  106. public static string PluginsPath => Path.Combine(InstallPath, "Plugins");
  107. /// <summary>
  108. /// The path to the `UserData` folder.
  109. /// </summary>
  110. /// <value>the path to the user data directory</value>
  111. public static string UserDataPath => Path.Combine(InstallPath, "UserData");
  112. private static bool FindSteamVRAsset()
  113. {
  114. // these require assembly qualified names....
  115. var steamUser = Type.GetType("Steamworks.SteamUser, Assembly-CSharp-firstpass, Version=0.0.0.0, Culture=neutral, PublicKeyToken=null", false);
  116. return steamUser != null;
  117. }
  118. }
  119. }