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.

103 lines
3.8 KiB

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