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.

101 lines
3.8 KiB

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