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.

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