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.

127 lines
4.8 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. var OldVersion = lastVerS != null ? new AlmostVersion(lastVerS, gameVer) : null;
  53. IsGameVersionBoundary = OldVersion != null && gameVer != OldVersion;
  54. SelfConfig.Instance.LastGameVersion = gameVer.ToString();
  55. }
  56. /// <summary>
  57. /// The different types of releases of the game.
  58. /// </summary>
  59. public enum Release
  60. {
  61. /// <summary>
  62. /// Indicates a Steam release.
  63. /// </summary>
  64. Steam,
  65. /// <summary>
  66. /// Indicates an Oculus release.
  67. /// </summary>
  68. Oculus
  69. }
  70. private static Release? _releaseCache;
  71. /// <summary>
  72. /// Gets the <see cref="Release"/> type of this installation of Beat Saber
  73. /// </summary>
  74. /// <value>the type of release this is</value>
  75. public static Release ReleaseType => (_releaseCache ?? (_releaseCache = FindSteamVRAsset() ? Release.Steam : Release.Oculus)).Value;
  76. private static string _installRoot;
  77. /// <summary>
  78. /// Gets the path to the Beat Saber install directory.
  79. /// </summary>
  80. /// <value>the path of the game install directory</value>
  81. public static string InstallPath
  82. {
  83. get
  84. {
  85. if (_installRoot == null)
  86. _installRoot = Path.GetFullPath(
  87. Path.Combine(Path.GetDirectoryName(Assembly.GetExecutingAssembly().Location), "..", ".."));
  88. return _installRoot;
  89. }
  90. }
  91. /// <summary>
  92. /// The path to the `Libs` folder. Use only if necessary.
  93. /// </summary>
  94. /// <value>the path to the library directory</value>
  95. public static string LibraryPath => Path.Combine(InstallPath, "Libs");
  96. /// <summary>
  97. /// The path to the `Libs\Native` folder. Use only if necessary.
  98. /// </summary>
  99. /// <value>the path to the native library directory</value>
  100. public static string NativeLibraryPath => Path.Combine(LibraryPath, "Native");
  101. /// <summary>
  102. /// The directory to load plugins from.
  103. /// </summary>
  104. /// <value>the path to the plugin directory</value>
  105. public static string PluginsPath => Path.Combine(InstallPath, "Plugins");
  106. /// <summary>
  107. /// The path to the `UserData` folder.
  108. /// </summary>
  109. /// <value>the path to the user data directory</value>
  110. public static string UserDataPath => Path.Combine(InstallPath, "UserData");
  111. private static bool FindSteamVRAsset()
  112. {
  113. // these require assembly qualified names....
  114. var steamUser = Type.GetType("Steamworks.SteamUser, Assembly-CSharp-firstpass, Version=0.0.0.0, Culture=neutral, PublicKeyToken=null", false);
  115. return steamUser != null;
  116. }
  117. }
  118. }